title: Perf Stat vs Perf Record
body: <p>I am confused about the difference between <code>perf record</code> and <code>perf stat</code> when it comes to counting events like page-faults, cache-misses and anything else from <code>perf list</code>. I have 2 questions below the answer to "Question 1" might also help answer "Question 2" but I wrote them out explicitly in the case that it doesn't.</p>
  
  <p>Question 1:
  It is my understanding that <code>perf stat</code> gets a "summary" of counts but when used with the -I option gets the counts at the specified millisecond interval. With this option does it sum up the counts over the interval or get the average over the interval, or something else entirely? I assume it is summed up. The <a href="https://perf.wiki.kernel.org/index.php/Tutorial#Counting_with_perf_stat" rel="noreferrer" title="perf stat wiki">perf wiki</a> states it is aggregated but I guess that could mean either.</p>
  
  <p>Question 2:
  Why doesn't <code>perf stat -e &lt;event1&gt; -I 1000 sleep 5</code> give about the same counts as if I summed up the counts over each second for the following command <code>perf record -e &lt;event1&gt; -F 1000 sleep 5</code>? </p>
  
  <p>For example if I use "page-faults" as the event for event1 I get the following outputs I have listed below under each command. (I am assuming the period field is the counts for the event in <code>perf record</code>'s perf.data file)</p>
  
  <p>PERF STAT</p>
  
  <pre><code>    perf stat -e page-faults -I 1000 sleep 5
      #           time             counts unit events
           1.000252928                 54      page-faults                                                 
           2.000498389      &lt;not counted&gt;      page-faults                                                 
           3.000569957      &lt;not counted&gt;      page-faults                                                 
           4.000659987      &lt;not counted&gt;      page-faults                                                 
           5.000837864                  2      page-faults
  </code></pre>
  
  <p>PERF RECORD</p>
  
  <pre><code>    perf record -e page-faults -F 1000 sleep 5
      [ perf record: Woken up 1 times to write data ]
      [ perf record: Captured and wrote 0.016 MB perf.data (6 samples) ]
      perf script -F period
               1
               1
               1
               5
              38
             164
  </code></pre>
  
  <p>I expected that if I summed up the counts from <code>perf stat</code> I would get the same as the sum from <code>perf record</code>. If I use the -c option with <code>perf record</code> and give an argument of 1 I do get a close match. Is this just a coincidence because of the relatively low number of page faults?</p>
  
  <p>References I have used so far:</p>
  
  <ul>
  <li><a href="http://www.brendangregg.com/perf.html#Events" rel="noreferrer">brendangregg's perf blog</a></li>
  <li>The perf record and stat links on <a href="https://perf.wiki.kernel.org/index.php/Main_Page" rel="noreferrer" title="Perf Wiki">this page</a> mentioned above as "perf wiki"</li>
  <li>I dug around <a href="https://elixir.bootlin.com/linux/v4.13/source/tools/perf" rel="noreferrer" title="Perf Source">here</a> to see how and when perf record actually records vs when it writes to perf.data.</li>
  </ul>
  
  <p>Thanks in advance for any and all insight you can provide.</p>