• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test/jemalloc_test.h"
2 
3 static void
mallctl_bool_get(const char * name,bool expected,const char * func,int line)4 mallctl_bool_get(const char *name, bool expected, const char *func, int line) {
5 	bool old;
6 	size_t sz;
7 
8 	sz = sizeof(old);
9 	assert_d_eq(mallctl(name, (void *)&old, &sz, NULL, 0), 0,
10 	    "%s():%d: Unexpected mallctl failure reading %s", func, line, name);
11 	assert_b_eq(old, expected, "%s():%d: Unexpected %s value", func, line,
12 	    name);
13 }
14 
15 static void
mallctl_bool_set(const char * name,bool old_expected,bool val_new,const char * func,int line)16 mallctl_bool_set(const char *name, bool old_expected, bool val_new,
17     const char *func, int line) {
18 	bool old;
19 	size_t sz;
20 
21 	sz = sizeof(old);
22 	assert_d_eq(mallctl(name, (void *)&old, &sz, (void *)&val_new,
23 	    sizeof(val_new)), 0,
24 	    "%s():%d: Unexpected mallctl failure reading/writing %s", func,
25 	    line, name);
26 	assert_b_eq(old, old_expected, "%s():%d: Unexpected %s value", func,
27 	    line, name);
28 }
29 
30 static void
mallctl_prof_active_get_impl(bool prof_active_old_expected,const char * func,int line)31 mallctl_prof_active_get_impl(bool prof_active_old_expected, const char *func,
32     int line) {
33 	mallctl_bool_get("prof.active", prof_active_old_expected, func, line);
34 }
35 #define mallctl_prof_active_get(a)					\
36 	mallctl_prof_active_get_impl(a, __func__, __LINE__)
37 
38 static void
mallctl_prof_active_set_impl(bool prof_active_old_expected,bool prof_active_new,const char * func,int line)39 mallctl_prof_active_set_impl(bool prof_active_old_expected,
40     bool prof_active_new, const char *func, int line) {
41 	mallctl_bool_set("prof.active", prof_active_old_expected,
42 	    prof_active_new, func, line);
43 }
44 #define mallctl_prof_active_set(a, b)					\
45 	mallctl_prof_active_set_impl(a, b, __func__, __LINE__)
46 
47 static void
mallctl_thread_prof_active_get_impl(bool thread_prof_active_old_expected,const char * func,int line)48 mallctl_thread_prof_active_get_impl(bool thread_prof_active_old_expected,
49     const char *func, int line) {
50 	mallctl_bool_get("thread.prof.active", thread_prof_active_old_expected,
51 	    func, line);
52 }
53 #define mallctl_thread_prof_active_get(a)				\
54 	mallctl_thread_prof_active_get_impl(a, __func__, __LINE__)
55 
56 static void
mallctl_thread_prof_active_set_impl(bool thread_prof_active_old_expected,bool thread_prof_active_new,const char * func,int line)57 mallctl_thread_prof_active_set_impl(bool thread_prof_active_old_expected,
58     bool thread_prof_active_new, const char *func, int line) {
59 	mallctl_bool_set("thread.prof.active", thread_prof_active_old_expected,
60 	    thread_prof_active_new, func, line);
61 }
62 #define mallctl_thread_prof_active_set(a, b)				\
63 	mallctl_thread_prof_active_set_impl(a, b, __func__, __LINE__)
64 
65 static void
prof_sampling_probe_impl(bool expect_sample,const char * func,int line)66 prof_sampling_probe_impl(bool expect_sample, const char *func, int line) {
67 	void *p;
68 	size_t expected_backtraces = expect_sample ? 1 : 0;
69 
70 	assert_zu_eq(prof_bt_count(), 0, "%s():%d: Expected 0 backtraces", func,
71 	    line);
72 	p = mallocx(1, 0);
73 	assert_ptr_not_null(p, "Unexpected mallocx() failure");
74 	assert_zu_eq(prof_bt_count(), expected_backtraces,
75 	    "%s():%d: Unexpected backtrace count", func, line);
76 	dallocx(p, 0);
77 }
78 #define prof_sampling_probe(a)						\
79 	prof_sampling_probe_impl(a, __func__, __LINE__)
80 
TEST_BEGIN(test_prof_active)81 TEST_BEGIN(test_prof_active) {
82 	test_skip_if(!config_prof);
83 
84 	mallctl_prof_active_get(true);
85 	mallctl_thread_prof_active_get(false);
86 
87 	mallctl_prof_active_set(true, true);
88 	mallctl_thread_prof_active_set(false, false);
89 	/* prof.active, !thread.prof.active. */
90 	prof_sampling_probe(false);
91 
92 	mallctl_prof_active_set(true, false);
93 	mallctl_thread_prof_active_set(false, false);
94 	/* !prof.active, !thread.prof.active. */
95 	prof_sampling_probe(false);
96 
97 	mallctl_prof_active_set(false, false);
98 	mallctl_thread_prof_active_set(false, true);
99 	/* !prof.active, thread.prof.active. */
100 	prof_sampling_probe(false);
101 
102 	mallctl_prof_active_set(false, true);
103 	mallctl_thread_prof_active_set(true, true);
104 	/* prof.active, thread.prof.active. */
105 	prof_sampling_probe(true);
106 
107 	/* Restore settings. */
108 	mallctl_prof_active_set(true, true);
109 	mallctl_thread_prof_active_set(true, false);
110 }
111 TEST_END
112 
113 int
main(void)114 main(void) {
115 	return test_no_reentrancy(
116 	    test_prof_active);
117 }
118