Skip to content

Instantly share code, notes, and snippets.

@savanovich
Last active October 16, 2025 12:39
Show Gist options
  • Select an option

  • Save savanovich/f07eda9dba9300eb9ccf to your computer and use it in GitHub Desktop.

Select an option

Save savanovich/f07eda9dba9300eb9ccf to your computer and use it in GitHub Desktop.

Revisions

  1. savanovich revised this gist Mar 12, 2016. 1 changed file with 41 additions and 0 deletions.
    41 changes: 41 additions & 0 deletions rdtscp.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // http://developers.redhat.com/blog/2016/03/11/practical-micro-benchmarking-with-ltrace-and-sched/

    /* One drawback of the RDTSC instruction is that the CPU is allowed to reorder
    it relative to other instructions, which causes noise in our results. Fortunately,
    Intel has provided an RDTSCP instruction that’s more deterministic. We’ll pair
    that with a CPUID instruction which acts as a memory barrier, resulting in this: */

    static __inline__ int64_t rdtsc_s(void)
    {
    unsigned a, d;
    asm volatile("cpuid" ::: "%rax", "%rbx", "%rcx", "%rdx");
    asm volatile("rdtsc" : "=a" (a), "=d" (d));
    return ((unsigned long)a) | (((unsigned long)d) << 32);
    }

    static __inline__ int64_t rdtsc_e(void)
    {
    unsigned a, d;
    asm volatile("rdtscp" : "=a" (a), "=d" (d));
    asm volatile("cpuid" ::: "%rax", "%rbx", "%rcx", "%rdx");
    return ((unsigned long)a) | (((unsigned long)d) << 32);
    }

    . . .

    clocks_before = rdtsc_s ();
    p = malloc (i); /* Test goes here */
    clocks_after = rdtsc_e ();
    clocks_per_malloc = clocks_after - clocks_before;

    // let the OS use CPU #0
    // boot options:
    // linux . . . isolcpus=1,2,3,4,5,6,7

    // check:
    // taskset -p $$

    // Interrupt affinity:
    // cd /proc/irq
    // for i in */smp_affinity; do echo 1 > $i; done

  2. savanovich revised this gist Jan 27, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion rdtsc.c
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    /* https://ru.wikipedia.org/wiki/Rdtsc */
    /*
    https://en.wikipedia.org/wiki/Time_Stamp_Counter
    https://ru.wikipedia.org/wiki/Rdtsc
    */

    #include <stdio.h>

  3. savanovich created this gist Jan 27, 2016.
    16 changes: 16 additions & 0 deletions rdtsc.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    /* https://ru.wikipedia.org/wiki/Rdtsc */

    #include <stdio.h>

    typedef unsigned long long uint64;

    int main() {
    uint64 val;
    unsigned int h, l;
    for (int i=0; i<=10; i++) {
    __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h));
    val = ((uint64)l) | (((uint64)h) << 32);

    printf("%llu \n", val);
    }
    }