Delay the atexit call to prof_log_start().

So that atexit() is only done when prof_log is used.
This commit is contained in:
Qi Wang 2021-09-28 15:41:10 -07:00 committed by Qi Wang
parent 11b6db7448
commit ab0f1604b4

View File

@ -412,6 +412,13 @@ prof_log_dummy_set(bool new_value) {
prof_log_dummy = new_value; prof_log_dummy = new_value;
} }
/* Used as an atexit function to stop logging on exit. */
static void
prof_log_stop_final(void) {
tsd_t *tsd = tsd_fetch();
prof_log_stop(tsd_tsdn(tsd));
}
JEMALLOC_COLD JEMALLOC_COLD
bool bool
prof_log_start(tsdn_t *tsdn, const char *filename) { prof_log_start(tsdn_t *tsdn, const char *filename) {
@ -425,6 +432,20 @@ prof_log_start(tsdn_t *tsdn, const char *filename) {
malloc_mutex_lock(tsdn, &log_mtx); malloc_mutex_lock(tsdn, &log_mtx);
static bool prof_log_atexit_called = false;
if (!prof_log_atexit_called) {
prof_log_atexit_called = true;
if (atexit(prof_log_stop_final) != 0) {
malloc_write("<jemalloc>: Error in atexit() "
"for logging\n");
if (opt_abort) {
abort();
}
ret = true;
goto label_done;
}
}
if (prof_logging_state != prof_logging_state_stopped) { if (prof_logging_state != prof_logging_state_stopped) {
ret = true; ret = true;
} else if (filename == NULL) { } else if (filename == NULL) {
@ -442,19 +463,12 @@ prof_log_start(tsdn_t *tsdn, const char *filename) {
if (!ret) { if (!ret) {
nstime_prof_init_update(&log_start_timestamp); nstime_prof_init_update(&log_start_timestamp);
} }
label_done:
malloc_mutex_unlock(tsdn, &log_mtx); malloc_mutex_unlock(tsdn, &log_mtx);
return ret; return ret;
} }
/* Used as an atexit function to stop logging on exit. */
static void
prof_log_stop_final(void) {
tsd_t *tsd = tsd_fetch();
prof_log_stop(tsd_tsdn(tsd));
}
struct prof_emitter_cb_arg_s { struct prof_emitter_cb_arg_s {
int fd; int fd;
ssize_t ret; ssize_t ret;
@ -697,15 +711,6 @@ prof_log_init(tsd_t *tsd) {
prof_log_start(tsd_tsdn(tsd), NULL); prof_log_start(tsd_tsdn(tsd), NULL);
} }
if (atexit(prof_log_stop_final) != 0) {
malloc_write("<jemalloc>: Error in atexit() "
"for logging\n");
if (opt_abort) {
abort();
}
return true;
}
return false; return false;
} }