为什么libuv计时器漂移

Why is libuv timer drifting?

本文关键字:漂移 计时器 libuv 为什么      更新时间:2023-10-16

尝试使用libuv实现一个简单的计时器时,我注意到回调是"漂移"。我在这里做错了什么?

代码:

uv_timer_t timer;
uv_timer_init(uv_default_loop(), &timer);
double ts = std::chrono::high_resolution_clock::now().time_since_epoch().count();
timer.data = &ts;
uv_timer_start(&timer,
               [](uv_timer_t* handle) {
                 double* before = (double*)handle->data;
                 double now =
                     std::chrono::high_resolution_clock::now().time_since_epoch().count();
                 auto diff = now - (*before);
                 is::log::info("tick... diff={}ns", diff);
                 *before = now;
               },
               1000, 1000);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);

输出[libuv] :( diff始终大于1.0)

[info][10:48:26:636439] tick... diff=1.00083e+09ns
[info][10:48:27:637071] tick... diff=1.00063e+09ns
[info][10:48:28:638155] tick... diff=1.00108e+09ns
[info][10:48:29:639219] tick... diff=1.00106e+09ns
[info][10:48:30:640291] tick... diff=1.00107e+09ns
[info][10:48:31:641364] tick... diff=1.00107e+09ns
[info][10:48:32:641457] tick... diff=1.00009e+09ns
[info][10:48:33:642468] tick... diff=1.00101e+09ns
[info][10:48:34:643621] tick... diff=1.00115e+09ns
[info][10:48:35:644701] tick... diff=1.00108e+09ns

等效的libev代码可完美。

代码:

ev_timer timer;
double ts = std::chrono::high_resolution_clock::now().time_since_epoch().count();
timer.data = &ts;
ev_timer_init(&timer,
              [](struct ev_loop*, ev_timer* handle, int) {
                double* before = (double*)handle->data;
                double now =
                    std::chrono::high_resolution_clock::now().time_since_epoch().count();
                auto diff = now - (*before);
                is::log::info("tick... diff={}ns", diff);
                *before = now;
              },
              1.0, 1.0);
ev_timer_start(EV_DEFAULT, &timer);
ev_run(EV_DEFAULT, 0);

输出[libev]:

[info][10:54:01:624788] tick... diff=1.00015e+09ns
[info][10:54:02:624943] tick... diff=1.00016e+09ns
[info][10:54:03:625035] tick... diff=1.00009e+09ns
[info][10:54:04:625177] tick... diff=1.00014e+09ns
[info][10:54:05:624284] tick... diff=9.99106e+08ns
[info][10:54:06:624415] tick... diff=1.00013e+09ns
[info][10:54:07:624533] tick... diff=1.00012e+09ns
[info][10:54:08:624592] tick... diff=1.00006e+09ns
[info][10:54:09:625245] tick... diff=1.00065e+09ns
[info][10:54:10:624331] tick... diff=9.99086e+08ns

解决方案

实现了我自己的计时器补偿逻辑,该逻辑启动了一个新的计时器,并在每个迭代中都具有"正确"超时。

using namespace std::chrono;
struct Timer {
  uv_loop_t* loop;
  uv_timer_t timer;
  uint64_t period;
  time_point<high_resolution_clock> ref;
  Timer(uv_loop_t* loop, uint64_t period)
      : loop(loop), period(period), ref(high_resolution_clock::now() + milliseconds(period)) {
    uv_timer_init(loop, &timer);
    timer.data = this;
    uv_timer_start(&timer, callback, period, 0);
  }
  static void callback(uv_timer_t* handle) {
    auto self = (Timer*)handle->data;
    {
      auto delta = self->ref - high_resolution_clock::now();
      auto delta_count = duration_cast<milliseconds>(delta).count();
      is::log::info("tick... delta={}ms", delta_count);
    }
    { 
      self->ref = self->ref + milliseconds(self->period);
      auto delta = self->ref - high_resolution_clock::now();
      auto delta_count = duration_cast<milliseconds>(delta).count();
      uv_timer_start(&self->timer, self->callback, delta_count, 0);
    }
  }
};
Timer timer(uv_default_loop(), 1000);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);

输出:

[info][14:20:19:772354] tick... delta=-1ms
[info][14:20:20:771120] tick... delta=0ms
[info][14:20:21:772235] tick... delta=-1ms
[info][14:20:22:771040] tick... delta=0ms
[info][14:20:23:772174] tick... delta=0ms
[info][14:20:24:771302] tick... delta=0ms
[info][14:20:25:771448] tick... delta=0ms
[info][14:20:26:771568] tick... delta=0ms
[info][14:20:27:771117] tick... delta=0ms
[info][14:20:28:772250] tick... delta=-1ms
[info][14:20:29:771374] tick... delta=0ms
[info][14:20:30:771495] tick... delta=0ms
[info][14:20:31:771608] tick... delta=0ms
[info][14:20:32:771691] tick... delta=0ms

libuv重复计时器在回调时没有进行调整。在调用回调之前,它们会在重复间隔的持续时间内重新参与,请参阅此处:https://github.com/libuv/libuv/libuv/blob/blob/v1.x/src/src/unix/timer.c#l165

有些人两种方式都在看,因此,为了调整您的期望,我建议您使用单次计时器并在回调中对其进行重新呼叫,就像您一样。

在事后看来,我希望我们不包括重复计时器。

它不能正确地漂移。它具有更长的时间(我说) delay 可能是由于回调计划在t毫秒之后进行的第一次迭代中。
实际上,LIBEV具有类似的行为,如果您认为有时在时间过期之前安排回调(至少在您的示例中)。
请注意,计时器没有提供严格的时间表,而是最佳努力方法(也就是说,在间隔后尽快执行回调 - 作为一个不太好的例子,如果您有100个CPU绑定,会发生什么情况处理所有同时发射的过程?您不能指望所有回调根本没有延迟)。