diff options
author | Joshua Bakita <jbakita@cs.unc.edu> | 2019-06-17 08:32:41 -0400 |
---|---|---|
committer | Joshua Bakita <jbakita@cs.unc.edu> | 2019-06-17 08:32:41 -0400 |
commit | c10cfd98cbaba8d0c602d1f790ce8e3b6c2ff40c (patch) | |
tree | f10dc1a56da0c9ce421bd6925df8c461e5fa10c0 |
Commit old version of extra.h
-rw-r--r-- | rtss19/extra.h | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/rtss19/extra.h b/rtss19/extra.h new file mode 100644 index 0000000..f089373 --- /dev/null +++ b/rtss19/extra.h | |||
@@ -0,0 +1,208 @@ | |||
1 | #include <time.h> | ||
2 | #include <sys/mman.h> | ||
3 | #include <sys/stat.h> | ||
4 | #include <unistd.h> | ||
5 | #include <fcntl.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <stdio.h> | ||
8 | #include <string.h> | ||
9 | #include <signal.h> | ||
10 | #include <limits.h> | ||
11 | #include <fcntl.h> | ||
12 | #include <semaphore.h> | ||
13 | |||
14 | // Benchmarks use SET_UP, START_LOOP, STOP_LOOP, and WRITE_TO_FILE | ||
15 | #define SET_UP if (argc < 8) {\ | ||
16 | printf("Usage: %s <name> <runs> <my core> <other core> <other program> <runID> <lockID>", argv[0]);\ | ||
17 | exit(1);\ | ||
18 | }\ | ||
19 | char * thisProgram = argv[1];\ | ||
20 | unsigned int maxJobs = atoi(argv[2]);\ | ||
21 | char * thisCore = argv[3];\ | ||
22 | char * otherCore = argv[4];\ | ||
23 | char * otherProgram = argv[5];\ | ||
24 | char * runID = argv[6];\ | ||
25 | int lockID = atoi(argv[7]);\ | ||
26 | struct timespec start, end;\ | ||
27 | unsigned int jobsComplete = 0;\ | ||
28 | long * startS = malloc(sizeof(long) *maxJobs);\ | ||
29 | long * startN = malloc(sizeof(long) *maxJobs);\ | ||
30 | long * endS = malloc(sizeof(long) *maxJobs);\ | ||
31 | long * endN = malloc(sizeof(long) *maxJobs);\ | ||
32 | char * bigArray;\ | ||
33 | char fileName[strlen(runID) + 5];\ | ||
34 | strcpy(fileName, runID);\ | ||
35 | strcat(fileName, ".txt");\ | ||
36 | mlockall(MCL_CURRENT || MCL_FUTURE);\ | ||
37 | sem_t *firstSem=sem_open("/firstTacleSem", O_CREAT, 644, 0);\ | ||
38 | if (firstSem == SEM_FAILED) {\ | ||
39 | perror("Error opening/creating first semaphore");\ | ||
40 | exit(1);\ | ||
41 | }\ | ||
42 | sem_t *secondSem=sem_open("/secondTacleSem", O_CREAT, 644, 0);\ | ||
43 | if (secondSem == SEM_FAILED) {\ | ||
44 | perror("Error opening/creating second semaphore");\ | ||
45 | exit(1);\ | ||
46 | }\ | ||
47 | int barrier_file = shm_open("/TacleBarrier", O_CREAT | O_RDWR, 644);\ | ||
48 | if (barrier_file == -1) {\ | ||
49 | perror("Error creating shared memory");\ | ||
50 | exit(1);\ | ||
51 | }\ | ||
52 | /* This sets our shared file to be one byte of '\0'*/ \ | ||
53 | if (ftruncate(barrier_file, 1) == -1) {\ | ||
54 | perror("Error setting size of shared memory");\ | ||
55 | exit(1);\ | ||
56 | }\ | ||
57 | char * barrier = mmap(NULL, 1, PROT_WRITE, MAP_SHARED, barrier_file, 0);\ | ||
58 | if (barrier == MAP_FAILED) {\ | ||
59 | perror("Error mapping shared memory");\ | ||
60 | exit(1);\ | ||
61 | }\ | ||
62 | int error;\ | ||
63 | int val; | ||
64 | |||
65 | #define SAVE_RESULTS if (jobsComplete > 0){\ | ||
66 | startS[jobsComplete]=start.tv_sec;\ | ||
67 | startN[jobsComplete]=start.tv_nsec;\ | ||
68 | endS[jobsComplete]=end.tv_sec;\ | ||
69 | endN[jobsComplete]=end.tv_nsec;} | ||
70 | |||
71 | #define WRITE_TO_FILE {\ | ||
72 | munlockall();\ | ||
73 | FILE *fp = fopen(fileName, "a");\ | ||
74 | if (fp == NULL) {\ | ||
75 | perror("Error opening file. \n");\ | ||
76 | exit(1);\ | ||
77 | }\ | ||
78 | for(jobsComplete=0; jobsComplete<maxJobs; jobsComplete++){\ | ||
79 | fprintf(fp, "%s %s %s %s %d %ld %ld %ld %ld %s %d \n",\ | ||
80 | thisProgram, otherProgram, thisCore, otherCore, maxJobs,\ | ||
81 | startS[jobsComplete], startN[jobsComplete], endS[jobsComplete], endN[jobsComplete],\ | ||
82 | runID, jobsComplete);\ | ||
83 | }\ | ||
84 | fclose(fp);\ | ||
85 | /* Clean up the barrier synchronization shared memory */\ | ||
86 | munmap(barrier, 1);\ | ||
87 | shm_unlink("/TacleBarrier");\ | ||
88 | free(startS);\ | ||
89 | free(startN);\ | ||
90 | free(endS);\ | ||
91 | free(endN);\ | ||
92 | } | ||
93 | |||
94 | // Call the wbinvld instruction (it's in a kernel module due to it being ring-0) | ||
95 | #define FLUSH_CACHES FILE *fp = fopen("/proc/wbinvd", "r");\ | ||
96 | if (fp == NULL) {\ | ||
97 | perror("Cache flush module interface cannot be opened");\ | ||
98 | exit(1);\ | ||
99 | }\ | ||
100 | char dummy;\ | ||
101 | fread(&dummy, 1, 1, fp);\ | ||
102 | fclose(fp); | ||
103 | |||
104 | //invoke start timer twice, stop timer to make sure timer and vars are in cache | ||
105 | #define START_TIMER clock_gettime(CLOCK_MONOTONIC, &start); | ||
106 | //clock_gettime(CLOCK_MONOTONIC, &end);\ | ||
107 | //clock_gettime(CLOCK_MONOTONIC, &start);\ | ||
108 | |||
109 | #define STOP_TIMER clock_gettime(CLOCK_MONOTONIC, &end); | ||
110 | |||
111 | #define LOCK2_LOCKED //printf("Lock2 locked. \n"); | ||
112 | #define LOCK1_LOCKED //printf("Lock1 locked. \n"); | ||
113 | #define LOCK1_UNLOCKED //printf("Lock1 unlocked. \n"); | ||
114 | #define LOCK2_UNLOCKED //printf("Lock2 unlocked. \n"); | ||
115 | |||
116 | //check value of sem | ||
117 | //if sem=0, unlock | ||
118 | //if sem=1, spin | ||
119 | |||
120 | #define SLEEP nanosleep((const struct timespec[]){{0, 1000000}}, NULL); | ||
121 | |||
122 | //#define SAFE_UNLOCK(whichSem) sem_getvalue(whichSem, &val); while(val==1) { printf("Attpt. to unlock unlocked lock. \n"); sem_getvalue(whichSem, &val); } sem_post(whichSem); | ||
123 | |||
124 | |||
125 | //#define SAFE_UNLOCK(whichSem) sem_post(whichSem); SLEEP ; printf("Used SAFE_UNLOCK. \n"); | ||
126 | |||
127 | #define FIRST_UNLOCK if (lockID == 1) {\ | ||
128 | if (sem_post(secondSem) != 0) {\ | ||
129 | perror("Unable to unlock second semaphore");\ | ||
130 | exit(1);\ | ||
131 | }\ | ||
132 | } \ | ||
133 | else {\ | ||
134 | if (sem_post(firstSem) != 0) {\ | ||
135 | perror("Unable to unlock first semaphore");\ | ||
136 | exit(1);\ | ||
137 | }\ | ||
138 | } \ | ||
139 | |||
140 | #define FIRST_LOCK if (lockID == 1) {\ | ||
141 | if (sem_wait(firstSem) != 0) {\ | ||
142 | perror("Unable to wait on first semaphore");\ | ||
143 | exit(1);\ | ||
144 | }\ | ||
145 | }\ | ||
146 | else {\ | ||
147 | if (sem_wait(secondSem) != 0) {\ | ||
148 | perror("Unable to wait on second semaphore");\ | ||
149 | exit(1);\ | ||
150 | }\ | ||
151 | } | ||
152 | |||
153 | |||
154 | #define SECOND_UNLOCK if (lockID==1){sem_post(fourthSem) ; }\ | ||
155 | else {sem_post(thirdSem) ; } | ||
156 | |||
157 | #define SECOND_LOCK if (lockID==1){sem_wait(thirdSem); }\ | ||
158 | else {sem_wait(fourthSem); } | ||
159 | |||
160 | #define BARRIER_SYNC if (__sync_bool_compare_and_swap(barrier, 0, 1)) {\ | ||
161 | while (!__sync_bool_compare_and_swap(barrier, 0, 0)) {};\ | ||
162 | }\ | ||
163 | else {\ | ||
164 | __sync_bool_compare_and_swap(barrier, 1, 0);\ | ||
165 | } | ||
166 | |||
167 | |||
168 | //#define SECOND_UNLOCK if (lockID==1){sem_post(firstSem) ; LOCK1_UNLOCKED }\ | ||
169 | else {sem_post(secondSem) ; LOCK2_UNLOCKED } | ||
170 | |||
171 | //#define SECOND_LOCK if (lockID==1){sem_wait(secondSem); LOCK2_LOCKED }\ | ||
172 | else {sem_wait(firstSem); LOCK1_LOCKED } | ||
173 | |||
174 | |||
175 | #define START_LOOP FIRST_UNLOCK FIRST_LOCK FLUSH_CACHES BARRIER_SYNC START_TIMER | ||
176 | |||
177 | |||
178 | /* | ||
179 | First task to reach TRY_CACHE will fail, but unlock the lock. | ||
180 | Second task will get the lock and kill the cache. | ||
181 | Lock remains in place until this point is reached again in code. | ||
182 | */ | ||
183 | |||
184 | //#define TRY_CACHE if (sem_trywait(cacheSem)==0){KILL_CACHE}\ | ||
185 | else {sem_post(cacheSem);} | ||
186 | |||
187 | //#define STOP_LOOP STOP_TIMER SAVE_RESULTS //TRY_CACHE | ||
188 | |||
189 | #define STOP_LOOP STOP_TIMER SAVE_RESULTS //SECOND_UNLOCK SECOND_LOCK KILL_CACHE | ||
190 | |||
191 | |||
192 | /* | ||
193 | Intended structure | ||
194 | |||
195 | main | ||
196 | SET_UP | ||
197 | notice that STOP LOOP negates the ++ if outout=0 | ||
198 | for (jobsComplete=-1; jobsComplete<maxJobs; jobsComplete++){ | ||
199 | KILL_CACHE | ||
200 | START_TIMER | ||
201 | tacleInit(); | ||
202 | tacleMain(); | ||
203 | STOP_TIMER | ||
204 | SAVE_RESULTS | ||
205 | } | ||
206 | WRITE_TO_FILE | ||
207 | tacleReturn | ||
208 | */ | ||