-
Notifications
You must be signed in to change notification settings - Fork 53
/
nocache.c
535 lines (431 loc) · 14.9 KB
/
nocache.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
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <assert.h>
#include <signal.h>
#include "pageinfo.h"
#include "fcntl_helpers.h"
static void init(void) __attribute__((constructor));
static void destroy(void) __attribute__((destructor));
static void init_mutexes(void);
static void init_debugging(void);
static void handle_stdout(void);
static void store_pageinfo(int fd);
static void free_unclaimed_pages(int fd, bool block_signals);
int open(const char *pathname, int flags, mode_t mode);
int open64(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, int flags, mode_t mode);
int creat64(const char *pathname, int flags, mode_t mode);
int openat(int dirfd, const char *pathname, int flags, mode_t mode);
int openat64(int dirfd, const char *pathname, int flags, mode_t mode);
int __openat_2(int dirfd, const char *pathname, int flags, mode_t mode)
__attribute__ ((alias ("openat")));
int dup(int oldfd);
int dup2(int oldfd, int newfd);
int close(int fd);
FILE *fopen(const char *path, const char *mode);
FILE *fopen64(const char *path, const char *mode);
int fclose(FILE *fp);
int (*_original_open)(const char *pathname, int flags, mode_t mode);
int (*_original_open64)(const char *pathname, int flags, mode_t mode);
int (*_original_creat)(const char *pathname, int flags, mode_t mode);
int (*_original_creat64)(const char *pathname, int flags, mode_t mode);
int (*_original_openat)(int dirfd, const char *pathname, int flags, mode_t mode);
int (*_original_openat64)(int dirfd, const char *pathname, int flags, mode_t mode);
int (*_original_dup)(int fd);
int (*_original_dup2)(int newfd, int oldfd);
int (*_original_close)(int fd);
FILE *(*_original_fopen)(const char *path, const char *mode);
FILE *(*_original_fopen64)(const char *path, const char *mode);
int (*_original_fclose)(FILE *fp);
/* Info about a file descriptor 'fd' is stored in fds[fd]. Before accessing an
* element, callers MUST both check fds_lock != NULL and then acquire
* fds_lock[fd] while holding the fds_iter_lock. While any mutex in fds_lock is
* held, fds_lock will not be freed and set to NULL. */
static int max_fds;
static struct file_pageinfo *fds;
static pthread_mutex_t *fds_lock;
static pthread_mutex_t fds_iter_lock;
static int max_fd_observed; /* guarded by fds_iter_lock */
static size_t PAGESIZE;
static char *env_nr_fadvise = "NOCACHE_NR_FADVISE";
static int nr_fadvise;
static char *env_debugfd = "NOCACHE_DEBUGFD";
int debugfd = -1;
FILE *debugfp;
static char *env_flushall = "NOCACHE_FLUSHALL";
static char flushall;
static char *env_max_fds = "NOCACHE_MAX_FDS";
static rlim_t max_fd_limit = 1 << 20;
#define DEBUG(...) \
do { \
if(debugfp != NULL) { \
fprintf(debugfp, "[nocache] DEBUG: " __VA_ARGS__); \
} \
} while(0)
static void init(void)
{
int i;
char *s;
char *error;
struct rlimit rlim;
if((s = getenv(env_nr_fadvise)) != NULL)
nr_fadvise = atoi(s);
if(nr_fadvise <= 0)
nr_fadvise = 1;
if((s = getenv(env_flushall)) != NULL)
flushall = atoi(s);
if(flushall <= 0)
flushall = 0;
if((s = getenv(env_max_fds)) != NULL)
max_fd_limit = atoll(s);
getrlimit(RLIMIT_NOFILE, &rlim);
max_fds = rlim.rlim_max;
if(max_fds > max_fd_limit)
max_fds = max_fd_limit;
if(max_fds == 0)
return; /* There's nothing to do for us here. */
init_mutexes();
/* make sure to re-initialize mutex if forked */
pthread_atfork(NULL, NULL, init_mutexes);
fds = malloc(max_fds * sizeof(*fds));
assert(fds != NULL);
_original_open = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open");
_original_open64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open64");
_original_creat = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "creat");
_original_creat64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "creat64");
_original_openat = (int (*)(int, const char *, int, mode_t)) dlsym(RTLD_NEXT, "openat");
_original_openat64 = (int (*)(int, const char *, int, mode_t)) dlsym(RTLD_NEXT, "openat64");
_original_dup = (int (*)(int)) dlsym(RTLD_NEXT, "dup");
_original_dup2 = (int (*)(int, int)) dlsym(RTLD_NEXT, "dup2");
_original_close = (int (*)(int)) dlsym(RTLD_NEXT, "close");
_original_fopen = (FILE *(*)(const char *, const char *)) dlsym(RTLD_NEXT, "fopen");
_original_fopen64 = (FILE *(*)(const char *, const char *)) dlsym(RTLD_NEXT, "fopen64");
_original_fclose = (int (*)(FILE *)) dlsym(RTLD_NEXT, "fclose");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
PAGESIZE = getpagesize();
pthread_mutex_lock(&fds_iter_lock);
for(i = 0; i < max_fds; i++) {
pthread_mutex_lock(&fds_lock[i]);
fds[i].fd = -1;
pthread_mutex_unlock(&fds_lock[i]);
}
max_fd_observed = 0;
pthread_mutex_unlock(&fds_iter_lock);
init_debugging();
handle_stdout();
}
static void init_mutexes(void)
{
int i;
pthread_mutex_init(&fds_iter_lock, NULL);
pthread_mutex_lock(&fds_iter_lock);
fds_lock = malloc(max_fds * sizeof(*fds_lock));
assert(fds_lock != NULL);
for(i = 0; i < max_fds; i++) {
pthread_mutex_init(&fds_lock[i], NULL);
}
pthread_mutex_unlock(&fds_iter_lock);
}
static void init_debugging(void)
{
char *s = getenv(env_debugfd);
if(!s)
return;
debugfd = atoi(s);
debugfp = fdopen(debugfd, "a");
}
/* duplicate stdout if it is a regular file. We will use this later to
* fadvise(DONTNEED) on it, although the real stdout was already
* closed. This makes "nocache tar cfz" work properly. */
static void handle_stdout(void)
{
int fd;
struct stat st;
if(fstat(STDOUT_FILENO, &st) == -1 || !S_ISREG(st.st_mode))
return;
fd = fcntl_dupfd(STDOUT_FILENO, 23);
if(fd == -1)
return;
store_pageinfo(fd);
}
/* try to advise fds that were not manually closed */
static void destroy(void)
{
int i;
int max_fd_to_clear;
sigset_t mask, old_mask;
/* There is a race condition here: If something opens files after
* extracting max_fd_observed, then it is possible we miss cleaning it up
* in the shutdown path. */
pthread_mutex_lock(&fds_iter_lock);
max_fd_to_clear = max_fd_observed;
pthread_mutex_unlock(&fds_iter_lock);
/* We block signals here, and then call free_unclaimed_pages in a loop. As
* max_fds may be high (millions), it's very wasteful to block signals
* repeatedly, so it's done once here. */
sigfillset(&mask);
sigprocmask(SIG_BLOCK, &mask, &old_mask);
for(i = 0; i < max_fd_to_clear; i++) {
free_unclaimed_pages(i, false);
}
sigprocmask(SIG_SETMASK, &old_mask, NULL);
pthread_mutex_lock(&fds_iter_lock);
if(fds_lock == NULL) {
pthread_mutex_unlock(&fds_iter_lock);
return;
}
for(i = 0; i < max_fds; i++) {
pthread_mutex_lock(&fds_lock[i]);
}
/* We have acquired all locks. It is now safe to delete and free the list. */
free(fds);
fds = NULL;
free(fds_lock);
fds_lock = NULL;
pthread_mutex_unlock(&fds_iter_lock);
}
int open(const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_open)
_original_open = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open");
assert(_original_open != NULL);
if((fd = _original_open(pathname, flags, mode)) != -1) {
DEBUG("open(pathname=%s, flags=0x%x, mode=0%o) = %d\n",
pathname, flags, mode, fd);
store_pageinfo(fd);
}
return fd;
}
int open64(const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_open64)
_original_open64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open64");
assert(_original_open64 != NULL);
DEBUG("open64(pathname=%s, flags=0x%x, mode=0%o)\n", pathname, flags, mode);
if((fd = _original_open64(pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
}
int creat(const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_creat)
_original_creat = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "creat");
assert(_original_creat != NULL);
DEBUG("creat(pathname=%s, flags=0x%x, mode=0%o)\n", pathname, flags, mode);
if((fd = _original_creat(pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
}
int creat64(const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_creat64)
_original_creat64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "creat64");
assert(_original_creat64 != NULL);
DEBUG("creat64(pathname=%s, flags=0x%x, mode=0%o)\n", pathname, flags, mode);
if((fd = _original_creat64(pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
}
int openat(int dirfd, const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_openat)
_original_openat = (int (*)(int, const char *, int, mode_t)) dlsym(RTLD_NEXT, "openat");
assert(_original_openat != NULL);
DEBUG("openat(dirfd=%d, pathname=%s, flags=0x%x, mode=0%o)\n", dirfd, pathname, flags, mode);
if((fd = _original_openat(dirfd, pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
}
int openat64(int dirfd, const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_openat64)
_original_openat64 = (int (*)(int, const char *, int, mode_t)) dlsym(RTLD_NEXT, "openat64");
assert(_original_openat64 != NULL);
DEBUG("openat64(dirfd=%d, pathname=%s, flags=0x%x, mode=0%o)\n", dirfd, pathname, flags, mode);
if((fd = _original_openat64(dirfd, pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
}
int dup(int oldfd)
{
int fd;
if(!_original_dup)
_original_dup = (int (*)(int)) dlsym(RTLD_NEXT, "dup");
assert(_original_dup != NULL);
DEBUG("dup(oldfd=%d)\n", oldfd);
if((fd = _original_dup(oldfd)) != -1)
store_pageinfo(fd);
return fd;
}
int dup2(int oldfd, int newfd)
{
int ret;
/* if newfd is already opened, the kernel will close it directly
* once dup2 is invoked. So now is the last chance to mark the
* pages as "DONTNEED" */
if(valid_fd(newfd))
free_unclaimed_pages(newfd, true);
if(!_original_dup2)
_original_dup2 = (int (*)(int, int)) dlsym(RTLD_NEXT, "dup2");
assert(_original_dup2 != NULL);
DEBUG("dup2(oldfd=%d, newfd=%d)\n", oldfd, newfd);
if((ret = _original_dup2(oldfd, newfd)) != -1)
store_pageinfo(newfd);
return ret;
}
int close(int fd)
{
if(!_original_close)
_original_close = (int (*)(int)) dlsym(RTLD_NEXT, "close");
assert(_original_close != NULL);
free_unclaimed_pages(fd, true);
DEBUG("close(%d)\n", fd);
return _original_close(fd);
}
FILE *fopen(const char *path, const char *mode)
{
int fd;
FILE *fp = NULL;
if(!_original_fopen)
_original_fopen = (FILE *(*)(const char *, const char *)) dlsym(RTLD_NEXT, "fopen");
assert(_original_fopen != NULL);
DEBUG("fopen(path=%s, mode=%s)\n", path, mode);
if((fp = _original_fopen(path, mode)) != NULL)
if((fd = fileno(fp)) != -1)
store_pageinfo(fd);
return fp;
}
FILE *fopen64(const char *path, const char *mode)
{
int fd;
FILE *fp;
fp = NULL;
if(!_original_fopen64)
_original_fopen64 = (FILE *(*)(const char *, const char *)) dlsym(RTLD_NEXT, "fopen64");
assert(_original_fopen64 != NULL);
DEBUG("fopen64(path=%s, mode=%s)\n", path, mode);
if((fp = _original_fopen64(path, mode)) != NULL)
if((fd = fileno(fp)) != -1)
store_pageinfo(fd);
return fp;
}
int fclose(FILE *fp)
{
if(!_original_fclose)
_original_fclose = (int (*)(FILE *)) dlsym(RTLD_NEXT, "fclose");
assert(_original_fclose != NULL);
if(_original_fclose) {
free_unclaimed_pages(fileno(fp), true);
return _original_fclose(fp);
}
errno = EFAULT;
return EOF;
}
static void store_pageinfo(int fd)
{
sigset_t mask, old_mask;
if(fd >= max_fds)
return;
/* We might know something about this fd already, so assume we have missed
* it being closed. */
free_unclaimed_pages(fd, true);
sigfillset(&mask);
sigprocmask(SIG_BLOCK, &mask, &old_mask);
pthread_mutex_lock(&fds_iter_lock);
if(fds_lock == NULL) {
pthread_mutex_unlock(&fds_iter_lock);
return;
}
pthread_mutex_lock(&fds_lock[fd]);
if(fd > max_fd_observed)
max_fd_observed = fd;
pthread_mutex_unlock(&fds_iter_lock);
/* Hint we'll be using this file only once;
* the Linux kernel will currently ignore this */
fadv_noreuse(fd, 0, 0);
fds[fd].fd = fd;
if(flushall)
goto out;
if(!fd_get_pageinfo(fd, &fds[fd])) {
fds[fd].fd = -1;
goto out;
}
DEBUG("store_pageinfo(fd=%d): pages in cache: %zd/%zd (%.1f%%) [filesize=%.1fK, "
"pagesize=%dK]\n", fd, fds[fd].nr_pages_cached, fds[fd].nr_pages,
fds[fd].nr_pages == 0 ? 0 : (100.0 * fds[fd].nr_pages_cached / fds[fd].nr_pages),
1.0 * fds[fd].size / 1024, (int) PAGESIZE / 1024);
out:
pthread_mutex_unlock(&fds_lock[fd]);
sigprocmask(SIG_SETMASK, &old_mask, NULL);
return;
}
static void free_unclaimed_pages(int fd, bool block_signals)
{
struct stat st;
sigset_t mask, old_mask;
if(fd == -1 || fd >= max_fds)
return;
if(block_signals) {
sigfillset(&mask);
sigprocmask(SIG_BLOCK, &mask, &old_mask);
}
pthread_mutex_lock(&fds_iter_lock);
if(fds_lock == NULL) {
pthread_mutex_unlock(&fds_iter_lock);
return;
}
pthread_mutex_lock(&fds_lock[fd]);
if(fd > max_fd_observed)
max_fd_observed = fd;
pthread_mutex_unlock(&fds_iter_lock);
if(fds[fd].fd == -1)
goto out;
sync_if_writable(fd);
if(flushall) {
DEBUG("fadv_dontneed(fd=%d, from=0, len=0 [till end])\n", fd);
fadv_dontneed(fd, 0, 0, nr_fadvise);
fds[fd].fd = -1;
goto out;
}
if(fstat(fd, &st) == -1)
goto out;
struct byterange *br;
for(br = fds[fd].unmapped; br; br = br->next) {
DEBUG("fadv_dontneed(fd=%d, from=%zd, len=%zd)\n", fd, br->pos, br->len);
fadv_dontneed(fd, br->pos, br->len, nr_fadvise);
}
/* Has the file grown bigger? */
if(st.st_size > fds[fd].size) {
DEBUG("fadv_dontneed(fd=%d, from=%lld, len=0 [till new end, file has grown])\n",
fd, (long long)fds[fd].size);
fadv_dontneed(fd, fds[fd].size, 0, nr_fadvise);
}
free_br_list(&fds[fd].unmapped);
fds[fd].fd = -1;
out:
pthread_mutex_unlock(&fds_lock[fd]);
if(block_signals)
sigprocmask(SIG_SETMASK, &old_mask, NULL);
}
/* vim:set et sw=4 ts=4: */