Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1257892
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: LINUX

2015-01-27 11:12:32

5.1.1. Network Profiling

This section describes how to profile network activity. provides a glimpse into how much network traffic each process is generating on a machine.
nettop.stp

点击(此处)折叠或打开

  1. #! /usr/bin/env stap

  2. global ifxmit, ifrecv
  3. global ifmerged

  4. probe netdev.transmit
  5. {
  6.   ifxmit[pid(), dev_name, execname(), uid()] <<< length
  7. }

  8. probe netdev.receive
  9. {
  10.   ifrecv[pid(), dev_name, execname(), uid()] <<< length
  11. }

  12. function print_activity()
  13. {
  14.   printf("%5s %5s %-7s %7s %7s %7s %7s %-15s\n",
  15.          "PID", "UID", "DEV", "XMIT_PK", "RECV_PK",
  16.          "XMIT_KB", "RECV_KB", "COMMAND")

  17.   foreach ([pid, dev, exec, uid] in ifrecv) {
  18.      ifmerged[pid, dev, exec, uid] += @count(ifrecv[pid,dev,exec,uid]);
  19.   }
  20.   foreach ([pid, dev, exec, uid] in ifxmit) {
  21.      ifmerged[pid, dev, exec, uid] += @count(ifxmit[pid,dev,exec,uid]);
  22.   }
  23.   foreach ([pid, dev, exec, uid] in ifmerged-) {
  24.     n_xmit = @count(ifxmit[pid, dev, exec, uid])
  25.     n_recv = @count(ifrecv[pid, dev, exec, uid])
  26.     printf("%5d %5d %-7s %7d %7d %7d %7d %-15s\n",
  27.            pid, uid, dev, n_xmit, n_recv,
  28.            n_xmit ? @sum(ifxmit[pid, dev, exec, uid])/1024 : 0,
  29.            n_recv ? @sum(ifrecv[pid, dev, exec, uid])/1024 : 0,
  30.            exec)
  31.   }

  32.   print("\n")

  33.   delete ifxmit
  34.   delete ifrecv
  35.   delete ifmerged
  36. }

  37. probe timer.ms(5000), end, error
  38. {
  39.   print_activity()
  40. }
Note that function print_activity() uses the following expressions:

点击(此处)折叠或打开

  1. n_xmit ? @sum(ifxmit[pid, dev, exec, uid])/1024 : 0
  2. n_recv ? @sum(ifrecv[pid, dev, exec, uid])/1024 : 0
These expressions are if/else conditionals. The first statement is simply a more concise way of writing the following psuedo code:

点击(此处)折叠或打开

  1. if n_recv != 0 then
  2.   @sum(ifrecv[pid, dev, exec, uid])/1024
  3. else
  4.   0
tracks which processes are generating network traffic on the system, and provides the following information about each process:
  • PID — the ID of the listed process.
  • UID — user ID. A user ID of 0 refers to the root user.
  • DEV — which ethernet device the process used to send / receive data (e.g. eth0, eth1)
  • XMIT_PK — number of packets transmitted by the process
  • RECV_PK — number of packets received by the process
  • XMIT_KB — amount of data sent by the process, in kilobytes
  • RECV_KB — amount of data received by the service, in kilobytes
provides network profile sampling every 5 seconds. You can change this setting by editing probe timer.ms(5000) accordingly. contains an excerpt of the output from over a 20-second period:

点击(此处)折叠或打开

  1. [...]
  2.   PID UID DEV XMIT_PK RECV_PK XMIT_KB RECV_KB COMMAND
  3.     0 0 eth0 0 5 0 0 swapper
  4. 11178 0 eth0 2 0 0 0 synergyc

  5.   PID UID DEV XMIT_PK RECV_PK XMIT_KB RECV_KB COMMAND
  6.  2886 4 eth0 79 0 5 0 cups-polld
  7. 11362 0 eth0 0 61 0 5 firefox
  8.     0 0 eth0 3 32 0 3 swapper
  9.  2886 4 lo 4 4 0 0 cups-polld
  10. 11178 0 eth0 3 0 0 0 synergyc

  11.   PID UID DEV XMIT_PK RECV_PK XMIT_KB RECV_KB COMMAND
  12.     0 0 eth0 0 6 0 0 swapper
  13.  2886 4 lo 2 2 0 0 cups-polld
  14. 11178 0 eth0 3 0 0 0 synergyc
  15.  3611 0 eth0 0 1 0 0 Xorg

  16.   PID UID DEV XMIT_PK RECV_PK XMIT_KB RECV_KB COMMAND
  17.     0 0 eth0 3 42 0 2 swapper
  18. 11178 0 eth0 43 1 3 0 synergyc
  19. 11362 0 eth0 0 7 0 0 firefox
  20.  3897 0 eth0 0 1 0 0 multiload-apple
  21. [...]
大部分英文和语法都不难
有几个点没有提:
1、probe netdev.transmit
这个探测点是tapset里定义的,具体看这里
https://sourceware.org/systemtap/tapsets/API-netdev-transmit.html
netdev.receive也是tapset里定义的




阅读(1090) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~