@cryptotaxi247 / netdata-1 / commits / f3f70779e

global variables to worker private structure

Costa Tsaousis (ktsaou) committed Jan 8, 2018 at 01:17 UTC f3f70779efc5bf7fa84a765efc8bf92a1035577f
2 files changed +46 -32
src/socket.c
+4
@@ -965,6 +965,8 @@ int accept_socket(int fd, int flags, char *client_ip, size_t ipsize, char *clien
965 #define POLLINFO_FLAG_CLIENT_SOCKET 0x00000002
966
967 struct pollinfo {
968 + struct poll *p; // the parent
969 +
970 size_t slot;
971 char *client_ip;
972 char *client_port;
@@ -1014,6 +1016,7 @@ static inline struct pollinfo *poll_add_fd(struct poll *p, int fd, int socktype,
1016 p->fds[i].events = 0;
1017 p->fds[i].revents = 0;
1018
1019 + p->inf[i].p = p;
1020 p->inf[i].slot = (size_t)i;
1021 p->inf[i].flags = 0;
1022 p->inf[i].socktype = -1;
@@ -1043,6 +1046,7 @@ static inline struct pollinfo *poll_add_fd(struct poll *p, int fd, int socktype,
1046 pf->events = events;
1047 pf->revents = 0;
1048
1049 + pi->p = p;
1050 pi->socktype = socktype;
1051 pi->flags = flags;
1052 pi->next = NULL;
src/web_server.c
+42 -32
@@ -405,17 +405,31 @@ void *socket_listen_main_single_threaded(void *ptr) {
405 // --------------------------------------------------------------------------------------
406 // the main socket listener - STATIC-THREADED
407
408 -static __thread size_t web_server_static_connected = 0,
409 - web_server_static_disconnected = 0,
410 - web_server_static_receptions = 0,
411 - web_server_static_sends = 0;
408 +struct web_server_static_threaded_worker {
409 + netdata_thread_t thread;
410 +
411 + int running;
412 +
413 + volatile size_t connected;
414 + volatile size_t disconnected;
415 + volatile size_t receptions;
416 + volatile size_t sends;
417 + volatile size_t max_concurrent;
418 +};
419 +
420 +static long long static_threaded_workers_count = 1;
421 +static struct web_server_static_threaded_worker *static_workers_private_data = NULL;
422 +static __thread struct web_server_static_threaded_worker *worker_private = NULL;
423
424 // new TCP client connected
425 static void *web_server_add_callback(int fd, int socktype, short int *events, const char *client_ip, const char *client_port) {
426 (void)fd;
427 (void)socktype;
428
418 - web_server_static_connected++;
429 + worker_private->connected++;
430 + size_t concurrent = worker_private->connected - worker_private->disconnected;
431 + if(unlikely(concurrent > worker_private->max_concurrent))
432 + worker_private->max_concurrent = concurrent;
433
434 *events = POLLIN;
435
@@ -435,7 +449,7 @@ static void web_server_del_callback(int fd, int socktype, void *data) {
449 (void)fd;
450 (void)socktype;
451
438 - web_server_static_disconnected++;
452 + worker_private->disconnected++;
453
454 struct web_client *w = (struct web_client *)data;
455
@@ -464,7 +478,7 @@ static int web_server_rcv_callback(int fd, int socktype, void *data, short int *
478 (void)fd;
479 (void)socktype;
480
467 - web_server_static_receptions++;
481 + worker_private->receptions++;
482
483 struct web_client *w = (struct web_client *)data;
484
@@ -503,7 +517,7 @@ static int web_server_snd_callback(int fd, int socktype, void *data, short int *
517 (void)fd;
518 (void)socktype;
519
506 - web_server_static_sends++;
520 + worker_private->sends++;
521
522 struct web_client *w = (struct web_client *)data;
523
@@ -523,20 +537,21 @@ static int web_server_snd_callback(int fd, int socktype, void *data, short int *
537 }
538
539 static void socket_listen_main_static_threaded_worker_cleanup(void *ptr) {
526 - int *running = (int *)ptr;
527 - *running = 0;
528 -
529 - info("stopped after %zu connects, %zu disconnects, %zu receptions and %zu sends",
530 - web_server_static_connected,
531 - web_server_static_disconnected,
532 - web_server_static_receptions,
533 - web_server_static_sends
540 + worker_private = (struct web_server_static_threaded_worker *)ptr;
541 + worker_private->running = 0;
542 +
543 + info("stopped after %zu connects, %zu disconnects (max concurrent %zu), %zu receptions and %zu sends",
544 + worker_private->connected,
545 + worker_private->disconnected,
546 + worker_private->max_concurrent,
547 + worker_private->receptions,
548 + worker_private->sends
549 );
550 }
551
552 void *socket_listen_main_static_threaded_worker(void *ptr) {
538 - int *running = (int *)ptr;
539 - *running = 1;
553 + worker_private = (struct web_server_static_threaded_worker *)ptr;
554 + worker_private->running = 1;
555
556 netdata_thread_cleanup_push(socket_listen_main_static_threaded_worker_cleanup, ptr);
557
@@ -553,20 +568,16 @@ void *socket_listen_main_static_threaded_worker(void *ptr) {
568 return NULL;
569 }
570
556 -static long long static_threaded_threads_count = 1;
557 -static netdata_thread_t *static_threaded_threads_ids = NULL;
558 -static volatile int *static_threaded_threads_running = NULL;
559 -
571 static void socket_listen_main_static_threaded_cleanup(void *ptr) {
572 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
573 if(static_thread->enabled) {
574 static_thread->enabled = 0;
575
576 int i;
566 - for(i = 1; i < static_threaded_threads_count; i++) {
567 - if(static_threaded_threads_running[i]) {
577 + for(i = 1; i < static_threaded_workers_count; i++) {
578 + if(static_workers_private_data[i].running) {
579 info("stopping worker %d", i + 1);
569 - netdata_thread_cancel(static_threaded_threads_ids[i]);
580 + netdata_thread_cancel(static_workers_private_data[i].thread);
581 }
582 else
583 info("found stopped worker %d", i + 1);
@@ -589,23 +600,22 @@ void *socket_listen_main_static_threaded(void *ptr) {
600 // so, if the machine has more CPUs, avoid using resources unnecessarily
601 int def_thread_count = (processors > 6)?6:processors;
602
592 - static_threaded_threads_count = config_get_number(CONFIG_SECTION_WEB, "web server threads", def_thread_count);
593 - if(static_threaded_threads_count < 1) static_threaded_threads_count = 1;
603 + static_threaded_workers_count = config_get_number(CONFIG_SECTION_WEB, "web server threads", def_thread_count);
604 + if(static_threaded_workers_count < 1) static_threaded_workers_count = 1;
605
595 - static_threaded_threads_ids = callocz((size_t)static_threaded_threads_count, sizeof(netdata_thread_t));
596 - static_threaded_threads_running = callocz((size_t)static_threaded_threads_count, sizeof(int));
606 + static_workers_private_data = callocz((size_t)static_threaded_workers_count, sizeof(struct web_server_static_threaded_worker));
607
608 int i;
599 - for(i = 1; i < static_threaded_threads_count; i++) {
609 + for(i = 1; i < static_threaded_workers_count; i++) {
610 char tag[50 + 1];
611 snprintfz(tag, 50, "WEB_SERVER[static%d]", i+1);
612
613 info("starting worker %d", i+1);
604 - netdata_thread_create(&static_threaded_threads_ids[i], tag, NETDATA_THREAD_OPTION_DEFAULT, socket_listen_main_static_threaded_worker, (void *)&static_threaded_threads_running[i]);
614 + netdata_thread_create(&static_workers_private_data[i].thread, tag, NETDATA_THREAD_OPTION_DEFAULT, socket_listen_main_static_threaded_worker, (void *)&static_workers_private_data[i]);
615 }
616
617 // and the main one
608 - socket_listen_main_static_threaded_worker((void *)&static_threaded_threads_running[0]);
618 + socket_listen_main_static_threaded_worker((void *)&static_workers_private_data[0]);
619
620 netdata_thread_cleanup_pop(1);
621 return NULL;