Repository navigation
Expand file tree
/
Copy pathresident.c
More file actions
222 lines (192 loc) · 5.55 KB
/
Copy pathresident.c
File metadata and controls
222 lines (192 loc) · 5.55 KB
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
#define _GNU_SOURCE
#include "../common/log.h"
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
int statx_helper(const char *path) {
LOG_INFO("statx", "path: %s", path);
struct statx stx = {0};
if (statx(AT_FDCWD, path, 0, STATX_BASIC_STATS | STATX_DIOALIGN, &stx) ==
-1) {
int err = errno;
LOG_ERROR("statx", "statx failed: %s", strerror(err));
return -1;
}
printf("blksize: %u, mode: %o, ino: %llu, size: %llu, blocks: %llu\n",
stx.stx_blksize, stx.stx_mode, (unsigned long long)stx.stx_ino,
(unsigned long long)stx.stx_size, (unsigned long long)stx.stx_blocks);
/* The kernel decides what it actually filled in; a field whose mask bit is
clear reads as 0 because of the {0} init, which is indistinguishable from
a real zero unless we check. */
if (stx.stx_mask & STATX_DIOALIGN)
printf("dio_mem_align: %u, dio_offset_align: %u\n", stx.stx_dio_mem_align,
stx.stx_dio_offset_align);
else
printf(
"dio: unsupported on this filesystem (STATX_DIOALIGN not returned)\n");
printf("\n");
return 0;
}
static bool getBit(unsigned char item, int i) {
// Return true if the bit is
// set. Otherwise return false
return ((item & (1 << i)) != 0);
}
static void human_size(double bytes, char *out, size_t outsz) {
static const char *u[] = {"B", "KiB", "MiB", "GiB", "TiB"};
int i = 0;
while (bytes >= 1024.0 && i < 4) {
bytes /= 1024.0;
i++;
}
snprintf(out, outsz, "%.1f %s", bytes, u[i]);
}
int resident_helper(const char *path, int *file_fd, struct stat *statbuf) {
char *file_address = NULL;
size_t file_size = 0;
int err = 0;
bool ok = false;
unsigned char *vec = NULL;
long page_size = -1;
*file_fd = open(path, O_RDONLY);
if (*file_fd == -1) {
err = errno;
LOG_ERROR("resident", "failed to open the file");
goto cleanup;
}
if (fstat(*file_fd, statbuf) == -1) {
err = errno;
LOG_ERROR("resident", "failed to fstat the file");
goto cleanup;
}
/* Only valid once fstat has filled statbuf -- reading it earlier picks up
uninitialised stack from main(). */
file_size = (size_t)statbuf->st_size;
if (file_size == 0) {
LOG_ERROR("resident", "empty file: mmap of length 0 is EINVAL");
goto cleanup;
}
file_address = mmap(NULL, file_size, PROT_READ, MAP_SHARED, *file_fd, 0);
if (file_address == MAP_FAILED) {
err = errno;
LOG_ERROR("resident", "failed to mmap the file");
goto cleanup;
}
page_size = sysconf(_SC_PAGESIZE);
if (page_size == -1) {
err = errno;
LOG_ERROR("resident", "failed to sysconf for _SC_PAGESIZE");
goto cleanup;
}
size_t npages = (file_size + page_size - 1) / page_size;
vec = malloc(npages);
if (vec == NULL) {
err = errno;
LOG_ERROR("resident", "failed malloc");
goto cleanup;
}
if (mincore(file_address, file_size, vec) == -1) {
err = errno;
LOG_ERROR("resident", "failed to mincore");
goto cleanup;
}
size_t group = 0;
size_t total = 0;
LOG_INFO("resident", "starting to visualising mincore pages");
for (size_t idx = 0; idx < npages; idx++) {
if (idx % 64 == 0) {
printf("page %8zu ", idx);
}
if (idx % 8 == 0 && idx % 64 != 0) {
putchar(' ');
}
if (getBit(vec[idx], 0)) {
group++;
putchar('#');
} else {
putchar('.');
}
/* close the line: filled 64 columns, OR ran out of pages */
if (idx % 64 == 63 || idx == npages - 1) {
size_t in_line = (idx % 64) + 1;
printf(" %3zu/%zu\n", group, in_line);
total += group;
group = 0;
}
}
/* A resident page contributes a whole page_size except possibly the last
one, which is partial -- capping at file_size keeps the total honest. */
size_t resident_bytes = total * (size_t)page_size;
if (resident_bytes > file_size) {
resident_bytes = file_size;
}
char resident_str[32];
char file_str[32];
human_size((double)resident_bytes, resident_str, sizeof resident_str);
human_size((double)file_size, file_str, sizeof file_str);
printf("\nresident: %zu / %zu pages (%.1f%%) %s / %s\n", total, npages,
100.0 * (double)total / (double)npages, resident_str, file_str);
fflush(stdout);
ok = true;
LOG_INFO("resident", "finished visualising mincore pages");
cleanup:
if (file_address != NULL) {
munmap(file_address, file_size);
}
if (vec != NULL) {
free(vec);
}
if (ok) {
return 0;
} else {
if (err != 0) {
LOG_ERROR("resident", "internal error: %s", strerror(err));
}
return -1;
}
}
int main(int argc, char *argv[]) {
bool ok = false;
int file_fd = -1;
struct stat statbuf;
if (argc != 2) {
LOG_ERROR("resident", "argc incorrect: %d", argc);
goto shutdown;
}
const char *path = argv[1];
// DISK DATA
printf("--------------------------------------\n");
printf("DISK DATA\n");
printf("--------------------------------------\n");
if (statx_helper(path) == -1) {
ok = false;
goto shutdown;
}
printf("--------------------------------------\n");
printf("RAM DATA\n");
printf("--------------------------------------\n");
// RAM DATA
if (resident_helper(path, &file_fd, &statbuf) == -1) {
ok = false;
goto shutdown;
}
ok = true;
shutdown:
if (file_fd != -1) {
close(file_fd);
}
if (ok) {
LOG_INFO("resident", "shutdown successfull");
return EXIT_SUCCESS;
} else {
LOG_ERROR("resident", "shutdown failed");
return EXIT_FAILURE;
}
}