Git init
[external/ltrace.git] / summary.c
1 #include "config.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/time.h>
6
7 #include "common.h"
8
9 static int num_entries = 0;
10 static struct entry_st {
11         char *name;
12         int count;
13         struct timeval tv;
14 } *entries = NULL;
15
16 static int tot_count = 0;
17 static unsigned long int tot_usecs = 0;
18
19 static void fill_struct(void *key, void *value, void *data)
20 {
21         struct opt_c_struct *st = (struct opt_c_struct *)value;
22
23         entries = realloc(entries, (num_entries + 1) * sizeof(struct entry_st));
24         if (!entries) {
25                 perror("realloc()");
26                 exit(1);
27         }
28         entries[num_entries].name = (char *)key;
29         entries[num_entries].count = st->count;
30         entries[num_entries].tv = st->tv;
31
32         tot_count += st->count;
33         tot_usecs += 1000000 * st->tv.tv_sec;
34         tot_usecs += st->tv.tv_usec;
35
36         num_entries++;
37 }
38
39 static int compar(const void *a, const void *b)
40 {
41         struct entry_st *en1, *en2;
42
43         en1 = (struct entry_st *)a;
44         en2 = (struct entry_st *)b;
45
46         if (en2->tv.tv_sec - en1->tv.tv_sec) {
47                 return (en2->tv.tv_sec - en1->tv.tv_sec);
48         } else {
49                 return (en2->tv.tv_usec - en1->tv.tv_usec);
50         }
51 }
52
53 void show_summary(void)
54 {
55         int i;
56
57         num_entries = 0;
58         entries = NULL;
59
60         dict_apply_to_all(dict_opt_c, fill_struct, NULL);
61
62         qsort(entries, num_entries, sizeof(*entries), compar);
63
64         fprintf(options.output, "%% time     seconds  usecs/call     calls      function\n");
65         fprintf(options.output, "------ ----------- ----------- --------- --------------------\n");
66         for (i = 0; i < num_entries; i++) {
67                 unsigned long long int c;
68                 unsigned long long int p;
69                 c = 1000000 * (int)entries[i].tv.tv_sec +
70                     (int)entries[i].tv.tv_usec;
71                 p = 100000 * c / tot_usecs + 5;
72                 fprintf(options.output, "%3lu.%02lu %4d.%06d %11lu %9d %s\n",
73                        (unsigned long int)(p / 1000),
74                        (unsigned long int)((p / 10) % 100),
75                        (int)entries[i].tv.tv_sec, (int)entries[i].tv.tv_usec,
76                        (unsigned long int)(c / entries[i].count),
77                        entries[i].count,
78 #ifdef USE_DEMANGLE
79                        options.demangle ? my_demangle(entries[i].name) :
80 #endif
81                        entries[i].name);
82         }
83         fprintf(options.output, "------ ----------- ----------- --------- --------------------\n");
84         fprintf(options.output, "100.00 %4lu.%06lu             %9d total\n", tot_usecs / 1000000,
85                tot_usecs % 1000000, tot_count);
86 }