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