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