[systemd-commits] 2 commits - src/cgtop src/shared
Zbigniew JÄdrzejewski-Szmek
zbyszek at kemper.freedesktop.org
Thu Jul 26 14:36:05 PDT 2012
src/cgtop/cgtop.c | 56 +++++++++++++++++++++++++++++++++++++-----------------
src/shared/util.c | 31 +++++++++++++++++++++++------
src/shared/util.h | 1
3 files changed, 64 insertions(+), 24 deletions(-)
New commits:
commit 0d7e32fa0a8e5f21a66c2f5504adabfa40523efc
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Jul 26 23:09:02 2012 +0200
cgtop: add --version option
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index d8d36b3..a57a468 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;
@@ -511,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"
@@ -523,19 +525,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;
@@ -552,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) {
commit 11f96fac8f30423cb14f84622de9ed56b3b8f093
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Jul 26 20:23:28 2012 +0200
cgtop: use full terminal width
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index 3009589..d8d36b3 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -426,7 +426,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);
@@ -446,13 +446,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_uncached() - 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 +473,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)
diff --git a/src/shared/util.c b/src/shared/util.c
index 5f36008..af97595 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -3774,18 +3774,27 @@ int fd_columns(int fd) {
return ws.ws_col;
}
-unsigned columns(void) {
- static __thread int parsed_columns = 0;
+static unsigned columns_cached(bool cached) {
+ static __thread int parsed_columns = 0, env_columns = -1;
const char *e;
- if (_likely_(parsed_columns > 0))
+ if (_likely_(parsed_columns > 0 && cached))
return parsed_columns;
- e = getenv("COLUMNS");
- if (e)
- parsed_columns = atoi(e);
+ if (_unlikely_(env_columns == -1)) {
+ e = getenv("COLUMNS");
+ if (e)
+ env_columns = atoi(e);
+ else
+ env_columns = 0;
+ }
- if (parsed_columns <= 0)
+ if (env_columns > 0) {
+ parsed_columns = env_columns;
+ return parsed_columns;
+ }
+
+ if (parsed_columns <= 0 || !cached)
parsed_columns = fd_columns(STDOUT_FILENO);
if (parsed_columns <= 0)
@@ -3794,6 +3803,14 @@ unsigned columns(void) {
return parsed_columns;
}
+unsigned columns(void) {
+ return columns_cached(true);
+}
+
+unsigned columns_uncached(void) {
+ return columns_cached(false);
+}
+
int fd_lines(int fd) {
struct winsize ws;
zero(ws);
diff --git a/src/shared/util.h b/src/shared/util.h
index d25b7ee..b315593 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -374,6 +374,7 @@ void status_welcome(void);
int fd_columns(int fd);
unsigned columns(void);
+unsigned columns_uncached(void);
int fd_lines(int fd);
unsigned lines(void);
More information about the systemd-commits
mailing list