From 2e418e01d13dc78332a13dc4eda0605d645e5307 Mon Sep 17 00:00:00 2001 From: Ian Rodney Date: Thu, 27 Aug 2020 23:09:57 -0700 Subject: [PATCH 1/4] fix profiling for docker --- dashboard/modules/reporter/reporter_agent.py | 4 ++-- python/ray/reporter.py | 4 ++-- python/ray/utils.py | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/dashboard/modules/reporter/reporter_agent.py b/dashboard/modules/reporter/reporter_agent.py index 4e3f7cd55d6d..3526ffc8f734 100644 --- a/dashboard/modules/reporter/reporter_agent.py +++ b/dashboard/modules/reporter/reporter_agent.py @@ -73,9 +73,9 @@ async def GetProfilingStats(self, request, context): duration = request.duration profiling_file_path = os.path.join(ray.utils.get_ray_temp_dir(), "{}_profiling.txt".format(pid)) + sudo = "sudo" if ray.utils.get_user() != "root" else "" process = subprocess.Popen( - "sudo $(which py-spy) record -o {} -p {} -d {} -f speedscope" - .format(profiling_file_path, pid, duration), + f"{sudo} $(which py-spy) record -o {profiling_file_path} -p {pid} -d {duration} -f speedscope", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) diff --git a/python/ray/reporter.py b/python/ray/reporter.py index 3f7295df6536..656c93ebf8ec 100644 --- a/python/ray/reporter.py +++ b/python/ray/reporter.py @@ -43,9 +43,9 @@ def GetProfilingStats(self, request, context): duration = request.duration profiling_file_path = os.path.join(ray.utils.get_ray_temp_dir(), f"{pid}_profiling.txt") + sudo = "sudo" if ray.utils.get_user() != "root" else "" process = subprocess.Popen( - "sudo $(which py-spy) record -o {} -p {} -d {} -f speedscope" - .format(profiling_file_path, pid, duration), + f"{sudo} $(which py-spy) record -o {profiling_file_path} -p {pid} -d {duration} -f speedscope", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) diff --git a/python/ray/utils.py b/python/ray/utils.py index c41c76ba5601..92933ddb77ea 100644 --- a/python/ray/utils.py +++ b/python/ray/utils.py @@ -5,6 +5,7 @@ import logging import numpy as np import os +import pwd import signal import subprocess import sys @@ -780,3 +781,10 @@ def try_to_symlink(symlink_path, target_path): os.symlink(target_path, symlink_path) except OSError: return + + +def get_user(): + try: + return pwd.getpwuid(os.getuid())[0] + except: + return "" From 1e819daf57a5897922faeab22e954bed1e7a71cd Mon Sep 17 00:00:00 2001 From: Ian Rodney Date: Thu, 27 Aug 2020 23:13:19 -0700 Subject: [PATCH 2/4] small fixes --- dashboard/modules/reporter/reporter_agent.py | 3 ++- python/ray/reporter.py | 3 ++- python/ray/utils.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/dashboard/modules/reporter/reporter_agent.py b/dashboard/modules/reporter/reporter_agent.py index 3526ffc8f734..f87569e24e1a 100644 --- a/dashboard/modules/reporter/reporter_agent.py +++ b/dashboard/modules/reporter/reporter_agent.py @@ -75,7 +75,8 @@ async def GetProfilingStats(self, request, context): "{}_profiling.txt".format(pid)) sudo = "sudo" if ray.utils.get_user() != "root" else "" process = subprocess.Popen( - f"{sudo} $(which py-spy) record -o {profiling_file_path} -p {pid} -d {duration} -f speedscope", + (f"{sudo} $(which py-spy) record -o {profiling_file_path} -p {pid}" + f" -d {duration} -f speedscope"), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) diff --git a/python/ray/reporter.py b/python/ray/reporter.py index 656c93ebf8ec..517f4d8ba77e 100644 --- a/python/ray/reporter.py +++ b/python/ray/reporter.py @@ -45,7 +45,8 @@ def GetProfilingStats(self, request, context): f"{pid}_profiling.txt") sudo = "sudo" if ray.utils.get_user() != "root" else "" process = subprocess.Popen( - f"{sudo} $(which py-spy) record -o {profiling_file_path} -p {pid} -d {duration} -f speedscope", + (f"{sudo} $(which py-spy) record -o {profiling_file_path} -p {pid}" + f" -d {duration} -f speedscope"), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) diff --git a/python/ray/utils.py b/python/ray/utils.py index 92933ddb77ea..2e74907f35fe 100644 --- a/python/ray/utils.py +++ b/python/ray/utils.py @@ -786,5 +786,5 @@ def try_to_symlink(symlink_path, target_path): def get_user(): try: return pwd.getpwuid(os.getuid())[0] - except: + except Exception: return "" From 7737ffede5f8e3cb1e478a19855491bea363c429 Mon Sep 17 00:00:00 2001 From: Ian Rodney Date: Fri, 28 Aug 2020 02:10:37 -0700 Subject: [PATCH 3/4] use name --- python/ray/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ray/utils.py b/python/ray/utils.py index 2e74907f35fe..2e8e0cf8d44c 100644 --- a/python/ray/utils.py +++ b/python/ray/utils.py @@ -785,6 +785,6 @@ def try_to_symlink(symlink_path, target_path): def get_user(): try: - return pwd.getpwuid(os.getuid())[0] + return pwd.getpwuid(os.getuid()).pw_name except Exception: return "" From cf65d00bbece1105c2f6faf8a22ab3dd76f49745 Mon Sep 17 00:00:00 2001 From: Ian Rodney Date: Fri, 28 Aug 2020 09:35:29 -0700 Subject: [PATCH 4/4] do not import pwd on windows --- python/ray/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/ray/utils.py b/python/ray/utils.py index 2e8e0cf8d44c..6cb9fb68ea94 100644 --- a/python/ray/utils.py +++ b/python/ray/utils.py @@ -5,7 +5,6 @@ import logging import numpy as np import os -import pwd import signal import subprocess import sys @@ -19,6 +18,10 @@ import ray.ray_constants as ray_constants import psutil +pwd = None +if sys.platform != "win32": + import pwd + logger = logging.getLogger(__name__) # Linux can bind child processes' lifetimes to that of their parents via prctl. @@ -784,6 +787,8 @@ def try_to_symlink(symlink_path, target_path): def get_user(): + if pwd is None: + return "" try: return pwd.getpwuid(os.getuid()).pw_name except Exception: