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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With