summaryrefslogtreecommitdiffstats
path: root/deps/jemalloc/test/unit/sec.c
blob: f3ec403dafd1f9a25253e4cd977babc0f7d39825 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#include "test/jemalloc_test.h"

#include "jemalloc/internal/sec.h"

typedef struct pai_test_allocator_s pai_test_allocator_t;
struct pai_test_allocator_s {
	pai_t pai;
	bool alloc_fail;
	size_t alloc_count;
	size_t alloc_batch_count;
	size_t dalloc_count;
	size_t dalloc_batch_count;
	/*
	 * We use a simple bump allocator as the implementation.  This isn't
	 * *really* correct, since we may allow expansion into a subsequent
	 * allocation, but it's not like the SEC is really examining the
	 * pointers it gets back; this is mostly just helpful for debugging.
	 */
	uintptr_t next_ptr;
	size_t expand_count;
	bool expand_return_value;
	size_t shrink_count;
	bool shrink_return_value;
};

static void
test_sec_init(sec_t *sec, pai_t *fallback, size_t nshards, size_t max_alloc,
    size_t max_bytes) {
	sec_opts_t opts;
	opts.nshards = 1;
	opts.max_alloc = max_alloc;
	opts.max_bytes = max_bytes;
	/*
	 * Just choose reasonable defaults for these; most tests don't care so
	 * long as they're something reasonable.
	 */
	opts.bytes_after_flush = max_bytes / 2;
	opts.batch_fill_extra = 4;

	/*
	 * We end up leaking this base, but that's fine; this test is
	 * short-running, and SECs are arena-scoped in reality.
	 */
	base_t *base = base_new(TSDN_NULL, /* ind */ 123,
	    &ehooks_default_extent_hooks, /* metadata_use_hooks */ true);

	bool err = sec_init(TSDN_NULL, sec, base, fallback, &opts);
	assert_false(err, "Unexpected initialization failure");
	assert_u_ge(sec->npsizes, 0, "Zero size classes allowed for caching");
}

static inline edata_t *
pai_test_allocator_alloc(tsdn_t *tsdn, pai_t *self, size_t size,
    size_t alignment, bool zero, bool guarded, bool frequent_reuse,
    bool *deferred_work_generated) {
	assert(!guarded);
	pai_test_allocator_t *ta = (pai_test_allocator_t *)self;
	if (ta->alloc_fail) {
		return NULL;
	}
	edata_t *edata = malloc(sizeof(edata_t));
	assert_ptr_not_null(edata, "");
	ta->next_ptr += alignment - 1;
	edata_init(edata, /* arena_ind */ 0,
	    (void *)(ta->next_ptr & ~(alignment - 1)), size,
	    /* slab */ false,
	    /* szind */ 0, /* sn */ 1, extent_state_active, /* zero */ zero,
	    /* comitted */ true, /* ranged */ false, EXTENT_NOT_HEAD);
	ta->next_ptr += size;
	ta->alloc_count++;
	return edata;
}

static inline size_t
pai_test_allocator_alloc_batch(tsdn_t *tsdn, pai_t *self, size_t size,
    size_t nallocs, edata_list_active_t *results,
    bool *deferred_work_generated) {
	pai_test_allocator_t *ta = (pai_test_allocator_t *)self;
	if (ta->alloc_fail) {
		return 0;
	}
	for (size_t i = 0; i < nallocs; i++) {
		edata_t *edata = malloc(sizeof(edata_t));
		assert_ptr_not_null(edata, "");
		edata_init(edata, /* arena_ind */ 0,
		    (void *)ta->next_ptr, size,
		    /* slab */ false, /* szind */ 0, /* sn */ 1,
		    extent_state_active, /* zero */ false, /* comitted */ true,
		    /* ranged */ false, EXTENT_NOT_HEAD);
		ta->next_ptr += size;
		ta->alloc_batch_count++;
		edata_list_active_append(results, edata);
	}
	return nallocs;
}

static bool
pai_test_allocator_expand(tsdn_t *tsdn, pai_t *self, edata_t *edata,
    size_t old_size, size_t new_size, bool zero,
    bool *deferred_work_generated) {
	pai_test_allocator_t *ta = (pai_test_allocator_t *)self;
	ta->expand_count++;
	return ta->expand_return_value;
}

static bool
pai_test_allocator_shrink(tsdn_t *tsdn, pai_t *self, edata_t *edata,
    size_t old_size, size_t new_size, bool *deferred_work_generated) {
	pai_test_allocator_t *ta = (pai_test_allocator_t *)self;
	ta->shrink_count++;
	return ta->shrink_return_value;
}

static void
pai_test_allocator_dalloc(tsdn_t *tsdn, pai_t *self, edata_t *edata,
    bool *deferred_work_generated) {
	pai_test_allocator_t *ta = (pai_test_allocator_t *)self;
	ta->dalloc_count++;
	free(edata);
}

static void
pai_test_allocator_dalloc_batch(tsdn_t *tsdn, pai_t *self,
    edata_list_active_t *list, bool *deferred_work_generated) {
	pai_test_allocator_t *ta = (pai_test_allocator_t *)self;

	edata_t *edata;
	while ((edata = edata_list_active_first(list)) != NULL) {
		edata_list_active_remove(list, edata);
		ta->dalloc_batch_count++;
		free(edata);
	}
}

static inline void
pai_test_allocator_init(pai_test_allocator_t *ta) {
	ta->alloc_fail = false;
	ta->alloc_count = 0;
	ta->alloc_batch_count = 0;
	ta->dalloc_count = 0;
	ta->dalloc_batch_count = 0;
	/* Just don't start the edata at 0. */
	ta->next_ptr = 10 * PAGE;
	ta->expand_count = 0;
	ta->expand_return_value = false;
	ta->shrink_count = 0;
	ta->shrink_return_value = false;
	ta->pai.alloc = &pai_test_allocator_alloc;
	ta->pai.alloc_batch = &pai_test_allocator_alloc_batch;
	ta->pai.expand = &pai_test_allocator_expand;
	ta->pai.shrink = &pai_test_allocator_shrink;
	ta->pai.dalloc = &pai_test_allocator_dalloc;
	ta->pai.dalloc_batch = &pai_test_allocator_dalloc_batch;
}

TEST_BEGIN(test_reuse) {
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;
	/*
	 * We can't use the "real" tsd, since we malloc within the test
	 * allocator hooks; we'd get lock inversion crashes.  Eventually, we
	 * should have a way to mock tsds, but for now just don't do any
	 * lock-order checking.
	 */
	tsdn_t *tsdn = TSDN_NULL;
	/*
	 * 11 allocs apiece of 1-PAGE and 2-PAGE objects means that we should be
	 * able to get to 33 pages in the cache before triggering a flush.  We
	 * set the flush liimt to twice this amount, to avoid accidentally
	 * triggering a flush caused by the batch-allocation down the cache fill
	 * pathway disrupting ordering.
	 */
	enum { NALLOCS = 11 };
	edata_t *one_page[NALLOCS];
	edata_t *two_page[NALLOCS];
	bool deferred_work_generated = false;
	test_sec_init(&sec, &ta.pai, /* nshards */ 1, /* max_alloc */ 2 * PAGE,
	    /* max_bytes */ 2 * (NALLOCS * PAGE + NALLOCS * 2 * PAGE));
	for (int i = 0; i < NALLOCS; i++) {
		one_page[i] = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
		expect_ptr_not_null(one_page[i], "Unexpected alloc failure");
		two_page[i] = pai_alloc(tsdn, &sec.pai, 2 * PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
		expect_ptr_not_null(one_page[i], "Unexpected alloc failure");
	}
	expect_zu_eq(0, ta.alloc_count, "Should be using batch allocs");
	size_t max_allocs = ta.alloc_count + ta.alloc_batch_count;
	expect_zu_le(2 * NALLOCS, max_allocs,
	    "Incorrect number of allocations");
	expect_zu_eq(0, ta.dalloc_count,
	    "Incorrect number of allocations");
	/*
	 * Free in a different order than we allocated, to make sure free-list
	 * separation works correctly.
	 */
	for (int i = NALLOCS - 1; i >= 0; i--) {
		pai_dalloc(tsdn, &sec.pai, one_page[i],
		    &deferred_work_generated);
	}
	for (int i = NALLOCS - 1; i >= 0; i--) {
		pai_dalloc(tsdn, &sec.pai, two_page[i],
		    &deferred_work_generated);
	}
	expect_zu_eq(max_allocs, ta.alloc_count + ta.alloc_batch_count,
	    "Incorrect number of allocations");
	expect_zu_eq(0, ta.dalloc_count,
	    "Incorrect number of allocations");
	/*
	 * Check that the n'th most recent deallocated extent is returned for
	 * the n'th alloc request of a given size.
	 */
	for (int i = 0; i < NALLOCS; i++) {
		edata_t *alloc1 = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
		edata_t *alloc2 = pai_alloc(tsdn, &sec.pai, 2 * PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
		expect_ptr_eq(one_page[i], alloc1,
		    "Got unexpected allocation");
		expect_ptr_eq(two_page[i], alloc2,
		    "Got unexpected allocation");
	}
	expect_zu_eq(max_allocs, ta.alloc_count + ta.alloc_batch_count,
	    "Incorrect number of allocations");
	expect_zu_eq(0, ta.dalloc_count,
	    "Incorrect number of allocations");
}
TEST_END


TEST_BEGIN(test_auto_flush) {
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;
	/* See the note above -- we can't use the real tsd. */
	tsdn_t *tsdn = TSDN_NULL;
	/*
	 * 10-allocs apiece of 1-PAGE and 2-PAGE objects means that we should be
	 * able to get to 30 pages in the cache before triggering a flush.  The
	 * choice of NALLOCS here is chosen to match the batch allocation
	 * default (4 extra + 1 == 5; so 10 allocations leaves the cache exactly
	 * empty, even in the presence of batch allocation on fill).
	 * Eventually, once our allocation batching strategies become smarter,
	 * this should change.
	 */
	enum { NALLOCS = 10 };
	edata_t *extra_alloc;
	edata_t *allocs[NALLOCS];
	bool deferred_work_generated = false;
	test_sec_init(&sec, &ta.pai, /* nshards */ 1, /* max_alloc */ PAGE,
	    /* max_bytes */ NALLOCS * PAGE);
	for (int i = 0; i < NALLOCS; i++) {
		allocs[i] = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
		expect_ptr_not_null(allocs[i], "Unexpected alloc failure");
	}
	extra_alloc = pai_alloc(tsdn, &sec.pai, PAGE, PAGE, /* zero */ false,
	    /* guarded */ false, /* frequent_reuse */ false,
	    &deferred_work_generated);
	expect_ptr_not_null(extra_alloc, "Unexpected alloc failure");
	size_t max_allocs = ta.alloc_count + ta.alloc_batch_count;
	expect_zu_le(NALLOCS + 1, max_allocs,
	    "Incorrect number of allocations");
	expect_zu_eq(0, ta.dalloc_count,
	    "Incorrect number of allocations");
	/* Free until the SEC is full, but should not have flushed yet. */
	for (int i = 0; i < NALLOCS; i++) {
		pai_dalloc(tsdn, &sec.pai, allocs[i], &deferred_work_generated);
	}
	expect_zu_le(NALLOCS + 1, max_allocs,
	    "Incorrect number of allocations");
	expect_zu_eq(0, ta.dalloc_count,
	    "Incorrect number of allocations");
	/*
	 * Free the extra allocation; this should trigger a flush.  The internal
	 * flushing logic is allowed to get complicated; for now, we rely on our
	 * whitebox knowledge of the fact that the SEC flushes bins in their
	 * entirety when it decides to do so, and it has only one bin active
	 * right now.
	 */
	pai_dalloc(tsdn, &sec.pai, extra_alloc, &deferred_work_generated);
	expect_zu_eq(max_allocs, ta.alloc_count + ta.alloc_batch_count,
	    "Incorrect number of allocations");
	expect_zu_eq(0, ta.dalloc_count,
	    "Incorrect number of (non-batch) deallocations");
	expect_zu_eq(NALLOCS + 1, ta.dalloc_batch_count,
	    "Incorrect number of batch deallocations");
}
TEST_END

/*
 * A disable and a flush are *almost* equivalent; the only difference is what
 * happens afterwards; disabling disallows all future caching as well.
 */
static void
do_disable_flush_test(bool is_disable) {
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;
	/* See the note above -- we can't use the real tsd. */
	tsdn_t *tsdn = TSDN_NULL;

	enum { NALLOCS = 11 };
	edata_t *allocs[NALLOCS];
	bool deferred_work_generated = false;
	test_sec_init(&sec, &ta.pai, /* nshards */ 1, /* max_alloc */ PAGE,
	    /* max_bytes */ NALLOCS * PAGE);
	for (int i = 0; i < NALLOCS; i++) {
		allocs[i] = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
		expect_ptr_not_null(allocs[i], "Unexpected alloc failure");
	}
	/* Free all but the last aloc. */
	for (int i = 0; i < NALLOCS - 1; i++) {
		pai_dalloc(tsdn, &sec.pai, allocs[i], &deferred_work_generated);
	}
	size_t max_allocs = ta.alloc_count + ta.alloc_batch_count;

	expect_zu_le(NALLOCS, max_allocs, "Incorrect number of allocations");
	expect_zu_eq(0, ta.dalloc_count,
	    "Incorrect number of allocations");

	if (is_disable) {
		sec_disable(tsdn, &sec);
	} else {
		sec_flush(tsdn, &sec);
	}

	expect_zu_eq(max_allocs, ta.alloc_count + ta.alloc_batch_count,
	    "Incorrect number of allocations");
	expect_zu_eq(0, ta.dalloc_count,
	    "Incorrect number of (non-batch) deallocations");
	expect_zu_le(NALLOCS - 1, ta.dalloc_batch_count,
	    "Incorrect number of batch deallocations");
	size_t old_dalloc_batch_count = ta.dalloc_batch_count;

	/*
	 * If we free into a disabled SEC, it should forward to the fallback.
	 * Otherwise, the SEC should accept the allocation.
	 */
	pai_dalloc(tsdn, &sec.pai, allocs[NALLOCS - 1],
	    &deferred_work_generated);

	expect_zu_eq(max_allocs, ta.alloc_count + ta.alloc_batch_count,
	    "Incorrect number of allocations");
	expect_zu_eq(is_disable ? 1 : 0, ta.dalloc_count,
	    "Incorrect number of (non-batch) deallocations");
	expect_zu_eq(old_dalloc_batch_count, ta.dalloc_batch_count,
	    "Incorrect number of batch deallocations");
}

TEST_BEGIN(test_disable) {
	do_disable_flush_test(/* is_disable */ true);
}
TEST_END

TEST_BEGIN(test_flush) {
	do_disable_flush_test(/* is_disable */ false);
}
TEST_END

TEST_BEGIN(test_max_alloc_respected) {
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;
	/* See the note above -- we can't use the real tsd. */
	tsdn_t *tsdn = TSDN_NULL;

	size_t max_alloc = 2 * PAGE;
	size_t attempted_alloc = 3 * PAGE;

	bool deferred_work_generated = false;

	test_sec_init(&sec, &ta.pai, /* nshards */ 1, max_alloc,
	    /* max_bytes */ 1000 * PAGE);

	for (size_t i = 0; i < 100; i++) {
		expect_zu_eq(i, ta.alloc_count,
		    "Incorrect number of allocations");
		expect_zu_eq(i, ta.dalloc_count,
		    "Incorrect number of deallocations");
		edata_t *edata = pai_alloc(tsdn, &sec.pai, attempted_alloc,
		    PAGE, /* zero */ false, /* guarded */ false,
		    /* frequent_reuse */ false, &deferred_work_generated);
		expect_ptr_not_null(edata, "Unexpected alloc failure");
		expect_zu_eq(i + 1, ta.alloc_count,
		    "Incorrect number of allocations");
		expect_zu_eq(i, ta.dalloc_count,
		    "Incorrect number of deallocations");
		pai_dalloc(tsdn, &sec.pai, edata, &deferred_work_generated);
	}
}
TEST_END

TEST_BEGIN(test_expand_shrink_delegate) {
	/*
	 * Expand and shrink shouldn't affect sec state; they should just
	 * delegate to the fallback PAI.
	 */
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;
	/* See the note above -- we can't use the real tsd. */
	tsdn_t *tsdn = TSDN_NULL;

	bool deferred_work_generated = false;

	test_sec_init(&sec, &ta.pai, /* nshards */ 1, /* max_alloc */ 10 * PAGE,
	    /* max_bytes */ 1000 * PAGE);
	edata_t *edata = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
	    /* zero */ false, /* guarded */ false, /* frequent_reuse */ false,
	    &deferred_work_generated);
	expect_ptr_not_null(edata, "Unexpected alloc failure");

	bool err = pai_expand(tsdn, &sec.pai, edata, PAGE, 4 * PAGE,
	    /* zero */ false, &deferred_work_generated);
	expect_false(err, "Unexpected expand failure");
	expect_zu_eq(1, ta.expand_count, "");
	ta.expand_return_value = true;
	err = pai_expand(tsdn, &sec.pai, edata, 4 * PAGE, 3 * PAGE,
	    /* zero */ false, &deferred_work_generated);
	expect_true(err, "Unexpected expand success");
	expect_zu_eq(2, ta.expand_count, "");

	err = pai_shrink(tsdn, &sec.pai, edata, 4 * PAGE, 2 * PAGE,
	    &deferred_work_generated);
	expect_false(err, "Unexpected shrink failure");
	expect_zu_eq(1, ta.shrink_count, "");
	ta.shrink_return_value = true;
	err = pai_shrink(tsdn, &sec.pai, edata, 2 * PAGE, PAGE,
	    &deferred_work_generated);
	expect_true(err, "Unexpected shrink success");
	expect_zu_eq(2, ta.shrink_count, "");
}
TEST_END

TEST_BEGIN(test_nshards_0) {
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;
	/* See the note above -- we can't use the real tsd. */
	tsdn_t *tsdn = TSDN_NULL;
	base_t *base = base_new(TSDN_NULL, /* ind */ 123,
	    &ehooks_default_extent_hooks, /* metadata_use_hooks */ true);

	sec_opts_t opts = SEC_OPTS_DEFAULT;
	opts.nshards = 0;
	sec_init(TSDN_NULL, &sec, base, &ta.pai, &opts);

	bool deferred_work_generated = false;
	edata_t *edata = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
	    /* zero */ false, /* guarded */ false, /* frequent_reuse */ false,
	    &deferred_work_generated);
	pai_dalloc(tsdn, &sec.pai, edata, &deferred_work_generated);

	/* Both operations should have gone directly to the fallback. */
	expect_zu_eq(1, ta.alloc_count, "");
	expect_zu_eq(1, ta.dalloc_count, "");
}
TEST_END

static void
expect_stats_pages(tsdn_t *tsdn, sec_t *sec, size_t npages) {
	sec_stats_t stats;
	/*
	 * Check that the stats merging accumulates rather than overwrites by
	 * putting some (made up) data there to begin with.
	 */
	stats.bytes = 123;
	sec_stats_merge(tsdn, sec, &stats);
	assert_zu_le(npages * PAGE + 123, stats.bytes, "");
}

TEST_BEGIN(test_stats_simple) {
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;

	/* See the note above -- we can't use the real tsd. */
	tsdn_t *tsdn = TSDN_NULL;

	enum {
		NITERS = 100,
		FLUSH_PAGES = 20,
	};

	bool deferred_work_generated = false;

	test_sec_init(&sec, &ta.pai, /* nshards */ 1, /* max_alloc */ PAGE,
	    /* max_bytes */ FLUSH_PAGES * PAGE);

	edata_t *allocs[FLUSH_PAGES];
	for (size_t i = 0; i < FLUSH_PAGES; i++) {
		allocs[i] = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
		expect_stats_pages(tsdn, &sec, 0);
	}

	/* Increase and decrease, without flushing. */
	for (size_t i = 0; i < NITERS; i++) {
		for (size_t j = 0; j < FLUSH_PAGES / 2; j++) {
			pai_dalloc(tsdn, &sec.pai, allocs[j],
			    &deferred_work_generated);
			expect_stats_pages(tsdn, &sec, j + 1);
		}
		for (size_t j = 0; j < FLUSH_PAGES / 2; j++) {
			allocs[j] = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
			    /* zero */ false, /* guarded */ false,
			    /* frequent_reuse */ false,
			    &deferred_work_generated);
			expect_stats_pages(tsdn, &sec, FLUSH_PAGES / 2 - j - 1);
		}
	}
}
TEST_END

TEST_BEGIN(test_stats_auto_flush) {
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;

	/* See the note above -- we can't use the real tsd. */
	tsdn_t *tsdn = TSDN_NULL;

	enum {
		FLUSH_PAGES = 10,
	};

	test_sec_init(&sec, &ta.pai, /* nshards */ 1, /* max_alloc */ PAGE,
	    /* max_bytes */ FLUSH_PAGES * PAGE);

	edata_t *extra_alloc0;
	edata_t *extra_alloc1;
	edata_t *allocs[2 * FLUSH_PAGES];

	bool deferred_work_generated = false;

	extra_alloc0 = pai_alloc(tsdn, &sec.pai, PAGE, PAGE, /* zero */ false,
	    /* guarded */ false, /* frequent_reuse */ false,
	    &deferred_work_generated);
	extra_alloc1 = pai_alloc(tsdn, &sec.pai, PAGE, PAGE, /* zero */ false,
	    /* guarded */ false, /* frequent_reuse */ false,
	    &deferred_work_generated);

	for (size_t i = 0; i < 2 * FLUSH_PAGES; i++) {
		allocs[i] = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
	}

	for (size_t i = 0; i < FLUSH_PAGES; i++) {
		pai_dalloc(tsdn, &sec.pai, allocs[i], &deferred_work_generated);
	}
	pai_dalloc(tsdn, &sec.pai, extra_alloc0, &deferred_work_generated);

	/* Flush the remaining pages; stats should still work. */
	for (size_t i = 0; i < FLUSH_PAGES; i++) {
		pai_dalloc(tsdn, &sec.pai, allocs[FLUSH_PAGES + i],
		    &deferred_work_generated);
	}

	pai_dalloc(tsdn, &sec.pai, extra_alloc1, &deferred_work_generated);

	expect_stats_pages(tsdn, &sec, ta.alloc_count + ta.alloc_batch_count
	    - ta.dalloc_count - ta.dalloc_batch_count);
}
TEST_END

TEST_BEGIN(test_stats_manual_flush) {
	pai_test_allocator_t ta;
	pai_test_allocator_init(&ta);
	sec_t sec;

	/* See the note above -- we can't use the real tsd. */
	tsdn_t *tsdn = TSDN_NULL;

	enum {
		FLUSH_PAGES = 10,
	};

	test_sec_init(&sec, &ta.pai, /* nshards */ 1, /* max_alloc */ PAGE,
	    /* max_bytes */ FLUSH_PAGES * PAGE);

	bool deferred_work_generated = false;
	edata_t *allocs[FLUSH_PAGES];
	for (size_t i = 0; i < FLUSH_PAGES; i++) {
		allocs[i] = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
		    /* zero */ false, /* guarded */ false, /* frequent_reuse */
		    false, &deferred_work_generated);
		expect_stats_pages(tsdn, &sec, 0);
	}

	/* Dalloc the first half of the allocations. */
	for (size_t i = 0; i < FLUSH_PAGES / 2; i++) {
		pai_dalloc(tsdn, &sec.pai, allocs[i], &deferred_work_generated);
		expect_stats_pages(tsdn, &sec, i + 1);
	}

	sec_flush(tsdn, &sec);
	expect_stats_pages(tsdn, &sec, 0);

	/* Flush the remaining pages. */
	for (size_t i = 0; i < FLUSH_PAGES / 2; i++) {
		pai_dalloc(tsdn, &sec.pai, allocs[FLUSH_PAGES / 2 + i],
		    &deferred_work_generated);
		expect_stats_pages(tsdn, &sec, i + 1);
	}
	sec_disable(tsdn, &sec);
	expect_stats_pages(tsdn, &sec, 0);
}
TEST_END

int
main(void) {
	return test(
	    test_reuse,
	    test_auto_flush,
	    test_disable,
	    test_flush,
	    test_max_alloc_respected,
	    test_expand_shrink_delegate,
	    test_nshards_0,
	    test_stats_simple,
	    test_stats_auto_flush,
	    test_stats_manual_flush);
}