diff options
Diffstat (limited to 'extra_prev.h')
| -rw-r--r-- | extra_prev.h | 601 |
1 files changed, 601 insertions, 0 deletions
diff --git a/extra_prev.h b/extra_prev.h new file mode 100644 index 0000000..64d334f --- /dev/null +++ b/extra_prev.h | |||
| @@ -0,0 +1,601 @@ | |||
| 1 | /** | ||
| 2 | * Copyright 2019 Sims Hill Osborne and 2020 Joshua Bakita | ||
| 3 | * | ||
| 4 | * This header provides facilities by which to separably run and time TACLeBench | ||
| 5 | * To use this for paired task timing, define PAIRED (pass CFLAGS=-DPAIRED to make) | ||
| 6 | **/ | ||
| 7 | #define _GNU_SOURCE | ||
| 8 | #include <fcntl.h> // For O_CREAT and O_RDWR | ||
| 9 | #include <sched.h> // For sched_yield() | ||
| 10 | #include <semaphore.h> // For sem_{open, post, wait}() | ||
| 11 | #include <stdio.h> | ||
| 12 | #include <stdlib.h> // For exit() | ||
| 13 | #include <string.h> // For strlen() | ||
| 14 | #include <sys/mman.h> // For mlockall() | ||
| 15 | #include <unistd.h> // For ftruncate() | ||
| 16 | #include <time.h> | ||
| 17 | |||
| 18 | // This is only visible if _GNU_SOURCE is defined, and that define does not | ||
| 19 | // come along to places where this file is included. Address this by manually | ||
| 20 | // forcing it into the global namespace. | ||
| 21 | extern int sched_getcpu(); | ||
| 22 | |||
| 23 | // These constants correspond to the imx6q-sabredb platform | ||
| 24 | #define LINE_SIZE 32 | ||
| 25 | #define L2_SIZE 16*2048*32 | ||
| 26 | |||
| 27 | #if __arm__ | ||
| 28 | #include <unistd.h> | ||
| 29 | #include <sys/syscall.h> | ||
| 30 | #endif | ||
| 31 | |||
| 32 | // This is a proxy for "case study mode" now | ||
| 33 | #define LITMUS 0 | ||
| 34 | #define MMDC_PROF 0 | ||
| 35 | |||
| 36 | #if LITMUS | ||
| 37 | #include <litmus.h> | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #if MMDC_PROF | ||
| 41 | #include "/media/speedy/litmus/tools/mmdc/mmdc.h" | ||
| 42 | #endif | ||
| 43 | |||
| 44 | // Store state globally so that the job can be outside main() | ||
| 45 | // Arrays use float as a comprimise between overflow and size | ||
| 46 | // Paired arrays use long longs as precision is more important for those times | ||
| 47 | // #ifdef PAIRED | ||
| 48 | long long *_rt_start_time; | ||
| 49 | long long *_rt_end_time; | ||
| 50 | // #else | ||
| 51 | float *_rt_exec_time; | ||
| 52 | // #endif | ||
| 53 | #if MMDC_PERF | ||
| 54 | float *_rt_mmdc_read; | ||
| 55 | float *_rt_mmdc_write; | ||
| 56 | #endif | ||
| 57 | long _rt_jobs_complete; | ||
| 58 | long _rt_max_jobs; | ||
| 59 | int _rt_core; | ||
| 60 | int _rt_will_output; | ||
| 61 | struct timespec _rt_start, _rt_end; | ||
| 62 | |||
| 63 | char *_rt_run_id; | ||
| 64 | char *_rt_our_prog_name; | ||
| 65 | char *_rt_other_prog_name; | ||
| 66 | char *_rt_other_core; | ||
| 67 | #define _RT_FILENAME_LEN 64 | ||
| 68 | #define _BILLION (1000*1000*1000) | ||
| 69 | #ifdef PAIRED | ||
| 70 | char *_rt_barrier; | ||
| 71 | sem_t *_rt_first_sem, *_rt_second_sem; | ||
| 72 | int _rt_lock_id; | ||
| 73 | #define _ID_SZ 128 | ||
| 74 | char _rt_sem1_name[_ID_SZ] = "/_libextra_first_sem-"; | ||
| 75 | char _rt_sem2_name[_ID_SZ] = "/_libextra_second_sem-"; | ||
| 76 | char _rt_shm_name[_ID_SZ] = "/_libextra_barrier-"; | ||
| 77 | #endif /* PAIRED */ | ||
| 78 | |||
| 79 | #if LITMUS | ||
| 80 | lt_t _rt_period; | ||
| 81 | lt_t _rt_phase; | ||
| 82 | lt_t _rt_cost; | ||
| 83 | lt_t _rt_deadline; | ||
| 84 | int _rt_crit; | ||
| 85 | struct control_page *_rt_cp; | ||
| 86 | #endif | ||
| 87 | |||
| 88 | static void _rt_load_params_itrl(int argc, char **argv) { | ||
| 89 | #ifdef PAIRED | ||
| 90 | if (argc != (8 + LITMUS*5) && argc != (9 + LITMUS*5)) { | ||
| 91 | fprintf(stderr, "Usage: %s <name> <loops> <my core> <other core> <other name> <runID> <save results?> <pairID>\n", argv[0]); | ||
| 92 | #else | ||
| 93 | if (argc != (6 + LITMUS*5)) { | ||
| 94 | fprintf(stderr, "Usage: %s <name> <loops> <my core> <runID> <save results?>\n", argv[0]); | ||
| 95 | #endif /* PAIRED */ | ||
| 96 | fprintf(stderr, " <name> string for logging. Name of this task.\n"); | ||
| 97 | fprintf(stderr, " <loops> integer number of iterations. -1 for infinite.\n"); | ||
| 98 | fprintf(stderr, " <my core> integer core number. Only used for LITMUS-RT.\n"); | ||
| 99 | #ifdef PAIRED | ||
| 100 | fprintf(stderr, " <other core> integer for logging. Core of paired task.\n"); | ||
| 101 | fprintf(stderr, " <other name> string for logging. Name of paired task.\n"); | ||
| 102 | #endif /* PAIRED */ | ||
| 103 | fprintf(stderr, " <runID> string to append with .txt to yield output file name.\n"); | ||
| 104 | fprintf(stderr, " <save results?> 1 to save results, 0 to discard.\n"); | ||
| 105 | #ifdef PAIRED | ||
| 106 | fprintf(stderr, " <pairID> (optional).\n"); | ||
| 107 | #endif | ||
| 108 | #if LITMUS | ||
| 109 | fprintf(stderr, " <task period> in ms\n"); | ||
| 110 | fprintf(stderr, " <task criticality level> 0 for Level-A, 1 for Level-B, 2 for Level-C\n"); | ||
| 111 | fprintf(stderr, " <task phase> in ms\n"); | ||
| 112 | fprintf(stderr, " <task cost> in ms\n"); | ||
| 113 | fprintf(stderr, " <task deadline> in ms\n"); | ||
| 114 | #endif /* LITMUS */ | ||
| 115 | exit(1); | ||
| 116 | } | ||
| 117 | _rt_our_prog_name = argv[1]; | ||
| 118 | _rt_max_jobs = atol(argv[2]); | ||
| 119 | #if !LITMUS | ||
| 120 | _rt_core = sched_getcpu(); | ||
| 121 | #else | ||
| 122 | _rt_core = atoi(argv[3]); | ||
| 123 | #endif | ||
| 124 | #ifdef PAIRED | ||
| 125 | _rt_other_core = argv[4]; | ||
| 126 | _rt_other_prog_name = argv[5]; | ||
| 127 | _rt_run_id = argv[6]; | ||
| 128 | _rt_will_output = atoi(argv[7]); | ||
| 129 | char *pairId; | ||
| 130 | int end; | ||
| 131 | if (argc > 8) { | ||
| 132 | pairId = argv[8]; | ||
| 133 | end = 9; | ||
| 134 | } else { | ||
| 135 | pairId = "none"; | ||
| 136 | end = 8; | ||
| 137 | } | ||
| 138 | #else | ||
| 139 | _rt_other_core = "none"; | ||
| 140 | _rt_other_prog_name = "none"; | ||
| 141 | _rt_run_id = argv[4]; | ||
| 142 | _rt_will_output = atoi(argv[5]); | ||
| 143 | int end = 6; | ||
| 144 | #endif /* PAIRED */ | ||
| 145 | if (_rt_max_jobs < 0 && _rt_will_output != 0) { | ||
| 146 | fprintf(stderr, "Infinite loops only supported when output is disabled!\n"); | ||
| 147 | exit(1); | ||
| 148 | } | ||
| 149 | if (strlen(_rt_run_id) + 5 > _RT_FILENAME_LEN) { | ||
| 150 | fprintf(stderr, "Run ID is too large! Keep it to less than %d characters.\n", _RT_FILENAME_LEN); | ||
| 151 | exit(1); | ||
| 152 | } | ||
| 153 | #ifdef PAIRED | ||
| 154 | // __rt_sem2_name happens to be the longest | ||
| 155 | if (strlen(pairId) + strlen(_rt_sem2_name) > _ID_SZ) { | ||
| 156 | fprintf(stderr, "PairID is too long! Maximum length is %ld characters.\n", _ID_SZ - strlen(_rt_sem2_name)); | ||
| 157 | exit(1); | ||
| 158 | } | ||
| 159 | _rt_start_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long)); | ||
| 160 | _rt_end_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long)); | ||
| 161 | _rt_exec_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(float)); | ||
| 162 | if (!_rt_end_time || !_rt_start_time) { | ||
| 163 | perror("Unable to allocate buffers for execution times"); | ||
| 164 | exit(1); | ||
| 165 | } | ||
| 166 | // Use PairID to create unique semaphore and shared memory paths | ||
| 167 | strcat(_rt_sem1_name, pairId); | ||
| 168 | strcat(_rt_sem2_name, pairId); | ||
| 169 | strcat(_rt_shm_name, pairId); | ||
| 170 | _rt_first_sem = sem_open(_rt_sem1_name, O_CREAT, 644, 0); | ||
| 171 | _rt_second_sem = sem_open(_rt_sem2_name, O_CREAT, 644, 0); | ||
| 172 | if (_rt_first_sem == SEM_FAILED || _rt_second_sem == SEM_FAILED) { | ||
| 173 | perror("Error while creating semaphores"); | ||
| 174 | exit(1); | ||
| 175 | } | ||
| 176 | // Create shared memory for barrier synchronization and infer lock ID | ||
| 177 | int barrier_file = shm_open(_rt_shm_name, O_CREAT | O_RDWR | O_EXCL, 644); | ||
| 178 | if (barrier_file == -1) { | ||
| 179 | // File already existed - we're the 2nd program and thus lock ID 2 | ||
| 180 | _rt_lock_id = 2; | ||
| 181 | barrier_file = shm_open(_rt_shm_name, O_CREAT | O_RDWR, 644); | ||
| 182 | } else { | ||
| 183 | _rt_lock_id = 1; | ||
| 184 | } | ||
| 185 | if (barrier_file == -1) { | ||
| 186 | perror("Error while creating shared memory for barrier synchronization"); | ||
| 187 | exit(1); | ||
| 188 | } | ||
| 189 | if (ftruncate(barrier_file, 2) == -1) { | ||
| 190 | perror("Error while setting size of shared memory for barrier synchronization"); | ||
| 191 | exit(1); | ||
| 192 | } | ||
| 193 | _rt_barrier = mmap(NULL, 2, PROT_WRITE, MAP_SHARED, barrier_file, 0); | ||
| 194 | if (_rt_barrier == MAP_FAILED) { | ||
| 195 | perror("Error while mapping shared memory for barrier synchronization"); | ||
| 196 | exit(1); | ||
| 197 | } | ||
| 198 | // If we're the 2nd user of this barrier, mark it as in-use | ||
| 199 | if (_rt_lock_id == 2 && !__sync_bool_compare_and_swap(_rt_barrier+1, 0, 1)) { | ||
| 200 | fprintf(stderr, "Pair ID already in use!\n"); | ||
| 201 | exit(1); | ||
| 202 | } | ||
| 203 | *_rt_barrier = 0; | ||
| 204 | #else | ||
| 205 | _rt_start_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long)); | ||
| 206 | _rt_end_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long)); | ||
| 207 | _rt_exec_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(float)); | ||
| 208 | if (!_rt_exec_time) { | ||
| 209 | perror("Unable to allocate buffer for execution times"); | ||
| 210 | exit(1); | ||
| 211 | } | ||
| 212 | #endif /* PAIRED */ | ||
| 213 | _rt_jobs_complete = 0; | ||
| 214 | mlockall(MCL_CURRENT || MCL_FUTURE); | ||
| 215 | #if LITMUS | ||
| 216 | _rt_period = ms2ns(strtoul(argv[end], NULL, 10)); | ||
| 217 | _rt_crit = atoi(argv[end+1]); | ||
| 218 | _rt_phase = ms2ns(strtoul(argv[end+2], NULL, 10)); | ||
| 219 | _rt_cost = ms2ns(strtoul(argv[end+3], NULL, 10)); | ||
