Skip to content

Commit 0516670

Browse files
authored
H2O: Track connection load across the worker threads (#9520)
1 parent 8b34594 commit 0516670

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

frameworks/C/h2o/src/event_loop.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@
3232
#include <netinet/tcp.h>
3333
#include <openssl/ssl.h>
3434
#include <sys/socket.h>
35+
#include <sys/syscall.h>
3536
#include <sys/types.h>
3637

3738
#include "error.h"
3839
#include "event_loop.h"
3940
#include "global_data.h"
4041
#include "thread.h"
42+
#include "utility.h"
4143

44+
#define CONN_NUM_SAMPLE_PERIOD 2500
4245
#define DEFAULT_TCP_FASTOPEN_QUEUE_LEN 4096
4346

4447
static void accept_connection(h2o_socket_t *listener, const char *err);
@@ -72,6 +75,7 @@ static void accept_connection(h2o_socket_t *listener, const char *err)
7275
if (!sock)
7376
break;
7477

78+
ctx->event_loop.accepted_conn_num++;
7579
ctx->event_loop.conn_num++;
7680
sock->on_close.cb = on_close_connection;
7781
sock->on_close.data = &ctx->event_loop.conn_num;
@@ -277,11 +281,36 @@ static void start_accept_polling(const config_t *config,
277281

278282
void event_loop(struct thread_context_t *ctx)
279283
{
284+
uint64_t last_sample = 0;
285+
280286
while (!ctx->shutdown || ctx->event_loop.conn_num) {
281287
h2o_evloop_run(ctx->event_loop.h2o_ctx.loop, INT32_MAX);
282288
process_messages(&ctx->global_thread_data->h2o_receiver,
283289
&ctx->event_loop.local_messages);
290+
291+
const uint64_t now = h2o_now(ctx->event_loop.h2o_ctx.loop);
292+
293+
if (now - last_sample > CONN_NUM_SAMPLE_PERIOD || last_sample > now) {
294+
const size_t i = ctx->event_loop.conn_num_sample_idx;
295+
296+
ctx->event_loop.conn_num_sample[i] = ctx->event_loop.conn_num;
297+
ctx->event_loop.conn_num_sample_idx =
298+
(i + 1) % ARRAY_SIZE(ctx->event_loop.conn_num_sample);
299+
last_sample = now;
300+
}
284301
}
302+
303+
flockfile(stdout);
304+
printf("Thread %ld statistics:\nAccepted connections: %zu\nConnection number samples: %zu",
305+
syscall(SYS_gettid),
306+
ctx->event_loop.accepted_conn_num,
307+
*ctx->event_loop.conn_num_sample);
308+
309+
for (size_t i = 1; i < ARRAY_SIZE(ctx->event_loop.conn_num_sample); i++)
310+
printf(",%zu", ctx->event_loop.conn_num_sample[i]);
311+
312+
putc_unlocked('\n', stdout);
313+
funlockfile(stdout);
285314
}
286315

287316
void free_event_loop(event_loop_t *event_loop, h2o_multithread_receiver_t *h2o_receiver)

frameworks/C/h2o/src/event_loop.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include "global_data.h"
2929

30+
#define CONN_NUM_SAMPLES 512
31+
3032
typedef enum {
3133
SHUTDOWN,
3234
TASK
@@ -41,6 +43,9 @@ typedef struct {
4143
h2o_accept_ctx_t h2o_accept_ctx;
4244
h2o_context_t h2o_ctx;
4345
h2o_linklist_t local_messages;
46+
size_t accepted_conn_num;
47+
size_t conn_num_sample[CONN_NUM_SAMPLES];
48+
size_t conn_num_sample_idx;
4449
} event_loop_t;
4550

4651
typedef struct {

0 commit comments

Comments
 (0)