summaryrefslogtreecommitdiffstats
path: root/rtss19/extra.h
blob: f0893730a0d25df06b1efe022fec70c1ec81f410 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <limits.h>
#include <fcntl.h>
#include <semaphore.h>

// Benchmarks use SET_UP, START_LOOP, STOP_LOOP, and WRITE_TO_FILE
#define SET_UP if (argc < 8) {\
        printf("Usage: %s <name> <runs> <my core> <other core> <other program> <runID> <lockID>", argv[0]);\
        exit(1);\
    }\
    char * thisProgram = argv[1];\
    unsigned int maxJobs = atoi(argv[2]);\
    char * thisCore = argv[3];\
    char * otherCore = argv[4];\
    char * otherProgram = argv[5];\
    char * runID = argv[6];\
    int lockID = atoi(argv[7]);\
    struct timespec start, end;\
    unsigned int jobsComplete = 0;\
    long * startS = malloc(sizeof(long) *maxJobs);\
    long * startN = malloc(sizeof(long) *maxJobs);\
    long * endS = malloc(sizeof(long) *maxJobs);\
    long * endN = malloc(sizeof(long) *maxJobs);\
    char * bigArray;\
    char fileName[strlen(runID) + 5];\
    strcpy(fileName, runID);\
    strcat(fileName, ".txt");\
    mlockall(MCL_CURRENT || MCL_FUTURE);\
    sem_t *firstSem=sem_open("/firstTacleSem", O_CREAT, 644, 0);\
    if (firstSem == SEM_FAILED) {\
        perror("Error opening/creating first semaphore");\
        exit(1);\
    }\
    sem_t *secondSem=sem_open("/secondTacleSem", O_CREAT, 644, 0);\
    if (secondSem == SEM_FAILED) {\
        perror("Error opening/creating second semaphore");\
        exit(1);\
    }\
    int barrier_file = shm_open("/TacleBarrier", O_CREAT | O_RDWR, 644);\
    if (barrier_file == -1) {\
        perror("Error creating shared memory");\
        exit(1);\
    }\
    /* This sets our shared file to be one byte of '\0'*/ \
    if (ftruncate(barrier_file, 1) == -1) {\
        perror("Error setting size of shared memory");\
        exit(1);\
    }\
    char * barrier = mmap(NULL, 1, PROT_WRITE, MAP_SHARED, barrier_file, 0);\
    if (barrier == MAP_FAILED) {\
        perror("Error mapping shared memory");\
        exit(1);\
    }\
    int error;\
    int val;

#define SAVE_RESULTS if (jobsComplete > 0){\
            startS[jobsComplete]=start.tv_sec;\
            startN[jobsComplete]=start.tv_nsec;\
            endS[jobsComplete]=end.tv_sec;\
            endN[jobsComplete]=end.tv_nsec;}

#define WRITE_TO_FILE {\
    munlockall();\
    FILE *fp = fopen(fileName, "a");\
    if (fp == NULL) {\
        perror("Error opening file. \n");\
        exit(1);\
    }\
    for(jobsComplete=0; jobsComplete<maxJobs; jobsComplete++){\
        fprintf(fp, "%s %s %s %s %d %ld %ld %ld %ld %s %d \n",\
        thisProgram, otherProgram, thisCore, otherCore, maxJobs,\
        startS[jobsComplete], startN[jobsComplete], endS[jobsComplete], endN[jobsComplete],\
        runID, jobsComplete);\
    }\
    fclose(fp);\
    /* Clean up the barrier synchronization shared memory */\
    munmap(barrier, 1);\
    shm_unlink("/TacleBarrier");\
    free(startS);\
    free(startN);\
    free(endS);\
    free(endN);\
}

// Call the wbinvld instruction (it's in a kernel module due to it being ring-0)
#define FLUSH_CACHES FILE *fp = fopen("/proc/wbinvd", "r");\
    if (fp == NULL) {\
        perror("Cache flush module interface cannot be opened");\
        exit(1);\
    }\
    char dummy;\
    fread(&dummy, 1, 1, fp);\
    fclose(fp);

//invoke start timer twice, stop timer to make sure timer and vars are in cache
#define START_TIMER clock_gettime(CLOCK_MONOTONIC, &start);
    //clock_gettime(CLOCK_MONOTONIC, &end);\
    //clock_gettime(CLOCK_MONOTONIC, &start);\
        
#define STOP_TIMER clock_gettime(CLOCK_MONOTONIC, &end);

#define LOCK2_LOCKED //printf("Lock2 locked. \n");
#define LOCK1_LOCKED //printf("Lock1 locked. \n");
#define LOCK1_UNLOCKED //printf("Lock1 unlocked. \n");
#define LOCK2_UNLOCKED //printf("Lock2 unlocked. \n");

//check value of sem
//if sem=0, unlock
//if sem=1, spin

#define SLEEP nanosleep((const struct timespec[]){{0, 1000000}}, NULL);

//#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);


//#define SAFE_UNLOCK(whichSem) sem_post(whichSem); SLEEP ; printf("Used SAFE_UNLOCK. \n");

#define FIRST_UNLOCK if (lockID == 1) {\
        if (sem_post(secondSem) != 0) {\
            perror("Unable to unlock second semaphore");\
            exit(1);\
        }\
    } \
    else {\
        if (sem_post(firstSem) != 0) {\
            perror("Unable to unlock first semaphore");\
            exit(1);\
        }\
    } \

#define FIRST_LOCK if (lockID == 1) {\
        if (sem_wait(firstSem) != 0) {\
            perror("Unable to wait on first semaphore");\
            exit(1);\
        }\
    }\
    else {\
        if (sem_wait(secondSem) != 0) {\
            perror("Unable to wait on second semaphore");\
            exit(1);\
        }\
    }


#define SECOND_UNLOCK   if (lockID==1){sem_post(fourthSem) ; }\
            else {sem_post(thirdSem) ; }

#define SECOND_LOCK if (lockID==1){sem_wait(thirdSem); }\
            else {sem_wait(fourthSem); }

#define BARRIER_SYNC if (__sync_bool_compare_and_swap(barrier, 0, 1)) {\
        while (!__sync_bool_compare_and_swap(barrier, 0, 0)) {};\
    }\
    else {\
        __sync_bool_compare_and_swap(barrier, 1, 0);\
    }


//#define SECOND_UNLOCK if (lockID==1){sem_post(firstSem) ; LOCK1_UNLOCKED }\
            else {sem_post(secondSem) ; LOCK2_UNLOCKED }

//#define SECOND_LOCK   if (lockID==1){sem_wait(secondSem); LOCK2_LOCKED }\
            else {sem_wait(firstSem); LOCK1_LOCKED }


#define START_LOOP FIRST_UNLOCK FIRST_LOCK FLUSH_CACHES BARRIER_SYNC START_TIMER


/*
First task to reach TRY_CACHE will fail, but unlock the lock.
Second task will get the lock and kill the cache.
Lock remains in place until this point is reached again in code.
*/

//#define TRY_CACHE if (sem_trywait(cacheSem)==0){KILL_CACHE}\
        else {sem_post(cacheSem);}

//#define STOP_LOOP STOP_TIMER SAVE_RESULTS //TRY_CACHE

#define STOP_LOOP STOP_TIMER SAVE_RESULTS //SECOND_UNLOCK SECOND_LOCK KILL_CACHE


/*
Intended structure

main
SET_UP
notice that STOP LOOP negates the ++ if outout=0
for (jobsComplete=-1; jobsComplete<maxJobs; jobsComplete++){
        KILL_CACHE
        START_TIMER
        tacleInit();
        tacleMain();
        STOP_TIMER
        SAVE_RESULTS
}
WRITE_TO_FILE
tacleReturn 
*/