aboutsummaryrefslogtreecommitdiff
path: root/test/test-metrics.c
blob: 361fcef5ced023d1b612459c4ea41e4e11eae25a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* Copyright libuv project contributors. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "uv.h"
#include "task.h"
#include <string.h> /* memset */

#define UV_NS_TO_MS 1000000

typedef struct {
  uv_fs_t open_req;
  uv_fs_t write_req;
  uv_fs_t close_req;
} fs_reqs_t;

static uint64_t last_events_count;
static char test_buf[] = "test-buffer\n";
static fs_reqs_t fs_reqs;
static int pool_events_counter;


static void timer_spin_cb(uv_timer_t* handle) {
  uint64_t t;

  (*(int*) handle->data)++;
  t = uv_hrtime();
  /* Spin for 500 ms to spin loop time out of the delta check. */
  while (uv_hrtime() - t < 600 * UV_NS_TO_MS) { }
}


TEST_IMPL(metrics_idle_time) {
#if defined(__OpenBSD__)
  RETURN_SKIP("Test does not currently work in OpenBSD");
#endif
  const uint64_t timeout = 1000;
  uv_timer_t timer;
  uint64_t idle_time;
  int cntr;

  cntr = 0;
  timer.data = &cntr;

  ASSERT_OK(uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME));
  ASSERT_OK(uv_timer_init(uv_default_loop(), &timer));
  ASSERT_OK(uv_timer_start(&timer, timer_spin_cb, timeout, 0));

  ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  ASSERT_GT(cntr, 0);

  idle_time = uv_metrics_idle_time(uv_default_loop());

  /* Permissive check that the idle time matches within the timeout ±500 ms. */
  ASSERT_LE(idle_time, (timeout + 500) * UV_NS_TO_MS);
  ASSERT_GE(idle_time, (timeout - 500) * UV_NS_TO_MS);

  MAKE_VALGRIND_HAPPY(uv_default_loop());
  return 0;
}


static void metrics_routine_cb(void* arg) {
  const uint64_t timeout = 1000;
  uv_loop_t loop;
  uv_timer_t timer;
  uint64_t idle_time;
  int cntr;

  cntr = 0;
  timer.data = &cntr;

  ASSERT_OK(uv_loop_init(&loop));
  ASSERT_OK(uv_loop_configure(&loop, UV_METRICS_IDLE_TIME));
  ASSERT_OK(uv_timer_init(&loop, &timer));
  ASSERT_OK(uv_timer_start(&timer, timer_spin_cb, timeout, 0));

  ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));
  ASSERT_GT(cntr, 0);

  idle_time = uv_metrics_idle_time(&loop);

  /* Only checking that idle time is greater than the lower bound since there
   * may have been thread contention, causing the event loop to be delayed in
   * the idle phase longer than expected.
   */
  ASSERT_GE(idle_time, (timeout - 500) * UV_NS_TO_MS);

  close_loop(&loop);
  ASSERT_OK(uv_loop_close(&loop));
}


TEST_IMPL(metrics_idle_time_thread) {
  uv_thread_t threads[5];
  int i;

  for (i = 0; i < 5; i++) {
    ASSERT_OK(uv_thread_create(&threads[i], metrics_routine_cb, NULL));
  }

  for (i = 0; i < 5; i++) {
    uv_thread_join(&threads[i]);
  }

  return 0;
}


static void timer_noop_cb(uv_timer_t* handle) {
  (*(int*) handle->data)++;
}


TEST_IMPL(metrics_idle_time_zero) {
  uv_metrics_t metrics;
  uv_timer_t timer;
  int cntr;

  cntr = 0;
  timer.data = &cntr;
  ASSERT_OK(uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME));
  ASSERT_OK(uv_timer_init(uv_default_loop(), &timer));
  ASSERT_OK(uv_timer_start(&timer, timer_noop_cb, 0, 0));

  ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));

  ASSERT_GT(cntr, 0);
  ASSERT_OK(uv_metrics_idle_time(uv_default_loop()));

  ASSERT_OK(uv_metrics_info(uv_default_loop(), &metrics));
  ASSERT_UINT64_EQ(cntr, metrics.loop_count);

  MAKE_VALGRIND_HAPPY(uv_default_loop());
  return 0;
}


static void close_cb(uv_fs_t* req) {
  uv_metrics_t metrics;

  ASSERT_OK(uv_metrics_info(uv_default_loop(), &metrics));
  ASSERT_UINT64_EQ(3, metrics.loop_count);
  ASSERT_UINT64_GT(metrics.events, last_events_count);

  uv_fs_req_cleanup(req);
  last_events_count = metrics.events;
}


static void write_cb(uv_fs_t* req) {
  uv_metrics_t metrics;

  ASSERT_OK(uv_metrics_info(uv_default_loop(), &metrics));
  ASSERT_UINT64_EQ(2, metrics.loop_count);
  ASSERT_UINT64_GT(metrics.events, last_events_count);
  ASSERT_EQ(req->result, sizeof(test_buf));

  uv_fs_req_cleanup(req);
  last_events_count = metrics.events;

  ASSERT_OK(uv_fs_close(uv_default_loop(),
                        &fs_reqs.close_req,
                        fs_reqs.open_req.result,
                        close_cb));
}


static void create_cb(uv_fs_t* req) {
  uv_metrics_t metrics;

  ASSERT_OK(uv_metrics_info(uv_default_loop(), &metrics));
  /* Event count here is still 0 so not going to check. */
  ASSERT_UINT64_EQ(1, metrics.loop_count);
  ASSERT_GE(req->result, 0);

  uv_fs_req_cleanup(req);
  last_events_count = metrics.events;

  uv_buf_t iov = uv_buf_init(test_buf, sizeof(test_buf));
  ASSERT_OK(uv_fs_write(uv_default_loop(),
                        &fs_reqs.write_req,
                        req->result,
                        &iov,
                        1,
                        0,
                        write_cb));
}


static void prepare_cb(uv_prepare_t* handle) {
  uv_metrics_t metrics;

  uv_prepare_stop(handle);

  ASSERT_OK(uv_metrics_info(uv_default_loop(), &metrics));
  ASSERT_UINT64_EQ(0, metrics.loop_count);
  ASSERT_UINT64_EQ(0, metrics.events);

  ASSERT_OK(uv_fs_open(uv_default_loop(),
                       &fs_reqs.open_req,
                       "test_file",
                       UV_FS_O_WRONLY | UV_FS_O_CREAT, S_IRUSR | S_IWUSR,
                       create_cb));
}


TEST_IMPL(metrics_info_check) {
  uv_fs_t unlink_req;
  uv_prepare_t prepare;

  uv_fs_unlink(NULL, &unlink_req, "test_file", NULL);
  uv_fs_req_cleanup(&unlink_req);

  ASSERT_OK(uv_prepare_init(uv_default_loop(), &prepare));
  ASSERT_OK(uv_prepare_start(&prepare, prepare_cb));

  ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));

  uv_fs_unlink(NULL, &unlink_req, "test_file", NULL);
  uv_fs_req_cleanup(&unlink_req);

  MAKE_VALGRIND_HAPPY(uv_default_loop());
  return 0;
}


static void fs_prepare_cb(uv_prepare_t* handle) {
  uv_metrics_t metrics;

  ASSERT_OK(uv_metrics_info(uv_default_loop(), &metrics));

  if (pool_events_counter == 1)
    ASSERT_EQ(metrics.events, metrics.events_waiting);

  if (pool_events_counter < 7)
    return;

  uv_prepare_stop(handle);
  pool_events_counter = -42;
}


static void fs_stat_cb(uv_fs_t* req) {
  uv_fs_req_cleanup(req);
  pool_events_counter++;
}


static void fs_work_cb(uv_work_t* req) {
}


static void fs_after_work_cb(uv_work_t* req, int status) {
  free(req);
  pool_events_counter++;
}


static void fs_write_cb(uv_fs_t* req) {
  uv_work_t* work1 = malloc(sizeof(*work1));
  uv_work_t* work2 = malloc(sizeof(*work2));
  pool_events_counter++;

  uv_fs_req_cleanup(req);

  ASSERT_OK(uv_queue_work(uv_default_loop(),
                          work1,
                          fs_work_cb,
                          fs_after_work_cb));
  ASSERT_OK(uv_queue_work(uv_default_loop(),
                          work2,
                          fs_work_cb,
                          fs_after_work_cb));
}


static void fs_random_cb(uv_random_t* req, int status, void* buf, size_t len) {
  pool_events_counter++;
}


static void fs_addrinfo_cb(uv_getaddrinfo_t* req,
                           int status,
                           struct addrinfo* res) {
  uv_freeaddrinfo(req->addrinfo);
  pool_events_counter++;
}


TEST_IMPL(metrics_pool_events) {
  uv_buf_t iov;
  uv_fs_t open_req;
  uv_fs_t stat1_req;
  uv_fs_t stat2_req;
  uv_fs_t unlink_req;
  uv_fs_t write_req;
  uv_getaddrinfo_t addrinfo_req;
  uv_metrics_t metrics;
  uv_prepare_t prepare;
  uv_random_t random_req;
  int fd;
  char rdata;

  ASSERT_OK(uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME));

  uv_fs_unlink(NULL, &unlink_req, "test_file", NULL);
  uv_fs_req_cleanup(&unlink_req);

  ASSERT_OK(uv_prepare_init(uv_default_loop(), &prepare));
  ASSERT_OK(uv_prepare_start(&prepare, fs_prepare_cb));

  pool_events_counter = 0;
  fd = uv_fs_open(NULL,
                  &open_req, "test_file", UV_FS_O_WRONLY | UV_FS_O_CREAT,
                  S_IRUSR | S_IWUSR,
                  NULL);
  ASSERT_GT(fd, 0);
  uv_fs_req_cleanup(&open_req);

  iov = uv_buf_init(test_buf, sizeof(test_buf));
  ASSERT_OK(uv_fs_write(uv_default_loop(),
                        &write_req,
                        fd,
                        &iov,
                        1,
                        0,
                        fs_write_cb));
  ASSERT_OK(uv_fs_stat(uv_default_loop(),
                       &stat1_req,
                       "test_file",
                       fs_stat_cb));
  ASSERT_OK(uv_fs_stat(uv_default_loop(),
                       &stat2_req,
                       "test_file",
                       fs_stat_cb));
  ASSERT_OK(uv_random(uv_default_loop(),
                      &random_req,
                      &rdata,
                      1,
                      0,
                      fs_random_cb));
  ASSERT_OK(uv_getaddrinfo(uv_default_loop(),
                           &addrinfo_req,
                           fs_addrinfo_cb,
                           "example.invalid",
                           NULL,
                           NULL));

  /* Sleep for a moment to hopefully force the events to complete before
   * entering the event loop. */
  uv_sleep(100);

  ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));

  ASSERT_OK(uv_metrics_info(uv_default_loop(), &metrics));
  /* It's possible for uv__work_done() to execute one extra time even though the
   * QUEUE has already been cleared out. This has to do with the way we use an
   * uv_async to tell the event loop thread to process the worker pool QUEUE. */
  ASSERT_GE(metrics.events, 7);
  /* It's possible one of the other events also got stuck in the event queue, so
   * check GE instead of EQ. Reason for 4 instead of 5 is because the call to
   * uv_getaddrinfo() is racey and slow. So can't guarantee that it'll always
   * execute before sleep completes. */
  ASSERT_GE(metrics.events_waiting, 4);
  ASSERT_EQ(pool_events_counter, -42);

  uv_fs_unlink(NULL, &unlink_req, "test_file", NULL);
  uv_fs_req_cleanup(&unlink_req);

  MAKE_VALGRIND_HAPPY(uv_default_loop());
  return 0;
}