about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--mincore.c30
-rw-r--r--pcu-mincore.1.txt9
2 files changed, 31 insertions, 8 deletions
diff --git a/mincore.c b/mincore.c
index 4bdbd0c..cafb63d 100644
--- a/mincore.c
+++ b/mincore.c
@@ -1,8 +1,10 @@
 #include "compat-util.h"
+static int summary;
 
 static int usage(const char * argv0)
 {
-        fprintf(stderr, "Usage: %s [-o OFFSET] [-l LENGTH] FILE...\n", argv0);
+        fprintf(stderr,
+                "Usage: %s [-o OFFSET] [-l LENGTH] [-s] FILE...\n", argv0);
         return 1;
 }
 
@@ -57,10 +59,19 @@ static void mincore_stats(const char *path, off_t offset, off_t len)
                 goto err_munmap;
         }
 
-        for (i = 0; i < vec_len; ++i)
-                printf(fmt, path,
-                       (unsigned long)((page_size() * i) + map_offset),
-                       vec[i] & 1);
+        if (summary) {
+                size_t n = 0;
+
+                for (i = 0; i < vec_len; ++i)
+                        if (vec[i] & 1)
+                                ++n;
+                printf("%s: %F\n", path, (double)n / (double)vec_len);
+        } else {
+                for (i = 0; i < vec_len; ++i)
+                        printf(fmt, path,
+                               (unsigned long)((page_size() * i) + map_offset),
+                               vec[i] & 1);
+        }
 err_munmap:
         munmap(map, map_len);
 err_free:
@@ -76,12 +87,13 @@ int main(int argc, char * const argv[])
         int argi = 1;
         int opt;
 
-        while ((opt = getopt(argc, argv, "o:l:h")) != -1) {
+        while ((opt = getopt(argc, argv, "o:l:hs")) != -1) {
                 char *err;
 
-                argi += 2;
+                ++argi;
                 switch(opt) {
                 case 'o':
+                        ++argi;
                         offset = cstr_to_off_t(optarg, &err, 10);
                         if (*err || offset < 0) {
                                 fprintf(stderr, "offset must be >= 0\n");
@@ -89,12 +101,16 @@ int main(int argc, char * const argv[])
                         }
                         break;
                 case 'l':
+                        ++argi;
                         len = cstr_to_off_t(optarg, &err, 10);
                         if (*err || len < 0) {
                                 fprintf(stderr, "length must be >= 0\n");
                                 return 1;
                         }
                         break;
+                case 's':
+                        summary = 1;
+                        break;
                 default:
                         return usage(argv[0]);
                 }
diff --git a/pcu-mincore.1.txt b/pcu-mincore.1.txt
index a3a8300..e0d9075 100644
--- a/pcu-mincore.1.txt
+++ b/pcu-mincore.1.txt
@@ -6,7 +6,7 @@
 pcu-mincore - shows cache status for given files
 
 # SYNOPSIS
-pcu-mincore [-o OFFSET] [-l LENGTH] FILE...
+pcu-mincore [-o OFFSET] [-l LENGTH] [-s] FILE...
 
 # DESCRIPTION
 A command-line interface to the mincore(2) syscall to inspect
@@ -24,7 +24,14 @@ resident memory.
     This length is rounded up next page offset.
     The default is to scan the entire file.
 
+-s
+:   Display the summary output as a floating point number between
+    0.0 and 1.0 representing the percentage of the file in core.
+
 # OUTPUT
+The following output is displayed unless "-s" summary format
+is chosen.
+
 One line for every page scanned in the following format:
 
    \<filename\>: \<offset\> \<incore\>