jjb: lava: kprobe-fuzzing: Split testcase into multiple independent ones
[lttng-ci.git] / scripts / system-tests / run-kprobe-fuzzing.py
CommitLineData
6dffa64f
FD
1# Copyright (C) 2018 - Francis Deslauriers <francis.deslauriers@efficios.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16import datetime
17import gzip
18import os
19import pprint
20import subprocess
21import sys
22
23NB_KPROBES_PER_ITER=500
c5b4a212 24NB_KPROBES_PER_ROUND=20000
6dffa64f
FD
25
26def load_instr_points(instr_points_archive):
27 print('Reading instrumentation points from \'{}\'.'.format(instr_points_archive), end='')
28 sys.stdout.flush()
29
30 with gzip.open(instr_points_archive, 'r') as f:
31 data = f.read()
32 print(' Done.')
33
34 return [x.decode('utf-8') for x in data.split()]
35
36def enable_kprobe_events(instr_points):
de9de073 37 print('Enabling events from {} to {}...'.format(instr_points[0], instr_points[-1]), end='')
6dffa64f
FD
38 sys.stdout.flush()
39
40 # Use os module directly, because this is a sysfs file and seeking inside
41 # the file is not supported. The python open() function with the append
42 # ('a') flag uses lseek(, SEEK_END) to move the write pointer to the end.
43 fd = os.open('/sys/kernel/debug/tracing/kprobe_events', os.O_WRONLY|os.O_CREAT|os.O_APPEND)
44 for i, point in enumerate(instr_points):
45
46 kprobe_cmd = 'r:event_{} {}\n'.format(i, point).encode('utf-8')
47 try:
48 os.write(fd, kprobe_cmd)
49 except OSError:
50 continue
51 os.close(fd)
52 print(' Done.')
53
54def set_kprobe_tracing_state(state):
55 if state not in (0 ,1):
56 raise ValueError
57
58 if state == 0:
59 # Clear the content of the trace.
60 open('/sys/kernel/debug/tracing/trace', 'w').close()
61
62 try:
63 with open('/sys/kernel/debug/tracing/events/kprobes/enable', 'w') as enable_kprobe_file:
64 enable_kprobe_file.write('{}\n'.format(state))
65 except IOError:
66 print('kprobes/enable file does not exist')
67
68def run_workload():
69 print('Running workload...', end='')
70 sys.stdout.flush()
71 workload = ['stress', '--cpu', '2', '--io', '4', '--vm', '2',
72 '--vm-bytes', '128M', '--hdd', '3', '--timeout', '3s']
73 try:
74 with open(os.devnull) as devnull:
75 subprocess.call(workload, stdout=devnull, stderr=devnull)
76 except OSError as e:
77 print("Workload execution failed:", e, file=sys.stderr)
78 pprint.pprint(workload)
79
80 print(' Done.')
81
82def mount_tracingfs():
83 with open(os.devnull) as devnull:
84 subprocess.call(['mount', '-t', 'debugfs', 'nodev', '/sys/kernel/debug/'],
85 stdout=devnull, stderr=devnull)
86
87def print_dashed_line():
88 print('-'*100)
89
90def main():
c5b4a212 91 assert(len(sys.argv) == 3)
6dffa64f
FD
92
93 instr_point_archive = sys.argv[1]
c5b4a212 94 round_nb = int(sys.argv[2])
6dffa64f
FD
95 # Load instrumentation points to disk and attach it to lava test run.
96 instrumentation_points = load_instr_points(instr_point_archive)
97
c5b4a212
FD
98 # We are past the end of the instrumentation point list.
99 if len(instrumentation_points)/NB_KPROBES_PER_ROUND <= round_nb:
100 print('No instrumentation point for round {}.'.format(round_nb))
101 return
102
6dffa64f
FD
103 mount_tracingfs()
104
105 # Loop over the list by enabling ranges of NB_KPROBES_PER_ITER kprobes.
c5b4a212 106 for i in range(int(NB_KPROBES_PER_ROUND/NB_KPROBES_PER_ITER)):
6dffa64f 107 print_dashed_line()
c5b4a212
FD
108 lower_bound = (round_nb * NB_KPROBES_PER_ROUND) + (i * NB_KPROBES_PER_ITER)
109 upper_bound = lower_bound + NB_KPROBES_PER_ITER
110 print('Time now: {}, {} to {}'.format(datetime.datetime.now(), lower_bound , upper_bound))
111 enable_kprobe_events(instrumentation_points[lower_bound:upper_bound])
6dffa64f
FD
112 set_kprobe_tracing_state(1)
113 run_workload()
114 print('\n')
c5b4a212 115 set_kprobe_tracing_state(0)
6dffa64f
FD
116
117if __name__ == "__main__":
118 main()
This page took 0.030629 seconds and 4 git commands to generate.