diff options
Diffstat (limited to 'bin/armsinglepage.c')
-rw-r--r-- | bin/armsinglepage.c | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/bin/armsinglepage.c b/bin/armsinglepage.c new file mode 100644 index 0000000..cf619b7 --- /dev/null +++ b/bin/armsinglepage.c | |||
@@ -0,0 +1,238 @@ | |||
1 | #include <stdint.h> | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <sys/ioctl.h> | ||
5 | #include <unistd.h> | ||
6 | #include <inttypes.h> | ||
7 | |||
8 | #include "asm/cacheparams.h" | ||
9 | |||
10 | #include <litmus/rt_param.h> | ||
11 | |||
12 | #include "perfcounters.h" | ||
13 | |||
14 | #include "color.h" | ||
15 | #include "litmus.h" | ||
16 | |||
17 | #define CPU 1 | ||
18 | #define NR_LOOPS 10000 | ||
19 | |||
20 | static int pages_per_datum; | ||
21 | /* Number of pages in a "data" element, each with same color. */ | ||
22 | |||
23 | static int nr_data; | ||
24 | /* Number of "data" elements. */ | ||
25 | |||
26 | |||
27 | struct page; | ||
28 | #define NR_PAGE_INTS ((PAGE_SIZE - CACHE_LINE_SIZE) / sizeof(uint32_t)) | ||
29 | struct page { | ||
30 | struct page *ptr; | ||
31 | struct page *_unused[CACHE_LINE_SIZE / sizeof(struct page*) - 1]; | ||
32 | uint32_t ints[NR_PAGE_INTS]; | ||
33 | }; | ||
34 | |||
35 | struct datum { | ||
36 | struct page *pages; | ||
37 | }; | ||
38 | |||
39 | /* | ||
40 | * Get a random number in [0, max). Not really a good way to do this. | ||
41 | */ | ||
42 | static int randrange(const int max) | ||
43 | { | ||
44 | return (rand() / (RAND_MAX / max + 1)); | ||
45 | } | ||
46 | |||
47 | static void setup_page_ints(struct page *page) | ||
48 | { | ||
49 | int i; | ||
50 | |||
51 | for (i = 0; i < NR_PAGE_INTS; i++) { | ||
52 | page->ints[i] = randrange(RAND_MAX); | ||
53 | } | ||
54 | } | ||
55 | |||
56 | /* | ||
57 | * Sattolo's algorithm makes a random cycle that includes all the elements | ||
58 | * in the items array. | ||
59 | */ | ||
60 | static void sattolo(int *items, const int len) | ||
61 | { | ||
62 | int i; | ||
63 | /* first set up 0, 1, ..., n - 1 */ | ||
64 | for (i = 0; i < len; i++) | ||
65 | items[i] = i; | ||
66 | /* note: i is now n */ | ||
67 | while (1 < i--) { | ||
68 | /* 0 <= j < i */ | ||
69 | int t, j = randrange(i); | ||
70 | t = items[i]; | ||
71 | items[i] = items[j]; | ||
72 | items[j] = t; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | static uint32_t get_sum(struct page *page) | ||
77 | { | ||
78 | uint32_t sum = 0; | ||
79 | int i; | ||
80 | |||
81 | for (i = 0; i < NR_PAGE_INTS; i += CACHE_LINE_SIZE / sizeof(uint32_t)) { | ||
82 | sum += page->ints[i]; | ||
83 | } | ||
84 | return sum; | ||
85 | } | ||
86 | |||
87 | static uint32_t do_read(struct page* page) | ||
88 | { | ||
89 | struct page *old; | ||
90 | uint32_t sum = 0; | ||
91 | |||
92 | do { | ||
93 | old = page; | ||
94 | page = page->ptr; | ||
95 | sum += get_sum(old); | ||
96 | } while (page); | ||
97 | return sum; | ||
98 | } | ||
99 | |||
100 | #define MULT 699050667 | ||
101 | #define SHIFT 24 | ||
102 | |||
103 | static inline int64_t clocksource_cyc2ns(cycles_t cycles, uint32_t mult, uint32_t shift) | ||
104 | { | ||
105 | return ((uint64_t) cycles * mult) >> shift; | ||
106 | } | ||
107 | |||
108 | #if 0 | ||
109 | static void print_difference(const cycles_t start, const cycles_t end) | ||
110 | { | ||
111 | cycles_t diff = end - start; | ||
112 | |||
113 | printf("difference: %10llu = %10llu ns\n", diff, clocksource_cyc2ns(diff, MULT, SHIFT)); | ||
114 | } | ||
115 | #endif | ||
116 | |||
117 | #define quit_on_err(err, msg) do { \ | ||
118 | if (err) { \ | ||
119 | fprintf(stderr, "error: " msg); \ | ||
120 | goto out; \ | ||
121 | } \ | ||
122 | } while (0) | ||
123 | |||
124 | int main(int argc, char **argv) | ||
125 | { | ||
126 | struct color_ctrl_page *color_ctrl; | ||
127 | struct datum *data; | ||
128 | int *read_order; | ||
129 | int i, j, err = 0; | ||
130 | cycles_t start, end; | ||
131 | uint32_t sum = 0; | ||
132 | int64_t nanoseconds; | ||
133 | double avg_nanoseconds; | ||
134 | |||
135 | quit_on_err(PAGE_SIZE != sizeof(struct page), | ||
136 | "PAGE_SIZE != sizeof(struct page)\n"); | ||
137 | |||
138 | if (3 > argc) { | ||
139 | fprintf(stderr, "%s: [pages-per-datum] [nr-data]\n", argv[0]); | ||
140 | err = 1; | ||
141 | goto out; | ||
142 | } | ||
143 | |||
144 | pages_per_datum = atoi(argv[1]); | ||
145 | nr_data = atoi(argv[2]); | ||
146 | |||
147 | quit_on_err(0 == nr_data || 0 == pages_per_datum, "zero argument\n"); | ||
148 | quit_on_err(16 < nr_data, "too many nr_data\n"); | ||
149 | |||
150 | read_order = malloc(pages_per_datum * sizeof(*read_order)); | ||
151 | data = malloc(nr_data * sizeof(*data)); | ||
152 | quit_on_err(!read_order || !data, "malloc\n"); | ||
153 | |||
154 | err = be_migrate_to(CPU); | ||
155 | quit_on_err(err, "migrate to cpu\n"); | ||
156 | |||
157 | color_ctrl = get_color_ctrl(); | ||
158 | if (!color_ctrl) { | ||
159 | fprintf(stderr, "could not map color ctrl\n"); | ||
160 | err = -1; | ||
161 | goto out; | ||
162 | } | ||
163 | |||
164 | /* Always allocating this many pages of the same color. */ | ||
165 | color_ctrl->pages[0] = pages_per_datum; | ||
166 | |||
167 | for (i = 0; i < nr_data; i++) { | ||
168 | const unsigned nr_bytes = sizeof(data[i].pages[0]) * pages_per_datum; | ||
169 | |||
170 | /* give each datum its own color (XXX no checks to ensure not > | ||
171 | * NR_COLORS!) */ | ||
172 | |||
173 | color_ctrl->colors[0] = i; | ||
174 | fprintf(stderr, "color_mallocing %u bytes\n", nr_bytes); | ||
175 | data[i].pages = color_malloc(nr_bytes); | ||
176 | if (!data[i].pages) { | ||
177 | fprintf(stderr, "could not color malloc\n"); | ||
178 | err = -1; | ||
179 | goto out; | ||
180 | } | ||
181 | } | ||
182 | |||
183 | sattolo(read_order, pages_per_datum); | ||
184 | |||
185 | for (i = 0; i < nr_data; i++) { | ||
186 | for (j = 0; j < pages_per_datum; j++) { | ||
187 | if (0 != read_order[j]) { | ||
188 | /* not the last element */ | ||
189 | data[i].pages[j].ptr = &(data[i].pages[read_order[j]]); | ||
190 | } else { | ||
191 | /* last element in pages inside of datum walk */ | ||
192 | if (nr_data - 1 == i) { | ||
193 | /* last element globally */ | ||
194 | data[i].pages[j].ptr = NULL; | ||
195 | } else { | ||
196 | /* jump to next datum */ | ||
197 | data[i].pages[j].ptr = &(data[i + 1].pages[0]); | ||
198 | } | ||
199 | } | ||
200 | |||
201 | setup_page_ints(&(data[i].pages[j])); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | #if 0 | ||
206 | null_call(&start); | ||
207 | null_call(&end); | ||
208 | print_difference(start, end); | ||
209 | |||
210 | null_call(&start); | ||
211 | sum += do_read(data[0].pages); | ||
212 | null_call(&end); | ||
213 | print_difference(start, end); | ||
214 | |||
215 | null_call(&start); | ||
216 | sum += do_read(data[0].pages); | ||
217 | null_call(&end); | ||
218 | print_difference(start, end); | ||
219 | |||
220 | null_call(&start); | ||
221 | sum += do_read(data[0].pages); | ||
222 | null_call(&end); | ||
223 | print_difference(start, end); | ||
224 | #endif | ||
225 | |||
226 | null_call(&start); | ||
227 | for (i = 0; i < NR_LOOPS; i++) { | ||
228 | sum += do_read(data[0].pages); | ||
229 | } | ||
230 | null_call(&end); | ||
231 | nanoseconds = clocksource_cyc2ns(end - start, MULT, SHIFT); | ||
232 | avg_nanoseconds = ((double)nanoseconds) / NR_LOOPS; | ||
233 | printf("%3d, %10.3f, %10.3f\n", nr_data, avg_nanoseconds, | ||
234 | avg_nanoseconds / (nr_data * pages_per_datum)); | ||
235 | |||
236 | out: | ||
237 | return err; | ||
238 | } | ||