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(
     14 	    old, expected, "%s():%d: Unexpected %s value", func, line, 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(
     25 	    mallctl(name, (void *)&old, &sz, (void *)&val_new, sizeof(val_new)),
     26 	    0, "%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(
     34     bool prof_active_old_expected, const char *func, 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(
     51     bool thread_prof_active_old_expected, const char *func, int line) {
     52 	mallctl_bool_get(
     53 	    "thread.prof.active", thread_prof_active_old_expected, 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(
     73 	    prof_bt_count(), 0, "%s():%d: Expected 0 backtraces", func, 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) prof_sampling_probe_impl(a, __func__, __LINE__)
     81 
     82 TEST_BEGIN(test_prof_active) {
     83 	test_skip_if(!config_prof);
     84 
     85 	mallctl_prof_active_get(true);
     86 	mallctl_thread_prof_active_get(false);
     87 
     88 	mallctl_prof_active_set(true, true);
     89 	mallctl_thread_prof_active_set(false, false);
     90 	/* prof.active, !thread.prof.active. */
     91 	prof_sampling_probe(false);
     92 
     93 	mallctl_prof_active_set(true, false);
     94 	mallctl_thread_prof_active_set(false, false);
     95 	/* !prof.active, !thread.prof.active. */
     96 	prof_sampling_probe(false);
     97 
     98 	mallctl_prof_active_set(false, false);
     99 	mallctl_thread_prof_active_set(false, true);
    100 	/* !prof.active, thread.prof.active. */
    101 	prof_sampling_probe(false);
    102 
    103 	mallctl_prof_active_set(false, true);
    104 	mallctl_thread_prof_active_set(true, true);
    105 	/* prof.active, thread.prof.active. */
    106 	prof_sampling_probe(true);
    107 
    108 	/* Restore settings. */
    109 	mallctl_prof_active_set(true, true);
    110 	mallctl_thread_prof_active_set(true, false);
    111 }
    112 TEST_END
    113 
    114 int
    115 main(void) {
    116 	return test_no_reentrancy(test_prof_active);
    117 }
    118