Menu

#11 fix to get CPU frequency on Linux

open
nobody
None
5
2013-03-06
2013-03-06
tom zhou
No

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></unistd.h></cerrno></cstring>

  • include <stdio.h></stdio.h>

    ifdef OSX

    #include <mach mach_time.h=""></mach>

    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.

Discussion


Log in to post a comment.

MongoDB Logo MongoDB