【问题标题】:How to count number of executed instructions of a process id including child processes如何计算包括子进程在内的进程ID的执行指令数
【发布时间】:2020-11-01 04:53:00
【问题描述】:

我将 nodejs 应用程序(作为服务器)部署为 Docker 容器,我想在调用其中的函数时计算执行指令的数量。

这是我如何找到容器的 PID:

$ pstree -p | grep node | grep npm
           |                 |-containerd-shim(114397)-+-npm(114414)-+-sh(114540)---node(114541)-+-{node}(114542)

然后,我需要知道 Docker ID:

$ docker ps | grep workload
root@node3:/home/m# docker ps | grep workload | grep npm
c7457f74536b        michelgokan/synthetic-workload-generator                   "npm start"              55 minutes ago      Up 55 minutes                           k8s_whatever_workload-5697bb48f9-gg8j5_default_896e5938-55f2-4875-bf6c-2bff2acbe0c6_0

现在,我知道父 PID 是 114397。所以我运行以下 perf 命令:

$ perf stat -p 114397 -e instructions,cycles,task-clock docker exec -it c7457f74536b curl 127.0.0.1:30005/workload/cpu
1000 CHKSM AND DIFFIEHELLMAN 60 OK!
 Performance counter stats for process id '114397':

         170057460      instructions              #    1.02  insn per cycle         
         166389574      cycles                    #    1.575 GHz                    
            105.67 msec task-clock                #    0.570 CPUs utilized          

       0.185362408 seconds time elapsed

它似乎不包括子进程执行的指令。所以我尝试了以下方法:

$ perf stat -p 1,722,114397,114414,114540,114541,114542 -e instructions,cycles,task-clock docker exec -it c7457f74536b curl 127.0.0.1:30005/workload/cpu
1000 CHKSM AND DIFFIEHELLMAN 60 OK!
 Performance counter stats for process id '1,722,114397,114414,114540,114541,114542':

         249803992      instructions              #    1.05  insn per cycle         
         236979702      cycles                    #    1.575 GHz                    
            150.47 msec task-clock                #    0.832 CPUs utilized          

       0.180848729 seconds time elapsed

其中1是systemd,722是父containerd的PID 容器。

问题:

  1. 有什么方法可以提供父 PID 并计算所有进程执行的指令数?
  2. 我的方法有意义吗?我的意思是我以逗号分隔的格式提供所有 PID。

【问题讨论】:

  • 您是否希望它像strace -f 那样跟踪在 perf 开始后分叉的孩子?仅使用为 -p 构建 PID 列表的包装器是无法做到的。
  • @PeterCordes 确实如此 :-(。您对此有什么解决方案吗?我在这里问了一个单独的问题:stackoverflow.com/questions/64076497/…

标签: linux performance docker cpu-architecture perf


【解决方案1】:

您可以获取其中一个进程(父进程)的 PI​​D,并使用pgrep 推断其他进程。

pgrep 有一个简洁的功能--ns,它可以让您在与给定 PID 相同的 PID 命名空间中运行所有进程。

如果您可以获取所有子进程并将它们转换为逗号分隔值并将它们提供给perf

$ perf stat -p $(pgrep --ns <pid> | paste -s -d ",") -e instructions,cycles,task-clock docker exec -it c7457f74536b curl 127.0.0.1:30005/workload/cpu

pgrep --ns 将为您获取 pid,paste -s -d "," 将转换它们。

【讨论】:

猜你喜欢
  • 2021-01-12
  • 1970-01-01
  • 2014-11-17
  • 2012-02-25
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多