]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - gw-tests/lib.py
Allow calling `make html` when results is a symlink
[can-benchmark.git] / gw-tests / lib.py
1 """
2 Contains various helper functions and information for benchmarking RTEMS CAN GW implementation.
3 """
4
5 import subprocess
6 import sys
7 import os
8 import shutil
9 import stat
10 import time
11 import glob
12
13 load_types = ("cpu", "eth", "none")
14 traffic_modes = ("oneatatime", "50", "flood")
15
16 class settings:
17     """Wrapper class for "globals"
18     Used to keep track of current test settings for other functions.
19     """
20     hostname = None
21     targetname = "rtems4.10-midam"
22     traffic = None
23     load = None
24     testname = None
25
26 def set_hostname(name):
27     settings.hostname = name
28
29 def set_targetname(name):
30     settings.targetname = name
31
32 def set_traffic(traffic):
33     settings.traffic = traffic
34
35 def set_load(load):
36     settings.load = load
37
38 def set_testname(name):
39     settings.testname = name
40
41 def set_testenv(hostname = None, targetname = None, traffic = None, load = None, testname = None):
42     set_hostname(hostname)
43     set_targetname(targetname)
44     set_traffic(traffic)
45     set_load(load)
46     set_testname(testname)
47
48 gateway_IP_addr = "192.168.2.3"
49 ping_flood_cmd = "ping -f -s 60000 -q {}".format(gateway_IP_addr)
50
51 #Prepares 755 flag for setting privileges
52 file_flag =  stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH
53
54 base_plot_string = ('#!/bin/bash\n'
55                     'export kvers={0}\n'
56                     'export hostkvers={1}\n'
57                     'export traffic={2}\n'
58                     'export load={3}\n'
59                     'cd $(dirname $0)/../../../../../..\n'
60                     'exec ./{4}.sh --plot "$@"\n')
61
62 tftpboot_path = "/var/lib/tftpboot/ryu/"
63 load_image_path = tftpboot_path + "rtems4.10_{}_FULL_LOAD"
64 no_load_image_path = tftpboot_path + "rtems4.10_{}_NO_LOAD"
65 boot_image_path = tftpboot_path + "uImage"
66
67 eth_process = None
68 def start_eth_load():
69     """Starts eth load by ping_flood_cmd"""
70     global eth_process
71     eth_process = subprocess.Popen(ping_flood_cmd.split())
72     
73 def stop_eth_load():
74     """Stops eth load if active"""
75     global eth_process
76     if eth_process is not None:
77         eth_process.terminate()
78         eth_process = None
79         print("Eth load stopped")
80     else:
81         print("Couldn't stop eth load -- was not active")
82
83 def restart_board():
84     """Physically restarts the board"""
85     result = subprocess.call("../scripts/restart")
86     print("Waiting on restart for 15s.")
87     time.sleep(15) # 15 seconds should be enough to boot up fully even when it runs slowly
88     
89 def start_cpu_load(test_name):
90     """Actually flashes different image, because I don't have working way of communicating commands to the board."""
91     shutil.copy(load_image_path.format(settings.testname), boot_image_path)
92     restart_board()
93     
94 def stop_cpu_load(test_name):
95     """Actually flashes different image, because I don't have working way of communicating commands to the board."""
96     shutil.copy(no_load_image_path.format(settings.testname), boot_image_path)
97     restart_board()
98
99 def run_in_dir(directory, test):
100     """Runs given test in given directory and after it is finished, changes it back."""
101     old = os.getcwd()
102     os.chdir(directory)
103     test.run()
104     os.chdir(old)
105
106 def run_test(test):
107     """Prepares proper directory for test and then runs it in the directory."""
108     target_dir = os.path.join("results", settings.hostname, settings.targetname, settings.traffic, settings.load, test.name)
109     prepare_directory(target_dir)
110     run_in_dir(target_dir, test)
111
112 def prepare_directory(path):
113     """Creates given path if it does not already exist."""
114     if not os.path.exists(path):
115         os.makedirs(path)
116     
117
118 def create_plot_script():
119     """Creates plot.sh with correct format to be graphed."""
120     with open("plot.sh", "w+") as file:
121         file.write(base_plot_string.format(settings.targetname, settings.hostname, settings.traffic, settings.load, settings.testname))
122     os.chmod("plot.sh", file_flag)
123
124 def get_latester_arg(msg_len):
125     """Returns first part of the latester arguments."""
126     if settings.traffic == "oneatatime":
127         return "-o"
128     elif settings.traffic == "flood":
129         return ""
130     elif settings.traffic == "50":
131         return "-p " + str(2 * (44 + 8 * msg_len))
132     else:
133         raise InvalidModeException("invalid argument for get_latester_arg")
134
135 def call_latester(args):
136     """Calls latester with the supplied arguments"""
137     cmd = "latester " + args
138     subprocess.call(cmd.split())
139
140 def symlink_results(orig_name):
141     """Symlinks all .txt files in path given by ../{orig_name}/ into current directory under their original names."""
142     files = glob.iglob("../{}/*.txt".format(orig_name))
143     for txt_file in files:
144         link_file = txt_file.split(os.path.sep)[-1]
145         if os.path.islink(link_file) or os.path.exists(link_file):
146             os.remove(link_file)
147         os.symlink(txt_file, link_file)
148
149 def read_hostname():
150     """Reads uname from host kernel."""
151     k_call = subprocess.Popen("uname -r".split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
152     output, error = k_call.communicate()
153     #we have to convert output from bytearray to string and strip trailing newline from output
154     return "host-" + output.decode(sys.stdout.encoding).rstrip()
155
156
157 def original_name_from_histogram_name(hist_name):
158     """Retuns name of the original test from the name of its histogram."""
159     return "-".join(hist_name.split("-")[:-1])
160     
161 def set_txqueuelen(length):
162     """Sets txqueuelen on can0 interface to given length"""
163     subprocess.call("ifconfig can0 txqueuelen {}".format(length).split())