cgtop.c revision b7def684941808600c344f0be7a2b9fcdda97e0f
/*-*- 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 <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <alloca.h>
#include <getopt.h>
#include "path-util.h"
#include "util.h"
#include "hashmap.h"
#include "cgroup-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 n_tasks;
unsigned cpu_iteration;
struct timespec cpu_timestamp;
double cpu_fraction;
unsigned io_iteration;
struct timespec io_timestamp;
} Group;
static unsigned arg_depth = 3;
static enum {
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);
}
static int process(const char *controller, const char *path, Hashmap *a, Hashmap *b, unsigned iteration) {
Group *g;
int r;
FILE *f;
unsigned n;
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 {
}
}
/* Regardless which controller, let's find the maximum number
* of processes in any of it */
if (r < 0)
return r;
n = 0;
while (cg_read_pid(f, &pid) > 0)
n++;
fclose(f);
if (n > 0) {
if (g->n_tasks_valid)
else
g->n_tasks = n;
g->n_tasks_valid = true;
}
char *p, *v;
if (r < 0)
return r;
r = read_one_line_file(p, &v);
free(p);
if (r < 0)
return r;
r = safe_atou64(v, &new_usage);
free(v);
if (r < 0)
return r;
uint64_t x, y;
if (y > 0) {
g->cpu_fraction = (double) y / (double) x;
g->cpu_valid = true;
}
}
g->cpu_timestamp = ts;
g->cpu_iteration = iteration;
char *p, *v;
if (r < 0)
return r;
r = read_one_line_file(p, &v);
free(p);
if (r < 0)
return r;
r = safe_atou64(v, &g->memory);
free(v);
if (r < 0)
return r;
if (g->memory > 0)
g->memory_valid = true;
char *p;
if (r < 0)
return r;
f = fopen(p, "re");
free(p);
if (!f)
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;
}
fclose(f);
g->io_valid = true;
}
}
g->io_timestamp = ts;
g->io_iteration = iteration;
}
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 < 0) {
if (r == ENOENT)
return 0;
return r;
}
for (;;) {
char *fn, *p;
r = cg_read_subgroup(d, &fn);
if (r <= 0)
goto finish;
if (!p) {
r = -ENOMEM;
goto finish;
}
free(p);
if (r < 0)
goto finish;
}
if (d)
closedir(d);
return r;
}
int r;
assert(a);
if (r < 0)
if (r != -ENOENT)
return r;
if (r < 0)
if (r != -ENOENT)
return r;
if (r < 0)
if (r != -ENOENT)
return r;
if (r < 0)
if (r != -ENOENT)
return r;
return 0;
}
static int group_compare(const void*a, const void *b) {
return -1;
return 1;
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;
}
if (arg_order == 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;
}
if (arg_order == 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;
}
return -1;
return 1;
} else if (x->io_valid)
return -1;
else if (y->io_valid)
return 1;
}
}
Iterator i;
Group *g;
unsigned rows, n = 0, j;
assert(a);
/* Set cursor to top left corner and clear screen */
fputs("\033[H"
"\033[2J", stdout);
HASHMAP_FOREACH(g, a, i)
array[n++] = g;
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 : "");
for (j = 0; j < n; j++) {
char *p;
char m[FORMAT_BYTES_MAX];
if (j + 5 > rows)
break;
g = array[j];
free(p);
if (g->n_tasks_valid)
else
if (g->cpu_valid)
else
if (g->memory_valid)
else
if (g->io_valid) {
printf(" %8s",
format_bytes(m, sizeof(m), g->io_input_bps));
printf(" %8s",
format_bytes(m, sizeof(m), g->io_output_bps));
} else
putchar('\n');
}
return 0;
}
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"
" -p Order by path\n"
" -t Order by number of tasks\n"
" -c Order by CPU load\n"
" -m Order by memory load\n"
" -i Order by IO load\n"
" -d --delay=DELAY Specify delay\n"
" --depth=DEPTH Maximum traversal depth (default: 2)\n",
}
enum {
ARG_DEPTH = 0x100
};
};
int c;
int r;
switch (c) {
case 'h':
help();
return 0;
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 'p':
break;
case 't':
break;
case 'c':
break;
case 'm':
break;
case 'i':
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
log_error("Too many arguments.");
return -EINVAL;
}
return 1;
}
int r;
unsigned iteration = 0;
usec_t last_refresh = 0;
bool quit = false, immediate_refresh = false;
log_open();
if (r <= 0)
goto finish;
if (!a || !b) {
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
while (!quit) {
Hashmap *c;
usec_t t;
char key;
char h[FORMAT_TIMESPAN_MAX];
t = now(CLOCK_MONOTONIC);
if (r < 0)
goto finish;
c = a;
a = b;
b = c;
last_refresh = t;
immediate_refresh = false;
}
r = display(b);
if (r < 0)
goto finish;
if (r == -ETIMEDOUT)
continue;
if (r < 0) {
goto finish;
}
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 '+':
if (arg_delay < USEC_PER_SEC)
else
sleep(1);
break;
case '-':
else
sleep(1);
break;
case '?':
case 'h':
"\t<" ANSI_HIGHLIGHT_ON "P" ANSI_HIGHLIGHT_OFF "> By path; <" ANSI_HIGHLIGHT_ON "T" ANSI_HIGHLIGHT_OFF "> By tasks; <" ANSI_HIGHLIGHT_ON "C" ANSI_HIGHLIGHT_OFF "> By CPU; <" ANSI_HIGHLIGHT_ON "M" ANSI_HIGHLIGHT_OFF "> By memory; <" ANSI_HIGHLIGHT_ON "I" ANSI_HIGHLIGHT_OFF "> By I/O\n"
"\t<" ANSI_HIGHLIGHT_ON "Q" ANSI_HIGHLIGHT_OFF "> Quit; <" ANSI_HIGHLIGHT_ON "+" ANSI_HIGHLIGHT_OFF "> Increase delay; <" ANSI_HIGHLIGHT_ON "-" ANSI_HIGHLIGHT_OFF "> Decrease delay; <" ANSI_HIGHLIGHT_ON "SPACE" ANSI_HIGHLIGHT_OFF "> Refresh");
sleep(3);
break;
default:
sleep(1);
break;
}
}
log_info("Exiting.");
r = 0;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}