From 81c661ab1e517996c6418b9d3d540ee7e92aae35 Mon Sep 17 00:00:00 2001 From: jharper5 Date: Tue, 16 Apr 2024 13:57:21 -0700 Subject: [PATCH] run without root permission --- perf-collect.py | 4 +++- src/perf_helpers.py | 28 +++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/perf-collect.py b/perf-collect.py index e3d95cb..9b04714 100644 --- a/perf-collect.py +++ b/perf-collect.py @@ -295,7 +295,9 @@ def validate_file(fname): sys.exit() if os.geteuid() != 0: - crash("Must run PerfSpect as root, please re-run") + logging.warning( + "PerfSpect requires elevated permissions to run. User is not root. Proceeding anyway..." + ) # disable nmi watchdog before collecting perf nmi_watchdog = perf_helpers.disable_nmi_watchdog() diff --git a/src/perf_helpers.py b/src/perf_helpers.py index c6d4456..e02dfe9 100644 --- a/src/perf_helpers.py +++ b/src/perf_helpers.py @@ -145,7 +145,9 @@ def disable_nmi_watchdog(): proc_output = subprocess.check_output(["cat", "/proc/sys/kernel/nmi_watchdog"]) nmi_watchdog_status = int(proc_output.decode().strip()) if nmi_watchdog_status == 1: - proc_output = subprocess.check_output(["sysctl", "kernel.nmi_watchdog=0"]) + proc_output = subprocess.check_output( + ["sysctl", "kernel.nmi_watchdog=0"], stderr=subprocess.STDOUT + ) new_watchdog_status = int( proc_output.decode().strip().replace("kernel.nmi_watchdog = ", "") ) @@ -158,7 +160,7 @@ def disable_nmi_watchdog(): logging.info("nmi_watchdog already disabled. No change needed.") return nmi_watchdog_status except (ValueError, FileNotFoundError, subprocess.CalledProcessError) as e: - crash(f"Failed to disable nmi_watchdog: {e}") + logging.warning(f"Failed to disable nmi_watchdog: {e}") # enable nmi watchdog @@ -183,15 +185,19 @@ def set_perf_event_mux_interval(reset, interval_ms, mux_interval): if os.path.isdir(dirpath): muxfile = os.path.join(dirpath, "perf_event_mux_interval_ms") if os.path.isfile(muxfile): - with open(muxfile, "w") as f_mux: - val = 0 - if reset: - val = int(mux_interval[f]) - else: - if int(mux_interval[f]): - val = int(interval_ms) - if val: - f_mux.write(str(val)) + try: + with open(muxfile, "w") as f_mux: + val = 0 + if reset: + val = int(mux_interval[f]) + else: + if int(mux_interval[f]): + val = int(interval_ms) + if val: + f_mux.write(str(val)) + except OSError as e: + logging.warning(f"Failed to write mux interval: {e}") + break # get linux kernel version