]> git.proxmox.com Git - rustc.git/blob - src/jemalloc/test/unit/prof_thread_name.c
New upstream version 1.22.1+dfsg1
[rustc.git] / src / jemalloc / test / unit / prof_thread_name.c
1 #include "test/jemalloc_test.h"
2
3 static void
4 mallctl_thread_name_get_impl(const char *thread_name_expected, const char *func,
5 int line)
6 {
7 const char *thread_name_old;
8 size_t sz;
9
10 sz = sizeof(thread_name_old);
11 assert_d_eq(mallctl("thread.prof.name", (void *)&thread_name_old, &sz,
12 NULL, 0), 0,
13 "%s():%d: Unexpected mallctl failure reading thread.prof.name",
14 func, line);
15 assert_str_eq(thread_name_old, thread_name_expected,
16 "%s():%d: Unexpected thread.prof.name value", func, line);
17 }
18 #define mallctl_thread_name_get(a) \
19 mallctl_thread_name_get_impl(a, __func__, __LINE__)
20
21 static void
22 mallctl_thread_name_set_impl(const char *thread_name, const char *func,
23 int line)
24 {
25
26 assert_d_eq(mallctl("thread.prof.name", NULL, NULL,
27 (void *)&thread_name, sizeof(thread_name)), 0,
28 "%s():%d: Unexpected mallctl failure reading thread.prof.name",
29 func, line);
30 mallctl_thread_name_get_impl(thread_name, func, line);
31 }
32 #define mallctl_thread_name_set(a) \
33 mallctl_thread_name_set_impl(a, __func__, __LINE__)
34
35 TEST_BEGIN(test_prof_thread_name_validation)
36 {
37 const char *thread_name;
38
39 test_skip_if(!config_prof);
40
41 mallctl_thread_name_get("");
42 mallctl_thread_name_set("hi there");
43
44 /* NULL input shouldn't be allowed. */
45 thread_name = NULL;
46 assert_d_eq(mallctl("thread.prof.name", NULL, NULL,
47 (void *)&thread_name, sizeof(thread_name)), EFAULT,
48 "Unexpected mallctl result writing \"%s\" to thread.prof.name",
49 thread_name);
50
51 /* '\n' shouldn't be allowed. */
52 thread_name = "hi\nthere";
53 assert_d_eq(mallctl("thread.prof.name", NULL, NULL,
54 (void *)&thread_name, sizeof(thread_name)), EFAULT,
55 "Unexpected mallctl result writing \"%s\" to thread.prof.name",
56 thread_name);
57
58 /* Simultaneous read/write shouldn't be allowed. */
59 {
60 const char *thread_name_old;
61 size_t sz;
62
63 sz = sizeof(thread_name_old);
64 assert_d_eq(mallctl("thread.prof.name",
65 (void *)&thread_name_old, &sz, (void *)&thread_name,
66 sizeof(thread_name)), EPERM,
67 "Unexpected mallctl result writing \"%s\" to "
68 "thread.prof.name", thread_name);
69 }
70
71 mallctl_thread_name_set("");
72 }
73 TEST_END
74
75 #define NTHREADS 4
76 #define NRESET 25
77 static void *
78 thd_start(void *varg)
79 {
80 unsigned thd_ind = *(unsigned *)varg;
81 char thread_name[16] = "";
82 unsigned i;
83
84 malloc_snprintf(thread_name, sizeof(thread_name), "thread %u", thd_ind);
85
86 mallctl_thread_name_get("");
87 mallctl_thread_name_set(thread_name);
88
89 for (i = 0; i < NRESET; i++) {
90 assert_d_eq(mallctl("prof.reset", NULL, NULL, NULL, 0), 0,
91 "Unexpected error while resetting heap profile data");
92 mallctl_thread_name_get(thread_name);
93 }
94
95 mallctl_thread_name_set(thread_name);
96 mallctl_thread_name_set("");
97
98 return (NULL);
99 }
100
101 TEST_BEGIN(test_prof_thread_name_threaded)
102 {
103 thd_t thds[NTHREADS];
104 unsigned thd_args[NTHREADS];
105 unsigned i;
106
107 test_skip_if(!config_prof);
108
109 for (i = 0; i < NTHREADS; i++) {
110 thd_args[i] = i;
111 thd_create(&thds[i], thd_start, (void *)&thd_args[i]);
112 }
113 for (i = 0; i < NTHREADS; i++)
114 thd_join(thds[i], NULL);
115 }
116 TEST_END
117 #undef NTHREADS
118 #undef NRESET
119
120 int
121 main(void)
122 {
123
124 return (test(
125 test_prof_thread_name_validation,
126 test_prof_thread_name_threaded));
127 }