unified thread creation and cleanup throughout the code
Costa Tsaousis (ktsaou) committed
Dec 27, 2017 at 22:57 UTC
0fd85c22255a4f7e8f23c78e8e07c82fe5fb6bf3
20 files changed
+247
-278
src/backends.c
+14
-21
@@ -498,22 +498,25 @@ inline uint32_t backend_parse_data_source(const char *source, uint32_t mode) {
498
return mode;
499
}
500
501
+static void backends_main_cleanup(void *ptr) {
502
+ struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
503
+ if(static_thread->enabled) {
504
+ static_thread->enabled = 0;
505
+
506
+ info("%s: cleaning up...", netdata_thread_tag());
507
+ }
508
+}
509
+
510
void *backends_main(void *ptr) {
511
+ netdata_thread_welcome("BACKEND");
512
+
513
int default_port = 0;
514
int sock = -1;
504
- struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
505
-
515
BUFFER *b = buffer_create(1), *response = buffer_create(1);
516
int (*backend_request_formatter)(BUFFER *, const char *, RRDHOST *, const char *, RRDSET *, RRDDIM *, time_t, time_t, uint32_t) = NULL;
517
int (*backend_response_checker)(BUFFER *) = NULL;
518
510
- info("BACKEND: thread created with task id %d", gettid());
511
-
512
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
513
- error("BACKEND: cannot set pthread cancel type to DEFERRED.");
514
-
515
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
516
- error("BACKEND: cannot set pthread cancel state to ENABLE.");
519
+ pthread_cleanup_push(backends_main_cleanup, ptr);
520
521
// ------------------------------------------------------------------------
522
// collect configuration options
@@ -664,7 +667,7 @@ void *backends_main(void *ptr) {
667
heartbeat_t hb;
668
heartbeat_init(&hb);
669
667
- for(;;) {
670
+ while(!netdata_exit) {
671
672
// ------------------------------------------------------------------------
673
// Wait for the next iteration point.
@@ -676,11 +679,6 @@ void *backends_main(void *ptr) {
679
// ------------------------------------------------------------------------
680
// add to the buffer the data we need to send to the backend
681
679
- int pthreadoldcancelstate;
680
-
681
- if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
682
- error("BACKEND: cannot set pthread cancel state to DISABLE.");
683
-
682
size_t count_hosts = 0;
683
size_t count_charts_total = 0;
684
size_t count_dims_total = 0;
@@ -730,9 +728,6 @@ void *backends_main(void *ptr) {
728
729
debug(D_BACKEND, "BACKEND: buffer has %zu bytes, added metrics for %zu dimensions, of %zu charts, from %zu hosts", buffer_strlen(b), count_dims_total, count_charts_total, count_hosts);
730
733
- if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
734
- error("BACKEND: cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
735
-
731
// ------------------------------------------------------------------------
732
733
chart_buffered_bytes = (collected_number)buffer_strlen(b);
@@ -918,9 +913,7 @@ cleanup:
913
buffer_free(b);
914
buffer_free(response);
915
921
- info("BACKEND: thread exiting");
922
-
923
- static_thread->enabled = 0;
916
+ pthread_cleanup_pop(1);
917
pthread_exit(NULL);
918
return NULL;
919
}
src/health.c
+12
-18
@@ -338,22 +338,24 @@ static inline int check_if_resumed_from_suspention(void) {
338
return ret;
339
}
340
341
-void *health_main(void *ptr) {
341
+static void health_main_cleanup(void *ptr) {
342
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
343
+ if(static_thread->enabled) {
344
+ static_thread->enabled = 0;
345
344
- info("HEALTH thread created with task id %d", gettid());
346
+ info("%s: cleaning up...", netdata_thread_tag());
347
+ }
348
+}
349
346
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
347
- error("Cannot set pthread cancel type to DEFERRED.");
350
+void *health_main(void *ptr) {
351
+ netdata_thread_welcome("HEALTH");
352
349
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
350
- error("Cannot set pthread cancel state to ENABLE.");
353
+ BUFFER *wb = buffer_create(100);
354
+ pthread_cleanup_push(health_main_cleanup, ptr);
355
356
int min_run_every = (int)config_get_number(CONFIG_SECTION_HEALTH, "run at least every seconds", 10);
357
if(min_run_every < 1) min_run_every = 1;
358
355
- BUFFER *wb = buffer_create(100);
356
-
359
time_t now = now_realtime_sec();
360
time_t hibernation_delay = config_get_number(CONFIG_SECTION_HEALTH, "postpone alarms during hibernation for seconds", 60);
361
@@ -362,7 +364,7 @@ void *health_main(void *ptr) {
364
loop++;
365
debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
366
365
- int oldstate, runnable = 0, apply_hibernation_delay = 0;
367
+ int runnable = 0, apply_hibernation_delay = 0;
368
time_t next_run = now + min_run_every;
369
RRDCALC *rc;
370
@@ -374,9 +376,6 @@ void *health_main(void *ptr) {
376
);
377
}
378
377
- if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
378
- error("Cannot set pthread cancel state to DISABLE.");
379
-
379
rrd_rdlock();
380
381
RRDHOST *host;
@@ -721,9 +720,6 @@ void *health_main(void *ptr) {
720
721
rrd_unlock();
722
724
- if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
725
- error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
726
-
723
if(unlikely(netdata_exit))
724
break;
725
@@ -740,9 +736,7 @@ void *health_main(void *ptr) {
736
737
buffer_free(wb);
738
743
- info("HEALTH thread exiting");
744
-
745
- static_thread->enabled = 0;
739
+ pthread_cleanup_pop(1);
740
pthread_exit(NULL);
741
return NULL;
742
}
src/locks.c
+28
-4
@@ -1,5 +1,29 @@
1
#include "common.h"
2
3
+// ----------------------------------------------------------------------------
4
+// threads initialization
5
+
6
+static __thread char *netdata_thread_tag_name = NULL;
7
+
8
+const char *netdata_thread_tag(void) {
9
+ return ((netdata_thread_tag_name && *netdata_thread_tag_name)?netdata_thread_tag_name:"unknown");
10
+}
11
+
12
+void netdata_thread_welcome_nolog(char *tag) {
13
+ netdata_thread_tag_name = tag;
14
+
15
+ if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
16
+ error("%s: cannot set pthread cancel type to DEFERRED.", netdata_thread_tag());
17
+
18
+ if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
19
+ error("%s: cannot set pthread cancel state to ENABLE.", netdata_thread_tag());
20
+}
21
+
22
+void netdata_thread_welcome(char *tag) {
23
+ netdata_thread_welcome_nolog(tag);
24
+ info("%s: thread created with task id %d", netdata_thread_tag(), gettid());
25
+}
26
+
27
// ----------------------------------------------------------------------------
28
// automatic thread cancelability management, based on locks
29
@@ -10,7 +34,7 @@ static inline void netdata_thread_disable_cancelability(void) {
34
int old;
35
int ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
36
if(ret != 0)
13
- error("THREAD_CANCELABILITY: pthread_setcancelstate() returned error %d", ret);
37
+ error("THREAD_CANCELABILITY: pthread_setcancelstate() on thread %s returned error %d", netdata_thread_tag(), ret);
38
else {
39
if(!netdata_thread_lock_cancelability)
40
netdata_thread_first_cancelability = old;
@@ -21,16 +45,16 @@ static inline void netdata_thread_disable_cancelability(void) {
45
46
static inline void netdata_thread_enable_cancelability(void) {
47
if(netdata_thread_lock_cancelability < 1) {
24
- error("THREAD_CANCELABILITY: netdata_thread_enable_cancelability(): oops! invalid thread cancelability count %d - results will be undefined - please report this!", netdata_thread_lock_cancelability);
48
+ error("THREAD_CANCELABILITY: netdata_thread_enable_cancelability(): invalid thread cancelability count %d on thread %s - results will be undefined - please report this!", netdata_thread_lock_cancelability, netdata_thread_tag());
49
}
50
else if(netdata_thread_lock_cancelability == 1) {
51
int old = 1;
52
int ret = pthread_setcancelstate(netdata_thread_first_cancelability, &old);
53
if(ret != 0)
30
- error("THREAD_CANCELABILITY: pthread_setcancelstate() returned error %d", ret);
54
+ error("THREAD_CANCELABILITY: pthread_setcancelstate() on thread %s returned error %d", netdata_thread_tag(), ret);
55
else {
56
if(!old)
33
- error("THREAD_CANCELABILITY: netdata_thread_enable_cancelability(): oops! old thread cancelability was changed, expected ENABLED, found DISABLED - please report this!");
57
+ error("THREAD_CANCELABILITY: netdata_thread_enable_cancelability(): old thread cancelability on thread %s was changed, expected ENABLED, found DISABLED - please report this!", netdata_thread_tag());
58
}
59
60
netdata_thread_lock_cancelability = 0;
src/locks.h
+4
@@ -7,6 +7,10 @@ typedef pthread_mutex_t netdata_mutex_t;
7
typedef pthread_rwlock_t netdata_rwlock_t;
8
#define NETDATA_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
9
10
+extern void netdata_thread_welcome(char *tag);
11
+extern void netdata_thread_welcome_nolog(char *tag);
12
+extern const char *netdata_thread_tag(void);
13
+
14
extern int __netdata_mutex_init(netdata_mutex_t *mutex);
15
extern int __netdata_mutex_lock(netdata_mutex_t *mutex);
16
extern int __netdata_mutex_trylock(netdata_mutex_t *mutex);
src/main.c
+13
-13
@@ -6,28 +6,28 @@ void netdata_cleanup_and_exit(int ret) {
6
netdata_exit = 1;
7
8
error_log_limit_unlimited();
9
- info("MAIN: netdata prepares to exit...");
9
+ info("EXIT: netdata prepares to exit...");
10
11
// stop everything
12
- info("MAIN: stopping all threads and child processes...");
12
+ info("EXIT: stopping master threads...");
13
cancel_main_threads();
14
15
// cleanup the database (delete files not needed)
16
- info("MAIN: cleaning up the database...");
16
+ info("EXIT: cleaning up the database...");
17
rrdhost_cleanup_all();
18
19
// free the database
20
- info("MAIN: freeing database memory...");
20
+ info("EXIT: freeing database memory...");
21
rrdhost_free_all();
22
23
// unlink the pid
24
if(pidfile[0]) {
25
- info("MAIN: removing netdata PID file '%s'...", pidfile);
25
+ info("EXIT: removing netdata PID file '%s'...", pidfile);
26
if(unlink(pidfile) != 0)
27
- error("MAIN: cannot unlink pidfile '%s'.", pidfile);
27
+ error("EXIT: cannot unlink pidfile '%s'.", pidfile);
28
}
29
30
- info("MAIN: all done - netdata is now exiting - bye bye...");
30
+ info("EXIT: all done - netdata is now exiting - bye bye...");
31
exit(ret);
32
}
33
@@ -183,12 +183,12 @@ void cancel_main_threads() {
183
int i;
184
for (i = 0; static_threads[i].name != NULL ; i++) {
185
if(static_threads[i].enabled) {
186
- info("MAIN: Calling pthread_cancel() on %s thread", static_threads[i].name);
186
+ info("EXIT: Stopping master thread: %s", static_threads[i].name);
187
int ret;
188
if((ret = pthread_cancel(*static_threads[i].thread)) != 0)
189
- error("MAIN: pthread_cancel() failed with code %d.", ret);
190
- else
191
- info("MAIN: thread %s cancelled", static_threads[i].name);
189
+ error("EXIT: pthread_cancel() failed with code %d.", ret);
190
+ //else
191
+ // info("MAIN: thread %s cancelled", static_threads[i].name);
192
193
static_threads[i].enabled = 0;
194
}
@@ -196,10 +196,10 @@ void cancel_main_threads() {
196
197
// if, for any reason there is any child exited
198
// catch it here
199
- info("MAIN: waiting for any unfinished child processes");
199
+ info("EXIT: waiting for any unfinished child processes");
200
siginfo_t info;
201
waitid(P_PID, 0, &info, WEXITED|WNOHANG);
202
- info("MAIN: all threads/childs stopped.");
202
+ info("EXIT: all threads/childs stopped.");
203
}
204
205
struct option_def option_definitions[] = {
src/plugin_checks.c
+11
-11
@@ -2,16 +2,18 @@
2
3
#ifdef NETDATA_INTERNAL_CHECKS
4
5
-void *checks_main(void *ptr) {
5
+static void checks_main_cleanup(void *ptr) {
6
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
7
+ if(static_thread->enabled) {
8
+ static_thread->enabled = 0;
9
8
- info("CHECKS thread created with task id %d", gettid());
9
-
10
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
11
- error("Cannot set pthread cancel type to DEFERRED.");
10
+ info("%s: cleaning up...", netdata_thread_tag());
11
+ }
12
+}
13
13
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
14
- error("Cannot set pthread cancel state to ENABLE.");
14
+void *checks_main(void *ptr) {
15
+ netdata_thread_welcome("CHECKS");
16
+ pthread_cleanup_push(checks_main_cleanup, ptr);
17
18
usec_t usec = 0, susec = localhost->rrd_update_every * USEC_PER_SEC, loop_usec = 0, total_susec = 0;
19
struct timeval now, last, loop;
@@ -72,7 +74,7 @@ void *checks_main(void *ptr) {
74
rrddim_add(check3, "apps.plugin", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
75
76
now_realtime_timeval(&last);
75
- while(1) {
77
+ while(!netdata_exit) {
78
usleep(susec);
79
80
// find the time to sleep in order to wait exactly update_every seconds
@@ -119,9 +121,7 @@ void *checks_main(void *ptr) {
121
rrdset_done(check3);
122
}
123
122
- info("CHECKS thread exiting");
123
-
124
- static_thread->enabled = 0;
124
+ pthread_cleanup_pop(1);
125
pthread_exit(NULL);
126
return NULL;
127
}
src/plugin_freebsd.c
+11
-11
@@ -66,16 +66,18 @@ static struct freebsd_module {
66
{ .name = NULL, .dim = NULL, .enabled = 0, .func = NULL }
67
};
68
69
-void *freebsd_main(void *ptr) {
69
+static void freebsd_main_cleanup(void *ptr) {
70
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
71
+ if(static_thread->enabled) {
72
+ static_thread->enabled = 0;
73
72
- info("FREEBSD Plugin thread created with task id %d", gettid());
73
-
74
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
75
- error("Cannot set pthread cancel type to DEFERRED.");
74
+ info("%s: cleaning up...", netdata_thread_tag());
75
+ }
76
+}
77
77
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
78
- error("Cannot set pthread cancel state to ENABLE.");
78
+void *freebsd_main(void *ptr) {
79
+ netdata_thread_welcome("FREEBSD");
80
+ pthread_cleanup_push(freebsd_main_cleanup, ptr);
81
82
int vdo_cpu_netdata = config_get_boolean("plugin:freebsd", "netdata server resources", 1);
83
@@ -97,7 +99,7 @@ void *freebsd_main(void *ptr) {
99
heartbeat_t hb;
100
heartbeat_init(&hb);
101
100
- for(;;) {
102
+ while(!netdata_exit) {
103
usec_t hb_dt = heartbeat_next(&hb, step);
104
usec_t duration = 0ULL;
105
@@ -167,9 +169,7 @@ void *freebsd_main(void *ptr) {
169
}
170
}
171
170
- info("FREEBSD thread exiting");
171
-
172
- static_thread->enabled = 0;
172
+ pthread_cleanup_pop(1);
173
pthread_exit(NULL);
174
return NULL;
175
}
src/plugin_idlejitter.c
+11
-10
@@ -2,16 +2,18 @@
2
3
#define CPU_IDLEJITTER_SLEEP_TIME_MS 20
4
5
-void *cpuidlejitter_main(void *ptr) {
5
+static void cpuidlejitter_main_cleanup(void *ptr) {
6
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
7
+ if(static_thread->enabled) {
8
+ static_thread->enabled = 0;
9
8
- info("IDLEJITTER thread created with task id %d", gettid());
9
-
10
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
11
- error("Cannot set pthread cancel type to DEFERRED.");
10
+ info("%s: cleaning up...", netdata_thread_tag());
11
+ }
12
+}
13
13
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
14
- error("Cannot set pthread cancel state to ENABLE.");
14
+void *cpuidlejitter_main(void *ptr) {
15
+ netdata_thread_welcome("IDLEJITTER");
16
+ pthread_cleanup_push(cpuidlejitter_main_cleanup, ptr);
17
18
usec_t sleep_ut = config_get_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS) * USEC_PER_MS;
19
if(sleep_ut <= 0) {
@@ -40,6 +42,7 @@ void *cpuidlejitter_main(void *ptr) {
42
usec_t update_every_ut = localhost->rrd_update_every * USEC_PER_SEC;
43
struct timeval before, after;
44
unsigned long long counter;
45
+
46
for(counter = 0; 1 ;counter++) {
47
int iterations = 0;
48
usec_t error_total = 0,
@@ -82,9 +85,7 @@ void *cpuidlejitter_main(void *ptr) {
85
}
86
}
87
85
- info("IDLEJITTER thread exiting");
86
-
87
- static_thread->enabled = 0;
88
+ pthread_cleanup_pop(1);
89
pthread_exit(NULL);
90
return NULL;
91
}
src/plugin_macos.c
+12
-11
@@ -1,15 +1,17 @@
1
#include "common.h"
2
3
-void *macos_main(void *ptr) {
3
+static void macos_main_cleanup(void *ptr) {
4
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
5
+ if(static_thread->enabled) {
6
+ static_thread->enabled = 0;
7
6
- info("MACOS Plugin thread created with task id %d", gettid());
7
-
8
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
9
- error("Cannot set pthread cancel type to DEFERRED.");
8
+ info("%s: cleaning up...", netdata_thread_tag());
9
+ }
10
+}
11
11
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
12
- error("Cannot set pthread cancel state to ENABLE.");
12
+void *macos_main(void *ptr) {
13
+ netdata_thread_welcome("MACOS");
14
+ pthread_cleanup_push(macos_main_cleanup, ptr);
15
16
// when ZERO, attempt to do it
17
int vdo_cpu_netdata = !config_get_boolean("plugin:macos", "netdata server resources", 1);
@@ -25,7 +27,8 @@ void *macos_main(void *ptr) {
27
usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
28
heartbeat_t hb;
29
heartbeat_init(&hb);
28
- for(;;) {
30
+
31
+ while(!netdata_exit) {
32
usec_t hb_dt = heartbeat_next(&hb, step);
33
34
if(unlikely(netdata_exit)) break;
@@ -60,9 +63,7 @@ void *macos_main(void *ptr) {
63
}
64
}
65
63
- info("MACOS thread exiting");
64
-
65
- static_thread->enabled = 0;
66
+ pthread_cleanup_pop(1);
67
pthread_exit(NULL);
68
return NULL;
69
}
src/plugin_proc.c
+11
-11
@@ -64,16 +64,18 @@ static struct proc_module {
64
{ .name = NULL, .dim = NULL, .func = NULL }
65
};
66
67
-void *proc_main(void *ptr) {
67
+static void proc_main_cleanup(void *ptr) {
68
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
69
+ if(static_thread->enabled) {
70
+ static_thread->enabled = 0;
71
70
- info("PROC Plugin thread created with task id %d", gettid());
71
-
72
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
73
- error("Cannot set pthread cancel type to DEFERRED.");
72
+ info("%s: cleaning up...", netdata_thread_tag());
73
+ }
74
+}
75
75
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
76
- error("Cannot set pthread cancel state to ENABLE.");
76
+void *proc_main(void *ptr) {
77
+ netdata_thread_welcome("PROC");
78
+ pthread_cleanup_push(proc_main_cleanup, ptr);
79
80
int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
81
@@ -91,7 +93,7 @@ void *proc_main(void *ptr) {
93
heartbeat_t hb;
94
heartbeat_init(&hb);
95
94
- for(;;) {
96
+ while(!netdata_exit) {
97
usec_t hb_dt = heartbeat_next(&hb, step);
98
usec_t duration = 0ULL;
99
@@ -161,9 +163,7 @@ void *proc_main(void *ptr) {
163
}
164
}
165
164
- info("PROC thread exiting");
165
-
166
- static_thread->enabled = 0;
166
+ pthread_cleanup_pop(1);
167
pthread_exit(NULL);
168
return NULL;
169
}
src/plugin_proc_diskspace.c
+11
-11
@@ -328,16 +328,18 @@ static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
328
m->collected++;
329
}
330
331
-void *proc_diskspace_main(void *ptr) {
331
+static void diskspace_main_cleanup(void *ptr) {
332
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
333
+ if(static_thread->enabled) {
334
+ static_thread->enabled = 0;
335
334
- info("DISKSPACE thread created with task id %d", gettid());
335
-
336
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
337
- error("DISKSPACE: Cannot set pthread cancel type to DEFERRED.");
336
+ info("%s: cleaning up...", netdata_thread_tag());
337
+ }
338
+}
339
339
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
340
- error("DISKSPACE: Cannot set pthread cancel state to ENABLE.");
340
+void *proc_diskspace_main(void *ptr) {
341
+ netdata_thread_welcome("DISKSPACE");
342
+ pthread_cleanup_push(diskspace_main_cleanup, ptr);
343
344
int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
345
@@ -357,7 +359,7 @@ void *proc_diskspace_main(void *ptr) {
359
usec_t step = update_every * USEC_PER_SEC;
360
heartbeat_t hb;
361
heartbeat_init(&hb);
360
- for(;;) {
362
+ while(!netdata_exit) {
363
duration = heartbeat_dt_usec(&hb);
364
/* usec_t hb_dt = */ heartbeat_next(&hb, step);
365
@@ -454,9 +456,7 @@ void *proc_diskspace_main(void *ptr) {
456
}
457
}
458
457
- info("DISKSPACE thread exiting");
458
-
459
- static_thread->enabled = 0;
459
+ pthread_cleanup_pop(1);
460
pthread_exit(NULL);
461
return NULL;
462
}
src/plugin_tc.c
+4
-19
@@ -828,14 +828,14 @@ static inline void tc_split_words(char *str, char **words, int max_words) {
828
while(i < max_words) words[i++] = NULL;
829
}
830
831
-volatile pid_t tc_child_pid = 0;
831
+static pid_t tc_child_pid = 0;
832
833
static void tc_main_cleanup(void *ptr) {
834
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
835
if(static_thread->enabled) {
836
static_thread->enabled = 0;
837
838
- info("TC: cleaning up...");
838
+ info("%s: cleaning up...", netdata_thread_tag());
839
840
if(tc_child_pid) {
841
info("TC: killing with SIGTERM tc-qos-helper process %d", tc_child_pid);
@@ -853,13 +853,8 @@ static void tc_main_cleanup(void *ptr) {
853
}
854
855
void *tc_main(void *ptr) {
856
- info("TC thread created with task id %d", gettid());
857
-
858
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
859
- error("Cannot set pthread cancel type to DEFERRED.");
860
-
861
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
862
- error("Cannot set pthread cancel state to ENABLE.");
856
+ netdata_thread_welcome("TC");
857
+ pthread_cleanup_push(tc_main_cleanup, ptr);
858
859
struct rusage thread;
860
@@ -885,8 +880,6 @@ void *tc_main(void *ptr) {
880
snprintfz(buffer, TC_LINE_MAX, "%s/tc-qos-helper.sh", netdata_configured_plugins_dir);
881
char *tc_script = config_get("plugin:tc", "script to run to get tc values", buffer);
882
888
- pthread_cleanup_push(tc_main_cleanup, ptr);
889
-
883
while(!netdata_exit) {
884
FILE *fp;
885
struct tc_device *device = NULL;
@@ -986,14 +979,8 @@ void *tc_main(void *ptr) {
979
// debug(D_TC_LOOP, "END line");
980
981
if(likely(device)) {
989
- if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
990
- error("Cannot set pthread cancel state to DISABLE.");
991
-
982
tc_device_commit(device);
983
// tc_device_free(device);
994
-
995
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
996
- error("Cannot set pthread cancel state to ENABLE.");
984
}
985
986
device = NULL;
@@ -1171,9 +1158,7 @@ void *tc_main(void *ptr) {
1158
}
1159
1160
cleanup:
1174
- info("TC thread exiting");
1161
pthread_cleanup_pop(1);
1176
-
1162
pthread_exit(NULL);
1163
return NULL;
1164
}
src/plugin_tc.h
-1
@@ -1,7 +1,6 @@
1
#ifndef NETDATA_PLUGIN_TC_H
2
#define NETDATA_PLUGIN_TC_H 1
3
4
-extern volatile pid_t tc_child_pid;
4
extern void *tc_main(void *ptr);
5
6
#endif /* NETDATA_PLUGIN_TC_H */
src/plugins_d.c
+12
-18
@@ -479,11 +479,11 @@ cleanup:
479
static void pluginsd_worker_thread_cleanup(void *arg) {
480
struct plugind *cd = (struct plugind *)arg;
481
482
- info("PLUGINSD: '%s' thread exiting", cd->fullfilename);
483
-
482
if(cd->enabled && !cd->obsolete) {
483
cd->obsolete = 1;
484
485
+ info("PLUGINSD: '%s' thread exiting", cd->fullfilename);
486
+
487
if (cd->pid) {
488
siginfo_t info;
489
info("PLUGINSD: killing %s plugin child process pid %d", cd->id, cd->pid);
@@ -498,13 +498,14 @@ static void pluginsd_worker_thread_cleanup(void *arg) {
498
}
499
500
void *pluginsd_worker_thread(void *arg) {
501
+ netdata_thread_welcome_nolog("PLUGINSD_WORKER");
502
+ pthread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
503
+
504
struct plugind *cd = (struct plugind *)arg;
502
- cd->obsolete = 0;
505
506
+ cd->obsolete = 0;
507
size_t count = 0;
508
506
- pthread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
507
-
509
while(!netdata_exit) {
510
FILE *fp = mypopen(cd->cmd, &cd->pid);
511
if(unlikely(!fp)) {
@@ -578,30 +579,25 @@ static void pluginsd_main_cleanup(void *data) {
579
if(static_thread->enabled) {
580
static_thread->enabled = 0;
581
581
- info("PLUGINSD: cleaning up plugin threads...");
582
- struct plugind *cd;
582
+ info("%s: cleaning up...", netdata_thread_tag());
583
584
+ struct plugind *cd;
585
for (cd = pluginsd_root; cd; cd = cd->next) {
586
if (cd->enabled && !cd->obsolete) {
586
- info("PLUGINSD: Calling pthread_cancel() on %s plugin thread", cd->id);
587
+ info("PLUGINSD: Stopping plugin thread: %s", cd->id);
588
int ret;
589
if ((ret = pthread_cancel(cd->thread)) != 0)
590
error("PLUGINSD: pthread_cancel() failed with code %d.", ret);
591
}
592
}
593
593
- info("PLUGINSD: cleanup completed.");
594
+ info("%s: cleanup completed.", netdata_thread_tag());
595
}
596
}
597
598
void *pluginsd_main(void *ptr) {
598
- info("PLUGINS.D thread created with task id %d", gettid());
599
-
600
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
601
- error("Cannot set pthread cancel type to DEFERRED.");
602
-
603
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
604
- error("Cannot set pthread cancel state to ENABLE.");
599
+ netdata_thread_welcome("PLUGINSD");
600
+ pthread_cleanup_push(pluginsd_main_cleanup, ptr);
601
602
int automatic_run = config_get_boolean(CONFIG_SECTION_PLUGINS, "enable running new plugins", 1);
603
int scan_frequency = (int) config_get_number(CONFIG_SECTION_PLUGINS, "check for new plugins every", 60);
@@ -611,8 +607,6 @@ void *pluginsd_main(void *ptr) {
607
// so that we don't log broken directories on each loop
608
int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
609
614
- pthread_cleanup_push(pluginsd_main_cleanup, ptr);
615
-
610
while(!netdata_exit) {
611
int idx;
612
const char *directory_name;
src/rrdpush.c
+43
-36
@@ -449,6 +449,8 @@ static void rrdpush_sender_thread_cleanup_callback(void *ptr) {
449
}
450
451
void *rrdpush_sender_thread(void *ptr) {
452
+ netdata_thread_welcome_nolog("STREAM_SEND");
453
+
454
RRDHOST *host = (RRDHOST *)ptr;
455
456
if(!host->rrdpush_send_enabled || !host->rrdpush_send_destination || !*host->rrdpush_send_destination || !host->rrdpush_send_api_key || !*host->rrdpush_send_api_key) {
@@ -459,12 +461,6 @@ void *rrdpush_sender_thread(void *ptr) {
461
462
info("STREAM %s [send]: thread created (task id %d)", host->hostname, gettid());
463
462
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
463
- error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
464
-
465
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
466
- error("STREAM %s [send]: cannot set pthread cancel type to DEFERRED.", host->hostname);
467
-
464
int timeout = (int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "timeout seconds", 60);
465
int default_port = (int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "default port", 19999);
466
size_t max_size = (size_t)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "buffer size bytes", 1024 * 1024);
@@ -596,9 +592,6 @@ void *rrdpush_sender_thread(void *ptr) {
592
// but the socket is in non-blocking mode
593
// so, we will not block at send()
594
599
- if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
600
- error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
601
-
595
debug(D_STREAM, "STREAM: Getting exclusive lock on host...");
596
rrdpush_buffer_lock(host);
597
@@ -648,9 +641,6 @@ void *rrdpush_sender_thread(void *ptr) {
641
debug(D_STREAM, "STREAM: Releasing exclusive lock on host...");
642
rrdpush_buffer_unlock(host);
643
651
- if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
652
- error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
653
-
644
// END RRDPUSH LOCKED SESSION
645
}
646
else {
@@ -691,7 +681,6 @@ void *rrdpush_sender_thread(void *ptr) {
681
}
682
683
pthread_cleanup_pop(1);
694
-
684
pthread_exit(NULL);
685
return NULL;
686
}
@@ -881,31 +870,49 @@ struct rrdpush_thread {
870
int update_every;
871
};
872
884
-static void *rrdpush_receiver_thread(void *ptr) {
885
- struct rrdpush_thread *rpt = (struct rrdpush_thread *)ptr;
886
-
887
- if (pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
888
- error("STREAM %s [receive]: cannot set pthread cancel type to DEFERRED.", rpt->hostname);
889
-
890
- if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
891
- error("STREAM %s [receive]: cannot set pthread cancel state to ENABLE.", rpt->hostname);
892
-
893
-
894
- info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
895
- rrdpush_receive(rpt->fd, rpt->key, rpt->hostname, rpt->registry_hostname, rpt->machine_guid, rpt->os, rpt->timezone, rpt->tags, rpt->update_every, rpt->client_ip, rpt->client_port);
896
- info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
873
+static void rrdpush_receiver_thread_cleanup(void *ptr) {
874
+ static __thread int executed = 0;
875
+ if(!executed) {
876
+ executed = 1;
877
+ struct rrdpush_thread *rpt = (struct rrdpush_thread *) ptr;
878
+
879
+ info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
880
+
881
+ freez(rpt->key);
882
+ freez(rpt->hostname);
883
+ freez(rpt->registry_hostname);
884
+ freez(rpt->machine_guid);
885
+ freez(rpt->os);
886
+ freez(rpt->timezone);
887
+ freez(rpt->tags);
888
+ freez(rpt->client_ip);
889
+ freez(rpt->client_port);
890
+ freez(rpt);
891
+ }
892
+}
893
898
- freez(rpt->key);
899
- freez(rpt->hostname);
900
- freez(rpt->registry_hostname);
901
- freez(rpt->machine_guid);
902
- freez(rpt->os);
903
- freez(rpt->timezone);
904
- freez(rpt->tags);
905
- freez(rpt->client_ip);
906
- freez(rpt->client_port);
907
- freez(rpt);
894
+static void *rrdpush_receiver_thread(void *ptr) {
895
+ netdata_thread_welcome_nolog("STREAM_RECEIVE");
896
+ pthread_cleanup_push(rrdpush_receiver_thread_cleanup, ptr);
897
+
898
+ struct rrdpush_thread *rpt = (struct rrdpush_thread *)ptr;
899
+ info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
900
+
901
+ rrdpush_receive(
902
+ rpt->fd
903
+ , rpt->key
904
+ , rpt->hostname
905
+ , rpt->registry_hostname
906
+ , rpt->machine_guid
907
+ , rpt->os
908
+ , rpt->timezone
909
+ , rpt->tags
910
+ , rpt->update_every
911
+ , rpt->client_ip
912
+ , rpt->client_port
913
+ );
914
915
+ pthread_cleanup_pop(1);
916
pthread_exit(NULL);
917
return NULL;
918
}
src/rrdset.c
-9
@@ -1087,9 +1087,6 @@ void rrdset_done(RRDSET *st) {
1087
1088
RRDDIM *rd;
1089
1090
- int
1091
- pthreadoldcancelstate; // store the old cancelable pthread state, to restore it at the end
1092
-
1090
char
1091
store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
1092
first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
@@ -1101,9 +1098,6 @@ void rrdset_done(RRDSET *st) {
1098
next_store_ut, // the timestamp in microseconds, of the next entry to store in the db
1099
update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
1100
1104
- if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
1105
- error("Cannot set pthread cancel state to DISABLE.");
1106
-
1101
// a read lock is OK here
1102
rrdset_rdlock(st);
1103
@@ -1546,7 +1540,4 @@ void rrdset_done(RRDSET *st) {
1540
*/
1541
1542
rrdset_unlock(st);
1549
-
1550
- if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
1551
- error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
1543
}
src/statsd.c
+19
-28
@@ -876,31 +876,32 @@ static int statsd_snd_callback(int fd, int socktype, void *data, short int *even
876
// statsd child thread to collect metrics from network
877
878
void statsd_collector_thread_cleanup(void *data) {
879
- struct statsd_udp *d = data;
879
+ static __thread int executed = 0;
880
+ if(!executed) {
881
+ executed = 1;
882
+ info("%s: cleaning up...", netdata_thread_tag());
883
+
884
+ struct statsd_udp *d = data;
885
886
#ifdef HAVE_RECVMMSG
882
- size_t i;
883
- for (i = 0; i < d->size; i++)
884
- freez(d->iovecs[i].iov_base);
887
+ size_t i;
888
+ for (i = 0; i < d->size; i++)
889
+ freez(d->iovecs[i].iov_base);
890
886
- freez(d->iovecs);
887
- freez(d->msgs);
891
+ freez(d->iovecs);
892
+ freez(d->msgs);
893
#endif
894
890
- freez(d);
895
+ freez(d);
896
+ }
897
}
898
899
void *statsd_collector_thread(void *ptr) {
894
- int id = *((int *)ptr);
900
+ netdata_thread_welcome_nolog("STATSD_COLLECTOR");
901
902
+ int id = *((int *)ptr);
903
info("STATSD collector thread No %d created with task id %d", id + 1, gettid());
904
898
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
899
- error("Cannot set pthread cancel type to DEFERRED.");
900
-
901
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
902
- error("Cannot set pthread cancel state to ENABLE.");
903
-
905
struct statsd_udp *d = callocz(sizeof(struct statsd_udp), 1);
906
pthread_cleanup_push(statsd_collector_thread_cleanup, d);
907
@@ -929,8 +930,6 @@ void *statsd_collector_thread(void *ptr) {
930
);
931
932
pthread_cleanup_pop(1);
932
-
933
- debug(D_WEB_CLIENT, "STATSD: exit!");
933
pthread_exit(NULL);
934
return NULL;
935
}
@@ -1977,12 +1976,12 @@ static void statsd_main_cleanup(void *data) {
1976
if(static_thread->enabled) {
1977
static_thread->enabled = 0;
1978
1980
- info("STATSD: cleaning up...");
1979
+ info("%s: cleaning up...", netdata_thread_tag());
1980
1981
if (statsd.collection_threads) {
1982
int i;
1983
for (i = 0; i < statsd.threads; i++) {
1985
- info("STATSD: stopping data collection thread %d...", i);
1984
+ info("STATSD: stopping data collection thread %d...", i + 1);
1985
pthread_cancel(statsd.collection_threads[i]);
1986
}
1987
}
@@ -1995,13 +1994,8 @@ static void statsd_main_cleanup(void *data) {
1994
}
1995
1996
void *statsd_main(void *ptr) {
1998
- info("STATSD main thread created with task id %d", gettid());
1999
-
2000
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2001
- error("Cannot set pthread cancel type to DEFERRED.");
2002
-
2003
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2004
- error("Cannot set pthread cancel state to ENABLE.");
1997
+ netdata_thread_welcome("STATSD");
1998
+ pthread_cleanup_push(statsd_main_cleanup, ptr);
1999
2000
// ----------------------------------------------------------------------------------------------------------------
2001
// statsd configuration
@@ -2103,8 +2097,6 @@ void *statsd_main(void *ptr) {
2097
error("STATSD: cannot request detach of child thread.");
2098
}
2099
2106
- pthread_cleanup_push(statsd_main_cleanup, ptr);
2107
-
2100
// ----------------------------------------------------------------------------------------------------------------
2101
// statsd monitoring charts
2102
@@ -2295,7 +2287,6 @@ void *statsd_main(void *ptr) {
2287
}
2288
2289
pthread_cleanup_pop(1);
2298
-
2290
pthread_exit(NULL);
2291
return NULL;
2292
}
src/sys_fs_cgroup.c
+12
-11
@@ -2674,16 +2674,18 @@ void update_cgroup_charts(int update_every) {
2674
// ----------------------------------------------------------------------------
2675
// cgroups main
2676
2677
-void *cgroups_main(void *ptr) {
2677
+static void cgroup_main_cleanup(void *ptr) {
2678
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
2679
+ if(static_thread->enabled) {
2680
+ static_thread->enabled = 0;
2681
2680
- info("CGROUP plugin thread created with task id %d", gettid());
2681
-
2682
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2683
- error("CGROUP: cannot set pthread cancel type to DEFERRED.");
2682
+ info("%s: cleaning up...", netdata_thread_tag());
2683
+ }
2684
+}
2685
2685
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2686
- error("CGROUP: cannot set pthread cancel state to ENABLE.");
2686
+void *cgroups_main(void *ptr) {
2687
+ netdata_thread_welcome("CGROUP");
2688
+ pthread_cleanup_push(cgroup_main_cleanup, ptr);
2689
2690
struct rusage thread;
2691
@@ -2698,7 +2700,8 @@ void *cgroups_main(void *ptr) {
2700
heartbeat_init(&hb);
2701
usec_t step = cgroup_update_every * USEC_PER_SEC;
2702
usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_dt = 0;
2701
- for(;;) {
2703
+
2704
+ while(!netdata_exit) {
2705
usec_t hb_dt = heartbeat_next(&hb, step);
2706
if(unlikely(netdata_exit)) break;
2707
@@ -2750,9 +2753,7 @@ void *cgroups_main(void *ptr) {
2753
}
2754
}
2755
2753
- info("CGROUP thread exiting");
2754
-
2755
- static_thread->enabled = 0;
2756
+ pthread_cleanup_pop(1);
2757
pthread_exit(NULL);
2758
return NULL;
2759
}
src/web_client.c
+11
-11
@@ -1758,22 +1758,23 @@ ssize_t web_client_receive(struct web_client *w)
1758
// 3. it generates HTTP responses
1759
// 4. it copies data from input to output if mode is FILECOPY
1760
1761
-void *web_client_main(void *ptr)
1762
-{
1763
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
1764
- error("Cannot set pthread cancel type to DEFERRED.");
1761
+static void web_client_main_cleanup(void *ptr) {
1762
+ struct web_client *w = ptr;
1763
+ if(!web_client_check_obsolete(w)) {
1764
+ WEB_CLIENT_IS_OBSOLETE(w);
1765
+ }
1766
+}
1767
1766
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
1767
- error("Cannot set pthread cancel state to ENABLE.");
1768
+void *web_client_main(void *ptr) {
1769
+ netdata_thread_welcome_nolog("WEB_CLIENT");
1770
+ pthread_cleanup_push(web_client_main_cleanup, ptr);
1771
1772
struct web_client *w = ptr;
1773
struct pollfd fds[2], *ifd, *ofd;
1774
int retval, timeout;
1775
nfds_t fdmax = 0;
1776
1774
- for(;;) {
1775
- if(unlikely(netdata_exit)) break;
1776
-
1777
+ while(!netdata_exit) {
1778
if(unlikely(web_client_check_dead(w))) {
1779
debug(D_WEB_CLIENT, "%llu: client is dead.", w->id);
1780
break;
@@ -1897,8 +1898,7 @@ void *web_client_main(void *ptr)
1898
w->ifd = -1;
1899
w->ofd = -1;
1900
1900
- WEB_CLIENT_IS_OBSOLETE(w);
1901
-
1901
+ pthread_cleanup_pop(1);
1902
pthread_exit(NULL);
1903
return NULL;
1904
}
src/web_server.c
+8
-24
@@ -116,7 +116,7 @@ static void socket_listen_main_multi_threaded_cleanup(void *data) {
116
if(static_thread->enabled) {
117
static_thread->enabled = 0;
118
119
- info("LISTENER: multi-threaded web server cleaning up...");
119
+ info("%s: cleaning up...", netdata_thread_tag());
120
121
info("LISTENER: releasing allocated memory...");
122
freez(socket_listen_main_multi_threaded_fds);
@@ -132,7 +132,7 @@ static void socket_listen_main_multi_threaded_cleanup(void *data) {
132
if(!web_client_check_obsolete(w)) {
133
WEB_CLIENT_IS_OBSOLETE(w);
134
135
- info("LISTENER: Calling pthread_cancel() on web client %s, id %llu", w->client_ip, w->id);
135
+ info("LISTENER: Stopping web client %s, id %llu", w->client_ip, w->id);
136
int ret;
137
if ((ret = pthread_cancel(w->thread)) != 0)
138
error("LISTENER: pthread_cancel() failed with code %d, id %llu.", ret, w->id);
@@ -143,23 +143,17 @@ static void socket_listen_main_multi_threaded_cleanup(void *data) {
143
}
144
145
void *socket_listen_main_multi_threaded(void *ptr) {
146
+ netdata_thread_welcome("WEBSERVER_MULTITHREADED");
147
+ pthread_cleanup_push(socket_listen_main_multi_threaded_cleanup, ptr);
148
+
149
web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
147
- info("Multi-threaded WEB SERVER thread created with task id %d", gettid());
150
151
struct web_client *w;
152
int retval, counter = 0;
153
152
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
153
- error("Cannot set pthread cancel type to DEFERRED.");
154
-
155
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
156
- error("Cannot set pthread cancel state to ENABLE.");
157
-
154
if(!api_sockets.opened)
155
fatal("LISTENER: No sockets to listen to.");
156
161
- pthread_cleanup_push(socket_listen_main_multi_threaded_cleanup, ptr);
162
-
157
socket_listen_main_multi_threaded_fds = callocz(sizeof(struct pollfd), api_sockets.opened);
158
159
size_t i;
@@ -227,7 +221,6 @@ void *socket_listen_main_multi_threaded(void *ptr) {
221
}
222
223
pthread_cleanup_pop(1);
230
-
224
pthread_exit(NULL);
225
return NULL;
226
}
@@ -281,7 +274,7 @@ static void socket_listen_main_single_threaded_cleanup(void *data) {
274
if(static_thread->enabled) {
275
static_thread->enabled = 0;
276
284
- info("LISTENER: single-threaded web server cleaning up...");
277
+ info("%s: cleaning up...", netdata_thread_tag());
278
279
info("LISTENER: closing all sockets...");
280
listen_sockets_close(&api_sockets);
@@ -292,19 +285,13 @@ static void socket_listen_main_single_threaded_cleanup(void *data) {
285
}
286
287
void *socket_listen_main_single_threaded(void *ptr) {
288
+ netdata_thread_welcome("WEBSERVER_SINGLETHREADED");
289
+ pthread_cleanup_push(socket_listen_main_single_threaded_cleanup, ptr);
290
web_server_mode = WEB_SERVER_MODE_SINGLE_THREADED;
291
297
- info("Single-threaded WEB SERVER thread created with task id %d", gettid());
298
-
292
struct web_client *w;
293
int retval;
294
302
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
303
- error("Cannot set pthread cancel type to DEFERRED.");
304
-
305
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
306
- error("Cannot set pthread cancel state to ENABLE.");
307
-
295
if(!api_sockets.opened)
296
fatal("LISTENER: no listen sockets available.");
297
@@ -318,8 +305,6 @@ void *socket_listen_main_single_threaded(void *ptr) {
305
FD_ZERO (&efds);
306
int fdmax = 0;
307
321
- pthread_cleanup_push(socket_listen_main_single_threaded_cleanup, ptr);
322
-
308
for(i = 0; i < api_sockets.opened ; i++) {
309
if (api_sockets.fds[i] < 0 || api_sockets.fds[i] >= (int)FD_SETSIZE)
310
fatal("LISTENER: Listen socket %d is not ready, or invalid.", api_sockets.fds[i]);
@@ -416,7 +401,6 @@ void *socket_listen_main_single_threaded(void *ptr) {
401
}
402
403
pthread_cleanup_pop(1);
419
-
404
pthread_exit(NULL);
405
return NULL;
406
}