summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2017-12-27 05:57:03 +0200
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>2017-12-27 05:57:03 +0200
commitc51e1551fc09d2c3eb2f8b3ad21767724b390780 (patch)
tree76f8be1f144a74fbe450d2ed86de3a314b26cd95
parentf40beee346198979f6e01b5ae04e4c7792c8d217 (diff)
optimize statsd cleanup; work around freeze of musl-libc cleanup on exit; https://github.com/firehol/binary-packages/issues/4
-rwxr-xr-xmakeself/jobs/70-netdata-git.install.sh3
-rw-r--r--src/main.c44
-rw-r--r--src/statsd.c30
3 files changed, 47 insertions, 30 deletions
diff --git a/makeself/jobs/70-netdata-git.install.sh b/makeself/jobs/70-netdata-git.install.sh
index b85481492c..5369205c8b 100755
--- a/makeself/jobs/70-netdata-git.install.sh
+++ b/makeself/jobs/70-netdata-git.install.sh
@@ -6,9 +6,10 @@ cd "${NETDATA_SOURCE_PATH}" || exit 1
if [ ${NETDATA_BUILD_WITH_DEBUG} -eq 0 ]
then
- export CFLAGS="-static -O3"
+ export CFLAGS="-static -O3 -DNETDATA_WAIT_BEFORE_EXIT=2"
else
export CFLAGS="-static -O1 -ggdb -Wall -Wextra -Wformat-signedness -fstack-protector-all -D_FORTIFY_SOURCE=2 -DNETDATA_INTERNAL_CHECKS=1"
+# export CFLAGS="-static -O1 -ggdb -Wall -Wextra -Wformat-signedness"
fi
if [ ! -z "${NETDATA_INSTALL_PATH}" -a -d "${NETDATA_INSTALL_PATH}/etc" ]
diff --git a/src/main.c b/src/main.c
index 0422b2047e..806028d476 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,28 +6,33 @@ void netdata_cleanup_and_exit(int ret) {
netdata_exit = 1;
error_log_limit_unlimited();
+ info("netdata is preparing to exit...");
- debug(D_EXIT, "Called: netdata_cleanup_and_exit()");
+ // allow all the threads to cleanup by themselves
+ unsigned int w = (unsigned int)default_rrd_update_every + 2;
+ info("Giving %u secs to background threads to cleanup...", w);
+ sleep(w);
- // cleanup the database
+ // kill all threads and childs
+ //info("Stopping all threads and child processes...");
+ //kill_childs();
+
+ // cleanup the database (delete files not needed)
+ info("Cleaning up the database...");
rrdhost_cleanup_all();
+ // free the database
+ //info("Freeing database memory...");
+ //rrdhost_free_all();
+
// unlink the pid
if(pidfile[0]) {
+ info("Removing netdata PID file '%s'...", pidfile);
if(unlink(pidfile) != 0)
error("Cannot unlink pidfile '%s'.", pidfile);
}
-#ifdef NETDATA_INTERNAL_CHECKS
- // kill all childs
- //kill_childs();
-
- // free database
- sleep(2);
- rrdhost_free_all();
-#endif
-
- info("netdata exiting. Bye bye...");
+ info("All done - netdata is now exiting - bye bye...");
exit(ret);
}
@@ -177,8 +182,7 @@ int killpid(pid_t pid, int sig)
return ret;
}
-void kill_childs()
-{
+void kill_childs() {
error_log_limit_unlimited();
siginfo_t info;
@@ -186,9 +190,9 @@ void kill_childs()
struct web_client *w;
for(w = web_clients; w ; w = w->next) {
info("Stopping web client %s", w->client_ip);
- pthread_cancel(w->thread);
- // it is detached
- // pthread_join(w->thread, NULL);
+ int ret;
+ if((ret = pthread_cancel(w->thread)) != 0)
+ error("pthread_cancel() failed with code %d.", ret);
WEB_CLIENT_IS_OBSOLETE(w);
}
@@ -197,9 +201,9 @@ void kill_childs()
for (i = 0; static_threads[i].name != NULL ; i++) {
if(static_threads[i].enabled) {
info("Stopping %s thread", static_threads[i].name);
- pthread_cancel(*static_threads[i].thread);
- // it is detached
- // pthread_join(*static_threads[i].thread, NULL);
+ int ret;
+ if((ret = pthread_cancel(*static_threads[i].thread)) != 0)
+ error("pthread_cancel() failed with code %d.", ret);
static_threads[i].enabled = 0;
}
diff --git a/src/statsd.c b/src/statsd.c
index 2d07a6126a..9499b1bc36 100644
--- a/src/statsd.c
+++ b/src/statsd.c
@@ -252,7 +252,10 @@ static struct statsd {
size_t histogram_increase_step;
double histogram_percentile;
char *histogram_percentile_str;
+
int threads;
+ pthread_t *collection_threads;
+
LISTEN_SOCKETS sockets;
} statsd = {
.enabled = 1,
@@ -320,6 +323,7 @@ static struct statsd {
.histogram_percentile = 95.0,
.histogram_increase_step = 10,
.threads = 0,
+ .collection_threads = NULL,
.sockets = {
.config_section = CONFIG_SECTION_STATSD,
.default_bind_to = "udp:localhost tcp:localhost",
@@ -1969,13 +1973,22 @@ int statsd_listen_sockets_setup(void) {
}
void statsd_main_cleanup(void *data) {
- pthread_t *threads = data;
+ (void)data;
- int i;
- for(i = 0; i < statsd.threads ;i++)
- pthread_cancel(threads[i]);
+ info("STATSD: cleaning up...");
+ if(statsd.collection_threads) {
+ int i;
+ for (i = 0; i < statsd.threads; i++) {
+ info("STATSD: stopping data collection thread %d...", i);
+ pthread_cancel(statsd.collection_threads[i]);
+ }
+ }
+
+ info("STATSD: closing sockets...");
listen_sockets_close(&statsd.sockets);
+
+ info("STATSD: cleanup completed.");
}
void *statsd_main(void *ptr) {
@@ -2079,18 +2092,17 @@ void *statsd_main(void *ptr) {
pthread_exit(NULL);
}
- pthread_t threads[statsd.threads];
+ statsd.collection_threads = callocz((size_t)statsd.threads, sizeof(pthread_t));
int i;
-
for(i = 0; i < statsd.threads ;i++) {
- if(pthread_create(&threads[i], NULL, statsd_collector_thread, &i))
+ if(pthread_create(&statsd.collection_threads[i], NULL, statsd_collector_thread, &i))
error("STATSD: failed to create child thread.");
- else if(pthread_detach(threads[i]))
+ else if(pthread_detach(statsd.collection_threads[i]))
error("STATSD: cannot request detach of child thread.");
}
- pthread_cleanup_push(statsd_main_cleanup, &threads);
+ pthread_cleanup_push(statsd_main_cleanup, NULL);
// ----------------------------------------------------------------------------------------------------------------
// statsd monitoring charts