blob: cb39ac46bc73c3e74435097bc5f3b5cc36c64ffd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Jiri Olsa52502bf2012-10-31 15:52:47 +01002
Tony Jones35ea7e42019-01-23 16:52:29 -08003from __future__ import print_function
4
Jiri Olsa52502bf2012-10-31 15:52:47 +01005import os
6import sys
7import glob
8import optparse
9import tempfile
10import logging
11import shutil
Tony Jones35ea7e42019-01-23 16:52:29 -080012
13try:
14 import configparser
15except ImportError:
16 import ConfigParser as configparser
Jiri Olsa52502bf2012-10-31 15:52:47 +010017
Jiri Olsadde622a2017-07-03 16:50:20 +020018def data_equal(a, b):
Jiri Olsa04c31bc2017-07-03 16:50:19 +020019 # Allow multiple values in assignment separated by '|'
20 a_list = a.split('|')
21 b_list = b.split('|')
22
23 for a_item in a_list:
24 for b_item in b_list:
25 if (a_item == b_item):
26 return True
27 elif (a_item == '*') or (b_item == '*'):
28 return True
29
30 return False
31
Jiri Olsa52502bf2012-10-31 15:52:47 +010032class Fail(Exception):
33 def __init__(self, test, msg):
34 self.msg = msg
35 self.test = test
36 def getMsg(self):
37 return '\'%s\' - %s' % (self.test.path, self.msg)
38
Thomas Richter19508c02017-06-22 09:36:25 +020039class Notest(Exception):
40 def __init__(self, test, arch):
41 self.arch = arch
42 self.test = test
43 def getMsg(self):
44 return '[%s] \'%s\'' % (self.arch, self.test.path)
45
Jiri Olsa52502bf2012-10-31 15:52:47 +010046class Unsup(Exception):
47 def __init__(self, test):
48 self.test = test
49 def getMsg(self):
50 return '\'%s\'' % self.test.path
51
52class Event(dict):
53 terms = [
Jiri Olsac21d0032013-02-25 10:52:50 +010054 'cpu',
Jiri Olsa52502bf2012-10-31 15:52:47 +010055 'flags',
56 'type',
57 'size',
58 'config',
59 'sample_period',
60 'sample_type',
61 'read_format',
62 'disabled',
63 'inherit',
64 'pinned',
65 'exclusive',
66 'exclude_user',
67 'exclude_kernel',
68 'exclude_hv',
69 'exclude_idle',
70 'mmap',
71 'comm',
72 'freq',
73 'inherit_stat',
74 'enable_on_exec',
75 'task',
Jiri Olsa45e40892012-11-05 16:49:38 +010076 'watermark',
Jiri Olsa52502bf2012-10-31 15:52:47 +010077 'precise_ip',
78 'mmap_data',
79 'sample_id_all',
80 'exclude_host',
81 'exclude_guest',
82 'exclude_callchain_kernel',
83 'exclude_callchain_user',
84 'wakeup_events',
85 'bp_type',
86 'config1',
87 'config2',
88 'branch_sample_type',
89 'sample_regs_user',
90 'sample_stack_user',
91 ]
92
93 def add(self, data):
94 for key, val in data:
95 log.debug(" %s = %s" % (key, val))
96 self[key] = val
97
98 def __init__(self, name, data, base):
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -030099 log.debug(" Event %s" % name);
Jiri Olsa52502bf2012-10-31 15:52:47 +0100100 self.name = name;
101 self.group = ''
102 self.add(base)
103 self.add(data)
104
Jiri Olsa52502bf2012-10-31 15:52:47 +0100105 def equal(self, other):
106 for t in Event.terms:
107 log.debug(" [%s] %s %s" % (t, self[t], other[t]));
Tony Jones35ea7e42019-01-23 16:52:29 -0800108 if t not in self or t not in other:
Jiri Olsa52502bf2012-10-31 15:52:47 +0100109 return False
Jiri Olsadde622a2017-07-03 16:50:20 +0200110 if not data_equal(self[t], other[t]):
Jiri Olsa52502bf2012-10-31 15:52:47 +0100111 return False
112 return True
113
Jiri Olsa1f418732017-07-03 16:50:29 +0200114 def optional(self):
Tony Jones35ea7e42019-01-23 16:52:29 -0800115 if 'optional' in self and self['optional'] == '1':
Jiri Olsa1f418732017-07-03 16:50:29 +0200116 return True
117 return False
118
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300119 def diff(self, other):
120 for t in Event.terms:
Tony Jones35ea7e42019-01-23 16:52:29 -0800121 if t not in self or t not in other:
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300122 continue
Jiri Olsadde622a2017-07-03 16:50:20 +0200123 if not data_equal(self[t], other[t]):
Adrian Hunter741dad82018-11-22 16:04:56 +0200124 log.warning("expected %s=%s, got %s" % (t, self[t], other[t]))
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300125
Jiri Olsa8dfec402012-11-05 16:49:39 +0100126# Test file description needs to have following sections:
127# [config]
128# - just single instance in file
129# - needs to specify:
130# 'command' - perf command name
131# 'args' - special command arguments
132# 'ret' - expected command return value (0 by default)
Thomas Richter19508c02017-06-22 09:36:25 +0200133# 'arch' - architecture specific test (optional)
134# comma separated list, ! at the beginning
135# negates it.
Jiri Olsa8dfec402012-11-05 16:49:39 +0100136#
137# [eventX:base]
138# - one or multiple instances in file
139# - expected values assignments
Jiri Olsa52502bf2012-10-31 15:52:47 +0100140class Test(object):
141 def __init__(self, path, options):
Tony Jones35ea7e42019-01-23 16:52:29 -0800142 parser = configparser.SafeConfigParser()
Jiri Olsa52502bf2012-10-31 15:52:47 +0100143 parser.read(path)
144
Jiri Olsa097c8752013-02-25 10:52:49 +0100145 log.warning("running '%s'" % path)
Jiri Olsa52502bf2012-10-31 15:52:47 +0100146
147 self.path = path
148 self.test_dir = options.test_dir
149 self.perf = options.perf
150 self.command = parser.get('config', 'command')
151 self.args = parser.get('config', 'args')
152
153 try:
154 self.ret = parser.get('config', 'ret')
155 except:
156 self.ret = 0
157
Thomas Richter19508c02017-06-22 09:36:25 +0200158 try:
159 self.arch = parser.get('config', 'arch')
160 log.warning("test limitation '%s'" % self.arch)
161 except:
162 self.arch = ''
163
Jiri Olsa52502bf2012-10-31 15:52:47 +0100164 self.expect = {}
165 self.result = {}
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300166 log.debug(" loading expected events");
Jiri Olsa52502bf2012-10-31 15:52:47 +0100167 self.load_events(path, self.expect)
168
169 def is_event(self, name):
170 if name.find("event") == -1:
171 return False
172 else:
173 return True
174
Thomas Richter19508c02017-06-22 09:36:25 +0200175 def skip_test(self, myarch):
176 # If architecture not set always run test
177 if self.arch == '':
178 # log.warning("test for arch %s is ok" % myarch)
179 return False
180
181 # Allow multiple values in assignment separated by ','
182 arch_list = self.arch.split(',')
183
184 # Handle negated list such as !s390x,ppc
185 if arch_list[0][0] == '!':
186 arch_list[0] = arch_list[0][1:]
187 log.warning("excluded architecture list %s" % arch_list)
188 for arch_item in arch_list:
189 # log.warning("test for %s arch is %s" % (arch_item, myarch))
190 if arch_item == myarch:
191 return True
192 return False
193
194 for arch_item in arch_list:
195 # log.warning("test for architecture '%s' current '%s'" % (arch_item, myarch))
196 if arch_item == myarch:
197 return False
198 return True
199
Jiri Olsa52502bf2012-10-31 15:52:47 +0100200 def load_events(self, path, events):
Tony Jones35ea7e42019-01-23 16:52:29 -0800201 parser_event = configparser.SafeConfigParser()
Jiri Olsa52502bf2012-10-31 15:52:47 +0100202 parser_event.read(path)
203
Jiri Olsa8dfec402012-11-05 16:49:39 +0100204 # The event record section header contains 'event' word,
205 # optionaly followed by ':' allowing to load 'parent
206 # event' first as a base
Jiri Olsa52502bf2012-10-31 15:52:47 +0100207 for section in filter(self.is_event, parser_event.sections()):
208
209 parser_items = parser_event.items(section);
210 base_items = {}
211
Jiri Olsa8dfec402012-11-05 16:49:39 +0100212 # Read parent event if there's any
Jiri Olsa52502bf2012-10-31 15:52:47 +0100213 if (':' in section):
214 base = section[section.index(':') + 1:]
Tony Jones35ea7e42019-01-23 16:52:29 -0800215 parser_base = configparser.SafeConfigParser()
Jiri Olsa52502bf2012-10-31 15:52:47 +0100216 parser_base.read(self.test_dir + '/' + base)
217 base_items = parser_base.items('event')
218
219 e = Event(section, parser_items, base_items)
220 events[section] = e
221
222 def run_cmd(self, tempdir):
Thomas Richter19508c02017-06-22 09:36:25 +0200223 junk1, junk2, junk3, junk4, myarch = (os.uname())
224
225 if self.skip_test(myarch):
226 raise Notest(self, myarch)
227
Jiri Olsa52502bf2012-10-31 15:52:47 +0100228 cmd = "PERF_TEST_ATTR=%s %s %s -o %s/perf.data %s" % (tempdir,
229 self.perf, self.command, tempdir, self.args)
230 ret = os.WEXITSTATUS(os.system(cmd))
231
Jiri Olsa04c31bc2017-07-03 16:50:19 +0200232 log.info(" '%s' ret '%s', expected '%s'" % (cmd, str(ret), str(self.ret)))
Jiri Olsa52502bf2012-10-31 15:52:47 +0100233
Jiri Olsadde622a2017-07-03 16:50:20 +0200234 if not data_equal(str(ret), str(self.ret)):
Jiri Olsa52502bf2012-10-31 15:52:47 +0100235 raise Unsup(self)
236
237 def compare(self, expect, result):
238 match = {}
239
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300240 log.debug(" compare");
Jiri Olsa52502bf2012-10-31 15:52:47 +0100241
242 # For each expected event find all matching
243 # events in result. Fail if there's not any.
244 for exp_name, exp_event in expect.items():
245 exp_list = []
Thomas Richter3440fe22017-09-13 10:12:08 +0200246 res_event = {}
Jiri Olsa52502bf2012-10-31 15:52:47 +0100247 log.debug(" matching [%s]" % exp_name)
248 for res_name, res_event in result.items():
249 log.debug(" to [%s]" % res_name)
250 if (exp_event.equal(res_event)):
251 exp_list.append(res_name)
252 log.debug(" ->OK")
253 else:
254 log.debug(" ->FAIL");
255
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300256 log.debug(" match: [%s] matches %s" % (exp_name, str(exp_list)))
Jiri Olsa52502bf2012-10-31 15:52:47 +0100257
258 # we did not any matching event - fail
Jiri Olsa1f418732017-07-03 16:50:29 +0200259 if not exp_list:
260 if exp_event.optional():
261 log.debug(" %s does not match, but is optional" % exp_name)
262 else:
Thomas Richter3440fe22017-09-13 10:12:08 +0200263 if not res_event:
264 log.debug(" res_event is empty");
265 else:
266 exp_event.diff(res_event)
Jiri Olsa1f418732017-07-03 16:50:29 +0200267 raise Fail(self, 'match failure');
Jiri Olsa52502bf2012-10-31 15:52:47 +0100268
269 match[exp_name] = exp_list
270
271 # For each defined group in the expected events
272 # check we match the same group in the result.
273 for exp_name, exp_event in expect.items():
274 group = exp_event.group
275
276 if (group == ''):
277 continue
278
Jiri Olsa52502bf2012-10-31 15:52:47 +0100279 for res_name in match[exp_name]:
280 res_group = result[res_name].group
281 if res_group not in match[group]:
282 raise Fail(self, 'group failure')
283
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300284 log.debug(" group: [%s] matches group leader %s" %
Jiri Olsa52502bf2012-10-31 15:52:47 +0100285 (exp_name, str(match[group])))
286
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300287 log.debug(" matched")
Jiri Olsa52502bf2012-10-31 15:52:47 +0100288
289 def resolve_groups(self, events):
290 for name, event in events.items():
291 group_fd = event['group_fd'];
292 if group_fd == '-1':
293 continue;
294
295 for iname, ievent in events.items():
296 if (ievent['fd'] == group_fd):
297 event.group = iname
298 log.debug('[%s] has group leader [%s]' % (name, iname))
299 break;
300
301 def run(self):
302 tempdir = tempfile.mkdtemp();
303
Jiri Olsad4fcf0a2012-11-08 17:01:01 +0100304 try:
305 # run the test script
306 self.run_cmd(tempdir);
Jiri Olsa52502bf2012-10-31 15:52:47 +0100307
Jiri Olsad4fcf0a2012-11-08 17:01:01 +0100308 # load events expectation for the test
Arnaldo Carvalho de Meloce90e382012-12-18 14:14:24 -0300309 log.debug(" loading result events");
Jiri Olsad4fcf0a2012-11-08 17:01:01 +0100310 for f in glob.glob(tempdir + '/event*'):
311 self.load_events(f, self.result);
Jiri Olsa52502bf2012-10-31 15:52:47 +0100312
Jiri Olsad4fcf0a2012-11-08 17:01:01 +0100313 # resolve group_fd to event names
314 self.resolve_groups(self.expect);
315 self.resolve_groups(self.result);
Jiri Olsa52502bf2012-10-31 15:52:47 +0100316
Jiri Olsad4fcf0a2012-11-08 17:01:01 +0100317 # do the expectation - results matching - both ways
318 self.compare(self.expect, self.result)
319 self.compare(self.result, self.expect)
Jiri Olsa52502bf2012-10-31 15:52:47 +0100320
Jiri Olsad4fcf0a2012-11-08 17:01:01 +0100321 finally:
322 # cleanup
323 shutil.rmtree(tempdir)
Jiri Olsa52502bf2012-10-31 15:52:47 +0100324
325
326def run_tests(options):
327 for f in glob.glob(options.test_dir + '/' + options.test):
328 try:
329 Test(f, options).run()
Tony Jones35ea7e42019-01-23 16:52:29 -0800330 except Unsup as obj:
Jiri Olsa52502bf2012-10-31 15:52:47 +0100331 log.warning("unsupp %s" % obj.getMsg())
Tony Jones35ea7e42019-01-23 16:52:29 -0800332 except Notest as obj:
Thomas Richter19508c02017-06-22 09:36:25 +0200333 log.warning("skipped %s" % obj.getMsg())
Jiri Olsa52502bf2012-10-31 15:52:47 +0100334
335def setup_log(verbose):
336 global log
337 level = logging.CRITICAL
338
339 if verbose == 1:
340 level = logging.WARNING
341 if verbose == 2:
342 level = logging.INFO
343 if verbose >= 3:
344 level = logging.DEBUG
345
346 log = logging.getLogger('test')
347 log.setLevel(level)
348 ch = logging.StreamHandler()
349 ch.setLevel(level)
350 formatter = logging.Formatter('%(message)s')
351 ch.setFormatter(formatter)
352 log.addHandler(ch)
353
354USAGE = '''%s [OPTIONS]
355 -d dir # tests dir
356 -p path # perf binary
357 -t test # single test
358 -v # verbose level
359''' % sys.argv[0]
360
361def main():
362 parser = optparse.OptionParser(usage=USAGE)
363
364 parser.add_option("-t", "--test",
365 action="store", type="string", dest="test")
366 parser.add_option("-d", "--test-dir",
367 action="store", type="string", dest="test_dir")
368 parser.add_option("-p", "--perf",
369 action="store", type="string", dest="perf")
370 parser.add_option("-v", "--verbose",
Tony Jones35ea7e42019-01-23 16:52:29 -0800371 default=0, action="count", dest="verbose")
Jiri Olsa52502bf2012-10-31 15:52:47 +0100372
373 options, args = parser.parse_args()
374 if args:
375 parser.error('FAILED wrong arguments %s' % ' '.join(args))
376 return -1
377
378 setup_log(options.verbose)
379
380 if not options.test_dir:
Tony Jones35ea7e42019-01-23 16:52:29 -0800381 print('FAILED no -d option specified')
Jiri Olsa52502bf2012-10-31 15:52:47 +0100382 sys.exit(-1)
383
384 if not options.test:
385 options.test = 'test*'
386
387 try:
388 run_tests(options)
389
Tony Jones35ea7e42019-01-23 16:52:29 -0800390 except Fail as obj:
391 print("FAILED %s" % obj.getMsg())
Jiri Olsa52502bf2012-10-31 15:52:47 +0100392 sys.exit(-1)
393
394 sys.exit(0)
395
396if __name__ == '__main__':
397 main()