From d6f2b0d93367150f1bf70b8ed82e4bf1e52a4469 Mon Sep 17 00:00:00 2001 From: Ian Rodney Date: Fri, 28 Aug 2020 21:25:10 -0700 Subject: [PATCH] [docker] Run profiling without sudo (#10388) * fix profiling for docker * small fixes * use name * do not import pwd on windows --- dashboard/modules/reporter/reporter_agent.py | 5 +++-- python/ray/reporter.py | 5 +++-- python/ray/utils.py | 13 +++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/dashboard/modules/reporter/reporter_agent.py b/dashboard/modules/reporter/reporter_agent.py index 4e3f7cd55..f87569e24 100644 --- a/dashboard/modules/reporter/reporter_agent.py +++ b/dashboard/modules/reporter/reporter_agent.py @@ -73,9 +73,10 @@ class ReporterAgent(dashboard_utils.DashboardAgentModule, 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}" + 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 3f7295df6..517f4d8ba 100644 --- a/python/ray/reporter.py +++ b/python/ray/reporter.py @@ -43,9 +43,10 @@ class ReporterServer(reporter_pb2_grpc.ReporterServiceServicer): 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}" + 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 c41c76ba5..6cb9fb68e 100644 --- a/python/ray/utils.py +++ b/python/ray/utils.py @@ -18,6 +18,10 @@ import ray.gcs_utils 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. @@ -780,3 +784,12 @@ def try_to_symlink(symlink_path, target_path): os.symlink(target_path, symlink_path) except OSError: return + + +def get_user(): + if pwd is None: + return "" + try: + return pwd.getpwuid(os.getuid()).pw_name + except Exception: + return ""