summaryrefslogtreecommitdiffstats
path: root/deps/jemalloc/test/unit/background_thread.c
blob: c60010a81ff62638730237d9f58096b47b11749d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "test/jemalloc_test.h"

#include "jemalloc/internal/util.h"

static void
test_switch_background_thread_ctl(bool new_val) {
	bool e0, e1;
	size_t sz = sizeof(bool);

	e1 = new_val;
	expect_d_eq(mallctl("background_thread", (void *)&e0, &sz,
	    &e1, sz), 0, "Unexpected mallctl() failure");
	expect_b_eq(e0, !e1,
	    "background_thread should be %d before.\n", !e1);
	if (e1) {
		expect_zu_gt(n_background_threads, 0,
		    "Number of background threads should be non zero.\n");
	} else {
		expect_zu_eq(n_background_threads, 0,
		    "Number of background threads should be zero.\n");
	}
}

static void
test_repeat_background_thread_ctl(bool before) {
	bool e0, e1;
	size_t sz = sizeof(bool);

	e1 = before;
	expect_d_eq(mallctl("background_thread", (void *)&e0, &sz,
	    &e1, sz), 0, "Unexpected mallctl() failure");
	expect_b_eq(e0, before,
	    "background_thread should be %d.\n", before);
	if (e1) {
		expect_zu_gt(n_background_threads, 0,
		    "Number of background threads should be non zero.\n");
	} else {
		expect_zu_eq(n_background_threads, 0,
		    "Number of background threads should be zero.\n");
	}
}

TEST_BEGIN(test_background_thread_ctl) {
	test_skip_if(!have_background_thread);

	bool e0, e1;
	size_t sz = sizeof(bool);

	expect_d_eq(mallctl("opt.background_thread", (void *)&e0, &sz,
	    NULL, 0), 0, "Unexpected mallctl() failure");
	expect_d_eq(mallctl("background_thread", (void *)&e1, &sz,
	    NULL, 0), 0, "Unexpected mallctl() failure");
	expect_b_eq(e0, e1,
	    "Default and opt.background_thread does not match.\n");
	if (e0) {
		test_switch_background_thread_ctl(false);
	}
	expect_zu_eq(n_background_threads, 0,
	    "Number of background threads should be 0.\n");

	for (unsigned i = 0; i < 4; i++) {
		test_switch_background_thread_ctl(true);
		test_repeat_background_thread_ctl(true);
		test_repeat_background_thread_ctl(true);

		test_switch_background_thread_ctl(false);
		test_repeat_background_thread_ctl(false);
		test_repeat_background_thread_ctl(false);
	}
}
TEST_END

TEST_BEGIN(test_background_thread_running) {
	test_skip_if(!have_background_thread);
	test_skip_if(!config_stats);

#if defined(JEMALLOC_BACKGROUND_THREAD)
	tsd_t *tsd = tsd_fetch();
	background_thread_info_t *info = &background_thread_info[0];

	test_repeat_background_thread_ctl(false);
	test_switch_background_thread_ctl(true);
	expect_b_eq(info->state, background_thread_started,
	    "Background_thread did not start.\n");

	nstime_t start;
	nstime_init_update(&start);

	bool ran = false;
	while (true) {
		malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
		if (info->tot_n_runs > 0) {
			ran = true;
		}
		malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
		if (ran) {
			break;
		}

		nstime_t now;
		nstime_init_update(&now);
		nstime_subtract(&now, &start);
		expect_u64_lt(nstime_sec(&now), 1000,
		    "Background threads did not run for 1000 seconds.");
		sleep(1);
	}
	test_switch_background_thread_ctl(false);
#endif
}
TEST_END

int
main(void) {
	/* Background_thread creation tests reentrancy naturally. */
	return test_no_reentrancy(
	    test_background_thread_ctl,
	    test_background_thread_running);
}