如何计算包括子进程在内的进程ID的已执行指令数
How to count number of executed instructions of a process id including child processes
我将 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为parent containerd的PID
集装箱。
问题:
- 有什么方法可以提供父 PID 并计算所有进程的已执行指令数?
- 我的方法有意义吗?我的意思是我以逗号分隔格式提供所有 PID 的方式。
您可以获得其中一个进程(父进程)的 PID,并使用 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 ","
会转换它们。
我将 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为parent containerd的PID 集装箱。
问题:
- 有什么方法可以提供父 PID 并计算所有进程的已执行指令数?
- 我的方法有意义吗?我的意思是我以逗号分隔格式提供所有 PID 的方式。
您可以获得其中一个进程(父进程)的 PID,并使用 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 ","
会转换它们。