Current and correct CPU clock and asm("cpuid")

rohan puri rohan.puri15 at gmail.com
Mon Oct 3 00:17:12 EDT 2011


On Mon, Oct 3, 2011 at 6:57 AM, Peter Senna Tschudin
<peter.senna at gmail.com>wrote:

> Dear list members,
>
> I'm following:
>
> http://people.virginia.edu/~chg5w/page3/assets/MeasuringUnix.pdf
>
> And I'm trying to measure executing time of simple operations with RDTSC.
>
> See the code below:
>
> #include <stdio.h>
> #define CPU_THOUSAND_HZ 800000
> typedef unsigned long long ticks;
> static __inline__ ticks getticks(void) {
>        unsigned a, d;
>        asm("cpuid");
>        asm volatile("rdtsc" : "=a" (a), "=d" (d));
>        return (((ticks)a) | (((ticks)d) << 32));
> }
>
> void main() {
>        ticks tickBegin, tickEnd;
>        tickBegin = getticks();
>
>        // code to time
>
>        tickEnd = getticks();
>        double time = (tickEnd-tickBegin)/CPU_THOUSAND_HZ;
>
>        printf ("%Le\n", time);
> }
>
> How can the C code detects the correct value for CPU_THOUSAND_HZ? The
> problems I see are:
>  - It is needed to collect the information for the CPU that will run
> the process. On Core i7 processors, different cores can run at
> different clock speed at same time.
>  - If the clock changes during the execution of process, what should
> it do? When is the best time for collecting the clock speed?
>
> The authors of the paper are not sure about the effects of
> "asm("cpuid");" Does it ensure that the entire process will run on the
> same CPU, and will serialize it avoiding out of order execution by the
> CPU?
>
> Thank you very much! :-)
>
> Peter
>
>
> --
> Peter Senna Tschudin
> peter.senna at gmail.com
> gpg id: 48274C36
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
Hi Peter,

Excellent reference http://en.wikipedia.org/wiki/Time_Stamp_Counter

Also, your interpretation is right. There are lots of things which need to
be considered instead of setting CPU_THOUSAND_HZ to any particular value,
some of them are whether its multi-core or single core CPU. On multi-core
its quite difficult to get the correct answers, The problems are mentioned
in the above wiki link.

Regards,
Rohan Puri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20111003/ceece1ef/attachment.html 


More information about the Kernelnewbies mailing list