测量延迟的定时器 [英] Timers to measure latency

查看:132
本文介绍了测量延迟的定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在任何TCP协议上测量网络延迟(接收到的时间 - 发送的时间msg)时,您会建议使用什么定时器,为什么?它有什么分辨率?其他优点/缺点是什么?



可选:它是如何工作的?



可选:不使用,为什么?



我主要寻找Windows / C ++解决方案,但如果你想评论其他系统,请随意这样做。 / p>

(目前我们使用GetTickCount(),但它不是一个非常精确的计时器。)

解决方案

这是我的答案的副本: C ++ Timer函数以纳秒为单位提供时间



对于Linux(和BSD),您希望使用 clock_gettime()

  #include< sys / time.h> 

int main()
{
timespec ts;
// clock_gettime(CLOCK_MONOTONIC,& ts); // FreeBSD上的工作
clock_gettime(CLOCK_REALTIME,& ts); //适用于Linux
}

对于Windows,您要使用 QueryPerformanceCounter 。这里还有更多 QPC



很明显,在某些芯片组上,有一个已知的问题与QPC,因此您可以想确保你没有那些芯​​片组。此外,一些双核AMD还可能会导致问题。查看sebbbi的第二篇文章,他说:


QueryPerformanceCounter()和
QueryPerformanceFrequency位更好的分辨率,但有
不同的问题。例如在
Windows XP中,所有AMD Athlon X2双核bbb核CPU将
核的随机(有时
向后跳一点)的PC返回,除非你
特别安装AMD双核驱动程序
软件包来解决这个问题。我们没有
注意到任何其他具有类似问题的双核CPUs
(p4 dual,p4 ht,
core2 dual,core2 quad,phenom quad)。



When measuring network latency (time ack received - time msg sent) in any protocol over TCP, what timer would you recommend to use and why? What resolution does it have? What are other advantages/disadvantages?

Optional: how does it work?

Optional: what timer would you NOT use and why?

I'm looking mostly for Windows / C++ solutions, but if you'd like to comment on other systems, feel free to do so.

(Currently we use GetTickCount(), but it's not a very accurate timer.)

解决方案

This is a copy of my answer from: C++ Timer function to provide time in nano seconds

For Linux (and BSD) you want to use clock_gettime().

#include <sys/time.h>

int main()
{
   timespec ts;
   // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
   clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux
}

For windows you want to use the QueryPerformanceCounter. And here is more on QPC

Apparently there is a known issue with QPC on some chipsets, so you may want to make sure you do not have those chipset. Additionally some dual core AMDs may also cause a problem. See the second post by sebbbi, where he states:

QueryPerformanceCounter() and QueryPerformanceFrequency() offer a bit better resolution, but have different issues. For example in Windows XP, all AMD Athlon X2 dual core CPUs return the PC of either of the cores "randomly" (the PC sometimes jumps a bit backwards), unless you specially install AMD dual core driver package to fix the issue. We haven't noticed any other dual+ core CPUs having similar issues (p4 dual, p4 ht, core2 dual, core2 quad, phenom quad).

这篇关于测量延迟的定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆