-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoreRequest_Contended.cc
131 lines (107 loc) · 4.32 KB
/
CoreRequest_Contended.cc
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
#include <sys/wait.h>
#include <stdio.h>
#include <thread>
#include <atomic>
#include "CoreArbiter/CoreArbiterClient.h"
#include "CoreArbiter/Logger.h"
#include "PerfUtils/Cycles.h"
#include "PerfUtils/TimeTrace.h"
#include "PerfUtils/Util.h"
using PerfUtils::TimeTrace;
using PerfUtils::Cycles;
using CoreArbiter::CoreArbiterClient;
using namespace CoreArbiter;
#define NUM_TRIALS 1000
void highPriorityRequest(CoreArbiterClient* client,
volatile bool* lowPriorityRunning) {
client->blockUntilCoreAvailable();
// Wait until the other high priority thread is running
while (client->getNumOwnedCores() < 2);
for (int i = 0; i < NUM_TRIALS; i++) {
TimeTrace::record("About to request fewer cores");
client->setRequestedCores({1,0,0,0,0,0,0,0});
TimeTrace::record("Requested fewer cores");
while (client->getNumBlockedThreadsFromServer() == 0);
TimeTrace::record("High priority thread blocked");
while (!(*lowPriorityRunning));
TimeTrace::record("About to request more cores");
client->setRequestedCores({2,0,0,0,0,0,0,0});
TimeTrace::record("Requested more cores");
while(client->getNumBlockedThreads() == 1);
}
client->unregisterThread();
}
void highPriorityBlock(CoreArbiterClient* client) {
client->blockUntilCoreAvailable();
// Wait until the other high priority thread is running
while (client->getNumOwnedCores() < 2);
for (int i = 0; i < NUM_TRIALS; i++) {
while (!client->mustReleaseCore());
TimeTrace::record("High priority core release requested.");
client->blockUntilCoreAvailable();
TimeTrace::record("High priority core acquired.");
}
client->unregisterThread();
}
void lowPriorityExec(CoreArbiterClient* client,
volatile bool* lowPriorityRunning) {
std::vector<uint32_t> lowPriorityRequest = {0,0,0,0,0,0,0,1};
client->setRequestedCores(lowPriorityRequest);
client->blockUntilCoreAvailable();
// Wait for other process to join
while (client->getNumProcessesOnServer() == 1);
for (int i = 0; i < NUM_TRIALS; i++) {
while (!client->mustReleaseCore());
TimeTrace::record("Low priority core release requested");
*lowPriorityRunning = false;
client->blockUntilCoreAvailable();
TimeTrace::record("Low priority core acquired");
*lowPriorityRunning = true;
}
client->unregisterThread();
}
int main(){
Logger::setLogLevel(ERROR);
int sharedMemFd = open("benchmark_sharedmem",
O_CREAT | O_RDWR | O_TRUNC, S_IRWXU);
if (sharedMemFd < 0) {
fprintf(stderr, "Error opening shared memory page: %s\n",
strerror(errno));
return -1;
}
if (ftruncate(sharedMemFd, sizeof(bool)) == -1) {
fprintf(stderr, "Error truncating sharedMemFd: %s\n",
strerror(errno));
return -1;
}
volatile bool* lowPriorityRunning = (bool*)mmap(NULL, getpagesize(),
PROT_READ | PROT_WRITE, MAP_SHARED,
sharedMemFd, 0);
if (lowPriorityRunning == MAP_FAILED) {
fprintf(stderr, "Error on global stats mmap: %s\n", strerror(errno));
return -1;
}
*lowPriorityRunning = false;
pid_t pid = fork();
if (pid == 0) {
CoreArbiterClient* client =
CoreArbiterClient::getInstance("/tmp/CoreArbiter/testsocket");
// Wait for the low priority thread to be put on a core
while (client->getNumUnoccupiedCores() == 2);
client->setRequestedCores({2,0,0,0,0,0,0,0});
std::thread highPriorityThread1(highPriorityBlock, std::ref(client));
std::thread highPriorityThread2(highPriorityRequest, std::ref(client),
lowPriorityRunning);
highPriorityThread1.join();
highPriorityThread2.join();
TimeTrace::setOutputFileName("CoreRequest_Contended_HighPriority.log");
TimeTrace::print();
} else {
CoreArbiterClient* client =
CoreArbiterClient::getInstance("/tmp/CoreArbiter/testsocket");
lowPriorityExec(client, lowPriorityRunning);
TimeTrace::setOutputFileName("CoreRequest_Contended_LowPriority.log");
TimeTrace::print();
wait(NULL);
}
}