Loading...
  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
//
//  basic_malloc_free_perf.c
//  libmalloc
//
//  Simple and repeatable performance tests for malloc/free, running tests
// 	on the regular and Nanov2 allocators.
//
#include <darwintest.h>
#include <dispatch/dispatch.h>
#include <../src/internal.h>
#include <perfcheck_keys.h>

// This value is a guess that will be refined over time.
#define PERFCHECK_THRESHOLD_PCT	10.0

static uint32_t
ncpu(void)
{
	static uint32_t activecpu, physicalcpu;
	if (!activecpu) {
		uint32_t n;
		size_t s = sizeof(n);
		sysctlbyname("hw.activecpu", &n, &s, NULL, 0);
		activecpu = n;
		s = sizeof(n);
		sysctlbyname("hw.physicalcpu", &n, &s, NULL, 0);
		physicalcpu = n;
	}
	return MIN(activecpu, physicalcpu);
}

// Code to run a single test and save the converged sample time as the DPS
// metric. The code also measures the time taken in dispatch_apply(), but that
// should be more or less constant in all cases. We are only interested in
// whether the overall sampled time regresses, not in the absolute time value.

static void
run_test(void (^test)(size_t), bool singlethreaded)
{
	uint32_t nthreads = 0;
	if (singlethreaded) {
		nthreads = 1;
	} else {
		const char *e;
		if ((e = getenv("DT_STAT_NTHREADS"))) {
			nthreads = strtoul(e, NULL, 0);
		}
		if (nthreads < 2) {
			nthreads = ncpu();
		}
	}
	dt_stat_time_t s = dt_stat_time_create(
			nthreads > 1 ? "basic_malloc_free_perf multithreaded" :
					"basic_malloc_free_perf singlethreaded");
	dt_stat_set_variable((dt_stat_t)s, "threads", nthreads);
	do {
		int batch_size = dt_stat_batch_size(s);
		dt_stat_token t = dt_stat_begin(s);
		// rdar://problem/40417821: disable thresholds for now.
		//dt_stat_set_variable((dt_stat_t)s, kPCFailureThresholdPctVar,
		//		PERFCHECK_THRESHOLD_PCT);

		dispatch_apply(nthreads,
				dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), test);

		dt_stat_end_batch(s, batch_size, t);
	} while (!dt_stat_stable(s));
	dt_stat_finalize(s);
}

// Test content. Each of the functions is called from six different test cases,
// with singlethreaded true and false and MallocNanoZone set to 0 and V2.
// The test content is biased towards the implementation of Nanov2.

static void
basic_perf_malloc_free_8_bytes(bool singlethreaded)
{
#define NUM_ALLOCS 512
	run_test(^(size_t iteration __unused) {
		void *ptrs[NUM_ALLOCS];
		for (int i = 0; i < NUM_ALLOCS; i++) {
			ptrs[i] = malloc(8);
		}

		for (int i = 0; i < NUM_ALLOCS; i++) {
			free(ptrs[i]);
		}
	}, singlethreaded);
#undef NUM_ALLOCS
}

static void
basic_perf_malloc_free_8_bytes_multi_block(bool singlethreaded)
{
	// Use a large number of allocations to amortize the cost of scanning
	// for a new block.
#define NUM_ALLOCS 65535
	run_test(^(size_t iteration __unused) {
		void **ptrs = calloc(NUM_ALLOCS, sizeof(void *));
		for (int i = 0; i < NUM_ALLOCS; i++) {
			ptrs[i] = malloc(8);
		}

		for (int i = 0; i < NUM_ALLOCS; i++) {
			free(ptrs[i]);
		}
		free(ptrs);
	}, singlethreaded);
#undef NUM_ALLOCS
}

static void
basic_perf_malloc_free_different_size_classes(bool singlethreaded)
{
#define NUM_ALLOCS 512
	run_test(^(size_t iteration __unused) {
		void *ptrs[NUM_ALLOCS];
		size_t sz = (iteration + 1) * 16;
		if (sz > 256) {
			// Too big for Nano.
			return;
		}
		for (int i = 0; i < NUM_ALLOCS; i++) {
			ptrs[i] = malloc(sz);
		}

		for (int i = 0; i < NUM_ALLOCS; i++) {
			free(ptrs[i]);
		}
	}, singlethreaded);
#undef NUM_ALLOCS
}

static void
basic_perf_malloc_free_by_size_class(bool singlethreaded)
{
#define NUM_LOOPS 16
	run_test(^(size_t iteration __unused) {
		void *ptrs[NUM_LOOPS * 16];
		int k = 0;
		for (int i = 0; i < NUM_LOOPS; i++) {
			for (int j = 0; j < 16; j++) {
				ptrs[k++] = malloc(16 * j + 8);
			}
		}

		for (int i = 0; i < k; i++) {
			free(ptrs[i]);
		}
	}, singlethreaded);
#undef NUM_LOOPS
}

static void
basic_perf_malloc_free_by_size_class_offset(bool singlethreaded)
{
#define NUM_LOOPS 16
	int cpu_number = _os_cpu_number();
	run_test(^(size_t iteration __unused) {
		void *ptrs[NUM_LOOPS * 16];
		int k = 0;
		for (int i = 0; i < NUM_LOOPS; i++) {
			for (int j = 0; j < 16; j++) {
				ptrs[k++] = malloc(16 * ((j + cpu_number) % 16) + 8);
			}
		}

		for (int i = 0; i < k; i++) {
			free(ptrs[i]);
		}
	}, singlethreaded);
#undef NUM_LOOPS
}

// The tests that follow are grouped as follows:
// 	- single-thread non-Nano version
//	- single-threaded NanoV2 version
// 	- parallel non-Nano version
//	- parallel NanoV2 version
// Each group probably could be built with a macro, but that would be harder
// to debug when there is a problem.

#pragma mark -
#pragma mark 8-byte allocation/free

T_DECL(basic_perf_serial_8_bytes, "Malloc/Free 8 bytes single-threaded",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_8_bytes(true);
}

T_DECL(basic_perf_serial_8_bytes_V2, "Malloc/Free 8 bytes single-threaded on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_8_bytes(true);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

T_DECL(basic_perf_parallel_8_bytes, "Malloc/Free 8 bytes parallel",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_8_bytes(false);
}

T_DECL(basic_perf_parallel_8_bytes_V2, "Malloc/Free 8 bytes single-threaded on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_8_bytes(false);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

#pragma mark -
#pragma mark 8-byte allocation/free forcing block overflow with default scan policy

T_DECL(basic_perf_serial_8_bytes_multi_block_default_scan_policy,
	   "Malloc/Free 8 bytes single-threaded with block overflow, default scan policy",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_8_bytes_multi_block(true);
}

T_DECL(basic_perf_serial_8_bytes_multi_block_default_scan_policy_V2,
	   	"Malloc/Free 8 bytes single-threaded with block overflow, default scan policy on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_8_bytes_multi_block(true);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

T_DECL(basic_perf_parallel_8_bytes_multi_block_default_scan_policy,
	   "Malloc/Free 8 bytes parallel with block overflow, default scan policy",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_8_bytes_multi_block(false);
}

T_DECL(basic_perf_parallel_8_bytes_multi_block_default_scan_policy_V2,
	   	"Malloc/Free 8 bytes parallel with block overflow, default scan policy on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_8_bytes_multi_block(false);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

#pragma mark -
#pragma mark 8-byte allocation/free forcing block overflow with first-fit

// This test only makes sense on Nanov2
T_DECL(basic_perf_serial_8_bytes_multi_block_first_fit_V2,
	   "Malloc/Free 8 bytes single-threaded with block overflow, first-fit on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT), T_META_CHECK_LEAKS(false),
	   T_META_ENVVAR("MallocNanoZone=V2"),
	   T_META_ENVVAR("MallocNanoScanPolicy=firstfit"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_8_bytes_multi_block(true);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

T_DECL(basic_perf_parallel_8_bytes_multi_block_first_fit_V2,
	   "Malloc/Free 8 bytes parallel with block overflow, first-fit on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=V2"),
	   T_META_ENVVAR("MallocNanoScanPolicy=firstfit"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_8_bytes_multi_block(false);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

#pragma mark -
#pragma mark Repeated allocation/free where each thread uses a different size class

T_DECL(basic_perf_serial_different_size_classes,
	   "Malloc/Free in different size classes single-threaded",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_different_size_classes(false);
}

T_DECL(basic_perf_serial_different_size_classes_V2,
	   "Malloc/Free in different size classes single-threaded on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_different_size_classes(true);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

T_DECL(basic_perf_parallel_different_size_classes,
	   "Malloc/Free in different size classes parallel",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_different_size_classes(false);
}

T_DECL(basic_perf_parallel_different_size_classes_V2,
	   "Malloc/Free in different size classes single-threaded on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_different_size_classes(false);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

#pragma mark -
#pragma mark Repeated allocation/free for each size class

T_DECL(basic_perf_serial_by_size_class,
	   "Malloc/Free by size class single-threaded",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_by_size_class(true);
}

T_DECL(basic_perf_serial_by_size_class_V2,
	   "Malloc/Free by size class single-threaded on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_by_size_class(true);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

T_DECL(basic_perf_parallel_by_size_class,
	   "Malloc/Free by size class parallel",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_by_size_class(false);
}

T_DECL(basic_perf_parallel_by_size_class_V2,
	   "Malloc/Free by size class single-threaded on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_by_size_class(false);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

#pragma mark -
#pragma mark Repeated allocation/free for each size class, offset by CPU

T_DECL(basic_perf_serial_by_size_class_offset,
	   "Malloc/Free by size class with offset single-threaded",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_by_size_class_offset(true);
}

T_DECL(basic_perf_serial_by_size_class_offset_V2,
	   	"Malloc/Free by size class with offset single-threaded on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_by_size_class_offset(true);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}

T_DECL(basic_perf_parallel_by_size_class_offset,
	   "Malloc/Free by size class with offset parallel",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=0"))
{
	basic_perf_malloc_free_by_size_class_offset(false);
}

T_DECL(basic_perf_parallel_by_size_class_offset_V2,
	   	"Malloc/Free by size class with offset single-threaded on V2",
	   T_META_TAG_PERF, T_META_ALL_VALID_ARCHS(NO),
	   T_META_LTEPHASE(LTE_POSTINIT),
	   T_META_ENVVAR("MallocNanoZone=V2"))
{
#if CONFIG_NANOZONE
	basic_perf_malloc_free_by_size_class_offset(false);
#else // CONFIG_NANOZONE
	T_SKIP("Nano allocator not configured");
#endif // CONFIG_NANOZONE
}