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