-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-fincore.c
590 lines (409 loc) · 14.6 KB
/
linux-fincore.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include <ctype.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux-ftools.h>
#include <math.h>
#include <errno.h>
#include <locale.h>
#include <sys/ioctl.h>
char STR_FORMAT[] = "%-80s %18s %18s %18s %18s %18s %18s\n";
char DATA_FORMAT[] = "%-80s %'18ld %'18d %'18ld %'18ld %'18ld %18.2f\n";
long DEFAULT_NR_REGIONS = 160; // default number of regions
// program options
int arg_pages = 0; // display/print pages we've found. Used for external programs.
int arg_summarize = 1; // print a summary at the end.
int arg_only_cached = 0; // only show cached files
int arg_graph = 0; // graph the page distribution of files.
int arg_verbose = 0; // level of verbosity.
int arg_vertical = 0; // print variables vertical
long arg_min_size = -1; // required minimum size for files or we ignore them.
long arg_min_cached_size = -1; // required minimum percent cached for files or we ignore them.
long arg_min_cached_perc = -1; // required minimum percent of a file in cache.
long nr_regions = 0;
// Array of longs of the NR of blocks per region (used for graphing)
static long *regions = NULL;
// Array of doubles of the percentages of blocks per region.
static double *region_percs = NULL;
//TODO:
//
// - ability to adjust print graph width and height width from the command line.
//
// - convert ALL the variables used to store stats to longs.
//
// - option to print symbols in human readable form (1GB, 2.1GB , 300KB
//
// - only call setlocale() if it's not currently set.
//
// - print new headers every N rows.
struct fincore_result
{
long cached_size;
};
int _argtobool( char* str ) {
int result = 0;
if ( optarg != NULL ) {
if ( strcmp( "true", optarg ) == 0 ) {
result = 1;
}
if ( strcmp( "1", optarg ) == 0 ) {
result = 1;
}
} else {
result = 1;
}
return result;
}
int _argtoint( char* str, int _default ) {
int result = _default;
if ( optarg != NULL ) {
result = atoi( str );
}
return result;
}
char *_itoa(int n) {
static char retbuf[100];
sprintf(retbuf, "%d", n);
return retbuf;
}
char *_ltoa( long value ) {
static char buff[100];
sprintf( buff, "%ld", value );
return buff;
}
char *_dtoa( double value ) {
static char buff[100];
sprintf( buff, "%f", value );
return buff;
}
double perc( long val, int range ) {
if ( range == 0 )
return 0;
double result = ( val / (double)range ) * 100;
return result;
}
void graph_header( long nr_regions ) {
int j;
printf( " ------" );
for( j = 0; j < nr_regions; ++j ) {
printf( "-" );
}
printf( "\n" );
}
void graph(double regions[], long nr_regions ) {
printf( "\n" );
graph_header(nr_regions);
int *ptr;
int i, j;
int perc_width = 10;
for( i = 0 ; i < perc_width; ++ i ) {
double perc_index = 100 - ((i+1) * perc_width );
// show where we are.
printf( "%4.0f %% ", perc_index + perc_width );
for( j = 0; j < nr_regions; ++j ) {
double val = regions[j];
if ( val < 1 ) {
printf( " " );
} else if ( val >= perc_index ) {
printf( "*" );
} else {
printf( " " );
}
}
printf( " | \n" );
}
graph_header(nr_regions);
printf( "\n" );
}
void fincore(char* path,
struct fincore_result *result ) {
int fd;
struct stat file_stat;
void *file_mmap;
// vector result from mincore
unsigned char *mincore_vec;
// default page size of this OS
size_t page_size = getpagesize();
size_t page_index;
int i;
// the number of pages that we have detected that are cached
size_t cached = 0;
// the number of pages that we have printed
size_t printed = 0;
//the total number of pages we're working with
size_t total_pages;
//the oldest page in the cache or -1 if it isn't in the cache.
off_t min_cached_page = -1 ;
// by default the cached size is zero so initialize this member.
result->cached_size = 0;
//FIXME: make this sizeof(regions)
if ( regions == NULL )
regions = calloc( nr_regions , sizeof(long) ) ;
if ( region_percs == NULL )
region_percs = calloc( nr_regions , sizeof(double) ) ;
if ( regions == NULL || region_percs == NULL ) {
perror( "Could not allocate memory" );
goto cleanup;
}
for( i = 0; i < nr_regions; ++i ) {
regions[i] = (long)0;
region_percs[i] = (double)0;
}
fd = open( path, O_RDONLY );
if ( fd == -1 ) {
//FIXME: migrate this code to a decated function for dealing with errors.
char buff[1024];
sprintf( buff, "Could not open file: %s", path );
perror( buff );
return;
}
if ( fstat( fd, &file_stat ) < 0 ) {
char buff[1024];
sprintf( buff, "Could not stat file: %s", path );
perror( buff );
goto cleanup;
}
if ( file_stat.st_size == 0 ) {
// this file could not have been cached as it's empty so anything we
//would do is pointless.
goto cleanup;
}
file_mmap = mmap((void *)0, file_stat.st_size, PROT_NONE, MAP_SHARED, fd, 0 );
if ( file_mmap == MAP_FAILED ) {
char buff[1024];
sprintf( buff, "Could not mmap file: %s", path );
perror( buff );
goto cleanup;
}
size_t calloc_size = (file_stat.st_size+page_size-1)/page_size;
mincore_vec = calloc(1, calloc_size);
if ( mincore_vec == NULL ) {
perror( "Could not calloc" );
exit( 1 );
}
if ( mincore(file_mmap, file_stat.st_size, mincore_vec) != 0 ) {
char buff[1024];
sprintf( buff, "%s: Could not call mincore on file", path );
perror( buff );
exit( 1 );
}
total_pages = (int)ceil( (double)file_stat.st_size / (double)page_size );
int region_ptr = (int)((total_pages - 1) / nr_regions);
for (page_index = 0; page_index <= file_stat.st_size/page_size; page_index++) {
if (mincore_vec[page_index]&1) {
++cached;
if ( min_cached_page == -1 || page_index < min_cached_page ) {
min_cached_page = page_index;
}
if ( arg_pages ) {
printf("%lu ", (unsigned long)page_index);
++printed;
}
if ( region_ptr > 0 ) {
int region = (int)(page_index / region_ptr );
++regions[region];
}
}
}
if ( printed ) printf("\n");
double cached_perc = 100 * (cached / (double)total_pages);
size_t cached_size = (long)cached * (long)page_size;
if ( arg_min_cached_size > 0 && cached_size <= arg_min_cached_size ) {
goto cleanup;
}
if ( arg_min_cached_perc > 0 && cached_perc <= arg_min_cached_perc ) {
goto cleanup;
}
if ( arg_min_size > 0 && file_stat.st_size <= arg_min_size ) {
goto cleanup;
}
if ( arg_only_cached == 0 || cached > 0 ) {
if ( arg_graph ) {
_show_headers();
}
if ( arg_vertical ) {
printf( "%s\n", path );
printf( "size: %'ld\n", file_stat.st_size );
printf( "total_pages: %'ld\n", total_pages );
printf( "min_cached_page: %'ld\n", min_cached_page );
printf( "cached: %'ld\n", cached );
printf( "cached_size: %'ld\n", cached_size );
printf( "cached_perc: %.2f\n", cached_perc );
} else {
printf( DATA_FORMAT,
path,
file_stat.st_size ,
total_pages ,
min_cached_page,
cached ,
cached_size ,
cached_perc );
}
for( i = 0 ; i < nr_regions; ++i ) {
region_percs[i] = perc(regions[i], region_ptr );
}
if ( arg_graph && total_pages > nr_regions ) {
graph( region_percs, nr_regions );
}
}
// update structure members when done.
result->cached_size = cached_size;
cleanup:
if ( mincore_vec != NULL ) {
free(mincore_vec);
mincore_vec = NULL;
}
if ( file_mmap != MAP_FAILED )
munmap(file_mmap, file_stat.st_size);
if ( fd != -1 )
close(fd);
return;
}
// print help / usage
void help() {
fprintf( stderr, "%s version %s\n", "fincore", LINUX_FTOOLS_VERSION );
fprintf( stderr, "fincore [options] files...\n" );
fprintf( stderr, "\n" );
fprintf( stderr, " -s --summarize When comparing multiple files, print a summary report\n" );
fprintf( stderr, " -p --pages Print pages that are cached\n" );
fprintf( stderr, " -o --only-cached Only print stats for files that are actually in cache.\n" );
fprintf( stderr, " -g --graph Print a visual graph of each file's cached page distribution.\n" );
fprintf( stderr, " -S --min-size Require that each files size be larger than N bytes.\n" );
fprintf( stderr, " -C --min-cached-size Require that each files cached size be larger than N bytes.\n" );
fprintf( stderr, " -P --min-perc-cached Require percentage of a file that must be cached.\n" );
fprintf( stderr, " -h --help Print this message.\n" );
fprintf( stderr, " -L --vertical Print the output of this script vertically.\n" );
fprintf( stderr, "\n" );
}
void _show_headers() {
printf( STR_FORMAT, "filename", "size", "total_pages", "min_cached page", "cached_pages", "cached_size", "cached_perc" );
printf( STR_FORMAT, "--------", "----", "-----------", "---------------", "------------", "-----------", "-----------" );
return;
}
/**
* see README
*/
int main(int argc, char *argv[]) {
int c;
static struct option long_options[] =
{
/* These options don't set a flag.
We distinguish them by their indices. */
{"summarize", optional_argument, 0, 's'},
{"pages", optional_argument, 0, 'p'},
{"only-cached", optional_argument, 0, 'c'},
{"graph", optional_argument, 0, 'g'},
{"verbose", optional_argument, 0, 'v'},
{"min-size", required_argument, 0, 'S'},
{"min-cached-size", required_argument, 0, 'C'},
{"min-cached-perc", required_argument, 0, 'P'},
{"help", no_argument, 0, 'h'},
{"vertical", no_argument, 0, 'L'},
{0, 0, 0, 0}
};
while (1) {
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "s::p::c::g::v::L::S:C:P:h",long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
//bool foo = 1;
switch (c)
{
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'p':
arg_pages = _argtobool( optarg );
break;
case 's':
arg_summarize = _argtobool( optarg );
break;
case 'c':
arg_only_cached = _argtobool( optarg );
break;
case 'g':
arg_graph = _argtobool( optarg );
break;
case 'v':
arg_verbose = _argtoint( optarg, 1 );
break;
case 'S':
arg_min_size = _argtoint( optarg, 0 );
break;
case 'C':
arg_min_cached_size = _argtoint( optarg, 0 );
break;
case 'P':
arg_min_cached_perc = _argtoint( optarg, 0 );
break;
case 'L':
arg_vertical = _argtoint( optarg, 1 );
break;
case 'h':
help();
exit(1);
case '?':
/* getopt_long already printed an error message. */
break;
default:
fprintf( stderr, "Invalid command line item: %s\n" , argv[ optind ] );
help();
exit(1);
}
} // done processing arguments.
if ( arg_verbose >= 1 ) {
printf( "Running with arguments: \n" );
printf( " pages: %d\n", arg_pages );
printf( " summarize: %d\n", arg_summarize );
printf( " only cached: %d\n", arg_only_cached );
printf( " graph: %d\n", arg_graph );
printf( " min size: %ld\n", arg_min_size );
printf( " min cached size: %ld\n", arg_min_cached_size );
printf( " min cached perc: %ld\n", arg_min_cached_perc );
printf( " vertical: %ld\n", arg_vertical );
}
setlocale( LC_NUMERIC, "en_US" );
nr_regions = DEFAULT_NR_REGIONS;
struct winsize ws;
if (ioctl(stdout,TIOCGWINSZ,&ws) == 0) {
if ( ws.ws_col > nr_regions ) {
nr_regions = ((ws.ws_col/2)*2) - 10;
}
//printf( "Using graph width: %d\n" , nr_regions );
}
if ( optind == argc ) {
help();
exit(1);
}
if ( ! arg_graph )
_show_headers();
long total_cached_size = 0;
/* Print any remaining command line arguments (not options). */
if (optind < argc) {
while (optind < argc) {
char* path = argv[optind++];
struct fincore_result result;
fincore( path, &result );
total_cached_size += result.cached_size;
//printf ("%s ", );
//putchar ('\n');
}
}
if ( arg_summarize ) {
printf( "---\n" );
//TODO: add more metrics including total size...
printf( "total cached size: %'ld\n", total_cached_size );
}
}