如何计算包含子进程的进程ID的已执行指令数 [英] How to count number of executed instructions of a process id including child processes

查看:121
本文介绍了如何计算包含子进程的进程ID的已执行指令数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


这就是我的方法找到容器的PID:

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

然后,我需要知道Docker ID:

  $ docker ps | grep工作负载
root @ node3:/ home / m#docker ps | grep工作量| grep npm
c7457f74536b michelgokan / synthetic-workload-generator npm start 55分钟前上55分钟k8s_whatever_workload-5697bb48f9-gg8j5_default_896e5938-55f2-4875-bf6c-2bff2acbe0c6_0

现在,我知道了父母PID为114397。因此,我运行以下 perf 命令:

  $ perf stat -p 114397 -e指令,周期,任务时钟docker exec -it c7457f74536b curl 127.0.0.1:30005/workload/cpu 
1000 CHKSM和DIFFIEHELLMAN 60好!
进程ID'114397'的性能计数器统计信息:

170057460指令#1.02 insn每个周期
166389574周期#1.575 GHz
105.67 msec任务时钟#0.570 CPU已使用

0.185362408秒经过的时间

似乎不包括子进程执行的指令。因此,我尝试了以下操作:

  $ perf stat -p 1,722,114397,114414,114540,114541,114542 -e指令,循环,任务-时钟docker exec -it c7457f74536b curl 127.0.0.1:30005/workload/cpu 
1000 CHKSM和DIFFIEHELLMAN 60好!
进程ID'1,722,114397,114414,114540,114541,114542'的性能计数器统计信息:

249803992指令#1.05每个周期$ ins
236979702个周期#1.575 GHz
150.47毫秒的任务时钟#0.832 CPU使用了

0.180848729秒经过的时间

1是系统化的,722是
容器的父容器化PID。


问题:


  1. 是有什么方法可以提供父PID并计算所有进程的已执行指令数?

  2. 我的方法有意义吗?我的意思是我用逗号分隔格式提供所有PID的方式。


解决方案

您可以获取其中一个进程(父进程)的PID并使用 pgrep 推导其他进程。


pgrep 具有整洁的功能-ns ,它将使您所有的进程都与给定的PID在同一个PID名称空间中运行。


可以获取所有子进程并将其转换为逗号分隔的值,并将其提供给 perf


< pre class = lang-sh prettyprint-override> $ perf stat -p $(pgrep --ns< pid> |粘贴-s -d,)-e指令,循环,task-clock docker exec -it c7457f74536b curl 127.0.0.1:30005/workload/cpu

pgrep --ns 将为您提供pid,而 paste -s -d, 会将其转换。


I have nodejs application (as a server) deployed as a Docker container and I want to count the number of executed instructions when I call a function in it.

Here is how I find the container's PID:

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

Then, I need to know the 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

Now, I know the parent PID is 114397. So I run the following perf command:

$ 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

It seems it's not including instructions executed by the child processes. So I tried the following:

$ 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

In which 1 is the systemd and 722 is the parent containerd PID of containers.

Questions:

  1. Is there any way that I can provide the parent PID and it counts number of executed instructions of all processes?
  2. Does my approach make sense? I mean the way I provided all the PIDs in a comma-separated format.

解决方案

You can get the PID of one of you processes (the parent) and deduce the others using pgrep.

pgrep has a neat feature --ns which will get you all the processes running in the same PID namespace as a given PID.

Having that you can get all the child process and convert them to comma separated values and feed them to 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 will get you the pid and paste -s -d "," will convert them.

这篇关于如何计算包含子进程的进程ID的已执行指令数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆