From a5984172572146a093980d555eda03c1a49db130 Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Thu, 1 Feb 2018 12:43:17 +0000 Subject: [PATCH] Allow runners to write into the instrumentation dir. --- krun.py | 4 ++++ krun/util.py | 48 +++++++++++++++++++++++++++++++++++++++++++----- krun/vm_defs.py | 4 ++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/krun.py b/krun.py index cd5c1c6..fe145ea 100755 --- a/krun.py +++ b/krun.py @@ -284,6 +284,10 @@ def inner_main(mailer, on_first_invocation, config, args): platform.no_pstate_check = args.no_pstate_check platform.hardware_reboots = args.hardware_reboots + # Create the instrumentation directory if required + if on_first_invocation: + util.make_instr_dir(config) + debug("Checking platform preliminaries") platform.check_preliminaries() diff --git a/krun/util.py b/krun/util.py index bb1bea9..b1c4abd 100644 --- a/krun/util.py +++ b/krun/util.py @@ -42,6 +42,9 @@ import shutil import sys import subprocess +import pwd +import grp +import getpass from subprocess import Popen, PIPE from logging import error, debug, info, warn, root as root_logger from bz2 import BZ2File @@ -60,6 +63,10 @@ # than are strictly necessary. In either case we are safe and correct. PIPE_BUF_SZ = 1024 * 16 +from stat import S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IXUSR, S_IXGRP, S_IROTH, S_IXOTH +INSTR_DIR_MODE = S_IRUSR | S_IWUSR | S_IXUSR \ + | S_IRGRP | S_IWGRP | S_IXGRP \ + | S_IROTH | S_IXOTH DIR = os.path.abspath(os.path.dirname(__file__)) @@ -439,15 +446,46 @@ def get_instr_json_dir(config): return os.path.join(os.getcwd(), "%s_instr_data" % config_base) +def make_instr_dir(config): + # We only want make a dir if >=1 VM is in instrumentation mode. + for vm in config.VMS.itervalues(): + if vm['vm_def'].instrument: + break + else: + return # no VM is in instrumentation mode. + + dir = get_instr_json_dir(config) + debug("making instrumentation dir: %s" % dir) + os.mkdir(dir) + + +def set_instr_dir_perms(config, platform): + """Grant the Krun user write access to the instrumentation dir. + + Sadly this has to be done as root as there is no guarantee that the initial + user is in the krun user's group.""" + + from krun.vm_defs import BENCHMARK_USER + group = grp.getgrnam(BENCHMARK_USER).gr_name + user = pwd.getpwnam(getpass.getuser()).pw_name + + from krun import util + path = util.get_instr_json_dir(config) + + args = platform.change_user_args() + args.extend(["chown", "%s:%s" % (user, group), path]) + run_shell_cmd(" ".join(args)) + + os.chmod(path, INSTR_DIR_MODE) + + def dump_instr_json(key, exec_num, config, instr_data): - """Write per-execution instrumentation data to a separate JSON file""" + """Write per-execution instrumentation data to a separate JSON file. - instr_json_dir = get_instr_json_dir(config) - if not os.path.exists(instr_json_dir): - os.mkdir(instr_json_dir) + Assumes the instrumentation directory exists.""" filename = "%s__%s.json.bz2" % (key.replace(":", "__"), exec_num) - path = os.path.join(instr_json_dir, filename) + path = os.path.join(get_instr_json_dir(config), filename) # The directory was checked to be non-existant when the benchmark session # started, so it follows that the instrumentation JSON file (each of which diff --git a/krun/vm_defs.py b/krun/vm_defs.py index e7cfcf4..5591a99 100644 --- a/krun/vm_defs.py +++ b/krun/vm_defs.py @@ -239,6 +239,10 @@ def _run_exec(self, args, heap_lim_k, stack_lim_k, key, key_pexec_idx, if not self.platform.no_user_change: self.platform.make_fresh_krun_user() + # If we are in instrumentation mode, grant the Krun user write + # access to the instrumentation directory. + if self.instrument: + util.set_instr_dir_perms(self.config, self.platform) wrapper_filename, envlog_filename = \ self.make_wrapper_script(args, heap_lim_k, stack_lim_k)