Menu

fix to get CPU frequency on Linux

tom zhou
2013-03-06
2013-03-08
  • tom zhou

    tom zhou - 2013-03-06

    Index: src/common.cpp

    --- src/common.cpp (revision 458)
    +++ src/common.cpp (working copy)
    @@ -43,6 +43,7 @@
    #include <cstring>
    #include <cerrno>
    #include <unistd.h>
    + #include <stdio.h>
    #ifdef OSX
    #include <mach mach_time.h="">
    #endif
    @@ -155,6 +156,35 @@
    mach_timebase_info_data_t info;
    mach_timebase_info(&info);
    frequency = info.denom * 1000ULL / info.numer;
    + #elif defined(LINUX)
    + // extract cpu frequency from /proc/cpuinfo
    + float mhz = 0;
    + char str[256], p = NULL;
    + int find = 0;
    + FILE * fd = fopen("/proc/cpuinfo", "r");
    +
    + if (fd == NULL) {
    + perror("fopen /proc/cpuinfo");
    + } else {
    + while (fgets(str, 256, fd)) {
    + if (strncmp("cpu MHz", str, sizeof("cpu MHz")-1) == 0) {
    + find = 1;
    + break;
    + }
    + }
    + fclose(fd);
    +
    + if (find) {
    + p = str;
    + while (
    p++ != ':');
    +
    + sscanf(p, "%f", &mhz);
    + frequency = (uint64_t)mhz;
    + ///printf("linux cpu MHz: %f, %lld\n", mhz, frequency);
    + } else {
    + printf("Warning!!! /proc/cpuinfo cpu MHz unknown\n");
    + }
    + }
    #endif

    // Fall back to microsecond if the resolution is not high enough.
    
     
  • tom zhou

    tom zhou - 2013-03-08

    one more fix for Linux :

    UDT4/src/common.cpp
    index 3285a7b..a1e706e 100755
    --- a/deps/uv/src/UDT4/src/common.cpp
    +++ b/deps/uv/src/UDT4/src/common.cpp
    @@ -128,7 +128,7 @@ void CTimer::rdtsc(uint64_t &x)
    x = mach_absolute_time();
    #else
    // use system call to read time clock for other archs
    - x = getTime();
    + x = getTime() * s_ullCPUFrequency;
    #endif
    }

     

Log in to post a comment.