]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - tools/perf/tests/attr.py
13 # Allow multiple values in assignment separated by '|'
19 if (a_item
== b_item
):
21 elif (a_item
== '*') or (b_item
== '*'):
26 class Fail(Exception):
27 def __init__(self
, test
, msg
):
31 return '\'%s\' - %s' % (self
.test
.path
, self
.msg
)
33 class Notest(Exception):
34 def __init__(self
, test
, arch
):
38 return '[%s] \'%s\'' % (self
.arch
, self
.test
.path
)
40 class Unsup(Exception):
41 def __init__(self
, test
):
44 return '\'%s\'' % self
.test
.path
76 'exclude_callchain_kernel',
77 'exclude_callchain_user',
89 log
.debug(" %s = %s" % (key
, val
))
92 def __init__(self
, name
, data
, base
):
93 log
.debug(" Event %s" % name
);
99 def equal(self
, other
):
100 for t
in Event
.terms
:
101 log
.debug(" [%s] %s %s" % (t
, self
[t
], other
[t
]));
102 if not self
.has_key(t
) or not other
.has_key(t
):
104 if not data_equal(self
[t
], other
[t
]):
109 if self
.has_key('optional') and self
['optional'] == '1':
113 def diff(self
, other
):
114 for t
in Event
.terms
:
115 if not self
.has_key(t
) or not other
.has_key(t
):
117 if not data_equal(self
[t
], other
[t
]):
118 log
.warning("expected %s=%s, got %s" % (t
, self
[t
], other
[t
]))
120 # Test file description needs to have following sections:
122 # - just single instance in file
123 # - needs to specify:
124 # 'command' - perf command name
125 # 'args' - special command arguments
126 # 'ret' - expected command return value (0 by default)
127 # 'arch' - architecture specific test (optional)
128 # comma separated list, ! at the beginning
132 # - one or multiple instances in file
133 # - expected values assignments
135 def __init__(self
, path
, options
):
136 parser
= ConfigParser
.SafeConfigParser()
139 log
.warning("running '%s'" % path
)
142 self
.test_dir
= options
.test_dir
143 self
.perf
= options
.perf
144 self
.command
= parser
.get('config', 'command')
145 self
.args
= parser
.get('config', 'args')
148 self
.ret
= parser
.get('config', 'ret')
153 self
.arch
= parser
.get('config', 'arch')
154 log
.warning("test limitation '%s'" % self
.arch
)
160 log
.debug(" loading expected events");
161 self
.load_events(path
, self
.expect
)
163 def is_event(self
, name
):
164 if name
.find("event") == -1:
169 def skip_test(self
, myarch
):
170 # If architecture not set always run test
172 # log.warning("test for arch %s is ok" % myarch)
175 # Allow multiple values in assignment separated by ','
176 arch_list
= self
.arch
.split(',')
178 # Handle negated list such as !s390x,ppc
179 if arch_list
[0][0] == '!':
180 arch_list
[0] = arch_list
[0][1:]
181 log
.warning("excluded architecture list %s" % arch_list
)
182 for arch_item
in arch_list
:
183 # log.warning("test for %s arch is %s" % (arch_item, myarch))
184 if arch_item
== myarch
:
188 for arch_item
in arch_list
:
189 # log.warning("test for architecture '%s' current '%s'" % (arch_item, myarch))
190 if arch_item
== myarch
:
194 def load_events(self
, path
, events
):
195 parser_event
= ConfigParser
.SafeConfigParser()
196 parser_event
.read(path
)
198 # The event record section header contains 'event' word,
199 # optionaly followed by ':' allowing to load 'parent
200 # event' first as a base
201 for section
in filter(self
.is_event
, parser_event
.sections()):
203 parser_items
= parser_event
.items(section
);
206 # Read parent event if there's any
208 base
= section
[section
.index(':') + 1:]
209 parser_base
= ConfigParser
.SafeConfigParser()
210 parser_base
.read(self
.test_dir
+ '/' + base
)
211 base_items
= parser_base
.items('event')
213 e
= Event(section
, parser_items
, base_items
)
216 def run_cmd(self
, tempdir
):
217 junk1
, junk2
, junk3
, junk4
, myarch
= (os
.uname())
219 if self
.skip_test(myarch
):
220 raise Notest(self
, myarch
)
222 cmd
= "PERF_TEST_ATTR=%s %s %s -o %s/perf.data %s" % (tempdir
,
223 self
.perf
, self
.command
, tempdir
, self
.args
)
224 ret
= os
.WEXITSTATUS(os
.system(cmd
))
226 log
.info(" '%s' ret '%s', expected '%s'" % (cmd
, str(ret
), str(self
.ret
)))
228 if not data_equal(str(ret
), str(self
.ret
)):
231 def compare(self
, expect
, result
):
234 log
.debug(" compare");
236 # For each expected event find all matching
237 # events in result. Fail if there's not any.
238 for exp_name
, exp_event
in expect
.items():
240 log
.debug(" matching [%s]" % exp_name
)
241 for res_name
, res_event
in result
.items():
242 log
.debug(" to [%s]" % res_name
)
243 if (exp_event
.equal(res_event
)):
244 exp_list
.append(res_name
)
247 log
.debug(" ->FAIL");
249 log
.debug(" match: [%s] matches %s" % (exp_name
, str(exp_list
)))
251 # we did not any matching event - fail
253 if exp_event
.optional():
254 log
.debug(" %s does not match, but is optional" % exp_name
)
256 exp_event
.diff(res_event
)
257 raise Fail(self
, 'match failure');
259 match
[exp_name
] = exp_list
261 # For each defined group in the expected events
262 # check we match the same group in the result.
263 for exp_name
, exp_event
in expect
.items():
264 group
= exp_event
.group
269 for res_name
in match
[exp_name
]:
270 res_group
= result
[res_name
].group
271 if res_group
not in match
[group
]:
272 raise Fail(self
, 'group failure')
274 log
.debug(" group: [%s] matches group leader %s" %
275 (exp_name
, str(match
[group
])))
277 log
.debug(" matched")
279 def resolve_groups(self
, events
):
280 for name
, event
in events
.items():
281 group_fd
= event
['group_fd'];
285 for iname
, ievent
in events
.items():
286 if (ievent
['fd'] == group_fd
):
288 log
.debug('[%s] has group leader [%s]' % (name
, iname
))
292 tempdir
= tempfile
.mkdtemp();
295 # run the test script
296 self
.run_cmd(tempdir
);
298 # load events expectation for the test
299 log
.debug(" loading result events");
300 for f
in glob
.glob(tempdir
+ '/event*'):
301 self
.load_events(f
, self
.result
);
303 # resolve group_fd to event names
304 self
.resolve_groups(self
.expect
);
305 self
.resolve_groups(self
.result
);
307 # do the expectation - results matching - both ways
308 self
.compare(self
.expect
, self
.result
)
309 self
.compare(self
.result
, self
.expect
)
313 shutil
.rmtree(tempdir
)
316 def run_tests(options
):
317 for f
in glob
.glob(options
.test_dir
+ '/' + options
.test
):
319 Test(f
, options
).run()
321 log
.warning("unsupp %s" % obj
.getMsg())
323 log
.warning("skipped %s" % obj
.getMsg())
325 def setup_log(verbose
):
327 level
= logging
.CRITICAL
330 level
= logging
.WARNING
334 level
= logging
.DEBUG
336 log
= logging
.getLogger('test')
338 ch
= logging
.StreamHandler()
340 formatter
= logging
.Formatter('%(message)s')
341 ch
.setFormatter(formatter
)
344 USAGE
= '''%s [OPTIONS]
346 -p path # perf binary
347 -t test # single test
352 parser
= optparse
.OptionParser(usage
=USAGE
)
354 parser
.add_option("-t", "--test",
355 action
="store", type="string", dest
="test")
356 parser
.add_option("-d", "--test-dir",
357 action
="store", type="string", dest
="test_dir")
358 parser
.add_option("-p", "--perf",
359 action
="store", type="string", dest
="perf")
360 parser
.add_option("-v", "--verbose",
361 action
="count", dest
="verbose")
363 options
, args
= parser
.parse_args()
365 parser
.error('FAILED wrong arguments %s' % ' '.join(args
))
368 setup_log(options
.verbose
)
370 if not options
.test_dir
:
371 print 'FAILED no -d option specified'
375 options
.test
= 'test*'
381 print "FAILED %s" % obj
.getMsg();
386 if __name__
== '__main__':