xref: /xnu-12377.41.6/tools/tests/perf_index/perfindex-memory.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions #include "perf_index.h"
2*bbb1b6f9SApple OSS Distributions #include "fail.h"
3*bbb1b6f9SApple OSS Distributions #include <stdlib.h>
4*bbb1b6f9SApple OSS Distributions #include <unistd.h>
5*bbb1b6f9SApple OSS Distributions #include <string.h>
6*bbb1b6f9SApple OSS Distributions #include <sys/sysctl.h>
7*bbb1b6f9SApple OSS Distributions 
8*bbb1b6f9SApple OSS Distributions static char *memblock;
9*bbb1b6f9SApple OSS Distributions static size_t memsize;
10*bbb1b6f9SApple OSS Distributions 
11*bbb1b6f9SApple OSS Distributions size_t
hw_memsize(void)12*bbb1b6f9SApple OSS Distributions hw_memsize(void)
13*bbb1b6f9SApple OSS Distributions {
14*bbb1b6f9SApple OSS Distributions 	int mib[2];
15*bbb1b6f9SApple OSS Distributions 	size_t len;
16*bbb1b6f9SApple OSS Distributions 	size_t my_memsize;
17*bbb1b6f9SApple OSS Distributions 	int retval;
18*bbb1b6f9SApple OSS Distributions 
19*bbb1b6f9SApple OSS Distributions 	mib[0] = CTL_HW;
20*bbb1b6f9SApple OSS Distributions 	mib[1] = HW_MEMSIZE;
21*bbb1b6f9SApple OSS Distributions 	len = sizeof(my_memsize);
22*bbb1b6f9SApple OSS Distributions 
23*bbb1b6f9SApple OSS Distributions 	retval = sysctl(mib, 2, &my_memsize, &len, NULL, 0);
24*bbb1b6f9SApple OSS Distributions 
25*bbb1b6f9SApple OSS Distributions 	if (retval != 0) {
26*bbb1b6f9SApple OSS Distributions 		return 0;
27*bbb1b6f9SApple OSS Distributions 	}
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions 	return my_memsize;
30*bbb1b6f9SApple OSS Distributions }
31*bbb1b6f9SApple OSS Distributions 
32*bbb1b6f9SApple OSS Distributions DECL_SETUP {
33*bbb1b6f9SApple OSS Distributions 	char *memblockfiller;
34*bbb1b6f9SApple OSS Distributions 	long long i;
35*bbb1b6f9SApple OSS Distributions 	int pgsz = getpagesize();
36*bbb1b6f9SApple OSS Distributions 
37*bbb1b6f9SApple OSS Distributions 	/* Heuristic: use half the physical memory, hopefully this should work on all
38*bbb1b6f9SApple OSS Distributions 	 * devices. We use the amount of physical memory, rather than some softer
39*bbb1b6f9SApple OSS Distributions 	 * metric, like amount of free memory, so that the memory allocated is always
40*bbb1b6f9SApple OSS Distributions 	 * consistent for a given device.
41*bbb1b6f9SApple OSS Distributions 	 */
42*bbb1b6f9SApple OSS Distributions 	memsize = hw_memsize();
43*bbb1b6f9SApple OSS Distributions 	VERIFY(memsize > 0, "hw_memsize failed");
44*bbb1b6f9SApple OSS Distributions 	memsize = memsize / 2;
45*bbb1b6f9SApple OSS Distributions 
46*bbb1b6f9SApple OSS Distributions 	memblock = (char*)malloc(memsize);
47*bbb1b6f9SApple OSS Distributions 	VERIFY(memblock != NULL, "malloc failed");
48*bbb1b6f9SApple OSS Distributions 
49*bbb1b6f9SApple OSS Distributions 	memblockfiller = memblock;
50*bbb1b6f9SApple OSS Distributions 
51*bbb1b6f9SApple OSS Distributions 	/* Do this manually, to make sure everything is paged in */
52*bbb1b6f9SApple OSS Distributions 	for (i = 0; i < memsize; i += pgsz) {
53*bbb1b6f9SApple OSS Distributions 		memblockfiller[i] = 1;
54*bbb1b6f9SApple OSS Distributions 	}
55*bbb1b6f9SApple OSS Distributions 
56*bbb1b6f9SApple OSS Distributions 	return PERFINDEX_SUCCESS;
57*bbb1b6f9SApple OSS Distributions }
58*bbb1b6f9SApple OSS Distributions 
59*bbb1b6f9SApple OSS Distributions /* figures out what region of memory to copy, so it does interfere with other
60*bbb1b6f9SApple OSS Distributions  *  threads,  */
61*bbb1b6f9SApple OSS Distributions DECL_TEST {
62*bbb1b6f9SApple OSS Distributions 	long long left = length;
63*bbb1b6f9SApple OSS Distributions 	long long region_len = memsize / num_threads / 2;
64*bbb1b6f9SApple OSS Distributions 	long long region_start = memsize / num_threads * thread_id / 2;
65*bbb1b6f9SApple OSS Distributions 	long long copy_len;
66*bbb1b6f9SApple OSS Distributions 
67*bbb1b6f9SApple OSS Distributions 	if (thread_id < memsize / 2 % num_threads) {
68*bbb1b6f9SApple OSS Distributions 		region_start += thread_id;
69*bbb1b6f9SApple OSS Distributions 		region_len++;
70*bbb1b6f9SApple OSS Distributions 	} else {
71*bbb1b6f9SApple OSS Distributions 		region_start += memsize / 2 % num_threads;
72*bbb1b6f9SApple OSS Distributions 	}
73*bbb1b6f9SApple OSS Distributions 
74*bbb1b6f9SApple OSS Distributions 	while (left > 0) {
75*bbb1b6f9SApple OSS Distributions 		copy_len = region_len < left ? region_len : left;
76*bbb1b6f9SApple OSS Distributions 		memcpy(memblock + region_start + memsize / 2, memblock + region_start, copy_len);
77*bbb1b6f9SApple OSS Distributions 		left -= copy_len;
78*bbb1b6f9SApple OSS Distributions 	}
79*bbb1b6f9SApple OSS Distributions 
80*bbb1b6f9SApple OSS Distributions 	return PERFINDEX_SUCCESS;
81*bbb1b6f9SApple OSS Distributions }
82*bbb1b6f9SApple OSS Distributions 
83*bbb1b6f9SApple OSS Distributions DECL_CLEANUP {
84*bbb1b6f9SApple OSS Distributions 	free(memblock);
85*bbb1b6f9SApple OSS Distributions 	return PERFINDEX_SUCCESS;
86*bbb1b6f9SApple OSS Distributions }
87