aboutsummaryrefslogtreecommitdiffstats
path: root/trace-mem.c
blob: a949b1588b0d3d3989b181f8fdca6d80219591b6 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright (C) 2013 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License (not later!)
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not,  see <http://www.gnu.org/licenses>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * This code was inspired by Ezequiel Garcia's trace_analyze program:
 *   git://github.com/ezequielgarcia/trace_analyze.git
 *
 * Unfortuntately, I hate working with Python, and I also had trouble
 * getting it to work, as I had an old python on my Fedora 13, and it
 * was written for the newer version. I decided to do some of it here
 * in C.
 */
#define _LARGEFILE64_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <signal.h>

#include "trace-local.h"
#include "trace-hash-local.h"
#include "list.h"

static int kmalloc_type;
static int kmalloc_node_type;
static int kfree_type;
static int kmem_cache_alloc_type;
static int kmem_cache_alloc_node_type;
static int kmem_cache_free_type;

struct format_field *common_type_field;

struct format_field *kmalloc_callsite_field;
struct format_field *kmalloc_bytes_req_field;
struct format_field *kmalloc_bytes_alloc_field;
struct format_field *kmalloc_ptr_field;

struct format_field *kmalloc_node_callsite_field;
struct format_field *kmalloc_node_bytes_req_field;
struct format_field *kmalloc_node_bytes_alloc_field;
struct format_field *kmalloc_node_ptr_field;

struct format_field *kfree_ptr_field;

struct format_field *kmem_cache_callsite_field;
struct format_field *kmem_cache_bytes_req_field;
struct format_field *kmem_cache_bytes_alloc_field;
struct format_field *kmem_cache_ptr_field;

struct format_field *kmem_cache_node_callsite_field;
struct format_field *kmem_cache_node_bytes_req_field;
struct format_field *kmem_cache_node_bytes_alloc_field;
struct format_field *kmem_cache_node_ptr_field;

struct format_field *kmem_cache_free_ptr_field;

static void *zalloc(size_t size)
{
	return calloc(1, size);
}

static struct event_format *
update_event(struct pevent *pevent,
	     const char *sys, const char *name, int *id)
{
	struct event_format *event;

	event = pevent_find_event_by_name(pevent, sys, name);
	if (!event)
		return NULL;

	*id = event->id;

	return event;
}

static void update_kmalloc(struct pevent *pevent)
{
	struct event_format *event;

	event = update_event(pevent, "kmem", "kmalloc", &kmalloc_type);
	if (!event)
		return;

	kmalloc_callsite_field = pevent_find_field(event, "call_site");
	kmalloc_bytes_req_field = pevent_find_field(event, "bytes_req");
	kmalloc_bytes_alloc_field = pevent_find_field(event, "bytes_alloc");
	kmalloc_ptr_field = pevent_find_field(event, "ptr");
}

static void update_kmalloc_node(struct pevent *pevent)
{
	struct event_format *event;

	event = update_event(pevent, "kmem", "kmalloc_node", &kmalloc_node_type);
	if (!event)
		return;

	kmalloc_node_callsite_field = pevent_find_field(event, "call_site");
	kmalloc_node_bytes_req_field = pevent_find_field(event, "bytes_req");
	kmalloc_node_bytes_alloc_field = pevent_find_field(event, "bytes_alloc");
	kmalloc_node_ptr_field = pevent_find_field(event, "ptr");
}

static void update_kfree(struct pevent *pevent)
{
	struct event_format *event;

	event = update_event(pevent, "kmem", "kfree", &kfree_type);
	if (!event)
		return;

	kfree_ptr_field = pevent_find_field(event, "ptr");
}

static void update_kmem_cache_alloc(struct pevent *pevent)
{
	struct event_format *event;

	event = update_event(pevent, "kmem", "kmem_cache_alloc", &kmem_cache_alloc_type);
	if (!event)
		return;

	kmem_cache_callsite_field = pevent_find_field(event, "call_site");
	kmem_cache_bytes_req_field = pevent_find_field(event, "bytes_req");
	kmem_cache_bytes_alloc_field = pevent_find_field(event, "bytes_alloc");
	kmem_cache_ptr_field = pevent_find_field(event, "ptr");
}

static void update_kmem_cache_alloc_node(struct pevent *pevent)
{
	struct event_format *event;

	event = update_event(pevent, "kmem", "kmem_cache_alloc_node",
			     &kmem_cache_alloc_node_type);
	if (!event)
		return;

	kmem_cache_node_callsite_field = pevent_find_field(event, "call_site");
	kmem_cache_node_bytes_req_field = pevent_find_field(event, "bytes_req");
	kmem_cache_node_bytes_alloc_field = pevent_find_field(event, "bytes_alloc");
	kmem_cache_node_ptr_field = pevent_find_field(event, "ptr");
}

static void update_kmem_cache_free(struct pevent *pevent)
{
	struct event_format *event;

	event = update_event(pevent, "kmem", "kmem_cache_free", &kmem_cache_free_type);
	if (!event)
		return;

	kmem_cache_free_ptr_field = pevent_find_field(event, "ptr");
}

struct func_descr {
	struct func_descr	*next;
	const char		*func;
	unsigned long		total_alloc;
	unsigned long		total_req;
	unsigned long		current_alloc;
	unsigned long		current_req;
	unsigned long		max_alloc;
	unsigned long		max_req;
	unsigned long		waste;
	unsigned long		max_waste;
};

struct ptr_descr {
	struct ptr_descr	*next;
	struct func_descr	*func;
	unsigned long long	ptr;
	unsigned long		alloc;
	unsigned long		req;
};

#define HASH_BITS	12
#define HASH_SIZE	(1 << HASH_BITS)
#define HASH_MASK	(HASH_SIZE - 1);

static struct func_descr *func_hash[HASH_SIZE];
static struct ptr_descr *ptr_hash[HASH_SIZE];
static struct func_descr **func_list;

static unsigned func_count;

static int make_key(const void *ptr, int size)
{
	int key = 0;
	int i;
	char *kp = (char *)&key;
	const char *indx = ptr;

	for (i = 0; i < size; i++)
		kp[i & 3] ^= indx[i];

	return trace_hash(key);
}

static struct func_descr *find_func(const char *func)
{
	struct func_descr *funcd;
	int key = make_key(func, strlen(func)) & HASH_MASK;

	for (funcd = func_hash[key]; funcd; funcd = funcd->next) {
		/*
		 * As func is always a constant to one pointer,
		 * we can use a direct compare instead of strcmp.
		 */
		if (funcd->func == func)
			return funcd;
	}

	return NULL;
}

static struct func_descr *create_func(const char *func)
{
	struct func_descr *funcd;
	int key = make_key(func, strlen(func)) & HASH_MASK;

	funcd = zalloc(sizeof(*funcd));
	if (!funcd)
		die("malloc");

	funcd->func = func;
	funcd->next = func_hash[key];
	func_hash[key] = funcd;

	func_count++;

	return funcd;
}

static struct ptr_descr *find_ptr(unsigned long long ptr)
{
	struct ptr_descr *ptrd;
	int key = make_key(&ptr, sizeof(ptr)) & HASH_MASK;

	for (ptrd = ptr_hash[key]; ptrd; ptrd = ptrd->next) {
		if (ptrd->ptr == ptr)
			return ptrd;
	}

	return NULL;
}

static struct ptr_descr *create_ptr(unsigned long long ptr)
{
	struct ptr_descr *ptrd;
	int key = make_key(&ptr, sizeof(ptr)) & HASH_MASK;

	ptrd = zalloc(sizeof(*ptrd));
	if (!ptrd)
		die("malloc");

	ptrd->ptr = ptr;
	ptrd->next = ptr_hash[key];
	ptr_hash[key] = ptrd;

	return ptrd;
}

static void remove_ptr(unsigned long long ptr)
{
	struct ptr_descr *ptrd, **last;
	int key = make_key(&ptr, sizeof(ptr)) & HASH_MASK;

	last = &ptr_hash[key];
	for (ptrd = ptr_hash[key]; ptrd; ptrd = ptrd->next) {
		if (ptrd->ptr == ptr)
			break;
		last = &ptrd->next;
	}

	if (!ptrd)
		return;

	*last = ptrd->next;
	free(ptrd);
}

static void add_kmalloc(const char *func, unsigned long long ptr,
			unsigned int req, int alloc)
{
	struct func_descr *funcd;
	struct ptr_descr *ptrd;

	funcd = find_func(func);
	if (!funcd)
		funcd = create_func(func);

	funcd->total_alloc += alloc;
	funcd->total_req += req;
	funcd->current_alloc += alloc;
	funcd->current_req += req;
	if (funcd->current_alloc > funcd->max_alloc)
		funcd->max_alloc = funcd->current_alloc;
	if (funcd->current_req > funcd->max_req)
		funcd->max_req = funcd->current_req;

	ptrd = find_ptr(ptr);
	if (!ptrd)
		ptrd = create_ptr(ptr);

	ptrd->alloc = alloc;
	ptrd->req = req;
	ptrd->func = funcd;
}

static void remove_kmalloc(unsigned long long ptr)
{
	struct func_descr *funcd;
	struct ptr_descr *ptrd;

	ptrd = find_ptr(ptr);
	if (!ptrd)
		return;

	funcd = ptrd->func;
	funcd->current_alloc -= ptrd->alloc;
	funcd->current_req -= ptrd->req;

	remove_ptr(ptr);
}

static void
process_kmalloc(struct pevent *pevent, struct pevent_record *record,
		struct format_field *callsite_field,
		struct format_field *bytes_req_field,
		struct format_field *bytes_alloc_field,
		struct format_field *ptr_field)
{
	unsigned long long callsite;
	unsigned long long val;
	unsigned long long ptr;
	unsigned int req;
	int alloc;
	const char *func;

	pevent_read_number_field(callsite_field, record->data, &callsite);
	pevent_read_number_field(bytes_req_field, record->data, &val);
	req = val;
	pevent_read_number_field(bytes_alloc_field, record->data, &val);
	alloc = val;
	pevent_read_number_field(ptr_field, record->data, &ptr);

	func = pevent_find_function(pevent, callsite);

	add_kmalloc(func, ptr, req, alloc);
}

static void
process_kfree(struct pevent *pevent, struct pevent_record *record,
	      struct format_field *ptr_field)
{
	unsigned long long ptr;

	pevent_read_number_field(ptr_field, record->data, &ptr);

	remove_kmalloc(ptr);
}

static void
process_record(struct pevent *pevent, struct pevent_record *record)
{
	unsigned long long val;
	int type;

	pevent_read_number_field(common_type_field, record->data, &val);
	type = val;

	if (type == kmalloc_type)
		return process_kmalloc(pevent, record,
				       kmalloc_callsite_field,
				       kmalloc_bytes_req_field,
				       kmalloc_bytes_alloc_field,
				       kmalloc_ptr_field);
	if (type == kmalloc_node_type)
		return process_kmalloc(pevent, record,
				       kmalloc_node_callsite_field,
				       kmalloc_node_bytes_req_field,
				       kmalloc_node_bytes_alloc_field,
				       kmalloc_node_ptr_field);
	if (type == kfree_type)
		return process_kfree(pevent, record, kfree_ptr_field);

	if (type == kmem_cache_alloc_type)
		return process_kmalloc(pevent, record,
				       kmem_cache_callsite_field,
				       kmem_cache_bytes_req_field,
				       kmem_cache_bytes_alloc_field,
				       kmem_cache_ptr_field);
	if (type == kmem_cache_alloc_node_type)
		return process_kmalloc(pevent, record,
				       kmem_cache_node_callsite_field,
				       kmem_cache_node_bytes_req_field,
				       kmem_cache_node_bytes_alloc_field,
				       kmem_cache_node_ptr_field);
	if (type == kmem_cache_free_type)
		return process_kfree(pevent, record, kmem_cache_free_ptr_field);
}

static int func_cmp(const void *a, const void *b)
{
	const struct func_descr *fa = *(const struct func_descr **)a;
	const struct func_descr *fb = *(const struct func_descr **)b;

	if (fa->waste > fb->waste)
		return -1;
	if (fa->waste < fb->waste)
		return 1;
	return 0;
}

static void sort_list(void)
{
	struct func_descr *funcd;
	int h;
	int i = 0;

	func_list = zalloc(sizeof(*func_list) * func_count);

	for (h = 0; h < HASH_SIZE; h++) {
		for (funcd = func_hash[h]; funcd; funcd = funcd->next) {
			funcd->waste = funcd->current_alloc - funcd->current_req;
			funcd->max_waste = funcd->max_alloc - funcd->max_req;
			if (i == func_count)
				die("more funcs than expected\n");
			func_list[i++] = funcd;
		}
	}

	qsort(func_list, func_count, sizeof(*func_list), func_cmp);
}

static void print_list(void)
{
	struct func_descr *funcd;
	int i;

	printf("                Function            \t");
	printf("Waste\tAlloc\treq\t\tTotAlloc     TotReq\t\tMaxAlloc     MaxReq\t");
	printf("MaxWaste\n");
	printf("                --------            \t");
	printf("-----\t-----\t---\t\t--------     ------\t\t--------     ------\t");
	printf("--------\n");
	
	for (i = 0; i < func_count; i++) {
		funcd = func_list[i];

		printf("%32s\t%ld\t%ld\t%ld\t\t%8ld   %8ld\t\t%8ld   %8ld\t%ld\n",
		       funcd->func, funcd->waste,
		       funcd->current_alloc, funcd->current_req,
		       funcd->total_alloc, funcd->total_req,
		       funcd->max_alloc, funcd->max_req, funcd->max_waste);
	}
}

static void do_trace_mem(struct tracecmd_input *handle)
{
	struct pevent *pevent = tracecmd_get_pevent(handle);
	struct event_format *event;
	struct pevent_record *record;
	int missed_events = 0;
	int cpus;
	int cpu;
	int ret;

	ret = tracecmd_init_data(handle);
	if (ret < 0)
		die("failed to init data");

	if (ret > 0)
		die("trace-cmd mem does not work with latency traces\n");

	cpus = tracecmd_cpus(handle);

	/* Need to get any event */
	for (cpu = 0; cpu < cpus; cpu++) {
		record = tracecmd_peek_data(handle, cpu);
		if (record)
			break;
	}
	if (!record)
		die("No records found in file");

	ret = pevent_data_type(pevent, record);
	event = pevent_data_event_from_type(pevent, ret);

	common_type_field = pevent_find_common_field(event, "common_type");
	if (!common_type_field)
		die("Can't find a 'type' field?");

	update_kmalloc(pevent);
	update_kmalloc_node(pevent);
	update_kfree(pevent);
	update_kmem_cache_alloc(pevent);
	update_kmem_cache_alloc_node(pevent);
	update_kmem_cache_free(pevent);

	while ((record = tracecmd_read_next_data(handle, &cpu))) {

		/* record missed event */
		if (!missed_events && record->missed_events)
			missed_events = 1;

		process_record(pevent, record);
		free_record(record);
	}

	sort_list();
	print_list();
}

void trace_mem(int argc, char **argv)
{
	struct tracecmd_input *handle;
	const char *input_file = NULL;
	int ret;

	for (;;) {
		int c;

		c = getopt(argc-1, argv+1, "+hi:");
		if (c == -1)
			break;
		switch (c) {
		case 'h':
			usage(argv);
			break;
		case 'i':
			if (input_file)
				die("Only one input for mem");
			input_file = optarg;
			break;
		default:
			usage(argv);
		}
	}

	if ((argc - optind) >= 2) {
		if (input_file)
			usage(argv);
		input_file = argv[optind + 1];
	}

	if (!input_file)
		input_file = "trace.dat";

	handle = tracecmd_alloc(input_file);
	if (!handle)
		die("can't open %s\n", input_file);

	ret = tracecmd_read_headers(handle);
	if (ret)
		return;

	do_trace_mem(handle);

	tracecmd_close(handle);
}