2 # SPDX-License-Identifier: GPL-2.0
14 # Allow multiple values in assignment separated by '|'
20 if (a_item == b_item):
22 elif (a_item == '*') or (b_item == '*'):
27 class Fail(Exception):
28 def __init__(self, test, msg):
32 return '\'%s\' - %s' % (self.test.path, self.msg)
34 class Notest(Exception):
35 def __init__(self, test, arch):
39 return '[%s] \'%s\'' % (self.arch, self.test.path)
41 class Unsup(Exception):
42 def __init__(self, test):
45 return '\'%s\'' % self.test.path
77 'exclude_callchain_kernel',
78 'exclude_callchain_user',
90 log.debug(" %s = %s" % (key, val))
93 def __init__(self, name, data, base):
94 log.debug(" Event %s" % name);
100 def equal(self, other):
101 for t in Event.terms:
102 log.debug(" [%s] %s %s" % (t, self[t], other[t]));
103 if not self.has_key(t) or not other.has_key(t):
105 if not data_equal(self[t], other[t]):
110 if self.has_key('optional') and self['optional'] == '1':
114 def diff(self, other):
115 for t in Event.terms:
116 if not self.has_key(t) or not other.has_key(t):
118 if not data_equal(self[t], other[t]):
119 log.warning("expected %s=%s, got %s" % (t, self[t], other[t]))
121 # Test file description needs to have following sections:
123 # - just single instance in file
124 # - needs to specify:
125 # 'command' - perf command name
126 # 'args' - special command arguments
127 # 'ret' - expected command return value (0 by default)
128 # 'arch' - architecture specific test (optional)
129 # comma separated list, ! at the beginning
133 # - one or multiple instances in file
134 # - expected values assignments
136 def __init__(self, path, options):
137 parser = ConfigParser.SafeConfigParser()
140 log.warning("running '%s'" % path)
143 self.test_dir = options.test_dir
144 self.perf = options.perf
145 self.command = parser.get('config', 'command')
146 self.args = parser.get('config', 'args')
149 self.ret = parser.get('config', 'ret')
154 self.arch = parser.get('config', 'arch')
155 log.warning("test limitation '%s'" % self.arch)
161 log.debug(" loading expected events");
162 self.load_events(path, self.expect)
164 def is_event(self, name):
165 if name.find("event") == -1:
170 def skip_test(self, myarch):
171 # If architecture not set always run test
173 # log.warning("test for arch %s is ok" % myarch)
176 # Allow multiple values in assignment separated by ','
177 arch_list = self.arch.split(',')
179 # Handle negated list such as !s390x,ppc
180 if arch_list[0][0] == '!':
181 arch_list[0] = arch_list[0][1:]
182 log.warning("excluded architecture list %s" % arch_list)
183 for arch_item in arch_list:
184 # log.warning("test for %s arch is %s" % (arch_item, myarch))
185 if arch_item == myarch:
189 for arch_item in arch_list:
190 # log.warning("test for architecture '%s' current '%s'" % (arch_item, myarch))
191 if arch_item == myarch:
195 def load_events(self, path, events):
196 parser_event = ConfigParser.SafeConfigParser()
197 parser_event.read(path)
199 # The event record section header contains 'event' word,
200 # optionaly followed by ':' allowing to load 'parent
201 # event' first as a base
202 for section in filter(self.is_event, parser_event.sections()):
204 parser_items = parser_event.items(section);
207 # Read parent event if there's any
209 base = section[section.index(':') + 1:]
210 parser_base = ConfigParser.SafeConfigParser()
211 parser_base.read(self.test_dir + '/' + base)
212 base_items = parser_base.items('event')
214 e = Event(section, parser_items, base_items)
217 def run_cmd(self, tempdir):
218 junk1, junk2, junk3, junk4, myarch = (os.uname())
220 if self.skip_test(myarch):
221 raise Notest(self, myarch)
223 cmd = "PERF_TEST_ATTR=%s %s %s -o %s/perf.data %s" % (tempdir,
224 self.perf, self.command, tempdir, self.args)
225 ret = os.WEXITSTATUS(os.system(cmd))
227 log.info(" '%s' ret '%s', expected '%s'" % (cmd, str(ret), str(self.ret)))
229 if not data_equal(str(ret), str(self.ret)):
232 def compare(self, expect, result):
235 log.debug(" compare");
237 # For each expected event find all matching
238 # events in result. Fail if there's not any.
239 for exp_name, exp_event in expect.items():
242 log.debug(" matching [%s]" % exp_name)
243 for res_name, res_event in result.items():
244 log.debug(" to [%s]" % res_name)
245 if (exp_event.equal(res_event)):
246 exp_list.append(res_name)
249 log.debug(" ->FAIL");
251 log.debug(" match: [%s] matches %s" % (exp_name, str(exp_list)))
253 # we did not any matching event - fail
255 if exp_event.optional():
256 log.debug(" %s does not match, but is optional" % exp_name)
259 log.debug(" res_event is empty");
261 exp_event.diff(res_event)
262 raise Fail(self, 'match failure');
264 match[exp_name] = exp_list
266 # For each defined group in the expected events
267 # check we match the same group in the result.
268 for exp_name, exp_event in expect.items():
269 group = exp_event.group
274 for res_name in match[exp_name]:
275 res_group = result[res_name].group
276 if res_group not in match[group]:
277 raise Fail(self, 'group failure')
279 log.debug(" group: [%s] matches group leader %s" %
280 (exp_name, str(match[group])))
282 log.debug(" matched")
284 def resolve_groups(self, events):
285 for name, event in events.items():
286 group_fd = event['group_fd'];
290 for iname, ievent in events.items():
291 if (ievent['fd'] == group_fd):
293 log.debug('[%s] has group leader [%s]' % (name, iname))
297 tempdir = tempfile.mkdtemp();
300 # run the test script
301 self.run_cmd(tempdir);
303 # load events expectation for the test
304 log.debug(" loading result events");
305 for f in glob.glob(tempdir + '/event*'):
306 self.load_events(f, self.result);
308 # resolve group_fd to event names
309 self.resolve_groups(self.expect);
310 self.resolve_groups(self.result);
312 # do the expectation - results matching - both ways
313 self.compare(self.expect, self.result)
314 self.compare(self.result, self.expect)
318 shutil.rmtree(tempdir)
321 def run_tests(options):
322 for f in glob.glob(options.test_dir + '/' + options.test):
324 Test(f, options).run()
326 log.warning("unsupp %s" % obj.getMsg())
328 log.warning("skipped %s" % obj.getMsg())
330 def setup_log(verbose):
332 level = logging.CRITICAL
335 level = logging.WARNING
339 level = logging.DEBUG
341 log = logging.getLogger('test')
343 ch = logging.StreamHandler()
345 formatter = logging.Formatter('%(message)s')
346 ch.setFormatter(formatter)
349 USAGE = '''%s [OPTIONS]
351 -p path # perf binary
352 -t test # single test
357 parser = optparse.OptionParser(usage=USAGE)
359 parser.add_option("-t", "--test",
360 action="store", type="string", dest="test")
361 parser.add_option("-d", "--test-dir",
362 action="store", type="string", dest="test_dir")
363 parser.add_option("-p", "--perf",
364 action="store", type="string", dest="perf")
365 parser.add_option("-v", "--verbose",
366 action="count", dest="verbose")
368 options, args = parser.parse_args()
370 parser.error('FAILED wrong arguments %s' % ' '.join(args))
373 setup_log(options.verbose)
375 if not options.test_dir:
376 print 'FAILED no -d option specified'
380 options.test = 'test*'
386 print "FAILED %s" % obj.getMsg();
391 if __name__ == '__main__':