In order to calculate time we usually use a system call, but what if I wanted to implement it myself, is it possible? Using rdtsc
gives me the amount of cpu clocks from the time we turned it on. It is still not possible to calculate the time since we need the cpu frequency for this purpose. The basic problem is that it looks to me that not only the cpu frequency that I measure is different from what I see in /proc/cpuinfo
but it also can change over time (overclocking and underclocking). So how is the system call implemented?
The solution framework I'm looking for is to calculate the time by looking at some initial time t0, the amount of cpu clocks since t0 (using rdtsc
) and the cpu frequency. Something like
t1 = t0 + (rdtsc1 - rdtsc0) / frequency