[systemd-commits] 2 commits - src/cgtop

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Thu Jul 26 11:29:53 PDT 2012


 src/cgtop/cgtop.c |   52 +++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 11 deletions(-)

New commits:
commit e66bb58bed3fe5ef152268ac257b2801a7679549
Author: David Strauss <david at davidstrauss.net>
Date:   Wed Jul 25 16:33:07 2012 -0700

    Add a 'b' option to cgtop, equivalent to the same option in top
    
    [zj: use static]

diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index c439d09..3009589 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -56,6 +56,7 @@ typedef struct 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 {
@@ -507,6 +508,7 @@ static void help(void) {
                "  -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);
 }
@@ -521,6 +523,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "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         }
         };
@@ -531,7 +534,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "hptcmin:d:", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "hptcmin:bd:", options, NULL)) >= 0) {
 
                 switch (c) {
 
@@ -566,6 +569,10 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case 'b':
+                        arg_batch = true;
+                        break;
+
                 case 'p':
                         arg_order = ORDER_PATH;
                         break;
@@ -655,17 +662,25 @@ int main(int argc, char *argv[]) {
                 if (arg_iterations && iteration >= arg_iterations)
                         break;
 
-                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_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 ' ':

commit a152771af17da095fc58302de5ea330f394f89c9
Author: David Strauss <david at davidstrauss.net>
Date:   Wed Jul 25 16:33:06 2012 -0700

    Add an 'n' option to cgtop (equivalent to top)

diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index f0ca83f..c439d09 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -55,6 +55,7 @@ typedef struct Group {
 } Group;
 
 static unsigned arg_depth = 3;
+static unsigned arg_iterations = 0;
 static usec_t arg_delay = 1*USEC_PER_SEC;
 
 static enum {
@@ -505,6 +506,7 @@ 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"
                "     --depth=DEPTH    Maximum traversal depth (default: 2)\n",
                program_invocation_short_name);
 }
@@ -516,10 +518,11 @@ 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'       },
+                { "depth",      required_argument, NULL, ARG_DEPTH },
+                { NULL,         0,                 NULL, 0         }
         };
 
         int c;
@@ -528,7 +531,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:d:", options, NULL)) >= 0) {
 
                 switch (c) {
 
@@ -554,6 +557,15 @@ 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 'p':
                         arg_order = ORDER_PATH;
                         break;
@@ -640,6 +652,9 @@ int main(int argc, char *argv[]) {
                 if (r < 0)
                         goto finish;
 
+                if (arg_iterations && iteration >= arg_iterations)
+                        break;
+
                 r = read_one_char(stdin, &key, last_refresh + arg_delay - t, NULL);
                 if (r == -ETIMEDOUT)
                         continue;



More information about the systemd-commits mailing list