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