cgtop.c revision 4afd3348c7506dd1d36305b7bcb9feb8952b9d6b
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <alloca.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sd-bus.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "cgroup-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "hashmap.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "terminal-util.h"
#include "unit-name.h"
#include "util.h"
typedef struct Group {
char *path;
bool n_tasks_valid:1;
bool cpu_valid:1;
bool memory_valid:1;
bool io_valid:1;
unsigned cpu_iteration;
double cpu_fraction;
unsigned io_iteration;
} Group;
static unsigned arg_depth = 3;
static unsigned arg_iterations = (unsigned) -1;
static bool arg_batch = false;
static bool arg_raw = false;
static char* arg_machine = NULL;
enum {
} arg_count = COUNT_PIDS;
static bool arg_recursive = true;
static enum {
static enum {
} arg_cpu_type = CPU_PERCENT;
static void group_free(Group *g) {
assert(g);
free(g);
}
static void group_hashmap_clear(Hashmap *h) {
Group *g;
while ((g = hashmap_steal_first(h)))
group_free(g);
}
static void group_hashmap_free(Hashmap *h) {
hashmap_free(h);
}
if (!is_valid)
return "-";
if (arg_raw) {
return buf;
}
return format_bytes(buf, l, t);
}
static int process(
const char *controller,
const char *path,
Hashmap *a,
Hashmap *b,
unsigned iteration,
Group *g;
int r;
assert(a);
g = hashmap_get(a, path);
if (!g) {
g = hashmap_get(b, path);
if (!g) {
if (!g)
return -ENOMEM;
if (!g->path) {
group_free(g);
return -ENOMEM;
}
r = hashmap_put(a, g->path, g);
if (r < 0) {
group_free(g);
return r;
}
} else {
r = hashmap_move_one(a, b, path);
if (r < 0)
return r;
}
}
if (streq(controller, SYSTEMD_CGROUP_CONTROLLER) && IN_SET(arg_count, COUNT_ALL_PROCESSES, COUNT_USERSPACE_PROCESSES)) {
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
g->n_tasks = 0;
while (cg_read_pid(f, &pid) > 0) {
continue;
g->n_tasks++;
}
if (g->n_tasks > 0)
g->n_tasks_valid = true;
if (r < 0)
return r;
r = read_one_line_file(p, &v);
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
r = safe_atou64(v, &g->n_tasks);
if (r < 0)
return r;
if (g->n_tasks > 0)
g->n_tasks_valid = true;
if (r < 0)
return r;
r = read_one_line_file(p, &v);
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
r = safe_atou64(v, &new_usage);
if (r < 0)
return r;
nsec_t x, y;
x = timestamp - g->cpu_timestamp;
if (x < 1)
x = 1;
g->cpu_fraction = (double) y / (double) x;
g->cpu_valid = true;
}
g->cpu_timestamp = timestamp;
g->cpu_iteration = iteration;
if (cg_unified() <= 0)
else
if (r < 0)
return r;
r = read_one_line_file(p, &v);
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
r = safe_atou64(v, &g->memory);
if (r < 0)
return r;
if (g->memory > 0)
g->memory_valid = true;
_cleanup_free_ char *p = NULL;
if (r < 0)
return r;
f = fopen(p, "re");
if (!f) {
return 0;
return -errno;
}
for (;;) {
uint64_t k, *q;
break;
l += strcspn(l, WHITESPACE);
l += strspn(l, WHITESPACE);
if (first_word(l, "Read")) {
l += 4;
q = &rd;
} else if (first_word(l, "Write")) {
l += 5;
q = ≀
} else
continue;
l += strspn(l, WHITESPACE);
r = safe_atou64(l, &k);
if (r < 0)
continue;
*q += k;
}
if (x < 1)
x = 1;
else
yr = 0;
else
yw = 0;
g->io_valid = true;
}
}
g->io_timestamp = timestamp;
g->io_iteration = iteration;
}
if (ret)
*ret = g;
return 0;
}
static int refresh_one(
const char *controller,
const char *path,
Hashmap *a,
Hashmap *b,
unsigned iteration,
unsigned depth,
int r;
assert(a);
return 0;
if (r < 0)
return r;
if (r == -ENOENT)
return 0;
if (r < 0)
return r;
for (;;) {
r = cg_read_subgroup(d, &fn);
if (r < 0)
return r;
if (r == 0)
break;
if (!p)
return -ENOMEM;
if (r < 0)
return r;
if (arg_recursive &&
child &&
child->n_tasks_valid &&
/* Recursively sum up processes */
if (ours->n_tasks_valid)
else {
ours->n_tasks_valid = true;
}
}
}
if (ret)
return 1;
}
int r;
assert(a);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
static int group_compare(const void*a, const void *b) {
/* Let's make sure that the parent is always before
* the child. Except when ordering by tasks and
* recursive summing is off, since that is actually
* not accumulative for all children. */
return -1;
return 1;
}
switch (arg_order) {
case ORDER_PATH:
break;
case ORDER_CPU:
if (arg_cpu_type == CPU_PERCENT) {
if (x->cpu_fraction > y->cpu_fraction)
return -1;
else if (x->cpu_fraction < y->cpu_fraction)
return 1;
} else if (x->cpu_valid)
return -1;
else if (y->cpu_valid)
return 1;
} else {
return -1;
return 1;
}
break;
case ORDER_TASKS:
if (x->n_tasks_valid && y->n_tasks_valid) {
return -1;
return 1;
} else if (x->n_tasks_valid)
return -1;
else if (y->n_tasks_valid)
return 1;
break;
case ORDER_MEMORY:
if (x->memory_valid && y->memory_valid) {
return -1;
return 1;
} else if (x->memory_valid)
return -1;
else if (y->memory_valid)
return 1;
break;
case ORDER_IO:
return -1;
return 1;
} else if (x->io_valid)
return -1;
else if (y->io_valid)
return 1;
}
}
Iterator i;
Group *g;
signed path_columns;
assert(a);
if (on_tty())
HASHMAP_FOREACH(g, a, i)
array[n++] = g;
/* Find the longest names in one run */
for (j = 0; j < n; j++) {
}
if (arg_cpu_type == CPU_PERCENT)
else
if (rows <= 10)
rows = 10;
if (on_tty()) {
if (path_columns < 10)
path_columns = 10;
off = ansi_underline();
printf("%s%s%-*s%s %s%7s%s %s%s%s %s%8s%s %s%8s%s %s%8s%s%s\n",
arg_order == ORDER_TASKS ? on : "", arg_count == COUNT_PIDS ? "Tasks" : arg_count == COUNT_USERSPACE_PROCESSES ? "Procs" : "Proc+",
ansi_normal());
} else
for (j = 0; j < n; j++) {
const char *path;
break;
g = array[j];
if (g->n_tasks_valid)
else
if (arg_cpu_type == CPU_PERCENT) {
if (g->cpu_valid)
else
} else
printf(" %*s", maxtcpu, format_timespan(buffer, sizeof(buffer), (usec_t) (g->cpu_usage / NSEC_PER_USEC), 0));
putchar('\n');
}
}
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 Show package version\n"
" -p --order=path Order by path\n"
" -c --order=cpu Order by CPU load (default)\n"
" -m --order=memory Order by memory load\n"
" -i --order=io Order by IO load\n"
" -r --raw Provide raw (not human-readable) numbers\n"
" --cpu=percentage Show CPU usage as percentage (default)\n"
" --cpu=time Show CPU usage as time\n"
" -P Count userspace processes instead of tasks (excl. kernel)\n"
" -k Count all processes instead of tasks (incl. kernel)\n"
" --recursive=BOOL Sum up process count recursively\n"
" -d --delay=DELAY Delay between updates\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: %u)\n"
" -M --machine= Show container\n"
}
enum {
ARG_VERSION = 0x100,
};
{}
};
bool recursive_unset = false;
int c, r;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return version();
case ARG_CPU_TYPE:
if (optarg) {
else {
return -EINVAL;
}
} else
break;
case ARG_DEPTH:
if (r < 0) {
log_error("Failed to parse depth parameter.");
return -EINVAL;
}
break;
case 'd':
if (r < 0 || arg_delay <= 0) {
log_error("Failed to parse delay parameter.");
return -EINVAL;
}
break;
case 'n':
if (r < 0) {
log_error("Failed to parse iterations parameter.");
return -EINVAL;
}
break;
case 'b':
arg_batch = true;
break;
case 'r':
arg_raw = true;
break;
case 'p':
break;
case 't':
break;
case 'c':
break;
case 'm':
break;
case 'i':
break;
case ARG_ORDER:
else {
return -EINVAL;
}
break;
case 'k':
break;
case 'P':
break;
case ARG_RECURSIVE:
r = parse_boolean(optarg);
if (r < 0) {
return r;
}
arg_recursive = r;
recursive_unset = r == 0;
break;
case 'M':
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("Too many arguments.");
return -EINVAL;
}
log_error("Non-recursive counting is only supported when counting processes, not tasks. Use -P or -k.");
return -EINVAL;
}
return 1;
}
static const char* counting_what(void) {
if (arg_count == COUNT_PIDS)
return "tasks";
else if (arg_count == COUNT_ALL_PROCESSES)
return "all processes (incl. kernel)";
else
return "userspace processes (excl. kernel)";
}
static int get_cgroup_root(char **ret) {
const char *m;
int r;
if (!arg_machine) {
r = cg_get_root_path(ret);
if (r < 0)
return log_error_errno(r, "Failed to get root control group path: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to load machine data: %m");
if (!path)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m");
bus,
"org.freedesktop.systemd1",
path,
"ControlGroup",
&error,
ret);
if (r < 0)
return log_error_errno(r, "Failed to query unit control group path: %s", bus_error_message(&error, r));
return 0;
}
int r;
unsigned iteration = 0;
usec_t last_refresh = 0;
bool quit = false, immediate_refresh = false;
log_open();
r = cg_mask_supported(&mask);
if (r < 0) {
log_error_errno(r, "Failed to determine supported controllers: %m");
goto finish;
}
if (r <= 0)
goto finish;
r = get_cgroup_root(&root);
if (r < 0) {
log_error_errno(r, "Failed to get root control group path: %m");
goto finish;
}
a = hashmap_new(&string_hash_ops);
b = hashmap_new(&string_hash_ops);
if (!a || !b) {
r = log_oom();
goto finish;
}
if (arg_iterations == (unsigned) -1)
while (!quit) {
Hashmap *c;
usec_t t;
char key;
char h[FORMAT_TIMESPAN_MAX];
t = now(CLOCK_MONOTONIC);
if (r < 0) {
log_error_errno(r, "Failed to refresh: %m");
goto finish;
}
c = a;
a = b;
b = c;
last_refresh = t;
immediate_refresh = false;
}
display(b);
break;
if (!on_tty()) /* non-TTY: Empty newline as delimiter between polls */
if (arg_batch)
else {
if (r == -ETIMEDOUT)
continue;
if (r < 0) {
log_error_errno(r, "Couldn't read key: %m");
goto finish;
}
}
if (on_tty()) { /* TTY: Clear any user keystroke */
}
if (arg_batch)
continue;
switch (key) {
case ' ':
immediate_refresh = true;
break;
case 'q':
quit = true;
break;
case 'p':
break;
case 't':
break;
case 'c':
break;
case 'm':
break;
case 'i':
break;
case '%':
break;
case 'k':
sleep(1);
break;
case 'P':
sleep(1);
break;
case 'r':
if (arg_count == COUNT_PIDS)
else {
}
sleep(1);
break;
case '+':
if (arg_delay < USEC_PER_SEC)
else
sleep(1);
break;
case '-':
else
sleep(1);
break;
case '?':
case 'h':
#define ON ANSI_HIGHLIGHT
#define OFF ANSI_NORMAL
"\t<" ON "p" OFF "> By path; <" ON "t" OFF "> By tasks/procs; <" ON "c" OFF "> By CPU; <" ON "m" OFF "> By memory; <" ON "i" OFF "> By I/O\n"
"\t<" ON "+" OFF "> Inc. delay; <" ON "-" OFF "> Dec. delay; <" ON "%%" OFF "> Toggle time; <" ON "SPACE" OFF "> Refresh\n"
"\t<" ON "P" OFF "> Toggle count userspace processes; <" ON "k" OFF "> Toggle count all processes\n"
sleep(3);
break;
default:
if (key < ' ')
else
sleep(1);
break;
}
}
r = 0;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}