@cryptotaxi247 / netdata-1 / commits / c51e1551f

optimize statsd cleanup; work around freeze of musl-libc cleanup on exit; https://github.com/firehol/binary-packages/issues/4

Costa Tsaousis (ktsaou) committed Dec 27, 2017 at 05:57 UTC c51e1551fc09d2c3eb2f8b3ad21767724b390780
3 files changed +47 -30
makeself/jobs/70-netdata-git.install.sh
+2 -1
@@ -6,9 +6,10 @@ cd "${NETDATA_SOURCE_PATH}" || exit 1
6
7 if [ ${NETDATA_BUILD_WITH_DEBUG} -eq 0 ]
8 then
9 - export CFLAGS="-static -O3"
9 + export CFLAGS="-static -O3 -DNETDATA_WAIT_BEFORE_EXIT=2"
10 else
11 export CFLAGS="-static -O1 -ggdb -Wall -Wextra -Wformat-signedness -fstack-protector-all -D_FORTIFY_SOURCE=2 -DNETDATA_INTERNAL_CHECKS=1"
12 +# export CFLAGS="-static -O1 -ggdb -Wall -Wextra -Wformat-signedness"
13 fi
14
15 if [ ! -z "${NETDATA_INSTALL_PATH}" -a -d "${NETDATA_INSTALL_PATH}/etc" ]
src/main.c
+24 -20
@@ -6,28 +6,33 @@ void netdata_cleanup_and_exit(int ret) {
6 netdata_exit = 1;
7
8 error_log_limit_unlimited();
9 + info("netdata is preparing to exit...");
10
10 - debug(D_EXIT, "Called: netdata_cleanup_and_exit()");
11 + // allow all the threads to cleanup by themselves
12 + unsigned int w = (unsigned int)default_rrd_update_every + 2;
13 + info("Giving %u secs to background threads to cleanup...", w);
14 + sleep(w);
15
12 - // cleanup the database
16 + // kill all threads and childs
17 + //info("Stopping all threads and child processes...");
18 + //kill_childs();
19 +
20 + // cleanup the database (delete files not needed)
21 + info("Cleaning up the database...");
22 rrdhost_cleanup_all();
23
24 + // free the database
25 + //info("Freeing database memory...");
26 + //rrdhost_free_all();
27 +
28 // unlink the pid
29 if(pidfile[0]) {
30 + info("Removing netdata PID file '%s'...", pidfile);
31 if(unlink(pidfile) != 0)
32 error("Cannot unlink pidfile '%s'.", pidfile);
33 }
34
21 -#ifdef NETDATA_INTERNAL_CHECKS
22 - // kill all childs
23 - //kill_childs();
24 -
25 - // free database
26 - sleep(2);
27 - rrdhost_free_all();
28 -#endif
29 -
30 - info("netdata exiting. Bye bye...");
35 + info("All done - netdata is now exiting - bye bye...");
36 exit(ret);
37 }
38
@@ -177,8 +182,7 @@ int killpid(pid_t pid, int sig)
182 return ret;
183 }
184
180 -void kill_childs()
181 -{
185 +void kill_childs() {
186 error_log_limit_unlimited();
187
188 siginfo_t info;
@@ -186,9 +190,9 @@ void kill_childs()
190 struct web_client *w;
191 for(w = web_clients; w ; w = w->next) {
192 info("Stopping web client %s", w->client_ip);
189 - pthread_cancel(w->thread);
190 - // it is detached
191 - // pthread_join(w->thread, NULL);
193 + int ret;
194 + if((ret = pthread_cancel(w->thread)) != 0)
195 + error("pthread_cancel() failed with code %d.", ret);
196
197 WEB_CLIENT_IS_OBSOLETE(w);
198 }
@@ -197,9 +201,9 @@ void kill_childs()
201 for (i = 0; static_threads[i].name != NULL ; i++) {
202 if(static_threads[i].enabled) {
203 info("Stopping %s thread", static_threads[i].name);
200 - pthread_cancel(*static_threads[i].thread);
201 - // it is detached
202 - // pthread_join(*static_threads[i].thread, NULL);
204 + int ret;
205 + if((ret = pthread_cancel(*static_threads[i].thread)) != 0)
206 + error("pthread_cancel() failed with code %d.", ret);
207
208 static_threads[i].enabled = 0;
209 }
src/statsd.c
+21 -9
@@ -252,7 +252,10 @@ static struct statsd {
252 size_t histogram_increase_step;
253 double histogram_percentile;
254 char *histogram_percentile_str;
255 +
256 int threads;
257 + pthread_t *collection_threads;
258 +
259 LISTEN_SOCKETS sockets;
260 } statsd = {
261 .enabled = 1,
@@ -320,6 +323,7 @@ static struct statsd {
323 .histogram_percentile = 95.0,
324 .histogram_increase_step = 10,
325 .threads = 0,
326 + .collection_threads = NULL,
327 .sockets = {
328 .config_section = CONFIG_SECTION_STATSD,
329 .default_bind_to = "udp:localhost tcp:localhost",
@@ -1969,13 +1973,22 @@ int statsd_listen_sockets_setup(void) {
1973 }
1974
1975 void statsd_main_cleanup(void *data) {
1972 - pthread_t *threads = data;
1976 + (void)data;
1977
1974 - int i;
1975 - for(i = 0; i < statsd.threads ;i++)
1976 - pthread_cancel(threads[i]);
1978 + info("STATSD: cleaning up...");
1979
1980 + if(statsd.collection_threads) {
1981 + int i;
1982 + for (i = 0; i < statsd.threads; i++) {
1983 + info("STATSD: stopping data collection thread %d...", i);
1984 + pthread_cancel(statsd.collection_threads[i]);
1985 + }
1986 + }
1987 +
1988 + info("STATSD: closing sockets...");
1989 listen_sockets_close(&statsd.sockets);
1990 +
1991 + info("STATSD: cleanup completed.");
1992 }
1993
1994 void *statsd_main(void *ptr) {
@@ -2079,18 +2092,17 @@ void *statsd_main(void *ptr) {
2092 pthread_exit(NULL);
2093 }
2094
2082 - pthread_t threads[statsd.threads];
2095 + statsd.collection_threads = callocz((size_t)statsd.threads, sizeof(pthread_t));
2096 int i;
2084 -
2097 for(i = 0; i < statsd.threads ;i++) {
2086 - if(pthread_create(&threads[i], NULL, statsd_collector_thread, &i))
2098 + if(pthread_create(&statsd.collection_threads[i], NULL, statsd_collector_thread, &i))
2099 error("STATSD: failed to create child thread.");
2100
2089 - else if(pthread_detach(threads[i]))
2101 + else if(pthread_detach(statsd.collection_threads[i]))
2102 error("STATSD: cannot request detach of child thread.");
2103 }
2104
2093 - pthread_cleanup_push(statsd_main_cleanup, &threads);
2105 + pthread_cleanup_push(statsd_main_cleanup, NULL);
2106
2107 // ----------------------------------------------------------------------------------------------------------------
2108 // statsd monitoring charts