Debian Codegraph @

scripts/kprobes_test/kprobes_test.py

raw
1 #!/usr/bin/python
2
3 # Copyright (C) 2010 Red Hat Inc.
4 #
5 # This file is part of systemtap, and is free software. You can
6 # redistribute it and/or modify it under the terms of the GNU General
7 # Public License (GPL); either version 2, or (at your option) any
8 # later version.
9
10 import os
11 import pickle
12 import re
13 import sys
14 import time
15 from config_opts import config_opts
16 from gen_code import gen_module
17 from run_module import run_module
18
19 class BucketSet(object):
20 def __init__(self, bucketA=list(), bucketA_result=0,
21 bucketB=list(), bucketB_result=0, buckets=list(),
22 passed=list(), failed=list(), untriggered=list(),
23 unregistered=list(), split=0):
24 # bucketA is the list of probes to test.
25 self.bucketA = bucketA
26 # bucketA_result tells us what state bucketA is in
27 self.bucketA_result = bucketA_result
28 # bucketB is the 2nd (optional) list of probes to test
29 self.bucketB = bucketB
30 # bucketB_result tells us what state bucketB is in
31 self.bucketB_result = bucketB_result
32 # buckets is the full list of probes to test (originally
33 # filled in by reading config_opts['probes_all'])
34 self.buckets = buckets
35 # passed is the list of probe points that were sucessfully
36 # registered and triggered (actually called)
37 self.passed = passed
38 # failed is the list of probe points that were sucessfully
39 # registered but caused a crash. The probe lists have been
40 # bisected and singly eliminated.
41 self.failed = failed
42 # untriggered is the list of probe points that were sucessfully
43 # registered and but never triggered (actually called)
44 self.untriggered = untriggered
45 # untriggered is the list of probe points that couldn't be
46 # registered
47 self.unregistered = unregistered
48 # split tells us how to split a bucket. 0 means bisect
49 # it, anything else is the number of times we've tried to do a
50 # 1-by-1 split.
51 self.split = split
52
53 def bucketA_result_str(self):
54 if self.bucketA_result == 0:
55 return "untested"
56 elif self.bucketA_result == 1:
57 return "succeeded"
58 elif self.bucketA_result == 2:
59 return "failed"
60 else:
61 return "UNKNOWN"
62
63 def bucketB_result_str(self):
64 if self.bucketB_result == 0:
65 return "untested"
66 elif self.bucketB_result == 1:
67 return "succeeded"
68 elif self.bucketB_result == 2:
69 return "failed"
70 else:
71 return "UNKNOWN"
72
73 probes = BucketSet()
74
75 # Install this script in config_opts['rclocal'] (typically
76 # /etc/rc.d/rc.local). Returns true if the line wasn't already there
77 # (which means this is the 1st time the script has been run).
78 def register_script(install=True):
79 rc = True
80 regexp = re.compile("^.+/kprobes_test.py")
81 f = open(config_opts['rclocal'], "r+")
82 data = ""
83 for line in f:
84 if not regexp.match(line):
85 data += line
86 else:
87 rc = False
88 if install:
89 data += "cd %s/ && ./kprobes_test.py &" % os.getcwd()
90 f.seek(0, 0)
91 f.write(data)
92 f.truncate(f.tell())
93 f.close()
94 return rc
95
96 # Because it is very possible that we'll crash the system, we need to
97 # make sure our data files are written to the disk sucessfully before
98 # we run a module. sync_disks() makes sure everything written is
99 # actually saved.
100 def sync_disks():
101 os.system("sync; sync; sync")
102 time.sleep(5)
103 return
104
105 def read_probe_list():
106 global probes
107
108 # If the "pickled" file exists, read it in to recover our state
109 if os.path.exists(config_opts['probes_db']):
110 print "Reading state..."
111 f = open(config_opts['probes_db'])
112 p = pickle.Unpickler(f)
113 probes = p.load()
114 f.close()
115
116 # if the 'probes_all' file exists, create the data from it
117 elif os.path.exists(config_opts['probes_all']):
118 # Read in the flat file
119 f = open(config_opts['probes_all'])
120 probe_lines = f.readlines()
121 f.close()
122
123 # Create the full bucket
124 full_bucket = list()
125 for line in probe_lines:
126 full_bucket.append(line.rstrip())
127 probes.buckets.append(full_bucket)
128
129 # create the probes_all file?
130 else:
131 print >>sys.stderr, ("Could not find probes file")
132 sys.exit(1)
133
134 def write_probe_list():
135 global probes
136
137 print "Writing state..."
138 f = open(config_opts['probes_db'], 'w')
139 p = pickle.Pickler(f)
140 p.dump(probes)
141 f.close()
142
143 def display_probe_list():
144 global probes
145
146 i = 0
147 print "bucketA (%s) has %d entries" % \
148 (probes.bucketA_result_str(), len(probes.bucketA))
149 print "bucketB (%s) has %d entries" % \
150 (probes.bucketB_result_str(), len(probes.bucketB))
151 for bucket in probes.buckets:
152 print "set %d has %d entries" % (i, len(bucket))
153 i += 1
154 print "passed set has %d entries" % len(probes.passed)
155 total = 0
156 for bucket in probes.failed:
157 total += len(bucket)
158 print "failed set has %d entries (in %d buckets)" % \
159 (total, len(probes.failed))
160 print "untriggered set has %d entries" % len(probes.untriggered)
161 print "unregistered set has %d entries" % len(probes.unregistered)
162
163 def reset_buckets():
164 global probes
165
166 probes.bucketA = list()
167 probes.bucketA_result = 0
168 probes.bucketB = list()
169 probes.bucketB_result = 0
170 probes.split = 0
171
172 def grab_bucket():
173 global probes
174
175 reset_buckets()
176
177 # Try to grab the 1st bucket from the list.
178 if len(probes.buckets) > 0:
179 bucket = probes.buckets[0]
180 del probes.buckets[0]
181
182 # if the bucket has more than 1000 probes, limit it to 1000
183 if len(bucket) > 1000:
184 probes.bucketA = bucket[0:1000]
185 probes.bucketA_result = 0
186 probes.bucketB_result = 0
187
188 rest = bucket[1000:]
189 probes.buckets.insert(0, rest)
190
191 # otherwise just use the bucket
192 else:
193 probes.bucketA = bucket
194 return True
195 else:
196 print "no buckets left"
197 return False
198
199 def split_bucket(bucket):
200 global probes
201
202 if probes.split == 0:
203 split = len(bucket) / 2
204 else:
205 split = 1
206 bucketA = bucket[0:split]
207 bucketB = bucket[split:]
208
209 probes.bucketA = bucketA
210 probes.bucketA_result = 0
211 probes.bucketB = bucketB
212 probes.bucketB_result = 0
213
214 def update_buckets(failed=True):
215 global probes
216 ret = True
217
218 # if we don't have a current set, get one
219 if len(probes.bucketA) == 0:
220 ret = grab_bucket()
221
222 # if bucketA is set, we've just finished up with it (or bucketB).
223 elif len(probes.bucketA) > 0:
224 if probes.bucketA_result == 0:
225 if not failed:
226 probes.bucketA_result = 1
227 else:
228 probes.bucketA_result = 2
229 print "bucketA %s with %d probes..." % \
230 (probes.bucketA_result_str(), len(probes.bucketA))
231 else:
232 if not failed:
233 probes.bucketB_result = 1
234 else:
235 probes.bucketB_result = 2
236 print "bucketB %s with %d probes..." % \
237 (probes.bucketB_result_str(), len(probes.bucketB))
238
239 # OK, we've got several cases here.
240 # (1) Only bucketA was set.
241 if len(probes.bucketB) == 0:
242 print "case (1)..."
243 # (1a) If bucketA passed, put it on the passed list and grab
244 # the next bucket.
245 if not failed:
246 probes.passed.extend(probes.bucketA)
247 probes.split = 0
248 ret = grab_bucket()
249
250 # (1b) If bucketA failed and is more than 1 probe, split it.
251 elif len(probes.bucketA) > 1:
252 split_bucket(probes.bucketA)
253
254 # (1c) if bucketA failed and was only 1 probe, put it on
255 # the failed list and grab the next bucket.
256 else:
257 probes.failed.append(probes.bucketA)
258 ret = grab_bucket()
259
260 # (2) Both bucketA and bucketB were set, but bucketB hasn't
261 # been tested yet.
262 elif probes.bucketB_result == 0:
263 # Do nothing and let bucketB be tested.
264 print "case (2)..."
265 pass
266
267 # (3) Both bucketA and bucketB have been tested. Figure out
268 # what to do next.
269 else:
270 print "case (3)..."
271
272 # (3a) bucketA passed, bucketB failed. Move bucketA to
273 # the passed list and split bucketB.
274 if probes.bucketA_result == 1 and probes.bucketB_result == 2:
275 print "case (3a)..."
276 probes.passed.extend(probes.bucketA)
277 probes.split = 0
278
279 # (3a1) If bucketB failed and is more than 1 probe,
280 # split it.
281 if len(probes.bucketB) > 1:
282 split_bucket(probes.bucketB)
283
284 # (3a2) If bucketB failed and was only 1 probe, put it
285 # on the failed list and grab the next bucket.
286 else:
287 probes.failed.append(probes.bucketB)
288 ret = grab_bucket()
289
290 # (3b) bucketA failed, bucketB passed. Move bucketB to
291 # the passed list and split bucketA.
292 elif probes.bucketA_result == 2 and probes.bucketB_result == 1:
293 print "case (3b)..."
294 probes.passed.extend(probes.bucketB)
295 probes.split = 0
296
297 # (3b1) If bucketA failed and is more than 1 probe,
298 # split it.
299 if len(probes.bucketA) > 1:
300 split_bucket(probes.bucketA)
301
302 # (3b2) If bucketA failed and was only 1 probe, put it
303 # on the failed list and grab the next bucket.
304 else:
305 probes.failed.append(probes.bucketA)
306 ret = grab_bucket()
307
308 # (3c) Both buckets failed.
309 elif probes.bucketA_result == 2 and probes.bucketB_result == 2:
310 print "case (3c)..."
311 # (3c1) bucketA and bucketB were both just 1 probe
312 # each. Put them both on the failed list and grab the
313 # next bucket.
314 if len(probes.bucketA) == 1 and len(probes.bucketB) == 1:
315 print "case (3c1)..."
316 probes.failed.append(probes.bucketA)
317 probes.failed.append(probes.bucketB)
318 ret = grab_bucket()
319
320 # (3c2) bucketA was just 1 probe, but bucketB was more
321 # than 1 probe
322 elif len(probes.bucketA) == 1 and len(probes.bucketB) > 1:
323 print "case (3c2)..."
324 probes.failed.append(probes.bucketA)
325 split_bucket(probes.bucketB)
326 # (3c3) bucketA was more than 1 probe, but bucketB was
327 # just 1 probe
328 elif len(probes.bucketA) > 1 and len(probes.bucketB) == 1:
329 print "case (3c3)..."
330 probes.failed.append(probes.bucketB)
331 split_bucket(probes.bucketA)
332 # (3c4) Both buckets were more than 1 probe. In this
333 # case, split bucketA and put bucketB back on the main
334 # list. This will cause bucketB to get tested twice.
335 else:
336 print "case (3c4)..."
337 probes.buckets.insert(0, probes.bucketB)
338 split_bucket(probes.bucketA)
339
340 # (3d) Both buckets passed. This sounds good, but this
341 # means that together bucketA and bucketB failed, but
342 # separately they passed. So, the combination of the sets
343 # is the problem.
344 else:
345 print "case (3d)..."
346 # (3d1) If both buckets are just 1 probe, combine the
347 # buckets into one and put it on the failed list.
348 # Then grab the next bucket.
349 if len(probes.bucketA) == 1 and len(probes.bucketB) == 1:
350 bucket = probes.bucketA + probes.bucketB
351 probes.failed.append(bucket)
352 ret = grab_bucket()
353
354 # (3d2) Combine both buckets here, and try eliminating
355 # the probes 1-by-1. We'll go back to bisecting if we
356 # can remove 1 probe from this list or the list is
357 # indivisible (case 3d3a).
358 elif probes.split == 0:
359 probes.split = 1
360 bucket = probes.bucketA + probes.bucketB
361 split_bucket(bucket)
362 # (3d3) We're eliminating the probes 1-by-1 and the
363 # last attempt failed. Reverse the order and try
364 # again.
365 else:
366 probes.split += 1
367 print "case (3d3): bucketA(%d) bucketB(%d)" % (len(probes.bucketA), len(probes.bucketB))
368 bucket = probes.bucketB + probes.bucketA
369 print "case (3d3): combined bucket(%d)" % (len(bucket))
370 if len(bucket) > 500:
371 print >>sys.stderr, "Error: bucket grew?"
372 return -1
373
374 # (3d3a) If we've tried every probe singly, and
375 # they all still worked, the combination is still
376 # the problem. So, we're done trying 1-by-1
377 # elimination.
378 if probes.split > len(bucket):
379 probes.split = 0
380 probes.failed.append(bucket)
381 ret = grab_bucket()
382 # (3d3b) Keep trying 1-by-1 splits.
383 else:
384 split_bucket(bucket)
385
386 write_probe_list()
387 return ret
388
389 def save_bucket():
390 global probes
391
392 f = open(config_opts['probes_current'], 'w')
393 if len(probes.bucketA) > 0 and probes.bucketA_result == 0:
394 f.write("\n".join(probes.bucketA))
395 elif len(probes.bucketB) > 0 and probes.bucketB_result == 0:
396 f.write("\n".join(probes.bucketB))
397 else:
398 print "Error: no bucket to write?"
399 f.close()
400
401 def parse_module_output():
402 global probes
403
404 # Parse the output file, looking for probe points
405 pp_re = re.compile(": (-?\d+) (\S+)$")
406 f = open(config_opts['probes_result'], 'r')
407 pp = dict()
408 line = f.readline()
409 while line:
410 match = pp_re.search(line)
411 if match:
412 pp[match.group(2)] = int(match.group(1))
413 line = f.readline()
414 f.close()
415
416 if len(pp.keys()) == 0:
417 print >>sys.stderr, "No data found?"
418 return 1
419
420 # We're done with the result file
421 os.unlink(config_opts['probes_result'])
422
423 # Parse the list of probe points. Since the result fields haven't
424 # been updated yet, pick the 1st bucket that has a status of 0
425 # (untested).
426 if probes.bucketA_result == 0:
427 bucket = probes.bucketA
428 else:
429 bucket = probes.bucketB
430
431 new_bucket = list()
432 for probe in bucket:
433 if pp.has_key(probe):
434 # > 0 == passed (registered and triggered)
435 if pp[probe] > 0:
436 new_bucket.append(probe)
437 # 0 == untriggered
438 elif pp[probe] == 0:
439 probes.untriggered.append(probe)
440 # -1 == unregistered
441 elif pp[probe] == -1:
442 probes.unregistered.append(probe)
443 else:
444 print >>sys.stderr, "failed probe %s?" % probe
445 else:
446 print >>sys.stderr, "Couldn't find %s?" % probe
447
448 # OK, we've gone through and removed all the
449 # untriggered/unregistered probes from bucket. Update the
450 # proper bucket.
451 if probes.bucketA_result == 0:
452 # Oops, all the probes were unregistered/untriggered.
453 if len(new_bucket) == 0:
454 # If we've got a bucketB, we'll think it just succeeded.
455 # So, put it back on the bucket list so it will get
456 # correctly processed.
457 if len(probes.bucketB):
458 print "Re-inserting bucketB..."
459 probes.buckets.insert(0, probes.bucketB)
460
461 # Reset everything
462 reset_buckets()
463 else:
464 probes.bucketA = new_bucket
465 else:
466 # If bucketB ended up with 0 entries, we need to handle
467 # bucketA.
468 if len(new_bucket) == 0:
469 # bucketA suceeded and bucketB ended up with 0 entries.
470 # Put bucketA on the 'passed' list.
471 if probes.bucketA_result == 1:
472 probes.passed.extend(probes.bucketA)
473 # bucketA failed and bucketB ended up with 0 entries.
474 # Put bucketA on the bucket list to get processed again.
475 # This will cause it to get tested twice, but it is the
476 # easiest way out.
477 else:
478 print "Re-inserting bucketA..."
479 probes.buckets.insert(0, probes.bucketA)
480
481 # Reset everything.
482 reset_buckets()
483 else:
484 probes.bucketB = new_bucket
485
486 return 0
487
488 def run_tests():
489 status = True
490 failed = True
491 while status:
492 status = update_buckets(failed)
493 display_probe_list()
494 if not status:
495 break
496
497 # Generate the module.
498 save_bucket()
499 rc = gen_module()
500 if rc != 0:
501 sys.exit(rc)
502
503 # Run the module.
504 sync_disks()
505 rc = run_module()
506 if rc != 0:
507 sys.exit(rc)
508
509 # Parse the module output.
510 rc = parse_module_output()
511 if rc != 0:
512 sys.exit(rc)
513
514 # If we're here, the current module was loaded and unloaded
515 # successfully.
516 failed = False
517
518 def dump_output():
519 global probes
520
521 f = open(config_opts['probes_passed'], 'w')
522 if len(probes.passed) > 0:
523 f.write("\n".join(probes.passed))
524 f.close()
525
526 # probes.failed is a list of lists.
527 f = open(config_opts['probes_failed'], 'w')
528 for bucket in probes.failed:
529 f.write("\n".join(bucket))
530 f.write("\n#\n")
531 f.close()
532
533 f = open(config_opts['probes_untriggered'], 'w')
534 if len(probes.untriggered) > 0:
535 f.write("\n".join(probes.untriggered))
536 f.close()
537
538 f = open(config_opts['probes_unregistered'], 'w')
539 if len(probes.unregistered) > 0:
540 f.write("\n".join(probes.unregistered))
541 f.close()
542
543 # Make sure we're running as root.
544 if os.getuid() != 0:
545 print >>sys.stderr, "Error: this script must be run by root"
546 sys.exit(1)
547
548 # Register this script. If this is the 1st time we've been run, start
549 # from scratch by removing old state files.
550 if register_script():
551 print >>sys.stderr, "Removing old state files..."
552 if os.path.exists(config_opts['log_file']):
553 os.unlink(config_opts['log_file'])
554 if os.path.exists(config_opts['probes_db']):
555 os.unlink(config_opts['probes_db'])
556
557 # Redirect stdout and stderr
558 sys.stdout.flush()
559 sys.stderr.flush()
560 so = open(config_opts['log_file'], 'a+', 0) # no buffering
561 os.dup2(so.fileno(), sys.stdout.fileno())
562 os.dup2(so.fileno(), sys.stderr.fileno())
563
564 # Go!
565 read_probe_list()
566 run_tests()
567
568 # Finish up.
569 dump_output()
570 register_script(False)
571 print >>sys.stderr, "Finished."