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