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>
26 #include <mono/utils/mono-counters.h>
28 #define HASH_SIZE 9371
29 #define SMALL_HASH_SIZE 31
31 #if defined(__native_client__) || defined(__native_client_codegen__)
32 volatile int __nacl_thread_suspension_needed = 0;
33 void __nacl_suspend_thread_if_needed() {}
37 static int collect_traces = 0;
38 static int show_traces = 0;
39 static int trace_max = 6;
40 static int verbose = 0;
41 static uintptr_t *tracked_objects = 0;
42 static int num_tracked_objects = 0;
43 static uintptr_t thread_filter = 0;
44 static uint64_t find_size = 0;
45 static const char* find_name = NULL;
46 static uint64_t time_from = 0;
47 static uint64_t time_to = 0xffffffffffffffffULL;
48 static int use_time_filter = 0;
49 static uint64_t startup_time = 0;
50 static FILE* outfile = NULL;
53 read_int16 (unsigned char *p)
61 read_int32 (unsigned char *p)
65 value |= (*p++) << 16;
66 value |= (uint32_t)(*p++) << 24;
71 read_int64 (unsigned char *p)
73 uint64_t value = *p++;
75 value |= (*p++) << 16;
76 value |= (uint64_t)(*p++) << 24;
77 value |= (uint64_t)(*p++) << 32;
78 value |= (uint64_t)(*p++) << 40;
79 value |= (uint64_t)(*p++) << 48;
80 value |= (uint64_t)(*p++) << 54;
85 pstrdup (const char *s)
87 int len = strlen (s) + 1;
88 char *p = malloc (len);
93 typedef struct _CounterValue CounterValue;
94 struct _CounterValue {
96 unsigned char *buffer;
100 typedef struct _Counter Counter;
108 CounterValue *values;
109 CounterValue *values_last;
112 typedef struct _CounterList CounterList;
113 struct _CounterList {
118 typedef struct _CounterSection CounterSection;
119 struct _CounterSection {
121 CounterList *counters;
122 CounterList *counters_last;
123 CounterSection *next;
126 typedef struct _CounterTimestamp CounterTimestamp;
127 struct _CounterTimestamp {
129 CounterSection *sections;
130 CounterSection *sections_last;
131 CounterTimestamp *next;
134 static CounterList *counters = NULL;
135 static CounterSection *counters_sections = NULL;
136 static CounterTimestamp *counters_timestamps = NULL;
140 COUNTERS_SORT_CATEGORY
143 static int counters_sort_mode = COUNTERS_SORT_TIME;
146 add_counter_to_section (Counter *counter)
148 CounterSection *csection, *s;
151 clist = calloc (1, sizeof (CounterList));
152 clist->counter = counter;
154 for (csection = counters_sections; csection; csection = csection->next) {
155 if (csection->value == counter->section) {
156 /* If section exist */
157 if (!csection->counters)
158 csection->counters = clist;
160 csection->counters_last->next = clist;
161 csection->counters_last = clist;
166 /* If section does not exist */
167 csection = calloc (1, sizeof (CounterSection));
168 csection->value = counter->section;
169 csection->counters = clist;
170 csection->counters_last = clist;
172 if (!counters_sections) {
173 counters_sections = csection;
175 s = counters_sections;
183 add_counter (int section, const char *name, int type, int unit, int variance, int index)
185 CounterList *list, *l;
188 for (list = counters; list; list = list->next)
189 if (list->counter->index == index)
192 counter = calloc (1, sizeof (Counter));
193 counter->section = section;
194 counter->name = name;
195 counter->type = type;
196 counter->unit = unit;
197 counter->variance = variance;
198 counter->index = index;
200 list = calloc (1, sizeof (CounterList));
201 list->counter = counter;
212 if (counters_sort_mode == COUNTERS_SORT_CATEGORY)
213 add_counter_to_section (counter);
217 add_counter_to_timestamp (uint64_t timestamp, Counter *counter)
219 CounterTimestamp *ctimestamp, *t;
220 CounterSection *csection;
223 clist = calloc (1, sizeof (CounterList));
224 clist->counter = counter;
226 for (ctimestamp = counters_timestamps; ctimestamp; ctimestamp = ctimestamp->next) {
227 if (ctimestamp->value == timestamp) {
228 for (csection = ctimestamp->sections; csection; csection = csection->next) {
229 if (csection->value == counter->section) {
230 /* if timestamp exist and section exist */
231 if (!csection->counters)
232 csection->counters = clist;
234 csection->counters_last->next = clist;
235 csection->counters_last = clist;
240 /* if timestamp exist and section does not exist */
241 csection = calloc (1, sizeof (CounterSection));
242 csection->value = counter->section;
243 csection->counters = clist;
244 csection->counters_last = clist;
246 if (!ctimestamp->sections)
247 ctimestamp->sections = csection;
249 ctimestamp->sections_last->next = csection;
250 ctimestamp->sections_last = csection;
255 /* If timestamp do not exist and section does not exist */
256 csection = calloc (1, sizeof (CounterSection));
257 csection->value = counter->section;
258 csection->counters = clist;
259 csection->counters_last = clist;
261 ctimestamp = calloc (1, sizeof (CounterTimestamp));
262 ctimestamp->value = timestamp;
263 ctimestamp->sections = csection;
264 ctimestamp->sections_last = csection;
266 if (!counters_timestamps) {
267 counters_timestamps = ctimestamp;
269 t = counters_timestamps;
272 t->next = ctimestamp;
277 add_counter_value (int index, CounterValue *value)
281 for (list = counters; list; list = list->next) {
282 if (list->counter->index == index) {
283 if (!list->counter->values)
284 list->counter->values = value;
286 list->counter->values_last->next = value;
287 list->counter->values_last = value;
289 if (counters_sort_mode == COUNTERS_SORT_TIME)
290 add_counter_to_timestamp (value->timestamp, list->counter);
298 section_name (int section)
301 case MONO_COUNTER_JIT: return "Mono JIT";
302 case MONO_COUNTER_GC: return "Mono GC";
303 case MONO_COUNTER_METADATA: return "Mono Metadata";
304 case MONO_COUNTER_GENERICS: return "Mono Generics";
305 case MONO_COUNTER_SECURITY: return "Mono Security";
306 case MONO_COUNTER_RUNTIME: return "Mono Runtime";
307 case MONO_COUNTER_SYSTEM: return "Mono System";
308 default: return "<unknown>";
316 case MONO_COUNTER_INT: return "Int";
317 case MONO_COUNTER_UINT: return "UInt";
318 case MONO_COUNTER_WORD: return "Word";
319 case MONO_COUNTER_LONG: return "Long";
320 case MONO_COUNTER_ULONG: return "ULong";
321 case MONO_COUNTER_DOUBLE: return "Double";
322 case MONO_COUNTER_STRING: return "String";
323 case MONO_COUNTER_TIME_INTERVAL: return "Time Interval";
324 default: return "<unknown>";
332 case MONO_COUNTER_RAW: return "Raw";
333 case MONO_COUNTER_BYTES: return "Bytes";
334 case MONO_COUNTER_TIME: return "Time";
335 case MONO_COUNTER_COUNT: return "Count";
336 case MONO_COUNTER_PERCENTAGE: return "Percentage";
337 default: return "<unknown>";
342 variance_name (int variance)
345 case MONO_COUNTER_MONOTONIC: return "Monotonic";
346 case MONO_COUNTER_CONSTANT: return "Constant";
347 case MONO_COUNTER_VARIABLE: return "Variable";
348 default: return "<unknown>";
353 dump_counters_value (Counter *counter, const char *key_format, const char *key, void *value)
357 switch (counter->type) {
358 case MONO_COUNTER_INT:
359 #if SIZEOF_VOID_P == 4
360 case MONO_COUNTER_WORD:
362 snprintf (format, sizeof (format), "\t\t\t%s: %%d\n", key_format);
363 fprintf (outfile, format, key, *(int32_t*)value);
365 case MONO_COUNTER_UINT:
366 snprintf (format, sizeof (format), "\t\t\t%s: %%u\n", key_format);
367 fprintf (outfile, format, key, *(uint32_t*)value);
369 case MONO_COUNTER_LONG:
370 #if SIZEOF_VOID_P == 8
371 case MONO_COUNTER_WORD:
373 case MONO_COUNTER_TIME_INTERVAL:
374 if (counter->type == MONO_COUNTER_LONG && counter->unit == MONO_COUNTER_TIME) {
375 snprintf (format, sizeof (format), "\t\t\t%s: %%0.3fms\n", key_format);
376 fprintf (outfile, format, key, (double)*(int64_t*)value / 10000.0);
377 } else if (counter->type == MONO_COUNTER_TIME_INTERVAL) {
378 snprintf (format, sizeof (format), "\t\t\t%s: %%0.3fms\n", key_format);
379 fprintf (outfile, format, key, (double)*(int64_t*)value / 1000.0);
381 snprintf (format, sizeof (format), "\t\t\t%s: %%u\n", key_format);
382 fprintf (outfile, format, key, *(int64_t*)value);
385 case MONO_COUNTER_ULONG:
386 snprintf (format, sizeof (format), "\t\t\t%s: %%llu\n", key_format);
387 fprintf (outfile, format, key, *(uint64_t*)value);
389 case MONO_COUNTER_DOUBLE:
390 snprintf (format, sizeof (format), "\t\t\t%s: %%f\n", key_format);
391 fprintf (outfile, format, key, *(double*)value);
393 case MONO_COUNTER_STRING:
394 snprintf (format, sizeof (format), "\t\t\t%s: %%s\n", key_format);
395 fprintf (outfile, format, key, *(char*)value);
404 CounterValue *cvalue;
405 CounterTimestamp *ctimestamp;
406 CounterSection *csection;
408 char strtimestamp[17];
410 fprintf (outfile, "\nCounters:\n");
413 for (csection = counters_sections; csection; csection = csection->next) {
414 fprintf (outfile, "\t%s:\n", section_name (csection->value));
416 for (clist = csection->counters; clist; clist = clist->next) {
417 counter = clist->counter;
418 dump_counters_value (counter, "%-30s", counter->name, counter->values_last->buffer);
421 } else if (counters_sort_mode == COUNTERS_SORT_TIME) {
422 for (ctimestamp = counters_timestamps; ctimestamp; ctimestamp = ctimestamp->next) {
423 fprintf (outfile, "\t%lld:%02lld:%02lld:%02lld.%03lld:\n", ctimestamp->value / 1000 / 60 / 60 / 24 % 1000,
424 ctimestamp->value / 1000 / 60 / 60 % 24, ctimestamp->value / 1000 / 60 % 60,
425 ctimestamp->value / 1000 % 60, ctimestamp->value % 1000);
427 for (csection = ctimestamp->sections; csection; csection = csection->next) {
428 fprintf (outfile, "\t\t%s:\n", section_name (csection->value));
430 for (clist = csection->counters; clist; clist = clist->next) {
431 counter = clist->counter;
432 for (cvalue = counter->values; cvalue; cvalue = cvalue->next) {
433 if (cvalue->timestamp != ctimestamp->value)
436 dump_counters_value (counter, "%-30s", counter->name, cvalue->buffer);
441 } else if (counters_sort_mode == COUNTERS_SORT_CATEGORY) {
442 for (csection = counters_sections; csection; csection = csection->next) {
443 fprintf (outfile, "\t%s:\n", section_name (csection->value));
445 for (clist = csection->counters; clist; clist = clist->next) {
446 counter = clist->counter;
447 fprintf (outfile, "\t\t%s: [type: %s, unit: %s, variance: %s]\n",
448 counter->name, type_name (counter->type), unit_name (counter->unit), variance_name (counter->variance));
450 for (cvalue = counter->values; cvalue; cvalue = cvalue->next) {
451 snprintf (strtimestamp, sizeof (strtimestamp), "%lld:%02lld:%02lld:%02lld.%03lld", cvalue->timestamp / 1000 / 60 / 60 / 24 % 1000,
452 cvalue->timestamp / 1000 / 60 / 60 % 24, cvalue->timestamp / 1000 / 60 % 60,
453 cvalue->timestamp / 1000 % 60, cvalue->timestamp % 1000);
455 dump_counters_value (counter, "%s", strtimestamp, cvalue->buffer);
462 static int num_images;
463 typedef struct _ImageDesc ImageDesc;
470 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
473 add_image (intptr_t image, char *name)
475 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
476 ImageDesc *cd = malloc (sizeof (ImageDesc));
478 cd->filename = pstrdup (name);
479 cd->next = image_hash [slot];
480 image_hash [slot] = cd;
484 typedef struct _BackTrace BackTrace;
496 typedef struct _ClassDesc ClassDesc;
506 static ClassDesc* class_hash [HASH_SIZE] = {0};
507 static int num_classes = 0;
510 add_class (intptr_t klass, const char *name)
512 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
514 cd = class_hash [slot];
515 while (cd && cd->klass != klass)
517 /* we resolved an unknown class (unless we had the code unloaded) */
519 /*printf ("resolved unknown: %s\n", name);*/
521 cd->name = pstrdup (name);
524 cd = calloc (sizeof (ClassDesc), 1);
526 cd->name = pstrdup (name);
527 cd->next = class_hash [slot];
530 cd->traces.count = 0;
532 cd->traces.traces = NULL;
533 class_hash [slot] = cd;
539 lookup_class (intptr_t klass)
541 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
542 ClassDesc *cd = class_hash [slot];
543 while (cd && cd->klass != klass)
547 snprintf (buf, sizeof (buf), "unresolved class %p", (void*)klass);
548 return add_class (klass, buf);
553 typedef struct _MethodDesc MethodDesc;
564 uint64_t callee_time;
569 static MethodDesc* method_hash [HASH_SIZE] = {0};
570 static int num_methods = 0;
573 add_method (intptr_t method, const char *name, intptr_t code, int len)
575 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
577 cd = method_hash [slot];
578 while (cd && cd->method != method)
580 /* we resolved an unknown method (unless we had the code unloaded) */
584 /*printf ("resolved unknown: %s\n", name);*/
586 cd->name = pstrdup (name);
589 cd = calloc (sizeof (MethodDesc), 1);
591 cd->name = pstrdup (name);
596 cd->traces.count = 0;
598 cd->traces.traces = NULL;
599 cd->next = method_hash [slot];
600 method_hash [slot] = cd;
606 lookup_method (intptr_t method)
608 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
609 MethodDesc *cd = method_hash [slot];
610 while (cd && cd->method != method)
614 snprintf (buf, sizeof (buf), "unknown method %p", (void*)method);
615 return add_method (method, buf, 0, 0);
620 static int num_stat_samples = 0;
621 static int size_stat_samples = 0;
622 uintptr_t *stat_samples = NULL;
623 int *stat_sample_desc = NULL;
626 add_stat_sample (int type, uintptr_t ip) {
627 if (num_stat_samples == size_stat_samples) {
628 size_stat_samples *= 2;
629 if (!size_stat_samples)
630 size_stat_samples = 32;
631 stat_samples = realloc (stat_samples, size_stat_samples * sizeof (uintptr_t));
632 stat_sample_desc = realloc (stat_sample_desc, size_stat_samples * sizeof (int));
634 stat_samples [num_stat_samples] = ip;
635 stat_sample_desc [num_stat_samples++] = type;
639 lookup_method_by_ip (uintptr_t ip)
644 for (i = 0; i < HASH_SIZE; ++i) {
647 //printf ("checking %p against %p-%p\n", (void*)ip, (void*)(m->code), (void*)(m->code + m->len));
648 if (ip >= (uintptr_t)m->code && ip < (uintptr_t)m->code + m->len) {
658 compare_method_samples (const void *a, const void *b)
660 MethodDesc *const*A = a;
661 MethodDesc *const*B = b;
662 if ((*A)->sample_hits == (*B)->sample_hits)
664 if ((*B)->sample_hits < (*A)->sample_hits)
669 typedef struct _UnmanagedSymbol UnmanagedSymbol;
670 struct _UnmanagedSymbol {
671 UnmanagedSymbol *parent;
676 uintptr_t sample_hits;
679 static UnmanagedSymbol **usymbols = NULL;
680 static int usymbols_size = 0;
681 static int usymbols_num = 0;
684 compare_usymbol_addr (const void *a, const void *b)
686 UnmanagedSymbol *const*A = a;
687 UnmanagedSymbol *const*B = b;
688 if ((*B)->addr == (*A)->addr)
690 if ((*B)->addr > (*A)->addr)
696 compare_usymbol_samples (const void *a, const void *b)
698 UnmanagedSymbol *const*A = a;
699 UnmanagedSymbol *const*B = b;
700 if ((*B)->sample_hits == (*A)->sample_hits)
702 if ((*B)->sample_hits < (*A)->sample_hits)
708 add_unmanaged_symbol (uintptr_t addr, char *name, uintptr_t size)
710 UnmanagedSymbol *sym;
711 if (usymbols_num == usymbols_size) {
712 int new_size = usymbols_size * 2;
715 usymbols = realloc (usymbols, sizeof (void*) * new_size);
716 usymbols_size = new_size;
718 sym = calloc (sizeof (UnmanagedSymbol), 1);
722 usymbols [usymbols_num++] = sym;
725 /* only valid after the symbols are sorted */
726 static UnmanagedSymbol*
727 lookup_unmanaged_symbol (uintptr_t addr)
729 int r = usymbols_num - 1;
731 UnmanagedSymbol *sym;
736 if (addr == sym->addr)
738 if (addr < sym->addr) {
740 } else if (addr > sym->addr) {
745 if (last_best >= 0 && (addr - usymbols [last_best]->addr) < 4096)
746 return usymbols [last_best];
750 /* we use the same structure for binaries */
751 static UnmanagedSymbol **ubinaries = NULL;
752 static int ubinaries_size = 0;
753 static int ubinaries_num = 0;
756 add_unmanaged_binary (uintptr_t addr, char *name, uintptr_t size)
758 UnmanagedSymbol *sym;
759 if (ubinaries_num == ubinaries_size) {
760 int new_size = ubinaries_size * 2;
763 ubinaries = realloc (ubinaries, sizeof (void*) * new_size);
764 ubinaries_size = new_size;
766 sym = calloc (sizeof (UnmanagedSymbol), 1);
771 ubinaries [ubinaries_num++] = sym;
774 static UnmanagedSymbol*
775 lookup_unmanaged_binary (uintptr_t addr)
778 for (i = 0; i < ubinaries_num; ++i) {
779 UnmanagedSymbol *ubin = ubinaries [i];
780 if (addr >= ubin->addr && addr < ubin->addr + ubin->size) {
788 sample_type_name (int type)
791 case SAMPLE_CYCLES: return "cycles";
792 case SAMPLE_INSTRUCTIONS: return "instructions retired";
793 case SAMPLE_CACHE_MISSES: return "cache misses";
794 case SAMPLE_CACHE_REFS: return "cache references";
795 case SAMPLE_BRANCHES: return "executed branches";
796 case SAMPLE_BRANCH_MISSES: return "unpredicted branches";
802 set_usym_parent (UnmanagedSymbol** cachedus, int count)
805 for (i = 0; i < count; ++i) {
806 UnmanagedSymbol *ubin = lookup_unmanaged_binary (cachedus [i]->addr);
807 if (ubin == cachedus [i])
809 cachedus [i]->parent = ubin;
814 print_usym (UnmanagedSymbol* um)
817 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);
819 fprintf (outfile, "\t%6d %6.2f %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name);
823 sym_percent (uintptr_t sample_hits)
828 pc = sample_hits*100.0/num_stat_samples;
836 int count = 0, msize = 0;
837 int unmanaged_hits = 0;
838 int unresolved_hits = 0;
839 MethodDesc** cachedm = NULL;
840 int ucount = 0, usize = 0;
841 UnmanagedSymbol** cachedus = NULL;
842 if (!num_stat_samples)
844 qsort (usymbols, usymbols_num, sizeof (UnmanagedSymbol*), compare_usymbol_addr);
845 for (i = 0; i < num_stat_samples; ++i) {
846 MethodDesc *m = lookup_method_by_ip (stat_samples [i]);
848 if (!m->sample_hits) {
849 if (count == msize) {
853 cachedm = realloc (cachedm, sizeof (void*) * msize);
855 cachedm [count++] = m;
859 UnmanagedSymbol *usym = lookup_unmanaged_symbol (stat_samples [i]);
862 //printf ("unmanaged hit at %p\n", (void*)stat_samples [i]);
863 usym = lookup_unmanaged_binary (stat_samples [i]);
866 if (!usym->sample_hits) {
867 if (ucount == usize) {
871 cachedus = realloc (cachedus, sizeof (void*) * usize);
873 cachedus [ucount++] = usym;
880 qsort (cachedm, count, sizeof (MethodDesc*), compare_method_samples);
881 qsort (cachedus, ucount, sizeof (UnmanagedSymbol*), compare_usymbol_samples);
882 set_usym_parent (cachedus, ucount);
883 fprintf (outfile, "\nStatistical samples summary\n");
884 fprintf (outfile, "\tSample type: %s\n", sample_type_name (stat_sample_desc [0]));
885 fprintf (outfile, "\tUnmanaged hits: %6d (%4.1f%%)\n", unmanaged_hits, (100.0*unmanaged_hits)/num_stat_samples);
886 fprintf (outfile, "\tManaged hits: %6d (%4.1f%%)\n", num_stat_samples - unmanaged_hits, (100.0*(num_stat_samples-unmanaged_hits))/num_stat_samples);
887 fprintf (outfile, "\tUnresolved hits: %6d (%4.1f%%)\n", unresolved_hits, (100.0*unresolved_hits)/num_stat_samples);
888 fprintf (outfile, "\t%6s %6s %s\n", "Hits", "%", "Method name");
891 while (i < count || u < ucount) {
893 MethodDesc *m = cachedm [i];
895 UnmanagedSymbol *um = cachedus [u];
896 if (um->sample_hits > m->sample_hits) {
897 if (!sym_percent (um->sample_hits))
904 if (!sym_percent (m->sample_hits))
906 fprintf (outfile, "\t%6d %6.2f %s\n", m->sample_hits, m->sample_hits*100.0/num_stat_samples, m->name);
911 UnmanagedSymbol *um = cachedus [u];
912 if (!sym_percent (um->sample_hits))
921 typedef struct _HeapClassDesc HeapClassDesc;
923 HeapClassDesc *klass;
927 struct _HeapClassDesc {
931 HeapClassRevRef *rev_hash;
934 uintptr_t pinned_references;
935 uintptr_t root_references;
939 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
943 start_pos = (hklass->klass->klass >> 2) % size;
944 assert (start_pos < size);
947 if (rev_hash [i].klass == hklass) {
948 rev_hash [i].count += value;
950 } else if (!rev_hash [i].klass) {
951 rev_hash [i].klass = hklass;
952 rev_hash [i].count += value;
954 for (i = 0; i < size; ++i)
955 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
957 assert (start_pos == 1);
963 } while (i != start_pos);
964 /* should not happen */
965 printf ("failed revref store\n");
970 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
973 if (to->rev_count * 2 >= to->rev_hash_size) {
975 uintptr_t old_size = to->rev_hash_size;
976 to->rev_hash_size *= 2;
977 if (to->rev_hash_size == 0)
978 to->rev_hash_size = 4;
979 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
980 for (i = 0; i < old_size; ++i) {
981 if (to->rev_hash [i].klass)
982 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
988 to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
993 HeapClassDesc *hklass;
998 typedef struct _HeapShot HeapShot;
1004 HeapClassDesc **class_hash;
1005 HeapClassDesc **sorted;
1006 HeapObjectDesc **objects_hash;
1007 uintptr_t objects_count;
1008 uintptr_t objects_hash_size;
1009 uintptr_t num_roots;
1011 uintptr_t *roots_extra;
1015 static HeapShot *heap_shots = NULL;
1016 static int num_heap_shots = 0;
1019 new_heap_shot (uint64_t timestamp)
1021 HeapShot *hs = calloc (sizeof (HeapShot), 1);
1023 hs->class_hash = calloc (sizeof (void*), hs->hash_size);
1024 hs->timestamp = timestamp;
1026 hs->next = heap_shots;
1031 static HeapClassDesc*
1032 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
1035 unsigned int start_pos;
1036 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
1039 HeapClassDesc* cd = hs->class_hash [i];
1042 if (cd->klass == klass)
1045 if (++i == hs->hash_size)
1047 } while (i != start_pos);
1052 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
1055 uintptr_t start_pos;
1056 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
1059 if (hash [i] && hash [i]->klass == klass) {
1060 hash [i]->total_size += size;
1061 hash [i]->count += count;
1064 } else if (!hash [i]) {
1069 hash [i] = calloc (sizeof (HeapClassDesc), 1);
1070 hash [i]->klass = klass;
1071 hash [i]->total_size += size;
1072 hash [i]->count += count;
1079 } while (i != start_pos);
1080 /* should not happen */
1081 printf ("failed heap class store\n");
1085 static HeapClassDesc*
1086 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
1090 if (hs->class_count * 2 >= hs->hash_size) {
1092 int old_size = hs->hash_size;
1094 if (hs->hash_size == 0)
1096 n = calloc (sizeof (void*) * hs->hash_size, 1);
1097 for (i = 0; i < old_size; ++i) {
1098 res = hs->class_hash [i];
1099 if (hs->class_hash [i])
1100 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
1103 free (hs->class_hash);
1107 hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
1108 //if (res->count == 1)
1109 // printf ("added heap class: %s\n", res->klass->name);
1113 static HeapObjectDesc*
1114 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
1116 HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
1117 ho->objaddr = objaddr;
1118 ho->hklass = hklass;
1119 ho->num_refs = num_refs;
1124 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
1127 uintptr_t start_pos;
1128 HeapObjectDesc **hash = hs->objects_hash;
1129 start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
1132 if (hash [i] && hash [i]->objaddr == objaddr) {
1134 } else if (!hash [i]) {
1138 if (++i == hs->objects_hash_size)
1140 } while (i != start_pos);
1141 /* should not happen */
1142 //printf ("failed heap obj slot\n");
1146 static HeapObjectDesc*
1147 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
1149 HeapObjectDesc **hash = hs->objects_hash;
1150 uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
1152 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
1153 *ref_offset = hash [i]->num_refs;
1154 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
1159 /* should not happen */
1160 printf ("failed heap obj update\n");
1166 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
1169 uintptr_t start_pos;
1170 start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
1173 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
1174 printf ("duplicate object!\n");
1176 } else if (!hash [i]) {
1183 } while (i != start_pos);
1184 /* should not happen */
1185 printf ("failed heap obj store\n");
1190 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
1193 if (hs->objects_count * 2 >= hs->objects_hash_size) {
1195 uintptr_t old_size = hs->objects_hash_size;
1196 hs->objects_hash_size *= 2;
1197 if (hs->objects_hash_size == 0)
1198 hs->objects_hash_size = 4;
1199 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
1200 for (i = 0; i < old_size; ++i) {
1201 if (hs->objects_hash [i])
1202 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
1204 if (hs->objects_hash)
1205 free (hs->objects_hash);
1206 hs->objects_hash = n;
1208 hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
1212 heap_shot_resolve_reverse_refs (HeapShot *hs)
1215 for (i = 0; i < hs->objects_hash_size; ++i) {
1217 HeapObjectDesc *ho = hs->objects_hash [i];
1220 for (r = 0; r < ho->num_refs; ++r) {
1221 uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
1222 add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
1228 #define MARK_BLACK 2
1231 heap_shot_mark_objects (HeapShot *hs)
1234 unsigned char *marks;
1235 HeapObjectDesc *obj, *ref;
1237 uintptr_t num_marked = 0, num_unmarked;
1238 for (i = 0; i < hs->num_roots; ++i) {
1240 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
1244 obj = hs->objects_hash [oi];
1246 if (hs->roots_types [i] & MONO_PROFILE_GC_ROOT_PINNING)
1247 cd->pinned_references++;
1248 cd->root_references++;
1252 /* consistency checks: it seems not all the objects are walked in the heap in some cases */
1253 marks = calloc (hs->objects_hash_size, 1);
1256 for (i = 0; i < hs->num_roots; ++i) {
1257 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
1259 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);
1262 obj = hs->objects_hash [oi];
1264 marks [oi] = obj->num_refs? MARK_GRAY: MARK_BLACK;
1269 while (marked_some) {
1271 for (i = 0; i < hs->objects_hash_size; ++i) {
1272 if (marks [i] != MARK_GRAY)
1274 marks [i] = MARK_BLACK;
1275 obj = hs->objects_hash [i];
1276 for (r = 0; r < obj->num_refs; ++r) {
1277 oi = heap_shot_find_obj_slot (hs, obj->refs [r]);
1279 fprintf (outfile, "referenced obj %p not found in heap\n", (void*)obj->refs [r]);
1282 ref = hs->objects_hash [oi];
1284 marks [oi] = ref->num_refs? MARK_GRAY: MARK_BLACK;
1292 for (i = 0; i < hs->objects_hash_size; ++i) {
1293 if (hs->objects_hash [i] && !marks [i]) {
1295 fprintf (outfile, "object %p (%s) unmarked\n", (void*)hs->objects_hash [i], hs->objects_hash [i]->hklass->klass->name);
1298 fprintf (outfile, "Total unmarked: %d/%d\n", num_unmarked, hs->objects_count);
1303 heap_shot_free_objects (HeapShot *hs)
1306 for (i = 0; i < hs->objects_hash_size; ++i) {
1307 HeapObjectDesc *ho = hs->objects_hash [i];
1311 if (hs->objects_hash)
1312 free (hs->objects_hash);
1313 hs->objects_hash = NULL;
1314 hs->objects_hash_size = 0;
1315 hs->objects_count = 0;
1324 MethodDesc *methods [1];
1327 static BackTrace *backtrace_hash [HASH_SIZE];
1328 static BackTrace **backtraces = NULL;
1329 static int num_backtraces = 0;
1330 static int next_backtrace = 0;
1333 hash_backtrace (int count, MethodDesc **methods)
1337 for (i = 0; i < count; ++i) {
1338 hash = (hash << 5) - hash + methods [i]->method;
1344 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
1347 if (bt->count != count)
1349 for (i = 0; i < count; ++i)
1350 if (methods [i] != bt->methods [i])
1356 add_backtrace (int count, MethodDesc **methods)
1358 int hash = hash_backtrace (count, methods);
1359 int slot = (hash & 0xffff) % HASH_SIZE;
1360 BackTrace *bt = backtrace_hash [slot];
1362 if (bt->hash == hash && compare_backtrace (bt, count, methods))
1366 bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
1367 bt->next = backtrace_hash [slot];
1368 backtrace_hash [slot] = bt;
1369 if (next_backtrace == num_backtraces) {
1370 num_backtraces *= 2;
1371 if (!num_backtraces)
1372 num_backtraces = 16;
1373 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
1375 bt->id = next_backtrace++;
1376 backtraces [bt->id] = bt;
1379 for (slot = 0; slot < count; ++slot)
1380 bt->methods [slot] = methods [slot];
1385 typedef struct _MonitorDesc MonitorDesc;
1386 typedef struct _ThreadContext ThreadContext;
1390 #if defined (HAVE_SYS_ZLIB)
1401 uint64_t startup_time;
1402 ThreadContext *threads;
1403 ThreadContext *current;
1406 struct _ThreadContext {
1407 ThreadContext *next;
1410 /* emulated stack */
1412 uint64_t *time_stack;
1413 uint64_t *callee_time_stack;
1415 uint64_t contention_start;
1416 MonitorDesc *monitor;
1419 HeapShot *current_heap_shot;
1420 uintptr_t num_roots;
1421 uintptr_t size_roots;
1423 uintptr_t *roots_extra;
1425 uint64_t gc_start_times [3];
1429 ensure_buffer (ProfContext *ctx, int size)
1431 if (ctx->size < size) {
1432 ctx->buf = realloc (ctx->buf, size);
1438 load_data (ProfContext *ctx, int size)
1440 ensure_buffer (ctx, size);
1441 #if defined (HAVE_SYS_ZLIB)
1443 int r = gzread (ctx->gzfile, ctx->buf, size);
1445 return size == 0? 1: 0;
1450 int r = fread (ctx->buf, size, 1, ctx->file);
1452 return size == 0? 1: 0;
1457 static ThreadContext*
1458 get_thread (ProfContext *ctx, intptr_t thread_id)
1460 ThreadContext *thread;
1461 if (ctx->current && ctx->current->thread_id == thread_id)
1462 return ctx->current;
1463 thread = ctx->threads;
1465 if (thread->thread_id == thread_id) {
1468 thread = thread->next;
1470 thread = calloc (sizeof (ThreadContext), 1);
1471 thread->next = ctx->threads;
1472 ctx->threads = thread;
1473 thread->thread_id = thread_id;
1474 thread->last_time = 0;
1475 thread->stack_id = 0;
1476 thread->stack_size = 32;
1477 thread->stack = malloc (thread->stack_size * sizeof (void*));
1478 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1479 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1483 static ThreadContext*
1484 load_thread (ProfContext *ctx, intptr_t thread_id)
1486 ThreadContext *thread = get_thread (ctx, thread_id);
1487 ctx->current = thread;
1492 ensure_thread_stack (ThreadContext *thread)
1494 if (thread->stack_id == thread->stack_size) {
1495 thread->stack_size *= 2;
1496 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
1497 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
1498 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
1503 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
1506 unsigned int start_pos;
1507 start_pos = bt->hash % size;
1510 if (traces [i].bt == bt) {
1511 traces [i].count += value;
1513 } else if (!traces [i].bt) {
1515 traces [i].count += value;
1521 } while (i != start_pos);
1522 /* should not happen */
1523 printf ("failed trace store\n");
1528 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
1531 if (!collect_traces)
1533 if (trace->count * 2 >= trace->size) {
1535 int old_size = trace->size;
1537 if (trace->size == 0)
1539 n = calloc (sizeof (CallContext) * trace->size, 1);
1540 for (i = 0; i < old_size; ++i) {
1541 if (trace->traces [i].bt)
1542 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
1545 free (trace->traces);
1548 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
1552 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
1555 int count = thread->stack_id;
1556 if (!collect_traces)
1558 if (count > trace_max)
1560 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
1561 add_trace_bt (bt, trace, value);
1566 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
1569 if (!collect_traces)
1571 if (count > trace_max)
1573 bt = add_backtrace (count, methods);
1574 add_trace_bt (bt, trace, value);
1579 thread_add_root (ThreadContext *ctx, uintptr_t obj, int root_type, uintptr_t extra_info)
1581 if (ctx->num_roots == ctx->size_roots) {
1582 int new_size = ctx->size_roots * 2;
1585 ctx->roots = realloc (ctx->roots, new_size * sizeof (uintptr_t));
1586 ctx->roots_extra = realloc (ctx->roots_extra, new_size * sizeof (uintptr_t));
1587 ctx->roots_types = realloc (ctx->roots_types, new_size * sizeof (int));
1588 ctx->size_roots = new_size;
1590 ctx->roots_types [ctx->num_roots] = root_type;
1591 ctx->roots_extra [ctx->num_roots] = extra_info;
1592 ctx->roots [ctx->num_roots++] = obj;
1596 compare_callc (const void *a, const void *b)
1598 const CallContext *A = a;
1599 const CallContext *B = b;
1600 if (B->count == A->count)
1602 if (B->count < A->count)
1608 sort_context_array (TraceDesc* traces)
1611 for (i = 0, j = 0; i < traces->size; ++i) {
1612 if (traces->traces [i].bt) {
1613 traces->traces [j].bt = traces->traces [i].bt;
1614 traces->traces [j].count = traces->traces [i].count;
1618 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
1622 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1624 ensure_thread_stack (thread);
1625 thread->time_stack [thread->stack_id] = timestamp;
1626 thread->callee_time_stack [thread->stack_id] = 0;
1627 thread->stack [thread->stack_id++] = method;
1628 method->recurse_count++;
1632 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1634 method->recurse_count--;
1635 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
1639 if (timestamp < thread->time_stack [thread->stack_id])
1640 fprintf (outfile, "time went backwards for %s\n", method->name);
1641 tdiff = timestamp - thread->time_stack [thread->stack_id];
1642 if (thread->callee_time_stack [thread->stack_id] > tdiff)
1643 fprintf (outfile, "callee time bigger for %s\n", method->name);
1644 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
1645 method->callee_time += thread->callee_time_stack [thread->stack_id];
1646 if (thread->stack_id)
1647 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
1648 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
1650 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
1655 uint64_t total_time;
1659 static GCDesc gc_info [3];
1660 static uint64_t max_heap_size;
1661 static uint64_t gc_object_moves;
1662 static int gc_resizes;
1670 static HandleInfo handle_info [4];
1673 gc_event_name (int ev)
1676 case MONO_GC_EVENT_START: return "start";
1677 case MONO_GC_EVENT_MARK_START: return "mark start";
1678 case MONO_GC_EVENT_MARK_END: return "mark end";
1679 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
1680 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
1681 case MONO_GC_EVENT_END: return "end";
1682 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
1683 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
1684 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
1685 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
1691 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
1692 static uint64_t throw_count = 0;
1693 static TraceDesc exc_traces;
1696 clause_name (int type)
1699 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
1700 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
1701 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
1702 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
1703 default: return "invalid";
1707 static uint64_t monitor_contention;
1708 static uint64_t monitor_failed;
1709 static uint64_t monitor_acquired;
1711 struct _MonitorDesc {
1714 uintptr_t contentions;
1716 uint64_t max_wait_time;
1720 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
1721 static int num_monitors = 0;
1724 lookup_monitor (uintptr_t objid)
1726 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
1727 MonitorDesc *cd = monitor_hash [slot];
1728 while (cd && cd->objid != objid)
1731 cd = calloc (sizeof (MonitorDesc), 1);
1733 cd->next = monitor_hash [slot];
1734 monitor_hash [slot] = cd;
1741 monitor_ev_name (int ev)
1744 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
1745 case MONO_PROFILER_MONITOR_DONE: return "acquired";
1746 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
1747 default: return "invalid";
1752 get_handle_name (int htype)
1755 case 0: return "weak";
1756 case 1: return "weaktrack";
1757 case 2: return "normal";
1758 case 3: return "pinned";
1759 default: return "unknown";
1764 get_root_name (int rtype)
1766 switch (rtype & MONO_PROFILE_GC_ROOT_TYPEMASK) {
1767 case MONO_PROFILE_GC_ROOT_STACK: return "stack";
1768 case MONO_PROFILE_GC_ROOT_FINALIZER: return "finalizer";
1769 case MONO_PROFILE_GC_ROOT_HANDLE: return "handle";
1770 case MONO_PROFILE_GC_ROOT_OTHER: return "other";
1771 case MONO_PROFILE_GC_ROOT_MISC: return "misc";
1772 default: return "unknown";
1777 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
1779 MethodDesc **frames;
1781 int flags = decode_uleb128 (p, &p);
1782 int count = decode_uleb128 (p, &p);
1786 frames = malloc (count * sizeof (void*));
1789 for (i = 0; i < count; ++i) {
1790 intptr_t ptrdiff = decode_sleb128 (p, &p);
1791 frames [i] = lookup_method (ptr_base + ptrdiff);
1799 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
1802 for (i = 0; i < num_tracked_objects; ++i) {
1803 if (tracked_objects [i] != obj)
1805 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
1806 if (bt && bt->count) {
1808 for (k = 0; k < bt->count; ++k)
1809 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
1815 track_handle (uintptr_t obj, int htype, uint32_t handle)
1818 for (i = 0; i < num_tracked_objects; ++i) {
1819 if (tracked_objects [i] == obj)
1820 fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1825 track_move (uintptr_t src, uintptr_t dst)
1828 for (i = 0; i < num_tracked_objects; ++i) {
1829 if (tracked_objects [i] == src)
1830 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1831 else if (tracked_objects [i] == dst)
1832 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1837 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1840 for (i = 0; i < num_tracked_objects; ++i) {
1841 if (tracked_objects [i] == obj)
1842 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1847 found_object (uintptr_t obj)
1849 num_tracked_objects ++;
1850 tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1851 tracked_objects [num_tracked_objects - 1] = obj;
1854 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
1855 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
1858 decode_buffer (ProfContext *ctx)
1865 intptr_t method_base;
1867 uint64_t file_offset;
1869 ThreadContext *thread;
1871 #ifdef HAVE_SYS_ZLIB
1873 file_offset = gztell (ctx->gzfile);
1876 file_offset = ftell (ctx->file);
1877 if (!load_data (ctx, 48))
1880 if (read_int32 (p) != BUF_ID) {
1881 fprintf (outfile, "Incorrect buffer id: 0x%x\n", read_int32 (p));
1882 for (i = 0; i < 48; ++i) {
1883 fprintf (outfile, "0x%x%s", p [i], i % 8?" ":"\n");
1887 len = read_int32 (p + 4);
1888 time_base = read_int64 (p + 8);
1889 ptr_base = read_int64 (p + 16);
1890 obj_base = read_int64 (p + 24);
1891 thread_id = read_int64 (p + 32);
1892 method_base = read_int64 (p + 40);
1894 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
1895 thread = load_thread (ctx, thread_id);
1896 if (!load_data (ctx, len))
1898 if (!startup_time) {
1899 startup_time = time_base;
1900 if (use_time_filter) {
1901 time_from += startup_time;
1902 time_to += startup_time;
1905 thread->name = pstrdup ("Main");
1907 for (i = 0; i < thread->stack_id; ++i)
1908 thread->stack [i]->recurse_count++;
1914 int subtype = *p & 0xf0;
1915 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1916 LOG_TIME (time_base, tdiff);
1918 if (subtype == TYPE_GC_RESIZE) {
1919 uint64_t new_size = decode_uleb128 (p, &p);
1921 fprintf (outfile, "gc heap resized to %llu\n", new_size);
1923 if (new_size > max_heap_size)
1924 max_heap_size = new_size;
1925 } else if (subtype == TYPE_GC_EVENT) {
1926 uint64_t ev = decode_uleb128 (p, &p);
1927 int gen = decode_uleb128 (p, &p);
1929 fprintf (outfile, "gc event for gen%d: %s at %llu (thread: 0x%x)\n", gen, gc_event_name (ev), time_base, thread->thread_id);
1931 fprintf (outfile, "incorrect gc gen: %d\n", gen);
1934 if (ev == MONO_GC_EVENT_START) {
1935 thread->gc_start_times [gen] = time_base;
1936 gc_info [gen].count++;
1937 } else if (ev == MONO_GC_EVENT_END) {
1938 tdiff = time_base - thread->gc_start_times [gen];
1939 gc_info [gen].total_time += tdiff;
1940 if (tdiff > gc_info [gen].max_time)
1941 gc_info [gen].max_time = tdiff;
1943 } else if (subtype == TYPE_GC_MOVE) {
1944 int j, num = decode_uleb128 (p, &p);
1945 gc_object_moves += num / 2;
1946 for (j = 0; j < num; j += 2) {
1947 intptr_t obj1diff = decode_sleb128 (p, &p);
1948 intptr_t obj2diff = decode_sleb128 (p, &p);
1949 if (num_tracked_objects)
1950 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
1952 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
1955 } else if (subtype == TYPE_GC_HANDLE_CREATED) {
1956 int htype = decode_uleb128 (p, &p);
1957 uint32_t handle = decode_uleb128 (p, &p);
1958 intptr_t objdiff = decode_sleb128 (p, &p);
1961 handle_info [htype].created++;
1962 handle_info [htype].live++;
1963 add_trace_thread (thread, &handle_info [htype].traces, 1);
1964 /* FIXME: we don't take into account timing here */
1965 if (handle_info [htype].live > handle_info [htype].max_live)
1966 handle_info [htype].max_live = handle_info [htype].live;
1967 if (num_tracked_objects)
1968 track_handle (OBJ_ADDR (objdiff), htype, handle);
1970 fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
1971 } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
1972 int htype = decode_uleb128 (p, &p);
1973 uint32_t handle = decode_uleb128 (p, &p);
1976 handle_info [htype].destroyed ++;
1977 handle_info [htype].live--;
1979 fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
1983 case TYPE_METADATA: {
1984 int error = *p & TYPE_LOAD_ERR;
1985 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1987 intptr_t ptrdiff = decode_sleb128 (p, &p);
1988 LOG_TIME (time_base, tdiff);
1990 if (mtype == TYPE_CLASS) {
1991 intptr_t imptrdiff = decode_sleb128 (p, &p);
1992 uint64_t flags = decode_uleb128 (p, &p);
1994 fprintf (outfile, "non-zero flags in class\n");
1998 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
2000 add_class (ptr_base + ptrdiff, (char*)p);
2003 } else if (mtype == TYPE_IMAGE) {
2004 uint64_t flags = decode_uleb128 (p, &p);
2006 fprintf (outfile, "non-zero flags in image\n");
2010 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
2012 add_image (ptr_base + ptrdiff, (char*)p);
2015 } else if (mtype == TYPE_THREAD) {
2017 uint64_t flags = decode_uleb128 (p, &p);
2019 fprintf (outfile, "non-zero flags in thread\n");
2022 nt = get_thread (ctx, ptr_base + ptrdiff);
2023 nt->name = pstrdup ((char*)p);
2025 fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
2032 int has_bt = *p & TYPE_ALLOC_BT;
2033 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2034 intptr_t ptrdiff = decode_sleb128 (p, &p);
2035 intptr_t objdiff = decode_sleb128 (p, &p);
2038 MethodDesc* sframes [8];
2039 MethodDesc** frames = sframes;
2040 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
2041 len = decode_uleb128 (p, &p);
2042 LOG_TIME (time_base, tdiff);
2045 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
2048 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2050 fprintf (outfile, "Cannot load backtrace\n");
2054 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
2057 cd->alloc_size += len;
2059 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
2061 bt = add_trace_thread (thread, &cd->traces, len);
2062 if (find_size && len >= find_size) {
2063 if (!find_name || strstr (cd->name, find_name))
2064 found_object (OBJ_ADDR (objdiff));
2065 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
2066 found_object (OBJ_ADDR (objdiff));
2068 if (num_tracked_objects)
2069 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
2071 if (frames != sframes)
2076 int subtype = *p & 0xf0;
2077 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2078 int64_t ptrdiff = decode_sleb128 (p, &p);
2079 LOG_TIME (time_base, tdiff);
2081 method_base += ptrdiff;
2082 if (subtype == TYPE_JIT) {
2083 intptr_t codediff = decode_sleb128 (p, &p);
2084 int codelen = decode_uleb128 (p, &p);
2086 fprintf (outfile, "jitted method %p (%s), size: %d, code: %p\n", (void*)(method_base), p, codelen, (void*)(ptr_base + codediff));
2087 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
2092 if ((thread_filter && thread_filter != thread->thread_id))
2094 method = lookup_method (method_base);
2095 if (subtype == TYPE_ENTER) {
2096 add_trace_thread (thread, &method->traces, 1);
2097 push_method (thread, method, time_base);
2099 pop_method (thread, method, time_base);
2102 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
2107 int subtype = *p & 0xf0;
2108 if (subtype == TYPE_HEAP_OBJECT) {
2111 intptr_t objdiff = decode_sleb128 (p + 1, &p);
2112 intptr_t ptrdiff = decode_sleb128 (p, &p);
2113 uint64_t size = decode_uleb128 (p, &p);
2114 uintptr_t num = decode_uleb128 (p, &p);
2115 uintptr_t ref_offset;
2116 uintptr_t last_obj_offset = 0;
2117 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
2119 HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
2120 if (collect_traces) {
2121 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
2122 add_heap_shot_obj (thread->current_heap_shot, ho);
2127 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
2129 for (i = 0; i < num; ++i) {
2130 /* FIXME: use object distance to measure how good
2131 * the GC is at keeping related objects close
2133 uintptr_t offset = ctx->data_version > 1? last_obj_offset + decode_uleb128 (p, &p): -1;
2134 intptr_t obj1diff = decode_sleb128 (p, &p);
2135 last_obj_offset = offset;
2137 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
2138 if (num_tracked_objects)
2139 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
2142 fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
2143 } else if (subtype == TYPE_HEAP_ROOT) {
2144 uintptr_t num = decode_uleb128 (p + 1, &p);
2145 uintptr_t gc_num = decode_uleb128 (p, &p);
2147 for (i = 0; i < num; ++i) {
2148 intptr_t objdiff = decode_sleb128 (p, &p);
2149 int root_type = decode_uleb128 (p, &p);
2150 /* we just discard the extra info for now */
2151 uintptr_t extra_info = decode_uleb128 (p, &p);
2153 fprintf (outfile, "object %p is a %s root\n", (void*)OBJ_ADDR (objdiff), get_root_name (root_type));
2155 thread_add_root (thread, OBJ_ADDR (objdiff), root_type, extra_info);
2157 } else if (subtype == TYPE_HEAP_END) {
2158 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2159 LOG_TIME (time_base, tdiff);
2162 fprintf (outfile, "heap shot end\n");
2163 if (collect_traces) {
2164 HeapShot *hs = thread->current_heap_shot;
2165 if (hs && thread->num_roots) {
2166 /* transfer the root ownershipt to the heapshot */
2167 hs->num_roots = thread->num_roots;
2168 hs->roots = thread->roots;
2169 hs->roots_extra = thread->roots_extra;
2170 hs->roots_types = thread->roots_types;
2172 free (thread->roots);
2173 free (thread->roots_extra);
2174 free (thread->roots_types);
2176 thread->num_roots = 0;
2177 thread->size_roots = 0;
2178 thread->roots = NULL;
2179 thread->roots_extra = NULL;
2180 thread->roots_types = NULL;
2181 heap_shot_resolve_reverse_refs (hs);
2182 heap_shot_mark_objects (hs);
2183 heap_shot_free_objects (hs);
2185 thread->current_heap_shot = NULL;
2186 } else if (subtype == TYPE_HEAP_START) {
2187 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2188 LOG_TIME (time_base, tdiff);
2191 fprintf (outfile, "heap shot start\n");
2192 thread->current_heap_shot = new_heap_shot (time_base);
2196 case TYPE_MONITOR: {
2197 int event = (*p >> 4) & 0x3;
2198 int has_bt = *p & TYPE_MONITOR_BT;
2199 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2200 intptr_t objdiff = decode_sleb128 (p, &p);
2201 MethodDesc* sframes [8];
2202 MethodDesc** frames = sframes;
2205 LOG_TIME (time_base, tdiff);
2207 record = (!thread_filter || thread_filter == thread->thread_id);
2208 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
2209 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
2211 monitor_contention++;
2212 mdesc->contentions++;
2213 thread->monitor = mdesc;
2214 thread->contention_start = time_base;
2218 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2220 fprintf (outfile, "Cannot load backtrace\n");
2224 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
2227 add_trace_thread (thread, &mdesc->traces, 1);
2229 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
2232 if (thread->monitor && thread->contention_start) {
2233 uint64_t wait_time = time_base - thread->contention_start;
2234 if (wait_time > thread->monitor->max_wait_time)
2235 thread->monitor->max_wait_time = wait_time;
2236 thread->monitor->wait_time += wait_time;
2237 thread->monitor = NULL;
2238 thread->contention_start = 0;
2241 } else if (event == MONO_PROFILER_MONITOR_DONE) {
2244 if (thread->monitor && thread->contention_start) {
2245 uint64_t wait_time = time_base - thread->contention_start;
2246 if (wait_time > thread->monitor->max_wait_time)
2247 thread->monitor->max_wait_time = wait_time;
2248 thread->monitor->wait_time += wait_time;
2249 thread->monitor = NULL;
2250 thread->contention_start = 0;
2255 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
2256 if (frames != sframes)
2260 case TYPE_EXCEPTION: {
2261 int subtype = *p & 0x70;
2262 int has_bt = *p & TYPE_EXCEPTION_BT;
2263 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2264 MethodDesc* sframes [8];
2265 MethodDesc** frames = sframes;
2267 LOG_TIME (time_base, tdiff);
2269 record = (!thread_filter || thread_filter == thread->thread_id);
2270 if (subtype == TYPE_CLAUSE) {
2271 int clause_type = decode_uleb128 (p, &p);
2272 int clause_num = decode_uleb128 (p, &p);
2273 int64_t ptrdiff = decode_sleb128 (p, &p);
2274 method_base += ptrdiff;
2276 clause_summary [clause_type]++;
2278 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
2280 intptr_t objdiff = decode_sleb128 (p, &p);
2285 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
2287 fprintf (outfile, "Cannot load backtrace\n");
2291 add_trace_methods (frames, has_bt, &exc_traces, 1);
2294 add_trace_thread (thread, &exc_traces, 1);
2296 if (frames != sframes)
2299 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
2304 int subtype = *p & 0xf0;
2305 if (subtype == TYPE_SAMPLE_HIT) {
2307 int sample_type = decode_uleb128 (p + 1, &p);
2308 uint64_t tstamp = decode_uleb128 (p, &p);
2309 int count = decode_uleb128 (p, &p);
2310 for (i = 0; i < count; ++i) {
2311 uintptr_t ip = ptr_base + decode_sleb128 (p, &p);
2312 add_stat_sample (sample_type, ip);
2314 fprintf (outfile, "sample hit, type: %d at %p\n", sample_type, (void*)ip);
2316 } else if (subtype == TYPE_SAMPLE_USYM) {
2317 /* un unmanaged symbol description */
2318 uintptr_t addr = ptr_base + decode_sleb128 (p + 1, &p);
2319 uintptr_t size = decode_uleb128 (p, &p);
2321 name = pstrdup ((char*)p);
2322 add_unmanaged_symbol (addr, name, size);
2324 fprintf (outfile, "unmanaged symbol %s at %p\n", name, (void*)addr);
2327 } else if (subtype == TYPE_SAMPLE_UBIN) {
2328 /* un unmanaged binary loaded in memory */
2329 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2330 uintptr_t addr = decode_sleb128 (p, &p);
2331 uint64_t offset = decode_uleb128 (p, &p);
2332 uintptr_t size = decode_uleb128 (p, &p);
2334 LOG_TIME (time_base, tdiff);
2336 name = pstrdup ((char*)p);
2337 add_unmanaged_binary (addr, name, size);
2339 fprintf (outfile, "unmanaged binary %s at %p\n", name, (void*)addr);
2342 } else if (subtype == TYPE_SAMPLE_COUNTERS_DESC) {
2343 uint64_t i, len = decode_uleb128 (p + 1, &p);
2344 for (i = 0; i < len; i++) {
2345 uint64_t section = decode_uleb128 (p, &p);
2346 char *name = pstrdup ((char*)p);
2348 uint64_t type = decode_uleb128 (p, &p);
2349 uint64_t unit = decode_uleb128 (p, &p);
2350 uint64_t variance = decode_uleb128 (p, &p);
2351 uint64_t index = decode_uleb128 (p, &p);
2352 add_counter ((int)section, name, (int)type, (int)unit, (int)variance, (int)index);
2354 } else if (subtype == TYPE_SAMPLE_COUNTERS) {
2356 CounterValue *value, *previous = NULL;
2358 uint64_t timestamp = decode_uleb128 (p + 1, &p);
2359 uint64_t time_between = timestamp / 1000 * 1000 * 1000 * 1000 + startup_time;
2361 uint64_t index = decode_uleb128 (p, &p);
2365 for (list = counters; list; list = list->next) {
2366 if (list->counter->index == (int)index) {
2367 previous = list->counter->values_last;
2372 uint64_t type = decode_uleb128 (p, &p);
2374 value = calloc (1, sizeof (CounterValue));
2375 value->timestamp = timestamp;
2378 case MONO_COUNTER_INT:
2379 #if SIZEOF_VOID_P == 4
2380 case MONO_COUNTER_WORD:
2382 value->buffer = malloc (sizeof (int32_t));
2383 *(int32_t*)value->buffer = (int32_t)decode_sleb128 (p, &p) + (previous ? (*(int32_t*)previous->buffer) : 0);
2385 case MONO_COUNTER_UINT:
2386 value->buffer = malloc (sizeof (uint32_t));
2387 *(uint32_t*)value->buffer = (uint32_t)decode_uleb128 (p, &p) + (previous ? (*(uint32_t*)previous->buffer) : 0);
2389 case MONO_COUNTER_LONG:
2390 #if SIZEOF_VOID_P == 8
2391 case MONO_COUNTER_WORD:
2393 case MONO_COUNTER_TIME_INTERVAL:
2394 value->buffer = malloc (sizeof (int64_t));
2395 *(int64_t*)value->buffer = (int64_t)decode_sleb128 (p, &p) + (previous ? (*(int64_t*)previous->buffer) : 0);
2397 case MONO_COUNTER_ULONG:
2398 value->buffer = malloc (sizeof (uint64_t));
2399 *(uint64_t*)value->buffer = (uint64_t)decode_uleb128 (p, &p) + (previous ? (*(uint64_t*)previous->buffer) : 0);
2401 case MONO_COUNTER_DOUBLE:
2402 value->buffer = malloc (sizeof (double));
2403 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
2404 for (i = 0; i < sizeof (double); i++)
2406 for (i = sizeof (double) - 1; i >= 0; i--)
2408 value->buffer[i] = *p++;
2410 case MONO_COUNTER_STRING:
2412 value->buffer = NULL;
2414 value->buffer = (unsigned char*) pstrdup ((char*)p);
2419 if (time_between >= time_from && time_between <= time_to)
2420 add_counter_value (index, value);
2428 fprintf (outfile, "unhandled profiler event: 0x%x at file offset: %llu + %d (len: %d\n)\n", *p, file_offset, p - ctx->buf, len);
2432 thread->last_time = time_base;
2433 for (i = 0; i < thread->stack_id; ++i)
2434 thread->stack [i]->recurse_count = 0;
2439 load_file (char *name)
2442 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
2443 if (strcmp (name, "-") == 0)
2446 ctx->file = fopen (name, "rb");
2448 printf ("Cannot open file: %s\n", name);
2451 #if defined (HAVE_SYS_ZLIB)
2452 if (ctx->file != stdin)
2453 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
2455 if (!load_data (ctx, 32))
2458 if (read_int32 (p) != LOG_HEADER_ID || p [6] > LOG_DATA_VERSION)
2460 ctx->version_major = p [4];
2461 ctx->version_minor = p [5];
2462 ctx->data_version = p [6];
2463 /* reading 64 bit files on 32 bit systems not supported yet */
2464 if (p [7] > sizeof (void*))
2466 if (read_int32 (p + 20)) /* flags must be 0 */
2468 ctx->startup_time = read_int64 (p + 8);
2469 ctx->timer_overhead = read_int32 (p + 16);
2470 ctx->pid = read_int32 (p + 24);
2471 ctx->port = read_int16 (p + 28);
2479 static int alloc_sort_mode = ALLOC_SORT_BYTES;
2482 compare_class (const void *a, const void *b)
2484 ClassDesc *const*A = a;
2485 ClassDesc *const*B = b;
2486 uint64_t vala, valb;
2487 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2488 vala = (*A)->alloc_size;
2489 valb = (*B)->alloc_size;
2491 vala = (*A)->allocs;
2492 valb = (*B)->allocs;
2502 dump_header (ProfContext *ctx)
2504 time_t st = ctx->startup_time / 1000;
2505 char *t = ctime (&st);
2506 fprintf (outfile, "\nMono log profiler data\n");
2507 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
2508 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
2509 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
2510 fprintf (outfile, "\tProgram startup: %s", t);
2512 fprintf (outfile, "\tProgram ID: %d\n", ctx->pid);
2514 fprintf (outfile, "\tServer listening on: %d\n", ctx->port);
2518 dump_traces (TraceDesc *traces, const char *desc)
2525 sort_context_array (traces);
2526 for (j = 0; j < traces->count; ++j) {
2529 bt = traces->traces [j].bt;
2532 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
2533 for (k = 0; k < bt->count; ++k)
2534 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
2539 dump_threads (ProfContext *ctx)
2541 ThreadContext *thread;
2542 fprintf (outfile, "\nThread summary\n");
2543 for (thread = ctx->threads; thread; thread = thread->next) {
2544 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
2549 dump_exceptions (void)
2552 fprintf (outfile, "\nException summary\n");
2553 fprintf (outfile, "\tThrows: %llu\n", throw_count);
2554 dump_traces (&exc_traces, "throws");
2555 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
2556 if (!clause_summary [i])
2558 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
2563 compare_monitor (const void *a, const void *b)
2565 MonitorDesc *const*A = a;
2566 MonitorDesc *const*B = b;
2567 if ((*B)->wait_time == (*A)->wait_time)
2569 if ((*B)->wait_time < (*A)->wait_time)
2575 dump_monitors (void)
2577 MonitorDesc **monitors;
2581 monitors = malloc (sizeof (void*) * num_monitors);
2582 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
2583 MonitorDesc *mdesc = monitor_hash [i];
2585 monitors [j++] = mdesc;
2586 mdesc = mdesc->next;
2589 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
2590 fprintf (outfile, "\nMonitor lock summary\n");
2591 for (i = 0; i < num_monitors; ++i) {
2592 MonitorDesc *mdesc = monitors [i];
2593 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
2594 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
2595 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
2596 dump_traces (&mdesc->traces, "contentions");
2598 fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
2599 fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
2600 fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
2607 fprintf (outfile, "\nGC summary\n");
2608 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
2609 fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
2610 fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
2611 for (i = 0; i < 3; ++i) {
2612 if (!gc_info [i].count)
2614 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
2615 i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
2616 gc_info [i].total_time / gc_info [i].count / 1000);
2618 for (i = 0; i < 3; ++i) {
2619 if (!handle_info [i].max_live)
2621 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
2622 get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
2623 dump_traces (&handle_info [i].traces, "created");
2632 int compiled_methods = 0;
2634 fprintf (outfile, "\nJIT summary\n");
2635 for (i = 0; i < HASH_SIZE; ++i) {
2636 m = method_hash [i];
2637 for (m = method_hash [i]; m; m = m->next) {
2641 code_size += m->len;
2644 fprintf (outfile, "\tCompiled methods: %d\n", compiled_methods);
2645 fprintf (outfile, "\tGenerated code size: %d\n", code_size);
2649 dump_allocations (void)
2652 intptr_t allocs = 0;
2654 int header_done = 0;
2655 ClassDesc **classes = malloc (num_classes * sizeof (void*));
2658 for (i = 0; i < HASH_SIZE; ++i) {
2659 cd = class_hash [i];
2665 qsort (classes, num_classes, sizeof (void*), compare_class);
2666 for (i = 0; i < num_classes; ++i) {
2670 allocs += cd->allocs;
2671 size += cd->alloc_size;
2672 if (!header_done++) {
2673 fprintf (outfile, "\nAllocation summary\n");
2674 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
2676 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
2677 dump_traces (&cd->traces, "bytes");
2680 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
2689 static int method_sort_mode = METHOD_SORT_TOTAL;
2692 compare_method (const void *a, const void *b)
2694 MethodDesc *const*A = a;
2695 MethodDesc *const*B = b;
2696 uint64_t vala, valb;
2697 if (method_sort_mode == METHOD_SORT_SELF) {
2698 vala = (*A)->self_time;
2699 valb = (*B)->self_time;
2700 } else if (method_sort_mode == METHOD_SORT_CALLS) {
2704 vala = (*A)->total_time;
2705 valb = (*B)->total_time;
2715 dump_metadata (void)
2717 fprintf (outfile, "\nMetadata summary\n");
2718 fprintf (outfile, "\tLoaded images: %d\n", num_images);
2722 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
2723 image = image_hash [i];
2725 fprintf (outfile, "\t\t%s\n", image->filename);
2726 image = image->next;
2738 int header_done = 0;
2739 MethodDesc **methods = malloc (num_methods * sizeof (void*));
2742 for (i = 0; i < HASH_SIZE; ++i) {
2743 cd = method_hash [i];
2745 cd->total_time = cd->self_time + cd->callee_time;
2750 qsort (methods, num_methods, sizeof (void*), compare_method);
2751 for (i = 0; i < num_methods; ++i) {
2758 msecs = cd->total_time / 1000000;
2759 smsecs = (cd->total_time - cd->callee_time) / 1000000;
2760 if (!msecs && !verbose)
2762 if (!header_done++) {
2763 fprintf (outfile, "\nMethod call summary\n");
2764 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
2766 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
2767 dump_traces (&cd->traces, "calls");
2770 fprintf (outfile, "Total calls: %llu\n", calls);
2774 compare_heap_class (const void *a, const void *b)
2776 HeapClassDesc *const*A = a;
2777 HeapClassDesc *const*B = b;
2778 uint64_t vala, valb;
2779 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2780 vala = (*A)->total_size;
2781 valb = (*B)->total_size;
2794 compare_rev_class (const void *a, const void *b)
2796 const HeapClassRevRef *A = a;
2797 const HeapClassRevRef *B = b;
2798 if (B->count == A->count)
2800 if (B->count < A->count)
2806 dump_rev_claases (HeapClassRevRef *revs, int count)
2813 for (j = 0; j < count; ++j) {
2814 HeapClassDesc *cd = revs [j].klass;
2815 fprintf (outfile, "\t\t%llu references from: %s\n", revs [j].count, cd->klass->name);
2820 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
2827 HeapClassDesc **sorted;
2828 sorted = malloc (sizeof (void*) * hs->class_count);
2829 for (i = 0; i < hs->hash_size; ++i) {
2830 cd = hs->class_hash [i];
2834 size += cd->total_size;
2835 sorted [ccount++] = cd;
2837 hs->sorted = sorted;
2838 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
2839 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d, roots: %d\n",
2840 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount, hs->num_roots);
2841 if (!verbose && ccount > 30)
2843 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
2844 for (i = 0; i < ccount; ++i) {
2845 HeapClassRevRef *rev_sorted;
2847 HeapClassDesc *ocd = NULL;
2850 ocd = heap_class_lookup (last_hs, cd->klass);
2851 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
2853 int64_t bdiff = cd->total_size - ocd->total_size;
2854 int64_t cdiff = cd->count - ocd->count;
2855 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
2857 fprintf (outfile, "\n");
2859 if (!collect_traces)
2861 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
2863 for (j = 0; j < cd->rev_hash_size; ++j) {
2864 if (cd->rev_hash [j].klass)
2865 rev_sorted [k++] = cd->rev_hash [j];
2867 assert (cd->rev_count == k);
2868 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
2869 if (cd->root_references)
2870 fprintf (outfile, "\t\t%d root references (%d pinning)\n", cd->root_references, cd->pinned_references);
2871 dump_rev_claases (rev_sorted, cd->rev_count);
2878 compare_heap_shots (const void *a, const void *b)
2880 HeapShot *const*A = a;
2881 HeapShot *const*B = b;
2882 if ((*B)->timestamp == (*A)->timestamp)
2884 if ((*B)->timestamp > (*A)->timestamp)
2890 dump_heap_shots (void)
2892 HeapShot **hs_sorted;
2894 HeapShot *last_hs = NULL;
2898 hs_sorted = malloc (num_heap_shots * sizeof (void*));
2899 fprintf (outfile, "\nHeap shot summary\n");
2901 for (hs = heap_shots; hs; hs = hs->next)
2902 hs_sorted [i++] = hs;
2903 qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
2904 for (i = 0; i < num_heap_shots; ++i) {
2906 heap_shot_summary (hs, i, last_hs);
2912 flush_context (ProfContext *ctx)
2914 ThreadContext *thread;
2915 /* FIXME: sometimes there are leftovers: indagate */
2916 for (thread = ctx->threads; thread; thread = thread->next) {
2917 while (thread->stack_id) {
2919 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
2920 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
2925 static const char *reports = "header,jit,gc,sample,alloc,call,metadata,exception,monitor,thread,heapshot,counters";
2928 match_option (const char *p, const char *opt)
2930 int len = strlen (opt);
2931 if (strncmp (p, opt, len) == 0) {
2940 print_reports (ProfContext *ctx, const char *reps, int parse_only)
2944 for (p = reps; *p; p = opt) {
2945 if ((opt = match_option (p, "header")) != p) {
2950 if ((opt = match_option (p, "thread")) != p) {
2955 if ((opt = match_option (p, "gc")) != p) {
2960 if ((opt = match_option (p, "jit")) != p) {
2965 if ((opt = match_option (p, "alloc")) != p) {
2967 dump_allocations ();
2970 if ((opt = match_option (p, "call")) != p) {
2975 if ((opt = match_option (p, "metadata")) != p) {
2980 if ((opt = match_option (p, "exception")) != p) {
2985 if ((opt = match_option (p, "monitor")) != p) {
2990 if ((opt = match_option (p, "heapshot")) != p) {
2995 if ((opt = match_option (p, "sample")) != p) {
3000 if ((opt = match_option (p, "counters")) != p) {
3011 add_find_spec (const char *p)
3013 if (p [0] == 'S' && p [1] == ':') {
3015 find_size = strtoul (p + 2, &vale, 10);
3017 } else if (p [0] == 'T' && p [1] == ':') {
3027 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
3028 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
3029 printf ("FILENAME can be '-' to read from standard input.\n");
3030 printf ("Options:\n");
3031 printf ("\t--help display this help\n");
3032 printf ("\t--out=FILE write to FILE instead of stdout\n");
3033 printf ("\t--traces collect and show backtraces\n");
3034 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
3035 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
3036 printf ("\t %s\n", reports);
3037 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
3038 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
3039 printf ("\t--counters-sort=MODE sort counters according to MODE: time, category\n");
3040 printf ("\t only accessible in verbose mode\n");
3041 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
3042 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
3043 printf ("\t S:minimum_size or T:partial_name\n");
3044 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
3045 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
3046 printf ("\t--verbose increase verbosity level\n");
3047 printf ("\t--debug display decoding debug info for mprof-report devs\n");
3051 main (int argc, char *argv[])
3056 for (i = 1; i < argc; ++i) {
3057 if (strcmp ("--debug", argv [i]) == 0) {
3059 } else if (strcmp ("--help", argv [i]) == 0) {
3062 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
3063 const char *val = argv [i] + 13;
3064 if (strcmp (val, "bytes") == 0) {
3065 alloc_sort_mode = ALLOC_SORT_BYTES;
3066 } else if (strcmp (val, "count") == 0) {
3067 alloc_sort_mode = ALLOC_SORT_COUNT;
3072 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
3073 const char *val = argv [i] + 14;
3074 if (strcmp (val, "total") == 0) {
3075 method_sort_mode = METHOD_SORT_TOTAL;
3076 } else if (strcmp (val, "self") == 0) {
3077 method_sort_mode = METHOD_SORT_SELF;
3078 } else if (strcmp (val, "calls") == 0) {
3079 method_sort_mode = METHOD_SORT_CALLS;
3084 } else if (strncmp ("--counters-sort=", argv [i], 16) == 0) {
3085 const char *val = argv [i] + 16;
3086 if (strcmp (val, "time") == 0) {
3087 counters_sort_mode = COUNTERS_SORT_TIME;
3088 } else if (strcmp (val, "category") == 0) {
3089 counters_sort_mode = COUNTERS_SORT_CATEGORY;
3094 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
3095 const char *val = argv [i] + 10;
3096 if (!print_reports (NULL, val, 1)) {
3101 } else if (strncmp ("--out=", argv [i], 6) == 0) {
3102 const char *val = argv [i] + 6;
3103 outfile = fopen (val, "w");
3105 printf ("Cannot open output file: %s\n", val);
3108 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
3109 const char *val = argv [i] + 12;
3111 trace_max = strtoul (val, &vale, 10);
3112 } else if (strncmp ("--find=", argv [i], 7) == 0) {
3113 const char *val = argv [i] + 7;
3114 if (!add_find_spec (val)) {
3118 } else if (strncmp ("--track=", argv [i], 8) == 0) {
3119 const char *val = argv [i] + 8;
3122 uintptr_t tracked_obj;
3127 tracked_obj = strtoul (val, &vale, 0);
3128 found_object (tracked_obj);
3131 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
3132 const char *val = argv [i] + 9;
3134 thread_filter = strtoul (val, &vale, 0);
3135 } else if (strncmp ("--time=", argv [i], 7) == 0) {
3136 char *val = pstrdup (argv [i] + 7);
3137 double from_secs, to_secs;
3138 char *top = strchr (val, '-');
3144 from_secs = atof (val);
3145 to_secs = atof (top);
3147 if (from_secs > to_secs) {
3151 time_from = from_secs * 1000000000;
3152 time_to = to_secs * 1000000000;
3153 use_time_filter = 1;
3154 } else if (strcmp ("--verbose", argv [i]) == 0) {
3156 } else if (strcmp ("--traces", argv [i]) == 0) {
3167 ctx = load_file (argv [i]);
3169 printf ("Not a log profiler data file (or unsupported version).\n");
3172 while (decode_buffer (ctx));
3173 flush_context (ctx);
3174 if (num_tracked_objects)
3176 print_reports (ctx, reports, 0);