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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-03-31 06:20:30 +08:00
|
|
|
"%s():%d: Unexpected mallctl failure writing thread.prof.name",
|
2014-10-04 14:25:30 +08:00
|
|
|
func, line);
|
|
|
|
mallctl_thread_name_get_impl(thread_name, func, line);
|
|
|
|
}
|
2023-03-29 09:02:34 +08:00
|
|
|
|
|
|
|
#define mallctl_thread_name_get(a) \
|
|
|
|
mallctl_thread_name_get_impl(a, __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:41:53 +08:00
|
|
|
test_skip_if(!config_prof);
|
2021-03-31 06:20:30 +08:00
|
|
|
test_skip_if(opt_prof_sys_thread_name);
|
2014-10-04 14:41:53 +08:00
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
mallctl_thread_name_get("");
|
2023-03-29 09:02:34 +08:00
|
|
|
|
|
|
|
const char *test_name1 = "test case1";
|
|
|
|
mallctl_thread_name_set(test_name1);
|
|
|
|
|
|
|
|
/* Test name longer than the max len. */
|
|
|
|
char long_name[] =
|
|
|
|
"test case longer than expected; test case longer than expected";
|
|
|
|
expect_zu_gt(strlen(long_name), PROF_THREAD_NAME_MAX_LEN,
|
|
|
|
"Long test name not long enough");
|
|
|
|
const char *test_name_long = long_name;
|
|
|
|
expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
|
|
|
|
(void *)&test_name_long, sizeof(test_name_long)), 0,
|
|
|
|
"Unexpected mallctl failure from thread.prof.name");
|
|
|
|
/* Long name cut to match. */
|
|
|
|
long_name[PROF_THREAD_NAME_MAX_LEN - 1] = '\0';
|
|
|
|
mallctl_thread_name_get(test_name_long);
|
2014-10-04 14:25:30 +08:00
|
|
|
|
|
|
|
/* NULL input shouldn't be allowed. */
|
2023-03-29 09:02:34 +08:00
|
|
|
const char *test_name2 = NULL;
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
|
2023-03-29 09:02:34 +08:00
|
|
|
(void *)&test_name2, sizeof(test_name2)), EINVAL,
|
|
|
|
"Unexpected mallctl result writing to thread.prof.name");
|
2014-10-04 14:25:30 +08:00
|
|
|
|
|
|
|
/* '\n' shouldn't be allowed. */
|
2023-03-29 09:02:34 +08:00
|
|
|
const char *test_name3 = "test\ncase";
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
|
2023-03-29 09:02:34 +08:00
|
|
|
(void *)&test_name3, sizeof(test_name3)), EINVAL,
|
2014-10-04 14:25:30 +08:00
|
|
|
"Unexpected mallctl result writing \"%s\" to thread.prof.name",
|
2023-03-29 09:02:34 +08:00
|
|
|
test_name3);
|
2014-10-04 14:25:30 +08:00
|
|
|
|
|
|
|
/* Simultaneous read/write shouldn't be allowed. */
|
2023-03-29 09:02:34 +08:00
|
|
|
const char *thread_name_old;
|
|
|
|
size_t sz = sizeof(thread_name_old);
|
|
|
|
expect_d_eq(mallctl("thread.prof.name", (void *)&thread_name_old, &sz,
|
|
|
|
(void *)&test_name1, sizeof(test_name1)), EPERM,
|
|
|
|
"Unexpected mallctl result from thread.prof.name");
|
2014-10-04 14:25:30 +08:00
|
|
|
|
|
|
|
mallctl_thread_name_set("");
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2023-03-29 09:02:34 +08:00
|
|
|
#define NRESET 25
|
2014-10-04 14:25:30 +08:00
|
|
|
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;
|
2023-03-29 09:02:34 +08:00
|
|
|
#undef NRESET
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_prof_thread_name_threaded) {
|
2021-03-31 06:20:30 +08:00
|
|
|
test_skip_if(!config_prof);
|
|
|
|
test_skip_if(opt_prof_sys_thread_name);
|
|
|
|
|
2023-03-29 09:02:34 +08:00
|
|
|
#define NTHREADS 4
|
2014-10-04 14:25:30 +08:00
|
|
|
thd_t thds[NTHREADS];
|
|
|
|
unsigned thd_args[NTHREADS];
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2023-03-29 09:02:34 +08:00
|
|
|
#undef NTHREADS
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
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
|
|
|
}
|