slabtop.c 11 KB
Newer Older
1
/*
2 3 4 5 6 7
 * slabtop.c - utility to display kernel slab information.
 *
 * Chris Rivera <cmrivera@ufl.edu>
 * Robert Love <rml@tech9.net>
 *
 * Copyright (C) 2003 Chris Rivera
8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it 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.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 23
 */

24
#include <limits.h>
25
#include <locale.h>
26 27 28 29 30 31 32 33 34 35
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <ncurses.h>
#include <termios.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/ioctl.h>
36

37
#include <sys/select.h>
38 39 40
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
41

42
#include "c.h"
43
#include "fileutils.h"
44
#include "nls.h"
45
#include "strutils.h"
46 47 48 49 50
#include "proc/slab.h"
#include "proc/version.h"

#define DEF_SORT_FUNC		sort_nr_objs

51
static int run_once;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static unsigned short cols, rows;
static struct termios saved_tty;
static long delay = 3;
static int (*sort_func)(const struct slab_info *, const struct slab_info *);

static struct slab_info *merge_objs(struct slab_info *a, struct slab_info *b)
{
	struct slab_info sorted_list;
	struct slab_info *curr = &sorted_list;

	while ((a != NULL) && (b != NULL)) {
		if (sort_func(a, b)) {
			curr->next = a;
			curr = a;
			a = a->next;
		} else {
			curr->next = b;
			curr = b;
			b = b->next;
		}
	}

	curr->next = (a == NULL) ? b : a;
	return sorted_list.next;
}

78
/*
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
 * slabsort - merge sort the slab_info linked list based on sort_func
 */
static struct slab_info *slabsort(struct slab_info *list)
{
	struct slab_info *a, *b;

	if ((list == NULL) || (list->next == NULL))
		return list;

	a = list;
	b = list->next;

	while ((b != NULL) && (b->next != NULL)) {
		list = list->next;
		b = b->next->next;
	}
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	b = list->next;
	list->next = NULL;

	return merge_objs(slabsort(a), slabsort(b));
}

/*
 * Sort Routines.  Each of these should be associated with a command-line
 * search option.  The functions should fit the prototype:
 *
 *	int sort_foo(const struct slab_info *a, const struct slab_info *b)
 *
 * They return one if the first parameter is larger than the second
 * Otherwise, they return zero.
 */

static int sort_name(const struct slab_info *a, const struct slab_info *b)
{
	return (strcmp(a->name, b->name) < 0) ? 1 : 0;
}

static int sort_nr_objs(const struct slab_info *a, const struct slab_info *b)
{
	return (a->nr_objs > b->nr_objs);
}

static int sort_nr_active_objs(const struct slab_info *a,
				const struct slab_info *b)
{
	return (a->nr_active_objs > b->nr_active_objs);
}

static int sort_obj_size(const struct slab_info *a, const struct slab_info *b)
{
	return (a->obj_size > b->obj_size);
}

static int sort_objs_per_slab(const struct slab_info *a,
				const struct slab_info *b)
{
	return (a->objs_per_slab > b->objs_per_slab);
}

static int sort_pages_per_slab(const struct slab_info *a,
		const struct slab_info *b)
{
	return (a->pages_per_slab > b->pages_per_slab);
}

static int sort_nr_slabs(const struct slab_info *a, const struct slab_info *b)
{
	return (a->nr_slabs > b->nr_slabs);
}

static int sort_nr_active_slabs(const struct slab_info *a,
			const struct slab_info *b)
{
	return (a->nr_active_slabs > b->nr_active_slabs);
}


static int sort_use(const struct slab_info *a, const struct slab_info *b)
{
	return (a->use > b->use);
}

static int sort_cache_size(const struct slab_info *a, const struct slab_info *b)
{
	return (a->cache_size > b->cache_size);
}

/*
 * term_size - set the globals 'cols' and 'rows' to the current terminal size
 */
170
static void term_size(int unusused __attribute__ ((__unused__)))
171 172 173
{
	struct winsize ws;

174
	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) && ws.ws_row > 10) {
175 176 177 178 179 180
		cols = ws.ws_col;
		rows = ws.ws_row;
	} else {
		cols = 80;
		rows = 24;
	}
181 182
	if (run_once)
		rows = USHRT_MAX;
183 184
}

185
static void sigint_handler(int unused __attribute__ ((__unused__)))
186 187 188 189
{
	delay = 0;
}

190
static void __attribute__((__noreturn__)) usage(FILE *out)
191
{
192
	fputs(USAGE_HEADER, out);
193
	fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
194
	fputs(USAGE_OPTIONS, out);
195 196 197
	fputs(_(" -d, --delay <secs>  delay updates\n"), out);
	fputs(_(" -o, --once          only display once, then exit\n"), out);
	fputs(_(" -s, --sort <char>   specify sort criteria by character (see below)\n"), out);
198 199 200 201
	fputs(USAGE_SEPARATOR, out);
	fputs(USAGE_HELP, out);
	fputs(USAGE_VERSION, out);

202 203 204 205 206 207 208 209 210 211 212
	fputs(_("\nThe following are valid sort criteria:\n"), out);
	fputs(_(" a: sort by number of active objects\n"), out);
	fputs(_(" b: sort by objects per slab\n"), out);
	fputs(_(" c: sort by cache size\n"), out);
	fputs(_(" l: sort by number of slabs\n"), out);
	fputs(_(" v: sort by number of active slabs\n"), out);
	fputs(_(" n: sort by name\n"), out);
	fputs(_(" o: sort by number of objects (the default)\n"), out);
	fputs(_(" p: sort by pages per slab\n"), out);
	fputs(_(" s: sort by object size\n"), out);
	fputs(_(" u: sort by cache utilization\n"), out);
213
	fprintf(out, USAGE_MAN_TAIL("slabtop(1)"));
214 215

	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
216 217 218 219 220 221 222 223 224 225
}

/*
 * set_sort_func - return the slab_sort_func that matches the given key.
 * On unrecognizable key, DEF_SORT_FUNC is returned.
 */
static void * set_sort_func(char key)
{
	switch (key) {
	case 'n':
226
		return (void *) sort_name;
227
	case 'o':
228
		return (void *) sort_nr_objs;
229
	case 'a':
230
		return (void *) sort_nr_active_objs;
231
	case 's':
232
		return (void *) sort_obj_size;
233
	case 'b':
234
		return (void *) sort_objs_per_slab;
235
	case 'p':
236
		return (void *) sort_pages_per_slab;
237
	case 'l':
238
		return (void *) sort_nr_slabs;
239
	case 'v':
240
		return (void *) sort_nr_active_slabs;
241
	case 'c':
242
		return (void *) sort_cache_size;
243
	case 'u':
244
		return (void *) sort_use;
245
	default:
246
		return (void *) DEF_SORT_FUNC;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	}
}

static void parse_input(char c)
{
	c = toupper(c);
	switch(c) {
	case 'A':
		sort_func = sort_nr_active_objs;
		break;
	case 'B':
		sort_func = sort_objs_per_slab;
		break;
	case 'C':
		sort_func = sort_cache_size;
		break;
	case 'L':
		sort_func = sort_nr_slabs;
		break;
	case 'V':
		sort_func = sort_nr_active_slabs;
		break;
	case 'N':
		sort_func = sort_name;
		break;
	case 'O':
		sort_func = sort_nr_objs;
		break;
	case 'P':
		sort_func = sort_pages_per_slab;
		break;
	case 'S':
		sort_func = sort_obj_size;
		break;
	case 'U':
		sort_func = sort_use;
		break;
	case 'Q':
		delay = 0;
		break;
	}
}

290
#define print_line(fmt, ...) if (run_once) printf(fmt, __VA_ARGS__); else printw(fmt, __VA_ARGS__)
291 292
int main(int argc, char *argv[])
{
293
	int is_tty, o;
294 295
	unsigned short old_rows;
	struct slab_info *slab_list = NULL;
296
	int retval = EXIT_SUCCESS;
297

298 299 300 301 302 303 304
	static const struct option longopts[] = {
		{ "delay",	required_argument, NULL, 'd' },
		{ "sort",	required_argument, NULL, 's' },
		{ "once",	no_argument,	   NULL, 'o' },
		{ "help",	no_argument,	   NULL, 'h' },
		{ "version",	no_argument,	   NULL, 'V' },
		{  NULL, 0, NULL, 0 }
305 306
	};

307
#ifdef HAVE_PROGRAM_INVOCATION_NAME
308
	program_invocation_name = program_invocation_short_name;
309
#endif
310 311 312
	setlocale (LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
313
	atexit(close_stdout);
314

315 316 317 318 319 320
	sort_func = DEF_SORT_FUNC;

	while ((o = getopt_long(argc, argv, "d:s:ohV", longopts, NULL)) != -1) {
		switch (o) {
		case 'd':
			errno = 0;
321 322
			delay = strtol_or_err(optarg, _("illegal delay"));
			if (delay < 1)
323
				xerrx(EXIT_FAILURE,
324
					_("delay must be positive integer"));
325 326
			break;
		case 's':
327 328
			sort_func = (int (*)(const struct slab_info*,
				const struct slab_info *)) set_sort_func(optarg[0]);
329 330
			break;
		case 'o':
331
			run_once=1;
332 333 334
			delay = 0;
			break;
		case 'V':
335
			printf(PROCPS_NG_VERSION);
336
			return EXIT_SUCCESS;
337
		case 'h':
338
			usage(stdout);
339
		default:
340
			usage(stderr);
341 342 343
		}
	}

344 345
	is_tty = isatty(STDIN_FILENO);
	if (is_tty && tcgetattr(STDIN_FILENO, &saved_tty) == -1)
346
		xwarn(_("terminal setting retrieval"));
347 348

	old_rows = rows;
349
	term_size(0);
350 351 352 353 354
	if (!run_once) {
		initscr();
		resizeterm(rows, cols);
		signal(SIGWINCH, term_size);
	}
355 356 357 358
	signal(SIGINT, sigint_handler);

	do {
		struct slab_info *curr;
359
		struct slab_stat stats;
360 361 362 363
		struct timeval tv;
		fd_set readfds;
		char c;
		int i;
364
		memset(&stats, 0, sizeof(struct slab_stat));
365

366
		if (get_slabinfo(&slab_list, &stats)) {
367
			slab_list = NULL;
368
			retval = EXIT_FAILURE;
369
			break;
370
		}
371

372
		if (!run_once && old_rows != rows) {
373 374 375 376
			resizeterm(rows, cols);
			old_rows = rows;
		}

377
		move(0, 0);
Craig Small's avatar
Craig Small committed
378
		print_line(" %-35s: %d / %d (%.1f%%)\n"
379 380 381 382
		       " %-35s: %d / %d (%.1f%%)\n"
		       " %-35s: %d / %d (%.1f%%)\n"
		       " %-35s: %.2fK / %.2fK (%.1f%%)\n"
		       " %-35s: %.2fK / %.2fK / %.2fK\n\n",
383 384
		       /* Translation Hint: Next five strings must not
			* exceed 35 length in characters.  */
Yuri Chornoivan's avatar
Yuri Chornoivan committed
385
		       /* xgettext:no-c-format */
386 387 388
		       _("Active / Total Objects (% used)"),
		       stats.nr_active_objs, stats.nr_objs,
		       100.0 * stats.nr_active_objs / stats.nr_objs,
Yuri Chornoivan's avatar
Yuri Chornoivan committed
389
	               /* xgettext:no-c-format */
390 391 392
		       _("Active / Total Slabs (% used)"),
		       stats.nr_active_slabs, stats.nr_slabs,
		       100.0 * stats.nr_active_slabs / stats.nr_slabs,
Yuri Chornoivan's avatar
Yuri Chornoivan committed
393
	               /* xgettext:no-c-format */
394 395 396
		       _("Active / Total Caches (% used)"),
		       stats.nr_active_caches, stats.nr_caches,
		       100.0 * stats.nr_active_caches / stats.nr_caches,
Yuri Chornoivan's avatar
Yuri Chornoivan committed
397
	               /* xgettext:no-c-format */
398 399 400 401 402 403
		       _("Active / Total Size (% used)"),
		       stats.active_size / 1024.0, stats.total_size / 1024.0,
		       100.0 * stats.active_size / stats.total_size,
		       _("Minimum / Average / Maximum Object"),
		       stats.min_obj_size / 1024.0, stats.avg_obj_size / 1024.0,
		       stats.max_obj_size / 1024.0);
404 405 406 407

		slab_list = slabsort(slab_list);

		attron(A_REVERSE);
408 409
		/* Translation Hint: Please keep alignment of the
		 * following intact. */
Craig Small's avatar
Craig Small committed
410
		print_line("%-78s\n", _("  OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME"));
411 412 413
		attroff(A_REVERSE);

		curr = slab_list;
414
		for (i = 0; i < rows - 8 && curr; i++) {
415
			print_line("%6u %6u %3u%% %7.2fK %6u %8u %9uK %-23s\n",
416 417
				curr->nr_objs, curr->nr_active_objs, curr->use,
				curr->obj_size / 1024.0, curr->nr_slabs,
albert's avatar
albert committed
418
				curr->objs_per_slab, (unsigned)(curr->cache_size / 1024),
419 420 421 422 423
				curr->name);
			curr = curr->next;
		}

		put_slabinfo(slab_list);
424
		if (!run_once) {
425 426 427 428 429 430
			refresh();
			FD_ZERO(&readfds);
			FD_SET(STDIN_FILENO, &readfds);
			tv.tv_sec = delay;
			tv.tv_usec = 0;
			if (select(STDOUT_FILENO, &readfds, NULL, NULL, &tv) > 0) {
431
				if (read(STDIN_FILENO, &c, 1) != 1)
432 433 434 435
					break;
				parse_input(c);
			}
		}
436 437
	} while (delay);

438 439
	if (is_tty)
		tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tty);
440 441
	if (slab_list)
		free_slabinfo(slab_list);
442 443 444
	if (!run_once)
		endwin();
	return retval;
445
}