X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fcgtop%2Fcgtop.c;h=ee421e383b2eec9e2314a006d79daee71033a32a;hp=c439d09fd64c2da2e2415c64651f029bd27f8a4a;hb=28917d7dc711746795f7e6468c06c1983a5cdf53;hpb=a152771af17da095fc58302de5ea330f394f89c9 diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index c439d09fd..ee421e383 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -30,6 +30,7 @@ #include "util.h" #include "hashmap.h" #include "cgroup-util.h" +#include "build.h" typedef struct Group { char *path; @@ -56,6 +57,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 { @@ -298,7 +300,7 @@ static int refresh_one( r = cg_enumerate_subgroups(controller, path, &d); if (r < 0) { - if (r == ENOENT) + if (r == -ENOENT) return 0; return r; @@ -425,7 +427,7 @@ static int display(Hashmap *a) { Iterator i; Group *g; Group **array; - unsigned rows, n = 0, j; + unsigned rows, path_columns, n = 0, j; assert(a); @@ -445,13 +447,23 @@ static int display(Hashmap *a) { if (rows <= 0) rows = 25; - printf("%s%-37s%s %s%7s%s %s%6s%s %s%8s%s %s%8s%s %s%8s%s\n\n", - arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_ON : "", "Path", arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_OFF : "", - arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_ON : "", "Tasks", arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_OFF : "", - arg_order == ORDER_CPU ? ANSI_HIGHLIGHT_ON : "", "%CPU", arg_order == ORDER_CPU ? ANSI_HIGHLIGHT_OFF : "", - arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_ON : "", "Memory", arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_OFF : "", - arg_order == ORDER_IO ? ANSI_HIGHLIGHT_ON : "", "Input/s", arg_order == ORDER_IO ? ANSI_HIGHLIGHT_OFF : "", - arg_order == ORDER_IO ? ANSI_HIGHLIGHT_ON : "", "Output/s", arg_order == ORDER_IO ? ANSI_HIGHLIGHT_OFF : ""); + path_columns = columns() - 42; + if (path_columns < 10) + path_columns = 10; + + printf("%s%-*s%s %s%7s%s %s%6s%s %s%8s%s %s%8s%s %s%8s%s\n\n", + arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_ON : "", path_columns, "Path", + arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_OFF : "", + arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_ON : "", "Tasks", + arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_OFF : "", + arg_order == ORDER_CPU ? ANSI_HIGHLIGHT_ON : "", "%CPU", + arg_order == ORDER_CPU ? ANSI_HIGHLIGHT_OFF : "", + arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_ON : "", "Memory", + arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_OFF : "", + arg_order == ORDER_IO ? ANSI_HIGHLIGHT_ON : "", "Input/s", + arg_order == ORDER_IO ? ANSI_HIGHLIGHT_OFF : "", + arg_order == ORDER_IO ? ANSI_HIGHLIGHT_ON : "", "Output/s", + arg_order == ORDER_IO ? ANSI_HIGHLIGHT_OFF : ""); for (j = 0; j < n; j++) { char *p; @@ -462,8 +474,8 @@ static int display(Hashmap *a) { g = array[j]; - p = ellipsize(g->path, 37, 33); - printf("%-37s", p ? p : g->path); + p = ellipsize(g->path, path_columns, 33); + printf("%-*s", path_columns, p ? p : g->path); free(p); if (g->n_tasks_valid) @@ -500,6 +512,7 @@ static void help(void) { printf("%s [OPTIONS...]\n\n" "Show top control groups by their resource usage.\n\n" " -h --help Show this help\n" + " --version Print version and exit\n" " -p Order by path\n" " -t Order by number of tasks\n" " -c Order by CPU load\n" @@ -507,22 +520,30 @@ 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); } +static void version(void) { + puts(PACKAGE_STRING " cgtop"); +} + static int parse_argv(int argc, char *argv[]) { enum { - ARG_DEPTH = 0x100 + ARG_VERSION = 0x100, + ARG_DEPTH, }; static const struct option options[] = { - { "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 } + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, ARG_VERSION }, + { "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; @@ -531,7 +552,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) { @@ -539,6 +560,10 @@ static int parse_argv(int argc, char *argv[]) { help(); return 0; + case ARG_VERSION: + version(); + return 0; + case ARG_DEPTH: r = safe_atou(optarg, &arg_depth); if (r < 0) { @@ -566,6 +591,10 @@ static int parse_argv(int argc, char *argv[]) { break; + case 'b': + arg_batch = true; + break; + case 'p': arg_order = ORDER_PATH; break; @@ -624,6 +653,8 @@ int main(int argc, char *argv[]) { goto finish; } + signal(SIGWINCH, columns_cache_reset); + while (!quit) { Hashmap *c; usec_t t; @@ -655,17 +686,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 ' ': @@ -745,5 +784,10 @@ finish: group_hashmap_free(a); group_hashmap_free(b); - return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; + if (r < 0) { + log_error("Exiting with failure: %s", strerror(-r)); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; }