File: randmap.c

package info (click to toggle)
numactl 2.0.11-2.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,912 kB
  • ctags: 922
  • sloc: sh: 11,860; ansic: 6,542; makefile: 104
file content (177 lines) | stat: -rw-r--r-- 3,784 bytes parent folder | download | duplicates (5)
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
/* Randomly change policy */ 
#include <stdio.h>
#include "numa.h"
#include "numaif.h"
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define SIZE (100*1024*1024) 
#define PAGES (SIZE/pagesize)

#define perror(x) printf("%s: %s\n", x, strerror(errno))
#define err(x) perror(x),exit(1)

struct page { 
	unsigned long mask; 
	int policy;
};

struct page *pages;
char *map;
int pagesize;

void setpol(unsigned long offset, unsigned long length, int policy, unsigned long nodes)
{
	long i, end;

	printf("off:%lx length:%lx policy:%d nodes:%lx\n",
	       offset, length, policy, nodes);

	if (mbind(map + offset*pagesize, length*pagesize, policy,
		  &nodes, 8, 0) < 0) {
		printf("mbind: %s offset %lx length %lx policy %d nodes %lx\n", 
		       strerror(errno),
		       offset*pagesize, length*pagesize,
		       policy, nodes);
		return;
	}

	for (i = offset; i < offset+length; i++) { 
		pages[i].mask = nodes;
		pages[i].policy = policy;
	}

	i = offset - 20;
	if (i < 0) 
		i = 0; 
	end = offset+length+20; 
	if (end > PAGES)
		end = PAGES;
	for (; i < end; i++) { 
		int pol2;
		unsigned long nodes2;
		if (get_mempolicy(&pol2, &nodes2, sizeof(long)*8, map+i*pagesize, 
				  			MPOL_F_ADDR) < 0)
			err("get_mempolicy");
		if (pol2 != pages[i].policy) { 
			printf("%lx: got policy %d expected %d, nodes got %lx expected %lx\n",
				i, pol2, pages[i].policy, nodes2, pages[i].mask);
		}
		if (policy != MPOL_DEFAULT && nodes2 != pages[i].mask) {
			printf("%lx: nodes %lx, expected %lx, policy %d\n",
			       i, nodes2, pages[i].mask, policy);
		}
	} 
}

static unsigned char pop4[16] = {
  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
}; 

int popcnt(unsigned long val)
{ 
	int count = 0;
	while (val) { 
		count += pop4[val & 0xf];
		val >>= 4; 
	}
	return count;
} 

void testmap(void) 
{ 

	pages = calloc(1, PAGES * sizeof(struct page)); 
	if (!pages)
		exit(100);

	printf("simple tests\n");
#define MB ((1024*1024)/pagesize)
	setpol(0, PAGES, MPOL_INTERLEAVE, 3); 
	setpol(0, MB, MPOL_BIND, 1); 
	setpol(MB, MB, MPOL_BIND, 1); 
	setpol(MB, MB, MPOL_DEFAULT, 0); 
	setpol(MB, MB, MPOL_PREFERRED, 2); 
	setpol(MB/2, MB, MPOL_DEFAULT, 0); 
	setpol(MB+MB/2, MB, MPOL_BIND, 2); 
	setpol(MB/2+100, 100, MPOL_PREFERRED, 1); 
	setpol(100, 200, MPOL_PREFERRED, 1);
	printf("done\n");

	for (;;) { 	
		unsigned long offset = random() % PAGES; 
		int policy = random() % (MPOL_MAX+1);
		unsigned long nodes = random() % 4;
		long length = random() % (PAGES - offset);

		/* validate */ 
		switch (policy) { 
		case MPOL_DEFAULT: 
			nodes = 0;
			break;
		case MPOL_INTERLEAVE:
		case MPOL_BIND:
			if (nodes == 0) 
				continue;
			break;
		case MPOL_PREFERRED:
			if (popcnt(nodes) != 1) 
				continue;
			break;
		} 


		setpol(offset, length, policy, nodes);

	} 
} 

int main(int ac, char **av)
{
	unsigned long seed;

	pagesize = getpagesize();
	
#if 0
	map = mmap(NULL, SIZE, PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
	if (map == (char*)-1)
		err("mmap");
#else	
	int shmid = shmget(IPC_PRIVATE, SIZE, IPC_CREAT|0666); 
	if (shmid < 0) err("shmget");
	map = shmat(shmid, NULL, SHM_RDONLY);
	shmctl(shmid, IPC_RMID, NULL);
	if (map == (char *)-1) err("shmat");		
	printf("map %p\n", map);
#endif

	if (av[1]) { 
		char *end;
		unsigned long timeout = strtoul(av[1], &end, 0);
		switch (*end) { 
		case 'h': timeout *= 3600; break;
		case 'm': timeout *= 60; break;
		}
		printf("running for %lu seconds\n", timeout);
		alarm(timeout);
	} else
		printf("running forever\n");

	if (av[1] && av[2])
		seed = strtoul(av[2], 0, 0);
	else
		seed = time(0);

	printf("random seed %lu\n", seed); 
	srandom(seed);

	testmap();
	/* test shm etc. */
	return 0;
}