-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslices-in-pagecache.c
140 lines (127 loc) · 3.76 KB
/
slices-in-pagecache.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
/*
* Copyright (c) 2006-2015, Loic Tortay <tortay@cc.in2p3.fr>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* slices-in-pagecache: try to infer which parts of files are actually in
* pagecache.
* All these files must be readable by the user.
*/
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "errwarn.h"
const char progname[] = "slices-in-pagecache";
void print_slice(int, long int, long int, long int);
void
print_slice(int sdx, long int pgsz, long int slstart, long int slend)
{
/* sim: pages in memory in "this slice", "+ 1" since slstart is
* 0-based
*/
unsigned long sim = (slend + 1 - slstart) / pgsz;
printf("\tSlice[%d]: %lu:%lu (%lu pages)\n", sdx, slstart, slend, sim);
}
int main(int argc, char *argv[])
{
struct stat st;
void *filemap = NULL;
#ifdef __linux__
unsigned char *pages = NULL;
#else
char *pages = NULL;
#endif
size_t lip; /* lip: length in pages */
unsigned long pim, /* pim: pages in memory counter */ k;
long int pagesize, slice_start, slice_end;
int i, fd, in_a_slice, sindex;
pagesize = sysconf(_SC_PAGESIZE);
if (pagesize == -1)
error(1, errno, "Unable to get pagesize");
else
printf("Pagesize is: %ld bytes.\n", pagesize);
for (i = 1; i < argc; i++) {
fd = open(argv[i], O_RDONLY);
if (fd != -1) {
if (fstat(fd, &st) == -1) {
warning(errno, "Unable to stat '%s'", argv[i]);
continue;
}
if (st.st_size > 0) {
filemap = mmap(NULL, st.st_size, PROT_READ,
MAP_SHARED, fd, 0);
if (filemap == MAP_FAILED) {
warning(errno, "Unable to map '%s'",
argv[i]);
continue;
}
lip = (st.st_size + pagesize - 1) / pagesize;
pages = malloc(lip);
if (pages == NULL)
error(2, errno, "Unable to allocate "
"pages[%lu]", (unsigned long) lip);
if (mincore(filemap, st.st_size, pages) == -1) {
warning(errno, "Unable to get core info"
" for '%s'", argv[i]);
continue;
}
pim = 0;
sindex = slice_start = slice_end = in_a_slice = 0;
printf("'%s':\n", argv[i]);
for (k = 0; k < lip; k++) {
if (pages[k] & 1) {
if (!in_a_slice) {
in_a_slice = 1;
slice_start = pagesize
* k;
slice_end = slice_start
+ pagesize - 1;
} else {
slice_end += pagesize;
}
pim++;
} else if (in_a_slice) {
in_a_slice = 0;
print_slice(sindex, pagesize,
slice_start, slice_end);
sindex++;
}
}
if (in_a_slice) {
/* last page of the file in pagecache ? */
print_slice(sindex, pagesize,
slice_start, slice_end);
}
printf("\t%lu pages out of %lu appear to be in "
"pagecache\n", pim, (unsigned long) lip);
free(pages);
pages = NULL;
if (munmap(filemap, st.st_size) == -1) {
warning(errno, "Unable to unmap '%s'",
argv[i]);
continue;
}
}
if (close(fd) == -1)
warning(errno, "Problem closing '%s'", argv[i]);
} else {
warning(errno, "Unable to open '%s'", argv[i]);
}
}
return (0);
}