186abd0dcd8e478759fe409d338d11558c4cec427Jason Evans#include "test/jemalloc_test.h"
2d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
32a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evansstatic const bool config_tcache =
42a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans#ifdef JEMALLOC_TCACHE
52a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans    true
62a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans#else
72a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans    false
82a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans#endif
92a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans    ;
102a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans
11d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evansvoid *
120f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansthd_start(void *arg)
13d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans{
14d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	int err;
15d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	size_t sz;
16d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	bool e0, e1;
17d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
18d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	sz = sizeof(bool);
19d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	if ((err = mallctl("thread.tcache.enabled", &e0, &sz, NULL, 0))) {
20d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans		if (err == ENOENT) {
212a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans			assert_false(config_tcache,
222a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans			    "ENOENT should only be returned if tcache is "
232a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans			    "disabled");
24d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans		}
252a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans		goto label_ENOENT;
26d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	}
27d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
28d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	if (e0) {
29d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans		e1 = false;
302a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans		assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz),
312a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans		    0, "Unexpected mallctl() error");
322a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans		assert_true(e0, "tcache should be enabled");
33d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	}
34d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
35d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	e1 = true;
362a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
372a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    "Unexpected mallctl() error");
382a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_false(e0, "tcache should be disabled");
39d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
40d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	e1 = true;
412a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
422a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    "Unexpected mallctl() error");
432a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_true(e0, "tcache should be enabled");
44d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
45d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	e1 = false;
462a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
472a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    "Unexpected mallctl() error");
482a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_true(e0, "tcache should be enabled");
49d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
50d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	e1 = false;
512a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
522a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    "Unexpected mallctl() error");
532a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_false(e0, "tcache should be disabled");
54d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
55d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	free(malloc(1));
56d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	e1 = true;
572a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
582a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    "Unexpected mallctl() error");
592a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_false(e0, "tcache should be disabled");
60d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
61d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	free(malloc(1));
62d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	e1 = true;
632a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
642a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    "Unexpected mallctl() error");
652a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_true(e0, "tcache should be enabled");
66d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
67d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	free(malloc(1));
68d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	e1 = false;
692a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
702a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    "Unexpected mallctl() error");
712a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_true(e0, "tcache should be enabled");
72d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
73d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	free(malloc(1));
74d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	e1 = false;
752a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_d_eq(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz), 0,
762a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    "Unexpected mallctl() error");
772a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	assert_false(e0, "tcache should be disabled");
78d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
79d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	free(malloc(1));
802a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	return (NULL);
812a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evanslabel_ENOENT:
822a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	test_skip("\"thread.tcache.enabled\" mallctl not available");
83d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans	return (NULL);
84d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans}
85d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
862a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason EvansTEST_BEGIN(test_main_thread)
87d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans{
88d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
890f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	thd_start(NULL);
902a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans}
912a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason EvansTEST_END
92d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
932a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason EvansTEST_BEGIN(test_subthread)
942a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans{
950f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	thd_t thd;
96d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
970f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	thd_create(&thd, thd_start, NULL);
980f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	thd_join(thd, NULL);
992a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans}
1002a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason EvansTEST_END
101d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
1022a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evansint
1032a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evansmain(void)
1042a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans{
105d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans
1062a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	/* Run tests multiple times to check for bad interactions. */
1072a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	return (test(
1082a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    test_main_thread,
1092a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    test_subthread,
1102a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    test_main_thread,
1112a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    test_subthread,
1122a83ed0284e92c7ba4bd4efe9df149ac724b2f26Jason Evans	    test_main_thread));
113d4be8b7b6ee2e21d079180455d4ccbf45cc1cee7Jason Evans}
114