What am I doing wrong in measuring execution time using RDTSC and CPUID instructions?

69 views Asked by At

I'm a C++ student and I was assigned to measure the time of memory allocation of 1.000.000 integers, using CPUID and RDTSC instructions in inline assembly for G++ compiler. Here is the code I came up with:

#include <iostream>

using namespace std;

int main()
{
    unsigned cycles_high1=0, cycles_low1=0, cpuid_time=0;
    unsigned cycles_high2=0, cycles_low2=0;
    unsigned  temp_cycles1=0, temp_cycles2=0;
    unsigned total_cycles=0;
     
    asm volatile(
        "cpuid\n\t"
        "rdtsc\n\t"
        "mov %0, %%edx\n\t"
        "mov %1, %%eax\n\t"
        "cpuid\n\t"
        "rdtsc\n\t"

        "cpuid\n\t"
        "rdtsc\n\t"
        "mov %0, %%edx\n\t"
        "mov %1, %%eax\n\t"
        "cpuid\n\t"
        "rdtsc\n\t"

        "cpuid\n\t"
        "rdtsc\n\t"
        "mov %0, %%edx\n\t"
        "mov %1, %%eax\n\t"
        "cpuid\n\t"
        "rdtsc\n\t"

        "sub %%eax, %1\n\t"
        "mov %2, %%eax\n\t"
        : "=r" (cycles_high1), "=r" (cycles_low1), "=r" (cpuid_time)
        :
        : "%rax", "%rbx", "%rcx", "%rdx"
    );

    cycles_high1=0;
    cycles_low1=0;
    
    asm(
        "cpuid\n\t"
        "rdtsc\n\t"
        "mov %0, %%edx\n\t"
        "mov %1, %%eax\n\t"
        : "=r" (cycles_high1), "=r" (cycles_low1)
        :
        : "%rax", "%rbx", "%rcx", "%rdx"
    );

    int *p = (int*)malloc(sizeof(int) * 1000000);

    asm(
        "cpuid\n\t"
        "rdtsc\n\t"
        "mov %0, %%edx\n\t"
        "mov %1, %%eax\n\t"
        : "=r" (cycles_high2), "=r" (cycles_low2)
        :
        : "%rax", "%rbx", "%rcx", "%rdx"
    );

    temp_cycles1 = ((unsigned long long)cycles_high1 << 32) | cycles_low1;
    temp_cycles2 = ((unsigned long long)cycles_high2 << 32) | cycles_low2;
    total_cycles = temp_cycles2 - temp_cycles1 - cpuid_time; 

    cout << total_cycles;

    return 0;
}   

The structure of the asm code was provided to me in Windows Visual Studio compiler asm syntax, and I adapted it to G++ asm syntax. Firstly I am calculating the CPUID overhead time in 'cpuid_time' and at the end subtracting from the total measured time.

The output is always 0. What am I doing wrong?

0

There are 0 answers