enable cleanup of threads
Costa Tsaousis (ktsaou) committed
Jan 4, 2018 at 12:05 UTC
07c3d948d9804f4e8b34f10b3afe0afaf438a3de
15 files changed
+52
-32
src/backends.c
+1
-1
@@ -503,7 +503,7 @@ static void backends_main_cleanup(void *ptr) {
503
if(static_thread->enabled) {
504
static_thread->enabled = 0;
505
506
- info("%s: cleaning up...", netdata_thread_tag());
506
+ info("cleaning up...");
507
}
508
}
509
src/health.c
+1
-1
@@ -343,7 +343,7 @@ static void health_main_cleanup(void *ptr) {
343
if(static_thread->enabled) {
344
static_thread->enabled = 0;
345
346
- info("%s: cleaning up...", netdata_thread_tag());
346
+ info("cleaning up...");
347
}
348
}
349
src/main.c
+22
-2
@@ -191,14 +191,34 @@ int killpid(pid_t pid, int sig)
191
void cancel_main_threads() {
192
error_log_limit_unlimited();
193
194
- int i;
194
+ int i, found = 0, max = 1 * USEC_PER_SEC, step = 100000;
195
for (i = 0; static_threads[i].name != NULL ; i++) {
196
if(static_threads[i].enabled) {
197
info("EXIT: Stopping master thread: %s", static_threads[i].name);
198
netdata_thread_cancel(*static_threads[i].thread);
199
- static_threads[i].enabled = 0;
199
+ found++;
200
+ }
201
+ }
202
+
203
+ while(found && max > 0) {
204
+ max -= step;
205
+ info("Waiting %d threads to finish...", found);
206
+ sleep_usec(step);
207
+ found = 0;
208
+ for (i = 0; static_threads[i].name != NULL ; i++) {
209
+ if (static_threads[i].enabled)
210
+ found++;
211
}
212
}
213
+
214
+ if(found) {
215
+ for (i = 0; static_threads[i].name != NULL ; i++) {
216
+ if (static_threads[i].enabled)
217
+ error("Master thread %s takes too long to exit. Giving up...", static_threads[i].name);
218
+ }
219
+ }
220
+ else
221
+ info("All threads finished.");
222
}
223
224
struct option_def option_definitions[] = {
src/plugin_checks.c
+1
-1
@@ -7,7 +7,7 @@ static void checks_main_cleanup(void *ptr) {
7
if(static_thread->enabled) {
8
static_thread->enabled = 0;
9
10
- info("%s: cleaning up...", netdata_thread_tag());
10
+ info("cleaning up...");
11
}
12
}
13
src/plugin_freebsd.c
+1
-1
@@ -71,7 +71,7 @@ static void freebsd_main_cleanup(void *ptr) {
71
if(static_thread->enabled) {
72
static_thread->enabled = 0;
73
74
- info("%s: cleaning up...", netdata_thread_tag());
74
+ info("cleaning up...");
75
}
76
}
77
src/plugin_idlejitter.c
+1
-1
@@ -7,7 +7,7 @@ static void cpuidlejitter_main_cleanup(void *ptr) {
7
if(static_thread->enabled) {
8
static_thread->enabled = 0;
9
10
- info("%s: cleaning up...", netdata_thread_tag());
10
+ info("cleaning up...");
11
}
12
}
13
src/plugin_macos.c
+1
-1
@@ -5,7 +5,7 @@ static void macos_main_cleanup(void *ptr) {
5
if(static_thread->enabled) {
6
static_thread->enabled = 0;
7
8
- info("%s: cleaning up...", netdata_thread_tag());
8
+ info("cleaning up...");
9
}
10
}
11
src/plugin_nfacct.c
+1
-1
@@ -756,7 +756,7 @@ static void nfacct_main_cleanup(void *ptr) {
756
if(static_thread->enabled) {
757
static_thread->enabled = 0;
758
759
- info("%s: cleaning up...", netdata_thread_tag());
759
+ info("cleaning up...");
760
761
#ifdef DO_NFACCT
762
nfacct_cleanup();
src/plugin_proc.c
+1
-1
@@ -69,7 +69,7 @@ static void proc_main_cleanup(void *ptr) {
69
if(static_thread->enabled) {
70
static_thread->enabled = 0;
71
72
- info("%s: cleaning up...", netdata_thread_tag());
72
+ info("cleaning up...");
73
}
74
}
75
src/plugin_proc_diskspace.c
+1
-1
@@ -333,7 +333,7 @@ static void diskspace_main_cleanup(void *ptr) {
333
if(static_thread->enabled) {
334
static_thread->enabled = 0;
335
336
- info("%s: cleaning up...", netdata_thread_tag());
336
+ info("cleaning up...");
337
}
338
}
339
src/plugin_tc.c
+1
-1
@@ -835,7 +835,7 @@ static void tc_main_cleanup(void *ptr) {
835
if(static_thread->enabled) {
836
static_thread->enabled = 0;
837
838
- info("%s: cleaning up...", netdata_thread_tag());
838
+ info("cleaning up...");
839
840
if(tc_child_pid) {
841
info("TC: killing with SIGTERM tc-qos-helper process %d", tc_child_pid);
src/statsd.c
+2
-2
@@ -879,7 +879,7 @@ void statsd_collector_thread_cleanup(void *data) {
879
static __thread int executed = 0;
880
if(!executed) {
881
executed = 1;
882
- info("%s: cleaning up...", netdata_thread_tag());
882
+ info("cleaning up...");
883
884
struct statsd_udp *d = data;
885
@@ -1973,7 +1973,7 @@ static void statsd_main_cleanup(void *data) {
1973
if(static_thread->enabled) {
1974
static_thread->enabled = 0;
1975
1976
- info("%s: cleaning up...", netdata_thread_tag());
1976
+ info("cleaning up...");
1977
1978
if (statsd.collection_threads) {
1979
int i;
src/sys_fs_cgroup.c
+1
-1
@@ -2679,7 +2679,7 @@ static void cgroup_main_cleanup(void *ptr) {
2679
if(static_thread->enabled) {
2680
static_thread->enabled = 0;
2681
2682
- info("%s: cleaning up...", netdata_thread_tag());
2682
+ info("cleaning up...");
2683
}
2684
}
2685
src/threads.c
+9
-9
@@ -98,7 +98,7 @@ static void thread_cleanup(void *ptr) {
98
}
99
100
if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP))
101
- info("%s: thread with task id %d finished", netdata_thread_tag(), gettid());
101
+ info("thread with task id %d finished", gettid());
102
103
freez((void *)netdata_thread->tag);
104
netdata_thread->tag = NULL;
@@ -111,13 +111,13 @@ static void *thread_start(void *ptr) {
111
netdata_thread = (NETDATA_THREAD *)ptr;
112
113
if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_STARTUP))
114
- info("%s: thread created with task id %d", netdata_thread_tag(), gettid());
114
+ info("thread created with task id %d", gettid());
115
116
if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
117
- error("%s: cannot set pthread cancel type to DEFERRED.", netdata_thread_tag());
117
+ error("cannot set pthread cancel type to DEFERRED.");
118
119
if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
120
- error("%s: cannot set pthread cancel state to ENABLE.", netdata_thread_tag());
120
+ error("cannot set pthread cancel state to ENABLE.");
121
122
void *ret = NULL;
123
pthread_cleanup_push(thread_cleanup, ptr);
@@ -137,13 +137,13 @@ int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THR
137
138
int ret = pthread_create(thread, attr, thread_start, info);
139
if(ret != 0)
140
- error("%s: failed to create new thread for %s. pthread_create() failed with code %d", netdata_thread_tag(), tag, ret);
140
+ error("failed to create new thread for %s. pthread_create() failed with code %d", tag, ret);
141
142
else {
143
if (!(options & NETDATA_THREAD_OPTION_JOINABLE)) {
144
int ret2 = pthread_detach(*thread);
145
if (ret2 != 0)
146
- error("%s: cannot request detach of newly created %s thread. pthread_detach() failed with code %d", netdata_thread_tag(), tag, ret2);
146
+ error("cannot request detach of newly created %s thread. pthread_detach() failed with code %d", tag, ret2);
147
}
148
}
149
@@ -156,7 +156,7 @@ int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THR
156
int netdata_thread_cancel(netdata_thread_t thread) {
157
int ret = pthread_cancel(thread);
158
if(ret != 0)
159
- error("%s: cannot cancel thread. pthread_cancel() failed with code %d.", netdata_thread_tag(), ret);
159
+ error("cannot cancel thread. pthread_cancel() failed with code %d.", ret);
160
161
return ret;
162
}
@@ -167,7 +167,7 @@ int netdata_thread_cancel(netdata_thread_t thread) {
167
int netdata_thread_join(netdata_thread_t thread, void **retval) {
168
int ret = pthread_join(thread, retval);
169
if(ret != 0)
170
- error("%s: cannot join thread. pthread_join() failed with code %d.", netdata_thread_tag(), ret);
170
+ error("cannot join thread. pthread_join() failed with code %d.", ret);
171
172
return ret;
173
}
@@ -175,7 +175,7 @@ int netdata_thread_join(netdata_thread_t thread, void **retval) {
175
int netdata_thread_detach(pthread_t thread) {
176
int ret = pthread_detach(thread);
177
if(ret != 0)
178
- error("%s: cannot detach thread. pthread_detach() failed with code %d.", netdata_thread_tag(), ret);
178
+ error("cannot detach thread. pthread_detach() failed with code %d.", ret);
179
180
return ret;
181
}
src/web_server.c
+8
-8
@@ -114,22 +114,22 @@ static void socket_listen_main_multi_threaded_cleanup(void *data) {
114
if(static_thread->enabled) {
115
static_thread->enabled = 0;
116
117
- info("%s: cleaning up...", netdata_thread_tag());
117
+ info("cleaning up...");
118
119
- info("LISTENER: releasing allocated memory...");
119
+ info("releasing allocated memory...");
120
freez(socket_listen_main_multi_threaded_fds);
121
122
- info("LISTENER: closing all sockets...");
122
+ info("closing all sockets...");
123
listen_sockets_close(&api_sockets);
124
125
- info("LISTENER: cleanup completed.");
125
+ info("cleanup completed.");
126
}
127
128
struct web_client *w;
129
for(w = web_clients; w ; w = w->next) {
130
if(!web_client_check_obsolete(w)) {
131
WEB_CLIENT_IS_OBSOLETE(w);
132
- info("LISTENER: Stopping web client %s, id %llu", w->client_ip, w->id);
132
+ info("Stopping web client %s, id %llu", w->client_ip, w->id);
133
netdata_thread_cancel(w->thread);
134
}
135
}
@@ -262,12 +262,12 @@ static void socket_listen_main_single_threaded_cleanup(void *data) {
262
if(static_thread->enabled) {
263
static_thread->enabled = 0;
264
265
- info("%s: cleaning up...", netdata_thread_tag());
265
+ info("cleaning up...");
266
267
- info("LISTENER: closing all sockets...");
267
+ info("closing all sockets...");
268
listen_sockets_close(&api_sockets);
269
270
- info("LISTENER: cleanup completed.");
270
+ info("cleanup completed.");
271
debug(D_WEB_CLIENT, "LISTENER: exit!");
272
}
273
}