Compute CPU clock cycles in Linux

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 the time, when all processes are blocked), so a cpu clock might mean more time in that case. This is of course CPU and OS dependant."

 

"/proc/cpuinfo indicates that your system uses "constant_tsc". This means that the number of ticks returned by rtdsc is constant regardless of the current cpu speed. So you can't use rdtsc to measure how fast your 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 what it says on the box. The Mhz is measured at boot time by the kernel and represents the true Mhz at that time.

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值