net_dropmonitor.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Monitor the system for dropped packets and proudce a report of drop locations and counts
  2. import os
  3. import sys
  4. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  5. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  6. from perf_trace_context import *
  7. from Core import *
  8. from Util import *
  9. drop_log = {}
  10. kallsyms = []
  11. def get_kallsyms_table():
  12. global kallsyms
  13. try:
  14. f = open("/proc/kallsyms", "r")
  15. except:
  16. return
  17. for line in f:
  18. loc = int(line.split()[0], 16)
  19. name = line.split()[2]
  20. kallsyms.append((loc, name))
  21. kallsyms.sort()
  22. def get_sym(sloc):
  23. loc = int(sloc)
  24. # Invariant: kallsyms[i][0] <= loc for all 0 <= i <= start
  25. # kallsyms[i][0] > loc for all end <= i < len(kallsyms)
  26. start, end = -1, len(kallsyms)
  27. while end != start + 1:
  28. pivot = (start + end) // 2
  29. if loc < kallsyms[pivot][0]:
  30. end = pivot
  31. else:
  32. start = pivot
  33. # Now (start == -1 or kallsyms[start][0] <= loc)
  34. # and (start == len(kallsyms) - 1 or loc < kallsyms[start + 1][0])
  35. if start >= 0:
  36. symloc, name = kallsyms[start]
  37. return (name, loc - symloc)
  38. else:
  39. return (None, 0)
  40. def print_drop_table():
  41. print "%25s %25s %25s" % ("LOCATION", "OFFSET", "COUNT")
  42. for i in drop_log.keys():
  43. (sym, off) = get_sym(i)
  44. if sym == None:
  45. sym = i
  46. print "%25s %25s %25s" % (sym, off, drop_log[i])
  47. def trace_begin():
  48. print "Starting trace (Ctrl-C to dump results)"
  49. def trace_end():
  50. print "Gathering kallsyms data"
  51. get_kallsyms_table()
  52. print_drop_table()
  53. # called from perf, when it finds a correspoinding event
  54. def skb__kfree_skb(name, context, cpu, sec, nsec, pid, comm,
  55. skbaddr, location, protocol):
  56. slocation = str(location)
  57. try:
  58. drop_log[slocation] = drop_log[slocation] + 1
  59. except:
  60. drop_log[slocation] = 1