summaryrefslogtreecommitdiff
path: root/test/unit/prof_thread_name.c
blob: 3c4614fca93e168ad5283da5c4e344d438779cda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "test/jemalloc_test.h"

static void
mallctl_thread_name_get_impl(const char *thread_name_expected, const char *func,
    int line) {
	const char *thread_name_old;
	size_t sz;

	sz = sizeof(thread_name_old);
	expect_d_eq(mallctl("thread.prof.name", (void *)&thread_name_old, &sz,
	    NULL, 0), 0,
	    "%s():%d: Unexpected mallctl failure reading thread.prof.name",
	    func, line);
	expect_str_eq(thread_name_old, thread_name_expected,
	    "%s():%d: Unexpected thread.prof.name value", func, line);
}
#define mallctl_thread_name_get(a)					\
	mallctl_thread_name_get_impl(a, __func__, __LINE__)

static void
mallctl_thread_name_set_impl(const char *thread_name, const char *func,
    int line) {
	expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
	    (void *)&thread_name, sizeof(thread_name)), 0,
	    "%s():%d: Unexpected mallctl failure writing thread.prof.name",
	    func, line);
	mallctl_thread_name_get_impl(thread_name, func, line);
}
#define mallctl_thread_name_set(a)					\
	mallctl_thread_name_set_impl(a, __func__, __LINE__)

TEST_BEGIN(test_prof_thread_name_validation) {
	const char *thread_name;

	test_skip_if(!config_prof);
	test_skip_if(opt_prof_sys_thread_name);

	mallctl_thread_name_get("");
	mallctl_thread_name_set("hi there");

	/* NULL input shouldn't be allowed. */
	thread_name = NULL;
	expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
	    (void *)&thread_name, sizeof(thread_name)), EFAULT,
	    "Unexpected mallctl result writing \"%s\" to thread.prof.name",
	    thread_name);

	/* '\n' shouldn't be allowed. */
	thread_name = "hi\nthere";
	expect_d_eq(mallctl("thread.prof.name", NULL, NULL,
	    (void *)&thread_name, sizeof(thread_name)), EFAULT,
	    "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);
		expect_d_eq(mallctl("thread.prof.name",
		    (void *)&thread_name_old, &sz, (void *)&thread_name,
		    sizeof(thread_name)), EPERM,
		    "Unexpected mallctl result writing \"%s\" to "
		    "thread.prof.name", thread_name);
	}

	mallctl_thread_name_set("");
}
TEST_END

#define NTHREADS	4
#define NRESET		25
static void *
thd_start(void *varg) {
	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++) {
		expect_d_eq(mallctl("prof.reset", NULL, NULL, NULL, 0), 0,
		    "Unexpected error while resetting heap profile data");
		mallctl_thread_name_get(thread_name);
	}

	mallctl_thread_name_set(thread_name);
	mallctl_thread_name_set("");

	return NULL;
}

TEST_BEGIN(test_prof_thread_name_threaded) {
	test_skip_if(!config_prof);
	test_skip_if(opt_prof_sys_thread_name);

	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]);
	}
	for (i = 0; i < NTHREADS; i++) {
		thd_join(thds[i], NULL);
	}
}
TEST_END
#undef NTHREADS
#undef NRESET

int
main(void) {
	return test(
	    test_prof_thread_name_validation,
	    test_prof_thread_name_threaded);
}