| Ian Wienand | bfdaacb | 2006-02-20 22:58:38 +0100 | [diff] [blame] | 1 | #include "config.h" |
| Ian Wienand | bfdaacb | 2006-02-20 22:58:38 +0100 | [diff] [blame] | 2 | |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <sys/time.h> |
| 6 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 7 | #include "common.h" |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 8 | |
| 9 | static int num_entries = 0; |
| 10 | static struct entry_st { |
| 11 | char *name; |
| 12 | int count; |
| 13 | struct timeval tv; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 14 | } *entries = NULL; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 15 | |
| 16 | static int tot_count = 0; |
| 17 | static unsigned long int tot_usecs = 0; |
| 18 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 19 | static void fill_struct(void *key, void *value, void *data) |
| 20 | { |
| 21 | struct opt_c_struct *st = (struct opt_c_struct *)value; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 22 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 23 | entries = realloc(entries, (num_entries + 1) * sizeof(struct entry_st)); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 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; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 33 | tot_usecs += 1000000 * st->tv.tv_sec; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 34 | tot_usecs += st->tv.tv_usec; |
| 35 | |
| 36 | num_entries++; |
| 37 | } |
| 38 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 39 | static int compar(const void *a, const void *b) |
| 40 | { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 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 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 53 | void show_summary(void) |
| 54 | { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 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 | |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 64 | fprintf(options.output, "%% time seconds usecs/call calls function\n"); |
| 65 | fprintf(options.output, "------ ----------- ----------- --------- --------------------\n"); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 66 | for (i = 0; i < num_entries; i++) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 67 | unsigned long long int c; |
| 68 | unsigned long long int p; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 69 | c = 1000000 * (int)entries[i].tv.tv_sec + |
| 70 | (int)entries[i].tv.tv_usec; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 71 | p = 100000 * c / tot_usecs + 5; |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 72 | fprintf(options.output, "%3lu.%02lu %4d.%06d %11lu %9d %s\n", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 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), |
| Ian Wienand | bfdaacb | 2006-02-20 22:58:38 +0100 | [diff] [blame] | 77 | entries[i].count, |
| Olaf Hering | a841f65 | 2006-09-15 01:57:49 +0200 | [diff] [blame] | 78 | #ifdef USE_DEMANGLE |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 79 | options.demangle ? my_demangle(entries[i].name) : |
| Olaf Hering | a841f65 | 2006-09-15 01:57:49 +0200 | [diff] [blame] | 80 | #endif |
| 81 | entries[i].name); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 82 | } |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 83 | fprintf(options.output, "------ ----------- ----------- --------- --------------------\n"); |
| 84 | 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] | 85 | tot_usecs % 1000000, tot_count); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 86 | } |