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