summaryrefslogtreecommitdiff
path: root/proc/slab.c
blob: b0bccaad0a7fc971cf868f543478b799a10db0a4 (plain)
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
352
353
/* 
 * slab.c - slab related functions for libproc
 *
 * Chris Rivera <cmrivera@ufl.edu>
 * Robert Love <rml@tech9.net>
 *
 * Copyright (C) 2003 Chris Rivera
 * Copyright 2004, Albert Cahalan
 *
 * 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
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <ctype.h>

#include "slab.h"
#include "procps.h"
#include "alloc.h"

#define SLABINFO_LINE_LEN	2048
#define SLABINFO_VER_LEN	100
#define SLABINFO_FILE		"/proc/slabinfo"

static struct slab_info *free_index;

/*
 * get_slabnode - allocate slab_info structures using a free list
 *
 * In the fast path, we simply return a node off the free list.  In the slow
 * list, we malloc() a new node.  The free list is never automatically reaped,
 * both for simplicity and because the number of slab caches is fairly
 * constant.
 */
static struct slab_info *get_slabnode(void)
{
	struct slab_info *node;

	if (free_index) {
		node = free_index;
		free_index = free_index->next;
	} else {
		node = xmalloc(sizeof(struct slab_info));
	}

	return node;
}

/*
 * slab_badname_detect - return true if current slab was declared with
 *                       whitespaces for instance 
 *			 FIXME :Other cases ?
 */

static int slab_badname_detect(const char *restrict buffer)
{
	int numberarea=0;
	while (*buffer){
		if((*buffer)==' ')
			numberarea=1;
		if(isalpha(*buffer)&&numberarea)	
			return 1;
		buffer++;	
	}
	return 0;
}

/*
 * put_slabinfo - return all allocated nodes to the free list
 */
void put_slabinfo(struct slab_info *head)
{
	free_index = head;
}

/*
 * free_slabinfo - deallocate the memory associated with each node in the
 * slab_info linked list
 */
void free_slabinfo(struct slab_info *list)
{
	while (list) {
		struct slab_info *temp = list->next;
		free(list);
		list = temp;
	}
}

// parse_slabinfo20 - actual parse routine for slabinfo 2.x (2.6 kernels)
// Note: difference between 2.0 and 2.1 is in the ": globalstat" part where version 2.1 
// has extra column <nodeallocs>. We don't use ": globalstat" part in both versions.
//
// Formats (we don't use "statistics" extensions)
//
//  slabinfo - version: 2.1
//  # name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> \
//  : tunables <batchcount> <limit> <sharedfactor> \
//  : slabdata <active_slabs> <num_slabs> <sharedavail>
//
//  slabinfo - version: 2.1 (statistics)
//  # name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> \
//  : tunables <batchcount> <limit> <sharedfactor> \
//  : slabdata <active_slabs> <num_slabs> <sharedavail> \
//  : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <freelimit> <nodeallocs> \
//  : cpustat <allochit> <allocmiss> <freehit> <freemiss>
//             
//  slabinfo - version: 2.0
//  # name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> \
//  : tunables <batchcount> <limit> <sharedfactor> \
//  : slabdata <active_slabs> <num_slabs> <sharedavail>
//
//  slabinfo - version: 2.0 (statistics)
//  # name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> \
//  : tunables <batchcount> <limit> <sharedfactor> \
//  : slabdata <active_slabs> <num_slabs> <sharedavail> \
//  : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <freelimit> \
//  : cpustat <allochit> <allocmiss> <freehit> <freemiss>
static int parse_slabinfo20(struct slab_info **list, struct slab_stat *stats,
				FILE *f)
{
	struct slab_info *curr = NULL, *prev = NULL;
	char buffer[SLABINFO_LINE_LEN];
	int entries = 0;
	int page_size = getpagesize();

	stats->min_obj_size = INT_MAX;
	stats->max_obj_size = 0;

	while (fgets(buffer, SLABINFO_LINE_LEN, f)) {
		int assigned;

		if (buffer[0] == '#')
			continue;
	
		curr = get_slabnode();
		if (!curr)
			break;

		if (entries++ == 0)
			*list = curr;
		else
			if (prev)
				prev->next = curr;

		assigned = sscanf(buffer, "%" STRINGIFY(SLAB_INFO_NAME_LEN)
				"s %d %d %d %d %d : tunables %*d %*d %*d : \
				slabdata %d %d %*d", curr->name, 
				&curr->nr_active_objs, &curr->nr_objs, 
				&curr->obj_size, &curr->objs_per_slab,
				&curr->pages_per_slab, &curr->nr_active_slabs,
				&curr->nr_slabs);

		if (assigned < 8) {
			fprintf(stderr, "unrecognizable data in slabinfo!\n");
			curr = NULL;
			break;
		}

		if (curr->obj_size < stats->min_obj_size)
			stats->min_obj_size = curr->obj_size;
		if (curr->obj_size > stats->max_obj_size)
			stats->max_obj_size = curr->obj_size;

		curr->cache_size = (unsigned long)curr->nr_slabs * curr->pages_per_slab * page_size;

		if (curr->nr_objs) {
			curr->use = 100 * curr->nr_active_objs / curr->nr_objs;
			stats->nr_active_caches++;
		} else
			curr->use = 0;

		stats->nr_objs += curr->nr_objs;
		stats->nr_active_objs += curr->nr_active_objs;
		stats->total_size += (unsigned long)curr->nr_objs * curr->obj_size;
		stats->active_size += (unsigned long)curr->nr_active_objs * curr->obj_size;
		stats->nr_pages += curr->nr_slabs * curr->pages_per_slab;
		stats->nr_slabs += curr->nr_slabs;
		stats->nr_active_slabs += curr->nr_active_slabs;

		prev = curr;
	}

	if (!curr) {
		fprintf(stderr, "\rerror reading slabinfo!\n");
		return 1;
	}

	curr->next = NULL;
	stats->nr_caches = entries;
	if (stats->nr_objs)
		stats->avg_obj_size = stats->total_size / stats->nr_objs;

	return 0;
}

/*
 * parse_slabinfo11 - actual parsing routine for slabinfo 1.1 (2.4 kernels)
 */
static int parse_slabinfo11(struct slab_info **list, struct slab_stat *stats,
				FILE *f)
{
	struct slab_info *curr = NULL, *prev = NULL;
	char buffer[SLABINFO_LINE_LEN];
	int entries = 0;
	int page_size = getpagesize();

	stats->min_obj_size = INT_MAX;
	stats->max_obj_size = 0;

	while (fgets(buffer, SLABINFO_LINE_LEN, f)) {
		int assigned;

		curr = get_slabnode();
		if (!curr)
			break;

		if (entries++ == 0)
			*list = curr;
		else
			if (prev)
				prev->next = curr;

		assigned = sscanf(buffer, "%" STRINGIFY(SLAB_INFO_NAME_LEN)
				"s %d %d %d %d %d %d",
				curr->name, &curr->nr_active_objs,
				&curr->nr_objs, &curr->obj_size,
				&curr->nr_active_slabs, &curr->nr_slabs,
				&curr->pages_per_slab);

		if (assigned < 6) {
			fprintf(stderr, "unrecognizable data in  your slabinfo version 1.1\n\r");
			if(slab_badname_detect(buffer))
				fprintf(stderr, "Found an error in cache name at line %s\n", buffer); 
			curr = NULL;
			break;
		}

		if (curr->obj_size < stats->min_obj_size)
			stats->min_obj_size = curr->obj_size;
		if (curr->obj_size > stats->max_obj_size)
			stats->max_obj_size = curr->obj_size;

		curr->cache_size = (unsigned long)curr->nr_slabs * curr->pages_per_slab * page_size;

		if (curr->nr_objs) {
			curr->use = 100 * curr->nr_active_objs / curr->nr_objs;
			stats->nr_active_caches++;
		} else
			curr->use = 0;

		if (curr->obj_size)
			curr->objs_per_slab = curr->pages_per_slab *
					page_size / curr->obj_size;		

		stats->nr_objs += curr->nr_objs;
		stats->nr_active_objs += curr->nr_active_objs;
		stats->total_size += (unsigned long)curr->nr_objs * curr->obj_size;
		stats->active_size += (unsigned long)curr->nr_active_objs * curr->obj_size;
		stats->nr_pages += curr->nr_slabs * curr->pages_per_slab;
		stats->nr_slabs += curr->nr_slabs;
		stats->nr_active_slabs += curr->nr_active_slabs;

		prev = curr;
	}

	if (!curr) {
		fprintf(stderr, "\rerror reading slabinfo!\n");
		return 1;
	}

	curr->next = NULL;
	stats->nr_caches = entries;
	if (stats->nr_objs)
		stats->avg_obj_size = stats->total_size / stats->nr_objs;

	return 0;
}

/*
 * parse_slabinfo10 - actual parsing routine for slabinfo 1.0 (2.2 kernels)
 *
 * Not yet implemented.  Please feel free.
 */
static int parse_slabinfo10(struct slab_info **list, struct slab_stat *stats,
				FILE *f)
{
	(void) list, (void) stats, (void) f;
	fprintf(stderr, "slabinfo version 1.0 not yet supported\n");
	return 1;
}

/*
 * slabinfo - parse the system's slabinfo and fill out both a linked list of
 * slab_info structures and the slab_stat structure
 *
 * The function returns zero on success, in which case 'list' and 'stats' are
 * valid.  Nonzero is returned on failure and the state of 'list' and 'stats'
 * are undefined.
 */
int get_slabinfo(struct slab_info **list, struct slab_stat *stats)
{
	FILE *slabfile;
	char buffer[SLABINFO_VER_LEN];
	int major, minor, ret = 0;

	slabfile = fopen(SLABINFO_FILE, "r");
	if (!slabfile) {
		perror("fopen " SLABINFO_FILE);
		return 1;
	}

	if (!fgets(buffer, SLABINFO_VER_LEN, slabfile)) {
		fprintf(stderr, "cannot read from slabinfo\n");
		fclose(slabfile);
		return 1;
	}

	if (sscanf(buffer, "slabinfo - version: %d.%d", &major, &minor) != 2) {
		fprintf(stderr, "not the good old slabinfo we know\n");
		fclose(slabfile);
		return 1;
	}

	if (major == 2)
		ret = parse_slabinfo20(list, stats, slabfile);
	else if (major == 1 && minor == 1)
		ret = parse_slabinfo11(list, stats, slabfile);
	else if (major == 1 && minor == 0)
		ret = parse_slabinfo10(list, stats, slabfile);
	else {
		fprintf(stderr, "unrecognizable slabinfo version\n");
		fclose(slabfile);
		return 1;
	}

	fclose(slabfile);

	return ret;
}