title: How to count number of executed instructions of a process id including child processes
body: <p>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.</p>
  <p>Here is how I find the container's PID:</p>
  <pre><code>$ pstree -p | grep node | grep npm
             |                 |-containerd-shim(114397)-+-npm(114414)-+-sh(114540)---node(114541)-+-{node}(114542)
  </code></pre>
  <p>Then, I need to know the Docker ID:</p>
  <pre><code>$ docker ps | grep workload
  root@node3:/home/m# docker ps | grep workload | grep npm
  c7457f74536b        michelgokan/synthetic-workload-generator                   &quot;npm start&quot;              55 minutes ago      Up 55 minutes                           k8s_whatever_workload-5697bb48f9-gg8j5_default_896e5938-55f2-4875-bf6c-2bff2acbe0c6_0
  </code></pre>
  <p>Now, I know the parent PID is 114397. So I run the following <code>perf</code> command:</p>
  <pre><code>$ 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
  </code></pre>
  <p>It seems it's not including instructions executed by the child processes. So I tried the following:</p>
  <pre><code>$ 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
  </code></pre>
  <p>In which 1 is the systemd and 722 is the parent containerd PID of
  containers.</p>
  <p>Questions:</p>
  <ol>
  <li>Is there any way that I can provide the parent PID and it counts number of executed instructions of all processes?</li>
  <li>Does my approach make sense? I mean the way I provided all the PIDs in a comma-separated format.</li>
  </ol>