为并发程序生成时间戳

发布于 2024-11-06 09:51:50 字数 430 浏览 3 评论 0原文

为了分析并发程序中的某些事件,我需要生成时间戳。到目前为止,我使用了clock_gettime,但遇到了一些问题,其中一些是由于不同时钟的问题造成的。

使用CLOCK_REALTIME生成的时间戳似乎与事件的实际顺序不匹配,这意味着映射到事件的结果时间戳没有任何意义。

使用 CLOCK_THREAD_CPUTIME_IDCLOCK_PROCESS_CPUTIME_ID 时,时间戳按有意义的顺序排列,但我预计 CLOCK_REALTIME 会出现这种行为,而不是依赖于线程/进程的版本时钟。此外,手册页还指出,SMP 系统上可能存在虚假结果,但我看不到。

我的问题是是否有 clock_gettime 的替代方案,如果没有,我该如何解决我对可用系统时钟的误解?

For profiling certain events in a concurrent program I need to generate timestamps. So far I used clock_gettime, but I am running 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-11-13 09:51:50

如果我理解正确,您正在尝试在多线程程序中获取事件的顺序。 Clock_gettime 对于 IMO 来说有点太侵入性了。如果您使用的是 Intel 机器,为什么不直接插入 rdtsc 指令呢?它对周期更加准确,而且开销也小得多。您可以按如下方式调用 rdtsc:

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 );
}

顺便说一句,它在 SMP 系统上仍然是假的,因为两个芯片模块上的时钟很可能不同步。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文