about summary refs log tree commit homepage
path: root/mincore.c
diff options
context:
space:
mode:
Diffstat (limited to 'mincore.c')
-rw-r--r--mincore.c30
1 files changed, 23 insertions, 7 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]);
                 }