summaryrefslogtreecommitdiffstats
path: root/extra.h
diff options
context:
space:
mode:
Diffstat (limited to 'extra.h')
-rw-r--r--extra.h261
1 files changed, 134 insertions, 127 deletions
diff --git a/extra.h b/extra.h
index a15313a..3f1df9d 100644
--- a/extra.h
+++ b/extra.h
@@ -29,8 +29,8 @@ extern int sched_getcpu();
29#include <sys/syscall.h> 29#include <sys/syscall.h>
30#endif 30#endif
31 31
32#define LITMUS 0 32// This is a proxy for "case study mode" now
33#define MC2 0 33#define LITMUS 1
34#define MMDC_PROF 0 34#define MMDC_PROF 0
35 35
36#if LITMUS 36#if LITMUS
@@ -41,18 +41,6 @@ extern int sched_getcpu();
41#include "/media/speedy/litmus/tools/mmdc/mmdc.h" 41#include "/media/speedy/litmus/tools/mmdc/mmdc.h"
42#endif 42#endif
43 43
44#if LITMUS
45#define SET_UP LOAD_PARAMS SETUP_LITMUS
46#else
47#define SET_UP LOAD_PARAMS
48#endif
49
50#if MMDC_PROF
51#define LOAD_PARAMS LOAD_PARAMS_ITRL SETUP_MMDC
52#else
53#define LOAD_PARAMS LOAD_PARAMS_ITRL
54#endif
55
56// Store state globally so that the job can be outside main() 44// Store state globally so that the job can be outside main()
57// Arrays use float as a comprimise between overflow and size 45// Arrays use float as a comprimise between overflow and size
58// Paired arrays use long longs as precision is more important for those times 46// Paired arrays use long longs as precision is more important for those times
@@ -82,50 +70,72 @@ char *_rt_other_core;
82char *_rt_barrier; 70char *_rt_barrier;
83sem_t *_rt_first_sem, *_rt_second_sem; 71sem_t *_rt_first_sem, *_rt_second_sem;
84int _rt_lock_id; 72int _rt_lock_id;
73#define _ID_SZ 128
74char _rt_sem1_name[_ID_SZ] = "/_libextra_first_sem-";
75char _rt_sem2_name[_ID_SZ] = "/_libextra_second_sem-";
76char _rt_shm_name[_ID_SZ] = "/_libextra_barrier-";
77#endif /* PAIRED */
78
79#if LITMUS
80long unsigned int _rt_period;
85#endif 81#endif
86 82
87static void _rt_load_params_itrl(int argc, char **argv) { 83static void _rt_load_params_itrl(int argc, char **argv) {
88#ifdef PAIRED 84#ifdef PAIRED
89 if (argc != 8) { 85 if (argc != (8 + LITMUS*2) && argc != (9 + LITMUS*2)) {
90 fprintf(stderr, "Usage: %s <name> <loops> <my core> <other core> <other name> <runID> <lockID>", argv[0]); 86 fprintf(stderr, "Usage: %s <name> <loops> <my core> <other core> <other name> <runID> <save results?>", argv[0]);
91 fprintf(stderr, " <name> string for logging. Name of this task.\n");
92 fprintf(stderr, " <loops> integer number of iterations. -1 for infinite.\n");
93 fprintf(stderr, " <my core> UNUSED. Core is now auto-detected.\n");
94 fprintf(stderr, " <other core> integer for logging. Core of paired task.\n");
95 fprintf(stderr, " <other name> string for logging. Name of paired task.\n");
96 fprintf(stderr, " <runID> string to append with .txt to yield output file name.\n");
97 fprintf(stderr, " <lockID> 1 to indicate this is pair member 1, otherwise pair member 2.\n");
98 exit(1);
99 }
100#else 87#else
101 if (argc != 6) { 88 if (argc != (6 + LITMUS*2)) {
102 fprintf(stderr, "Usage: %s <name> <loops> <my core> <runID> <save results?>\n", argv[0]); 89 fprintf(stderr, "Usage: %s <name> <loops> <my core> <runID> <save results?>\n", argv[0]);
90#endif /* PAIRED */
103 fprintf(stderr, " <name> string for logging. Name of this task.\n"); 91 fprintf(stderr, " <name> string for logging. Name of this task.\n");
104 fprintf(stderr, " <loops> integer number of iterations. -1 for infinite.\n"); 92 fprintf(stderr, " <loops> integer number of iterations. -1 for infinite.\n");
105 fprintf(stderr, " <my core> UNUSED. Core is now auto-detected.\n"); 93 fprintf(stderr, " <my core> integer core number. Only used for LITMUS-RT.\n");
94#ifdef PAIRED
95 fprintf(stderr, " <other core> integer for logging. Core of paired task.\n");
96 fprintf(stderr, " <other name> string for logging. Name of paired task.\n");
97#endif /* PAIRED */
106 fprintf(stderr, " <runID> string to append with .txt to yield output file name.\n"); 98 fprintf(stderr, " <runID> string to append with .txt to yield output file name.\n");
107 fprintf(stderr, " <save results?> 1 to save results, 0 to discard.\n"); 99 fprintf(stderr, " <save results?> 1 to save results, 0 to discard.\n");
100#ifdef PAIRED
101 fprintf(stderr, " <pairID> (optional).\n");
102#endif
103#if LITMUS
104 fprintf(stderr, " <task period> in ms\n");
105 fprintf(stderr, " <task criticality level> 0 for Level-A, 1 for Level-B, 2 for Level-C\n");
106#endif /* LITMUS */
108 exit(1); 107 exit(1);
109 } 108 }
110#endif
111 _rt_our_prog_name = argv[1]; 109 _rt_our_prog_name = argv[1];
112 _rt_max_jobs = atol(argv[2]); 110 _rt_max_jobs = atol(argv[2]);
111#if !LITMUS
113 _rt_core = sched_getcpu(); 112 _rt_core = sched_getcpu();
113#else
114 _rt_core = atoi(argv[3]);
115#endif
114#ifdef PAIRED 116#ifdef PAIRED
115 _rt_other_core = argv[4]; 117 _rt_other_core = argv[4];
116 _rt_other_prog_name = argv[5]; 118 _rt_other_prog_name = argv[5];
117 _rt_run_id = argv[6]; 119 _rt_run_id = argv[6];
118 _rt_lock_id = atoi(argv[7]); 120 _rt_will_output = atoi(argv[7]);
119 // The paired version doesn't support disabling output (legacy compatibility) 121 char *pairId;
120 _rt_will_output = 1; 122 int end;
123 if (argc > 8) {
124 pairId = argv[8];
125 end = 8;
126 } else {
127 pairId = "none";
128 end = 9;
129 }
121#else 130#else
122 _rt_other_core = "none"; 131 _rt_other_core = "none";
123 _rt_other_prog_name = "none"; 132 _rt_other_prog_name = "none";
124 _rt_run_id = argv[4]; 133 _rt_run_id = argv[4];
125 _rt_will_output = atoi(argv[5]); 134 _rt_will_output = atoi(argv[5]);
135 int end = 6;
126#endif /* PAIRED */ 136#endif /* PAIRED */
127 if (_rt_max_jobs < 0 && _rt_will_output != 0) { 137 if (_rt_max_jobs < 0 && _rt_will_output != 0) {
128 fprintf(stderr, "Infinite loops only supported when _rt_will_output is disabled!\n"); 138 fprintf(stderr, "Infinite loops only supported when output is disabled!\n");
129 exit(1); 139 exit(1);
130 } 140 }
131 if (strlen(_rt_run_id) + 5 > _RT_FILENAME_LEN) { 141 if (strlen(_rt_run_id) + 5 > _RT_FILENAME_LEN) {
@@ -133,32 +143,54 @@ static void _rt_load_params_itrl(int argc, char **argv) {
133 exit(1); 143 exit(1);
134 } 144 }
135#ifdef PAIRED 145#ifdef PAIRED
146 // __rt_sem2_name happens to be the longest
147 if (strlen(pairId) + strlen(_rt_sem2_name) > _ID_SZ) {
148 fprintf(stderr, "PairID is too long! Maximum length is %ld characters.\n", _ID_SZ - strlen(_rt_sem2_name));
149 exit(1);
150 }
136 _rt_start_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long)); 151 _rt_start_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long));
137 _rt_end_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long)); 152 _rt_end_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long));
138 if (!_rt_end_time || !_rt_start_time) { 153 if (!_rt_end_time || !_rt_start_time) {
139 perror("Unable to allocate buffers for execution times"); 154 perror("Unable to allocate buffers for execution times");
140 exit(1); 155 exit(1);
141 } 156 }
142 _rt_first_sem = sem_open("/_libextra_first_sem", O_CREAT, 644, 0); 157 // Use PairID to create unique semaphore and shared memory paths
143 _rt_second_sem = sem_open("/_libextra_second_sem", O_CREAT, 644, 0); 158 strcat(_rt_sem1_name, pairId);
159 strcat(_rt_sem2_name, pairId);
160 strcat(_rt_shm_name, pairId);
161 _rt_first_sem = sem_open(_rt_sem1_name, O_CREAT, 644, 0);
162 _rt_second_sem = sem_open(_rt_sem2_name, O_CREAT, 644, 0);
144 if (_rt_first_sem == SEM_FAILED || _rt_second_sem == SEM_FAILED) { 163 if (_rt_first_sem == SEM_FAILED || _rt_second_sem == SEM_FAILED) {
145 perror("Error while creating semaphores"); 164 perror("Error while creating semaphores");
146 exit(1); 165 exit(1);
147 } 166 }
148 int barrier_file = shm_open("/_libextra_barrier", O_CREAT | O_RDWR, 644); 167 // Create shared memory for barrier synchronization and infer lock ID
168 int barrier_file = shm_open(_rt_shm_name, O_CREAT | O_RDWR | O_EXCL, 644);
169 if (barrier_file == -1) {
170 // File already existed - we're the 2nd program and thus lock ID 2
171 _rt_lock_id = 2;
172 barrier_file = shm_open(_rt_shm_name, O_CREAT | O_RDWR, 644);
173 } else {
174 _rt_lock_id = 1;
175 }
149 if (barrier_file == -1) { 176 if (barrier_file == -1) {
150 perror("Error while creating shared memory for barrier synchronization"); 177 perror("Error while creating shared memory for barrier synchronization");
151 exit(1); 178 exit(1);
152 } 179 }
153 if (ftruncate(barrier_file, 1) == -1) { 180 if (ftruncate(barrier_file, 2) == -1) {
154 perror("Error while setting size of shared memory for barrier synchronization"); 181 perror("Error while setting size of shared memory for barrier synchronization");
155 exit(1); 182 exit(1);
156 } 183 }
157 _rt_barrier = mmap(NULL, 1, PROT_WRITE, MAP_SHARED, barrier_file, 0); 184 _rt_barrier = mmap(NULL, 2, PROT_WRITE, MAP_SHARED, barrier_file, 0);
158 if (_rt_barrier == MAP_FAILED) { 185 if (_rt_barrier == MAP_FAILED) {
159 perror("Error while mapping shared memory for barrier synchronization"); 186 perror("Error while mapping shared memory for barrier synchronization");
160 exit(1); 187 exit(1);
161 } 188 }
189 // If we're the 2nd user of this barrier, mark it as in-use
190 if (_rt_lock_id == 2 && !__sync_bool_compare_and_swap(_rt_barrier+1, 0, 1)) {
191 fprintf(stderr, "Pair ID already in use!\n");
192 exit(1);
193 }
162 *_rt_barrier = 0; 194 *_rt_barrier = 0;
163#else 195#else
164 _rt_exec_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(float)); 196 _rt_exec_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(float));
@@ -169,8 +201,46 @@ static void _rt_load_params_itrl(int argc, char **argv) {
169#endif /* PAIRED */ 201#endif /* PAIRED */
170 _rt_jobs_complete = 0; 202 _rt_jobs_complete = 0;
171 mlockall(MCL_CURRENT || MCL_FUTURE); 203 mlockall(MCL_CURRENT || MCL_FUTURE);
204#if LITMUS
205 _rt_period = strtoul(argv[end], NULL, 10);
206 unsigned int crit = atoi(argv[end+1]);
207 unsigned int wait = 1;
208 if (be_migrate_to_domain(_rt_core) < 0) {
209 perror("Unable to migrate to specified CPU");
210 exit(1);
211 }
212 struct rt_task rt_param;
213 init_rt_task_param(&rt_param);
214 rt_param.exec_cost = 0; // We disable budget enforcement, so this doesn't matter
215 rt_param.period = ms2ns(_rt_period);
216 rt_param.relative_deadline = 0;
217 rt_param.phase = 0;
218 rt_param.priority = LITMUS_LOWEST_PRIORITY;
219 rt_param.cls = crit;
220 rt_param.release_policy = TASK_PERIODIC;
221 rt_param.budget_policy = NO_ENFORCEMENT;
222 rt_param.cpu = _rt_core;
223 if (set_rt_task_param(gettid(), &rt_param) < 0) {
224 perror("Unable to set real-time parameters");
225 exit(1);
226 }
227 if (init_litmus() != 0) {
228 perror("init_litmus failed");
229 exit(1);
230 }
231 if (task_mode(LITMUS_RT_TASK) != 0) {
232 perror("Unable to become real-time task");
233 exit(1);
234 }
235 if (wait && wait_for_ts_release() != 0) {
236 perror("Unable to wait for taskset release");
237 exit(1);
238 }
239#endif /* LITMUS */
240#if MMDC_PROF
241 SETUP_MMDC
242#endif
172} 243}
173#define LOAD_PARAMS_ITRL _rt_load_params_itrl(argc, argv);
174 244
175#define SETUP_MMDC \ 245#define SETUP_MMDC \
176 _rt_mmdc_read = calloc(_rt_max_jobs * _rt_will_output, sizeof(float));\ 246 _rt_mmdc_read = calloc(_rt_max_jobs * _rt_will_output, sizeof(float));\
@@ -194,75 +264,6 @@ static void _rt_load_params_itrl(int argc, char **argv) {
194 mmdc->madpcr1 = axi_arm1;\ 264 mmdc->madpcr1 = axi_arm1;\
195 msync(&(mmdc->madpcr1),4,MS_SYNC); 265 msync(&(mmdc->madpcr1),4,MS_SYNC);
196 266
197#define SETUP_LITMUS \
198 unsigned int wait = 0; \
199 if (be_migrate_to_domain(_rt_core) < 0) { \
200 perror("Unable to migrate to specified CPU"); \
201 exit(1); \
202 } \
203 struct reservation_config res; \
204 res.id = gettid(); \
205 res.cpu = cpu; \
206 res.priority = LITMUS_HIGHEST_PRIORITY; \
207 /* we take over half the CPU time (these are ns) */ \
208 res.polling_params.budget = ms2ns(3000); \
209 res.polling_params.period = ms2ns(3000); \
210 res.polling_params.offset = 0; \
211 res.polling_params.relative_deadline = ms2ns(3000); \
212 /* Not 100% sure that we should use periodic polling */ \
213 if (reservation_create(PERIODIC_POLLING, &res) < 0) { \
214 perror("Unable to create reservation"); \
215 exit(1); \
216 } \
217 struct rt_task rt_param; \
218 init_rt_task_param(&rt_param); \
219 /* Supposedly the next two parameters are irrelevant when reservations are enabled, but I'm leaving them anyway... */ \
220 rt_param.exec_cost = ms2ns(999); \
221 rt_param.period = ms2ns(1000); \
222 rt_param.priority = LITMUS_HIGHEST_PRIORITY; \
223 rt_param.cls = RT_CLASS_HARD; \
224 rt_param.release_policy = TASK_PERIODIC; \
225 rt_param.budget_policy = NO_ENFORCEMENT; \
226 rt_param.cpu = cpu; \
227 if (set_rt_task_param(gettid(), &rt_param) < 0) { \
228 perror("Unable to set real-time parameters"); \
229 exit(1); \
230 } \
231 if (init_litmus() != 0) { \
232 perror("init_litmus failed"); \
233 exit(1); \
234 } \
235 MC2_SETUP \
236 if (task_mode(LITMUS_RT_TASK) != 0) { \
237 perror("Unable to become real-time task"); \
238 exit(1); \
239 } \
240 if (wait && wait_for_ts_release() != 0) { \
241 perror("Unable to wait for taskset release"); \
242 exit(1); \
243 }
244
245#if MC2
246#define MC2_SETUP \
247 struct mc2_task mc2_param; \
248 mc2_param.res_id = gettid(); \
249 mc2_param.crit = CRIT_LEVEL_A; \
250 if (set_mc2_task_param(gettid(), &mc2_param) < 0) { \
251 perror("Unable to set MC^2 task params"); \
252 exit(1); \
253 } \
254 set_page_color(rt_param.cpu);
255#else
256#define MC2_SETUP
257#endif
258
259#define CLEANUP_LITMUS \
260 if (task_mode(BACKGROUND_TASK) != 0) { \
261 perror("Unable to become a real-time task"); \
262 exit(1); \
263 } \
264 reservation_destroy(gettid(), rt_param.cpu);
265
266#if __arm__ 267#if __arm__
267// On ARM, manually flush the cache 268// On ARM, manually flush the cache
268#define FLUSH_CACHES \ 269#define FLUSH_CACHES \
@@ -326,28 +327,28 @@ static void _rt_load_params_itrl(int argc, char **argv) {
326 327
327// Buffer timing result from a single job 328// Buffer timing result from a single job
328static void _rt_save_job_result() { 329static void _rt_save_job_result() {
330 if (!_rt_will_output)
331 return;
329 if (_rt_jobs_complete >= _rt_max_jobs) { 332 if (_rt_jobs_complete >= _rt_max_jobs) {
330 fprintf(stderr, "Max jobs setting too small! Trying to record job #%ld when we only have space for %ld jobs. Exiting...\n", _rt_jobs_complete, _rt_max_jobs); 333 fprintf(stderr, "Max jobs setting too small! Trying to record job #%ld when we only have space for %ld jobs. Exiting...\n", _rt_jobs_complete, _rt_max_jobs);
331 exit(1); 334 exit(1);
332 } 335 }
333 if (_rt_jobs_complete > -1 && _rt_will_output) {
334#ifdef PAIRED 336#ifdef PAIRED
335 _rt_start_time[_rt_jobs_complete] = _rt_start.tv_sec; 337 _rt_start_time[_rt_jobs_complete] = _rt_start.tv_sec;
336 _rt_start_time[_rt_jobs_complete] *= _BILLION; 338 _rt_start_time[_rt_jobs_complete] *= _BILLION;
337 _rt_start_time[_rt_jobs_complete] += _rt_start.tv_nsec; 339 _rt_start_time[_rt_jobs_complete] += _rt_start.tv_nsec;
338 _rt_end_time[_rt_jobs_complete] = _rt_end.tv_sec; 340 _rt_end_time[_rt_jobs_complete] = _rt_end.tv_sec;
339 _rt_end_time[_rt_jobs_complete] *= _BILLION; 341 _rt_end_time[_rt_jobs_complete] *= _BILLION;
340 _rt_end_time[_rt_jobs_complete] += _rt_end.tv_nsec; 342 _rt_end_time[_rt_jobs_complete] += _rt_end.tv_nsec;
341#else 343#else
342 _rt_exec_time[_rt_jobs_complete] = _rt_end.tv_sec - _rt_start.tv_sec; 344 _rt_exec_time[_rt_jobs_complete] = _rt_end.tv_sec - _rt_start.tv_sec;
343 _rt_exec_time[_rt_jobs_complete] *= _BILLION; 345 _rt_exec_time[_rt_jobs_complete] *= _BILLION;
344 _rt_exec_time[_rt_jobs_complete] += _rt_end.tv_nsec - _rt_start.tv_nsec; 346 _rt_exec_time[_rt_jobs_complete] += _rt_end.tv_nsec - _rt_start.tv_nsec;
345#endif /* PAIRED */ 347#endif /* PAIRED */
346#if MMDC_PROF 348#if MMDC_PROF
347 _rt_mmdc_read[_rt_jobs_complete] = mmdc_res.read_bytes; 349 _rt_mmdc_read[_rt_jobs_complete] = mmdc_res.read_bytes;
348 _rt_mmdc_write[_rt_jobs_complete] = mmdc_res.write_bytes; 350 _rt_mmdc_write[_rt_jobs_complete] = mmdc_res.write_bytes;
349#endif 351#endif /* MMDC_PROF */
350 }
351} 352}
352 353
353// Save all buffered timing results to disk 354// Save all buffered timing results to disk
@@ -386,13 +387,16 @@ static void _rt_write_to_file() {
386 fclose(fp); 387 fclose(fp);
387out: 388out:
388#if LITMUS 389#if LITMUS
389 CLEANUP_LITMUS 390 if (task_mode(BACKGROUND_TASK) != 0) {
391 perror("Unable to become a real-time task");
392 exit(1);
393 }
390#endif /* LITMUS */ 394#endif /* LITMUS */
391#ifdef PAIRED 395#ifdef PAIRED
392 munmap(_rt_barrier, 1); 396 munmap(_rt_barrier, 2);
393 shm_unlink("/_libextra_barrier"); 397 sem_unlink(_rt_sem1_name);
394 sem_unlink("/_libextra_first_sem"); 398 sem_unlink(_rt_sem2_name);
395 sem_unlink("/_libextra_second_sem"); 399 shm_unlink(_rt_shm_name);
396 free(_rt_start_time); 400 free(_rt_start_time);
397 free(_rt_end_time); 401 free(_rt_end_time);
398#else 402#else
@@ -417,7 +421,9 @@ static void _rt_start_loop() {
417 FIRST_UNLOCK 421 FIRST_UNLOCK
418 FIRST_LOCK 422 FIRST_LOCK
419#endif /* PAIRED */ 423#endif /* PAIRED */
424#if !LITMUS
420 FLUSH_CACHES 425 FLUSH_CACHES
426#endif
421#ifdef PAIRED 427#ifdef PAIRED
422 BARRIER_SYNC 428 BARRIER_SYNC
423#endif /* PAIRED */ 429#endif /* PAIRED */
@@ -465,7 +471,7 @@ static void _rt_stop_loop() {
465 * result. We use this to call our void function from inside a comparison. 471 * result. We use this to call our void function from inside a comparison.
466 */ 472 */
467#define for_each_job \ 473#define for_each_job \
468 for (; _rt_jobs_complete < _rt_max_jobs && (_rt_start_loop(),1); \ 474 for (; (_rt_max_jobs == -1 || _rt_jobs_complete < _rt_max_jobs) && (_rt_start_loop(),1); \
469 _rt_stop_loop()) 475 _rt_stop_loop())
470 476
471/****** Legacy API ****** 477/****** Legacy API ******
@@ -487,6 +493,7 @@ static void _rt_stop_loop() {
487 * able to read them. 493 * able to read them.
488 */ 494 */
489static int jobsComplete = 0; 495static int jobsComplete = 0;
496#define SET_UP _rt_load_params_itrl(argc, argv);
490#define START_LOOP _rt_start_loop(); 497#define START_LOOP _rt_start_loop();
491#define STOP_LOOP _rt_stop_loop(); 498#define STOP_LOOP _rt_stop_loop();
492#define WRITE_TO_FILE _rt_write_to_file(); 499#define WRITE_TO_FILE _rt_write_to_file();