d3cb8985d9db29138d54aacfedf8cb93abec239d
[mono.git] / mono / profiler / decode.c
1 /*
2  * decode.c: mprof-report program source: decode and analyze the log profiler data
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2010 Novell, Inc (http://www.novell.com)
8  */
9 #include <config.h>
10 #include "utils.c"
11 #include "proflog.h"
12 #include <string.h>
13 #include <assert.h>
14 #include <stdio.h>
15 #if !defined(__APPLE__) && !defined(__FreeBSD__)
16 #include <malloc.h>
17 #endif
18 #include <unistd.h>
19 #include <stdlib.h>
20 #if defined (HAVE_SYS_ZLIB)
21 #include <zlib.h>
22 #endif
23 #include <mono/metadata/profiler.h>
24 #include <mono/metadata/object.h>
25 #include <mono/metadata/debug-helpers.h>
26
27 #define HASH_SIZE 9371
28 #define SMALL_HASH_SIZE 31
29
30 static int debug = 0;
31 static int collect_traces = 0;
32 static int show_traces = 0;
33 static int trace_max = 6;
34 static int verbose = 0;
35 static uintptr_t *tracked_objects = 0;
36 static int num_tracked_objects = 0;
37 static uintptr_t thread_filter = 0;
38 static uint64_t find_size = 0;
39 static const char* find_name = NULL;
40 static uint64_t time_from = 0;
41 static uint64_t time_to = 0xffffffffffffffffULL;
42 static uint64_t startup_time = 0;
43 static FILE* outfile = NULL;
44
45 static int32_t
46 read_int32 (unsigned char *p)
47 {
48         int32_t value = *p++;
49         value |= (*p++) << 8;
50         value |= (*p++) << 16;
51         value |= (uint32_t)(*p++) << 24;
52         return value;
53 }
54
55 static int64_t
56 read_int64 (unsigned char *p)
57 {
58         uint64_t value = *p++;
59         value |= (*p++) << 8;
60         value |= (*p++) << 16;
61         value |= (uint64_t)(*p++) << 24;
62         value |= (uint64_t)(*p++) << 32;
63         value |= (uint64_t)(*p++) << 40;
64         value |= (uint64_t)(*p++) << 48;
65         value |= (uint64_t)(*p++) << 54;
66         return value;
67 }
68
69 static char*
70 pstrdup (const char *s)
71 {
72         int len = strlen (s) + 1;
73         char *p = malloc (len);
74         memcpy (p, s, len);
75         return p;
76 }
77
78 static int num_images;
79 typedef struct _ImageDesc ImageDesc;
80 struct _ImageDesc {
81         ImageDesc *next;
82         intptr_t image;
83         char *filename;
84 };
85
86 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
87
88 static void
89 add_image (intptr_t image, char *name)
90 {
91         int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
92         ImageDesc *cd = malloc (sizeof (ImageDesc));
93         cd->image = image;
94         cd->filename = pstrdup (name);
95         cd->next = image_hash [slot];
96         image_hash [slot] = cd;
97         num_images++;
98 }
99
100 typedef struct _BackTrace BackTrace;
101 typedef struct {
102         uint64_t count;
103         BackTrace *bt;
104 } CallContext;
105
106 typedef struct {
107         int count;
108         int size;
109         CallContext *traces;
110 } TraceDesc;
111
112 typedef struct _ClassDesc ClassDesc;
113 struct _ClassDesc {
114         ClassDesc *next;
115         intptr_t klass;
116         char *name;
117         intptr_t allocs;
118         uint64_t alloc_size;
119         TraceDesc traces;
120 };
121
122 static ClassDesc* class_hash [HASH_SIZE] = {0};
123 static int num_classes = 0;
124
125 static ClassDesc*
126 add_class (intptr_t klass, const char *name)
127 {
128         int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
129         ClassDesc *cd;
130         cd = class_hash [slot];
131         while (cd && cd->klass != klass)
132                 cd = cd->next;
133         /* we resolved an unknown class (unless we had the code unloaded) */
134         if (cd) {
135                 /*printf ("resolved unknown: %s\n", name);*/
136                 free (cd->name);
137                 cd->name = pstrdup (name);
138                 return cd;
139         }
140         cd = calloc (sizeof (ClassDesc), 1);
141         cd->klass = klass;
142         cd->name = pstrdup (name);
143         cd->next = class_hash [slot];
144         cd->allocs = 0;
145         cd->alloc_size = 0;
146         cd->traces.count = 0;
147         cd->traces.size = 0;
148         cd->traces.traces = NULL;
149         class_hash [slot] = cd;
150         num_classes++;
151         return cd;
152 }
153
154 static ClassDesc *
155 lookup_class (intptr_t klass)
156 {
157         int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
158         ClassDesc *cd = class_hash [slot];
159         while (cd && cd->klass != klass)
160                 cd = cd->next;
161         if (!cd)
162                 return add_class (klass, "unresolved class");
163         return cd;
164 }
165
166 typedef struct _MethodDesc MethodDesc;
167 struct _MethodDesc {
168         MethodDesc *next;
169         intptr_t method;
170         char *name;
171         intptr_t code;
172         int len;
173         int recurse_count;
174         uint64_t calls;
175         uint64_t total_time;
176         uint64_t callee_time;
177         uint64_t self_time;
178         TraceDesc traces;
179 };
180
181 static MethodDesc* method_hash [HASH_SIZE] = {0};
182 static int num_methods = 0;
183
184 static MethodDesc*
185 add_method (intptr_t method, const char *name, intptr_t code, int len)
186 {
187         int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
188         MethodDesc *cd;
189         cd = method_hash [slot];
190         while (cd && cd->method != method)
191                 cd = cd->next;
192         /* we resolved an unknown method (unless we had the code unloaded) */
193         if (cd) {
194                 cd->code = code;
195                 cd->len = len;
196                 /*printf ("resolved unknown: %s\n", name);*/
197                 free (cd->name);
198                 cd->name = pstrdup (name);
199                 return cd;
200         }
201         cd = calloc (sizeof (MethodDesc), 1);
202         cd->method = method;
203         cd->name = pstrdup (name);
204         cd->code = code;
205         cd->len = len;
206         cd->calls = 0;
207         cd->total_time = 0;
208         cd->traces.count = 0;
209         cd->traces.size = 0;
210         cd->traces.traces = NULL;
211         cd->next = method_hash [slot];
212         method_hash [slot] = cd;
213         num_methods++;
214         return cd;
215 }
216
217 static MethodDesc *
218 lookup_method (intptr_t method)
219 {
220         int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
221         MethodDesc *cd = method_hash [slot];
222         while (cd && cd->method != method)
223                 cd = cd->next;
224         if (!cd)
225                 return add_method (method, "unknown method", 0, 0);
226         return cd;
227 }
228
229 typedef struct _HeapClassDesc HeapClassDesc;
230 typedef struct {
231         HeapClassDesc *klass;
232         uint64_t count;
233 } HeapClassRevRef;
234
235 struct _HeapClassDesc {
236         ClassDesc *klass;
237         int64_t count;
238         int64_t total_size;
239         HeapClassRevRef *rev_hash;
240         int rev_hash_size;
241         int rev_count;
242         uintptr_t pinned_references;
243         uintptr_t root_references;
244 };
245
246 static int
247 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
248 {
249         uintptr_t i;
250         uintptr_t start_pos;
251         start_pos = (hklass->klass->klass >> 2) % size;
252         assert (start_pos < size);
253         i = start_pos;
254         do {
255                 if (rev_hash [i].klass == hklass) {
256                         rev_hash [i].count += value;
257                         return 0;
258                 } else if (!rev_hash [i].klass) {
259                         rev_hash [i].klass = hklass;
260                         rev_hash [i].count += value;
261                         start_pos = 0;
262                         for (i = 0; i < size; ++i)
263                                 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
264                                         start_pos ++;
265                         assert (start_pos == 1);
266                         return 1;
267                 }
268                 /* wrap around */
269                 if (++i == size)
270                         i = 0;
271         } while (i != start_pos);
272         /* should not happen */
273         printf ("failed revref store\n");
274         return 0;
275 }
276
277 static void
278 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
279 {
280         uintptr_t i;
281         if (to->rev_count * 2 >= to->rev_hash_size) {
282                 HeapClassRevRef *n;
283                 uintptr_t old_size = to->rev_hash_size;
284                 to->rev_hash_size *= 2;
285                 if (to->rev_hash_size == 0)
286                         to->rev_hash_size = 4;
287                 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
288                 for (i = 0; i < old_size; ++i) {
289                         if (to->rev_hash [i].klass)
290                                 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
291                 }
292                 if (to->rev_hash)
293                         free (to->rev_hash);
294                 to->rev_hash = n;
295         }
296         to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
297 }
298
299 typedef struct {
300         uintptr_t objaddr;
301         HeapClassDesc *hklass;
302         uintptr_t num_refs;
303         uintptr_t refs [0];
304 } HeapObjectDesc;
305
306 typedef struct _HeapShot HeapShot;
307 struct _HeapShot {
308         HeapShot *next;
309         uint64_t timestamp;
310         int class_count;
311         int hash_size;
312         HeapClassDesc **class_hash;
313         HeapClassDesc **sorted;
314         HeapObjectDesc **objects_hash;
315         uintptr_t objects_count;
316         uintptr_t objects_hash_size;
317         uintptr_t num_roots;
318         uintptr_t *roots;
319         uintptr_t *roots_extra;
320         int *roots_types;
321 };
322
323 static HeapShot *heap_shots = NULL;
324 static int num_heap_shots = 0;
325
326 static HeapShot*
327 new_heap_shot (uint64_t timestamp)
328 {
329         HeapShot *hs = calloc (sizeof (HeapShot), 1);
330         hs->hash_size = 4;
331         hs->class_hash = calloc (sizeof (void*), hs->hash_size);
332         hs->timestamp = timestamp;
333         num_heap_shots++;
334         hs->next = heap_shots;
335         heap_shots = hs;
336         return hs;
337 }
338
339 static HeapClassDesc*
340 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
341 {
342         int i;
343         unsigned int start_pos;
344         start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
345         i = start_pos;
346         do {
347                 HeapClassDesc* cd = hs->class_hash [i];
348                 if (!cd)
349                         return NULL;
350                 if (cd->klass == klass)
351                         return cd;
352                 /* wrap around */
353                 if (++i == hs->hash_size)
354                         i = 0;
355         } while (i != start_pos);
356         return NULL;
357 }
358
359 static int
360 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
361 {
362         uintptr_t i;
363         uintptr_t start_pos;
364         start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
365         i = start_pos;
366         do {
367                 if (hash [i] && hash [i]->klass == klass) {
368                         hash [i]->total_size += size;
369                         hash [i]->count += count;
370                         *retv = hash [i];
371                         return 0;
372                 } else if (!hash [i]) {
373                         if (*retv) {
374                                 hash [i] = *retv;
375                                 return 1;
376                         }
377                         hash [i] = calloc (sizeof (HeapClassDesc), 1);
378                         hash [i]->klass = klass;
379                         hash [i]->total_size += size;
380                         hash [i]->count += count;
381                         *retv = hash [i];
382                         return 1;
383                 }
384                 /* wrap around */
385                 if (++i == hsize)
386                         i = 0;
387         } while (i != start_pos);
388         /* should not happen */
389         printf ("failed heap class store\n");
390         return 0;
391 }
392
393 static HeapClassDesc*
394 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
395 {
396         HeapClassDesc *res;
397         int i;
398         if (hs->class_count * 2 >= hs->hash_size) {
399                 HeapClassDesc **n;
400                 int old_size = hs->hash_size;
401                 hs->hash_size *= 2;
402                 if (hs->hash_size == 0)
403                         hs->hash_size = 4;
404                 n = calloc (sizeof (void*) * hs->hash_size, 1);
405                 for (i = 0; i < old_size; ++i) {
406                         res = hs->class_hash [i];
407                         if (hs->class_hash [i])
408                                 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
409                 }
410                 if (hs->class_hash)
411                         free (hs->class_hash);
412                 hs->class_hash = n;
413         }
414         res = NULL;
415         hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
416         //if (res->count == 1)
417         //      printf ("added heap class: %s\n", res->klass->name);
418         return res;
419 }
420
421 static HeapObjectDesc*
422 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
423 {
424         HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
425         ho->objaddr = objaddr;
426         ho->hklass = hklass;
427         ho->num_refs = num_refs;
428         return ho;
429 }
430
431 static uintptr_t
432 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
433 {
434         uintptr_t i;
435         uintptr_t start_pos;
436         HeapObjectDesc **hash = hs->objects_hash;
437         start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
438         i = start_pos;
439         do {
440                 if (hash [i] && hash [i]->objaddr == objaddr) {
441                         return i;
442                 } else if (!hash [i]) {
443                         break; /* fail */
444                 }
445                 /* wrap around */
446                 if (++i == hs->objects_hash_size)
447                         i = 0;
448         } while (i != start_pos);
449         /* should not happen */
450         //printf ("failed heap obj slot\n");
451         return -1;
452 }
453
454 static HeapObjectDesc*
455 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
456 {
457         HeapObjectDesc **hash = hs->objects_hash;
458         uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
459         if (i >= 0) {
460                 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
461                 *ref_offset = hash [i]->num_refs;
462                 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
463                 free (hash [i]);
464                 hash [i] = ho;
465                 return ho;
466         }
467         /* should not happen */
468         printf ("failed heap obj update\n");
469         return NULL;
470         
471 }
472
473 static uintptr_t
474 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
475 {
476         uintptr_t i;
477         uintptr_t start_pos;
478         start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
479         i = start_pos;
480         do {
481                 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
482                         printf ("duplicate object!\n");
483                         return 0;
484                 } else if (!hash [i]) {
485                         hash [i] = obj;
486                         return 1;
487                 }
488                 /* wrap around */
489                 if (++i == hsize)
490                         i = 0;
491         } while (i != start_pos);
492         /* should not happen */
493         printf ("failed heap obj store\n");
494         return 0;
495 }
496
497 static void
498 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
499 {
500         uintptr_t i;
501         if (hs->objects_count * 2 >= hs->objects_hash_size) {
502                 HeapObjectDesc **n;
503                 uintptr_t old_size = hs->objects_hash_size;
504                 hs->objects_hash_size *= 2;
505                 if (hs->objects_hash_size == 0)
506                         hs->objects_hash_size = 4;
507                 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
508                 for (i = 0; i < old_size; ++i) {
509                         if (hs->objects_hash [i])
510                                 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
511                 }
512                 if (hs->objects_hash)
513                         free (hs->objects_hash);
514                 hs->objects_hash = n;
515         }
516         hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
517 }
518
519 static void
520 heap_shot_resolve_reverse_refs (HeapShot *hs)
521 {
522         uintptr_t i;
523         for (i = 0; i < hs->objects_hash_size; ++i) {
524                 uintptr_t r;
525                 HeapObjectDesc *ho = hs->objects_hash [i];
526                 if (!ho)
527                         continue;
528                 for (r = 0; r < ho->num_refs; ++r) {
529                         uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
530                         add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
531                 }
532         }
533 }
534
535 #define MARK_GRAY 1
536 #define MARK_BLACK 2
537
538 static void
539 heap_shot_mark_objects (HeapShot *hs)
540 {
541         uintptr_t i, oi, r;
542         unsigned char *marks;
543         HeapObjectDesc *obj, *ref;
544         int marked_some;
545         uintptr_t num_marked = 0, num_unmarked;
546         for (i = 0; i < hs->num_roots; ++i) {
547                 HeapClassDesc *cd;
548                 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
549                 if (oi == -1) {
550                         continue;
551                 }
552                 obj = hs->objects_hash [oi];
553                 cd = obj->hklass;
554                 if (hs->roots_types [i] & MONO_PROFILE_GC_ROOT_PINNING)
555                         cd->pinned_references++;
556                 cd->root_references++;
557         }
558         if (!debug)
559                 return;
560         /* consistency checks: it seems not all the objects are walked in the heap in some cases */
561         marks = calloc (hs->objects_hash_size, 1);
562         if (!marks)
563                 return;
564         for (i = 0; i < hs->num_roots; ++i) {
565                 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
566                 if (oi == -1) {
567                         fprintf (outfile, "root type 0x%x for obj %p (%s) not found in heap\n", hs->roots_types [i], (void*)hs->roots [i], lookup_class (hs->roots_extra [i])->name);
568                         continue;
569                 }
570                 obj = hs->objects_hash [oi];
571                 if (!marks [oi]) {
572                         marks [oi] = obj->num_refs? MARK_GRAY: MARK_BLACK;
573                         num_marked++;
574                 }
575         }
576         marked_some = 1;
577         while (marked_some) {
578                 marked_some = 0;
579                 for (i = 0; i < hs->objects_hash_size; ++i) {
580                         if (marks [i] != MARK_GRAY)
581                                 continue;
582                         marks [i] = MARK_BLACK;
583                         obj = hs->objects_hash [i];
584                         for (r = 0; r < obj->num_refs; ++r) {
585                                 oi = heap_shot_find_obj_slot (hs, obj->refs [r]);
586                                 if (oi == -1) {
587                                         fprintf (outfile, "referenced obj %p not found in heap\n", (void*)obj->refs [r]);
588                                         continue;
589                                 }
590                                 ref = hs->objects_hash [oi];
591                                 if (!marks [oi]) {
592                                         marks [oi] = ref->num_refs? MARK_GRAY: MARK_BLACK;
593                                 }
594                         }
595                         marked_some++;
596                 }
597         }
598
599         num_unmarked = 0;
600         for (i = 0; i < hs->objects_hash_size; ++i) {
601                 if (hs->objects_hash [i] && !marks [i]) {
602                         num_unmarked++;
603                         fprintf (outfile, "object %p (%s) unmarked\n", (void*)hs->objects_hash [i], hs->objects_hash [i]->hklass->klass->name);
604                 }
605         }
606         fprintf (outfile, "Total unmarked: %d/%d\n", num_unmarked, hs->objects_count);
607         free (marks);
608 }
609
610 static void
611 heap_shot_free_objects (HeapShot *hs)
612 {
613         uintptr_t i;
614         for (i = 0; i < hs->objects_hash_size; ++i) {
615                 HeapObjectDesc *ho = hs->objects_hash [i];
616                 if (ho)
617                         free (ho);
618         }
619         if (hs->objects_hash)
620                 free (hs->objects_hash);
621         hs->objects_hash = NULL;
622         hs->objects_hash_size = 0;
623         hs->objects_count = 0;
624 }
625
626 struct _BackTrace {
627         BackTrace *next;
628         unsigned int hash;
629         int count;
630         int id;
631         MethodDesc *methods [1];
632 };
633
634 static BackTrace *backtrace_hash [HASH_SIZE];
635 static BackTrace **backtraces = NULL;
636 static int num_backtraces = 0;
637 static int next_backtrace = 0;
638
639 static int
640 hash_backtrace (int count, MethodDesc **methods)
641 {
642         int hash = count;
643         int i;
644         for (i = 0; i < count; ++i) {
645                 hash = (hash << 5) - hash + methods [i]->method;
646         }
647         return hash;
648 }
649
650 static int
651 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
652 {
653         int i;
654         if (bt->count != count)
655                 return 0;
656         for (i = 0; i < count; ++i)
657                 if (methods [i] != bt->methods [i])
658                         return 0;
659         return 1;
660 }
661
662 static BackTrace*
663 add_backtrace (int count, MethodDesc **methods)
664 {
665         int hash = hash_backtrace (count, methods);
666         int slot = (hash & 0xffff) % HASH_SIZE;
667         BackTrace *bt = backtrace_hash [slot];
668         while (bt) {
669                 if (bt->hash == hash && compare_backtrace (bt, count, methods))
670                         return bt;
671                 bt = bt->next;
672         }
673         bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
674         bt->next = backtrace_hash [slot];
675         backtrace_hash [slot] = bt;
676         if (next_backtrace == num_backtraces) {
677                 num_backtraces *= 2;
678                 if (!num_backtraces)
679                         num_backtraces = 16;
680                 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
681         }
682         bt->id = next_backtrace++;
683         backtraces [bt->id] = bt;
684         bt->count = count;
685         bt->hash = hash;
686         for (slot = 0; slot < count; ++slot)
687                 bt->methods [slot] = methods [slot];
688
689         return bt;
690 }
691
692 typedef struct _MonitorDesc MonitorDesc;
693 typedef struct _ThreadContext ThreadContext;
694
695 typedef struct {
696         FILE *file;
697 #if defined (HAVE_SYS_ZLIB)
698         gzFile *gzfile;
699 #endif
700         unsigned char *buf;
701         int size;
702         int data_version;
703         int version_major;
704         int version_minor;
705         int timer_overhead;
706         int pid;
707         uint64_t startup_time;
708         ThreadContext *threads;
709         ThreadContext *current;
710 } ProfContext;
711
712 struct _ThreadContext {
713         ThreadContext *next;
714         intptr_t thread_id;
715         char *name;
716         /* emulated stack */
717         MethodDesc **stack;
718         uint64_t *time_stack;
719         uint64_t *callee_time_stack;
720         uint64_t last_time;
721         uint64_t contention_start;
722         MonitorDesc *monitor;
723         int stack_size;
724         int stack_id;
725         HeapShot *current_heap_shot;
726         uintptr_t num_roots;
727         uintptr_t size_roots;
728         uintptr_t *roots;
729         uintptr_t *roots_extra;
730         int *roots_types;
731 };
732
733 static void
734 ensure_buffer (ProfContext *ctx, int size)
735 {
736         if (ctx->size < size) {
737                 ctx->buf = realloc (ctx->buf, size);
738                 ctx->size = size;
739         }
740 }
741
742 static int
743 load_data (ProfContext *ctx, int size)
744 {
745         ensure_buffer (ctx, size);
746 #if defined (HAVE_SYS_ZLIB)
747         if (ctx->gzfile) {
748                 int r = gzread (ctx->gzfile, ctx->buf, size);
749                 if (r == 0)
750                         return size == 0? 1: 0;
751                 return r == size;
752         } else 
753 #endif
754         {
755                 int r = fread (ctx->buf, size, 1, ctx->file);
756                 if (r == 0)
757                         return size == 0? 1: 0;
758                 return r;
759         }
760 }
761
762 static ThreadContext*
763 get_thread (ProfContext *ctx, intptr_t thread_id)
764 {
765         ThreadContext *thread;
766         if (ctx->current && ctx->current->thread_id == thread_id)
767                 return ctx->current;
768         thread = ctx->threads;
769         while (thread) {
770                 if (thread->thread_id == thread_id) {
771                         return thread;
772                 }
773                 thread = thread->next;
774         }
775         thread = calloc (sizeof (ThreadContext), 1);
776         thread->next = ctx->threads;
777         ctx->threads = thread;
778         thread->thread_id = thread_id;
779         thread->last_time = 0;
780         thread->stack_id = 0;
781         thread->stack_size = 32;
782         thread->stack = malloc (thread->stack_size * sizeof (void*));
783         thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
784         thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
785         return thread;
786 }
787
788 static ThreadContext*
789 load_thread (ProfContext *ctx, intptr_t thread_id)
790 {
791         ThreadContext *thread = get_thread (ctx, thread_id);
792         ctx->current = thread;
793         return thread;
794 }
795
796 static void
797 ensure_thread_stack (ThreadContext *thread)
798 {
799         if (thread->stack_id == thread->stack_size) {
800                 thread->stack_size *= 2;
801                 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
802                 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
803                 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
804         }
805 }
806
807 static int
808 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
809 {
810         int i;
811         unsigned int start_pos;
812         start_pos = bt->hash % size;
813         i = start_pos;
814         do {
815                 if (traces [i].bt == bt) {
816                         traces [i].count += value;
817                         return 0;
818                 } else if (!traces [i].bt) {
819                         traces [i].bt = bt;
820                         traces [i].count += value;
821                         return 1;
822                 }
823                 /* wrap around */
824                 if (++i == size)
825                         i = 0;
826         } while (i != start_pos);
827         /* should not happen */
828         printf ("failed trace store\n");
829         return 0;
830 }
831
832 static void
833 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
834 {
835         int i;
836         if (!collect_traces)
837                 return;
838         if (trace->count * 2 >= trace->size) {
839                 CallContext *n;
840                 int old_size = trace->size;
841                 trace->size *= 2;
842                 if (trace->size == 0)
843                         trace->size = 4;
844                 n = calloc (sizeof (CallContext) * trace->size, 1);
845                 for (i = 0; i < old_size; ++i) {
846                         if (trace->traces [i].bt)
847                                 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
848                 }
849                 if (trace->traces)
850                         free (trace->traces);
851                 trace->traces = n;
852         }
853         trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
854 }
855
856 static BackTrace*
857 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
858 {
859         BackTrace *bt;
860         int count = thread->stack_id;
861         if (!collect_traces)
862                 return NULL;
863         if (count > trace_max)
864                 count = trace_max;
865         bt = add_backtrace (count, thread->stack + thread->stack_id - count);
866         add_trace_bt (bt, trace, value);
867         return bt;
868 }
869
870 static BackTrace*
871 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
872 {
873         BackTrace *bt;
874         if (!collect_traces)
875                 return NULL;
876         if (count > trace_max)
877                 count = trace_max;
878         bt = add_backtrace (count, methods);
879         add_trace_bt (bt, trace, value);
880         return bt;
881 }
882
883 static void
884 thread_add_root (ThreadContext *ctx, uintptr_t obj, int root_type, uintptr_t extra_info)
885 {
886         if (ctx->num_roots == ctx->size_roots) {
887                 int new_size = ctx->size_roots * 2;
888                 if (!new_size)
889                         new_size = 4;
890                 ctx->roots = realloc (ctx->roots, new_size * sizeof (uintptr_t));
891                 ctx->roots_extra = realloc (ctx->roots_extra, new_size * sizeof (uintptr_t));
892                 ctx->roots_types = realloc (ctx->roots_types, new_size * sizeof (int));
893                 ctx->size_roots = new_size;
894         }
895         ctx->roots_types [ctx->num_roots] = root_type;
896         ctx->roots_extra [ctx->num_roots] = extra_info;
897         ctx->roots [ctx->num_roots++] = obj;
898 }
899
900 static int
901 compare_callc (const void *a, const void *b)
902 {
903         const CallContext *A = a;
904         const CallContext *B = b;
905         if (B->count == A->count)
906                 return 0;
907         if (B->count < A->count)
908                 return -1;
909         return 1;
910 }
911
912 static void
913 sort_context_array (TraceDesc* traces)
914 {
915         int i, j;
916         for (i = 0, j = 0; i < traces->size; ++i) {
917                 if (traces->traces [i].bt) {
918                         traces->traces [j].bt = traces->traces [i].bt;
919                         traces->traces [j].count = traces->traces [i].count;
920                         j++;
921                 }
922         }
923         qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
924 }
925
926 static void
927 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
928 {
929         ensure_thread_stack (thread);
930         thread->time_stack [thread->stack_id] = timestamp;
931         thread->callee_time_stack [thread->stack_id] = 0;
932         thread->stack [thread->stack_id++] = method;
933         method->recurse_count++;
934 }
935
936 static void
937 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
938 {
939         method->recurse_count--;
940         if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
941                 uint64_t tdiff;
942                 thread->stack_id--;
943                 method->calls++;
944                 if (timestamp < thread->time_stack [thread->stack_id])
945                         fprintf (outfile, "time went backwards for %s\n", method->name);
946                 tdiff = timestamp - thread->time_stack [thread->stack_id];
947                 if (thread->callee_time_stack [thread->stack_id] > tdiff)
948                         fprintf (outfile, "callee time bigger for %s\n", method->name);
949                 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
950                 method->callee_time += thread->callee_time_stack [thread->stack_id];
951                 if (thread->stack_id)
952                         thread->callee_time_stack [thread->stack_id - 1] += tdiff;
953                 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
954         } else {
955                 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
956         }
957 }
958
959 typedef struct {
960         uint64_t start_time; /* move this per-thread? */
961         uint64_t total_time;
962         uint64_t max_time;
963         int count;
964 } GCDesc;
965 static GCDesc gc_info [3];
966 static uint64_t max_heap_size;
967 static uint64_t gc_object_moves;
968 static int gc_resizes;
969 typedef struct {
970         uint64_t created;
971         uint64_t destroyed;
972         uint64_t max_live;
973         TraceDesc traces;
974 } HandleInfo;
975 static HandleInfo handle_info [4];
976
977 static const char*
978 gc_event_name (int ev)
979 {
980         switch (ev) {
981         case MONO_GC_EVENT_START: return "start";
982         case MONO_GC_EVENT_MARK_START: return "mark start";
983         case MONO_GC_EVENT_MARK_END: return "mark end";
984         case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
985         case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
986         case MONO_GC_EVENT_END: return "end";
987         case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
988         case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
989         case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
990         case MONO_GC_EVENT_POST_START_WORLD: return "post start";
991         default:
992                 return "unknown";
993         }
994 }
995
996 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
997 static uint64_t throw_count = 0;
998 static TraceDesc exc_traces;
999
1000 static const char*
1001 clause_name (int type)
1002 {
1003         switch (type) {
1004         case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
1005         case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
1006         case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
1007         case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
1008         default: return "invalid";
1009         }
1010 }
1011
1012 static uint64_t monitor_contention;
1013 static uint64_t monitor_failed;
1014 static uint64_t monitor_acquired;
1015
1016 struct _MonitorDesc {
1017         MonitorDesc *next;
1018         uintptr_t objid;
1019         uintptr_t contentions;
1020         uint64_t wait_time;
1021         uint64_t max_wait_time;
1022         TraceDesc traces;
1023 };
1024
1025 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
1026 static int num_monitors = 0;
1027
1028 static MonitorDesc*
1029 lookup_monitor (uintptr_t objid)
1030 {
1031         int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
1032         MonitorDesc *cd = monitor_hash [slot];
1033         while (cd && cd->objid != objid)
1034                 cd = cd->next;
1035         if (!cd) {
1036                 cd = calloc (sizeof (MonitorDesc), 1);
1037                 cd->objid = objid;
1038                 cd->next = monitor_hash [slot];
1039                 monitor_hash [slot] = cd;
1040                 num_monitors++;
1041         }
1042         return cd;
1043 }
1044
1045 static const char*
1046 monitor_ev_name (int ev)
1047 {
1048         switch (ev) {
1049         case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
1050         case MONO_PROFILER_MONITOR_DONE: return "acquired";
1051         case MONO_PROFILER_MONITOR_FAIL: return "not taken";
1052         default: return "invalid";
1053         }
1054 }
1055
1056 static const char*
1057 get_handle_name (int htype)
1058 {
1059         switch (htype) {
1060         case 0: return "weak";
1061         case 1: return "weaktrack";
1062         case 2: return "normal";
1063         case 3: return "pinned";
1064         default: return "unknown";
1065         }
1066 }
1067
1068 static const char*
1069 get_root_name (int rtype)
1070 {
1071         switch (rtype & MONO_PROFILE_GC_ROOT_TYPEMASK) {
1072         case MONO_PROFILE_GC_ROOT_STACK: return "stack";
1073         case MONO_PROFILE_GC_ROOT_FINALIZER: return "finalizer";
1074         case MONO_PROFILE_GC_ROOT_HANDLE: return "handle";
1075         case MONO_PROFILE_GC_ROOT_OTHER: return "other";
1076         case MONO_PROFILE_GC_ROOT_MISC: return "misc";
1077         default: return "unknown";
1078         }
1079 }
1080
1081 static MethodDesc**
1082 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
1083 {
1084         MethodDesc **frames;
1085         int i;
1086         int flags = decode_uleb128 (p, &p);
1087         int count = decode_uleb128 (p, &p);
1088         if (flags != 0)
1089                 return NULL;
1090         if (count > *size)
1091                 frames = malloc (count * sizeof (void*));
1092         else
1093                 frames = sframes;
1094         for (i = 0; i < count; ++i) {
1095                 intptr_t ptrdiff = decode_sleb128 (p, &p);
1096                 frames [i] = lookup_method (ptr_base + ptrdiff);
1097         }
1098         *size = count;
1099         *endp = p;
1100         return frames;
1101 }
1102
1103 static void
1104 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
1105 {
1106         int i;
1107         for (i = 0; i < num_tracked_objects; ++i) {
1108                 if (tracked_objects [i] != obj)
1109                         continue;
1110                 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
1111                 if (bt && bt->count) {
1112                         int k;
1113                         for (k = 0; k < bt->count; ++k)
1114                                 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
1115                 }
1116         }
1117 }
1118
1119 static void
1120 track_handle (uintptr_t obj, int htype, uint32_t handle)
1121 {
1122         int i;
1123         for (i = 0; i < num_tracked_objects; ++i) {
1124                 if (tracked_objects [i] == obj)
1125                         fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1126         }
1127 }
1128
1129 static void
1130 track_move (uintptr_t src, uintptr_t dst)
1131 {
1132         int i;
1133         for (i = 0; i < num_tracked_objects; ++i) {
1134                 if (tracked_objects [i] == src)
1135                         fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1136                 else if (tracked_objects [i] == dst)
1137                         fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1138         }
1139 }
1140
1141 static void
1142 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1143 {
1144         int i;
1145         for (i = 0; i < num_tracked_objects; ++i) {
1146                 if (tracked_objects [i] == obj) 
1147                         fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1148         }
1149 }
1150
1151 static void
1152 found_object (uintptr_t obj)
1153 {
1154         num_tracked_objects ++;
1155         tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1156         tracked_objects [num_tracked_objects - 1] = obj;
1157 }
1158
1159 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
1160 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
1161
1162 static int
1163 decode_buffer (ProfContext *ctx)
1164 {
1165         unsigned char *p;
1166         unsigned char *end;
1167         intptr_t thread_id;
1168         intptr_t ptr_base;
1169         intptr_t obj_base;
1170         intptr_t method_base;
1171         uint64_t time_base;
1172         uint64_t file_offset;
1173         int len, i;
1174         ThreadContext *thread;
1175
1176 #ifdef HAVE_SYS_ZLIB
1177         if (ctx->gzfile)
1178                 file_offset = gztell (ctx->gzfile);
1179         else
1180 #endif
1181                 file_offset = ftell (ctx->file);
1182         if (!load_data (ctx, 48))
1183                 return 0;
1184         p = ctx->buf;
1185         if (read_int32 (p) != BUF_ID) {
1186                 fprintf (outfile, "Incorrect buffer id: 0x%x\n", read_int32 (p));
1187                 for (i = 0; i < 48; ++i) {
1188                         fprintf (outfile, "0x%x%s", p [i], i % 8?" ":"\n");
1189                 }
1190                 return 0;
1191         }
1192         len = read_int32 (p + 4);
1193         time_base = read_int64 (p + 8);
1194         ptr_base = read_int64 (p + 16);
1195         obj_base = read_int64 (p + 24);
1196         thread_id = read_int64 (p + 32);
1197         method_base = read_int64 (p + 40);
1198         if (debug)
1199                 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
1200         thread = load_thread (ctx, thread_id);
1201         if (!load_data (ctx, len))
1202                 return 0;
1203         if (!startup_time) {
1204                 startup_time = time_base;
1205                 if (time_from) {
1206                         time_from += startup_time;
1207                         time_to += startup_time;
1208                 }
1209                 if (!thread->name)
1210                         thread->name = pstrdup ("Main");
1211         }
1212         for (i = 0; i < thread->stack_id; ++i)
1213                 thread->stack [i]->recurse_count++;
1214         p = ctx->buf;
1215         end = p + len;
1216         while (p < end) {
1217                 switch (*p & 0xf) {
1218                 case TYPE_GC: {
1219                         int subtype = *p & 0xf0;
1220                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1221                         LOG_TIME (time_base, tdiff);
1222                         time_base += tdiff;
1223                         if (subtype == TYPE_GC_RESIZE) {
1224                                 uint64_t new_size = decode_uleb128 (p, &p);
1225                                 if (debug)
1226                                         fprintf (outfile, "gc heap resized to %llu\n", new_size);
1227                                 gc_resizes++;
1228                                 if (new_size > max_heap_size)
1229                                         max_heap_size = new_size;
1230                         } else if (subtype == TYPE_GC_EVENT) {
1231                                 uint64_t ev = decode_uleb128 (p, &p);
1232                                 int gen = decode_uleb128 (p, &p);
1233                                 if (debug)
1234                                         fprintf (outfile, "gc event for gen%d: %s at %llu\n", gen - 1, gc_event_name (ev), time_base);
1235                                 if (gen > 2) {
1236                                         fprintf (outfile, "incorrect gc gen: %d\n", gen);
1237                                         break;
1238                                 }
1239                                 if (ev == MONO_GC_EVENT_START) {
1240                                         gc_info [gen].start_time = time_base;
1241                                         gc_info [gen].count++;
1242                                 } else if (ev == MONO_GC_EVENT_END) {
1243                                         tdiff = time_base - gc_info [gen].start_time;
1244                                         gc_info [gen].total_time += tdiff;
1245                                         if (tdiff > gc_info [gen].max_time)
1246                                                 gc_info [gen].max_time = tdiff;
1247                                 }
1248                         } else if (subtype == TYPE_GC_MOVE) {
1249                                 int j, num = decode_uleb128 (p, &p);
1250                                 gc_object_moves += num / 2;
1251                                 for (j = 0; j < num; j += 2) {
1252                                         intptr_t obj1diff = decode_sleb128 (p, &p);
1253                                         intptr_t obj2diff = decode_sleb128 (p, &p);
1254                                         if (num_tracked_objects)
1255                                                 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
1256                                         if (debug) {
1257                                                 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
1258                                         }
1259                                 }
1260                         } else if (subtype == TYPE_GC_HANDLE_CREATED) {
1261                                 int htype = decode_uleb128 (p, &p);
1262                                 uint32_t handle = decode_uleb128 (p, &p);
1263                                 intptr_t objdiff = decode_sleb128 (p, &p);
1264                                 if (htype > 3)
1265                                         return 0;
1266                                 handle_info [htype].created++;
1267                                 add_trace_thread (thread, &handle_info [htype].traces, 1);
1268                                 /* FIXME: we don't take into account timing here */
1269                                 if (handle_info [htype].created > handle_info [htype].max_live)
1270                                         handle_info [htype].max_live = handle_info [htype].created;
1271                                 if (num_tracked_objects)
1272                                         track_handle (OBJ_ADDR (objdiff), htype, handle);
1273                                 if (debug)
1274                                         fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
1275                         } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
1276                                 int htype = decode_uleb128 (p, &p);
1277                                 uint32_t handle = decode_uleb128 (p, &p);
1278                                 if (htype > 3)
1279                                         return 0;
1280                                 handle_info [htype].created--;
1281                                 if (debug)
1282                                         fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
1283                         }
1284                         break;
1285                 }
1286                 case TYPE_METADATA: {
1287                         int error = *p & TYPE_LOAD_ERR;
1288                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1289                         int mtype = *p++;
1290                         intptr_t ptrdiff = decode_sleb128 (p, &p);
1291                         LOG_TIME (time_base, tdiff);
1292                         time_base += tdiff;
1293                         if (mtype == TYPE_CLASS) {
1294                                 intptr_t imptrdiff = decode_sleb128 (p, &p);
1295                                 uint64_t flags = decode_uleb128 (p, &p);
1296                                 if (flags) {
1297                                         fprintf (outfile, "non-zero flags in class\n");
1298                                         return 0;
1299                                 }
1300                                 if (debug)
1301                                         fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
1302                                 if (!error)
1303                                         add_class (ptr_base + ptrdiff, (char*)p);
1304                                 while (*p) p++;
1305                                 p++;
1306                         } else if (mtype == TYPE_IMAGE) {
1307                                 uint64_t flags = decode_uleb128 (p, &p);
1308                                 if (flags) {
1309                                         fprintf (outfile, "non-zero flags in image\n");
1310                                         return 0;
1311                                 }
1312                                 if (debug)
1313                                         fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
1314                                 if (!error)
1315                                         add_image (ptr_base + ptrdiff, (char*)p);
1316                                 while (*p) p++;
1317                                 p++;
1318                         } else if (mtype == TYPE_THREAD) {
1319                                 ThreadContext *nt;
1320                                 uint64_t flags = decode_uleb128 (p, &p);
1321                                 if (flags) {
1322                                         fprintf (outfile, "non-zero flags in thread\n");
1323                                         return 0;
1324                                 }
1325                                 nt = get_thread (ctx, ptr_base * ptrdiff);
1326                                 nt->name = pstrdup ((char*)p);
1327                                 if (debug)
1328                                         fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
1329                                 while (*p) p++;
1330                                 p++;
1331                         }
1332                         break;
1333                 }
1334                 case TYPE_ALLOC: {
1335                         int has_bt = *p & TYPE_ALLOC_BT;
1336                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1337                         intptr_t ptrdiff = decode_sleb128 (p, &p);
1338                         intptr_t objdiff = decode_sleb128 (p, &p);
1339                         uint64_t len;
1340                         int num_bt = 0;
1341                         MethodDesc* sframes [8];
1342                         MethodDesc** frames = sframes;
1343                         ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1344                         len = decode_uleb128 (p, &p);
1345                         LOG_TIME (time_base, tdiff);
1346                         time_base += tdiff;
1347                         if (debug)
1348                                 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
1349                         if (has_bt) {
1350                                 num_bt = 8;
1351                                 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1352                                 if (!frames) {
1353                                         fprintf (outfile, "Cannot load backtrace\n");
1354                                         return 0;
1355                                 }
1356                         }
1357                         if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
1358                                 BackTrace *bt;
1359                                 cd->allocs++;
1360                                 cd->alloc_size += len;
1361                                 if (has_bt)
1362                                         bt = add_trace_methods (frames, num_bt, &cd->traces, len);
1363                                 else
1364                                         bt = add_trace_thread (thread, &cd->traces, len);
1365                                 if (find_size && len >= find_size) {
1366                                         if (!find_name || strstr (cd->name, find_name))
1367                                                 found_object (OBJ_ADDR (objdiff));
1368                                 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
1369                                         found_object (OBJ_ADDR (objdiff));
1370                                 }
1371                                 if (num_tracked_objects)
1372                                         tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
1373                         }
1374                         if (frames != sframes)
1375                                 free (frames);
1376                         break;
1377                 }
1378                 case TYPE_METHOD: {
1379                         int subtype = *p & 0xf0;
1380                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1381                         int64_t ptrdiff = decode_sleb128 (p, &p);
1382                         LOG_TIME (time_base, tdiff);
1383                         time_base += tdiff;
1384                         method_base += ptrdiff;
1385                         if (subtype == TYPE_JIT) {
1386                                 intptr_t codediff = decode_sleb128 (p, &p);
1387                                 int codelen = decode_uleb128 (p, &p);
1388                                 if (debug)
1389                                         fprintf (outfile, "jitted method %p (%s), size: %d\n", (void*)(method_base), p, codelen);
1390                                 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
1391                                 while (*p) p++;
1392                                 p++;
1393                         } else {
1394                                 MethodDesc *method;
1395                                 if ((thread_filter && thread_filter != thread->thread_id))
1396                                         break;
1397                                 method = lookup_method (method_base);
1398                                 if (subtype == TYPE_ENTER) {
1399                                         add_trace_thread (thread, &method->traces, 1);
1400                                         push_method (thread, method, time_base);
1401                                 } else {
1402                                         pop_method (thread, method, time_base);
1403                                 }
1404                                 if (debug)
1405                                         fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
1406                         }
1407                         break;
1408                 }
1409                 case TYPE_HEAP: {
1410                         int subtype = *p & 0xf0;
1411                         if (subtype == TYPE_HEAP_OBJECT) {
1412                                 HeapObjectDesc *ho;
1413                                 int i;
1414                                 intptr_t objdiff = decode_sleb128 (p + 1, &p);
1415                                 intptr_t ptrdiff = decode_sleb128 (p, &p);
1416                                 uint64_t size = decode_uleb128 (p, &p);
1417                                 uintptr_t num = decode_uleb128 (p, &p);
1418                                 uintptr_t ref_offset;
1419                                 uintptr_t last_obj_offset = 0;
1420                                 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1421                                 if (size) {
1422                                         HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
1423                                         if (collect_traces) {
1424                                                 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
1425                                                 add_heap_shot_obj (thread->current_heap_shot, ho);
1426                                                 ref_offset = 0;
1427                                         }
1428                                 } else {
1429                                         if (collect_traces)
1430                                                 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
1431                                 }
1432                                 for (i = 0; i < num; ++i) {
1433                                         /* FIXME: use object distance to measure how good
1434                                          * the GC is at keeping related objects close
1435                                          */
1436                                         uintptr_t offset = ctx->data_version > 1? last_obj_offset + decode_uleb128 (p, &p): -1;
1437                                         intptr_t obj1diff = decode_sleb128 (p, &p);
1438                                         last_obj_offset = offset;
1439                                         if (collect_traces)
1440                                                 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
1441                                         if (num_tracked_objects)
1442                                                 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
1443                                 }
1444                                 if (debug && size)
1445                                         fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
1446                         } else if (subtype == TYPE_HEAP_ROOT) {
1447                                 uintptr_t num = decode_uleb128 (p + 1, &p);
1448                                 uintptr_t gc_num = decode_uleb128 (p, &p);
1449                                 int i;
1450                                 for (i = 0; i < num; ++i) {
1451                                         intptr_t objdiff = decode_sleb128 (p, &p);
1452                                         int root_type = decode_uleb128 (p, &p);
1453                                         /* we just discard the extra info for now */
1454                                         uintptr_t extra_info = decode_uleb128 (p, &p);
1455                                         if (debug)
1456                                                 fprintf (outfile, "object %p is a %s root\n", (void*)OBJ_ADDR (objdiff), get_root_name (root_type));
1457                                         if (collect_traces)
1458                                                 thread_add_root (thread, OBJ_ADDR (objdiff), root_type, extra_info);
1459                                 }
1460                         } else if (subtype == TYPE_HEAP_END) {
1461                                 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1462                                 LOG_TIME (time_base, tdiff);
1463                                 time_base += tdiff;
1464                                 if (debug)
1465                                         fprintf (outfile, "heap shot end\n");
1466                                 if (collect_traces) {
1467                                         HeapShot *hs = thread->current_heap_shot;
1468                                         if (hs && thread->num_roots) {
1469                                                 /* transfer the root ownershipt to the heapshot */
1470                                                 hs->num_roots = thread->num_roots;
1471                                                 hs->roots = thread->roots;
1472                                                 hs->roots_extra = thread->roots_extra;
1473                                                 hs->roots_types = thread->roots_types;
1474                                         } else {
1475                                                 free (thread->roots);
1476                                                 free (thread->roots_extra);
1477                                                 free (thread->roots_types);
1478                                         }
1479                                         thread->num_roots = 0;
1480                                         thread->size_roots = 0;
1481                                         thread->roots = NULL;
1482                                         thread->roots_extra = NULL;
1483                                         thread->roots_types = NULL;
1484                                         heap_shot_resolve_reverse_refs (hs);
1485                                         heap_shot_mark_objects (hs);
1486                                         heap_shot_free_objects (hs);
1487                                 }
1488                                 thread->current_heap_shot = NULL;
1489                         } else if (subtype == TYPE_HEAP_START) {
1490                                 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1491                                 LOG_TIME (time_base, tdiff);
1492                                 time_base += tdiff;
1493                                 if (debug)
1494                                         fprintf (outfile, "heap shot start\n");
1495                                 thread->current_heap_shot = new_heap_shot (time_base);
1496                         }
1497                         break;
1498                 }
1499                 case TYPE_MONITOR: {
1500                         int event = (*p >> 4) & 0x3;
1501                         int has_bt = *p & TYPE_MONITOR_BT;
1502                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1503                         intptr_t objdiff = decode_sleb128 (p, &p);
1504                         MethodDesc* sframes [8];
1505                         MethodDesc** frames = sframes;
1506                         int record;
1507                         int num_bt = 0;
1508                         LOG_TIME (time_base, tdiff);
1509                         time_base += tdiff;
1510                         record = (!thread_filter || thread_filter == thread->thread_id);
1511                         if (event == MONO_PROFILER_MONITOR_CONTENTION) {
1512                                 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
1513                                 if (record) {
1514                                         monitor_contention++;
1515                                         mdesc->contentions++;
1516                                         thread->monitor = mdesc;
1517                                         thread->contention_start = time_base;
1518                                 }
1519                                 if (has_bt) {
1520                                         num_bt = 8;
1521                                         frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1522                                         if (!frames) {
1523                                                 fprintf (outfile, "Cannot load backtrace\n");
1524                                                 return 0;
1525                                         }
1526                                         if (record)
1527                                                 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1528                                 } else {
1529                                         if (record)
1530                                                 add_trace_thread (thread, &mdesc->traces, 1);
1531                                 }
1532                         } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1533                                 if (record) {
1534                                         monitor_failed++;
1535                                         if (thread->monitor && thread->contention_start) {
1536                                                 uint64_t wait_time = time_base - thread->contention_start;
1537                                                 if (wait_time > thread->monitor->max_wait_time)
1538                                                         thread->monitor->max_wait_time = wait_time;
1539                                                 thread->monitor->wait_time += wait_time;
1540                                                 thread->monitor = NULL;
1541                                                 thread->contention_start = 0;
1542                                         }
1543                                 }
1544                         } else if (event == MONO_PROFILER_MONITOR_DONE) {
1545                                 if (record) {
1546                                         monitor_acquired++;
1547                                         if (thread->monitor && thread->contention_start) {
1548                                                 uint64_t wait_time = time_base - thread->contention_start;
1549                                                 if (wait_time > thread->monitor->max_wait_time)
1550                                                         thread->monitor->max_wait_time = wait_time;
1551                                                 thread->monitor->wait_time += wait_time;
1552                                                 thread->monitor = NULL;
1553                                                 thread->contention_start = 0;
1554                                         }
1555                                 }
1556                         }
1557                         if (debug)
1558                                 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1559                         if (frames != sframes)
1560                                 free (frames);
1561                         break;
1562                 }
1563                 case TYPE_EXCEPTION: {
1564                         int subtype = *p & 0x70;
1565                         int has_bt = *p & TYPE_EXCEPTION_BT;
1566                         uint64_t tdiff = decode_uleb128 (p + 1, &p);
1567                         MethodDesc* sframes [8];
1568                         MethodDesc** frames = sframes;
1569                         int record;
1570                         LOG_TIME (time_base, tdiff);
1571                         time_base += tdiff;
1572                         record = (!thread_filter || thread_filter == thread->thread_id);
1573                         if (subtype == TYPE_CLAUSE) {
1574                                 int clause_type = decode_uleb128 (p, &p);
1575                                 int clause_num = decode_uleb128 (p, &p);
1576                                 int64_t ptrdiff = decode_sleb128 (p, &p);
1577                                 method_base += ptrdiff;
1578                                 if (record)
1579                                         clause_summary [clause_type]++;
1580                                 if (debug)
1581                                         fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1582                         } else {
1583                                 intptr_t objdiff = decode_sleb128 (p, &p);
1584                                 if (record)
1585                                         throw_count++;
1586                                 if (has_bt) {
1587                                         has_bt = 8;
1588                                         frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1589                                         if (!frames) {
1590                                                 fprintf (outfile, "Cannot load backtrace\n");
1591                                                 return 0;
1592                                         }
1593                                         if (record)
1594                                                 add_trace_methods (frames, has_bt, &exc_traces, 1);
1595                                 } else {
1596                                         if (record)
1597                                                 add_trace_thread (thread, &exc_traces, 1);
1598                                 }
1599                                 if (frames != sframes)
1600                                         free (frames);
1601                                 if (debug)
1602                                         fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1603                         }
1604                         break;
1605                 }
1606                 default:
1607                         fprintf (outfile, "unhandled profiler event: 0x%x at file offset: %llu + %d (len: %d\n)\n", *p, file_offset, p - ctx->buf, len);
1608                         exit (1);
1609                 }
1610         }
1611         thread->last_time = time_base;
1612         for (i = 0; i < thread->stack_id; ++i)
1613                 thread->stack [i]->recurse_count = 0;
1614         return 1;
1615 }
1616
1617 static ProfContext*
1618 load_file (char *name)
1619 {
1620         unsigned char *p;
1621         ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1622         if (strcmp (name, "-") == 0)
1623                 ctx->file = stdin;
1624         else
1625                 ctx->file = fopen (name, "rb");
1626         if (!ctx->file) {
1627                 printf ("Cannot open file: %s\n", name);
1628                 exit (1);
1629         }
1630 #if defined (HAVE_SYS_ZLIB)
1631         if (ctx->file != stdin)
1632                 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
1633 #endif
1634         if (!load_data (ctx, 32))
1635                 return NULL;
1636         p = ctx->buf;
1637         if (read_int32 (p) != LOG_HEADER_ID || p [6] > LOG_DATA_VERSION)
1638                 return NULL;
1639         ctx->version_major = p [4];
1640         ctx->version_minor = p [5];
1641         ctx->data_version = p [6];
1642         /* reading 64 bit files on 32 bit systems not supported yet */
1643         if (p [7] > sizeof (void*))
1644                 return NULL;
1645         if (read_int32 (p + 20)) /* flags must be 0 */
1646                 return NULL;
1647         ctx->startup_time = read_int64 (p + 8);
1648         ctx->timer_overhead = read_int32 (p + 16);
1649         ctx->pid = read_int32 (p + 24);
1650         return ctx;
1651 }
1652
1653 enum {
1654         ALLOC_SORT_BYTES,
1655         ALLOC_SORT_COUNT
1656 };
1657 static int alloc_sort_mode = ALLOC_SORT_BYTES;
1658
1659 static int
1660 compare_class (const void *a, const void *b)
1661 {
1662         ClassDesc *const*A = a;
1663         ClassDesc *const*B = b;
1664         uint64_t vala, valb;
1665         if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1666                 vala = (*A)->alloc_size;
1667                 valb = (*B)->alloc_size;
1668         } else {
1669                 vala = (*A)->allocs;
1670                 valb = (*B)->allocs;
1671         }
1672         if (valb == vala)
1673                 return 0;
1674         if (valb < vala)
1675                 return -1;
1676         return 1;
1677 }
1678
1679 static void
1680 dump_header (ProfContext *ctx)
1681 {
1682         time_t st = ctx->startup_time / 1000;
1683         char *t = ctime (&st);
1684         fprintf (outfile, "\nMono log profiler data\n");
1685         fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
1686         fprintf (outfile, "\tData version: %d\n", ctx->data_version);
1687         fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
1688         fprintf (outfile, "\tProgram startup: %s", t);
1689         if (ctx->pid)
1690                 fprintf (outfile, "\tProgram ID: %d\n", ctx->pid);
1691 }
1692
1693 static void
1694 dump_traces (TraceDesc *traces, const char *desc)
1695 {
1696         int j;
1697         if (!show_traces)
1698                 return;
1699         if (!traces->count)
1700                 return;
1701         sort_context_array (traces);
1702         for (j = 0; j < traces->count; ++j) {
1703                 int k;
1704                 BackTrace *bt;
1705                 bt = traces->traces [j].bt;
1706                 if (!bt->count)
1707                         continue;
1708                 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
1709                 for (k = 0; k < bt->count; ++k)
1710                         fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
1711         }
1712 }
1713
1714 static void
1715 dump_threads (ProfContext *ctx)
1716 {
1717         ThreadContext *thread;
1718         fprintf (outfile, "\nThread summary\n");
1719         for (thread = ctx->threads; thread; thread = thread->next) {
1720                 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
1721         }
1722 }
1723
1724 static void
1725 dump_exceptions (void)
1726 {
1727         int i;
1728         fprintf (outfile, "\nException summary\n");
1729         fprintf (outfile, "\tThrows: %llu\n", throw_count);
1730         dump_traces (&exc_traces, "throws");
1731         for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
1732                 if (!clause_summary [i])
1733                         continue;
1734                 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
1735         }
1736 }
1737
1738 static int
1739 compare_monitor (const void *a, const void *b)
1740 {
1741         MonitorDesc *const*A = a;
1742         MonitorDesc *const*B = b;
1743         if ((*B)->wait_time == (*A)->wait_time)
1744                 return 0;
1745         if ((*B)->wait_time < (*A)->wait_time)
1746                 return -1;
1747         return 1;
1748 }
1749
1750 static void
1751 dump_monitors (void)
1752 {
1753         MonitorDesc **monitors;
1754         int i, j;
1755         if (!num_monitors)
1756                 return;
1757         monitors = malloc (sizeof (void*) * num_monitors);
1758         for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
1759                 MonitorDesc *mdesc = monitor_hash [i];
1760                 while (mdesc) {
1761                         monitors [j++] = mdesc;
1762                         mdesc = mdesc->next;
1763                 }
1764         }
1765         qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
1766         fprintf (outfile, "\nMonitor lock summary\n");
1767         for (i = 0; i < num_monitors; ++i) {
1768                 MonitorDesc *mdesc = monitors [i];
1769                 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
1770                 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
1771                         mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
1772                 dump_traces (&mdesc->traces, "contentions");
1773         }
1774         fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
1775         fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
1776         fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
1777 }
1778
1779 static void
1780 dump_gcs (void)
1781 {
1782         int i;
1783         fprintf (outfile, "\nGC summary\n");
1784         fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
1785         fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
1786         fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
1787         for (i = 0; i < 3; ++i) {
1788                 if (!gc_info [i].count)
1789                         continue;
1790                 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
1791                         i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
1792                         gc_info [i].total_time / gc_info [i].count / 1000);
1793         }
1794         for (i = 0; i < 3; ++i) {
1795                 if (!handle_info [i].max_live)
1796                         continue;
1797                 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
1798                         get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
1799                 dump_traces (&handle_info [i].traces, "created");
1800         }
1801 }
1802
1803 static void
1804 dump_allocations (void)
1805 {
1806         int i, c;
1807         intptr_t allocs = 0;
1808         uint64_t size = 0;
1809         int header_done = 0;
1810         ClassDesc **classes = malloc (num_classes * sizeof (void*));
1811         ClassDesc *cd;
1812         c = 0;
1813         for (i = 0; i < HASH_SIZE; ++i) {
1814                 cd = class_hash [i];
1815                 while (cd) {
1816                         classes [c++] = cd;
1817                         cd = cd->next;
1818                 }
1819         }
1820         qsort (classes, num_classes, sizeof (void*), compare_class);
1821         for (i = 0; i < num_classes; ++i) {
1822                 cd = classes [i];
1823                 if (!cd->allocs)
1824                         continue;
1825                 allocs += cd->allocs;
1826                 size += cd->alloc_size;
1827                 if (!header_done++) {
1828                         fprintf (outfile, "\nAllocation summary\n");
1829                         fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
1830                 }
1831                 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
1832                 dump_traces (&cd->traces, "bytes");
1833         }
1834         if (allocs)
1835                 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
1836 }
1837
1838 enum {
1839         METHOD_SORT_TOTAL,
1840         METHOD_SORT_SELF,
1841         METHOD_SORT_CALLS
1842 };
1843
1844 static int method_sort_mode = METHOD_SORT_TOTAL;
1845
1846 static int
1847 compare_method (const void *a, const void *b)
1848 {
1849         MethodDesc *const*A = a;
1850         MethodDesc *const*B = b;
1851         uint64_t vala, valb;
1852         if (method_sort_mode == METHOD_SORT_SELF) {
1853                 vala = (*A)->self_time;
1854                 valb = (*B)->self_time;
1855         } else if (method_sort_mode == METHOD_SORT_CALLS) {
1856                 vala = (*A)->calls;
1857                 valb = (*B)->calls;
1858         } else {
1859                 vala = (*A)->total_time;
1860                 valb = (*B)->total_time;
1861         }
1862         if (vala == valb)
1863                 return 0;
1864         if (valb < vala)
1865                 return -1;
1866         return 1;
1867 }
1868
1869 static void
1870 dump_metadata (void)
1871 {
1872         fprintf (outfile, "\nMetadata summary\n");
1873         fprintf (outfile, "\tLoaded images: %d\n", num_images);
1874         if (verbose) {
1875                 ImageDesc *image;
1876                 int i;
1877                 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
1878                         image = image_hash [i];
1879                         while (image) {
1880                                 fprintf (outfile, "\t\t%s\n", image->filename);
1881                                 image = image->next;
1882                         }
1883                 }
1884         }
1885
1886 }
1887
1888 static void
1889 dump_methods (void)
1890 {
1891         int i, c;
1892         uint64_t calls = 0;
1893         int header_done = 0;
1894         MethodDesc **methods = malloc (num_methods * sizeof (void*));
1895         MethodDesc *cd;
1896         c = 0;
1897         for (i = 0; i < HASH_SIZE; ++i) {
1898                 cd = method_hash [i];
1899                 while (cd) {
1900                         cd->total_time = cd->self_time + cd->callee_time;
1901                         methods [c++] = cd;
1902                         cd = cd->next;
1903                 }
1904         }
1905         qsort (methods, num_methods, sizeof (void*), compare_method);
1906         for (i = 0; i < num_methods; ++i) {
1907                 uint64_t msecs;
1908                 uint64_t smsecs;
1909                 cd = methods [i];
1910                 if (!cd->calls)
1911                         continue;
1912                 calls += cd->calls;
1913                 msecs = cd->total_time / 1000000;
1914                 smsecs = (cd->total_time - cd->callee_time) / 1000000;
1915                 if (!msecs && !verbose)
1916                         continue;
1917                 if (!header_done++) {
1918                         fprintf (outfile, "\nMethod call summary\n");
1919                         fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
1920                 }
1921                 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
1922                 dump_traces (&cd->traces, "calls");
1923         }
1924         if (calls)
1925                 fprintf (outfile, "Total calls: %llu\n", calls);
1926 }
1927
1928 static int
1929 compare_heap_class (const void *a, const void *b)
1930 {
1931         HeapClassDesc *const*A = a;
1932         HeapClassDesc *const*B = b;
1933         uint64_t vala, valb;
1934         if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1935                 vala = (*A)->total_size;
1936                 valb = (*B)->total_size;
1937         } else {
1938                 vala = (*A)->count;
1939                 valb = (*B)->count;
1940         }
1941         if (valb == vala)
1942                 return 0;
1943         if (valb < vala)
1944                 return -1;
1945         return 1;
1946 }
1947
1948 static int
1949 compare_rev_class (const void *a, const void *b)
1950 {
1951         const HeapClassRevRef *A = a;
1952         const HeapClassRevRef *B = b;
1953         if (B->count == A->count)
1954                 return 0;
1955         if (B->count < A->count)
1956                 return -1;
1957         return 1;
1958 }
1959
1960 static void
1961 dump_rev_claases (HeapClassRevRef *revs, int count)
1962 {
1963         int j;
1964         if (!show_traces)
1965                 return;
1966         if (!count)
1967                 return;
1968         for (j = 0; j < count; ++j) {
1969                 HeapClassDesc *cd = revs [j].klass;
1970                 fprintf (outfile, "\t\t%llu references from: %s\n", revs [j].count, cd->klass->name);
1971         }
1972 }
1973
1974 static void
1975 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
1976 {
1977         uint64_t size = 0;
1978         uint64_t count = 0;
1979         int ccount = 0;
1980         int i;
1981         HeapClassDesc *cd;
1982         HeapClassDesc **sorted;
1983         sorted = malloc (sizeof (void*) * hs->class_count);
1984         for (i = 0; i < hs->hash_size; ++i) {
1985                 cd = hs->class_hash [i];
1986                 if (!cd)
1987                         continue;
1988                 count += cd->count;
1989                 size += cd->total_size;
1990                 sorted [ccount++] = cd;
1991         }
1992         hs->sorted = sorted;
1993         qsort (sorted, ccount, sizeof (void*), compare_heap_class);
1994         fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d, roots: %d\n",
1995                 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount, hs->num_roots);
1996         if (!verbose && ccount > 30)
1997                 ccount = 30;
1998         fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
1999         for (i = 0; i < ccount; ++i) {
2000                 HeapClassRevRef *rev_sorted;
2001                 int j, k;
2002                 HeapClassDesc *ocd = NULL;
2003                 cd = sorted [i];
2004                 if (last_hs)
2005                         ocd = heap_class_lookup (last_hs, cd->klass);
2006                 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
2007                 if (ocd) {
2008                         int64_t bdiff = cd->total_size - ocd->total_size;
2009                         int64_t cdiff = cd->count - ocd->count;
2010                         fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
2011                 } else {
2012                         fprintf (outfile, "\n");
2013                 }
2014                 if (!collect_traces)
2015                         continue;
2016                 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
2017                 k = 0;
2018                 for (j = 0; j < cd->rev_hash_size; ++j) {
2019                         if (cd->rev_hash [j].klass)
2020                                 rev_sorted [k++] = cd->rev_hash [j];
2021                 }
2022                 assert (cd->rev_count == k);
2023                 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
2024                 if (cd->root_references)
2025                         fprintf (outfile, "\t\t%d root references (%d pinning)\n", cd->root_references, cd->pinned_references);
2026                 dump_rev_claases (rev_sorted, cd->rev_count);
2027                 free (rev_sorted);
2028         }
2029         free (sorted);
2030 }
2031
2032 static int
2033 compare_heap_shots (const void *a, const void *b)
2034 {
2035         HeapShot *const*A = a;
2036         HeapShot *const*B = b;
2037         if ((*B)->timestamp == (*A)->timestamp)
2038                 return 0;
2039         if ((*B)->timestamp > (*A)->timestamp)
2040                 return -1;
2041         return 1;
2042 }
2043
2044 static void
2045 dump_heap_shots (void)
2046 {
2047         HeapShot **hs_sorted;
2048         HeapShot *hs;
2049         HeapShot *last_hs = NULL;
2050         int i;
2051         if (!heap_shots)
2052                 return;
2053         hs_sorted = malloc (num_heap_shots * sizeof (void*));
2054         fprintf (outfile, "\nHeap shot summary\n");
2055         i = 0;
2056         for (hs = heap_shots; hs; hs = hs->next)
2057                 hs_sorted [i++] = hs;
2058         qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
2059         for (i = 0; i < num_heap_shots; ++i) {
2060                 hs = hs_sorted [i];
2061                 heap_shot_summary (hs, i, last_hs);
2062                 last_hs = hs;
2063         }
2064 }
2065
2066 static void
2067 flush_context (ProfContext *ctx)
2068 {
2069         ThreadContext *thread;
2070         /* FIXME: sometimes there are leftovers: indagate */
2071         for (thread = ctx->threads; thread; thread = thread->next) {
2072                 while (thread->stack_id) {
2073                         if (debug)
2074                                 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
2075                         pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
2076                 }
2077         }
2078 }
2079
2080 static const char *reports = "header,gc,alloc,call,metadata,exception,monitor,thread,heapshot";
2081
2082 static const char*
2083 match_option (const char *p, const char *opt)
2084 {
2085         int len = strlen (opt);
2086         if (strncmp (p, opt, len) == 0) {
2087                 if (p [len] == ',')
2088                         len++;
2089                 return p + len;
2090         }
2091         return p;
2092 }
2093
2094 static int
2095 print_reports (ProfContext *ctx, const char *reps, int parse_only)
2096 {
2097         const char *opt;
2098         const char *p;
2099         for (p = reps; *p; p = opt) {
2100                 if ((opt = match_option (p, "header")) != p) {
2101                         if (!parse_only)
2102                                 dump_header (ctx);
2103                         continue;
2104                 }
2105                 if ((opt = match_option (p, "thread")) != p) {
2106                         if (!parse_only)
2107                                 dump_threads (ctx);
2108                         continue;
2109                 }
2110                 if ((opt = match_option (p, "gc")) != p) {
2111                         if (!parse_only)
2112                                 dump_gcs ();
2113                         continue;
2114                 }
2115                 if ((opt = match_option (p, "alloc")) != p) {
2116                         if (!parse_only)
2117                                 dump_allocations ();
2118                         continue;
2119                 }
2120                 if ((opt = match_option (p, "call")) != p) {
2121                         if (!parse_only)
2122                                 dump_methods ();
2123                         continue;
2124                 }
2125                 if ((opt = match_option (p, "metadata")) != p) {
2126                         if (!parse_only)
2127                                 dump_metadata ();
2128                         continue;
2129                 }
2130                 if ((opt = match_option (p, "exception")) != p) {
2131                         if (!parse_only)
2132                                 dump_exceptions ();
2133                         continue;
2134                 }
2135                 if ((opt = match_option (p, "monitor")) != p) {
2136                         if (!parse_only)
2137                                 dump_monitors ();
2138                         continue;
2139                 }
2140                 if ((opt = match_option (p, "heapshot")) != p) {
2141                         if (!parse_only)
2142                                 dump_heap_shots ();
2143                         continue;
2144                 }
2145                 return 0;
2146         }
2147         return 1;
2148 }
2149
2150 static int
2151 add_find_spec (const char *p)
2152 {
2153         if (p [0] == 'S' && p [1] == ':') {
2154                 char *vale;
2155                 find_size = strtoul (p + 2, &vale, 10);
2156                 return 1;
2157         } else if (p [0] == 'T' && p [1] == ':') {
2158                 find_name = p + 2;
2159                 return 1;
2160         }
2161         return 0;
2162 }
2163
2164 static void
2165 usage (void)
2166 {
2167         printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
2168         printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
2169         printf ("FILENAME can be '-' to read from standard input.\n");
2170         printf ("Options:\n");
2171         printf ("\t--help               display this help\n");
2172         printf ("\t--out=FILE           write to FILE instead of stdout\n");
2173         printf ("\t--traces             collect and show backtraces\n"); 
2174         printf ("\t--maxframes=NUM      limit backtraces to NUM entries\n");
2175         printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
2176         printf ("\t                     %s\n", reports);
2177         printf ("\t--method-sort=MODE   sort methods according to MODE: total, self, calls\n");
2178         printf ("\t--alloc-sort=MODE    sort allocations according to MODE: bytes, count\n");
2179         printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
2180         printf ("\t--find=FINDSPEC      find and track objects matching FINFSPEC, where FINDSPEC is:\n");
2181         printf ("\t                     S:minimum_size or T:partial_name\n");
2182         printf ("\t--thread=THREADID    consider just the data for thread THREADID\n");
2183         printf ("\t--time=FROM-TO       consider data FROM seconds from startup up to TO seconds\n");
2184         printf ("\t--verbose            increase verbosity level\n");
2185         printf ("\t--debug              display decoding debug info for mprof-report devs\n");
2186 }
2187
2188 int
2189 main (int argc, char *argv[])
2190 {
2191         ProfContext *ctx;
2192         int i;
2193         outfile = stdout;
2194         for (i = 1; i < argc; ++i) {
2195                 if (strcmp ("--debug", argv [i]) == 0) {
2196                         debug++;
2197                 } else if (strcmp ("--help", argv [i]) == 0) {
2198                         usage ();
2199                         return 0;
2200                 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
2201                         const char *val = argv [i] + 13;
2202                         if (strcmp (val, "bytes") == 0) {
2203                                 alloc_sort_mode = ALLOC_SORT_BYTES;
2204                         } else if (strcmp (val, "count") == 0) {
2205                                 alloc_sort_mode = ALLOC_SORT_COUNT;
2206                         } else {
2207                                 usage ();
2208                                 return 1;
2209                         }
2210                 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
2211                         const char *val = argv [i] + 14;
2212                         if (strcmp (val, "total") == 0) {
2213                                 method_sort_mode = METHOD_SORT_TOTAL;
2214                         } else if (strcmp (val, "self") == 0) {
2215                                 method_sort_mode = METHOD_SORT_SELF;
2216                         } else if (strcmp (val, "calls") == 0) {
2217                                 method_sort_mode = METHOD_SORT_CALLS;
2218                         } else {
2219                                 usage ();
2220                                 return 1;
2221                         }
2222                 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
2223                         const char *val = argv [i] + 10;
2224                         if (!print_reports (NULL, val, 1)) {
2225                                 usage ();
2226                                 return 1;
2227                         }
2228                         reports = val;
2229                 } else if (strncmp ("--out=", argv [i], 6) == 0) {
2230                         const char *val = argv [i] + 6;
2231                         outfile = fopen (val, "w");
2232                         if (!outfile) {
2233                                 printf ("Cannot open output file: %s\n", val);
2234                                 return 1;
2235                         }
2236                 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
2237                         const char *val = argv [i] + 12;
2238                         char *vale;
2239                         trace_max = strtoul (val, &vale, 10);
2240                 } else if (strncmp ("--find=", argv [i], 7) == 0) {
2241                         const char *val = argv [i] + 7;
2242                         if (!add_find_spec (val)) {
2243                                 usage ();
2244                                 return 1;
2245                         }
2246                 } else if (strncmp ("--track=", argv [i], 8) == 0) {
2247                         const char *val = argv [i] + 8;
2248                         char *vale;
2249                         while (*val) {
2250                                 uintptr_t tracked_obj;
2251                                 if (*val == ',') {
2252                                         val++;
2253                                         continue;
2254                                 }
2255                                 tracked_obj = strtoul (val, &vale, 0);
2256                                 found_object (tracked_obj);
2257                                 val = vale;
2258                         }
2259                 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
2260                         const char *val = argv [i] + 9;
2261                         char *vale;
2262                         thread_filter = strtoul (val, &vale, 0);
2263                 } else if (strncmp ("--time=", argv [i], 7) == 0) {
2264                         char *val = pstrdup (argv [i] + 7);
2265                         double from_secs, to_secs;
2266                         char *top = strchr (val, '-');
2267                         if (!top) {
2268                                 usage ();
2269                                 return 1;
2270                         }
2271                         *top++ = 0;
2272                         from_secs = atof (val);
2273                         to_secs = atof (top);
2274                         free (val);
2275                         if (from_secs > to_secs) {
2276                                 usage ();
2277                                 return 1;
2278                         }
2279                         time_from = from_secs * 1000000000;
2280                         time_to = to_secs * 1000000000;
2281                 } else if (strcmp ("--verbose", argv [i]) == 0) {
2282                         verbose++;
2283                 } else if (strcmp ("--traces", argv [i]) == 0) {
2284                         show_traces = 1;
2285                         collect_traces = 1;
2286                 } else {
2287                         break;
2288                 }
2289         }
2290         if (i >= argc) {
2291                 usage ();
2292                 return 2;
2293         }
2294         ctx = load_file (argv [i]);
2295         if (!ctx) {
2296                 printf ("Not a log profiler data file (or unsupported version).\n");
2297                 return 1;
2298         }
2299         while (decode_buffer (ctx));
2300         flush_context (ctx);
2301         if (num_tracked_objects)
2302                 return 0;
2303         print_reports (ctx, reports, 0);
2304         return 0;
2305 }
2306