summaryrefslogtreecommitdiff
path: root/mtshow.c
blob: a39602bba2ccfb8cb9ec03af3cf37d529a1020dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <Eina.h>

enum {
   BYTES, KB, MB, SIZE, COUNT
};

typedef struct _Group Group;

struct _Group
{
   char *trace;
   unsigned long total;
   unsigned long count;
};

static Eina_Hash *groups = NULL;
static Eina_List *sorted = NULL;
static int size = BYTES;
static int sort = SIZE;

static int
listsort(const void *data1, const void *data2)
{
   Group *g = (Group *)data1;
   Group *gg = (Group *)data2;
   if (sort == SIZE)       return (int)(g->total - gg->total);
   else if (sort == COUNT) return (int)(g->count - gg->count);
   return (int)(g->total - gg->total);
}

static Eina_Bool
hashwalk(const Eina_Hash *hash, const void *key, void *data, void *fdata)
{
   Group *g = data;
   sorted = eina_list_append(sorted, g);
   return EINA_TRUE;
}

int
main(int argc, char **argv)
{
   FILE *f;
   char *file;
   char buf[4096];
   int i;
   int group = 0;
   Group *g;
   Eina_List *l;
   unsigned long total_count = 0, total_size = 0;

   eina_init();
   if (argc < 1)
     {
        printf("usage: mtdump MTOUTFILE [OPT]\n"
               "OPT can be 0 or more of:\n"
               "  -f N     - group by trace of up to N functions in backtrace (0 == unlimited)\n"
               "  -m     - show size in Mb\n"
               "  -k     - show size in Kb\n"
               "  -b     - show size in bytes\n"
               "  -s     - sout by size\n"
               "  -c     - sout by count\n"
              );
        exit(1);
     }
   for (i = 0; i < argc; i++)
     {
        if (!strcmp(argv[i], "-f"))
          {
             i++;
             group = atoi(argv[i]);
          }
        else if (!strcmp(argv[i], "-m"))
          {
             size = MB;
          }
        else if (!strcmp(argv[i], "-k"))
          {
             size = KB;
          }
        else if (!strcmp(argv[i], "-b"))
          {
             size = BYTES;
          }
        else if (!strcmp(argv[i], "-s"))
          {
             sort = SIZE;
          }
        else if (!strcmp(argv[i], "-c"))
          {
             sort = COUNT;
          }
        else
          file = argv[i];
     }
   f = fopen(file, "r");
   if (!f)
     {
        perror("fopen");
        exit(1);
     }
   
   groups = eina_hash_string_superfast_new(NULL);
   while (fgets(buf, sizeof(buf) - 1, f))
     {
        int len;
        unsigned long bytes = 0;
        char *p;
        
        len = strlen(buf);
        if (len < 2) continue;
        if ((len > 1) && (buf[len - 1] == '\n'))
          {
             buf[len - 1] = 0;
             len--;
          }
        sscanf(buf, "%lu", &bytes);
        p = strchr(buf, '|');
        if (p)
          {
             char trace[4096] = "", *t;
             
             p += 2;
             if (group == 0)
               {
                  strcpy(trace, p);
               }
             else
               {
                  i = group;
                  t = trace;
                  while (*p)
                    {
                       if (isspace(*p))
                         {
                            i--;
                            if (i == 0) break;
                         }
                       *t = *p;
                       t++; p++;
                    }
                  *t = 0;
               }
             g = eina_hash_find(groups, trace);
             if (!g)
               {
                  g = calloc(1, sizeof(Group));
                  g->trace = strdup(trace);
                  eina_hash_add(groups, trace, g);
               }
             g->total += bytes;
             g->count++;
          }
     }
   fclose(f);
   eina_hash_foreach(groups, hashwalk, NULL);
   printf("|-- MEM --|- COUNT --|----------------------------------------------\n");
   sorted = eina_list_sort(sorted, eina_list_count(sorted), listsort);
   EINA_LIST_FOREACH(sorted, l, g)
     {
        switch (size)
          {
           case MB:
             printf("%10lu [%8lu] %s\n", g->total / (1024 * 1024), g->count, g->trace);
             break;
           case KB:
             printf("%10lu [%8lu] %s\n", g->total / 1024, g->count, g->trace);
             break;
           default:
             printf("%10lu [%8lu] %s\n", g->total, g->count, g->trace);
             break;
          }
        total_count += g->count;
        total_size += g->total;
     }
   printf("|-- MEM --|- COUNT --|----------------------------------------------\n");
   printf("Total allocs: %lu\n", total_count);
   switch (size)
     {
      case MB:
        printf("Total memory: %lu Mb\n", total_size / (1024 * 1024));
        break;
      case KB:
        printf("Total memory: %lu Kb\n", total_size / 1024);
        break;
      default:
        printf("Total memory: %lu bytes\n", total_size);
        break;
     }
   exit(0);
   return 0;
}