4

I am profiling some code on three different computers with three different frequencies. I need the frequencies to measure GFLOPs/s. I have some code which does this but it does not account for Turboboost. For example on my 2600k CPU it reports 3.4 GHz but I can see when I run CPUz that my CPU is running at 4.3 GHz (overclocked) for my code which uses all cores.

#include "stdint.h"
#include "stdio.h"
#include "omp.h"
int main() {
    int64_t cycles = rdtsc(); double dtime = omp_get_wtime();
    //run some code which uses all cores for a while (few ms)   
    dtime = omp_get_wtime() - dtime;
    cycles = rdtsc() - cycles;
    double freq = (double)cycles/dtime*1E-9;
    printf("freq %.2f GHz\n", freq);
}
__int64 rdtsc() {
#ifdef _WIN32
    return __rdtsc();
#else
  uint64_t t;
  asm volatile ("rdtsc" : "=A"(t));
  return t;
#endif
}  

I know this question has been asked various times with various answers but it's still not clear to me if this can be done. I don't care about hackers trying to change timers. This code is only for myself. Is it possible to get the actual frequency in code? How is this done on Linux? Every example I have found on linux gives the base frequency (or maybe max) but not the operating frequency under load like CPUz does.

Edit: I found a program, Powertop, for Linux which appears to show the actual operating frequency. Since the source code is available maybe it's possible to figure out how to get the actual frequency in my own code.

  • This is easier said than done: http://stackoverflow.com/questions/8351944/finding-out-the-cpu-clock-frequency-per-core-per-processor – Mysticial Apr 19 '13 at 19:34
  • Thanks, I read that thread already (among others). I guess the fact that I can't find an easy answer explains why there is no easy solution. It's annoying to have to change the values in my code every time I switch computers. –  Apr 19 '13 at 19:50
  • @Mysticial, do you know how Powertop gets the actual frequency? –  Apr 20 '13 at 17:21
  • I have never even heard of Powertop. lol – Mysticial Apr 20 '13 at 19:32
  • Neither had I until today :-) Anyway, it's the only program I have found on Linux that reports the actual turbo boost speed like CPUz. The code is open source http://en.wikipedia.org/wiki/PowerTOP –  Apr 20 '13 at 21:30
  • Figure out how to ask the kernel seems to like it would be most reliable... – Spudd86 Aug 04 '13 at 02:41

1 Answers1

0

I finally solved this problem. It is possible to measure the actuall operating frequency in code without needing device drivers or reading special counters.

Basically you time a loop for an operation with a carried loop dependency which always takes the same latency. For example

for(int i=0; i<spinCount; i++) {
    x = _mm_add_ps(x,_mm_set1_ps(1.0f));
}

You run this loop in threads bound to each physical core (not logical) core. The requires that no other threads in the system then these ones take any significant CPU time so this method won't always give the correct answer but in my case it works quite well. I get results that deviate less than 0.5% from the correct turbo frequency for one thread and for many threads on Nahalem, Ivy Bridge, and Haswell on single socket system and multi-socket system. I described the details of this at how-can-i-programmatically-find-the-cpu-frequency-with-c so I won't repeat all the details here.

Z boson
  • 32,619
  • 11
  • 123
  • 226