netdev-times.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. # Display a process of packets and processed time.
  2. # It helps us to investigate networking or network device.
  3. #
  4. # options
  5. # tx: show only tx chart
  6. # rx: show only rx chart
  7. # dev=: show only thing related to specified device
  8. # debug: work with debug mode. It shows buffer status.
  9. import os
  10. import sys
  11. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  12. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  13. from perf_trace_context import *
  14. from Core import *
  15. from Util import *
  16. all_event_list = []; # insert all tracepoint event related with this script
  17. irq_dic = {}; # key is cpu and value is a list which stacks irqs
  18. # which raise NET_RX softirq
  19. net_rx_dic = {}; # key is cpu and value include time of NET_RX softirq-entry
  20. # and a list which stacks receive
  21. receive_hunk_list = []; # a list which include a sequence of receive events
  22. rx_skb_list = []; # received packet list for matching
  23. # skb_copy_datagram_iovec
  24. buffer_budget = 65536; # the budget of rx_skb_list, tx_queue_list and
  25. # tx_xmit_list
  26. of_count_rx_skb_list = 0; # overflow count
  27. tx_queue_list = []; # list of packets which pass through dev_queue_xmit
  28. of_count_tx_queue_list = 0; # overflow count
  29. tx_xmit_list = []; # list of packets which pass through dev_hard_start_xmit
  30. of_count_tx_xmit_list = 0; # overflow count
  31. tx_free_list = []; # list of packets which is freed
  32. # options
  33. show_tx = 0;
  34. show_rx = 0;
  35. dev = 0; # store a name of device specified by option "dev="
  36. debug = 0;
  37. # indices of event_info tuple
  38. EINFO_IDX_NAME= 0
  39. EINFO_IDX_CONTEXT=1
  40. EINFO_IDX_CPU= 2
  41. EINFO_IDX_TIME= 3
  42. EINFO_IDX_PID= 4
  43. EINFO_IDX_COMM= 5
  44. # Calculate a time interval(msec) from src(nsec) to dst(nsec)
  45. def diff_msec(src, dst):
  46. return (dst - src) / 1000000.0
  47. # Display a process of transmitting a packet
  48. def print_transmit(hunk):
  49. if dev != 0 and hunk['dev'].find(dev) < 0:
  50. return
  51. print "%7s %5d %6d.%06dsec %12.3fmsec %12.3fmsec" % \
  52. (hunk['dev'], hunk['len'],
  53. nsecs_secs(hunk['queue_t']),
  54. nsecs_nsecs(hunk['queue_t'])/1000,
  55. diff_msec(hunk['queue_t'], hunk['xmit_t']),
  56. diff_msec(hunk['xmit_t'], hunk['free_t']))
  57. # Format for displaying rx packet processing
  58. PF_IRQ_ENTRY= " irq_entry(+%.3fmsec irq=%d:%s)"
  59. PF_SOFT_ENTRY=" softirq_entry(+%.3fmsec)"
  60. PF_NAPI_POLL= " napi_poll_exit(+%.3fmsec %s)"
  61. PF_JOINT= " |"
  62. PF_WJOINT= " | |"
  63. PF_NET_RECV= " |---netif_receive_skb(+%.3fmsec skb=%x len=%d)"
  64. PF_NET_RX= " |---netif_rx(+%.3fmsec skb=%x)"
  65. PF_CPY_DGRAM= " | skb_copy_datagram_iovec(+%.3fmsec %d:%s)"
  66. PF_KFREE_SKB= " | kfree_skb(+%.3fmsec location=%x)"
  67. PF_CONS_SKB= " | consume_skb(+%.3fmsec)"
  68. # Display a process of received packets and interrputs associated with
  69. # a NET_RX softirq
  70. def print_receive(hunk):
  71. show_hunk = 0
  72. irq_list = hunk['irq_list']
  73. cpu = irq_list[0]['cpu']
  74. base_t = irq_list[0]['irq_ent_t']
  75. # check if this hunk should be showed
  76. if dev != 0:
  77. for i in range(len(irq_list)):
  78. if irq_list[i]['name'].find(dev) >= 0:
  79. show_hunk = 1
  80. break
  81. else:
  82. show_hunk = 1
  83. if show_hunk == 0:
  84. return
  85. print "%d.%06dsec cpu=%d" % \
  86. (nsecs_secs(base_t), nsecs_nsecs(base_t)/1000, cpu)
  87. for i in range(len(irq_list)):
  88. print PF_IRQ_ENTRY % \
  89. (diff_msec(base_t, irq_list[i]['irq_ent_t']),
  90. irq_list[i]['irq'], irq_list[i]['name'])
  91. print PF_JOINT
  92. irq_event_list = irq_list[i]['event_list']
  93. for j in range(len(irq_event_list)):
  94. irq_event = irq_event_list[j]
  95. if irq_event['event'] == 'netif_rx':
  96. print PF_NET_RX % \
  97. (diff_msec(base_t, irq_event['time']),
  98. irq_event['skbaddr'])
  99. print PF_JOINT
  100. print PF_SOFT_ENTRY % \
  101. diff_msec(base_t, hunk['sirq_ent_t'])
  102. print PF_JOINT
  103. event_list = hunk['event_list']
  104. for i in range(len(event_list)):
  105. event = event_list[i]
  106. if event['event_name'] == 'napi_poll':
  107. print PF_NAPI_POLL % \
  108. (diff_msec(base_t, event['event_t']), event['dev'])
  109. if i == len(event_list) - 1:
  110. print ""
  111. else:
  112. print PF_JOINT
  113. else:
  114. print PF_NET_RECV % \
  115. (diff_msec(base_t, event['event_t']), event['skbaddr'],
  116. event['len'])
  117. if 'comm' in event.keys():
  118. print PF_WJOINT
  119. print PF_CPY_DGRAM % \
  120. (diff_msec(base_t, event['comm_t']),
  121. event['pid'], event['comm'])
  122. elif 'handle' in event.keys():
  123. print PF_WJOINT
  124. if event['handle'] == "kfree_skb":
  125. print PF_KFREE_SKB % \
  126. (diff_msec(base_t,
  127. event['comm_t']),
  128. event['location'])
  129. elif event['handle'] == "consume_skb":
  130. print PF_CONS_SKB % \
  131. diff_msec(base_t,
  132. event['comm_t'])
  133. print PF_JOINT
  134. def trace_begin():
  135. global show_tx
  136. global show_rx
  137. global dev
  138. global debug
  139. for i in range(len(sys.argv)):
  140. if i == 0:
  141. continue
  142. arg = sys.argv[i]
  143. if arg == 'tx':
  144. show_tx = 1
  145. elif arg =='rx':
  146. show_rx = 1
  147. elif arg.find('dev=',0, 4) >= 0:
  148. dev = arg[4:]
  149. elif arg == 'debug':
  150. debug = 1
  151. if show_tx == 0 and show_rx == 0:
  152. show_tx = 1
  153. show_rx = 1
  154. def trace_end():
  155. # order all events in time
  156. all_event_list.sort(lambda a,b :cmp(a[EINFO_IDX_TIME],
  157. b[EINFO_IDX_TIME]))
  158. # process all events
  159. for i in range(len(all_event_list)):
  160. event_info = all_event_list[i]
  161. name = event_info[EINFO_IDX_NAME]
  162. if name == 'irq__softirq_exit':
  163. handle_irq_softirq_exit(event_info)
  164. elif name == 'irq__softirq_entry':
  165. handle_irq_softirq_entry(event_info)
  166. elif name == 'irq__softirq_raise':
  167. handle_irq_softirq_raise(event_info)
  168. elif name == 'irq__irq_handler_entry':
  169. handle_irq_handler_entry(event_info)
  170. elif name == 'irq__irq_handler_exit':
  171. handle_irq_handler_exit(event_info)
  172. elif name == 'napi__napi_poll':
  173. handle_napi_poll(event_info)
  174. elif name == 'net__netif_receive_skb':
  175. handle_netif_receive_skb(event_info)
  176. elif name == 'net__netif_rx':
  177. handle_netif_rx(event_info)
  178. elif name == 'skb__skb_copy_datagram_iovec':
  179. handle_skb_copy_datagram_iovec(event_info)
  180. elif name == 'net__net_dev_queue':
  181. handle_net_dev_queue(event_info)
  182. elif name == 'net__net_dev_xmit':
  183. handle_net_dev_xmit(event_info)
  184. elif name == 'skb__kfree_skb':
  185. handle_kfree_skb(event_info)
  186. elif name == 'skb__consume_skb':
  187. handle_consume_skb(event_info)
  188. # display receive hunks
  189. if show_rx:
  190. for i in range(len(receive_hunk_list)):
  191. print_receive(receive_hunk_list[i])
  192. # display transmit hunks
  193. if show_tx:
  194. print " dev len Qdisc " \
  195. " netdevice free"
  196. for i in range(len(tx_free_list)):
  197. print_transmit(tx_free_list[i])
  198. if debug:
  199. print "debug buffer status"
  200. print "----------------------------"
  201. print "xmit Qdisc:remain:%d overflow:%d" % \
  202. (len(tx_queue_list), of_count_tx_queue_list)
  203. print "xmit netdevice:remain:%d overflow:%d" % \
  204. (len(tx_xmit_list), of_count_tx_xmit_list)
  205. print "receive:remain:%d overflow:%d" % \
  206. (len(rx_skb_list), of_count_rx_skb_list)
  207. # called from perf, when it finds a correspoinding event
  208. def irq__softirq_entry(name, context, cpu, sec, nsec, pid, comm, callchain, vec):
  209. if symbol_str("irq__softirq_entry", "vec", vec) != "NET_RX":
  210. return
  211. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm, vec)
  212. all_event_list.append(event_info)
  213. def irq__softirq_exit(name, context, cpu, sec, nsec, pid, comm, callchain, vec):
  214. if symbol_str("irq__softirq_entry", "vec", vec) != "NET_RX":
  215. return
  216. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm, vec)
  217. all_event_list.append(event_info)
  218. def irq__softirq_raise(name, context, cpu, sec, nsec, pid, comm, callchain, vec):
  219. if symbol_str("irq__softirq_entry", "vec", vec) != "NET_RX":
  220. return
  221. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm, vec)
  222. all_event_list.append(event_info)
  223. def irq__irq_handler_entry(name, context, cpu, sec, nsec, pid, comm,
  224. callchain, irq, irq_name):
  225. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  226. irq, irq_name)
  227. all_event_list.append(event_info)
  228. def irq__irq_handler_exit(name, context, cpu, sec, nsec, pid, comm, callchain, irq, ret):
  229. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm, irq, ret)
  230. all_event_list.append(event_info)
  231. def napi__napi_poll(name, context, cpu, sec, nsec, pid, comm, callchain, napi,
  232. dev_name, work=None, budget=None):
  233. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  234. napi, dev_name, work, budget)
  235. all_event_list.append(event_info)
  236. def net__netif_receive_skb(name, context, cpu, sec, nsec, pid, comm, callchain, skbaddr,
  237. skblen, dev_name):
  238. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  239. skbaddr, skblen, dev_name)
  240. all_event_list.append(event_info)
  241. def net__netif_rx(name, context, cpu, sec, nsec, pid, comm, callchain, skbaddr,
  242. skblen, dev_name):
  243. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  244. skbaddr, skblen, dev_name)
  245. all_event_list.append(event_info)
  246. def net__net_dev_queue(name, context, cpu, sec, nsec, pid, comm, callchain,
  247. skbaddr, skblen, dev_name):
  248. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  249. skbaddr, skblen, dev_name)
  250. all_event_list.append(event_info)
  251. def net__net_dev_xmit(name, context, cpu, sec, nsec, pid, comm, callchain,
  252. skbaddr, skblen, rc, dev_name):
  253. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  254. skbaddr, skblen, rc ,dev_name)
  255. all_event_list.append(event_info)
  256. def skb__kfree_skb(name, context, cpu, sec, nsec, pid, comm, callchain,
  257. skbaddr, protocol, location):
  258. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  259. skbaddr, protocol, location)
  260. all_event_list.append(event_info)
  261. def skb__consume_skb(name, context, cpu, sec, nsec, pid, comm, callchain, skbaddr):
  262. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  263. skbaddr)
  264. all_event_list.append(event_info)
  265. def skb__skb_copy_datagram_iovec(name, context, cpu, sec, nsec, pid, comm, callchain,
  266. skbaddr, skblen):
  267. event_info = (name, context, cpu, nsecs(sec, nsec), pid, comm,
  268. skbaddr, skblen)
  269. all_event_list.append(event_info)
  270. def handle_irq_handler_entry(event_info):
  271. (name, context, cpu, time, pid, comm, irq, irq_name) = event_info
  272. if cpu not in irq_dic.keys():
  273. irq_dic[cpu] = []
  274. irq_record = {'irq':irq, 'name':irq_name, 'cpu':cpu, 'irq_ent_t':time}
  275. irq_dic[cpu].append(irq_record)
  276. def handle_irq_handler_exit(event_info):
  277. (name, context, cpu, time, pid, comm, irq, ret) = event_info
  278. if cpu not in irq_dic.keys():
  279. return
  280. irq_record = irq_dic[cpu].pop()
  281. if irq != irq_record['irq']:
  282. return
  283. irq_record.update({'irq_ext_t':time})
  284. # if an irq doesn't include NET_RX softirq, drop.
  285. if 'event_list' in irq_record.keys():
  286. irq_dic[cpu].append(irq_record)
  287. def handle_irq_softirq_raise(event_info):
  288. (name, context, cpu, time, pid, comm, vec) = event_info
  289. if cpu not in irq_dic.keys() \
  290. or len(irq_dic[cpu]) == 0:
  291. return
  292. irq_record = irq_dic[cpu].pop()
  293. if 'event_list' in irq_record.keys():
  294. irq_event_list = irq_record['event_list']
  295. else:
  296. irq_event_list = []
  297. irq_event_list.append({'time':time, 'event':'sirq_raise'})
  298. irq_record.update({'event_list':irq_event_list})
  299. irq_dic[cpu].append(irq_record)
  300. def handle_irq_softirq_entry(event_info):
  301. (name, context, cpu, time, pid, comm, vec) = event_info
  302. net_rx_dic[cpu] = {'sirq_ent_t':time, 'event_list':[]}
  303. def handle_irq_softirq_exit(event_info):
  304. (name, context, cpu, time, pid, comm, vec) = event_info
  305. irq_list = []
  306. event_list = 0
  307. if cpu in irq_dic.keys():
  308. irq_list = irq_dic[cpu]
  309. del irq_dic[cpu]
  310. if cpu in net_rx_dic.keys():
  311. sirq_ent_t = net_rx_dic[cpu]['sirq_ent_t']
  312. event_list = net_rx_dic[cpu]['event_list']
  313. del net_rx_dic[cpu]
  314. if irq_list == [] or event_list == 0:
  315. return
  316. rec_data = {'sirq_ent_t':sirq_ent_t, 'sirq_ext_t':time,
  317. 'irq_list':irq_list, 'event_list':event_list}
  318. # merge information realted to a NET_RX softirq
  319. receive_hunk_list.append(rec_data)
  320. def handle_napi_poll(event_info):
  321. (name, context, cpu, time, pid, comm, napi, dev_name,
  322. work, budget) = event_info
  323. if cpu in net_rx_dic.keys():
  324. event_list = net_rx_dic[cpu]['event_list']
  325. rec_data = {'event_name':'napi_poll',
  326. 'dev':dev_name, 'event_t':time,
  327. 'work':work, 'budget':budget}
  328. event_list.append(rec_data)
  329. def handle_netif_rx(event_info):
  330. (name, context, cpu, time, pid, comm,
  331. skbaddr, skblen, dev_name) = event_info
  332. if cpu not in irq_dic.keys() \
  333. or len(irq_dic[cpu]) == 0:
  334. return
  335. irq_record = irq_dic[cpu].pop()
  336. if 'event_list' in irq_record.keys():
  337. irq_event_list = irq_record['event_list']
  338. else:
  339. irq_event_list = []
  340. irq_event_list.append({'time':time, 'event':'netif_rx',
  341. 'skbaddr':skbaddr, 'skblen':skblen, 'dev_name':dev_name})
  342. irq_record.update({'event_list':irq_event_list})
  343. irq_dic[cpu].append(irq_record)
  344. def handle_netif_receive_skb(event_info):
  345. global of_count_rx_skb_list
  346. (name, context, cpu, time, pid, comm,
  347. skbaddr, skblen, dev_name) = event_info
  348. if cpu in net_rx_dic.keys():
  349. rec_data = {'event_name':'netif_receive_skb',
  350. 'event_t':time, 'skbaddr':skbaddr, 'len':skblen}
  351. event_list = net_rx_dic[cpu]['event_list']
  352. event_list.append(rec_data)
  353. rx_skb_list.insert(0, rec_data)
  354. if len(rx_skb_list) > buffer_budget:
  355. rx_skb_list.pop()
  356. of_count_rx_skb_list += 1
  357. def handle_net_dev_queue(event_info):
  358. global of_count_tx_queue_list
  359. (name, context, cpu, time, pid, comm,
  360. skbaddr, skblen, dev_name) = event_info
  361. skb = {'dev':dev_name, 'skbaddr':skbaddr, 'len':skblen, 'queue_t':time}
  362. tx_queue_list.insert(0, skb)
  363. if len(tx_queue_list) > buffer_budget:
  364. tx_queue_list.pop()
  365. of_count_tx_queue_list += 1
  366. def handle_net_dev_xmit(event_info):
  367. global of_count_tx_xmit_list
  368. (name, context, cpu, time, pid, comm,
  369. skbaddr, skblen, rc, dev_name) = event_info
  370. if rc == 0: # NETDEV_TX_OK
  371. for i in range(len(tx_queue_list)):
  372. skb = tx_queue_list[i]
  373. if skb['skbaddr'] == skbaddr:
  374. skb['xmit_t'] = time
  375. tx_xmit_list.insert(0, skb)
  376. del tx_queue_list[i]
  377. if len(tx_xmit_list) > buffer_budget:
  378. tx_xmit_list.pop()
  379. of_count_tx_xmit_list += 1
  380. return
  381. def handle_kfree_skb(event_info):
  382. (name, context, cpu, time, pid, comm,
  383. skbaddr, protocol, location) = event_info
  384. for i in range(len(tx_queue_list)):
  385. skb = tx_queue_list[i]
  386. if skb['skbaddr'] == skbaddr:
  387. del tx_queue_list[i]
  388. return
  389. for i in range(len(tx_xmit_list)):
  390. skb = tx_xmit_list[i]
  391. if skb['skbaddr'] == skbaddr:
  392. skb['free_t'] = time
  393. tx_free_list.append(skb)
  394. del tx_xmit_list[i]
  395. return
  396. for i in range(len(rx_skb_list)):
  397. rec_data = rx_skb_list[i]
  398. if rec_data['skbaddr'] == skbaddr:
  399. rec_data.update({'handle':"kfree_skb",
  400. 'comm':comm, 'pid':pid, 'comm_t':time})
  401. del rx_skb_list[i]
  402. return
  403. def handle_consume_skb(event_info):
  404. (name, context, cpu, time, pid, comm, skbaddr) = event_info
  405. for i in range(len(tx_xmit_list)):
  406. skb = tx_xmit_list[i]
  407. if skb['skbaddr'] == skbaddr:
  408. skb['free_t'] = time
  409. tx_free_list.append(skb)
  410. del tx_xmit_list[i]
  411. return
  412. def handle_skb_copy_datagram_iovec(event_info):
  413. (name, context, cpu, time, pid, comm, skbaddr, skblen) = event_info
  414. for i in range(len(rx_skb_list)):
  415. rec_data = rx_skb_list[i]
  416. if skbaddr == rec_data['skbaddr']:
  417. rec_data.update({'handle':"skb_copy_datagram_iovec",
  418. 'comm':comm, 'pid':pid, 'comm_t':time})
  419. del rx_skb_list[i]
  420. return