为并发程序生成时间戳
为了分析并发程序中的某些事件,我需要生成时间戳。到目前为止,我使用了clock_gettime,但遇到了一些问题,其中一些是由于不同时钟的问题造成的。
使用CLOCK_REALTIME
生成的时间戳似乎与事件的实际顺序不匹配,这意味着映射到事件的结果时间戳没有任何意义。
使用 CLOCK_THREAD_CPUTIME_ID
和 CLOCK_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您正在尝试在多线程程序中获取事件的顺序。 Clock_gettime 对于 IMO 来说有点太侵入性了。如果您使用的是 Intel 机器,为什么不直接插入 rdtsc 指令呢?它对周期更加准确,而且开销也小得多。您可以按如下方式调用 rdtsc:
顺便说一句,它在 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:
Btw, it will still be bogus on SMP systems as the clocks on the two chip modules can very well be out of synch.