-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1CpuMeasurements.cpp
209 lines (184 loc) · 6.03 KB
/
part1CpuMeasurements.cpp
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
209
#include "part1CpuMeasurements.h"
#include <stdio.h>
#include "rdtsc.h"
#include <math.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <pthread.h>
//#include <time.h>
// Don't forget to disable 3 of the 4 cores!! This is done by executing
// echo 0 > /sys/devices/system/cpu/cpu1/online
// for each of the cpus (cpu1, cpu2 and cpu3 (cpu0 cant be turned off))
// you can also see which CPUs are online with "lscpu"
// Or, alternitively
// You can execute program with "taskset -c 0 ./a.out" to ensure
// that it executes on a single core
// Also don't forget that to use the pthread stuff you have to append
// "-lpthread" to the very end of the compile command to gcc
// Download rdtsc.h here: (don't need to do this if you copied my folder)
// https://www.mcs.anl.gov/~kazutomo/rdtsc. h
// We might have to change line 18 to: (don't do this yet!!)
// __asm__ __volatile__ ("cpuid;" "rdtsc" : "=a"(lo), "=d"(hi));
// We might have to turn off caching at some point (don't do this yet)
// https://geteventstore.com/blog/20131218/disabling-disk-caching-in-ubuntu/
// you MIGHT** have to download hdparm here:
// https://sourceforge.net/projects/hdparm/
int tempGlobal = 88;
int main(){
printf("hello world!!!!!!!! WOOOOOOO!! %d \n", tempGlobal);
// Calculate overhead of reading TIME
int clockNumTrials = 1;
double clockMean;
double clockStDev;
measureTimeMeasurement(clockNumTrials, clockMean, clockStDev);
printf("Time Mean: %0.2f, Time Standard Deviation: %0.2f \n", clockMean, clockStDev);
return 0;
}
// function to get the data for 4.1.1 - Operations: CPU Scheduleing
// and OS services: Measurement Overhead.
// numTimesToTest: number of times to test
// mean and stDev are passed by reference and will be updated with
// the appropriate values
void measureTimeMeasurement(int numTrials, double &mean, double &stDev){
double means[numTrials];
for(int i=0; i<numTrials; i++){
means[i] = measureTimeMeasurementHelper(1);
printf("Trial %d: %0.2f \n", i, means[i]);
calcStDev(means, numTrials);
calcMean(means, numTrials);
//usleep(100000);
}
mean = calcMean(means, numTrials);
stDev = calcStDev(means, numTrials);
}
// helper function for measureTimeMeasurement
// this checks the time 1000 times and takes the mean
double measureTimeMeasurementHelper(int numTimesToTest) {
unsigned long long results[numTimesToTest];
unsigned long long beg, end;
struct timespec test;
pid_t pid;
pthread_t thread1;
int temp;
int myPipe[2];
for(int i=0; i<numTimesToTest; i++){
//beg = rdtsc();
//beg = 0;
//for(int j=0; j<10; j++){
//asm(""); // command Marc talked about that should order things?
//temp = dummy(temp, temp, temp, temp, temp, temp, temp, temp);
//tempGlobal = dummy(temp);
//asm("");
//syscall(SYS_getpid);
/*
if(pthread_create(&thread1, NULL, threadFunction, &temp)){
fprintf(stderr, "Something went wrong creating thread!!");
printf("something went wrong creating thread!!!!");
return 1;
}
if(pthread_join(thread1, NULL)){
printf("error joining thread! :(");
return 2;
}
//printf("threads joined now?");
*/
temp = pipe(myPipe);
if(temp==-1){
perror("problem making pipe!");
}
pid = fork();
if(pid==0){ // Child process
close(myPipe[0]);
//sleep(.1);
beg = rdtsc();
write(myPipe[1], &beg, sizeof(beg));
pthread_yield();
_exit(1);
} else if(pid > 0) {
// parent process;
read(myPipe[0], &beg, sizeof(beg));
end = rdtsc();
//wait(NULL);
//printf("Parent process. beg = %lld \n", beg);
} else {
// error
printf("Problem using fork()!!!");
perror("problem executing fork()");
exit(1);
}
//}
//end = rdtsc();
//tempGlobal = temp+temp;
//temp = temp+temp;
//printf("measurementOverhead: %llu\n", end-beg);
// need to flush cache here? Maybe not...
//calcStDev(results, numTimesToTest);
results[i] = end-beg;
//temp = temp+8;
}
return calcMean(results, numTimesToTest);
}
// function to run in the kernel thread
void* threadFunction(void *void_pointer){
//printf("in thread Function");
return NULL;
}
// dummy functions to test overhead of calling a function
int dummy(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8){return x1+x2+x3+x4+x5+x6+x7+x8;}
int dummy(int x1, int x2, int x3, int x4, int x5, int x6, int x7){return x1+x2+x3+x4+x5+x6+x7;}
int dummy(int x1, int x2, int x3, int x4, int x5, int x6){return x1+x2+x3+x4+x5+x6;}
int dummy(int x1, int x2, int x3, int x4, int x5){return x1+x2+x3+x4+x5;}
int dummy(int x1, int x2, int x3, int x4){return x1+x2+x3+x4;}
int dummy(int x1, int x2, int x3){return x1+x2+x3;}
int dummy(int x1, int x2){return x1+x2;}
int dummy(int x1){return x1+1;}
int dummy(){int test = tempGlobal; return test;}
// function to calculate the mean of an array
double calcMean(unsigned long long data[], int length){
double answer = 0;
for(int i=0; i<length; i++){
answer += data[i];
}
answer = answer/length;
return answer;
}
// function to calculate the standard deviation of an array
double calcStDev(unsigned long long data[], int length){
double mean = 0;
double stDev = 0;
for(int i=0; i<length; i++){
mean += data[i];
}
mean = mean/length;
for(int i=0; i<length; i++){
stDev += ((data[i]-mean)*(data[i]-mean));
}
stDev = stDev/length;
return sqrt(stDev);
}
// function to calculate the mean of an array
double calcMean(double data[], int length){
double answer = 0;
for(int i=0; i<length; i++){
answer += data[i];
}
answer = answer/length;
return answer;
}
// function to calculate the standard deviation of an array
double calcStDev(double data[], int length){
double mean = 0;
double stDev = 0;
for(int i=0; i<length; i++){
mean += data[i];
}
mean = mean/length;
for(int i=0; i<length; i++){
stDev += ((data[i]-mean)*(data[i]-mean));
}
stDev = stDev/length;
return sqrt(stDev);
}