diff options
author | Joshua Bakita <jbakita@cs.unc.edu> | 2020-10-19 01:09:53 -0400 |
---|---|---|
committer | Joshua Bakita <jbakita@cs.unc.edu> | 2020-10-19 01:09:53 -0400 |
commit | a71fc97fd262e1b5770f827047ea60bbaf38d9a2 (patch) | |
tree | b45ef48c63a35817f2db93dd2fec718778f58b99 /all_pairs/source/extra.h | |
parent | 41358857592f1908d0c0f9898b6c9acabc1ad161 (diff) |
Unify all the versions of extra.h into a single multipurpose header
There was previously a huge amount of shared code that had to be
copied back and forth. This should reduce the maintenance burden
by containing all future changes to a single file.
New unified library is fully backwards-compatible but also
introduces and the easy-to-use `for_each_job` macro which
replaces the specific `for(...) START_LOOP ... STOP_LOOP`
format requirement and is generally much harder to abuse.
New unified library also automatically cleans up its shared memory
and semaphores, so this commit also removes the separate
`cleanupSemaphores` binary.
I also found a precursor of `extra.h` written by Sims in
`litmusStuff.h`. This code is only interesting for historical
purposes, so it is also removed in this commit.
This commit also adds debug options to all the Makefiles and
silences rm's complaints about non-existent files in make clean.
Diffstat (limited to 'all_pairs/source/extra.h')
-rw-r--r-- | all_pairs/source/extra.h | 211 |
1 files changed, 0 insertions, 211 deletions
diff --git a/all_pairs/source/extra.h b/all_pairs/source/extra.h deleted file mode 100644 index 0681250..0000000 --- a/all_pairs/source/extra.h +++ /dev/null | |||
@@ -1,211 +0,0 @@ | |||
1 | /** | ||
2 | * Copyright 2019 Sims Hill Osborne and Joshua Bakita | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
5 | * of this software and associated documentation files (the "Software"), to deal | ||
6 | * in the Software without restriction, including without limitation the rights | ||
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
8 | * copies of the Software, and to permit persons to whom the Software is | ||
9 | * furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
20 | * SOFTWARE. | ||
21 | **/ | ||
22 | #define _GNU_SOURCE | ||
23 | #include <fcntl.h> | ||
24 | #include <limits.h> | ||
25 | #include <semaphore.h> | ||
26 | #include <signal.h> | ||
27 | #include <stdlib.h> | ||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <sys/mman.h> | ||
31 | #include <sys/stat.h> | ||
32 | #include <time.h> | ||
33 | #include <unistd.h> | ||
34 | #include <sched.h> | ||
35 | |||
36 | // This is only visible if _GNU_SOURCE is defined, and that define does not | ||
37 | // come along to places where this file is included. Address this by manually | ||
38 | // forcing it into the global namespace. | ||
39 | extern int sched_getcpu(); | ||
40 | |||
41 | // Benchmarks use SET_UP, START_LOOP, STOP_LOOP, and WRITE_TO_FILE | ||
42 | // These are macros so that we can declare and maintain additional state inside | ||
43 | // the benchmark. | ||
44 | #define SET_UP if (argc != 8) {\ | ||
45 | printf("Usage: %s <name> <runs> <my core> <other core> <other program> <runID> <lockID>", argv[0]);\ | ||
46 | exit(1);\ | ||
47 | }\ | ||
48 | char * thisProgram = argv[1];\ | ||
49 | int maxJobs = atoi(argv[2]);\ | ||
50 | unsigned int thisCore = atoi(argv[3]);\ | ||
51 | unsigned int otherCore = atoi(argv[4]);\ | ||
52 | thisCore = sched_getcpu();\ | ||
53 | char * otherProgram = argv[5];\ | ||
54 | char * runID = argv[6];\ | ||
55 | int lockID = atoi(argv[7]);\ | ||
56 | struct timespec _start, _end;\ | ||
57 | int jobsComplete;\ | ||
58 | int jobs_complete = -1;\ | ||
59 | long * startS = malloc(sizeof(long) *maxJobs);\ | ||
60 | long * startN = malloc(sizeof(long) *maxJobs);\ | ||
61 | long * endS = malloc(sizeof(long) *maxJobs);\ | ||
62 | long * endN = malloc(sizeof(long) *maxJobs);\ | ||
63 | char * bigArray;\ | ||
64 | char fileName[strlen(runID) + 5];\ | ||
65 | strcpy(fileName, runID);\ | ||
66 | strcat(fileName, ".txt");\ | ||
67 | mlockall(MCL_CURRENT || MCL_FUTURE);\ | ||
68 | sem_t *firstSem=sem_open("/firstTacleSem", O_CREAT, 644, 0);\ | ||
69 | if (firstSem == SEM_FAILED) {\ | ||
70 | perror("Error opening/creating first semaphore");\ | ||
71 | exit(1);\ | ||
72 | }\ | ||
73 | sem_t *secondSem=sem_open("/secondTacleSem", O_CREAT, 644, 0);\ | ||
74 | if (secondSem == SEM_FAILED) {\ | ||
75 | perror("Error opening/creating second semaphore");\ | ||
76 | exit(1);\ | ||
77 | }\ | ||
78 | int barrier_file = shm_open("/TacleBarrier", O_CREAT | O_RDWR, 644);\ | ||
79 | if (barrier_file == -1) {\ | ||
80 | perror("Error creating shared memory");\ | ||
81 | exit(1);\ | ||
82 | }\ | ||
83 | /* This sets our shared file to be one byte of '\0'*/ \ | ||
84 | if (ftruncate(barrier_file, 1) == -1) {\ | ||
85 | perror("Error setting size of shared memory");\ | ||
86 | exit(1);\ | ||
87 | }\ | ||
88 | char * barrier = mmap(NULL, 1, PROT_WRITE, MAP_SHARED, barrier_file, 0);\ | ||
89 | if (barrier == MAP_FAILED) {\ | ||
90 | perror("Error mapping shared memory");\ | ||
91 | exit(1);\ | ||
92 | }\ | ||
93 | int val; | ||
94 | |||
95 | #define SAVE_RESULTS \ | ||
96 | if(jobs_complete >= maxJobs) {\ | ||
97 | fprintf(stderr, "Max jobs setting too small! Trying to record job #%d when we only have space for %d jobs. Exiting...\n", jobs_complete, maxJobs);\ | ||
98 | exit(1);\ | ||
99 | }\ | ||
100 | if (jobs_complete > -1){\ | ||
101 | startS[jobs_complete]=_start.tv_sec;\ | ||
102 | startN[jobs_complete]=_start.tv_nsec;\ | ||
103 | endS[jobs_complete]=_end.tv_sec;\ | ||
104 | endN[jobs_complete]=_end.tv_nsec;\ | ||
105 | } | ||
106 | |||
107 | #define WRITE_TO_FILE {\ | ||
108 | munlockall();\ | ||
109 | FILE *fp = fopen(fileName, "a");\ | ||
110 | if (fp == NULL) {\ | ||
111 | perror("Error opening file. \n");\ | ||
112 | exit(1);\ | ||
113 | }\ | ||
114 | for(int i = 0; i <= jobs_complete; i++){\ | ||
115 | fprintf(fp, "%s %s %u %u %d %ld %ld %ld %ld %s %d \n",\ | ||
116 | thisProgram, otherProgram, thisCore, otherCore, maxJobs,\ | ||
117 | startS[i], startN[i], endS[i], endN[i],\ | ||
118 | runID, i);\ | ||
119 | }\ | ||
120 | fclose(fp);\ | ||
121 | /* Clean up the barrier synchronization shared memory */\ | ||
122 | munmap(barrier, 1);\ | ||
123 | shm_unlink("/TacleBarrier");\ | ||
124 | free(startS);\ | ||
125 | free(startN);\ | ||
126 | free(endS);\ | ||
127 | free(endN);\ | ||
128 | } | ||
129 | |||
130 | // Call the wbinvld instruction (it's in a kernel module due to it being ring-0) | ||
131 | #define FLUSH_CACHES FILE *fp = fopen("/proc/wbinvd", "r");\ | ||
132 | if (fp == NULL) {\ | ||
133 | perror("Cache flush module interface cannot be opened");\ | ||
134 | exit(1);\ | ||
135 | }\ | ||
136 | char dummy;\ | ||
137 | if (fread(&dummy, 1, 1, fp) == 0) {\ | ||
138 | perror("Unable to access cache flush module interface");\ | ||
139 | exit(1);\ | ||
140 | }\ | ||
141 | fclose(fp); | ||
142 | |||
143 | // These timers should just be aliases to the hardware counters w/ some small adjustments | ||
144 | #define START_TIMER clock_gettime(CLOCK_MONOTONIC, &_start); | ||
145 | #define STOP_TIMER clock_gettime(CLOCK_MONOTONIC, &_end); | ||
146 | |||
147 | //check value of sem | ||
148 | //if sem=0, unlock | ||
149 | //if sem=1, spin | ||
150 | |||
151 | #define SLEEP nanosleep((const struct timespec[]){{0, 1000000}}, NULL); | ||
152 | |||
153 | #define FIRST_UNLOCK if (lockID == 1) {\ | ||
154 | if (sem_post(secondSem) != 0) {\ | ||
155 | perror("Unable to unlock second semaphore");\ | ||
156 | exit(1);\ | ||
157 | }\ | ||
158 | } \ | ||
159 | else {\ | ||
160 | if (sem_post(firstSem) != 0) {\ | ||
161 | perror("Unable to unlock first semaphore");\ | ||
162 | exit(1);\ | ||
163 | }\ | ||
164 | } \ | ||
165 | |||
166 | #define FIRST_LOCK if (lockID == 1) {\ | ||
167 | if (sem_wait(firstSem) != 0) {\ | ||
168 | perror("Unable to wait on first semaphore");\ | ||
169 | exit(1);\ | ||
170 | }\ | ||
171 | }\ | ||
172 | else {\ | ||
173 | if (sem_wait(secondSem) != 0) {\ | ||
174 | perror("Unable to wait on second semaphore");\ | ||
175 | exit(1);\ | ||
176 | }\ | ||
177 | } | ||
178 | |||
179 | |||
180 | #define SECOND_UNLOCK if (lockID==1){sem_post(fourthSem) ; }\ | ||
181 | else {sem_post(thirdSem) ; } | ||
182 | |||
183 | #define SECOND_LOCK if (lockID==1){sem_wait(thirdSem); }\ | ||
184 | else {sem_wait(fourthSem); } | ||
185 | |||
186 | #define BARRIER_SYNC if (__sync_bool_compare_and_swap(barrier, 0, 1)) {\ | ||
187 | while (!__sync_bool_compare_and_swap(barrier, 0, 0)) {};\ | ||
188 | }\ | ||
189 | else {\ | ||
190 | __sync_bool_compare_and_swap(barrier, 1, 0);\ | ||
191 | } | ||
192 | |||
193 | #define START_LOOP FIRST_UNLOCK FIRST_LOCK sched_yield(); FLUSH_CACHES BARRIER_SYNC START_TIMER | ||
194 | #define STOP_LOOP STOP_TIMER jobs_complete++; SAVE_RESULTS | ||
195 | |||
196 | |||
197 | /* | ||
198 | Intended structure | ||
199 | |||
200 | main | ||
201 | SET_UP | ||
202 | notice that STOP LOOP negates the ++ if outout=0 | ||
203 | for (jobsComplete=0; jobsComplete<maxJobs; jobsComplete++){ | ||
204 | START_LOOP | ||
205 | tacleInit(); | ||
206 | tacleMain(); | ||
207 | STOP_LOOP | ||
208 | } | ||
209 | WRITE_TO_FILE | ||
210 | tacleReturn | ||
211 | */ | ||