diff options
Diffstat (limited to 'bin/rt_pointer_spin.c')
-rw-r--r-- | bin/rt_pointer_spin.c | 439 |
1 files changed, 0 insertions, 439 deletions
diff --git a/bin/rt_pointer_spin.c b/bin/rt_pointer_spin.c deleted file mode 100644 index 73e7b1d..0000000 --- a/bin/rt_pointer_spin.c +++ /dev/null | |||
@@ -1,439 +0,0 @@ | |||
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 | |||
12 | |||
13 | #include "litmus.h" | ||
14 | #include "common.h" | ||
15 | #include "DISstressmarkRNG.h" | ||
16 | |||
17 | #define MIN_FIELD_SIZE 16 | ||
18 | #define MAX_FIELD_SIZE 16777216 | ||
19 | #define MIN_WINDOW_SIZE 1 | ||
20 | #define MAX_WINDOW_SIZE 15 | ||
21 | #define MIN_HOP_LIMIT 1 | ||
22 | |||
23 | #define MAX_HOP_LIMIT 4294967295U | ||
24 | |||
25 | #define MIN_SEED -2147483647 | ||
26 | #define MAX_SEED -1 | ||
27 | #define MIN_THREADS 1 | ||
28 | #define MAX_THREADS 256 | ||
29 | |||
30 | static char* progname; | ||
31 | int loops = 1; | ||
32 | struct timeval t1, t2; | ||
33 | |||
34 | unsigned int *field; | ||
35 | unsigned int f; | ||
36 | unsigned short int w; | ||
37 | unsigned int maxhops; | ||
38 | int seed = -2; | ||
39 | unsigned int n; | ||
40 | |||
41 | clock_t startTime; | ||
42 | |||
43 | struct threadS{ | ||
44 | unsigned int initial; | ||
45 | unsigned int minStop; | ||
46 | unsigned int maxStop; | ||
47 | unsigned int hops; | ||
48 | }*thread; | ||
49 | |||
50 | unsigned int l; | ||
51 | |||
52 | int init_job() { | ||
53 | //fscanf(stdin, "%lu %u %lu %ld %u", | ||
54 | // &f, &l, &maxhops, &seed, &n); | ||
55 | |||
56 | f = 1048570; | ||
57 | l = 15; | ||
58 | maxhops = 5120000; | ||
59 | n = 1; | ||
60 | |||
61 | assert ((f >= MIN_FIELD_SIZE) && (f <= MAX_FIELD_SIZE)); | ||
62 | w = (unsigned int) l; | ||
63 | assert ((w >= MIN_WINDOW_SIZE) && (w <= MAX_WINDOW_SIZE)); | ||
64 | assert (w % 2 == 1); | ||
65 | assert (f > w); | ||
66 | assert ((maxhops >= MIN_HOP_LIMIT) && (maxhops <= MAX_HOP_LIMIT)); | ||
67 | assert ((seed >= MIN_SEED) && (seed <= MAX_SEED)); | ||
68 | |||
69 | assert ((n >= MIN_THREADS) && (n <= MAX_THREADS)); | ||
70 | if ((thread = (struct threadS *)malloc(n*sizeof(struct threadS))) == NULL) | ||
71 | return (-1); | ||
72 | |||
73 | /*for (l=0; l<n; l++){ | ||
74 | //fscanf(stdin, "%lu %lu %lu", | ||
75 | //&(thread[l].initial), &(thread[l].minStop), &(thread[l].maxStop)); | ||
76 | thread[l].initial = 10; | ||
77 | thread[l].minStop = f - thread[l].initial; | ||
78 | thread[l].maxStop = f - thread[l].initial; | ||
79 | |||
80 | assert ((thread[l].initial >= 0) && (thread[l].initial < f)); | ||
81 | assert ((thread[l].minStop >= 0) && (thread[l].minStop < f)); | ||
82 | assert ((thread[l].maxStop >= 0) && (thread[l].maxStop < f)); | ||
83 | } | ||
84 | */ | ||
85 | if ((field = (unsigned int *)malloc(f*sizeof(int))) == NULL) | ||
86 | return (-1); | ||
87 | |||
88 | //randInit(seed); | ||
89 | /* | ||
90 | for (l=0; l<f; l++){ | ||
91 | field[l] = randInt(0, f-w); | ||
92 | } | ||
93 | */ | ||
94 | return 0; | ||
95 | } | ||
96 | |||
97 | int main_job() { | ||
98 | f = randInt(16,65536); | ||
99 | w = randInt(0,7)*2 + 1; | ||
100 | maxhops = 32768; | ||
101 | n = 1; | ||
102 | for (l=0; l<n; l++){ | ||
103 | //fscanf(stdin, "%lu %lu %lu", | ||
104 | //&(thread[l].initial), &(thread[l].minStop), &(thread[l].maxStop)); | ||
105 | thread[l].initial = 10; | ||
106 | thread[l].minStop = f - thread[l].initial; | ||
107 | thread[l].maxStop = f - thread[l].initial; | ||
108 | |||
109 | assert ((thread[l].initial >= 0) && (thread[l].initial < f)); | ||
110 | assert ((thread[l].minStop >= 0) && (thread[l].minStop < f)); | ||
111 | assert ((thread[l].maxStop >= 0) && (thread[l].maxStop < f)); | ||
112 | } | ||
113 | |||
114 | for (l=0; l<f; l++){ | ||
115 | field[l] = randInt(0, f-w); | ||
116 | } | ||
117 | |||
118 | for (l=0; l<n; l++) { | ||
119 | unsigned int index; | ||
120 | unsigned int minStop, maxStop; | ||
121 | unsigned int hops; | ||
122 | |||
123 | hops = 0; | ||
124 | minStop = thread[l].minStop; | ||
125 | maxStop = thread[l].maxStop; | ||
126 | index = thread[l].initial; | ||
127 | while ((hops < maxhops) && | ||
128 | (!((index >= minStop) && | ||
129 | (index < maxStop)))){ | ||
130 | |||
131 | unsigned int ll, lll; | ||
132 | unsigned int max, min; | ||
133 | unsigned int partition; | ||
134 | unsigned int high; | ||
135 | |||
136 | partition = field[index]; | ||
137 | max = MAX_FIELD_SIZE; | ||
138 | min = 0; | ||
139 | high = 0; | ||
140 | |||
141 | for (ll=0; ll<w; ll++){ | ||
142 | unsigned int balance; | ||
143 | unsigned int x; | ||
144 | x = field[index+ll]; | ||
145 | |||
146 | if (x > max) high++; | ||
147 | else if (x > min){ /* start else* */ | ||
148 | partition = x; | ||
149 | balance = 0; | ||
150 | for (lll=ll+1; lll<w; lll++){ | ||
151 | if (field[index+lll] > partition) balance++; | ||
152 | }/* end for loop */ | ||
153 | |||
154 | if (balance+high == w/2) break; | ||
155 | else if (balance+high > w/2){ | ||
156 | min = partition; | ||
157 | }/* end if */ | ||
158 | else { | ||
159 | max = partition; | ||
160 | high++; | ||
161 | }/* end else */ | ||
162 | } | ||
163 | if (min == max) break; | ||
164 | } /* end else* */ | ||
165 | index = (partition+hops)%(f-w); | ||
166 | hops++; | ||
167 | }/* end loop ll */ | ||
168 | thread[l].hops = hops; | ||
169 | } /* end while */ | ||
170 | |||
171 | return(0); | ||
172 | } | ||
173 | |||
174 | int post_job() { | ||
175 | if (field) { | ||
176 | free(field); | ||
177 | field = NULL; | ||
178 | } | ||
179 | if (thread) { | ||
180 | free(thread); | ||
181 | thread = NULL; | ||
182 | } | ||
183 | return 0; | ||
184 | } | ||
185 | |||
186 | static void usage(char *error) { | ||
187 | fprintf(stderr, "Error: %s\n", error); | ||
188 | fprintf(stderr, | ||
189 | "Usage:\n" | ||
190 | " rt_spin [COMMON-OPTS] WCET PERIOD DURATION\n" | ||
191 | " rt_spin [COMMON-OPTS] -f FILE [-o COLUMN] WCET PERIOD\n" | ||
192 | " rt_spin -l\n" | ||
193 | "\n" | ||
194 | "COMMON-OPTS = [-w] [-s SCALE]\n" | ||
195 | " [-p PARTITION/CLUSTER [-z CLUSTER SIZE]] [-c CLASS] [-m CRITICALITY LEVEL]\n" | ||
196 | "\n" | ||
197 | "WCET and PERIOD are microseconds, DURATION is seconds.\n"); | ||
198 | exit(EXIT_FAILURE); | ||
199 | } | ||
200 | |||
201 | inline unsigned long get_cyclecount (void) | ||
202 | { | ||
203 | unsigned long value; | ||
204 | // Read CCNT Register | ||
205 | asm volatile ("MRC p15, 0, %0, c9, c13, 0\t\n": "=r"(value)); | ||
206 | return value; | ||
207 | } | ||
208 | |||
209 | static int loop_main(double exec_time, double emergency_exit) | ||
210 | { | ||
211 | double last_loop = 0, loop_start; | ||
212 | int tmp = 0; | ||
213 | |||
214 | double start = cputime(); | ||
215 | double now = cputime(); | ||
216 | |||
217 | while (now + last_loop < start + exec_time) { | ||
218 | loop_start = now; | ||
219 | tmp += main_job(); | ||
220 | now = cputime(); | ||
221 | last_loop = now - loop_start; | ||
222 | if (emergency_exit && wctime() > emergency_exit) { | ||
223 | /* Oops --- this should only be possible if the execution time tracking | ||
224 | * is broken in the LITMUS^RT kernel. */ | ||
225 | fprintf(stderr, "!!! rtspin/%d emergency exit!\n", getpid()); | ||
226 | fprintf(stderr, "Something is seriously wrong! Do not ignore this.\n"); | ||
227 | break; | ||
228 | } | ||
229 | } | ||
230 | |||
231 | return tmp; | ||
232 | } | ||
233 | |||
234 | static int job(double exec_time, double program_end) | ||
235 | { | ||
236 | if (wctime() > program_end) | ||
237 | return 0; | ||
238 | else { | ||
239 | register int iter = 0; | ||
240 | //register unsigned long t; | ||
241 | //t = get_cyclecount(); | ||
242 | //init_job(); | ||
243 | //gettimeofday(&t1, NULL); | ||
244 | loop_main(exec_time, program_end + 1); | ||
245 | //t = get_cyclecount() - t; | ||
246 | //printf("%ld cycles\n", t); | ||
247 | //gettimeofday(&t2, NULL); | ||
248 | //printf("%ld\n", ((t2.tv_sec * 1000000 + t2.tv_usec) - (t1.tv_sec * 1000000 + t1.tv_usec))); | ||
249 | //post_job(); | ||
250 | sleep_next_period(); | ||
251 | return 1; | ||
252 | } | ||
253 | } | ||
254 | |||
255 | #define OPTSTR "p:wves:l:m:i:b:" | ||
256 | int main(int argc, char** argv) | ||
257 | { | ||
258 | int ret; | ||
259 | lt_t wcet; | ||
260 | lt_t period; | ||
261 | lt_t budget; | ||
262 | double wcet_ms, period_ms, budget_ms; | ||
263 | unsigned int priority = LITMUS_NO_PRIORITY; | ||
264 | int migrate = 0; | ||
265 | int cluster = 0; | ||
266 | int opt; | ||
267 | int wait = 0; | ||
268 | int want_enforcement = 0; | ||
269 | double duration = 0, start = 0; | ||
270 | double scale = 1.0; | ||
271 | task_class_t class = RT_CLASS_HARD; | ||
272 | struct rt_task param; | ||
273 | struct mc2_task mc2_param; | ||
274 | struct reservation_config config; | ||
275 | int res_type = PERIODIC_POLLING; | ||
276 | |||
277 | progname = argv[0]; | ||
278 | |||
279 | /* default for reservation */ | ||
280 | config.id = 0; | ||
281 | config.priority = LITMUS_NO_PRIORITY; /* use EDF by default */ | ||
282 | config.cpu = -1; | ||
283 | |||
284 | mc2_param.crit = CRIT_LEVEL_C; | ||
285 | |||
286 | budget_ms = 10; | ||
287 | |||
288 | while ((opt = getopt(argc, argv, OPTSTR)) != -1) { | ||
289 | switch (opt) { | ||
290 | case 'w': | ||
291 | wait = 1; | ||
292 | break; | ||
293 | case 'p': | ||
294 | cluster = atoi(optarg); | ||
295 | migrate = 1; | ||
296 | config.cpu = cluster; | ||
297 | break; | ||
298 | case 'e': | ||
299 | want_enforcement = 1; | ||
300 | break; | ||
301 | case 's': | ||
302 | scale = atof(optarg); | ||
303 | break; | ||
304 | case 'l': | ||
305 | loops = atoi(optarg); | ||
306 | break; | ||
307 | case 'm': | ||
308 | mc2_param.crit = atoi(optarg); | ||
309 | if (mc2_param.crit < CRIT_LEVEL_A || mc2_param.crit == NUM_CRIT_LEVELS) { | ||
310 | usage("Invalid criticality level."); | ||
311 | } | ||
312 | res_type = PERIODIC_POLLING; | ||
313 | break; | ||
314 | case 'b': | ||
315 | budget_ms = atof(optarg); | ||
316 | break; | ||
317 | case 'i': | ||
318 | config.priority = atoi(optarg); | ||
319 | break; | ||
320 | case ':': | ||
321 | usage("Argument missing."); | ||
322 | break; | ||
323 | case '?': | ||
324 | default: | ||
325 | usage("Bad argument."); | ||
326 | break; | ||
327 | } | ||
328 | } | ||
329 | |||
330 | if (mc2_param.crit > CRIT_LEVEL_A && config.priority != LITMUS_NO_PRIORITY) | ||
331 | usage("Bad criticailty level or priority"); | ||
332 | |||
333 | if (argc - optind < 3) | ||
334 | usage("Arguments missing."); | ||
335 | |||
336 | wcet_ms = atof(argv[optind + 0]); | ||
337 | period_ms = atof(argv[optind + 1]); | ||
338 | |||
339 | wcet = ms2ns(wcet_ms); | ||
340 | period = ms2ns(period_ms); | ||
341 | budget = ms2ns(budget_ms); | ||
342 | |||
343 | if (wcet <= 0) | ||
344 | usage("The worst-case execution time must be a " | ||
345 | "positive number."); | ||
346 | if (period <= 0) | ||
347 | usage("The period must be a positive number."); | ||
348 | if (wcet > period) { | ||
349 | usage("The worst-case execution time must not " | ||
350 | "exceed the period."); | ||
351 | } | ||
352 | |||
353 | duration = atof(argv[optind + 2]); | ||
354 | |||
355 | if (migrate) { | ||
356 | ret = be_migrate_to_domain(cluster); | ||
357 | if (ret < 0) | ||
358 | bail_out("could not migrate to target partition or cluster."); | ||
359 | } | ||
360 | |||
361 | /* reservation config */ | ||
362 | config.id = gettid(); | ||
363 | |||
364 | config.polling_params.budget = budget; | ||
365 | config.polling_params.period = period; | ||
366 | config.polling_params.offset = 0; | ||
367 | config.polling_params.relative_deadline = 0; | ||
368 | if (config.polling_params.budget > config.polling_params.period) { | ||
369 | usage("The budget must not exceed the period."); | ||
370 | } | ||
371 | |||
372 | /* create a reservation */ | ||
373 | ret = reservation_create(res_type, &config); | ||
374 | if (ret < 0) { | ||
375 | bail_out("failed to create reservation."); | ||
376 | } | ||
377 | //srand (time(NULL)); | ||
378 | //randInit((-rand()%65535)-2); | ||
379 | randInit(-2); | ||
380 | |||
381 | init_job(); | ||
382 | |||
383 | init_rt_task_param(¶m); | ||
384 | param.exec_cost = wcet; | ||
385 | param.period = period; | ||
386 | param.priority = priority; | ||
387 | param.cls = class; | ||
388 | param.release_policy = TASK_PERIODIC; | ||
389 | param.budget_policy = (want_enforcement) ? | ||
390 | PRECISE_ENFORCEMENT : NO_ENFORCEMENT; | ||
391 | if (migrate) { | ||
392 | param.cpu = gettid(); | ||
393 | } | ||
394 | ret = set_rt_task_param(gettid(), ¶m); | ||
395 | |||
396 | if (ret < 0) | ||
397 | bail_out("could not setup rt task params"); | ||
398 | |||
399 | mc2_param.res_id = gettid(); | ||
400 | ret = set_mc2_task_param(gettid(), &mc2_param); | ||
401 | //printf("SET_MC2_TASK\n"); | ||
402 | if (ret < 0) | ||
403 | bail_out("could not setup mc2 task params"); | ||
404 | |||
405 | init_litmus(); | ||
406 | //printf("CALL\n"); | ||
407 | if (mc2_param.crit == CRIT_LEVEL_C) | ||
408 | set_page_color(-1); | ||
409 | else if (mc2_param.crit < CRIT_LEVEL_C) | ||
410 | set_page_color(config.cpu); | ||
411 | //printf("CALL\n"); | ||
412 | |||
413 | //printf("INIT_LITMUS\n"); | ||
414 | start = wctime(); | ||
415 | ret = task_mode(LITMUS_RT_TASK); | ||
416 | //printf("TASK_MODE\n"); | ||
417 | if (ret != 0) | ||
418 | bail_out("could not become RT task"); | ||
419 | |||
420 | |||
421 | if (wait) { | ||
422 | //printf("BEFORE WAIT\n"); | ||
423 | ret = wait_for_ts_release(); | ||
424 | if (ret != 0) | ||
425 | bail_out("wait_for_ts_release()"); | ||
426 | start = wctime(); | ||
427 | } | ||
428 | |||
429 | while (job(wcet_ms * 0.001 * scale, start + duration)) {}; | ||
430 | |||
431 | ret = task_mode(BACKGROUND_TASK); | ||
432 | if (ret != 0) | ||
433 | bail_out("could not become regular task (huh?)"); | ||
434 | |||
435 | reservation_destroy(gettid(), config.cpu); | ||
436 | post_job(); | ||
437 | //printf("%s/%d finished.\n",progname, gettid()); | ||
438 | return 0; | ||
439 | } | ||