Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"m = np.random.randint(0, 100, (500, 500)); " \
"print(list(map(lambda x: len(np.linalg.eig(m)), range(10000))))' > /dev/null".format(heat_time)
cmds = []
for i in range(1, multiprocessing.cpu_count()):
cmds.append(cmd + "& > /dev/null")
cmds.append(cmd)
return ";".join(cmds)
@register(ExecRunDriver, "other_nice", Dict({
"nice": Int(range=range(-20, 20)) // Description("Niceness values for other processes.")
// Default(19),
"min_nice": Int(range=range(-15, 20)) // Default(-10)
// Description("Processes with lower nice values are ignored.")
}))
class OtherNicePlugin(AbstractRunDriverPlugin):
"""
Allows the setting of the nice value of most other processes (as far as possible).
"""
def __init__(self, misc_settings):
super().__init__(misc_settings)
self._old_nices = {}
def setup(self):
for line in self._exec_command("/bin/ps --noheaders -e -o pid,nice").split("\n"):
line = line.strip()
arr = list(filter(lambda x: len(x) > 0, line.split(" ")))
if len(arr) == 0:
continue
pid = int(arr[0].strip())
nice = arr[1].strip()
"min_nice": Int(range=range(-15, 20)) // Default(-10)
// Description("Processes with lower nice values are ignored."),
"min_id": PositiveInt() // Default(1500)
// Description("Processes with lower id are ignored."),
"comm_prefixes": ListOrTuple(Str()) // Default(["ssh", "xorg", "bluetoothd"])
// Description("Each process which name (lower cased) starts with one of the prefixes is not ignored. "
"Overrides the decision based on the min_id."),
"comm_prefixes_ignored": ListOrTuple(Str()) // Default(["dbus", "kworker"])
// Description("Each process which name (lower cased) starts with one of the prefixes is ignored. "
"It overrides the decisions based on comm_prefixes and min_id."),
"subtree_suffixes": ListOrTuple(Str()) // Default(["dm", "apache"])
// Description("Suffixes of processes names which are stopped."),
"dry_run": Bool() // Default(False)
// Description("Just output the to be stopped processes but don't actually stop them?")
}))
class StopStartPlugin(AbstractRunDriverPlugin):
"""
Stop almost all other processes (as far as possible).
"""
def __init__(self, misc_settings):
super().__init__(misc_settings)
self._processes = {} # type: t.Dict[str, t.Union[str, int]]
self._pids = [] # type: t.List[int]
def parse_processes(self):
self._processes = {}
for line in self._exec_command("/bin/ps --noheaders -e -o pid,nice,comm,cmd,ppid").split("\n"):
line = line.strip()
arr = list(map(lambda x: x.strip(), filter(lambda x: len(x) > 0, line.split(" "))))
if len(arr) == 0:
continue
self._send_signal(signal.SIGSTOP)
def _send_signal(self, signal: int):
for pid in self._pids:
try:
os.kill(pid, signal)
except BaseException as ex:
#logging.info(ex)
pass
def teardown(self):
self._send_signal(signal.SIGCONT)
@register(ExecRunDriver, "sync", Dict({}))
class SyncPlugin(AbstractRunDriverPlugin):
"""
Calls ``sync`` before each program execution.
"""
def setup_block_run(self, block: RunProgramBlock, runs: int = 1):
block["cmd_prefix"].append("sync")
@register(ExecRunDriver, "sleep", Dict({
"seconds": PositiveInt() // Default(10) // Description("Seconds to sleep")
}))
class SleepPlugin(AbstractRunDriverPlugin):
"""
Sleep a given amount of time before the benchmarking begins.
See Gernot Heisers Systems Benchmarking Crimes:
"free_dentries_inodes": Bool() // Default(True) // Description("Free dentries and inodes")
}))
class DropFSCaches(AbstractRunDriverPlugin):
"""
Drop page cache, directoy entries and inodes before every benchmarking run.
"""
needs_root_privileges = True
def setup_block(self, block: RunProgramBlock, runs: int = 1):
num = self.misc_settings["free_pagecache"] + 2 * self.misc_settings["free_dentries_inodes"]
block["cmd_prefix"].append("sync; echo {} > /proc/sys/vm/drop_caches".format(num))
@register(ExecRunDriver, "disable_swap", Dict({}))
class DisableSwap(AbstractRunDriverPlugin):
"""
Disables swapping on the system before the benchmarking and enables it after.
"""
needs_root_privileges = True
def setup(self):
self._exec_command("swapoff -a")
def teardown(self):
self._exec_command("swapon -a")
@register(ExecRunDriver, "disable_cpu_caches", Dict({}))
class DisableCPUCaches(AbstractRunDriverPlugin):
"""
:warning: slows program down significantly and has probably other weird consequences
:warning: this is untested
:warning: a linux-forum user declared: Disabling cpu caches gives you a pentium I like processor!!!
"""
needs_root_privileges = True
def setup(self):
setup.exec("cpu_cache/disable", "insmod disable_cache.ko")
def teardown(self):
setup.exec("cpu_cache/disable", "rmmod disable_cache.ko")
@register(ExecRunDriver, "flush_cpu_caches", Dict({}))
class FlushCPUCaches(AbstractRunDriverPlugin):
"""
Flushes the CPU caches on a x86 CPU using a small kernel module,
see `WBINVD `_
"""
needs_root_privileges = True
def setup_block_run(self, block: RunProgramBlock):
block["cmd_prefix"].append("insmod {c}/flush_cache.ko; rmmod {c}/flush_cache.ko"
.format(c=setup.script_relative("cpu_cache/flush")))
@register(ExecRunDriver, "cpu_governor", Dict({
"governor": Str() // Default("performance") // Description("New scaling governor for all cpus")
}))
class CPUGovernor(AbstractRunDriverPlugin):
@register(ExecRunDriver, "sync", Dict({}))
class SyncPlugin(AbstractRunDriverPlugin):
"""
Calls ``sync`` before each program execution.
"""
def setup_block_run(self, block: RunProgramBlock, runs: int = 1):
block["cmd_prefix"].append("sync")
@register(ExecRunDriver, "sleep", Dict({
"seconds": PositiveInt() // Default(10) // Description("Seconds to sleep")
}))
class SleepPlugin(AbstractRunDriverPlugin):
"""
Sleep a given amount of time before the benchmarking begins.
See Gernot Heisers Systems Benchmarking Crimes:
Make sure that the system is really quiescent when starting an experiment,
leave enough time to ensure all previous data is flushed out.
"""
def setup_block(self, block: RunProgramBlock, runs: int = 1):
block["cmd_prefix"].append("sleep {}".format(self.misc_settings["seconds"]))
@register(ExecRunDriver, "drop_fs_caches", Dict({
"free_pagecache": Bool() // Default(True) // Description("Free the page cache"),
"free_dentries_inodes": Bool() // Default(True) // Description("Free dentries and inodes")
}))
def _set_io_nice(self, nice: int):
self._exec_command("ionice -n {} -p {}".format(nice, os.getpid()))
def teardown(self):
self._set_nice(self._old_nice)
self._set_io_nice(self._old_io_nice)
@register(ExecRunDriver, "env_randomize", Dict({
"min": NaturalNumber() // Default(4) // Description("Minimum number of added random environment variables"),
"max": PositiveInt() // Default(4) // Description("Maximum number of added random environment variables"),
"var_max": PositiveInt() // Default(get_memory_page_size()) // Description("Maximum length of each random value"),
"key_max": PositiveInt() // Default(get_memory_page_size()) // Description("Maximum length of each random key")
}))
class EnvRandomizePlugin(AbstractRunDriverPlugin):
"""
Adds random environment variables.
"""
def setup_block_run(self, block: RunProgramBlock, runs: int = 1):
env = {}
for i in range(random.randint(self.misc_settings["min"], self.misc_settings["max"] + 1)):
env["a" * random.randint(0, self.misc_settings["key_max"])] \
= "a" * random.randint(0, self.misc_settings["var_max"])
block["env"] = env
@register(ExecRunDriver, "preheat", Dict({
"time": NaturalNumber() // Default(10)
// Description("Number of seconds to preheat the system with an cpu bound task"),
"when": ListOrTuple(ExactEither("before_each_run", "at_setup")) // Default(["before_each_run"])
def _set_scaling_governor(self, cpu: int, governor: str):
assert cpu <= len(self._cpu_paths)
if governor not in self._av_governors:
raise ValueError("No such governor {} for cpu {}, expected one of these: ".
format(cpu, governor, ", ".join(self._av_governors)))
cpu_file = self._cpu_paths[cpu] + "scaling_governor"
if list(open(cpu_file, "r"))[0].strip() != governor:
try:
self._exec_command("echo {} > {}".format(governor, cpu_file))
except EnvironmentError as err:
logging.info(err)
@register(ExecRunDriver, "disable_aslr", Dict({}))
class DisableASLR(AbstractRunDriverPlugin):
"""
Disable address space randomization
"""
needs_root_privileges = True
def setup(self):
self._exec_command("echo 0 > /proc/sys/kernel/randomize_va_space")
def teardown(self):
self._exec_command("echo 1 > /proc/sys/kernel/randomize_va_space")
@register(ExecRunDriver, "disable_ht", Dict({}))
class DisableHyperThreading(AbstractRunDriverPlugin):
"""
class DisableASLR(AbstractRunDriverPlugin):
"""
Disable address space randomization
"""
needs_root_privileges = True
def setup(self):
self._exec_command("echo 0 > /proc/sys/kernel/randomize_va_space")
def teardown(self):
self._exec_command("echo 1 > /proc/sys/kernel/randomize_va_space")
@register(ExecRunDriver, "disable_ht", Dict({}))
class DisableHyperThreading(AbstractRunDriverPlugin):
"""
Disable hyper-threading
"""
needs_root_privileges = True
def setup(self):
AbstractRunWorkerPool.disable_hyper_threading()
def teardown(self):
AbstractRunWorkerPool.enable_hyper_threading()
@register(ExecRunDriver, "disable_intel_turbo", Dict({}))
class DisableIntelTurbo(AbstractRunDriverPlugin):
"""
class DisableSwap(AbstractRunDriverPlugin):
"""
Disables swapping on the system before the benchmarking and enables it after.
"""
needs_root_privileges = True
def setup(self):
self._exec_command("swapoff -a")
def teardown(self):
self._exec_command("swapon -a")
@register(ExecRunDriver, "disable_cpu_caches", Dict({}))
class DisableCPUCaches(AbstractRunDriverPlugin):
"""
Disable the L1 and L2 caches on x86 and x86-64 architectures.
Uses a small custom kernel module (be sure to compile it via 'temci setup').
:warning: slows program down significantly and has probably other weird consequences
:warning: this is untested
:warning: a linux-forum user declared: Disabling cpu caches gives you a pentium I like processor!!!
"""
needs_root_privileges = True
def setup(self):
setup.exec("cpu_cache/disable", "insmod disable_cache.ko")
def teardown(self):
setup.exec("cpu_cache/disable", "rmmod disable_cache.ko")