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