<p>There is an example of rdpmc usage: <a href="https://github.com/jdmccalpin/low-overhead-timers" rel="nofollow noreferrer">https://github.com/jdmccalpin/low-overhead-timers</a> by John <a href="https://stackoverflow.com/a/60267195">https://stackoverflow.com/a/60267195</a> (<a href="http://sites.utexas.edu/jdm4372/2018/07/23/comments-on-timing-short-code-sections-on-intel-processors/" rel="nofollow noreferrer">http://sites.utexas.edu/jdm4372/2018/07/23/comments-on-timing-short-code-sections-on-intel-processors/</a>).</p>
<p>Also there was mentioned ready to use tool to measure instructions: <a href="https://arxiv.org/pdf/1911.03282.pdf" rel="nofollow noreferrer">https://arxiv.org/pdf/1911.03282.pdf</a> <a href="https://github.com/andreas-abel/nanoBench" rel="nofollow noreferrer">https://github.com/andreas-abel/nanoBench</a></p>
<p>This answer <a href="https://stackoverflow.com/a/60267531">https://stackoverflow.com/a/60267531</a> has example of using perf_event_open to setup event counter and rdpmc to read counter.</p>
<p>rdpmc is not serializing and also not monotonic between two unserialized rdpmcs according to <a href="https://www.felixcloutier.com/x86/rdpmc" rel="nofollow noreferrer">https://www.felixcloutier.com/x86/rdpmc</a>:</p>
<blockquote>
<p>The RDPMC instruction is not a serializing instruction; that is, it does not imply that all the events caused by the preceding instructions have been completed or that events caused by subsequent instructions have not begun. If an exact event count is desired, software must insert a serializing instruction (such as the CPUID instruction) before and/or after the RDPMC instruction.</p>
<p>Performing back-to-back fast reads are not guaranteed to be monotonic. To  guarantee monotonicity on back-to-back reads, a serializing instruction must be placed between the two RDPMC instructions.</p>
</blockquote>
<p>jevents library can be used to generate PMC event selectors: <a href="https://github.com/andikleen/pmu-tools/tree/master/jevents" rel="nofollow noreferrer">https://github.com/andikleen/pmu-tools/tree/master/jevents</a>. It is used internally by recent versions of perf linux profiling tool. jevents also has simple api to use rdpmc command</p>
<pre><code>if (rdpmc_open(PERF_COUNT_HW_CPU_CYCLES, &amp;ctx) &lt; 0) ... error ...
start = rdpmc_read(&amp;ctx);
... your workload ...
end = rdpmc_read(&amp;ctx);
</code></pre>
<p>showevtinfo of libpfm4 may generate event id compatible to rdpmc's ecx format, but I'm not sure: <a href="https://stackoverflow.com/a/46370111">https://stackoverflow.com/a/46370111</a></p>
<p>With nanobench we can check source code for Skylake events:
<a href="https://github.com/andreas-abel/nanoBench/blob/master/configs/cfg_Skylake_common.txt" rel="nofollow noreferrer">https://github.com/andreas-abel/nanoBench/blob/master/configs/cfg_Skylake_common.txt</a></p>
<pre><code>D1.01 MEM_LOAD_RETIRED.L1_HIT
D1.08 MEM_LOAD_RETIRED.L1_MISS
D1.02 MEM_LOAD_RETIRED.L2_HIT
D1.10 MEM_LOAD_RETIRED.L2_MISS
D1.04 MEM_LOAD_RETIRED.L3_HIT
D1.20 MEM_LOAD_RETIRED.L3_MISS
</code></pre>
<p>parsed in <a href="https://github.com/andreas-abel/nanoBench/blob/master/common/nanoBench.c" rel="nofollow noreferrer">https://github.com/andreas-abel/nanoBench/blob/master/common/nanoBench.c</a>  <code>parse_counter_configs()</code> as <code>pfc_configs[n_pfc_configs].evt_num</code> dot <code>pfc_configs[n_pfc_configs].umask</code>; encoded in <code>configure_perf_ctrs_programmable</code> as</p>
<pre><code>        uint64_t perfevtselx = read_msr(MSR_IA32_PERFEVTSEL0+i);
        perfevtselx &amp;= ~(((uint64_t)1 &lt;&lt; 32) - 1);

        perfevtselx |= ((config.cmask &amp; 0xFF) &lt;&lt; 24);
        perfevtselx |= (config.inv &lt;&lt; 23);
        perfevtselx |= (1ULL &lt;&lt; 22);
        perfevtselx |= (config.any &lt;&lt; 21);
        perfevtselx |= (config.edge &lt;&lt; 18);
        perfevtselx |= (os &lt;&lt; 17);
        perfevtselx |= (usr &lt;&lt; 16);

        perfevtselx |= ((config.umask &amp; 0xFF) &lt;&lt; 8);
        perfevtselx |= (config.evt_num &amp; 0xFF);

        write_msr(MSR_IA32_PERFEVTSEL0+i, perfevtselx);
</code></pre>
<p>So, two lower bytes of register value written into  IA32_PERF_EVTSELx MSR are evt_num and umask. Not sure how it is translated into rdpmc ecx format.</p>
<p>John says that rdpmc command takes &quot;something in the range of 24-40 cycles&quot; and describes that &quot;Intel architecture makes it impossible to change the performance counter event select programming from user space at low latency/overhead.&quot;  <a href="https://community.intel.com/t5/Software-Tuning-Performance/Capturing-multiple-events-simultaneously-using-RDPMC-instruction/td-p/1097868" rel="nofollow noreferrer">https://community.intel.com/t5/Software-Tuning-Performance/Capturing-multiple-events-simultaneously-using-RDPMC-instruction/td-p/1097868</a></p>
<p>And documentation of rdpmc says the same <a href="https://www.felixcloutier.com/x86/rdpmc" rel="nofollow noreferrer">https://www.felixcloutier.com/x86/rdpmc</a>:</p>
<blockquote>
<p>The ECX register specifies the counter type (if the processor supports architectural performance monitoring) and counter index.
General-purpose or special-purpose performance counters are specified with ECX[30] = 0</p>
</blockquote>
<p>ECX does contain not the exact event to count, but the index of counter. There are 2, 4 or 8  &quot;programmable performance counters&quot;, and you must first use wrmsr (in kernel mode) to setup some counter, for example with MSR IA32_PERF_EVTSEL0 to setup counter with index 0, and then use rdpmc with ecx[30]=0 and ecx[29:0]=0; with MSR IA32_PERF_EVTSEL3 use rdpmc with ecx[30]=0 and ecx[29:0]=3.</p>
<p>I think that it will be easier to use PAPI API to setup counter and get readings from it before and after your test code. But API call adds overhead, so your test code should be designed to repeat the sequence to be tested for several times (thousands or more). By default rdpmc/rdmsr for perfcounters are disabled for user-space code by PCE flag in CR4 - <a href="https://www.felixcloutier.com/x86/rdpmc" rel="nofollow noreferrer">https://www.felixcloutier.com/x86/rdpmc</a> (<code>echo 2 &gt; /sys/bus/event_source/devices/cpu/rdpmc</code>); with only linux kernel access enabled. And wrmsr for setup of counter is disabled too.</p>
<p>There are several known methods of measuring cache hierarchy latency without perfcounters: <a href="https://www.7-cpu.com/utils.html" rel="nofollow noreferrer">https://www.7-cpu.com/utils.html</a> and lmbench/src/lat_mem_rd.c, but to get actual cache latency some manual post-processing is required.</p>
How to use rdpmc instruction for counting L1d cache miss?
<p>I am wondering is there any single event that can capture the L1D cache misses. I tried to capture L1d cache miss by measuring latency to access specific memory with rdtsc at the beginning. On my setting, if the L1d cache miss happens, it should hit L2 cache.  Therefore I measure latency of accessing memory with RDTSC and compare it with L1 cache latency and L2 cache latency. However, because of the noise, I cannot discern whether it hits L1 or L2. So I decided to use RDPMC.</p>
<p>I found that several APIs provide some functions to monitor perf events easily, but I would like to use RDPMC instruction directly on my test program. I found that MEM_INST_RETIRED.ALL_LOADS-MEM_LOAD_RETIRED.L1_HIT can be used to count the number of retired load instructions that miss in the L1D.(<a href="https://stackoverflow.com/questions/54638486/counting-l1-cache-misses-with-papi-read-counters-gives-unexpected-results">counting L1 cache misses with PAPI_read_counters gives unexpected results</a>). However, it seems that this posting talks about the papi Api.</p>
<p>How can I find what values should be assigned for ecx register before executing rdpmc instruction to capture specific events?? Also, I am wondering is there any single event that can tell me L1 miss happens for one memory load instruction in between two rdpmc instructions back to back like below.</p>
<pre class="lang-cpp prettyprint-override"><code>c = XXX; //I don't know what value should be assigned for what perf counter..
asm volatile(
    &quot;lfence&quot;
    &quot;rdpmc&quot; 
    &quot;lfence&quot;
    &quot;mov (0xdeadbeef), %%r10&quot;//read memory
    &quot;mov %%eax, %%r10        //read lower 32 bits of counter
    &quot;lfence&quot;                
    &quot;rdpmc&quot;                  //another rdpmc to capture difference
    &quot;sub %%r10, %%eax        //sub two counter to get difference
    :&quot;=a&quot;(a)
    :&quot;c&quot;(c)
    :&quot;r10&quot;, &quot;edx&quot;);

</code></pre>
<p><a href="https://i.stack.imgur.com/yM2FQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yM2FQ.png" alt="enter image description here" /></a></p>
<p>I am currently using 9900k coffee lake machine, so I searched perf counter number for coffee lake machine in the intel manual. It seems that just capturing two MEM_LOAD_RETIRED.L1_HIT before and after the load instruction is enough to capture the event, but I am not sure whether it is okay to do so.. Also I don't know well how to encode that perf event as ecx register.</p>
<p>Lastly, I am wondering does the rdpmc instruction back-to-back require any serialization instructions. In my case, because I only put the load instruction and measure the L1d cache miss happens or not, I enclose the first rdpmc instruction with lfence instruction and put one more lfence instruction before last rdpmc to make sure the load instruction finish before second rdpmc.</p>
<p>Added code</p>
<pre class="lang-cpp prettyprint-override"><code>asm volatile (                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
        &quot;lfence\n\t&quot;                                                                                                                                                                                                                                                                              
        &quot;rdpmc\n\t&quot;                                                                                                                                                                                                                                                                               
        &quot;lfence\n\t&quot;                                                                                                                                                                                                                                                                              
        &quot;mov %%eax, %%esi\n\t&quot;                                                                                                                                                                                                                                                                    
        //measure                                                                                                                                                                                                                                                                                 
        &quot;mov (%4), %%r10\n\t&quot;                                                                                                                                                                                                                                                                     
        &quot;lfence\n\t&quot;                                                                                                                                                                                                                                                                              
        &quot;rdpmc\n\t&quot;                                                                                                                                                                                                                                                                               
        &quot;lfence\n\t&quot;                                                                                                                                                                                                                                                                              
        &quot;sub %%esi, %%eax\n\t&quot;                                                                                                                                                                                                                                                                    
        &quot;mov %%eax, (%0)\n\t&quot;
        :
        :&quot;r&quot;(&amp;perf[1]), &quot;r&quot;(&amp;perf[2]), &quot;r&quot;(&amp;perf[3]),                                                                                                                                                                                                                                              
         &quot;r&quot;(myAddr),   &quot;c&quot;(0x0)                                                                                                                                                                                                         
        :&quot;eax&quot;,&quot;edx&quot;,&quot;esi&quot;,&quot;r10&quot;, &quot;memory&quot;);

</code></pre>
<p>Also I pinned my core number 3 with isolcpu and disable hyperthreading for testing. MSR register has been figured with below command</p>
<pre class="lang-sh prettyprint-override"><code>    sudo wrmsr -p 3 0x186 0x4108D1 #L1 MISS
</code></pre>