net_dropmonitor.py 1.8 KB

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