aboutsummaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorNamhoon Kim <namhoonk@cs.unc.edu>2016-10-13 16:27:40 -0400
committerNamhoon Kim <namhoonk@cs.unc.edu>2016-10-13 16:27:40 -0400
commitb6ef12e31351d23a439597162f8d0fa7031b771d (patch)
tree9e1c36eabaef6def1df0c0ffe56db608f0c5852c /bin
parent262677fc436c3e2109e628bb2f4708c6b279a551 (diff)
RTAS 2017 submissionwip-shared-lib
Diffstat (limited to 'bin')
-rw-r--r--bin/mc2mem.c304
-rw-r--r--bin/mc2spin.c250
2 files changed, 554 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}
diff --git a/bin/mc2spin.c b/bin/mc2spin.c
new file mode 100644
index 0000000..e41c62c
--- /dev/null
+++ b/bin/mc2spin.c
@@ -0,0 +1,250 @@
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
17#define PAGE_SIZE (4096)
18
19static int verbose = 0;
20
21#define UNCACHE_DEV "/dev/litmus/uncache"
22
23static void usage(char *error) {
24 fprintf(stderr, "Error: %s\n", error);
25 fprintf(stderr,
26 "Usage:\n"
27 " rt_spin [COMMON-OPTS] WCET PERIOD DURATION\n"
28 " rt_spin [COMMON-OPTS] -f FILE [-o COLUMN] WCET PERIOD\n"
29 " rt_spin -l\n"
30 "\n"
31 "COMMON-OPTS = [-w] [-s SCALE]\n"
32 " [-p PARTITION/CLUSTER [-z CLUSTER SIZE]] [-m CRITICALITY LEVEL]\n"
33 " [-k WSS] [-l LOOPS] [-b BUDGET]\n"
34 "\n"
35 "WCET and PERIOD are milliseconds, DURATION is seconds.\n");
36 exit(EXIT_FAILURE);
37}
38
39#define NUMS 4096
40static int num[NUMS];
41static char* progname;
42
43static int loop_once(void)
44{
45 int i, j = 0;
46 for (i = 0; i < NUMS; i++)
47 j += num[i]++;
48 return j;
49}
50
51static int loop_for(double exec_time, double emergency_exit)
52{
53 double last_loop = 0, loop_start;
54 int tmp = 0;
55
56 double start = cputime();
57 double now = cputime();
58
59 while (now + last_loop < start + exec_time) {
60 loop_start = now;
61 tmp += loop_once();
62 now = cputime();
63 last_loop = now - loop_start;
64 if (emergency_exit && wctime() > emergency_exit) {
65 /* Oops --- this should only be possible if the execution time tracking
66 * is broken in the LITMUS^RT kernel. */
67 fprintf(stderr, "!!! rtspin/%d emergency exit!\n", getpid());
68 fprintf(stderr, "Something is seriously wrong! Do not ignore this.\n");
69 break;
70 }
71 }
72
73 return tmp;
74}
75
76static int job(double exec_time, double program_end)
77{
78 if (wctime() > program_end)
79 return 0;
80 else {
81 loop_for(exec_time, program_end + 1);
82 sleep_next_period();
83 return 1;
84 }
85}
86
87#define OPTSTR "p:wm:i:v"
88int main(int argc, char** argv)
89{
90 int ret;
91 lt_t wcet, period, budget;
92 double wcet_ms, period_ms;
93 unsigned int priority = LITMUS_NO_PRIORITY;
94 int migrate = 0;
95 int cluster = 0;
96 int opt;
97 int wait = 0;
98 double duration = 0, start = 0;
99 struct rt_task param;
100 struct mc2_task mc2_param;
101 struct reservation_config config;
102 int res_type = PERIODIC_POLLING;
103
104 /* default for reservation */
105 config.id = 0;
106 config.priority = LITMUS_NO_PRIORITY; /* use EDF by default */
107 config.cpu = -1;
108
109 mc2_param.crit = CRIT_LEVEL_C;
110
111 progname = argv[0];
112
113 while ((opt = getopt(argc, argv, OPTSTR)) != -1) {
114 switch (opt) {
115 case 'w':
116 wait = 1;
117 break;
118 case 'p':
119 cluster = atoi(optarg);
120 migrate = 1;
121 config.cpu = cluster;
122 break;
123 case 'm':
124 mc2_param.crit = atoi(optarg);
125 if ((mc2_param.crit >= CRIT_LEVEL_A) && (mc2_param.crit <= CRIT_LEVEL_C)) {
126 res_type = PERIODIC_POLLING;
127 }
128 else
129 usage("Invalid criticality level.");
130 break;
131 case 'i':
132 config.priority = atoi(optarg);
133 break;
134 case 'v':
135 verbose = 1;
136 break;
137 case ':':
138 usage("Argument missing.");
139 break;
140 case '?':
141 default:
142 usage("Bad argument.");
143 break;
144 }
145 }
146 srand(getpid());
147
148 /*
149 * We need three parameters
150 */
151 if (argc - optind < 3)
152 usage("Arguments missing.");
153
154 wcet_ms = atof(argv[optind + 0]);
155 period_ms = atof(argv[optind + 1]);
156
157 wcet = ms2ns(wcet_ms);
158 period = ms2ns(period_ms);
159 budget = ms2ns(wcet_ms+1);
160 if (wcet <= 0)
161 usage("The worst-case execution time must be a "
162 "positive number.");
163 if (period <= 0)
164 usage("The period must be a positive number.");
165 if (wcet > period) {
166 usage("The worst-case execution time must not "
167 "exceed the period.");
168 }
169
170 duration = atof(argv[optind + 2]);
171
172 if (mc2_param.crit == CRIT_LEVEL_C) {
173 migrate = 0;
174 config.cpu = -1;
175 }
176
177 if (migrate) {
178 ret = be_migrate_to_domain(cluster);
179 if (ret < 0)
180 bail_out("could not migrate to target partition or cluster.");
181 }
182
183 /* reservation config */
184 config.id = gettid();
185 config.polling_params.budget = budget;
186 config.polling_params.period = period;
187 config.polling_params.offset = 0;
188 config.polling_params.relative_deadline = 0;
189
190 if (config.polling_params.budget > config.polling_params.period) {
191 usage("The budget must not exceed the period.");
192 }
193
194 /* create a reservation */
195 ret = reservation_create(res_type, &config);
196 if (ret < 0) {
197 bail_out("failed to create reservation.");
198 }
199
200 init_rt_task_param(&param);
201 param.exec_cost = wcet;
202 param.period = period;
203 param.priority = priority;
204 param.cls = RT_CLASS_HARD;
205 param.release_policy = TASK_PERIODIC;
206 param.budget_policy = NO_ENFORCEMENT;
207 if (migrate) {
208 param.cpu = gettid();
209 }
210 ret = set_rt_task_param(gettid(), &param);
211 if (ret < 0)
212 bail_out("could not setup rt task params");
213
214 mc2_param.res_id = gettid();
215 ret = set_mc2_task_param(gettid(), &mc2_param);
216 if (ret < 0)
217 bail_out("could not setup mc2 task params");
218
219 ret = init_litmus();
220 if (ret != 0)
221 bail_out("init_litmus() failed\n");
222
223 start = wctime();
224
225 ret = task_mode(LITMUS_RT_TASK);
226 if (ret != 0)
227 bail_out("could not become RT task");
228
229 if (wait) {
230 ret = wait_for_ts_release();
231 if (ret != 0)
232 bail_out("wait_for_ts_release()");
233 start = wctime();
234 }
235
236 do {
237 if (verbose) {
238 printf("rtspin:%u @ %.4fms\n", gettid(),
239 (wctime() - start) * 1000);
240 }
241 } while (job(wcet_ms * 0.001, start + duration));
242
243 ret = task_mode(BACKGROUND_TASK);
244 if (ret != 0)
245 bail_out("could not become regular task (huh?)");
246
247 reservation_destroy(gettid(), config.cpu);
248 return 0;
249}
250