title: Linux perf events: cpu-clock and task-clock - what is the difference
body: <p>Linux <code>perf</code> tools (some time ago named <code>perf_events</code>) has several builtin universal software events. Two most basic of them are: <code>task-clock</code> and <code>cpu_clock</code> (internally called <code>PERF_COUNT_SW_CPU_CLOCK</code> and <code>PERF_COUNT_SW_TASK_CLOCK</code>). But what is wrong with them is lack of description.</p>
  
  <p><a href="https://stackoverflow.com/users/637866/ysdx">ysdx</a> user <a href="https://stackoverflow.com/a/23850694/196561">reports</a> that <a href="http://man7.org/linux/man-pages/man2/perf_event_open.2.html" rel="noreferrer"><code>man perf_event_open</code></a> has short description:</p>
  
  <pre><code>    PERF_COUNT_SW_CPU_CLOCK
            This reports the CPU clock, a high-resolution per-
            CPU timer.
  
      PERF_COUNT_SW_TASK_CLOCK
            This reports a clock count specific to the task
            that is running.
  </code></pre>
  
  <p>But the description is hard to understand.</p>
  
  <p>Can somebody give authoritative answer about how and when the <code>task-clock</code> and <code>cpu-clock</code> events are accounted? How does they relate to the linux kernel scheduler?</p>
  
  <p>When <code>task-clock</code> and <code>cpu-clock</code> will give different values? Which one should I use?</p>