netdev-times.py 15 KB

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