X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcgtop%2Fcgtop.c;h=f80d51efed60cfe31e56e8ed3b752188c83b5466;hb=a4783bd17ad96f55b0fe83a50959da13555292bf;hp=3009589597325246cccfbff32b699b220374097f;hpb=e66bb58bed3fe5ef152268ac257b2801a7679549;p=elogind.git diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 300958959..f80d51efe 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -30,6 +30,8 @@ #include "util.h" #include "hashmap.h" #include "cgroup-util.h" +#include "build.h" +#include "fileio.h" typedef struct Group { char *path; @@ -299,7 +301,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; @@ -426,7 +428,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); @@ -442,17 +444,27 @@ static int display(Hashmap *a) { qsort(array, n, sizeof(Group*), group_compare); - rows = fd_lines(STDOUT_FILENO); - 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 : ""); + rows = lines(); + if (rows <= 10) + rows = 10; + + 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; @@ -463,8 +475,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) @@ -501,6 +513,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" @@ -513,19 +526,25 @@ static void help(void) { 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' }, - { "batch", no_argument, NULL, 'b' }, - { "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; @@ -542,6 +561,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) { @@ -631,6 +654,8 @@ int main(int argc, char *argv[]) { goto finish; } + signal(SIGWINCH, columns_lines_cache_reset); + while (!quit) { Hashmap *c; usec_t t; @@ -760,5 +785,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; }