summary.c revision e99af270a60891e68d465c4cd97dbe29cd1a05e4
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2003,2008,2009 Juan Cespedes
4 * Copyright (C) 2006 Ian Wienand
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22#include "config.h"
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <sys/time.h>
27
28#include "common.h"
29
30static int num_entries = 0;
31static struct entry_st {
32	char *name;
33	int count;
34	struct timeval tv;
35} *entries = NULL;
36
37static int tot_count = 0;
38static unsigned long int tot_usecs = 0;
39
40static void fill_struct(void *key, void *value, void *data)
41{
42	struct opt_c_struct *st = (struct opt_c_struct *)value;
43
44	entries = realloc(entries, (num_entries + 1) * sizeof(struct entry_st));
45	if (!entries) {
46		perror("realloc()");
47		exit(1);
48	}
49	entries[num_entries].name = (char *)key;
50	entries[num_entries].count = st->count;
51	entries[num_entries].tv = st->tv;
52
53	tot_count += st->count;
54	tot_usecs += 1000000 * st->tv.tv_sec;
55	tot_usecs += st->tv.tv_usec;
56
57	num_entries++;
58}
59
60static int compar(const void *a, const void *b)
61{
62	struct entry_st *en1, *en2;
63
64	en1 = (struct entry_st *)a;
65	en2 = (struct entry_st *)b;
66
67	if (en2->tv.tv_sec - en1->tv.tv_sec) {
68		return (en2->tv.tv_sec - en1->tv.tv_sec);
69	} else {
70		return (en2->tv.tv_usec - en1->tv.tv_usec);
71	}
72}
73
74void show_summary(void)
75{
76	int i;
77
78	num_entries = 0;
79	entries = NULL;
80
81	dict_apply_to_all(dict_opt_c, fill_struct, NULL);
82
83	qsort(entries, num_entries, sizeof(*entries), compar);
84
85	fprintf(options.output, "%% time     seconds  usecs/call     calls      function\n");
86	fprintf(options.output, "------ ----------- ----------- --------- --------------------\n");
87	for (i = 0; i < num_entries; i++) {
88		unsigned long long int c;
89		unsigned long long int p;
90		c = 1000000 * (int)entries[i].tv.tv_sec +
91		    (int)entries[i].tv.tv_usec;
92		p = 100000 * c / tot_usecs + 5;
93		fprintf(options.output, "%3lu.%02lu %4d.%06d %11lu %9d %s\n",
94		       (unsigned long int)(p / 1000),
95		       (unsigned long int)((p / 10) % 100),
96		       (int)entries[i].tv.tv_sec, (int)entries[i].tv.tv_usec,
97		       (unsigned long int)(c / entries[i].count),
98		       entries[i].count,
99#ifdef USE_DEMANGLE
100		       options.demangle ? my_demangle(entries[i].name) :
101#endif
102		       entries[i].name);
103	}
104	fprintf(options.output, "------ ----------- ----------- --------- --------------------\n");
105	fprintf(options.output, "100.00 %4lu.%06lu             %9d total\n", tot_usecs / 1000000,
106	       tot_usecs % 1000000, tot_count);
107}
108