mirror of
https://github.com/jemalloc/jemalloc.git
synced 2026-07-23 13:13:06 +00:00
Inline the storage for thread name in prof_tdata_t.
The previous approach managed the thread name in a separate buffer, which causes races because the thread name update (triggered by new samples) can happen at the same time as prof dumping (which reads the thread names) -- these two operations are under separate locks to avoid blocking each other. Implemented the thread name storage as part of the tdata struct, which resolves the lifetime issue and also avoids internal alloc / dalloc during prof_sample.
This commit is contained in:
@@ -441,64 +441,30 @@ prof_bt_count(void) {
|
||||
return bt_count;
|
||||
}
|
||||
|
||||
char *
|
||||
prof_thread_name_alloc(tsd_t *tsd, const char *thread_name) {
|
||||
char *ret;
|
||||
size_t size;
|
||||
|
||||
if (thread_name == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size = strlen(thread_name) + 1;
|
||||
ret = iallocztm(tsd_tsdn(tsd), size, sz_size2index(size), false, NULL,
|
||||
true, arena_get(TSDN_NULL, 0, true), true);
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(ret, thread_name, size);
|
||||
ret[size - 1] = '\0';
|
||||
|
||||
return ret;
|
||||
static void
|
||||
prof_thread_name_write_tdata(prof_tdata_t *tdata, const char *thread_name) {
|
||||
strncpy(tdata->thread_name, thread_name, PROF_THREAD_NAME_MAX_LEN);
|
||||
tdata->thread_name[PROF_THREAD_NAME_MAX_LEN - 1] = '\0';
|
||||
}
|
||||
|
||||
int
|
||||
prof_thread_name_set_impl(tsd_t *tsd, const char *thread_name) {
|
||||
assert(tsd_reentrancy_level_get(tsd) == 0);
|
||||
assert(thread_name != NULL);
|
||||
|
||||
prof_tdata_t *tdata;
|
||||
unsigned i;
|
||||
char *s;
|
||||
|
||||
tdata = prof_tdata_get(tsd, true);
|
||||
if (tdata == NULL) {
|
||||
return EAGAIN;
|
||||
}
|
||||
|
||||
/* Validate input. */
|
||||
if (thread_name == NULL) {
|
||||
return EFAULT;
|
||||
}
|
||||
for (i = 0; thread_name[i] != '\0'; i++) {
|
||||
for (unsigned i = 0; thread_name[i] != '\0'; i++) {
|
||||
char c = thread_name[i];
|
||||
if (!isgraph(c) && !isblank(c)) {
|
||||
return EFAULT;
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
s = prof_thread_name_alloc(tsd, thread_name);
|
||||
if (s == NULL) {
|
||||
return EAGAIN;
|
||||
prof_tdata_t *tdata = prof_tdata_get(tsd, true);
|
||||
if (tdata == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
char *old_thread_name = tdata->thread_name;
|
||||
tdata->thread_name = s;
|
||||
if (old_thread_name != NULL) {
|
||||
idalloctm(tsd_tsdn(tsd), old_thread_name, /* tcache */ NULL,
|
||||
/* alloc_ctx */ NULL, /* is_internal */ true,
|
||||
/* slow_path */ true);
|
||||
}
|
||||
prof_thread_name_write_tdata(tdata, thread_name);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -949,7 +915,7 @@ prof_tdata_dump_iter(prof_tdata_tree_t *tdatas_ptr, prof_tdata_t *tdata,
|
||||
tdata->thr_uid);
|
||||
prof_dump_print_cnts(arg->prof_dump_write, arg->cbopaque,
|
||||
&tdata->cnt_summed);
|
||||
if (tdata->thread_name != NULL) {
|
||||
if (!prof_thread_name_empty(tdata)) {
|
||||
arg->prof_dump_write(arg->cbopaque, " ");
|
||||
arg->prof_dump_write(arg->cbopaque, tdata->thread_name);
|
||||
}
|
||||
@@ -1179,10 +1145,15 @@ prof_tdata_init_impl(tsd_t *tsd, uint64_t thr_uid, uint64_t thr_discrim,
|
||||
tdata->lock = prof_tdata_mutex_choose(thr_uid);
|
||||
tdata->thr_uid = thr_uid;
|
||||
tdata->thr_discrim = thr_discrim;
|
||||
tdata->thread_name = thread_name;
|
||||
tdata->attached = true;
|
||||
tdata->expired = false;
|
||||
tdata->tctx_uid_next = 0;
|
||||
if (thread_name == NULL) {
|
||||
prof_thread_name_clear(tdata);
|
||||
} else {
|
||||
prof_thread_name_write_tdata(tdata, thread_name);
|
||||
}
|
||||
prof_thread_name_assert(tdata);
|
||||
|
||||
if (ckh_new(tsd, &tdata->bt2tctx, PROF_CKH_MINITEMS, prof_bt_hash,
|
||||
prof_bt_keycomp)) {
|
||||
@@ -1230,13 +1201,8 @@ prof_tdata_destroy_locked(tsd_t *tsd, prof_tdata_t *tdata,
|
||||
malloc_mutex_assert_not_owner(tsd_tsdn(tsd), tdata->lock);
|
||||
|
||||
tdata_tree_remove(&tdatas, tdata);
|
||||
|
||||
assert(prof_tdata_should_destroy_unlocked(tdata, even_if_attached));
|
||||
|
||||
if (tdata->thread_name != NULL) {
|
||||
idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,
|
||||
true);
|
||||
}
|
||||
ckh_delete(tsd, &tdata->bt2tctx);
|
||||
idalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user