为什么libuv计时器漂移?

时间:2017-02-18 13:02:46

标签: c++ timer libuv drift

在尝试使用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] :(差异总是大于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

2 个答案:

答案 0 :(得分:1)

libuv重复计时器不会调整其回调所需的时间。它们在调用回调之前的重复间隔期间重新参与,请参见此处:https://github.com/libuv/libuv/blob/v1.x/src/unix/timer.c#L165

有些人会两种方式看待它,所以为了调整你的期望,我建议你使用单次计时器并在你认为合适的情况下在回调中重新安装它,就像你一样。

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

答案 1 :(得分:0)

它不适合漂流。它有一个更长的(让我说)延迟可能是因为回调是在t毫秒后发生的第一次迭代时安排的。 实际上,libev有类似的行为,如果你认为有时回调是在时间到期之前安排的(至少在你的例子中),那就更糟了。
请注意,计时器不提供严格的计划,但是尽力而为(即,它会在间隔后尽快执行回调 - 作为一个不太好的示例,如果您有100个会发生什么,会发生什么? CPU绑定的进程同时触发所有进程?你不能指望所有的回调都没有延迟。