GithubHelp home page GithubHelp logo

Comments (3)

pavlinux avatar pavlinux commented on May 23, 2024 1
$ echo a > -v
$ cachestats -v

Shoot yourself in the foot, then complain about the gunsmiths.

from nocache.

VorpalBlade avatar VorpalBlade commented on May 23, 2024

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.

pavlinux avatar pavlinux commented on May 23, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.