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