Skip to content

Commit

Permalink
Determine the python path for denoise and set it in environment
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Sep 3, 2024
1 parent 3c25c14 commit 03ceb8d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
39 changes: 36 additions & 3 deletions rebench/denoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self):
self._cset_path = None
self._denoise_path = None
self._which_path = None
self._denoise_python_path = None

def get_which(self):
if not self._which_path:
Expand Down Expand Up @@ -96,6 +97,19 @@ def get_denoise(self):

return self._denoise_path

def get_denoise_python_path(self):
if self._denoise_python_path is None:
active_python_path = sys.path
current_file = os.path.abspath(__file__)

# find the element in active_python_path that has the start of the current file path
for path in active_python_path:
if current_file.startswith(path):
self._denoise_python_path = path
return path

return self._denoise_python_path


paths = CommandsPaths()

Expand All @@ -110,9 +124,23 @@ def __init__(self, succeeded, warn_msg, use_nice, use_shielding, details):
self.details = details


def get_env_with_python_path_for_denoise():
return add_denoise_python_path_to_env(os.environ)


def add_denoise_python_path_to_env(env):
env = env.copy()
if 'PYTHONPATH' in env and env['PYTHONPATH']:
env['PYTHONPATH'] += os.pathsep + paths.get_denoise_python_path()
else:
env['PYTHONPATH'] = paths.get_denoise_python_path()
return env


def minimize_noise(show_warnings, ui, for_profiling): # pylint: disable=too-many-statements
result = {}

env = get_env_with_python_path_for_denoise()
cmd = ['sudo', '-n', paths.get_denoise()]
if for_profiling:
cmd += ['--for-profiling']
Expand All @@ -122,7 +150,9 @@ def minimize_noise(show_warnings, ui, for_profiling): # pylint: disable=too-man
cmd += ['--json', 'minimize']

try:
output = output_as_str(subprocess.check_output(cmd, stderr=subprocess.STDOUT))
output = output_as_str(subprocess.check_output(cmd,
stderr=subprocess.STDOUT,
env=env))
try:
result = json.loads(output)
got_json = True
Expand Down Expand Up @@ -196,6 +226,7 @@ def restore_noise(denoise_result, show_warning, ui):
# likely has failed completely. And without details, just no-op
return

env = get_env_with_python_path_for_denoise()
values = set(denoise_result.details.values())
if len(values) == 1 and "failed" in values:
# everything failed, don't need to try to restore things
Expand All @@ -207,7 +238,7 @@ def restore_noise(denoise_result, show_warning, ui):
cmd += ['--without-shielding']
if not denoise_result.use_nice:
cmd += ['--without-nice']
subprocess.check_output(cmd + ['restore'], stderr=subprocess.STDOUT)
subprocess.check_output(cmd + ['restore'], stderr=subprocess.STDOUT, env=env)
except (subprocess.CalledProcessError, FileNotFoundError):
pass

Expand All @@ -217,9 +248,11 @@ def restore_noise(denoise_result, show_warning, ui):


def deliver_kill_signal(pid):
env = get_env_with_python_path_for_denoise()

try:
cmd = ['sudo', '-n', paths.get_denoise(), '--json', 'kill', str(pid)]
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
subprocess.check_output(cmd, stderr=subprocess.STDOUT, env=env)
except (subprocess.CalledProcessError, FileNotFoundError):
pass

Expand Down
4 changes: 2 additions & 2 deletions rebench/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from time import time

from . import subprocess_with_timeout as subprocess_timeout
from .denoise import paths as denoise_paths
from .denoise import paths as denoise_paths, add_denoise_python_path_to_env
from .interop.adapter import ExecutionDeliveredNoResults, instantiate_adapter, OutputNotParseable, \
ResultsIndicatedAsInvalid
from .ui import escape_braces
Expand Down Expand Up @@ -529,7 +529,7 @@ def _keep_alive(seconds):
location = os.path.expanduser(location)

(return_code, output, _) = subprocess_timeout.run(
cmdline, env=run_id.env, cwd=location, stdout=subprocess.PIPE,
cmdline, env=add_denoise_python_path_to_env(run_id.env), cwd=location, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True, verbose=self.debug,
timeout=run_id.max_invocation_time,
keep_alive_output=_keep_alive,
Expand Down

0 comments on commit 03ceb8d

Please sign in to comment.