Compute CPU clock cycles in Linux

来源:互联网 发布:剑网3秀太捏脸数据 编辑:程序博客网 时间:2024/05/21 11:00

Refer to

http://www.unix.com/high-level-programming/81639-rdtsc-use-c.html

 

"rdtsc is not good for time measurement.

The problem is, that the CPU might throttle (with it does most of thetime, when all processes are blocked), so a cpu clock might mean moretime in that case. This is of course CPU and OS dependant."

 

"/proc/cpuinfo indicates that your system uses "constant_tsc". Thismeans that the number of ticks returned by rtdsc is constant regardlessof the current cpu speed. So you can't use rdtsc to measure how fastyour cpu is running..."

 

 

My Code:

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <asm/msr.h>
#include <sys/time.h>

//#include <linux/timex.h>

// cycles_t get_cycles(void);

#ifndef CPU_FREQ
#define CPU_FREQ 800
#endif

static __inline__ unsigned long long get_cycles(void)
{
    unsigned long long low, high;
    asm("cpuid");
    asm volatile("rdtsc" : "=a" (low), "=d" (high));
    return (unsigned long long) ((high << 32) | low);
}

int main(int argc, char **argv)
{
    unsigned long int begin, end, duration;
    int sleeptime = 1;
    int i = 0;

    begin = 0;
    end = 0;
    duration = 0;

    // rdtsc(low, high);
    // low = get_cycles();
    if (argc > 1) {
        sleeptime = atoi(argv[1]);
        printf("%s/n", argv[1]);
    }

    for (i = 0; i < 10; i++) {
        begin = get_cycles();
        sleep(sleeptime);
        end = get_cycles();
        duration = end - begin;

        printf("%Ld, %d/n", (long long int)duration, (int)CPU_FREQ);
        // printf("%f/n", (double)(duration/CPU_FREQ));
    }
    return EXIT_SUCCESS;
}

 

 

Result:

 

[frank@apollo cpu_clock]$ ./clockcycles
1381329110, 1600
1444006680, 1600
1325221500, 1600
1127375570, 1600
1167663350, 1600
1208044260, 1600
1271470650, 1600
1516098220, 1600
1543729780, 1600
1432241600, 1600
[frank@apollo cpu_clock]$ ./clockcycles 2
2
2666254360, 1600
3116791020, 1600
2813633460, 1600
2801129480, 1600
2964726810, 1600
2664956190, 1600
2561190510, 1600
2576108640, 1600
2790339540, 1600
2542260270, 1600
[frank@apollo cpu_clock]$ ./clockcycles 5
5
6272247380, 1600
6487991310, 1600
6558096670, 1600
6374877600, 1600
6303548680, 1600
6744883540, 1600
6559147250, 1600
6334932360, 1600
6507320110, 1600
6549438150, 1600

 

 

[frank@apollo cpu_clock]$ cat /proc/cpuinfo
processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model        : 15
model name    : Intel(R) Core(TM)2 Duo CPU     T7250  @ 2.00GHz
stepping    : 13
cpu MHz        : 800.000
cache size    : 2048 KB
physical id    : 0
siblings    : 2
core id        : 0
cpu cores    : 2
apicid        : 0
initial apicid    : 0
fpu        : yes
fpu_exception    : yes
cpuid level    : 10
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall lm constant_tsc arch_perfmon pebs bts rep_good pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm ida tpr_shadow vnmi flexpriority
bogomips    : 3990.12
clflush size    : 64
cache_alignment    : 64
address sizes    : 36 bits physical, 48 bits virtual
power management:

processor    : 1
vendor_id    : GenuineIntel
cpu family    : 6
model        : 15
model name    : Intel(R) Core(TM)2 Duo CPU     T7250  @ 2.00GHz
stepping    : 13
cpu MHz        : 800.000
cache size    : 2048 KB
physical id    : 0
siblings    : 2
core id        : 1
cpu cores    : 2
apicid        : 1
initial apicid    : 1
fpu        : yes
fpu_exception    : yes
cpuid level    : 10
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall lm constant_tsc arch_perfmon pebs bts rep_good pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm lahf_lm ida tpr_shadow vnmi flexpriority
bogomips    : 3989.81
clflush size    : 64
cache_alignment    : 64
address sizes    : 36 bits physical, 48 bits virtual
power management:

 

 

 

CPUINFO:

http://www.linfo.org/proc_cpuinfo.html

 

http://www.linuxquestions.org/questions/programming-9/how-to-get-cpu-information-on-linux-machine-358037/page2.html

 

http://kobesearch.cpan.org/htdocs/Linux-Cpuinfo/Linux/Cpuinfo.html#cpu_mhz

cpu_mhz

I guess this is self explanatory - it might however be different to whatit says on the box. The Mhz is measured at boot time by the kernel andrepresents the true Mhz at that time.

 


 

原创粉丝点击