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