-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.c
241 lines (202 loc) · 5.56 KB
/
logger.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*** CSE 2431 Project***/
/*** Jiaqian Huang ***/
/*** Andrew Maloney ***/
/*** Alec Wilson ***/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>
#define ITERATION 10
#define ALPHA 0.5
volatile int alarmFlag = 0;
long int nextBurstTime(long int, double, long int);
int areWeLogging();
int oneMoreTime();
int getPID();
int getTicks(int, long*, long*);
void alarmHandler(int sig){
alarmFlag = 1;
}
int main() {
//file pointer for log file and read file
FILE *logFile;
int neededPID = 1;
//longs to hold utimes and stimes (both are clock ticks)
long utime, stime;
long lutime, lstime;
utime=stime=lutime=lstime=0;
//longs to hold delta utime stime
long dutime, dstime;
//long to hold predicted next clock ticks
long taunu=10, tauns=10;
long ltaunu=10, ltauns=10;
//counter for loop
int loopCounter = 0;//loop variable
int dowhileCount = 1;//count iterations
//open file to log data at user's request
char response;
int logging=0;
//Create timer for intervals
struct itimerval timer;
int interval=500;
if(areWeLogging()){
logFile = fopen("log.txt", "w");
if(logFile == NULL){
printf("Could not open log file\n");
return 0;
}
logging=1;
}
//Get interval to poll proc files
printf("How fast would you like to report this data? (ms): ");
scanf("%d", &interval);
while(interval>999 || interval<20){
printf("Please enter a value between 20 and 999: ");
scanf("%d", &interval);
}
//loop if user wants to continue
do{
if(logging){
fprintf(logFile, "Iteration%d:", dowhileCount);
}
//get maps file path
neededPID = getPID();
if(logging){
fprintf(logFile, "PID%d\n", neededPID);
fprintf(logFile, "User System ticks\n");
}
//signal so that alarm can be used
signal(SIGALRM, alarmHandler);
//initialize alarmFlag to 0
alarmFlag = 0;
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = interval*1000;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = interval*1000;
setitimer(ITIMER_REAL, &timer, NULL);
//alarm(1);
loopCounter = 0;
//Iterate through process measurement ITERATION times
while(loopCounter<ITERATION){
//if alarm goes off
if(alarmFlag){
//get user ticks and system ticks
if(!getTicks(neededPID, &utime, &stime)){
return 0;
//close log file if necessary
if(logging){
fclose(logFile);
}
}
//write ticks
if(logging){
fprintf(logFile, "%lu %lu\n", utime, stime);
}
printf("\nUser Ticks: %lu\tSystem Ticks: %lu\n", utime, stime);
//Print difference if applicable
if(loopCounter>0){
dutime = utime-lutime;
dstime = stime-lstime;
taunu = nextBurstTime(dutime, ALPHA, ltaunu);
tauns = nextBurstTime(dstime, ALPHA, ltauns);
printf("User delta: %lu\tSystem delta: %lu\n", dutime, dstime);
printf("Next User: %lu\tNext System: %lu\n", taunu, tauns);
}
//update last tick count
lutime=utime;
lstime=stime;
ltaunu=taunu;
ltauns=tauns;
//increment loopCounter
loopCounter++;
//reset alarm
alarmFlag = 0;
//alarm(1);
}
}
dowhileCount++;
if(logging){
fprintf(logFile, "\n");
}
}while(oneMoreTime());
//close log file if necessary
if(logging){
fclose(logFile);
}
}
//Calculates next process burst time using exponential averaging algorithm
//Returns next process burst time (CPU ticks)
long int nextBurstTime(long int tn, double alpha, long int taun){
long int nextBurst;
nextBurst = alpha * tn + (1 - alpha) * taun;
return nextBurst;
}
//Ask user if data should be logged
//Returns 1 if logging should be done
// 0 otherwise
int areWeLogging(){
char response;
int invalid;
printf("Would you like to log this data? (Y/N): ");
response = getchar();
invalid = (response != 'Y' && response != 'y' && response != 'N' && response != 'n');
while(invalid){
printf("%c is not a valid input.\n", response);
printf("Would you like to log this data? (Y/N): ");
getchar();
response = getchar();
invalid = (response != 'Y' && response != 'y' && response != 'N' && response != 'n');
}
return (response == 'Y' || response == 'y');
}
//Polls to the user whether to run again or not
//Returns: 1 if user wants to run again
// 0 otherwise
int oneMoreTime(){
char cont;
int invalid;
printf("Would you like to run again? (Y/N): ");
getchar();
cont = getchar();
invalid = (cont != 'Y' && cont != 'y' && cont != 'N' && cont != 'n');
while(invalid){
printf("%c is not a valid input.\n", cont);
printf("Would you like to run again? (Y/N): ");
//getchar();
cont = getchar();
invalid = (cont != 'Y' && cont != 'y' && cont != 'N' && cont != 'n');
}
return(cont == 'Y' || cont == 'y');
}
//Gets file path to read process information based on user input
int getPID(){
int neededPID=1;
//get pid from user
printf("Please enter PID of process to measure: ");
scanf("%d", &neededPID);
return neededPID;
}
//Gets user and system ticks
//Replaces: utime, stime
//Returns: 1 if successful
// 0 otherwise
int getTicks(int neededPID, long* utime, long* stime){
//File pointer for read
FILE *readFile;
char filePath[30];
//create string for file path
sprintf(filePath, "/proc/%d/stat", neededPID);
//open /proc/<neededPID> folder
readFile = fopen(filePath, "r");
if(readFile == NULL){
printf("Could not open %s\n", filePath);
return 0;
}
//read utume and stime from readFile aka the scariest fscanf in the west
fscanf(readFile,"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", utime, stime);
//offset to beginning of file
//rewind(readFile);
fclose(readFile);
return 1;
}