cgtop.c revision 43a99a7afe3063eebc901452026b13360b69a7b5
/*-*- 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/>.
***/
#define __STDC_FORMAT_MACROS
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <alloca.h>
#include <getopt.h>
#include "path-util.h"
#include "util.h"
#include "hashmap.h"
#include "cgroup-util.h"
#include "build.h"
#include "fileio.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 unsigned arg_iterations = 0;
static bool arg_batch = false;
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);
}
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 (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;
}
}
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;
}
}
#define ON ANSI_HIGHLIGHT_ON
#define OFF ANSI_HIGHLIGHT_OFF
Iterator i;
Group *g;
signed path_columns;
assert(a);
/* Set cursor to top left corner and clear screen */
if (on_tty())
fputs("\033[H"
"\033[2J", stdout);
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;
printf("%s%-*s%s %s%7s%s %s%s%s %s%8s%s %s%8s%s %s%8s%s\n\n",
} else
for (j = 0; j < n; j++) {
char *p;
break;
g = array[j];
free(p);
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), (nsec_t) (g->cpu_usage / NSEC_PER_USEC), 0));
if (g->memory_valid)
else
if (g->io_valid) {
printf(" %8s",
printf(" %8s",
} else
putchar('\n');
}
return 0;
}
static int 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"
" -m Order by memory load\n"
" -i Order by IO load\n"
" --cpu[=TYPE] Show CPU usage as time or percentage (default)\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",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
int r;
switch (c) {
case 'h':
return help();
case ARG_VERSION:
return 0;
case ARG_CPU_TYPE:
if (optarg) {
else
return -EINVAL;
}
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 'p':
break;
case 't':
break;
case 'c':
break;
case 'm':
break;
case 'i':
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
}
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) {
r = log_oom();
goto finish;
}
if (!on_tty())
arg_iterations = 1;
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;
break;
if (arg_batch) {
} else {
if (r == -ETIMEDOUT)
continue;
if (r < 0) {
goto finish;
}
}
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 '+':
if (arg_delay < USEC_PER_SEC)
else
sleep(1);
break;
case '-':
else
sleep(1);
break;
case '?':
case 'h':
"\t<" ON "p" OFF "> By path; <" ON "t" OFF "> By tasks; <" ON "c" OFF "> By CPU; <" ON "m" OFF "> By memory; <" ON "i" OFF "> By I/O\n"
"\t<" ON "+" OFF "> Increase delay; <" ON "-" OFF "> Decrease delay; <" ON "%%" OFF "> Toggle time\n"
sleep(3);
break;
default:
sleep(1);
break;
}
}
r = 0;
if (r < 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}