-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc-stats.c
253 lines (213 loc) · 6.54 KB
/
proc-stats.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
242
243
244
245
246
247
248
249
250
251
252
253
#include <pthread.h>
#define WAIT_TIME 100000
#define BLOCK_DEV_STAT_COUNT 11
struct proc_stats{
// /proc/meminfo
// Buffers: 1046936 kB
uint64_t buffers_kb;
// Cached: 14549024 kB
uint64_t cached_kb;
// Dirty: 820 kB
uint64_t dirty_kb;
// Writeback: 0 kB
uint64_t writeback_kb;
// /sys/block/<dev/stats> https://www.kernel.org/doc/Documentation/block/stat.txt
uint64_t blockdev_stats[BLOCK_DEV_STAT_COUNT];
};
typedef struct proc_stats proc_stats_t;
static char *stat_names_self_io[] = {"rchar:", "wchar:", "syscr:", "syscw:", "read_bytes:", "write_bytes:", "cancelled_write_bytes:", NULL};
static char *stat_names_meminfo[] = {"Buffers:", "Cached:", "Dirty:", "Writeback:", NULL};
static char *stat_names_blockdev[] = {"read I/Os", "read merges", "read sectors", "read ticks", "write I/Os", "write merges", "write sectors", "write ticks", "in_flight", "io_ticks", "time_in_queue", NULL};
struct proc_stats_pp{
double time;
// /proc/self/stat
uint64_t rchar;
uint64_t wchar;
uint64_t syscr; // read syscalls
uint64_t syscw; // write syscalls
uint64_t read_bytes; // actually fetched from the storage layer
uint64_t write_bytes; // actually written to the storage layer
uint64_t cancelled_write_bytes; // e.g., data deleted before written
};
typedef struct proc_stats_pp proc_stats_pp_t;
static volatile int finish_background_thread = 0;
static pthread_t thread;
static proc_stats_t * proc_stats;
static proc_stats_pp_t * proc_stats_pp;
static int pos_proc_stats = 0;
static void addProcStats(int pos){
proc_stats_t * p = proc_stats;
FILE * f = fopen("/proc/meminfo", "r");
char buff[2048];
int ret;
long long unsigned value;
char ** curStatName = stat_names_meminfo;
uint64_t * curValue = & p[pos].buffers_kb;
while(*curStatName != NULL){
ret = fscanf(f, "%s %llu", buff, & value);
if (ret != 2){
continue;
}
ret = strcmp(buff, *curStatName);
if (ret == 0){
*curValue = value;
curStatName++;
curValue++;
}
}
fclose(f);
sprintf(buff, "/sys/block/%s/stat", o.deviceName);
f = fopen(buff, "r");
if (f == NULL){
printf("Error reading from %s\n", buff);
}else{
for(int i=0; i < BLOCK_DEV_STAT_COUNT; i++){
ret = fscanf(f, "%lu", & p[pos].blockdev_stats[i]);
}
fclose(f);
}
}
static void addProcPPStats(int pos, double time){
proc_stats_pp_t * p = proc_stats_pp;
p[pos].time = time;
FILE * f = fopen("/proc/self/io", "r");
char buff[1023];
int ret;
long long unsigned value;
uint64_t * curValue = & p[pos].rchar;
for( int i=0; i < 7 ; i++ ){
ret = fscanf(f, "%s %llu", buff, & value);
if (ret != 2){
printf("Error accessing /proc/self/io, read only %d tokens\n", ret);
fclose(f);
return;
}
ret = strcmp(buff, stat_names_self_io[i]);
if (ret != 0){
printf("%s %s", buff, stat_names_self_io[i]);
printf("Error accessing /proc/self/io\n");
fclose(f);
return;
}else{
*curValue = value;
curValue++;
}
}
fclose(f);
}
static void * background_thread(void * arg){
Timer t;
pos_proc_stats = 0;
double ft;
while(! finish_background_thread){
timerStart(& t);
ft = timeSinceStart(t);
addProcPPStats(pos_proc_stats, ft);
addProcStats(pos_proc_stats);
// store current values from proc into: proc_stats_t
usleep(WAIT_TIME);
pos_proc_stats++;
}
timerStart(& t);
ft = timeSinceStart(t);
addProcPPStats(pos_proc_stats, ft);
addProcStats(pos_proc_stats);
pos_proc_stats++;
return NULL;
}
static void * background_thread_proc(void * arg){
Timer t;
pos_proc_stats=0;
double ft;
while(! finish_background_thread){
timerStart(& t);
ft = timeSinceStart(t);
addProcPPStats(pos_proc_stats, ft);
sleep(1);
pos_proc_stats++;
}
timerStart(& t);
ft = timeSinceStart(t);
addProcPPStats(pos_proc_stats, ft);
pos_proc_stats++;
return NULL;
}
void start_background_threads(int rank, int repeats){
// assume at most 1s per I/O operation ...
proc_stats_pp = (proc_stats_pp_t*) mmalloc(sizeof(proc_stats_pp_t) * repeats*10+2);
if (rank == 0){
proc_stats = (proc_stats_t*) mmalloc(sizeof(proc_stats_t) * repeats*10+2);
pthread_create(& thread, NULL, background_thread, NULL);
}else{
pthread_create(& thread, NULL, background_thread_proc, NULL);
}
}
void stop_background_threads(int rank){
finish_background_thread = 1;
int retval;
pthread_join(thread, (void *) & retval);
}
void dumpStats(int rank, size_t done_repeats){
char buff[4096];
int pos = 0;
for( int i=0; i < 7 ; i++ ){
float delta = (float) *(& proc_stats_pp[pos_proc_stats-1].rchar + i) - *(& proc_stats_pp[0].rchar + i);
pos += sprintf(buff + pos, "%s %.1f ", stat_names_self_io[i], delta / done_repeats);
}
pos += sprintf(buff + pos, "\n");
if(rank == 0){
fprintf(r.outputFile, "%d: %s", 0, buff);
for(int i=1; i < size; i++){
MPI_Recv(buff, 4096, MPI_BYTE, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
fprintf(r.outputFile, "%d: %s", i, buff);
}
}else{
MPI_Send(buff, 4096, MPI_BYTE, 0, 0, MPI_COMM_WORLD);
}
char fname[100];
sprintf(fname, "stats-%d.csv", rank);
FILE * out = fopen(fname, "w");
proc_stats_pp_t * pp = proc_stats_pp;
proc_stats_t * p = proc_stats;
fprintf(out, "start_time");
char ** curStatName = stat_names_self_io;
while(*curStatName != NULL){
fprintf(out, ",self_%s", *curStatName);
curStatName++;
}
if (rank == 0){
curStatName = stat_names_meminfo;
while(*curStatName != NULL){
fprintf(out, ",meminfo_%s", *curStatName);
curStatName++;
}
for(int i=0; i < BLOCK_DEV_STAT_COUNT; i++){
fprintf(out, ",blockdev_%s", stat_names_blockdev[i]);
}
}
fprintf(out, "\n");
for(int pos=0; pos < pos_proc_stats; pos++){
fprintf(out, "%.9f", pp[pos].time);
curStatName = stat_names_self_io;
uint64_t * curValue = & pp[pos].rchar;
while(*curStatName != NULL){
fprintf(out, ",%ld", *curValue);
curStatName++;
curValue++;
}
if (rank == 0){
curStatName = stat_names_meminfo;
curValue = & p[pos].buffers_kb;
while(*curStatName != NULL){
fprintf(out, ",%ld", *curValue);
curStatName++;
curValue++;
}
for(int i=0; i < BLOCK_DEV_STAT_COUNT; i++){
fprintf(out, ",%ld", p[pos].blockdev_stats[i]);
}
}
fprintf(out, "\n");
}
fclose(out);
}