开发者

Generate timestamps for concurrent programs

For profiling certain events in a concurrent program I need to generate timestamps. So far I used clock_gettime, but I am runnin开发者_StackOverflow社区g into some issues, some of them are due to trouble with the different clocks.

With CLOCK_REALTIME the generated timestamps don't seem to match the actual order of events, which means the resulting timestamps mapped to the events does not make any sense.

With CLOCK_THREAD_CPUTIME_ID and CLOCK_PROCESS_CPUTIME_ID the timestamps are in a meaningful order, but I expected this behavior from CLOCK_REALTIME and not the thread/process-dependent versions of the clocks. Further the man page states, there might be bogus results on SMP systems, which I cannot see.

My question is are there alternatives to clock_gettime and if not, how could I approach my misunderstanding with the available system clocks?


If I understand correctly, you are trying to get an ordering of the events in a multi-threaded program. Clock_gettime is a bit too intrusive for that IMO. If you are on an Intel machine, why don't you just insert an rdtsc instruction. Its way more accurate to the cycle and the overhead is much smaller. You can call rdtsc as follows:

static inline unsigned long long rdtsc(void)
{
    unsigned hi, lo;
    __asm__ __volatile__ ( 
                      "rdtsc \n"
                      : "=a"(lo), "=d"(hi));
    return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

Btw, it will still be bogus on SMP systems as the clocks on the two chip modules can very well be out of synch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜