Comments (3)
$ echo a > -v $ cachestats -v
Shoot yourself in the foot, then complain about the gunsmiths.
from nocache.
Looking at the code of cachedel I have no idea why I thought it handled -- correctly. It did not give me any error, which made me trust it worked correctly. I now believe it simply ended up doing nothing somehow.
from nocache.
index 7e9d9f6..27b6e2b 100644
--- a/cachestats.c
+++ b/cachestats.c
@@ -1,9 +1,11 @@
+#define _GNU_SOURCE /* O_DIRECT */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
+#include <getopt.h>
#include <string.h>
#include <error.h>
#include <stdio.h>
@@ -14,6 +16,14 @@ int exiterr(const char *s)
exit(-1);
}
+static const struct option longOpts[] = {
+ { "verbose", required_argument, NULL, 'v' },
+ { "quiet", required_argument, NULL, 'q' },
+ { "help", no_argument, NULL, 'h' },
+ { "?", no_argument, NULL, '?' },
+ { NULL, no_argument, NULL, 0 }
+};
+
int main(int argc, char *argv[])
{
int i, j;
@@ -22,57 +32,85 @@ int main(int argc, char *argv[])
int quiet = 0;
int verbose = 0;
+ int opt = -1;
+ int long_index = -1;
int fd;
struct stat st;
void *file = NULL;
+ char *name = NULL;
unsigned char *pageinfo = NULL;
PAGESIZE = getpagesize();
- if(argc > 1) {
- if(!strcmp("-v", argv[1]))
- verbose = 1;
- else if(!strcmp("-q", argv[1]))
- quiet = 1;
+ if (argv[2]) {
+ while ((opt = getopt_long_only(argc, argv, "", longOpts, &long_index)) != -1) {
+ /* short arg will be filename */
+ switch (opt) {
+ case 'v':
+ verbose = 1;
+ name = optarg;
+ break;
+ case 'q':
+ quiet = 1;
+ name = optarg;
+ break;
+ case 'h':
+ case '?':
+ default:
+ fprintf(stderr, "usage: %s [--quiet | --verbose] <file> "
+ "-- print out cache statistics\n", argv[0]);
+ fprintf(stderr, "\t--verbose\tprint verbose cache map\n");
+ fprintf(stderr, "\t--quiet\texit code tells if file is fully cached\n");
+ exit(1);
+ break;
+ }
+ }
} else {
- fprintf(stderr, "usage: %s [-qv] <file> "
- "-- print out cache statistics\n", argv[0]);
- fprintf(stderr, "\t-v\tprint verbose cache map\n");
- fprintf(stderr, "\t-q\texit code tells if file is fully cached\n");
- exit(1);
+ name = argv[1];
}
- if(quiet || verbose)
- argv++;
+ if (name == NULL)
+ exit(1);
- fd = open(argv[1], O_RDONLY);
+ fd = open(name, O_RDONLY|O_DIRECT|O_SYNC|O_NDELAY);
if(fd == -1)
exiterr("open");
if(fstat(fd, &st) == -1)
exiterr("fstat");
if(!S_ISREG(st.st_mode)) {
- fprintf(stderr, "%s: S_ISREG: not a regular file", argv[1]);
+ fprintf(stderr, "%s: S_ISREG: not a regular file", optarg);
+ close(fd);
return EXIT_FAILURE;
}
if(st.st_size == 0) {
- printf("pages in cache: %d/%d (%.1f%%) [filesize=%.1fK, "
- "pagesize=%dK]\n", 0, 0, 0.0,
- 0.0, PAGESIZE / 1024);
+ if (verbose) {
+ printf("pages in cache: %d/%d (%.1f%%) [filesize=%.1fK, "
+ "pagesize=%dK]\n", 0, 0, 0.0,
+ 0.0, PAGESIZE / 1024);
+ }
+ close(fd);
return EXIT_SUCCESS;
}
pages = (st.st_size + PAGESIZE - 1) / PAGESIZE;
pageinfo = calloc(sizeof(*pageinfo), pages);
- if(!pageinfo)
+ if(!pageinfo) {
+ close(fd);
exiterr("calloc");
+ }
file = mmap(NULL, st.st_size, PROT_NONE, MAP_SHARED, fd, 0);
- if(file == MAP_FAILED)
+ if(file == MAP_FAILED) {
+ close(fd);
exiterr("mmap");
- if(mincore(file, st.st_size, pageinfo) == -1)
+ }
+ if(mincore(file, st.st_size, pageinfo) == -1) {
+ munmap(file, st.st_size);
+ close(fd);
exiterr("mincore");
+ }
i = j = 0;
while(i < pages)
@@ -80,6 +118,8 @@ int main(int argc, char *argv[])
j++;
if(quiet) {
+ munmap(file, st.st_size);
+ close(fd);
if(j == i)
return EXIT_SUCCESS;
return EXIT_FAILURE;
@@ -100,10 +140,11 @@ int main(int argc, char *argv[])
printf("%c|",
pageinfo[i * PAGES_PER_LINE + j] & 1 ? 'x' : ' ');
}
- printf("\n");
+ printf("\n");
}
}
munmap(file, st.st_size);
+ close(fd);
return EXIT_SUCCESS;
}
from nocache.
Related Issues (20)
- Support O_DIRECT? HOT 1
- Incompatibility with glibc 2.28 HOT 23
- Do SubProcess calls work as expected. HOT 2
- "transmission-gtk" freezes right after downloading because of "nocache" HOT 4
- nocache.c:148: init_mutexes: Assertion `fds_lock != NULL' failed. HOT 3
- nocache makes MC very slow HOT 2
- Doesn't work for pv in conjunction with physical partitions/disks? HOT 3
- GCC cache size flags HOT 3
- Off-by-one issue with NOCACHE_MAX_FDS HOT 6
- Lock pages from preventing of being paged and tune malloc. HOT 1
- Trivial suggestions for README.md
- effect on write cache HOT 1
- Default Thread limit is not respecting container cpu limit but underline node(vm) HOT 2
- Mention no help for huge files
- unexpected nocache overhead on trivial commands HOT 14
- BUG: pthread_mutex_lock on destroy HOT 3
- nocache works, systemd-run doesn't
- backup still using cache HOT 1
- FTBFS on armhf: symbol `fopen64' is already defined
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nocache.