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