net_dropmonitor.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. linecount = 0
  16. for line in f:
  17. linecount = linecount+1
  18. f.seek(0)
  19. except:
  20. return
  21. j = 0
  22. for line in f:
  23. loc = int(line.split()[0], 16)
  24. name = line.split()[2]
  25. j = j +1
  26. if ((j % 100) == 0):
  27. print "\r" + str(j) + "/" + str(linecount),
  28. kallsyms.append((loc, name))
  29. print "\r" + str(j) + "/" + str(linecount)
  30. kallsyms.sort()
  31. return
  32. def get_sym(sloc):
  33. loc = int(sloc)
  34. # Invariant: kallsyms[i][0] <= loc for all 0 <= i <= start
  35. # kallsyms[i][0] > loc for all end <= i < len(kallsyms)
  36. start, end = -1, len(kallsyms)
  37. while end != start + 1:
  38. pivot = (start + end) // 2
  39. if loc < kallsyms[pivot][0]:
  40. end = pivot
  41. else:
  42. start = pivot
  43. # Now (start == -1 or kallsyms[start][0] <= loc)
  44. # and (start == len(kallsyms) - 1 or loc < kallsyms[start + 1][0])
  45. if start >= 0:
  46. symloc, name = kallsyms[start]
  47. return (name, loc - symloc)
  48. else:
  49. return (None, 0)
  50. def print_drop_table():
  51. print "%25s %25s %25s" % ("LOCATION", "OFFSET", "COUNT")
  52. for i in drop_log.keys():
  53. (sym, off) = get_sym(i)
  54. if sym == None:
  55. sym = i
  56. print "%25s %25s %25s" % (sym, off, drop_log[i])
  57. def trace_begin():
  58. print "Starting trace (Ctrl-C to dump results)"
  59. def trace_end():
  60. print "Gathering kallsyms data"
  61. get_kallsyms_table()
  62. print_drop_table()
  63. # called from perf, when it finds a correspoinding event
  64. def skb__kfree_skb(name, context, cpu, sec, nsec, pid, comm,
  65. skbaddr, location, protocol):
  66. slocation = str(location)
  67. try:
  68. drop_log[slocation] = drop_log[slocation] + 1
  69. except:
  70. drop_log[slocation] = 1