aboutsummaryrefslogtreecommitdiffstats
path: root/bin/mc2mem.c
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mc2mem.c')
-rw-r--r--bin/mc2mem.c304
1 files changed, 304 insertions, 0 deletions
diff --git a/bin/mc2mem.c b/bin/mc2mem.c
new file mode 100644
index 0000000..2fba878
--- /dev/null
+++ b/bin/mc2mem.c
@@ -0,0 +1,304 @@
1#include <sys/time.h>
2#include <sys/mman.h>
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <time.h>
8#include <string.h>
9#include <assert.h>
10#include <limits.h>
11#include <fcntl.h>
12#include <math.h>
13
14#include "litmus.h"
15#include "common.h"
16#include "cache_common.h"
17
18#define PAGE_SIZE (4096)
19#define CACHELINE_SIZE 32
20#define INTS_IN_CACHELINE (CACHELINE_SIZE/sizeof(int))
21#define CACHELINES_IN_1KB (1024 / sizeof(cacheline_t))
22#define INTS_IN_1KB (1024 / sizeof(int))
23#define INTS_IN_CACHELINE (CACHELINE_SIZE/sizeof(int))
24
25static int loops = 100;
26static cacheline_t* arena = NULL;
27static int verbose = 0;
28
29#define UNCACHE_DEV "/dev/litmus/uncache"
30
31/* Random walk around the arena in cacheline-sized chunks.
32 Cacheline-sized chucks ensures the same utilization of each
33 hit line as sequential read. (Otherwise, our utilization
34 would only be 1/INTS_IN_CACHELINE.) */
35static int random_walk(cacheline_t *mem, int wss, int write_cycle)
36{
37 /* a random cycle among the cache lines was set up by init_arena(). */
38 int sum, i, next;
39
40 int numlines = wss * CACHELINES_IN_1KB;
41
42 sum = 0;
43
44 /* contents of arena is structured s.t. offsets are all
45 w.r.t. to start of arena, so compute the initial offset */
46 next = mem - arena;
47
48 if (write_cycle == 0) {
49 for (i = 0; i < numlines; i++) {
50 /* every element in the cacheline has the same value */
51 next = arena[next].line[0];
52 sum += next;
53 }
54 }
55
56 else {
57 int w, which_line;
58 for (i = 0, w = 0; i < numlines; i++) {
59 which_line = next;
60 next = arena[next].line[0];
61 if((w % write_cycle) != (write_cycle - 1)) {
62 sum += next;
63 }
64 else {
65 ((volatile cacheline_t*)arena)[which_line].line[0] = next;
66 }
67 }
68 }
69 return sum;
70}
71
72static cacheline_t* random_start(int wss)
73{
74 return arena + randrange(0, ((wss * 1024)/sizeof(cacheline_t)));
75}
76
77static volatile int dont_optimize_me = 0;
78
79static void usage(char *error) {
80 fprintf(stderr, "Error: %s\n", error);
81 fprintf(stderr,
82 "Usage:\n"
83 " rt_spin [COMMON-OPTS] WCET PERIOD DURATION\n"
84 " rt_spin [COMMON-OPTS] -f FILE [-o COLUMN] WCET PERIOD\n"
85 " rt_spin -l\n"
86 "\n"
87 "COMMON-OPTS = [-w] [-s SCALE]\n"
88 " [-p PARTITION/CLUSTER [-z CLUSTER SIZE]] [-m CRITICALITY LEVEL]\n"
89 " [-k WSS] [-l LOOPS] [-b BUDGET]\n"
90 "\n"
91 "WCET and PERIOD are milliseconds, DURATION is seconds.\n");
92 exit(EXIT_FAILURE);
93}
94
95static int loop_once(int wss)
96{
97 cacheline_t *mem;
98 int temp;
99
100 mem = random_start(wss);
101 temp = random_walk(mem, wss, 0);
102
103 dont_optimize_me = temp;
104
105 return dont_optimize_me;
106}
107
108static int job(int wss, double exec_time, double program_end)
109{
110 if (wctime() > program_end)
111 return 0;
112 else {
113 register unsigned int iter = 0;
114 register cycles_t t;
115 t = get_cycles();
116 while(iter++ < loops) {
117 loop_once(wss);
118 }
119 t = get_cycles() - t;
120 if (verbose)
121 printf("%ld cycles\n", t);
122 sleep_next_period();
123 return 1;
124 }
125}
126
127#define OPTSTR "p:wl:m:i:b:k:v"
128int main(int argc, char** argv)
129{
130 int ret;
131 lt_t wcet, period, budget;
132 double wcet_ms, period_ms, budget_ms;
133 unsigned int priority = LITMUS_NO_PRIORITY;
134 int migrate = 0;
135 int cluster = 0;
136 int opt;
137 int wait = 0;
138 double duration = 0, start = 0;
139 struct rt_task param;
140 struct mc2_task mc2_param;
141 struct reservation_config config;
142 int res_type = PERIODIC_POLLING;
143 size_t arena_sz;
144 int wss = 0;
145
146 /* default for reservation */
147 config.id = 0;
148 config.priority = LITMUS_NO_PRIORITY; /* use EDF by default */
149 config.cpu = -1;
150
151 mc2_param.crit = CRIT_LEVEL_C;
152
153 budget_ms = 1000;
154
155 while ((opt = getopt(argc, argv, OPTSTR)) != -1) {
156 switch (opt) {
157 case 'w':
158 wait = 1;
159 break;
160 case 'p':
161 cluster = atoi(optarg);
162 migrate = 1;
163 config.cpu = cluster;
164 break;
165 case 'l':
166 loops = atoi(optarg);
167 break;
168 case 'k':
169 wss = atoi(optarg);
170 break;
171 case 'm':
172 mc2_param.crit = atoi(optarg);
173 if ((mc2_param.crit >= CRIT_LEVEL_A) && (mc2_param.crit <= CRIT_LEVEL_C)) {
174 res_type = PERIODIC_POLLING;
175 }
176 else
177 usage("Invalid criticality level.");
178 break;
179 case 'b':
180 budget_ms = atof(optarg);
181 break;
182 case 'i':
183 config.priority = atoi(optarg);
184 break;
185 case 'v':
186 verbose = 1;
187 break;
188 case ':':
189 usage("Argument missing.");
190 break;
191 case '?':
192 default:
193 usage("Bad argument.");
194 break;
195 }
196 }
197 srand(getpid());
198
199 /*
200 * We need three parameters
201 */
202 if (argc - optind < 3)
203 usage("Arguments missing.");
204
205 wcet_ms = atof(argv[optind + 0]);
206 period_ms = atof(argv[optind + 1]);
207
208 wcet = ms2ns(wcet_ms);
209 period = ms2ns(period_ms);
210 budget = ms2ns(budget_ms);
211 if (wcet <= 0)
212 usage("The worst-case execution time must be a "
213 "positive number.");
214 if (period <= 0)
215 usage("The period must be a positive number.");
216 if (wcet > period) {
217 usage("The worst-case execution time must not "
218 "exceed the period.");
219 }
220 if (wss == 0) {
221 usage("You need to specify a WSS (-k option).");
222 }
223
224 duration = atof(argv[optind + 2]);
225
226 if (migrate) {
227 ret = be_migrate_to_domain(cluster);
228 if (ret < 0)
229 bail_out("could not migrate to target partition or cluster.");
230 }
231
232 /* reservation config */
233 config.id = gettid();
234 config.polling_params.budget = budget;
235 config.polling_params.period = period;
236 config.polling_params.offset = 0;
237 config.polling_params.relative_deadline = 0;
238
239 if (config.polling_params.budget > config.polling_params.period) {
240 usage("The budget must not exceed the period.");
241 }
242
243 /* create a reservation */
244 ret = reservation_create(res_type, &config);
245 if (ret < 0) {
246 bail_out("failed to create reservation.");
247 }
248
249 init_rt_task_param(&param);
250 param.exec_cost = wcet;
251 param.period = period;
252 param.priority = priority;
253 param.cls = RT_CLASS_HARD;
254 param.release_policy = TASK_PERIODIC;
255 param.budget_policy = NO_ENFORCEMENT;
256 if (migrate) {
257 param.cpu = gettid();
258 }
259 ret = set_rt_task_param(gettid(), &param);
260 if (ret < 0)
261 bail_out("could not setup rt task params");
262
263 mc2_param.res_id = gettid();
264 ret = set_mc2_task_param(gettid(), &mc2_param);
265 if (ret < 0)
266 bail_out("could not setup mc2 task params");
267
268 arena_sz = wss*1024;
269 arena = alloc_arena(arena_sz, 0, 0);
270 init_arena(arena, arena_sz);
271
272 lock_memory();
273
274 if (mc2_param.crit == CRIT_LEVEL_C)
275 set_page_color(-1);
276 else
277 set_page_color(config.cpu);
278
279 ret = init_litmus();
280 if (ret != 0)
281 bail_out("init_litmus() failed\n");
282
283 start = wctime();
284 ret = task_mode(LITMUS_RT_TASK);
285 if (ret != 0)
286 bail_out("could not become RT task");
287
288 if (wait) {
289 ret = wait_for_ts_release();
290 if (ret != 0)
291 bail_out("wait_for_ts_release()");
292 start = wctime();
293 }
294
295 while (job(wss, wcet_ms * 0.001, start + duration)) {};
296
297 ret = task_mode(BACKGROUND_TASK);
298 if (ret != 0)
299 bail_out("could not become regular task (huh?)");
300
301 reservation_destroy(gettid(), config.cpu);
302 dealloc_arena(arena, arena_sz);
303 return 0;
304}