diff options
Diffstat (limited to 'experiment/litmus_util.py')
-rw-r--r-- | experiment/litmus_util.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/experiment/litmus_util.py b/experiment/litmus_util.py new file mode 100644 index 0000000..114f4c9 --- /dev/null +++ b/experiment/litmus_util.py | |||
@@ -0,0 +1,63 @@ | |||
1 | import re | ||
2 | import time | ||
3 | import subprocess | ||
4 | import os | ||
5 | import stat | ||
6 | import config.config as conf | ||
7 | |||
8 | def num_cpus(): | ||
9 | """Return the number of CPUs in the system.""" | ||
10 | |||
11 | lnx_re = re.compile(r'^(processor|online)') | ||
12 | cpus = 0 | ||
13 | |||
14 | with open('/proc/cpuinfo', 'r') as f: | ||
15 | for line in f: | ||
16 | if lnx_re.match(line): | ||
17 | cpus += 1 | ||
18 | return cpus | ||
19 | |||
20 | def switch_scheduler(switch_to_in): | ||
21 | """Switch the scheduler to whatever is passed in. | ||
22 | |||
23 | This methods sleeps for two seconds to give Linux the chance to execute | ||
24 | schedule switching code. Raises an exception if the switch does not work. | ||
25 | |||
26 | """ | ||
27 | |||
28 | switch_to = str(switch_to_in).strip() | ||
29 | |||
30 | with open('/proc/litmus/active_plugin', 'w') as active_plugin: | ||
31 | subprocess.Popen(["echo", switch_to], stdout=active_plugin) | ||
32 | |||
33 | # it takes a bit to do the switch, sleep an arbitrary amount of time | ||
34 | time.sleep(2) | ||
35 | |||
36 | with open('/proc/litmus/active_plugin', 'r') as active_plugin: | ||
37 | cur_plugin = active_plugin.read().strip() | ||
38 | |||
39 | if switch_to != cur_plugin: | ||
40 | raise Exception("Could not switch to plugin: %s" % switch_to) | ||
41 | |||
42 | |||
43 | def is_executable(fname): | ||
44 | """Return whether the file passed in is executable""" | ||
45 | mode = os.stat(fname)[stat.ST_MODE] | ||
46 | return mode & stat.S_IXUSR and mode & stat.S_IRUSR | ||
47 | |||
48 | def is_device(dev): | ||
49 | if not os.path.exists(dev): | ||
50 | return False | ||
51 | mode = os.stat(dev)[stat.ST_MODE] | ||
52 | return not (not mode & stat.S_IFCHR) | ||
53 | |||
54 | def release_tasks(): | ||
55 | |||
56 | try: | ||
57 | data = subprocess.check_output([conf.BINS['release']]) | ||
58 | except subprocess.CalledProcessError: | ||
59 | raise Exception('Something went wrong in release_ts') | ||
60 | |||
61 | released = re.findall(r"([0-9]+) real-time", data)[0] | ||
62 | |||
63 | return int(released) | ||