1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <limits.h>
#define STRINGIFY_ARG(a) #a
#define STRINGIFY(a) STRINGIFY_ARG(a)
#define DEF_SORT_FUNC sort_nr_objs
#define SLABINFO_LINE_LEN 512 /* size of longest line */
#define SLABINFO_NAME_LEN 32 /* cache name size (will truncate) */
#define SLABINFO_FILE "/proc/slabinfo"
#define DEF_NR_ROWS 15 /* default nr of caches to show */
/* object representing a slab cache (each line of slabinfo) */
struct slab_info {
char name[SLABINFO_NAME_LEN]; /* name of this cache */
struct slab_info *next;
unsigned long nr_pages; /* size of cache in pages */
unsigned long nr_objs; /* number of objects in this cache */
unsigned long nr_active_objs; /* number of active objects */
unsigned long obj_size; /* size of each object */
unsigned long objs_per_slab; /* number of objects per slab */
unsigned long nr_slabs; /* number of slabs in this cache */
unsigned long use; /* percent full: total / active */
};
/* object representing system-wide statistics */
struct slab_stat {
unsigned long total_size; /* size of all objects */
unsigned long active_size; /* size of all active objects */
unsigned long nr_objs; /* total number of objects */
unsigned long nr_active_objs; /* total number of active objects */
unsigned long nr_slabs; /* total number of slabs */
unsigned long nr_active_slabs; /* total number of active slabs*/
unsigned long nr_caches; /* number of caches */
unsigned long nr_active_caches; /* number of active caches */
unsigned long avg_obj_size; /* average object size */
unsigned long min_obj_size; /* size of smallest object */
unsigned long max_obj_size; /* size of largest object */
};
typedef int (*sort_t)(const struct slab_info *, const struct slab_info *);
static sort_t sort_func;
/*
* get_slabinfo - open, read, and parse a slabinfo 2.x file, which has the
* following format:
*
* slabinfo - version: 2.1
* <name> <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>
* : tunables <limit> <batchcount> <sharedfactor>
* : slabdata <active_slabs> <num_slabs> <sharedavail>
*
* Returns the head of the new list of slab_info structures, or NULL on error.
*/
static struct slab_info * get_slabinfo(struct slab_stat *stats)
{
struct slab_info *head = NULL, *p = NULL, *prev = NULL;
FILE *slabfile;
char line[SLABINFO_LINE_LEN];
unsigned int major, minor;
slabfile = fopen(SLABINFO_FILE, "r");
if (!slabfile) {
perror("fopen");
return NULL;
}
if (!fgets(line, SLABINFO_LINE_LEN, slabfile)) {
fprintf(stderr, "cannot read from " SLABINFO_FILE "\n");
return NULL;
}
if (sscanf(line, "slabinfo - version: %u.%u", &major, &minor) != 2) {
fprintf(stderr, "unable to parse slabinfo version!\n");
return NULL;
}
if (major != 2 || minor > 1) {
fprintf(stderr, "we only support slabinfo 2.0 and 2.1!\n");
return NULL;
}
stats->min_obj_size = INT_MAX;
while (fgets(line, SLABINFO_LINE_LEN, slabfile)) {
unsigned long nr_active_slabs, pages_per_slab;
int ret;
if (line[0] == '#')
continue;
p = malloc(sizeof (struct slab_info));
if (!p) {
perror("malloc");
head = NULL;
break;
}
if (stats->nr_caches++ == 0)
head = prev = p;
ret = sscanf(line, "%" STRINGIFY(SLABINFO_NAME_LEN) "s"
" %lu %lu %lu %lu %lu : tunables %*d %*d %*d : \
slabdata %lu %lu %*d", p->name,
&p->nr_active_objs, &p->nr_objs,
&p->obj_size, &p->objs_per_slab,
&pages_per_slab,
&nr_active_slabs,
&p->nr_slabs);
if (ret != 8) {
fprintf(stderr, "unrecognizable data in slabinfo!\n");
head = NULL;
break;
}
if (p->obj_size < stats->min_obj_size)
stats->min_obj_size = p->obj_size;
if (p->obj_size > stats->max_obj_size)
stats->max_obj_size = p->obj_size;
p->nr_pages = p->nr_slabs * pages_per_slab;
if (p->nr_objs) {
p->use = 100 * p->nr_active_objs / p->nr_objs;
stats->nr_active_caches++;
} else
p->use = 0;
stats->nr_objs += p->nr_objs;
stats->nr_active_objs += p->nr_active_objs;
stats->total_size += p->nr_objs * p->obj_size;
stats->active_size += p->nr_active_objs * p->obj_size;
stats->nr_slabs += p->nr_slabs;
stats->nr_active_slabs += nr_active_slabs;
prev->next = p;
prev = p;
}
if (fclose(slabfile))
perror("fclose");
if (p)
p->next = NULL;
if (stats->nr_objs)
stats->avg_obj_size = stats->total_size / stats->nr_objs;
return head;
}
/*
* free_slablist - deallocate the memory associated with each node in the
* provided slab_info linked list
*/
static void free_slablist(struct slab_info *list)
{
while (list) {
struct slab_info *temp = list->next;
free(list);
list = temp;
}
}
static struct slab_info *merge_objs(struct slab_info *a, struct slab_info *b)
{
struct slab_info list;
struct slab_info *p = &list;
while (a && b) {
if (sort_func(a, b)) {
p->next = a;
p = a;
a = a->next;
} else {
p->next = b;
p = b;
b = b->next;
}
}
p->next = (a == NULL) ? b : a;
return list.next;
}
/*
* 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 || !list->next)
return list;
a = list;
b = list->next;
while (b && b->next) {
list = list->next;
b = b->next->next;
}
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 zero if the first parameter is smaller than the second.
* Otherwise, they return nonzero.
*/
static int sort_name(const struct slab_info *a, const struct slab_info *b)
{
return (strcmp(a->name, b->name) < 0 ) ? 1: 0;
}
#define BUILD_SORT_FUNC(VAL) \
static int sort_ ## VAL \
(const struct slab_info *a, const struct slab_info *b) { \
return (a-> VAL > b-> VAL); }
BUILD_SORT_FUNC(nr_objs)
BUILD_SORT_FUNC(nr_active_objs)
BUILD_SORT_FUNC(obj_size)
BUILD_SORT_FUNC(objs_per_slab)
BUILD_SORT_FUNC(nr_slabs)
BUILD_SORT_FUNC(use)
BUILD_SORT_FUNC(nr_pages)
/*
* set_sort_func - return the slab_sort_func that matches the given key.
* On unrecognizable key, the call returns NULL.
*/
static sort_t set_sort_func(char key)
{
switch (tolower(key)) {
case 'a':
return sort_nr_active_objs;
case 'c':
return sort_nr_pages;
case 'l':
return sort_nr_slabs;
case 'n':
return sort_name;
case 'o':
return sort_nr_objs;
case 'p':
return sort_objs_per_slab;
case 's':
return sort_obj_size;
case 'u':
return sort_use;
default:
return NULL;
}
}
int main(int argc, char *argv[])
{
struct slab_info *list, *p;
struct slab_stat stats = { .nr_objs = 0 };
unsigned int page_size = getpagesize() / 1024, nr_rows = DEF_NR_ROWS, i;
sort_func = DEF_SORT_FUNC;
if (argc > 1) {
/* FIXME: Ugh. */
if (argc == 3 && !strcmp(argv[1], "-n")) {
errno = 0;
nr_rows = (unsigned int) strtoul(argv[2], NULL, 0);
if (errno) {
perror("strtoul");
exit(EXIT_FAILURE);
}
}
else if (argc == 3 && !strcmp(argv[1], "-s"))
sort_func = set_sort_func(argv[2][0]) ? : DEF_SORT_FUNC;
else {
fprintf(stderr, "usage: %s [options]\n\n", argv[0]);
fprintf(stderr, "options:\n");
fprintf(stderr, " -s S specify sort criteria S\n");
fprintf(stderr, " -h display this help\n\n");
fprintf(stderr, "Valid sort criteria:\n");
fprintf(stderr, " a: number of Active objects\n");
fprintf(stderr, " c: Cache size\n");
fprintf(stderr, " l: number of sLabs\n");
fprintf(stderr, " n: Name\n");
fprintf(stderr, " o: number of Objects\n");
fprintf(stderr, " p: objects Per slab\n");
fprintf(stderr, " s: object Size\n");
fprintf(stderr, " u: cache Utilization\n");
exit(EXIT_FAILURE);
}
}
list = get_slabinfo (&stats);
if (!list)
exit(EXIT_FAILURE);
printf(" Active / Total Objects (%% used) : %lu / %lu (%.1f%%)\n"
" Active / Total Slabs (%% used) : %lu / %lu (%.1f%%)\n"
" Active / Total Caches (%% used) : %lu / %lu (%.1f%%)\n"
" Active / Total Size (%% used) : %.2fK / %.2fK (%.1f%%)\n"
" Min / Avg / Max Object Size : %.2fK / %.2fK / %.2fK\n\n",
stats.nr_active_objs,
stats.nr_objs,
100.0 * stats.nr_active_objs / stats.nr_objs,
stats.nr_active_slabs,
stats.nr_slabs,
100.0 * stats.nr_active_slabs / stats.nr_slabs,
stats.nr_active_caches,
stats.nr_caches,
100.0 * stats.nr_active_caches / stats.nr_caches,
stats.active_size / 1024.0,
stats.total_size / 1024.0,
100.0 * stats.active_size / stats.total_size,
stats.min_obj_size / 1024.0,
stats.avg_obj_size / 1024.0,
stats.max_obj_size / 1024.0);
printf("%6s %6s %4s %8s %6s %8s %10s %-23s\n",
"OBJS", "ACTIVE", "USE", "OBJ SIZE", "SLABS",
"OBJ/SLAB", "CACHE SIZE", "NAME");
p = list = slabsort(list);
for (i = 0; i < nr_rows && p; i++) {
printf("%6lu %6lu %3lu%% %7.2fK %6lu %8lu %9luK %-23s\n",
p->nr_objs, p->nr_active_objs, p->use,
p->obj_size / 1024.0, p->nr_slabs,
p->objs_per_slab,
p->nr_pages * page_size,
p->name);
p = p->next;
}
free_slablist(list);
return 0;
}
|