title: The cpu_time obtained by proc_pid_rusage does not meet expectations on the macOS M1 chip
body: <p>At present, I need to calculate the cpu usage of a certain process on the macOS platform (the target process is not directly related to the current process). I use the <a href="https://opensource.apple.com/source/xnu/xnu-2422.1.72/libsyscall/wrappers/libproc/libproc.h.auto.html" rel="nofollow noreferrer">proc_pid_rusage</a> API. The calculation method is to call it every once in a while, and then calculate this section The difference between ri_user_time and ri_system_time of the time. So as to calculate the percentage of cpu usage.</p>
  <p>I used it on a macOS system with non-M1 chip, and the results were in line with expectations (basically the same as what I saw on the activity monitor), but recently I found that the value obtained on the macOS system with the M1 chip is small. For example, <strong>one of my processes that consumes 30+% of the cpu(from activity monitor) is less than 1%</strong>.</p>
  <p>I provide a demo code, you can directly create a new project to run:</p>
  <pre><code>//
  //  main.cpp
  //  SimpleMonitor
  //
  //  Created by m1 on 2021/2/23.
  //
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;libproc.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;iostream&gt;
  #include &lt;thread&gt;         // std::this_thread::sleep_for
  #include &lt;chrono&gt;         // std::chrono::seconds
  
  int main(int argc, const char * argv[]) {
      // insert code here...
      std::cout &lt;&lt; &quot;run simple monitor!\n&quot;;
      
      // TODO: change process id:
      int64_t pid = 12483;
  
      struct rusage_info_v4 ru;
      struct rusage_info_v4 ru2;
  
      int64_t success = (int64_t)proc_pid_rusage((pid_t)pid, RUSAGE_INFO_V4, (rusage_info_t *)&amp;ru);
      
      if (success != 0) {
          std::cout &lt;&lt; &quot;get cpu time fail \n&quot;;
          return 0;
      }
      
      std::cout&lt;&lt;&quot;getProcessPerformance, pid=&quot; + std::to_string(pid) + &quot; ru.ri_user_time=&quot; + std::to_string(ru.ri_user_time) + &quot; ru.ri_system_time=&quot; + std::to_string(ru.ri_system_time)&lt;&lt;std::endl;
  
      std::this_thread::sleep_for (std::chrono::seconds(10));
      
      int64_t success2 = (int64_t)proc_pid_rusage((pid_t)pid, RUSAGE_INFO_V4, (rusage_info_t *)&amp;ru2);
      
      if (success2 != 0) {
          std::cout &lt;&lt; &quot;get cpu time fail \n&quot;;
          return 0;
      }
  
      std::cout&lt;&lt;&quot;getProcessPerformance, pid=&quot; + std::to_string(pid) + &quot; ru2.ri_user_time=&quot; + std::to_string(ru2.ri_user_time) + &quot; ru2.ri_system_time=&quot; + std::to_string(ru2.ri_system_time)&lt;&lt;std::endl;
      
      int64_t cpu_time = ru2.ri_user_time - ru.ri_user_time + ru2.ri_system_time - ru.ri_system_time;
  
      // percentage:
      double cpu_usage = (double)cpu_time / 10 / 1000000000 * 100 ;
      
      std::cout&lt;&lt;pid&lt;&lt;&quot; cpu usage: &quot;&lt;&lt;cpu_usage&lt;&lt;std::endl;
  
  }
  
  </code></pre>
  <p>Here I want to know whether there is a problem with my calculation method, if there is no problem, how can I handle the inaccurate results on the M1 chip macOS system?</p>