2 * decode.c: mprof-report program source: decode and analyze the log profiler data
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2010 Novell, Inc (http://www.novell.com)
15 #if !defined(__APPLE__) && !defined(__FreeBSD__)
20 #if defined (HAVE_SYS_ZLIB)
23 #include <mono/metadata/profiler.h>
24 #include <mono/metadata/object.h>
25 #include <mono/metadata/debug-helpers.h>
27 #define HASH_SIZE 9371
28 #define SMALL_HASH_SIZE 31
30 #if defined(__native_client__) || defined(__native_client_codegen__)
31 volatile int __nacl_thread_suspension_needed = 0;
32 void __nacl_suspend_thread_if_needed() {}
36 static int collect_traces = 0;
37 static int show_traces = 0;
38 static int trace_max = 6;
39 static int verbose = 0;
40 static uintptr_t *tracked_objects = 0;
41 static int num_tracked_objects = 0;
42 static uintptr_t thread_filter = 0;
43 static uint64_t find_size = 0;
44 static const char* find_name = NULL;
45 static uint64_t time_from = 0;
46 static uint64_t time_to = 0xffffffffffffffffULL;
47 static uint64_t startup_time = 0;
48 static FILE* outfile = NULL;
51 read_int16 (unsigned char *p)
59 read_int32 (unsigned char *p)
63 value |= (*p++) << 16;
64 value |= (uint32_t)(*p++) << 24;
69 read_int64 (unsigned char *p)
71 uint64_t value = *p++;
73 value |= (*p++) << 16;
74 value |= (uint64_t)(*p++) << 24;
75 value |= (uint64_t)(*p++) << 32;
76 value |= (uint64_t)(*p++) << 40;
77 value |= (uint64_t)(*p++) << 48;
78 value |= (uint64_t)(*p++) << 54;
83 pstrdup (const char *s)
85 int len = strlen (s) + 1;
86 char *p = malloc (len);
91 static int num_images;
92 typedef struct _ImageDesc ImageDesc;
99 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
102 add_image (intptr_t image, char *name)
104 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
105 ImageDesc *cd = malloc (sizeof (ImageDesc));
107 cd->filename = pstrdup (name);
108 cd->next = image_hash [slot];
109 image_hash [slot] = cd;
113 typedef struct _BackTrace BackTrace;
125 typedef struct _ClassDesc ClassDesc;
135 static ClassDesc* class_hash [HASH_SIZE] = {0};
136 static int num_classes = 0;
139 add_class (intptr_t klass, const char *name)
141 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
143 cd = class_hash [slot];
144 while (cd && cd->klass != klass)
146 /* we resolved an unknown class (unless we had the code unloaded) */
148 /*printf ("resolved unknown: %s\n", name);*/
150 cd->name = pstrdup (name);
153 cd = calloc (sizeof (ClassDesc), 1);
155 cd->name = pstrdup (name);
156 cd->next = class_hash [slot];
159 cd->traces.count = 0;
161 cd->traces.traces = NULL;
162 class_hash [slot] = cd;
168 lookup_class (intptr_t klass)
170 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
171 ClassDesc *cd = class_hash [slot];
172 while (cd && cd->klass != klass)
176 snprintf (buf, sizeof (buf), "unresolved class %p", (void*)klass);
177 return add_class (klass, buf);
182 typedef struct _MethodDesc MethodDesc;
193 uint64_t callee_time;
198 static MethodDesc* method_hash [HASH_SIZE] = {0};
199 static int num_methods = 0;
202 add_method (intptr_t method, const char *name, intptr_t code, int len)
204 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
206 cd = method_hash [slot];
207 while (cd && cd->method != method)
209 /* we resolved an unknown method (unless we had the code unloaded) */
213 /*printf ("resolved unknown: %s\n", name);*/
215 cd->name = pstrdup (name);
218 cd = calloc (sizeof (MethodDesc), 1);
220 cd->name = pstrdup (name);
225 cd->traces.count = 0;
227 cd->traces.traces = NULL;
228 cd->next = method_hash [slot];
229 method_hash [slot] = cd;
235 lookup_method (intptr_t method)
237 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
238 MethodDesc *cd = method_hash [slot];
239 while (cd && cd->method != method)
243 snprintf (buf, sizeof (buf), "unknown method %p", (void*)method);
244 return add_method (method, buf, 0, 0);
249 static int num_stat_samples = 0;
250 static int size_stat_samples = 0;
251 uintptr_t *stat_samples = NULL;
252 int *stat_sample_desc = NULL;
255 add_stat_sample (int type, uintptr_t ip) {
256 if (num_stat_samples == size_stat_samples) {
257 size_stat_samples *= 2;
258 if (!size_stat_samples)
259 size_stat_samples = 32;
260 stat_samples = realloc (stat_samples, size_stat_samples * sizeof (uintptr_t));
261 stat_sample_desc = realloc (stat_sample_desc, size_stat_samples * sizeof (int));
263 stat_samples [num_stat_samples] = ip;
264 stat_sample_desc [num_stat_samples++] = type;
268 lookup_method_by_ip (uintptr_t ip)
273 for (i = 0; i < HASH_SIZE; ++i) {
276 //printf ("checking %p against %p-%p\n", (void*)ip, (void*)(m->code), (void*)(m->code + m->len));
277 if (ip >= (uintptr_t)m->code && ip < (uintptr_t)m->code + m->len) {
287 compare_method_samples (const void *a, const void *b)
289 MethodDesc *const*A = a;
290 MethodDesc *const*B = b;
291 if ((*A)->sample_hits == (*B)->sample_hits)
293 if ((*B)->sample_hits < (*A)->sample_hits)
298 typedef struct _UnmanagedSymbol UnmanagedSymbol;
299 struct _UnmanagedSymbol {
300 UnmanagedSymbol *parent;
305 uintptr_t sample_hits;
308 static UnmanagedSymbol **usymbols = NULL;
309 static int usymbols_size = 0;
310 static int usymbols_num = 0;
313 compare_usymbol_addr (const void *a, const void *b)
315 UnmanagedSymbol *const*A = a;
316 UnmanagedSymbol *const*B = b;
317 if ((*B)->addr == (*A)->addr)
319 if ((*B)->addr > (*A)->addr)
325 compare_usymbol_samples (const void *a, const void *b)
327 UnmanagedSymbol *const*A = a;
328 UnmanagedSymbol *const*B = b;
329 if ((*B)->sample_hits == (*A)->sample_hits)
331 if ((*B)->sample_hits < (*A)->sample_hits)
337 add_unmanaged_symbol (uintptr_t addr, char *name, uintptr_t size)
339 UnmanagedSymbol *sym;
340 if (usymbols_num == usymbols_size) {
341 int new_size = usymbols_size * 2;
344 usymbols = realloc (usymbols, sizeof (void*) * new_size);
345 usymbols_size = new_size;
347 sym = calloc (sizeof (UnmanagedSymbol), 1);
351 usymbols [usymbols_num++] = sym;
354 /* only valid after the symbols are sorted */
355 static UnmanagedSymbol*
356 lookup_unmanaged_symbol (uintptr_t addr)
358 int r = usymbols_num - 1;
360 UnmanagedSymbol *sym;
365 if (addr == sym->addr)
367 if (addr < sym->addr) {
369 } else if (addr > sym->addr) {
374 if (last_best >= 0 && (addr - usymbols [last_best]->addr) < 4096)
375 return usymbols [last_best];
379 /* we use the same structure for binaries */
380 static UnmanagedSymbol **ubinaries = NULL;
381 static int ubinaries_size = 0;
382 static int ubinaries_num = 0;
385 add_unmanaged_binary (uintptr_t addr, char *name, uintptr_t size)
387 UnmanagedSymbol *sym;
388 if (ubinaries_num == ubinaries_size) {
389 int new_size = ubinaries_size * 2;
392 ubinaries = realloc (ubinaries, sizeof (void*) * new_size);
393 ubinaries_size = new_size;
395 sym = calloc (sizeof (UnmanagedSymbol), 1);
400 ubinaries [ubinaries_num++] = sym;
403 static UnmanagedSymbol*
404 lookup_unmanaged_binary (uintptr_t addr)
407 for (i = 0; i < ubinaries_num; ++i) {
408 UnmanagedSymbol *ubin = ubinaries [i];
409 if (addr >= ubin->addr && addr < ubin->addr + ubin->size) {
417 sample_type_name (int type)
420 case SAMPLE_CYCLES: return "cycles";
421 case SAMPLE_INSTRUCTIONS: return "instructions retired";
422 case SAMPLE_CACHE_MISSES: return "cache misses";
423 case SAMPLE_CACHE_REFS: return "cache references";
424 case SAMPLE_BRANCHES: return "executed branches";
425 case SAMPLE_BRANCH_MISSES: return "unpredicted branches";
431 set_usym_parent (UnmanagedSymbol** cachedus, int count)
434 for (i = 0; i < count; ++i) {
435 UnmanagedSymbol *ubin = lookup_unmanaged_binary (cachedus [i]->addr);
436 if (ubin == cachedus [i])
438 cachedus [i]->parent = ubin;
443 print_usym (UnmanagedSymbol* um)
446 fprintf (outfile, "\t%6d %6.2f %-36s in %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name, um->parent->name);
448 fprintf (outfile, "\t%6d %6.2f %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name);
452 sym_percent (uintptr_t sample_hits)
457 pc = sample_hits*100.0/num_stat_samples;
465 int count = 0, msize = 0;
466 int unmanaged_hits = 0;
467 int unresolved_hits = 0;
468 MethodDesc** cachedm = NULL;
469 int ucount = 0, usize = 0;
470 UnmanagedSymbol** cachedus = NULL;
471 if (!num_stat_samples)
473 qsort (usymbols, usymbols_num, sizeof (UnmanagedSymbol*), compare_usymbol_addr);
474 for (i = 0; i < num_stat_samples; ++i) {
475 MethodDesc *m = lookup_method_by_ip (stat_samples [i]);
477 if (!m->sample_hits) {
478 if (count == msize) {
482 cachedm = realloc (cachedm, sizeof (void*) * msize);
484 cachedm [count++] = m;
488 UnmanagedSymbol *usym = lookup_unmanaged_symbol (stat_samples [i]);
491 //printf ("unmanaged hit at %p\n", (void*)stat_samples [i]);
492 usym = lookup_unmanaged_binary (stat_samples [i]);
495 if (!usym->sample_hits) {
496 if (ucount == usize) {
500 cachedus = realloc (cachedus, sizeof (void*) * usize);
502 cachedus [ucount++] = usym;
509 qsort (cachedm, count, sizeof (MethodDesc*), compare_method_samples);
510 qsort (cachedus, ucount, sizeof (UnmanagedSymbol*), compare_usymbol_samples);
511 set_usym_parent (cachedus, ucount);
512 fprintf (outfile, "\nStatistical samples summary\n");
513 fprintf (outfile, "\tSample type: %s\n", sample_type_name (stat_sample_desc [0]));
514 fprintf (outfile, "\tUnmanaged hits: %6d (%4.1f%%)\n", unmanaged_hits, (100.0*unmanaged_hits)/num_stat_samples);
515 fprintf (outfile, "\tManaged hits: %6d (%4.1f%%)\n", num_stat_samples - unmanaged_hits, (100.0*(num_stat_samples-unmanaged_hits))/num_stat_samples);
516 fprintf (outfile, "\tUnresolved hits: %6d (%4.1f%%)\n", unresolved_hits, (100.0*unresolved_hits)/num_stat_samples);
517 fprintf (outfile, "\t%6s %6s %s\n", "Hits", "%", "Method name");
520 while (i < count || u < ucount) {
522 MethodDesc *m = cachedm [i];
524 UnmanagedSymbol *um = cachedus [u];
525 if (um->sample_hits > m->sample_hits) {
526 if (!sym_percent (um->sample_hits))
533 if (!sym_percent (m->sample_hits))
535 fprintf (outfile, "\t%6d %6.2f %s\n", m->sample_hits, m->sample_hits*100.0/num_stat_samples, m->name);
540 UnmanagedSymbol *um = cachedus [u];
541 if (!sym_percent (um->sample_hits))
550 typedef struct _HeapClassDesc HeapClassDesc;
552 HeapClassDesc *klass;
556 struct _HeapClassDesc {
560 HeapClassRevRef *rev_hash;
563 uintptr_t pinned_references;
564 uintptr_t root_references;
568 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
572 start_pos = (hklass->klass->klass >> 2) % size;
573 assert (start_pos < size);
576 if (rev_hash [i].klass == hklass) {
577 rev_hash [i].count += value;
579 } else if (!rev_hash [i].klass) {
580 rev_hash [i].klass = hklass;
581 rev_hash [i].count += value;
583 for (i = 0; i < size; ++i)
584 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
586 assert (start_pos == 1);
592 } while (i != start_pos);
593 /* should not happen */
594 printf ("failed revref store\n");
599 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
602 if (to->rev_count * 2 >= to->rev_hash_size) {
604 uintptr_t old_size = to->rev_hash_size;
605 to->rev_hash_size *= 2;
606 if (to->rev_hash_size == 0)
607 to->rev_hash_size = 4;
608 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
609 for (i = 0; i < old_size; ++i) {
610 if (to->rev_hash [i].klass)
611 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
617 to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
622 HeapClassDesc *hklass;
627 typedef struct _HeapShot HeapShot;
633 HeapClassDesc **class_hash;
634 HeapClassDesc **sorted;
635 HeapObjectDesc **objects_hash;
636 uintptr_t objects_count;
637 uintptr_t objects_hash_size;
640 uintptr_t *roots_extra;
644 static HeapShot *heap_shots = NULL;
645 static int num_heap_shots = 0;
648 new_heap_shot (uint64_t timestamp)
650 HeapShot *hs = calloc (sizeof (HeapShot), 1);
652 hs->class_hash = calloc (sizeof (void*), hs->hash_size);
653 hs->timestamp = timestamp;
655 hs->next = heap_shots;
660 static HeapClassDesc*
661 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
664 unsigned int start_pos;
665 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
668 HeapClassDesc* cd = hs->class_hash [i];
671 if (cd->klass == klass)
674 if (++i == hs->hash_size)
676 } while (i != start_pos);
681 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
685 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
688 if (hash [i] && hash [i]->klass == klass) {
689 hash [i]->total_size += size;
690 hash [i]->count += count;
693 } else if (!hash [i]) {
698 hash [i] = calloc (sizeof (HeapClassDesc), 1);
699 hash [i]->klass = klass;
700 hash [i]->total_size += size;
701 hash [i]->count += count;
708 } while (i != start_pos);
709 /* should not happen */
710 printf ("failed heap class store\n");
714 static HeapClassDesc*
715 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
719 if (hs->class_count * 2 >= hs->hash_size) {
721 int old_size = hs->hash_size;
723 if (hs->hash_size == 0)
725 n = calloc (sizeof (void*) * hs->hash_size, 1);
726 for (i = 0; i < old_size; ++i) {
727 res = hs->class_hash [i];
728 if (hs->class_hash [i])
729 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
732 free (hs->class_hash);
736 hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
737 //if (res->count == 1)
738 // printf ("added heap class: %s\n", res->klass->name);
742 static HeapObjectDesc*
743 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
745 HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
746 ho->objaddr = objaddr;
748 ho->num_refs = num_refs;
753 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
757 HeapObjectDesc **hash = hs->objects_hash;
758 start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
761 if (hash [i] && hash [i]->objaddr == objaddr) {
763 } else if (!hash [i]) {
767 if (++i == hs->objects_hash_size)
769 } while (i != start_pos);
770 /* should not happen */
771 //printf ("failed heap obj slot\n");
775 static HeapObjectDesc*
776 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
778 HeapObjectDesc **hash = hs->objects_hash;
779 uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
781 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
782 *ref_offset = hash [i]->num_refs;
783 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
788 /* should not happen */
789 printf ("failed heap obj update\n");
795 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
799 start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
802 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
803 printf ("duplicate object!\n");
805 } else if (!hash [i]) {
812 } while (i != start_pos);
813 /* should not happen */
814 printf ("failed heap obj store\n");
819 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
822 if (hs->objects_count * 2 >= hs->objects_hash_size) {
824 uintptr_t old_size = hs->objects_hash_size;
825 hs->objects_hash_size *= 2;
826 if (hs->objects_hash_size == 0)
827 hs->objects_hash_size = 4;
828 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
829 for (i = 0; i < old_size; ++i) {
830 if (hs->objects_hash [i])
831 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
833 if (hs->objects_hash)
834 free (hs->objects_hash);
835 hs->objects_hash = n;
837 hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
841 heap_shot_resolve_reverse_refs (HeapShot *hs)
844 for (i = 0; i < hs->objects_hash_size; ++i) {
846 HeapObjectDesc *ho = hs->objects_hash [i];
849 for (r = 0; r < ho->num_refs; ++r) {
850 uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
851 add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
860 heap_shot_mark_objects (HeapShot *hs)
863 unsigned char *marks;
864 HeapObjectDesc *obj, *ref;
866 uintptr_t num_marked = 0, num_unmarked;
867 for (i = 0; i < hs->num_roots; ++i) {
869 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
873 obj = hs->objects_hash [oi];
875 if (hs->roots_types [i] & MONO_PROFILE_GC_ROOT_PINNING)
876 cd->pinned_references++;
877 cd->root_references++;
881 /* consistency checks: it seems not all the objects are walked in the heap in some cases */
882 marks = calloc (hs->objects_hash_size, 1);
885 for (i = 0; i < hs->num_roots; ++i) {
886 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
888 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);
891 obj = hs->objects_hash [oi];
893 marks [oi] = obj->num_refs? MARK_GRAY: MARK_BLACK;
898 while (marked_some) {
900 for (i = 0; i < hs->objects_hash_size; ++i) {
901 if (marks [i] != MARK_GRAY)
903 marks [i] = MARK_BLACK;
904 obj = hs->objects_hash [i];
905 for (r = 0; r < obj->num_refs; ++r) {
906 oi = heap_shot_find_obj_slot (hs, obj->refs [r]);
908 fprintf (outfile, "referenced obj %p not found in heap\n", (void*)obj->refs [r]);
911 ref = hs->objects_hash [oi];
913 marks [oi] = ref->num_refs? MARK_GRAY: MARK_BLACK;
921 for (i = 0; i < hs->objects_hash_size; ++i) {
922 if (hs->objects_hash [i] && !marks [i]) {
924 fprintf (outfile, "object %p (%s) unmarked\n", (void*)hs->objects_hash [i], hs->objects_hash [i]->hklass->klass->name);
927 fprintf (outfile, "Total unmarked: %d/%d\n", num_unmarked, hs->objects_count);
932 heap_shot_free_objects (HeapShot *hs)
935 for (i = 0; i < hs->objects_hash_size; ++i) {
936 HeapObjectDesc *ho = hs->objects_hash [i];
940 if (hs->objects_hash)
941 free (hs->objects_hash);
942 hs->objects_hash = NULL;
943 hs->objects_hash_size = 0;
944 hs->objects_count = 0;
953 MethodDesc *methods [1];
956 static BackTrace *backtrace_hash [HASH_SIZE];
957 static BackTrace **backtraces = NULL;
958 static int num_backtraces = 0;
959 static int next_backtrace = 0;
962 hash_backtrace (int count, MethodDesc **methods)
966 for (i = 0; i < count; ++i) {
967 hash = (hash << 5) - hash + methods [i]->method;
973 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
976 if (bt->count != count)
978 for (i = 0; i < count; ++i)
979 if (methods [i] != bt->methods [i])
985 add_backtrace (int count, MethodDesc **methods)
987 int hash = hash_backtrace (count, methods);
988 int slot = (hash & 0xffff) % HASH_SIZE;
989 BackTrace *bt = backtrace_hash [slot];
991 if (bt->hash == hash && compare_backtrace (bt, count, methods))
995 bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
996 bt->next = backtrace_hash [slot];
997 backtrace_hash [slot] = bt;
998 if (next_backtrace == num_backtraces) {
1000 if (!num_backtraces)
1001 num_backtraces = 16;
1002 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
1004 bt->id = next_backtrace++;
1005 backtraces [bt->id] = bt;
1008 for (slot = 0; slot < count; ++slot)
1009 bt->methods [slot] = methods [slot];
1014 typedef struct _MonitorDesc MonitorDesc;
1015 typedef struct _ThreadContext ThreadContext;
1019 #if defined (HAVE_SYS_ZLIB)
1030 uint64_t startup_time;
1031 ThreadContext *threads;
1032 ThreadContext *current;
1035 struct _ThreadContext {
1036 ThreadContext *next;
1039 /* emulated stack */
1041 uint64_t *time_stack;
1042 uint64_t *callee_time_stack;
1044 uint64_t contention_start;
1045 MonitorDesc *monitor;
1048 HeapShot *current_heap_shot;
1049 uintptr_t num_roots;
1050 uintptr_t size_roots;
1052 uintptr_t *roots_extra;
1054 uint64_t gc_start_times [3];
1058 ensure_buffer (ProfContext *ctx, int size)
1060 if (ctx->size < size) {
1061 ctx->buf = realloc (ctx->buf, size);
1067 load_data (ProfContext *ctx, int size)
1069 ensure_buffer (ctx, size);
1070 #if defined (HAVE_SYS_ZLIB)
1072 int r = gzread (ctx->gzfile, ctx->buf, size);
1074 return size == 0? 1: 0;
1079 int r = fread (ctx->buf, size, 1, ctx->file);
1081 return size == 0? 1: 0;
1086 static ThreadContext*
1087 get_thread (ProfContext *ctx, intptr_t thread_id)
1089 ThreadContext *thread;
1090 if (ctx->current && ctx->current->thread_id == thread_id)
1091 return ctx->current;
1092 thread = ctx->threads;
1094 if (thread->thread_id == thread_id) {
1097 thread = thread->next;
1099 thread = calloc (sizeof (ThreadContext), 1);
1100 thread->next = ctx->threads;
1101 ctx->threads = thread;
1102 thread->thread_id = thread_id;
1103 thread->last_time = 0;
1104 thread->stack_id = 0;
1105 thread->stack_size = 32;
1106 thread->stack = malloc (thread->stack_size * sizeof (void*));
1107 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1108 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1112 static ThreadContext*
1113 load_thread (ProfContext *ctx, intptr_t thread_id)
1115 ThreadContext *thread = get_thread (ctx, thread_id);
1116 ctx->current = thread;
1121 ensure_thread_stack (ThreadContext *thread)
1123 if (thread->stack_id == thread->stack_size) {
1124 thread->stack_size *= 2;
1125 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
1126 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
1127 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
1132 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
1135 unsigned int start_pos;
1136 start_pos = bt->hash % size;
1139 if (traces [i].bt == bt) {
1140 traces [i].count += value;
1142 } else if (!traces [i].bt) {
1144 traces [i].count += value;
1150 } while (i != start_pos);
1151 /* should not happen */
1152 printf ("failed trace store\n");
1157 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
1160 if (!collect_traces)
1162 if (trace->count * 2 >= trace->size) {
1164 int old_size = trace->size;
1166 if (trace->size == 0)
1168 n = calloc (sizeof (CallContext) * trace->size, 1);
1169 for (i = 0; i < old_size; ++i) {
1170 if (trace->traces [i].bt)
1171 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
1174 free (trace->traces);
1177 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
1181 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
1184 int count = thread->stack_id;
1185 if (!collect_traces)
1187 if (count > trace_max)
1189 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
1190 add_trace_bt (bt, trace, value);
1195 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
1198 if (!collect_traces)
1200 if (count > trace_max)
1202 bt = add_backtrace (count, methods);
1203 add_trace_bt (bt, trace, value);
1208 thread_add_root (ThreadContext *ctx, uintptr_t obj, int root_type, uintptr_t extra_info)
1210 if (ctx->num_roots == ctx->size_roots) {
1211 int new_size = ctx->size_roots * 2;
1214 ctx->roots = realloc (ctx->roots, new_size * sizeof (uintptr_t));
1215 ctx->roots_extra = realloc (ctx->roots_extra, new_size * sizeof (uintptr_t));
1216 ctx->roots_types = realloc (ctx->roots_types, new_size * sizeof (int));
1217 ctx->size_roots = new_size;
1219 ctx->roots_types [ctx->num_roots] = root_type;
1220 ctx->roots_extra [ctx->num_roots] = extra_info;
1221 ctx->roots [ctx->num_roots++] = obj;
1225 compare_callc (const void *a, const void *b)
1227 const CallContext *A = a;
1228 const CallContext *B = b;
1229 if (B->count == A->count)
1231 if (B->count < A->count)
1237 sort_context_array (TraceDesc* traces)
1240 for (i = 0, j = 0; i < traces->size; ++i) {
1241 if (traces->traces [i].bt) {
1242 traces->traces [j].bt = traces->traces [i].bt;
1243 traces->traces [j].count = traces->traces [i].count;
1247 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
1251 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1253 ensure_thread_stack (thread);
1254 thread->time_stack [thread->stack_id] = timestamp;
1255 thread->callee_time_stack [thread->stack_id] = 0;
1256 thread->stack [thread->stack_id++] = method;
1257 method->recurse_count++;
1261 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1263 method->recurse_count--;
1264 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
1268 if (timestamp < thread->time_stack [thread->stack_id])
1269 fprintf (outfile, "time went backwards for %s\n", method->name);
1270 tdiff = timestamp - thread->time_stack [thread->stack_id];
1271 if (thread->callee_time_stack [thread->stack_id] > tdiff)
1272 fprintf (outfile, "callee time bigger for %s\n", method->name);
1273 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
1274 method->callee_time += thread->callee_time_stack [thread->stack_id];
1275 if (thread->stack_id)
1276 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
1277 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
1279 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
1284 uint64_t total_time;
1288 static GCDesc gc_info [3];
1289 static uint64_t max_heap_size;
1290 static uint64_t gc_object_moves;
1291 static int gc_resizes;
1299 static HandleInfo handle_info [4];
1302 gc_event_name (int ev)
1305 case MONO_GC_EVENT_START: return "start";
1306 case MONO_GC_EVENT_MARK_START: return "mark start";
1307 case MONO_GC_EVENT_MARK_END: return "mark end";
1308 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
1309 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
1310 case MONO_GC_EVENT_END: return "end";
1311 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
1312 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
1313 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
1314 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
1320 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
1321 static uint64_t throw_count = 0;
1322 static TraceDesc exc_traces;
1325 clause_name (int type)
1328 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
1329 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
1330 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
1331 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
1332 default: return "invalid";
1336 static uint64_t monitor_contention;
1337 static uint64_t monitor_failed;
1338 static uint64_t monitor_acquired;
1340 struct _MonitorDesc {
1343 uintptr_t contentions;
1345 uint64_t max_wait_time;
1349 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
1350 static int num_monitors = 0;
1353 lookup_monitor (uintptr_t objid)
1355 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
1356 MonitorDesc *cd = monitor_hash [slot];
1357 while (cd && cd->objid != objid)
1360 cd = calloc (sizeof (MonitorDesc), 1);
1362 cd->next = monitor_hash [slot];
1363 monitor_hash [slot] = cd;
1370 monitor_ev_name (int ev)
1373 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
1374 case MONO_PROFILER_MONITOR_DONE: return "acquired";
1375 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
1376 default: return "invalid";
1381 get_handle_name (int htype)
1384 case 0: return "weak";
1385 case 1: return "weaktrack";
1386 case 2: return "normal";
1387 case 3: return "pinned";
1388 default: return "unknown";
1393 get_root_name (int rtype)
1395 switch (rtype & MONO_PROFILE_GC_ROOT_TYPEMASK) {
1396 case MONO_PROFILE_GC_ROOT_STACK: return "stack";
1397 case MONO_PROFILE_GC_ROOT_FINALIZER: return "finalizer";
1398 case MONO_PROFILE_GC_ROOT_HANDLE: return "handle";
1399 case MONO_PROFILE_GC_ROOT_OTHER: return "other";
1400 case MONO_PROFILE_GC_ROOT_MISC: return "misc";
1401 default: return "unknown";
1406 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
1408 MethodDesc **frames;
1410 int flags = decode_uleb128 (p, &p);
1411 int count = decode_uleb128 (p, &p);
1415 frames = malloc (count * sizeof (void*));
1418 for (i = 0; i < count; ++i) {
1419 intptr_t ptrdiff = decode_sleb128 (p, &p);
1420 frames [i] = lookup_method (ptr_base + ptrdiff);
1428 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
1431 for (i = 0; i < num_tracked_objects; ++i) {
1432 if (tracked_objects [i] != obj)
1434 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
1435 if (bt && bt->count) {
1437 for (k = 0; k < bt->count; ++k)
1438 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
1444 track_handle (uintptr_t obj, int htype, uint32_t handle)
1447 for (i = 0; i < num_tracked_objects; ++i) {
1448 if (tracked_objects [i] == obj)
1449 fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1454 track_move (uintptr_t src, uintptr_t dst)
1457 for (i = 0; i < num_tracked_objects; ++i) {
1458 if (tracked_objects [i] == src)
1459 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1460 else if (tracked_objects [i] == dst)
1461 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1466 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1469 for (i = 0; i < num_tracked_objects; ++i) {
1470 if (tracked_objects [i] == obj)
1471 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1476 found_object (uintptr_t obj)
1478 num_tracked_objects ++;
1479 tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1480 tracked_objects [num_tracked_objects - 1] = obj;
1483 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
1484 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
1487 decode_buffer (ProfContext *ctx)
1494 intptr_t method_base;
1496 uint64_t file_offset;
1498 ThreadContext *thread;
1500 #ifdef HAVE_SYS_ZLIB
1502 file_offset = gztell (ctx->gzfile);
1505 file_offset = ftell (ctx->file);
1506 if (!load_data (ctx, 48))
1509 if (read_int32 (p) != BUF_ID) {
1510 fprintf (outfile, "Incorrect buffer id: 0x%x\n", read_int32 (p));
1511 for (i = 0; i < 48; ++i) {
1512 fprintf (outfile, "0x%x%s", p [i], i % 8?" ":"\n");
1516 len = read_int32 (p + 4);
1517 time_base = read_int64 (p + 8);
1518 ptr_base = read_int64 (p + 16);
1519 obj_base = read_int64 (p + 24);
1520 thread_id = read_int64 (p + 32);
1521 method_base = read_int64 (p + 40);
1523 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
1524 thread = load_thread (ctx, thread_id);
1525 if (!load_data (ctx, len))
1527 if (!startup_time) {
1528 startup_time = time_base;
1530 time_from += startup_time;
1531 time_to += startup_time;
1534 thread->name = pstrdup ("Main");
1536 for (i = 0; i < thread->stack_id; ++i)
1537 thread->stack [i]->recurse_count++;
1543 int subtype = *p & 0xf0;
1544 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1545 LOG_TIME (time_base, tdiff);
1547 if (subtype == TYPE_GC_RESIZE) {
1548 uint64_t new_size = decode_uleb128 (p, &p);
1550 fprintf (outfile, "gc heap resized to %llu\n", new_size);
1552 if (new_size > max_heap_size)
1553 max_heap_size = new_size;
1554 } else if (subtype == TYPE_GC_EVENT) {
1555 uint64_t ev = decode_uleb128 (p, &p);
1556 int gen = decode_uleb128 (p, &p);
1558 fprintf (outfile, "gc event for gen%d: %s at %llu (thread: 0x%x)\n", gen, gc_event_name (ev), time_base, thread->thread_id);
1560 fprintf (outfile, "incorrect gc gen: %d\n", gen);
1563 if (ev == MONO_GC_EVENT_START) {
1564 thread->gc_start_times [gen] = time_base;
1565 gc_info [gen].count++;
1566 } else if (ev == MONO_GC_EVENT_END) {
1567 tdiff = time_base - thread->gc_start_times [gen];
1568 gc_info [gen].total_time += tdiff;
1569 if (tdiff > gc_info [gen].max_time)
1570 gc_info [gen].max_time = tdiff;
1572 } else if (subtype == TYPE_GC_MOVE) {
1573 int j, num = decode_uleb128 (p, &p);
1574 gc_object_moves += num / 2;
1575 for (j = 0; j < num; j += 2) {
1576 intptr_t obj1diff = decode_sleb128 (p, &p);
1577 intptr_t obj2diff = decode_sleb128 (p, &p);
1578 if (num_tracked_objects)
1579 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
1581 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
1584 } else if (subtype == TYPE_GC_HANDLE_CREATED) {
1585 int htype = decode_uleb128 (p, &p);
1586 uint32_t handle = decode_uleb128 (p, &p);
1587 intptr_t objdiff = decode_sleb128 (p, &p);
1590 handle_info [htype].created++;
1591 handle_info [htype].live++;
1592 add_trace_thread (thread, &handle_info [htype].traces, 1);
1593 /* FIXME: we don't take into account timing here */
1594 if (handle_info [htype].live > handle_info [htype].max_live)
1595 handle_info [htype].max_live = handle_info [htype].live;
1596 if (num_tracked_objects)
1597 track_handle (OBJ_ADDR (objdiff), htype, handle);
1599 fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
1600 } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
1601 int htype = decode_uleb128 (p, &p);
1602 uint32_t handle = decode_uleb128 (p, &p);
1605 handle_info [htype].destroyed ++;
1606 handle_info [htype].live--;
1608 fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
1612 case TYPE_METADATA: {
1613 int error = *p & TYPE_LOAD_ERR;
1614 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1616 intptr_t ptrdiff = decode_sleb128 (p, &p);
1617 LOG_TIME (time_base, tdiff);
1619 if (mtype == TYPE_CLASS) {
1620 intptr_t imptrdiff = decode_sleb128 (p, &p);
1621 uint64_t flags = decode_uleb128 (p, &p);
1623 fprintf (outfile, "non-zero flags in class\n");
1627 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
1629 add_class (ptr_base + ptrdiff, (char*)p);
1632 } else if (mtype == TYPE_IMAGE) {
1633 uint64_t flags = decode_uleb128 (p, &p);
1635 fprintf (outfile, "non-zero flags in image\n");
1639 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
1641 add_image (ptr_base + ptrdiff, (char*)p);
1644 } else if (mtype == TYPE_THREAD) {
1646 uint64_t flags = decode_uleb128 (p, &p);
1648 fprintf (outfile, "non-zero flags in thread\n");
1651 nt = get_thread (ctx, ptr_base * ptrdiff);
1652 nt->name = pstrdup ((char*)p);
1654 fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
1661 int has_bt = *p & TYPE_ALLOC_BT;
1662 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1663 intptr_t ptrdiff = decode_sleb128 (p, &p);
1664 intptr_t objdiff = decode_sleb128 (p, &p);
1667 MethodDesc* sframes [8];
1668 MethodDesc** frames = sframes;
1669 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1670 len = decode_uleb128 (p, &p);
1671 LOG_TIME (time_base, tdiff);
1674 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
1677 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1679 fprintf (outfile, "Cannot load backtrace\n");
1683 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
1686 cd->alloc_size += len;
1688 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
1690 bt = add_trace_thread (thread, &cd->traces, len);
1691 if (find_size && len >= find_size) {
1692 if (!find_name || strstr (cd->name, find_name))
1693 found_object (OBJ_ADDR (objdiff));
1694 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
1695 found_object (OBJ_ADDR (objdiff));
1697 if (num_tracked_objects)
1698 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
1700 if (frames != sframes)
1705 int subtype = *p & 0xf0;
1706 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1707 int64_t ptrdiff = decode_sleb128 (p, &p);
1708 LOG_TIME (time_base, tdiff);
1710 method_base += ptrdiff;
1711 if (subtype == TYPE_JIT) {
1712 intptr_t codediff = decode_sleb128 (p, &p);
1713 int codelen = decode_uleb128 (p, &p);
1715 fprintf (outfile, "jitted method %p (%s), size: %d, code: %p\n", (void*)(method_base), p, codelen, (void*)(ptr_base + codediff));
1716 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
1721 if ((thread_filter && thread_filter != thread->thread_id))
1723 method = lookup_method (method_base);
1724 if (subtype == TYPE_ENTER) {
1725 add_trace_thread (thread, &method->traces, 1);
1726 push_method (thread, method, time_base);
1728 pop_method (thread, method, time_base);
1731 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
1736 int subtype = *p & 0xf0;
1737 if (subtype == TYPE_HEAP_OBJECT) {
1740 intptr_t objdiff = decode_sleb128 (p + 1, &p);
1741 intptr_t ptrdiff = decode_sleb128 (p, &p);
1742 uint64_t size = decode_uleb128 (p, &p);
1743 uintptr_t num = decode_uleb128 (p, &p);
1744 uintptr_t ref_offset;
1745 uintptr_t last_obj_offset = 0;
1746 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1748 HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
1749 if (collect_traces) {
1750 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
1751 add_heap_shot_obj (thread->current_heap_shot, ho);
1756 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
1758 for (i = 0; i < num; ++i) {
1759 /* FIXME: use object distance to measure how good
1760 * the GC is at keeping related objects close
1762 uintptr_t offset = ctx->data_version > 1? last_obj_offset + decode_uleb128 (p, &p): -1;
1763 intptr_t obj1diff = decode_sleb128 (p, &p);
1764 last_obj_offset = offset;
1766 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
1767 if (num_tracked_objects)
1768 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
1771 fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
1772 } else if (subtype == TYPE_HEAP_ROOT) {
1773 uintptr_t num = decode_uleb128 (p + 1, &p);
1774 uintptr_t gc_num = decode_uleb128 (p, &p);
1776 for (i = 0; i < num; ++i) {
1777 intptr_t objdiff = decode_sleb128 (p, &p);
1778 int root_type = decode_uleb128 (p, &p);
1779 /* we just discard the extra info for now */
1780 uintptr_t extra_info = decode_uleb128 (p, &p);
1782 fprintf (outfile, "object %p is a %s root\n", (void*)OBJ_ADDR (objdiff), get_root_name (root_type));
1784 thread_add_root (thread, OBJ_ADDR (objdiff), root_type, extra_info);
1786 } else if (subtype == TYPE_HEAP_END) {
1787 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1788 LOG_TIME (time_base, tdiff);
1791 fprintf (outfile, "heap shot end\n");
1792 if (collect_traces) {
1793 HeapShot *hs = thread->current_heap_shot;
1794 if (hs && thread->num_roots) {
1795 /* transfer the root ownershipt to the heapshot */
1796 hs->num_roots = thread->num_roots;
1797 hs->roots = thread->roots;
1798 hs->roots_extra = thread->roots_extra;
1799 hs->roots_types = thread->roots_types;
1801 free (thread->roots);
1802 free (thread->roots_extra);
1803 free (thread->roots_types);
1805 thread->num_roots = 0;
1806 thread->size_roots = 0;
1807 thread->roots = NULL;
1808 thread->roots_extra = NULL;
1809 thread->roots_types = NULL;
1810 heap_shot_resolve_reverse_refs (hs);
1811 heap_shot_mark_objects (hs);
1812 heap_shot_free_objects (hs);
1814 thread->current_heap_shot = NULL;
1815 } else if (subtype == TYPE_HEAP_START) {
1816 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1817 LOG_TIME (time_base, tdiff);
1820 fprintf (outfile, "heap shot start\n");
1821 thread->current_heap_shot = new_heap_shot (time_base);
1825 case TYPE_MONITOR: {
1826 int event = (*p >> 4) & 0x3;
1827 int has_bt = *p & TYPE_MONITOR_BT;
1828 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1829 intptr_t objdiff = decode_sleb128 (p, &p);
1830 MethodDesc* sframes [8];
1831 MethodDesc** frames = sframes;
1834 LOG_TIME (time_base, tdiff);
1836 record = (!thread_filter || thread_filter == thread->thread_id);
1837 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
1838 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
1840 monitor_contention++;
1841 mdesc->contentions++;
1842 thread->monitor = mdesc;
1843 thread->contention_start = time_base;
1847 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1849 fprintf (outfile, "Cannot load backtrace\n");
1853 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1856 add_trace_thread (thread, &mdesc->traces, 1);
1858 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1861 if (thread->monitor && thread->contention_start) {
1862 uint64_t wait_time = time_base - thread->contention_start;
1863 if (wait_time > thread->monitor->max_wait_time)
1864 thread->monitor->max_wait_time = wait_time;
1865 thread->monitor->wait_time += wait_time;
1866 thread->monitor = NULL;
1867 thread->contention_start = 0;
1870 } else if (event == MONO_PROFILER_MONITOR_DONE) {
1873 if (thread->monitor && thread->contention_start) {
1874 uint64_t wait_time = time_base - thread->contention_start;
1875 if (wait_time > thread->monitor->max_wait_time)
1876 thread->monitor->max_wait_time = wait_time;
1877 thread->monitor->wait_time += wait_time;
1878 thread->monitor = NULL;
1879 thread->contention_start = 0;
1884 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1885 if (frames != sframes)
1889 case TYPE_EXCEPTION: {
1890 int subtype = *p & 0x70;
1891 int has_bt = *p & TYPE_EXCEPTION_BT;
1892 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1893 MethodDesc* sframes [8];
1894 MethodDesc** frames = sframes;
1896 LOG_TIME (time_base, tdiff);
1898 record = (!thread_filter || thread_filter == thread->thread_id);
1899 if (subtype == TYPE_CLAUSE) {
1900 int clause_type = decode_uleb128 (p, &p);
1901 int clause_num = decode_uleb128 (p, &p);
1902 int64_t ptrdiff = decode_sleb128 (p, &p);
1903 method_base += ptrdiff;
1905 clause_summary [clause_type]++;
1907 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1909 intptr_t objdiff = decode_sleb128 (p, &p);
1914 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1916 fprintf (outfile, "Cannot load backtrace\n");
1920 add_trace_methods (frames, has_bt, &exc_traces, 1);
1923 add_trace_thread (thread, &exc_traces, 1);
1925 if (frames != sframes)
1928 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1933 int subtype = *p & 0xf0;
1934 if (subtype == TYPE_SAMPLE_HIT) {
1936 int sample_type = decode_uleb128 (p + 1, &p);
1937 uint64_t tstamp = decode_uleb128 (p, &p);
1938 int count = decode_uleb128 (p, &p);
1939 for (i = 0; i < count; ++i) {
1940 uintptr_t ip = ptr_base + decode_sleb128 (p, &p);
1941 add_stat_sample (sample_type, ip);
1943 fprintf (outfile, "sample hit, type: %d at %p\n", sample_type, (void*)ip);
1945 } else if (subtype == TYPE_SAMPLE_USYM) {
1946 /* un unmanaged symbol description */
1947 uintptr_t addr = ptr_base + decode_sleb128 (p + 1, &p);
1948 uintptr_t size = decode_uleb128 (p, &p);
1950 name = pstrdup ((char*)p);
1951 add_unmanaged_symbol (addr, name, size);
1953 fprintf (outfile, "unmanaged symbol %s at %p\n", name, (void*)addr);
1956 } else if (subtype == TYPE_SAMPLE_UBIN) {
1957 /* un unmanaged binary loaded in memory */
1958 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1959 uintptr_t addr = decode_sleb128 (p, &p);
1960 uint64_t offset = decode_uleb128 (p, &p);
1961 uintptr_t size = decode_uleb128 (p, &p);
1963 LOG_TIME (time_base, tdiff);
1965 name = pstrdup ((char*)p);
1966 add_unmanaged_binary (addr, name, size);
1968 fprintf (outfile, "unmanaged binary %s at %p\n", name, (void*)addr);
1977 fprintf (outfile, "unhandled profiler event: 0x%x at file offset: %llu + %d (len: %d\n)\n", *p, file_offset, p - ctx->buf, len);
1981 thread->last_time = time_base;
1982 for (i = 0; i < thread->stack_id; ++i)
1983 thread->stack [i]->recurse_count = 0;
1988 load_file (char *name)
1991 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1992 if (strcmp (name, "-") == 0)
1995 ctx->file = fopen (name, "rb");
1997 printf ("Cannot open file: %s\n", name);
2000 #if defined (HAVE_SYS_ZLIB)
2001 if (ctx->file != stdin)
2002 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
2004 if (!load_data (ctx, 32))
2007 if (read_int32 (p) != LOG_HEADER_ID || p [6] > LOG_DATA_VERSION)
2009 ctx->version_major = p [4];
2010 ctx->version_minor = p [5];
2011 ctx->data_version = p [6];
2012 /* reading 64 bit files on 32 bit systems not supported yet */
2013 if (p [7] > sizeof (void*))
2015 if (read_int32 (p + 20)) /* flags must be 0 */
2017 ctx->startup_time = read_int64 (p + 8);
2018 ctx->timer_overhead = read_int32 (p + 16);
2019 ctx->pid = read_int32 (p + 24);
2020 ctx->port = read_int16 (p + 28);
2028 static int alloc_sort_mode = ALLOC_SORT_BYTES;
2031 compare_class (const void *a, const void *b)
2033 ClassDesc *const*A = a;
2034 ClassDesc *const*B = b;
2035 uint64_t vala, valb;
2036 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2037 vala = (*A)->alloc_size;
2038 valb = (*B)->alloc_size;
2040 vala = (*A)->allocs;
2041 valb = (*B)->allocs;
2051 dump_header (ProfContext *ctx)
2053 time_t st = ctx->startup_time / 1000;
2054 char *t = ctime (&st);
2055 fprintf (outfile, "\nMono log profiler data\n");
2056 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
2057 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
2058 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
2059 fprintf (outfile, "\tProgram startup: %s", t);
2061 fprintf (outfile, "\tProgram ID: %d\n", ctx->pid);
2063 fprintf (outfile, "\tServer listening on: %d\n", ctx->port);
2067 dump_traces (TraceDesc *traces, const char *desc)
2074 sort_context_array (traces);
2075 for (j = 0; j < traces->count; ++j) {
2078 bt = traces->traces [j].bt;
2081 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
2082 for (k = 0; k < bt->count; ++k)
2083 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
2088 dump_threads (ProfContext *ctx)
2090 ThreadContext *thread;
2091 fprintf (outfile, "\nThread summary\n");
2092 for (thread = ctx->threads; thread; thread = thread->next) {
2093 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
2098 dump_exceptions (void)
2101 fprintf (outfile, "\nException summary\n");
2102 fprintf (outfile, "\tThrows: %llu\n", throw_count);
2103 dump_traces (&exc_traces, "throws");
2104 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
2105 if (!clause_summary [i])
2107 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
2112 compare_monitor (const void *a, const void *b)
2114 MonitorDesc *const*A = a;
2115 MonitorDesc *const*B = b;
2116 if ((*B)->wait_time == (*A)->wait_time)
2118 if ((*B)->wait_time < (*A)->wait_time)
2124 dump_monitors (void)
2126 MonitorDesc **monitors;
2130 monitors = malloc (sizeof (void*) * num_monitors);
2131 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
2132 MonitorDesc *mdesc = monitor_hash [i];
2134 monitors [j++] = mdesc;
2135 mdesc = mdesc->next;
2138 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
2139 fprintf (outfile, "\nMonitor lock summary\n");
2140 for (i = 0; i < num_monitors; ++i) {
2141 MonitorDesc *mdesc = monitors [i];
2142 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
2143 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
2144 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
2145 dump_traces (&mdesc->traces, "contentions");
2147 fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
2148 fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
2149 fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
2156 fprintf (outfile, "\nGC summary\n");
2157 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
2158 fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
2159 fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
2160 for (i = 0; i < 3; ++i) {
2161 if (!gc_info [i].count)
2163 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
2164 i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
2165 gc_info [i].total_time / gc_info [i].count / 1000);
2167 for (i = 0; i < 3; ++i) {
2168 if (!handle_info [i].max_live)
2170 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
2171 get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
2172 dump_traces (&handle_info [i].traces, "created");
2181 int compiled_methods = 0;
2183 fprintf (outfile, "\nJIT summary\n");
2184 for (i = 0; i < HASH_SIZE; ++i) {
2185 m = method_hash [i];
2186 for (m = method_hash [i]; m; m = m->next) {
2190 code_size += m->len;
2193 fprintf (outfile, "\tCompiled methods: %d\n", compiled_methods);
2194 fprintf (outfile, "\tGenerated code size: %d\n", code_size);
2198 dump_allocations (void)
2201 intptr_t allocs = 0;
2203 int header_done = 0;
2204 ClassDesc **classes = malloc (num_classes * sizeof (void*));
2207 for (i = 0; i < HASH_SIZE; ++i) {
2208 cd = class_hash [i];
2214 qsort (classes, num_classes, sizeof (void*), compare_class);
2215 for (i = 0; i < num_classes; ++i) {
2219 allocs += cd->allocs;
2220 size += cd->alloc_size;
2221 if (!header_done++) {
2222 fprintf (outfile, "\nAllocation summary\n");
2223 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
2225 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
2226 dump_traces (&cd->traces, "bytes");
2229 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
2238 static int method_sort_mode = METHOD_SORT_TOTAL;
2241 compare_method (const void *a, const void *b)
2243 MethodDesc *const*A = a;
2244 MethodDesc *const*B = b;
2245 uint64_t vala, valb;
2246 if (method_sort_mode == METHOD_SORT_SELF) {
2247 vala = (*A)->self_time;
2248 valb = (*B)->self_time;
2249 } else if (method_sort_mode == METHOD_SORT_CALLS) {
2253 vala = (*A)->total_time;
2254 valb = (*B)->total_time;
2264 dump_metadata (void)
2266 fprintf (outfile, "\nMetadata summary\n");
2267 fprintf (outfile, "\tLoaded images: %d\n", num_images);
2271 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
2272 image = image_hash [i];
2274 fprintf (outfile, "\t\t%s\n", image->filename);
2275 image = image->next;
2287 int header_done = 0;
2288 MethodDesc **methods = malloc (num_methods * sizeof (void*));
2291 for (i = 0; i < HASH_SIZE; ++i) {
2292 cd = method_hash [i];
2294 cd->total_time = cd->self_time + cd->callee_time;
2299 qsort (methods, num_methods, sizeof (void*), compare_method);
2300 for (i = 0; i < num_methods; ++i) {
2307 msecs = cd->total_time / 1000000;
2308 smsecs = (cd->total_time - cd->callee_time) / 1000000;
2309 if (!msecs && !verbose)
2311 if (!header_done++) {
2312 fprintf (outfile, "\nMethod call summary\n");
2313 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
2315 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
2316 dump_traces (&cd->traces, "calls");
2319 fprintf (outfile, "Total calls: %llu\n", calls);
2323 compare_heap_class (const void *a, const void *b)
2325 HeapClassDesc *const*A = a;
2326 HeapClassDesc *const*B = b;
2327 uint64_t vala, valb;
2328 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2329 vala = (*A)->total_size;
2330 valb = (*B)->total_size;
2343 compare_rev_class (const void *a, const void *b)
2345 const HeapClassRevRef *A = a;
2346 const HeapClassRevRef *B = b;
2347 if (B->count == A->count)
2349 if (B->count < A->count)
2355 dump_rev_claases (HeapClassRevRef *revs, int count)
2362 for (j = 0; j < count; ++j) {
2363 HeapClassDesc *cd = revs [j].klass;
2364 fprintf (outfile, "\t\t%llu references from: %s\n", revs [j].count, cd->klass->name);
2369 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
2376 HeapClassDesc **sorted;
2377 sorted = malloc (sizeof (void*) * hs->class_count);
2378 for (i = 0; i < hs->hash_size; ++i) {
2379 cd = hs->class_hash [i];
2383 size += cd->total_size;
2384 sorted [ccount++] = cd;
2386 hs->sorted = sorted;
2387 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
2388 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d, roots: %d\n",
2389 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount, hs->num_roots);
2390 if (!verbose && ccount > 30)
2392 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
2393 for (i = 0; i < ccount; ++i) {
2394 HeapClassRevRef *rev_sorted;
2396 HeapClassDesc *ocd = NULL;
2399 ocd = heap_class_lookup (last_hs, cd->klass);
2400 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
2402 int64_t bdiff = cd->total_size - ocd->total_size;
2403 int64_t cdiff = cd->count - ocd->count;
2404 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
2406 fprintf (outfile, "\n");
2408 if (!collect_traces)
2410 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
2412 for (j = 0; j < cd->rev_hash_size; ++j) {
2413 if (cd->rev_hash [j].klass)
2414 rev_sorted [k++] = cd->rev_hash [j];
2416 assert (cd->rev_count == k);
2417 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
2418 if (cd->root_references)
2419 fprintf (outfile, "\t\t%d root references (%d pinning)\n", cd->root_references, cd->pinned_references);
2420 dump_rev_claases (rev_sorted, cd->rev_count);
2427 compare_heap_shots (const void *a, const void *b)
2429 HeapShot *const*A = a;
2430 HeapShot *const*B = b;
2431 if ((*B)->timestamp == (*A)->timestamp)
2433 if ((*B)->timestamp > (*A)->timestamp)
2439 dump_heap_shots (void)
2441 HeapShot **hs_sorted;
2443 HeapShot *last_hs = NULL;
2447 hs_sorted = malloc (num_heap_shots * sizeof (void*));
2448 fprintf (outfile, "\nHeap shot summary\n");
2450 for (hs = heap_shots; hs; hs = hs->next)
2451 hs_sorted [i++] = hs;
2452 qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
2453 for (i = 0; i < num_heap_shots; ++i) {
2455 heap_shot_summary (hs, i, last_hs);
2461 flush_context (ProfContext *ctx)
2463 ThreadContext *thread;
2464 /* FIXME: sometimes there are leftovers: indagate */
2465 for (thread = ctx->threads; thread; thread = thread->next) {
2466 while (thread->stack_id) {
2468 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
2469 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
2474 static const char *reports = "header,jit,gc,sample,alloc,call,metadata,exception,monitor,thread,heapshot";
2477 match_option (const char *p, const char *opt)
2479 int len = strlen (opt);
2480 if (strncmp (p, opt, len) == 0) {
2489 print_reports (ProfContext *ctx, const char *reps, int parse_only)
2493 for (p = reps; *p; p = opt) {
2494 if ((opt = match_option (p, "header")) != p) {
2499 if ((opt = match_option (p, "thread")) != p) {
2504 if ((opt = match_option (p, "gc")) != p) {
2509 if ((opt = match_option (p, "jit")) != p) {
2514 if ((opt = match_option (p, "alloc")) != p) {
2516 dump_allocations ();
2519 if ((opt = match_option (p, "call")) != p) {
2524 if ((opt = match_option (p, "metadata")) != p) {
2529 if ((opt = match_option (p, "exception")) != p) {
2534 if ((opt = match_option (p, "monitor")) != p) {
2539 if ((opt = match_option (p, "heapshot")) != p) {
2544 if ((opt = match_option (p, "sample")) != p) {
2555 add_find_spec (const char *p)
2557 if (p [0] == 'S' && p [1] == ':') {
2559 find_size = strtoul (p + 2, &vale, 10);
2561 } else if (p [0] == 'T' && p [1] == ':') {
2571 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
2572 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
2573 printf ("FILENAME can be '-' to read from standard input.\n");
2574 printf ("Options:\n");
2575 printf ("\t--help display this help\n");
2576 printf ("\t--out=FILE write to FILE instead of stdout\n");
2577 printf ("\t--traces collect and show backtraces\n");
2578 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
2579 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
2580 printf ("\t %s\n", reports);
2581 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
2582 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
2583 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
2584 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
2585 printf ("\t S:minimum_size or T:partial_name\n");
2586 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
2587 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
2588 printf ("\t--verbose increase verbosity level\n");
2589 printf ("\t--debug display decoding debug info for mprof-report devs\n");
2593 main (int argc, char *argv[])
2598 for (i = 1; i < argc; ++i) {
2599 if (strcmp ("--debug", argv [i]) == 0) {
2601 } else if (strcmp ("--help", argv [i]) == 0) {
2604 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
2605 const char *val = argv [i] + 13;
2606 if (strcmp (val, "bytes") == 0) {
2607 alloc_sort_mode = ALLOC_SORT_BYTES;
2608 } else if (strcmp (val, "count") == 0) {
2609 alloc_sort_mode = ALLOC_SORT_COUNT;
2614 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
2615 const char *val = argv [i] + 14;
2616 if (strcmp (val, "total") == 0) {
2617 method_sort_mode = METHOD_SORT_TOTAL;
2618 } else if (strcmp (val, "self") == 0) {
2619 method_sort_mode = METHOD_SORT_SELF;
2620 } else if (strcmp (val, "calls") == 0) {
2621 method_sort_mode = METHOD_SORT_CALLS;
2626 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
2627 const char *val = argv [i] + 10;
2628 if (!print_reports (NULL, val, 1)) {
2633 } else if (strncmp ("--out=", argv [i], 6) == 0) {
2634 const char *val = argv [i] + 6;
2635 outfile = fopen (val, "w");
2637 printf ("Cannot open output file: %s\n", val);
2640 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
2641 const char *val = argv [i] + 12;
2643 trace_max = strtoul (val, &vale, 10);
2644 } else if (strncmp ("--find=", argv [i], 7) == 0) {
2645 const char *val = argv [i] + 7;
2646 if (!add_find_spec (val)) {
2650 } else if (strncmp ("--track=", argv [i], 8) == 0) {
2651 const char *val = argv [i] + 8;
2654 uintptr_t tracked_obj;
2659 tracked_obj = strtoul (val, &vale, 0);
2660 found_object (tracked_obj);
2663 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
2664 const char *val = argv [i] + 9;
2666 thread_filter = strtoul (val, &vale, 0);
2667 } else if (strncmp ("--time=", argv [i], 7) == 0) {
2668 char *val = pstrdup (argv [i] + 7);
2669 double from_secs, to_secs;
2670 char *top = strchr (val, '-');
2676 from_secs = atof (val);
2677 to_secs = atof (top);
2679 if (from_secs > to_secs) {
2683 time_from = from_secs * 1000000000;
2684 time_to = to_secs * 1000000000;
2685 } else if (strcmp ("--verbose", argv [i]) == 0) {
2687 } else if (strcmp ("--traces", argv [i]) == 0) {
2698 ctx = load_file (argv [i]);
2700 printf ("Not a log profiler data file (or unsupported version).\n");
2703 while (decode_buffer (ctx));
2704 flush_context (ctx);
2705 if (num_tracked_objects)
2707 print_reports (ctx, reports, 0);