net_dropmonitor.py 1.7 KB

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