show_delta 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/python
  2. #
  3. # show_deltas: Read list of printk messages instrumented with
  4. # time data, and format with time deltas.
  5. #
  6. # Also, you can show the times relative to a fixed point.
  7. #
  8. # Copyright 2003 Sony Corporation
  9. #
  10. # GPL 2.0 applies.
  11. import sys
  12. import string
  13. def usage():
  14. print ("""usage: show_delta [<options>] <filename>
  15. This program parses the output from a set of printk message lines which
  16. have time data prefixed because the CONFIG_PRINTK_TIME option is set, or
  17. the kernel command line option "time" is specified. When run with no
  18. options, the time information is converted to show the time delta between
  19. each printk line and the next. When run with the '-b' option, all times
  20. are relative to a single (base) point in time.
  21. Options:
  22. -h Show this usage help.
  23. -b <base> Specify a base for time references.
  24. <base> can be a number or a string.
  25. If it is a string, the first message line
  26. which matches (at the beginning of the
  27. line) is used as the time reference.
  28. ex: $ dmesg >timefile
  29. $ show_delta -b NET4 timefile
  30. will show times relative to the line in the kernel output
  31. starting with "NET4".
  32. """)
  33. sys.exit(1)
  34. # returns a tuple containing the seconds and text for each message line
  35. # seconds is returned as a float
  36. # raise an exception if no timing data was found
  37. def get_time(line):
  38. if line[0]!="[":
  39. raise ValueError
  40. # split on closing bracket
  41. (time_str, rest) = string.split(line[1:],']',1)
  42. time = string.atof(time_str)
  43. #print "time=", time
  44. return (time, rest)
  45. # average line looks like:
  46. # [ 0.084282] VFS: Mounted root (romfs filesystem) readonly
  47. # time data is expressed in seconds.useconds,
  48. # convert_line adds a delta for each line
  49. last_time = 0.0
  50. def convert_line(line, base_time):
  51. global last_time
  52. try:
  53. (time, rest) = get_time(line)
  54. except:
  55. # if any problem parsing time, don't convert anything
  56. return line
  57. if base_time:
  58. # show time from base
  59. delta = time - base_time
  60. else:
  61. # just show time from last line
  62. delta = time - last_time
  63. last_time = time
  64. return ("[%5.6f < %5.6f >]" % (time, delta)) + rest
  65. def main():
  66. base_str = ""
  67. filein = ""
  68. for arg in sys.argv[1:]:
  69. if arg=="-b":
  70. base_str = sys.argv[sys.argv.index("-b")+1]
  71. elif arg=="-h":
  72. usage()
  73. else:
  74. filein = arg
  75. if not filein:
  76. usage()
  77. try:
  78. lines = open(filein,"r").readlines()
  79. except:
  80. print ("Problem opening file: %s" % filein)
  81. sys.exit(1)
  82. if base_str:
  83. print ('base= "%s"' % base_str)
  84. # assume a numeric base. If that fails, try searching
  85. # for a matching line.
  86. try:
  87. base_time = float(base_str)
  88. except:
  89. # search for line matching <base> string
  90. found = 0
  91. for line in lines:
  92. try:
  93. (time, rest) = get_time(line)
  94. except:
  95. continue
  96. if string.find(rest, base_str)==1:
  97. base_time = time
  98. found = 1
  99. # stop at first match
  100. break
  101. if not found:
  102. print ('Couldn\'t find line matching base pattern "%s"' % base_str)
  103. sys.exit(1)
  104. else:
  105. base_time = 0.0
  106. for line in lines:
  107. print (convert_line(line, base_time),)
  108. main()