chiark / gitweb /
Add a 'b' option to cgtop, equivalent to the same option in top
[elogind.git] / src / cgtop / cgtop.c
index ddb57094b1c527328ee1b152f8660babec1f9b39..3009589597325246cccfbff32b699b220374097f 100644 (file)
@@ -55,6 +55,8 @@ typedef struct Group {
 } Group;
 
 static unsigned arg_depth = 3;
+static unsigned arg_iterations = 0;
+static bool arg_batch = false;
 static usec_t arg_delay = 1*USEC_PER_SEC;
 
 static enum {
@@ -310,7 +312,7 @@ static int refresh_one(
                 if (r <= 0)
                         goto finish;
 
-                p = join(path, "/", fn, NULL);
+                p = strjoin(path, "/", fn, NULL);
                 free(fn);
 
                 if (!p) {
@@ -341,17 +343,22 @@ static int refresh(Hashmap *a, Hashmap *b, unsigned iteration) {
 
         r = refresh_one("name=systemd", "/", a, b, iteration, 0);
         if (r < 0)
-                return r;
-
+                if (r != -ENOENT)
+                    return r;
         r = refresh_one("cpuacct", "/", a, b, iteration, 0);
         if (r < 0)
-                return r;
-
+                if (r != -ENOENT)
+                    return r;
         r = refresh_one("memory", "/", a, b, iteration, 0);
         if (r < 0)
-                return r;
+                if (r != -ENOENT)
+                    return r;
 
-        return refresh_one("blkio", "/", a, b, iteration, 0);
+        r = refresh_one("blkio", "/", a, b, iteration, 0);
+        if (r < 0)
+                if (r != -ENOENT)
+                    return r;
+        return 0;
 }
 
 static int group_compare(const void*a, const void *b) {
@@ -500,6 +507,8 @@ static void help(void) {
                "  -m                  Order by memory load\n"
                "  -i                  Order by IO load\n"
                "  -d --delay=DELAY    Specify delay\n"
+               "  -n --iterations=N   Run for N iterations before exiting\n"
+               "  -b --batch          Run in batch mode, accepting no input\n"
                "     --depth=DEPTH    Maximum traversal depth (default: 2)\n",
                program_invocation_short_name);
 }
@@ -511,10 +520,12 @@ static int parse_argv(int argc, char *argv[]) {
         };
 
         static const struct option options[] = {
-                { "help",  no_argument,       NULL, 'h'       },
-                { "delay", required_argument, NULL, 'd'       },
-                { "depth", required_argument, NULL, ARG_DEPTH },
-                { NULL,    0,                 NULL, 0         }
+                { "help",       no_argument,       NULL, 'h'       },
+                { "delay",      required_argument, NULL, 'd'       },
+                { "iterations", required_argument, NULL, 'n'       },
+                { "batch",      no_argument,       NULL, 'b'       },
+                { "depth",      required_argument, NULL, ARG_DEPTH },
+                { NULL,         0,                 NULL, 0         }
         };
 
         int c;
@@ -523,7 +534,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "hptcmid:", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "hptcmin:bd:", options, NULL)) >= 0) {
 
                 switch (c) {
 
@@ -549,6 +560,19 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case 'n':
+                        r = safe_atou(optarg, &arg_iterations);
+                        if (r < 0) {
+                                log_error("Failed to parse iterations parameter.");
+                                return -EINVAL;
+                        }
+
+                        break;
+
+                case 'b':
+                        arg_batch = true;
+                        break;
+
                 case 'p':
                         arg_order = ORDER_PATH;
                         break;
@@ -603,8 +627,7 @@ int main(int argc, char *argv[]) {
         a = hashmap_new(string_hash_func, string_compare_func);
         b = hashmap_new(string_hash_func, string_compare_func);
         if (!a || !b) {
-                log_error("Out of memory");
-                r = -ENOMEM;
+                r = log_oom();
                 goto finish;
         }
 
@@ -636,17 +659,28 @@ int main(int argc, char *argv[]) {
                 if (r < 0)
                         goto finish;
 
-                r = read_one_char(stdin, &key, last_refresh + arg_delay - t, NULL);
-                if (r == -ETIMEDOUT)
-                        continue;
-                if (r < 0) {
-                        log_error("Couldn't read key: %s", strerror(-r));
-                        goto finish;
+                if (arg_iterations && iteration >= arg_iterations)
+                        break;
+
+                if (arg_batch) {
+                        usleep(last_refresh + arg_delay - t);
+                } else {
+                        r = read_one_char(stdin, &key,
+                                          last_refresh + arg_delay - t, NULL);
+                        if (r == -ETIMEDOUT)
+                                continue;
+                        if (r < 0) {
+                                log_error("Couldn't read key: %s", strerror(-r));
+                                goto finish;
+                        }
                 }
 
                 fputs("\r \r", stdout);
                 fflush(stdout);
 
+                if (arg_batch)
+                        continue;
+
                 switch (key) {
 
                 case ' ':