2014-10-04 14:25:30 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
mallctl_thread_name_get_impl(const char *thread_name_expected, const char *func,
|
2017-01-16 08:56:30 +08:00
|
|
|
int line) {
|
2014-10-04 14:25:30 +08:00
|
|
|
const char *thread_name_old;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
sz = sizeof(thread_name_old);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("thread.prof.name", (void *)&thread_name_old, &sz,
|
2016-10-28 12:31:25 +08:00
|
|
|
NULL, 0), 0,
|
|
|
|
"%s():%d: Unexpected mallctl failure reading thread.prof.name",
|
2014-10-04 14:25:30 +08:00
|
|
|
func, line);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_str_eq(thread_name_old, thread_name_expected,
|
2014-10-04 14:25:30 +08:00
|
|
|
"%s():%d: Unexpected thread.prof.name value", func, line);
|
|
|
|
}
|
2017-01-20 13:41:41 +08:00
|
|
|
#define mallctl_thread_name_get(a) \
|
2014-10-04 14:25:30 +08:00
|
|
|
mallctl_thread_name_get_impl(a, __func__, __LINE__)
|
|
|
|
|
|
|
|
static void
|
|
|
|
mallctl_thread_name_set_impl(const char *thread_name, const char *func,
|
2017-01-16 08:56:30 +08:00
|
|
|
int line) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
|
2016-10-28 12:31:25 +08:00
|
|
|
(void *)&thread_name, sizeof(thread_name)), 0,
|
2014-10-04 14:25:30 +08:00
|
|
|
"%s():%d: Unexpected mallctl failure reading thread.prof.name",
|
|
|
|
func, line);
|
|
|
|
mallctl_thread_name_get_impl(thread_name, func, line);
|
|
|
|
}
|
2017-01-20 13:41:41 +08:00
|
|
|
#define mallctl_thread_name_set(a) \
|
2014-10-04 14:25:30 +08:00
|
|
|
mallctl_thread_name_set_impl(a, __func__, __LINE__)
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_prof_thread_name_validation) {
|
2014-10-04 14:25:30 +08:00
|
|
|
const char *thread_name;
|
|
|
|
|
2014-10-04 14:41:53 +08:00
|
|
|
test_skip_if(!config_prof);
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
mallctl_thread_name_get("");
|
|
|
|
mallctl_thread_name_set("hi there");
|
|
|
|
|
|
|
|
/* NULL input shouldn't be allowed. */
|
|
|
|
thread_name = NULL;
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
|
2016-10-28 12:31:25 +08:00
|
|
|
(void *)&thread_name, sizeof(thread_name)), EFAULT,
|
2014-10-04 14:25:30 +08:00
|
|
|
"Unexpected mallctl result writing \"%s\" to thread.prof.name",
|
|
|
|
thread_name);
|
|
|
|
|
|
|
|
/* '\n' shouldn't be allowed. */
|
|
|
|
thread_name = "hi\nthere";
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
|
2016-10-28 12:31:25 +08:00
|
|
|
(void *)&thread_name, sizeof(thread_name)), EFAULT,
|
2014-10-04 14:25:30 +08:00
|
|
|
"Unexpected mallctl result writing \"%s\" to thread.prof.name",
|
|
|
|
thread_name);
|
|
|
|
|
|
|
|
/* Simultaneous read/write shouldn't be allowed. */
|
|
|
|
{
|
|
|
|
const char *thread_name_old;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
sz = sizeof(thread_name_old);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("thread.prof.name",
|
2016-10-28 12:31:25 +08:00
|
|
|
(void *)&thread_name_old, &sz, (void *)&thread_name,
|
|
|
|
sizeof(thread_name)), EPERM,
|
2014-10-04 14:25:30 +08:00
|
|
|
"Unexpected mallctl result writing \"%s\" to "
|
|
|
|
"thread.prof.name", thread_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
mallctl_thread_name_set("");
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NTHREADS 4
|
|
|
|
#define NRESET 25
|
2014-10-04 14:25:30 +08:00
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
thd_start(void *varg) {
|
2014-10-04 14:25:30 +08:00
|
|
|
unsigned thd_ind = *(unsigned *)varg;
|
|
|
|
char thread_name[16] = "";
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
malloc_snprintf(thread_name, sizeof(thread_name), "thread %u", thd_ind);
|
|
|
|
|
|
|
|
mallctl_thread_name_get("");
|
|
|
|
mallctl_thread_name_set(thread_name);
|
|
|
|
|
|
|
|
for (i = 0; i < NRESET; i++) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("prof.reset", NULL, NULL, NULL, 0), 0,
|
2014-10-04 14:25:30 +08:00
|
|
|
"Unexpected error while resetting heap profile data");
|
|
|
|
mallctl_thread_name_get(thread_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
mallctl_thread_name_set(thread_name);
|
|
|
|
mallctl_thread_name_set("");
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_prof_thread_name_threaded) {
|
2014-10-04 14:25:30 +08:00
|
|
|
thd_t thds[NTHREADS];
|
|
|
|
unsigned thd_args[NTHREADS];
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
test_skip_if(!config_prof);
|
|
|
|
|
|
|
|
for (i = 0; i < NTHREADS; i++) {
|
|
|
|
thd_args[i] = i;
|
|
|
|
thd_create(&thds[i], thd_start, (void *)&thd_args[i]);
|
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < NTHREADS; i++) {
|
2014-10-04 14:25:30 +08:00
|
|
|
thd_join(thds[i], NULL);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
#undef NTHREADS
|
|
|
|
#undef NRESET
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2014-10-04 14:25:30 +08:00
|
|
|
test_prof_thread_name_validation,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_prof_thread_name_threaded);
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|