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 || !verbose)
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)
358 snprintf (format, sizeof (format), "%s : %%s\n", key_format);
359 fprintf (outfile, format, key, "<null>");
361 switch (counter->type) {
362 case MONO_COUNTER_INT:
363 #if SIZEOF_VOID_P == 4
364 case MONO_COUNTER_WORD:
366 snprintf (format, sizeof (format), "%s : %%d\n", key_format);
367 fprintf (outfile, format, key, *(int32_t*)value);
369 case MONO_COUNTER_UINT:
370 snprintf (format, sizeof (format), "%s : %%u\n", key_format);
371 fprintf (outfile, format, key, *(uint32_t*)value);
373 case MONO_COUNTER_LONG:
374 #if SIZEOF_VOID_P == 8
375 case MONO_COUNTER_WORD:
377 case MONO_COUNTER_TIME_INTERVAL:
378 if (counter->type == MONO_COUNTER_LONG && counter->unit == MONO_COUNTER_TIME) {
379 snprintf (format, sizeof (format), "%s : %%0.3fms\n", key_format);
380 fprintf (outfile, format, key, (double)*(int64_t*)value / 10000.0);
381 } else if (counter->type == MONO_COUNTER_TIME_INTERVAL) {
382 snprintf (format, sizeof (format), "%s : %%0.3fms\n", key_format);
383 fprintf (outfile, format, key, (double)*(int64_t*)value / 1000.0);
385 snprintf (format, sizeof (format), "%s : %%u\n", key_format);
386 fprintf (outfile, format, key, *(int64_t*)value);
389 case MONO_COUNTER_ULONG:
390 snprintf (format, sizeof (format), "%s : %%llu\n", key_format);
391 fprintf (outfile, format, key, *(uint64_t*)value);
393 case MONO_COUNTER_DOUBLE:
394 snprintf (format, sizeof (format), "%s : %%f\n", key_format);
395 fprintf (outfile, format, key, *(double*)value);
397 case MONO_COUNTER_STRING:
398 snprintf (format, sizeof (format), "%s : %%s\n", key_format);
399 fprintf (outfile, format, key, *(char*)value);
409 CounterValue *cvalue;
410 CounterTimestamp *ctimestamp;
411 CounterSection *csection;
413 char strtimestamp[17];
414 int i, section_printed;
416 fprintf (outfile, "\nCounters:\n");
419 char counters_to_print[][64] = {
421 "Methods JITted using mono JIT",
422 "Methods JITted using LLVM",
423 "Total time spent JITting (sec)",
431 "CPU Load Average - 1min",
432 "CPU Load Average - 5min",
433 "CPU Load Average - 15min",
437 for (csection = counters_sections; csection; csection = csection->next) {
440 for (clist = csection->counters; clist; clist = clist->next) {
441 counter = clist->counter;
442 if (!counter->values_last)
445 for (i = 0; counters_to_print [i][0] != 0; i++) {
446 if (strcmp (counters_to_print [i], counter->name) == 0) {
447 if (!section_printed) {
448 fprintf (outfile, "\t%s:\n", section_name (csection->value));
452 dump_counters_value (counter, "\t\t%-30s", counter->name, counter->values_last->buffer);
458 } else if (counters_sort_mode == COUNTERS_SORT_TIME) {
459 for (ctimestamp = counters_timestamps; ctimestamp; ctimestamp = ctimestamp->next) {
460 fprintf (outfile, "\t%lld:%02lld:%02lld:%02lld.%03lld:\n", ctimestamp->value / 1000 / 60 / 60 / 24 % 1000,
461 ctimestamp->value / 1000 / 60 / 60 % 24, ctimestamp->value / 1000 / 60 % 60,
462 ctimestamp->value / 1000 % 60, ctimestamp->value % 1000);
464 for (csection = ctimestamp->sections; csection; csection = csection->next) {
465 fprintf (outfile, "\t\t%s:\n", section_name (csection->value));
467 for (clist = csection->counters; clist; clist = clist->next) {
468 counter = clist->counter;
469 for (cvalue = counter->values; cvalue; cvalue = cvalue->next) {
470 if (cvalue->timestamp != ctimestamp->value)
473 dump_counters_value (counter, "\t\t\t%-30s", counter->name, cvalue->buffer);
478 } else if (counters_sort_mode == COUNTERS_SORT_CATEGORY) {
479 for (csection = counters_sections; csection; csection = csection->next) {
480 fprintf (outfile, "\t%s:\n", section_name (csection->value));
482 for (clist = csection->counters; clist; clist = clist->next) {
483 counter = clist->counter;
484 fprintf (outfile, "\t\t%s: [type: %s, unit: %s, variance: %s]\n",
485 counter->name, type_name (counter->type), unit_name (counter->unit), variance_name (counter->variance));
487 for (cvalue = counter->values; cvalue; cvalue = cvalue->next) {
488 snprintf (strtimestamp, sizeof (strtimestamp), "%lld:%02lld:%02lld:%02lld.%03lld", cvalue->timestamp / 1000 / 60 / 60 / 24 % 1000,
489 cvalue->timestamp / 1000 / 60 / 60 % 24, cvalue->timestamp / 1000 / 60 % 60,
490 cvalue->timestamp / 1000 % 60, cvalue->timestamp % 1000);
492 dump_counters_value (counter, "\t\t\t%s", strtimestamp, cvalue->buffer);
499 static int num_images;
500 typedef struct _ImageDesc ImageDesc;
507 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
510 add_image (intptr_t image, char *name)
512 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
513 ImageDesc *cd = malloc (sizeof (ImageDesc));
515 cd->filename = pstrdup (name);
516 cd->next = image_hash [slot];
517 image_hash [slot] = cd;
521 typedef struct _BackTrace BackTrace;
533 typedef struct _ClassDesc ClassDesc;
543 static ClassDesc* class_hash [HASH_SIZE] = {0};
544 static int num_classes = 0;
547 add_class (intptr_t klass, const char *name)
549 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
551 cd = class_hash [slot];
552 while (cd && cd->klass != klass)
554 /* we resolved an unknown class (unless we had the code unloaded) */
556 /*printf ("resolved unknown: %s\n", name);*/
558 cd->name = pstrdup (name);
561 cd = calloc (sizeof (ClassDesc), 1);
563 cd->name = pstrdup (name);
564 cd->next = class_hash [slot];
567 cd->traces.count = 0;
569 cd->traces.traces = NULL;
570 class_hash [slot] = cd;
576 lookup_class (intptr_t klass)
578 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
579 ClassDesc *cd = class_hash [slot];
580 while (cd && cd->klass != klass)
584 snprintf (buf, sizeof (buf), "unresolved class %p", (void*)klass);
585 return add_class (klass, buf);
590 typedef struct _MethodDesc MethodDesc;
599 int ignore_jit; /* when this is set, we collect the metadata but don't count this method fot jit time and code size, when filtering events */
602 uint64_t callee_time;
607 static MethodDesc* method_hash [HASH_SIZE] = {0};
608 static int num_methods = 0;
611 add_method (intptr_t method, const char *name, intptr_t code, int len)
613 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
615 cd = method_hash [slot];
616 while (cd && cd->method != method)
618 /* we resolved an unknown method (unless we had the code unloaded) */
622 /*printf ("resolved unknown: %s\n", name);*/
624 cd->name = pstrdup (name);
627 cd = calloc (sizeof (MethodDesc), 1);
629 cd->name = pstrdup (name);
634 cd->traces.count = 0;
636 cd->traces.traces = NULL;
637 cd->next = method_hash [slot];
638 method_hash [slot] = cd;
644 lookup_method (intptr_t method)
646 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
647 MethodDesc *cd = method_hash [slot];
648 while (cd && cd->method != method)
652 snprintf (buf, sizeof (buf), "unknown method %p", (void*)method);
653 return add_method (method, buf, 0, 0);
658 static int num_stat_samples = 0;
659 static int size_stat_samples = 0;
660 uintptr_t *stat_samples = NULL;
661 int *stat_sample_desc = NULL;
664 add_stat_sample (int type, uintptr_t ip) {
665 if (num_stat_samples == size_stat_samples) {
666 size_stat_samples *= 2;
667 if (!size_stat_samples)
668 size_stat_samples = 32;
669 stat_samples = realloc (stat_samples, size_stat_samples * sizeof (uintptr_t));
670 stat_sample_desc = realloc (stat_sample_desc, size_stat_samples * sizeof (int));
672 stat_samples [num_stat_samples] = ip;
673 stat_sample_desc [num_stat_samples++] = type;
677 lookup_method_by_ip (uintptr_t ip)
682 for (i = 0; i < HASH_SIZE; ++i) {
685 //printf ("checking %p against %p-%p\n", (void*)ip, (void*)(m->code), (void*)(m->code + m->len));
686 if (ip >= (uintptr_t)m->code && ip < (uintptr_t)m->code + m->len) {
696 compare_method_samples (const void *a, const void *b)
698 MethodDesc *const*A = a;
699 MethodDesc *const*B = b;
700 if ((*A)->sample_hits == (*B)->sample_hits)
702 if ((*B)->sample_hits < (*A)->sample_hits)
707 typedef struct _UnmanagedSymbol UnmanagedSymbol;
708 struct _UnmanagedSymbol {
709 UnmanagedSymbol *parent;
714 uintptr_t sample_hits;
717 static UnmanagedSymbol **usymbols = NULL;
718 static int usymbols_size = 0;
719 static int usymbols_num = 0;
722 compare_usymbol_addr (const void *a, const void *b)
724 UnmanagedSymbol *const*A = a;
725 UnmanagedSymbol *const*B = b;
726 if ((*B)->addr == (*A)->addr)
728 if ((*B)->addr > (*A)->addr)
734 compare_usymbol_samples (const void *a, const void *b)
736 UnmanagedSymbol *const*A = a;
737 UnmanagedSymbol *const*B = b;
738 if ((*B)->sample_hits == (*A)->sample_hits)
740 if ((*B)->sample_hits < (*A)->sample_hits)
746 add_unmanaged_symbol (uintptr_t addr, char *name, uintptr_t size)
748 UnmanagedSymbol *sym;
749 if (usymbols_num == usymbols_size) {
750 int new_size = usymbols_size * 2;
753 usymbols = realloc (usymbols, sizeof (void*) * new_size);
754 usymbols_size = new_size;
756 sym = calloc (sizeof (UnmanagedSymbol), 1);
760 usymbols [usymbols_num++] = sym;
763 /* only valid after the symbols are sorted */
764 static UnmanagedSymbol*
765 lookup_unmanaged_symbol (uintptr_t addr)
767 int r = usymbols_num - 1;
769 UnmanagedSymbol *sym;
774 if (addr == sym->addr)
776 if (addr < sym->addr) {
778 } else if (addr > sym->addr) {
783 if (last_best >= 0 && (addr - usymbols [last_best]->addr) < 4096)
784 return usymbols [last_best];
788 /* we use the same structure for binaries */
789 static UnmanagedSymbol **ubinaries = NULL;
790 static int ubinaries_size = 0;
791 static int ubinaries_num = 0;
794 add_unmanaged_binary (uintptr_t addr, char *name, uintptr_t size)
796 UnmanagedSymbol *sym;
797 if (ubinaries_num == ubinaries_size) {
798 int new_size = ubinaries_size * 2;
801 ubinaries = realloc (ubinaries, sizeof (void*) * new_size);
802 ubinaries_size = new_size;
804 sym = calloc (sizeof (UnmanagedSymbol), 1);
809 ubinaries [ubinaries_num++] = sym;
812 static UnmanagedSymbol*
813 lookup_unmanaged_binary (uintptr_t addr)
816 for (i = 0; i < ubinaries_num; ++i) {
817 UnmanagedSymbol *ubin = ubinaries [i];
818 if (addr >= ubin->addr && addr < ubin->addr + ubin->size) {
826 sample_type_name (int type)
829 case SAMPLE_CYCLES: return "cycles";
830 case SAMPLE_INSTRUCTIONS: return "instructions retired";
831 case SAMPLE_CACHE_MISSES: return "cache misses";
832 case SAMPLE_CACHE_REFS: return "cache references";
833 case SAMPLE_BRANCHES: return "executed branches";
834 case SAMPLE_BRANCH_MISSES: return "unpredicted branches";
840 set_usym_parent (UnmanagedSymbol** cachedus, int count)
843 for (i = 0; i < count; ++i) {
844 UnmanagedSymbol *ubin = lookup_unmanaged_binary (cachedus [i]->addr);
845 if (ubin == cachedus [i])
847 cachedus [i]->parent = ubin;
852 print_usym (UnmanagedSymbol* um)
855 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);
857 fprintf (outfile, "\t%6d %6.2f %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name);
861 sym_percent (uintptr_t sample_hits)
866 pc = sample_hits*100.0/num_stat_samples;
874 int count = 0, msize = 0;
875 int unmanaged_hits = 0;
876 int unresolved_hits = 0;
877 MethodDesc** cachedm = NULL;
878 int ucount = 0, usize = 0;
879 UnmanagedSymbol** cachedus = NULL;
880 if (!num_stat_samples)
882 qsort (usymbols, usymbols_num, sizeof (UnmanagedSymbol*), compare_usymbol_addr);
883 for (i = 0; i < num_stat_samples; ++i) {
884 MethodDesc *m = lookup_method_by_ip (stat_samples [i]);
886 if (!m->sample_hits) {
887 if (count == msize) {
891 cachedm = realloc (cachedm, sizeof (void*) * msize);
893 cachedm [count++] = m;
897 UnmanagedSymbol *usym = lookup_unmanaged_symbol (stat_samples [i]);
900 //printf ("unmanaged hit at %p\n", (void*)stat_samples [i]);
901 usym = lookup_unmanaged_binary (stat_samples [i]);
904 if (!usym->sample_hits) {
905 if (ucount == usize) {
909 cachedus = realloc (cachedus, sizeof (void*) * usize);
911 cachedus [ucount++] = usym;
918 qsort (cachedm, count, sizeof (MethodDesc*), compare_method_samples);
919 qsort (cachedus, ucount, sizeof (UnmanagedSymbol*), compare_usymbol_samples);
920 set_usym_parent (cachedus, ucount);
921 fprintf (outfile, "\nStatistical samples summary\n");
922 fprintf (outfile, "\tSample type: %s\n", sample_type_name (stat_sample_desc [0]));
923 fprintf (outfile, "\tUnmanaged hits: %6d (%4.1f%%)\n", unmanaged_hits, (100.0*unmanaged_hits)/num_stat_samples);
924 fprintf (outfile, "\tManaged hits: %6d (%4.1f%%)\n", num_stat_samples - unmanaged_hits, (100.0*(num_stat_samples-unmanaged_hits))/num_stat_samples);
925 fprintf (outfile, "\tUnresolved hits: %6d (%4.1f%%)\n", unresolved_hits, (100.0*unresolved_hits)/num_stat_samples);
926 fprintf (outfile, "\t%6s %6s %s\n", "Hits", "%", "Method name");
929 while (i < count || u < ucount) {
931 MethodDesc *m = cachedm [i];
933 UnmanagedSymbol *um = cachedus [u];
934 if (um->sample_hits > m->sample_hits) {
935 if (!sym_percent (um->sample_hits))
942 if (!sym_percent (m->sample_hits))
944 fprintf (outfile, "\t%6d %6.2f %s\n", m->sample_hits, m->sample_hits*100.0/num_stat_samples, m->name);
949 UnmanagedSymbol *um = cachedus [u];
950 if (!sym_percent (um->sample_hits))
959 typedef struct _HeapClassDesc HeapClassDesc;
961 HeapClassDesc *klass;
965 struct _HeapClassDesc {
969 HeapClassRevRef *rev_hash;
972 uintptr_t pinned_references;
973 uintptr_t root_references;
977 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
981 start_pos = (hklass->klass->klass >> 2) % size;
982 assert (start_pos < size);
985 if (rev_hash [i].klass == hklass) {
986 rev_hash [i].count += value;
988 } else if (!rev_hash [i].klass) {
989 rev_hash [i].klass = hklass;
990 rev_hash [i].count += value;
992 for (i = 0; i < size; ++i)
993 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
995 assert (start_pos == 1);
1001 } while (i != start_pos);
1002 /* should not happen */
1003 printf ("failed revref store\n");
1008 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
1011 if (to->rev_count * 2 >= to->rev_hash_size) {
1013 uintptr_t old_size = to->rev_hash_size;
1014 to->rev_hash_size *= 2;
1015 if (to->rev_hash_size == 0)
1016 to->rev_hash_size = 4;
1017 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
1018 for (i = 0; i < old_size; ++i) {
1019 if (to->rev_hash [i].klass)
1020 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
1023 free (to->rev_hash);
1026 to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
1031 HeapClassDesc *hklass;
1036 typedef struct _HeapShot HeapShot;
1042 HeapClassDesc **class_hash;
1043 HeapClassDesc **sorted;
1044 HeapObjectDesc **objects_hash;
1045 uintptr_t objects_count;
1046 uintptr_t objects_hash_size;
1047 uintptr_t num_roots;
1049 uintptr_t *roots_extra;
1053 static HeapShot *heap_shots = NULL;
1054 static int num_heap_shots = 0;
1057 new_heap_shot (uint64_t timestamp)
1059 HeapShot *hs = calloc (sizeof (HeapShot), 1);
1061 hs->class_hash = calloc (sizeof (void*), hs->hash_size);
1062 hs->timestamp = timestamp;
1064 hs->next = heap_shots;
1069 static HeapClassDesc*
1070 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
1073 unsigned int start_pos;
1074 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
1077 HeapClassDesc* cd = hs->class_hash [i];
1080 if (cd->klass == klass)
1083 if (++i == hs->hash_size)
1085 } while (i != start_pos);
1090 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
1093 uintptr_t start_pos;
1094 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
1097 if (hash [i] && hash [i]->klass == klass) {
1098 hash [i]->total_size += size;
1099 hash [i]->count += count;
1102 } else if (!hash [i]) {
1107 hash [i] = calloc (sizeof (HeapClassDesc), 1);
1108 hash [i]->klass = klass;
1109 hash [i]->total_size += size;
1110 hash [i]->count += count;
1117 } while (i != start_pos);
1118 /* should not happen */
1119 printf ("failed heap class store\n");
1123 static HeapClassDesc*
1124 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
1128 if (hs->class_count * 2 >= hs->hash_size) {
1130 int old_size = hs->hash_size;
1132 if (hs->hash_size == 0)
1134 n = calloc (sizeof (void*) * hs->hash_size, 1);
1135 for (i = 0; i < old_size; ++i) {
1136 res = hs->class_hash [i];
1137 if (hs->class_hash [i])
1138 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
1141 free (hs->class_hash);
1145 hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
1146 //if (res->count == 1)
1147 // printf ("added heap class: %s\n", res->klass->name);
1151 static HeapObjectDesc*
1152 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
1154 HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
1155 ho->objaddr = objaddr;
1156 ho->hklass = hklass;
1157 ho->num_refs = num_refs;
1162 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
1165 uintptr_t start_pos;
1166 HeapObjectDesc **hash = hs->objects_hash;
1167 start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
1170 if (hash [i] && hash [i]->objaddr == objaddr) {
1172 } else if (!hash [i]) {
1176 if (++i == hs->objects_hash_size)
1178 } while (i != start_pos);
1179 /* should not happen */
1180 //printf ("failed heap obj slot\n");
1184 static HeapObjectDesc*
1185 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
1187 HeapObjectDesc **hash = hs->objects_hash;
1188 uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
1190 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
1191 *ref_offset = hash [i]->num_refs;
1192 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
1197 /* should not happen */
1198 printf ("failed heap obj update\n");
1204 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
1207 uintptr_t start_pos;
1208 start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
1211 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
1212 printf ("duplicate object!\n");
1214 } else if (!hash [i]) {
1221 } while (i != start_pos);
1222 /* should not happen */
1223 printf ("failed heap obj store\n");
1228 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
1231 if (hs->objects_count * 2 >= hs->objects_hash_size) {
1233 uintptr_t old_size = hs->objects_hash_size;
1234 hs->objects_hash_size *= 2;
1235 if (hs->objects_hash_size == 0)
1236 hs->objects_hash_size = 4;
1237 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
1238 for (i = 0; i < old_size; ++i) {
1239 if (hs->objects_hash [i])
1240 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
1242 if (hs->objects_hash)
1243 free (hs->objects_hash);
1244 hs->objects_hash = n;
1246 hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
1250 heap_shot_resolve_reverse_refs (HeapShot *hs)
1253 for (i = 0; i < hs->objects_hash_size; ++i) {
1255 HeapObjectDesc *ho = hs->objects_hash [i];
1258 for (r = 0; r < ho->num_refs; ++r) {
1259 uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
1260 add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
1266 #define MARK_BLACK 2
1269 heap_shot_mark_objects (HeapShot *hs)
1272 unsigned char *marks;
1273 HeapObjectDesc *obj, *ref;
1275 uintptr_t num_marked = 0, num_unmarked;
1276 for (i = 0; i < hs->num_roots; ++i) {
1278 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
1282 obj = hs->objects_hash [oi];
1284 if (hs->roots_types [i] & MONO_PROFILE_GC_ROOT_PINNING)
1285 cd->pinned_references++;
1286 cd->root_references++;
1290 /* consistency checks: it seems not all the objects are walked in the heap in some cases */
1291 marks = calloc (hs->objects_hash_size, 1);
1294 for (i = 0; i < hs->num_roots; ++i) {
1295 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
1297 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);
1300 obj = hs->objects_hash [oi];
1302 marks [oi] = obj->num_refs? MARK_GRAY: MARK_BLACK;
1307 while (marked_some) {
1309 for (i = 0; i < hs->objects_hash_size; ++i) {
1310 if (marks [i] != MARK_GRAY)
1312 marks [i] = MARK_BLACK;
1313 obj = hs->objects_hash [i];
1314 for (r = 0; r < obj->num_refs; ++r) {
1315 oi = heap_shot_find_obj_slot (hs, obj->refs [r]);
1317 fprintf (outfile, "referenced obj %p not found in heap\n", (void*)obj->refs [r]);
1320 ref = hs->objects_hash [oi];
1322 marks [oi] = ref->num_refs? MARK_GRAY: MARK_BLACK;
1330 for (i = 0; i < hs->objects_hash_size; ++i) {
1331 if (hs->objects_hash [i] && !marks [i]) {
1333 fprintf (outfile, "object %p (%s) unmarked\n", (void*)hs->objects_hash [i], hs->objects_hash [i]->hklass->klass->name);
1336 fprintf (outfile, "Total unmarked: %d/%d\n", num_unmarked, hs->objects_count);
1341 heap_shot_free_objects (HeapShot *hs)
1344 for (i = 0; i < hs->objects_hash_size; ++i) {
1345 HeapObjectDesc *ho = hs->objects_hash [i];
1349 if (hs->objects_hash)
1350 free (hs->objects_hash);
1351 hs->objects_hash = NULL;
1352 hs->objects_hash_size = 0;
1353 hs->objects_count = 0;
1362 MethodDesc *methods [1];
1365 static BackTrace *backtrace_hash [HASH_SIZE];
1366 static BackTrace **backtraces = NULL;
1367 static int num_backtraces = 0;
1368 static int next_backtrace = 0;
1371 hash_backtrace (int count, MethodDesc **methods)
1375 for (i = 0; i < count; ++i) {
1376 hash = (hash << 5) - hash + methods [i]->method;
1382 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
1385 if (bt->count != count)
1387 for (i = 0; i < count; ++i)
1388 if (methods [i] != bt->methods [i])
1394 add_backtrace (int count, MethodDesc **methods)
1396 int hash = hash_backtrace (count, methods);
1397 int slot = (hash & 0xffff) % HASH_SIZE;
1398 BackTrace *bt = backtrace_hash [slot];
1400 if (bt->hash == hash && compare_backtrace (bt, count, methods))
1404 bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
1405 bt->next = backtrace_hash [slot];
1406 backtrace_hash [slot] = bt;
1407 if (next_backtrace == num_backtraces) {
1408 num_backtraces *= 2;
1409 if (!num_backtraces)
1410 num_backtraces = 16;
1411 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
1413 bt->id = next_backtrace++;
1414 backtraces [bt->id] = bt;
1417 for (slot = 0; slot < count; ++slot)
1418 bt->methods [slot] = methods [slot];
1423 typedef struct _MonitorDesc MonitorDesc;
1424 typedef struct _ThreadContext ThreadContext;
1428 #if defined (HAVE_SYS_ZLIB)
1439 uint64_t startup_time;
1440 ThreadContext *threads;
1441 ThreadContext *current;
1444 struct _ThreadContext {
1445 ThreadContext *next;
1448 /* emulated stack */
1450 uint64_t *time_stack;
1451 uint64_t *callee_time_stack;
1453 uint64_t contention_start;
1454 MonitorDesc *monitor;
1457 HeapShot *current_heap_shot;
1458 uintptr_t num_roots;
1459 uintptr_t size_roots;
1461 uintptr_t *roots_extra;
1463 uint64_t gc_start_times [3];
1467 ensure_buffer (ProfContext *ctx, int size)
1469 if (ctx->size < size) {
1470 ctx->buf = realloc (ctx->buf, size);
1476 load_data (ProfContext *ctx, int size)
1478 ensure_buffer (ctx, size);
1479 #if defined (HAVE_SYS_ZLIB)
1481 int r = gzread (ctx->gzfile, ctx->buf, size);
1483 return size == 0? 1: 0;
1488 int r = fread (ctx->buf, size, 1, ctx->file);
1490 return size == 0? 1: 0;
1495 static ThreadContext*
1496 get_thread (ProfContext *ctx, intptr_t thread_id)
1498 ThreadContext *thread;
1499 if (ctx->current && ctx->current->thread_id == thread_id)
1500 return ctx->current;
1501 thread = ctx->threads;
1503 if (thread->thread_id == thread_id) {
1506 thread = thread->next;
1508 thread = calloc (sizeof (ThreadContext), 1);
1509 thread->next = ctx->threads;
1510 ctx->threads = thread;
1511 thread->thread_id = thread_id;
1512 thread->last_time = 0;
1513 thread->stack_id = 0;
1514 thread->stack_size = 32;
1515 thread->stack = malloc (thread->stack_size * sizeof (void*));
1516 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1517 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1521 static ThreadContext*
1522 load_thread (ProfContext *ctx, intptr_t thread_id)
1524 ThreadContext *thread = get_thread (ctx, thread_id);
1525 ctx->current = thread;
1530 ensure_thread_stack (ThreadContext *thread)
1532 if (thread->stack_id == thread->stack_size) {
1533 thread->stack_size *= 2;
1534 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
1535 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
1536 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
1541 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
1544 unsigned int start_pos;
1545 start_pos = bt->hash % size;
1548 if (traces [i].bt == bt) {
1549 traces [i].count += value;
1551 } else if (!traces [i].bt) {
1553 traces [i].count += value;
1559 } while (i != start_pos);
1560 /* should not happen */
1561 printf ("failed trace store\n");
1566 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
1569 if (!collect_traces)
1571 if (trace->count * 2 >= trace->size) {
1573 int old_size = trace->size;
1575 if (trace->size == 0)
1577 n = calloc (sizeof (CallContext) * trace->size, 1);
1578 for (i = 0; i < old_size; ++i) {
1579 if (trace->traces [i].bt)
1580 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
1583 free (trace->traces);
1586 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
1590 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
1593 int count = thread->stack_id;
1594 if (!collect_traces)
1596 if (count > trace_max)
1598 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
1599 add_trace_bt (bt, trace, value);
1604 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
1607 if (!collect_traces)
1609 if (count > trace_max)
1611 bt = add_backtrace (count, methods);
1612 add_trace_bt (bt, trace, value);
1617 thread_add_root (ThreadContext *ctx, uintptr_t obj, int root_type, uintptr_t extra_info)
1619 if (ctx->num_roots == ctx->size_roots) {
1620 int new_size = ctx->size_roots * 2;
1623 ctx->roots = realloc (ctx->roots, new_size * sizeof (uintptr_t));
1624 ctx->roots_extra = realloc (ctx->roots_extra, new_size * sizeof (uintptr_t));
1625 ctx->roots_types = realloc (ctx->roots_types, new_size * sizeof (int));
1626 ctx->size_roots = new_size;
1628 ctx->roots_types [ctx->num_roots] = root_type;
1629 ctx->roots_extra [ctx->num_roots] = extra_info;
1630 ctx->roots [ctx->num_roots++] = obj;
1634 compare_callc (const void *a, const void *b)
1636 const CallContext *A = a;
1637 const CallContext *B = b;
1638 if (B->count == A->count)
1640 if (B->count < A->count)
1646 sort_context_array (TraceDesc* traces)
1649 for (i = 0, j = 0; i < traces->size; ++i) {
1650 if (traces->traces [i].bt) {
1651 traces->traces [j].bt = traces->traces [i].bt;
1652 traces->traces [j].count = traces->traces [i].count;
1656 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
1660 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1662 ensure_thread_stack (thread);
1663 thread->time_stack [thread->stack_id] = timestamp;
1664 thread->callee_time_stack [thread->stack_id] = 0;
1665 thread->stack [thread->stack_id++] = method;
1666 method->recurse_count++;
1670 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1672 method->recurse_count--;
1673 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
1677 if (timestamp < thread->time_stack [thread->stack_id])
1678 fprintf (outfile, "time went backwards for %s\n", method->name);
1679 tdiff = timestamp - thread->time_stack [thread->stack_id];
1680 if (thread->callee_time_stack [thread->stack_id] > tdiff)
1681 fprintf (outfile, "callee time bigger for %s\n", method->name);
1682 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
1683 method->callee_time += thread->callee_time_stack [thread->stack_id];
1684 if (thread->stack_id)
1685 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
1686 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
1688 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
1693 uint64_t total_time;
1697 static GCDesc gc_info [3];
1698 static uint64_t max_heap_size;
1699 static uint64_t gc_object_moves;
1700 static int gc_resizes;
1708 static HandleInfo handle_info [4];
1711 gc_event_name (int ev)
1714 case MONO_GC_EVENT_START: return "start";
1715 case MONO_GC_EVENT_MARK_START: return "mark start";
1716 case MONO_GC_EVENT_MARK_END: return "mark end";
1717 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
1718 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
1719 case MONO_GC_EVENT_END: return "end";
1720 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
1721 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
1722 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
1723 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
1729 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
1730 static uint64_t throw_count = 0;
1731 static TraceDesc exc_traces;
1734 clause_name (int type)
1737 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
1738 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
1739 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
1740 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
1741 default: return "invalid";
1745 static uint64_t monitor_contention;
1746 static uint64_t monitor_failed;
1747 static uint64_t monitor_acquired;
1749 struct _MonitorDesc {
1752 uintptr_t contentions;
1754 uint64_t max_wait_time;
1758 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
1759 static int num_monitors = 0;
1762 lookup_monitor (uintptr_t objid)
1764 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
1765 MonitorDesc *cd = monitor_hash [slot];
1766 while (cd && cd->objid != objid)
1769 cd = calloc (sizeof (MonitorDesc), 1);
1771 cd->next = monitor_hash [slot];
1772 monitor_hash [slot] = cd;
1779 monitor_ev_name (int ev)
1782 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
1783 case MONO_PROFILER_MONITOR_DONE: return "acquired";
1784 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
1785 default: return "invalid";
1790 get_handle_name (int htype)
1793 case 0: return "weak";
1794 case 1: return "weaktrack";
1795 case 2: return "normal";
1796 case 3: return "pinned";
1797 default: return "unknown";
1802 get_root_name (int rtype)
1804 switch (rtype & MONO_PROFILE_GC_ROOT_TYPEMASK) {
1805 case MONO_PROFILE_GC_ROOT_STACK: return "stack";
1806 case MONO_PROFILE_GC_ROOT_FINALIZER: return "finalizer";
1807 case MONO_PROFILE_GC_ROOT_HANDLE: return "handle";
1808 case MONO_PROFILE_GC_ROOT_OTHER: return "other";
1809 case MONO_PROFILE_GC_ROOT_MISC: return "misc";
1810 default: return "unknown";
1815 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
1817 MethodDesc **frames;
1819 int flags = decode_uleb128 (p, &p);
1820 int count = decode_uleb128 (p, &p);
1824 frames = malloc (count * sizeof (void*));
1827 for (i = 0; i < count; ++i) {
1828 intptr_t ptrdiff = decode_sleb128 (p, &p);
1829 frames [i] = lookup_method (ptr_base + ptrdiff);
1837 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
1840 for (i = 0; i < num_tracked_objects; ++i) {
1841 if (tracked_objects [i] != obj)
1843 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
1844 if (bt && bt->count) {
1846 for (k = 0; k < bt->count; ++k)
1847 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
1853 track_handle (uintptr_t obj, int htype, uint32_t handle)
1856 for (i = 0; i < num_tracked_objects; ++i) {
1857 if (tracked_objects [i] == obj)
1858 fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1863 track_move (uintptr_t src, uintptr_t dst)
1866 for (i = 0; i < num_tracked_objects; ++i) {
1867 if (tracked_objects [i] == src)
1868 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1869 else if (tracked_objects [i] == dst)
1870 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1875 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1878 for (i = 0; i < num_tracked_objects; ++i) {
1879 if (tracked_objects [i] == obj)
1880 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1885 found_object (uintptr_t obj)
1887 num_tracked_objects ++;
1888 tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1889 tracked_objects [num_tracked_objects - 1] = obj;
1892 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
1893 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
1896 decode_buffer (ProfContext *ctx)
1903 intptr_t method_base;
1905 uint64_t file_offset;
1907 ThreadContext *thread;
1909 #ifdef HAVE_SYS_ZLIB
1911 file_offset = gztell (ctx->gzfile);
1914 file_offset = ftell (ctx->file);
1915 if (!load_data (ctx, 48))
1918 if (read_int32 (p) != BUF_ID) {
1919 fprintf (outfile, "Incorrect buffer id: 0x%x\n", read_int32 (p));
1920 for (i = 0; i < 48; ++i) {
1921 fprintf (outfile, "0x%x%s", p [i], i % 8?" ":"\n");
1925 len = read_int32 (p + 4);
1926 time_base = read_int64 (p + 8);
1927 ptr_base = read_int64 (p + 16);
1928 obj_base = read_int64 (p + 24);
1929 thread_id = read_int64 (p + 32);
1930 method_base = read_int64 (p + 40);
1932 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
1933 thread = load_thread (ctx, thread_id);
1934 if (!load_data (ctx, len))
1936 if (!startup_time) {
1937 startup_time = time_base;
1938 if (use_time_filter) {
1939 time_from += startup_time;
1940 time_to += startup_time;
1943 thread->name = pstrdup ("Main");
1945 for (i = 0; i < thread->stack_id; ++i)
1946 thread->stack [i]->recurse_count++;
1952 int subtype = *p & 0xf0;
1953 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1954 LOG_TIME (time_base, tdiff);
1956 if (subtype == TYPE_GC_RESIZE) {
1957 uint64_t new_size = decode_uleb128 (p, &p);
1959 fprintf (outfile, "gc heap resized to %llu\n", new_size);
1961 if (new_size > max_heap_size)
1962 max_heap_size = new_size;
1963 } else if (subtype == TYPE_GC_EVENT) {
1964 uint64_t ev = decode_uleb128 (p, &p);
1965 int gen = decode_uleb128 (p, &p);
1967 fprintf (outfile, "gc event for gen%d: %s at %llu (thread: 0x%x)\n", gen, gc_event_name (ev), time_base, thread->thread_id);
1969 fprintf (outfile, "incorrect gc gen: %d\n", gen);
1972 if (ev == MONO_GC_EVENT_START) {
1973 thread->gc_start_times [gen] = time_base;
1974 gc_info [gen].count++;
1975 } else if (ev == MONO_GC_EVENT_END) {
1976 tdiff = time_base - thread->gc_start_times [gen];
1977 gc_info [gen].total_time += tdiff;
1978 if (tdiff > gc_info [gen].max_time)
1979 gc_info [gen].max_time = tdiff;
1981 } else if (subtype == TYPE_GC_MOVE) {
1982 int j, num = decode_uleb128 (p, &p);
1983 gc_object_moves += num / 2;
1984 for (j = 0; j < num; j += 2) {
1985 intptr_t obj1diff = decode_sleb128 (p, &p);
1986 intptr_t obj2diff = decode_sleb128 (p, &p);
1987 if (num_tracked_objects)
1988 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
1990 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
1993 } else if (subtype == TYPE_GC_HANDLE_CREATED) {
1994 int htype = decode_uleb128 (p, &p);
1995 uint32_t handle = decode_uleb128 (p, &p);
1996 intptr_t objdiff = decode_sleb128 (p, &p);
1999 handle_info [htype].created++;
2000 handle_info [htype].live++;
2001 add_trace_thread (thread, &handle_info [htype].traces, 1);
2002 /* FIXME: we don't take into account timing here */
2003 if (handle_info [htype].live > handle_info [htype].max_live)
2004 handle_info [htype].max_live = handle_info [htype].live;
2005 if (num_tracked_objects)
2006 track_handle (OBJ_ADDR (objdiff), htype, handle);
2008 fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
2009 } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
2010 int htype = decode_uleb128 (p, &p);
2011 uint32_t handle = decode_uleb128 (p, &p);
2014 handle_info [htype].destroyed ++;
2015 handle_info [htype].live--;
2017 fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
2021 case TYPE_METADATA: {
2022 int error = *p & TYPE_LOAD_ERR;
2023 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2025 intptr_t ptrdiff = decode_sleb128 (p, &p);
2026 LOG_TIME (time_base, tdiff);
2028 if (mtype == TYPE_CLASS) {
2029 intptr_t imptrdiff = decode_sleb128 (p, &p);
2030 uint64_t flags = decode_uleb128 (p, &p);
2032 fprintf (outfile, "non-zero flags in class\n");
2036 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
2038 add_class (ptr_base + ptrdiff, (char*)p);
2041 } else if (mtype == TYPE_IMAGE) {
2042 uint64_t flags = decode_uleb128 (p, &p);
2044 fprintf (outfile, "non-zero flags in image\n");
2048 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
2050 add_image (ptr_base + ptrdiff, (char*)p);
2053 } else if (mtype == TYPE_THREAD) {
2055 uint64_t flags = decode_uleb128 (p, &p);
2057 fprintf (outfile, "non-zero flags in thread\n");
2060 nt = get_thread (ctx, ptr_base + ptrdiff);
2061 nt->name = pstrdup ((char*)p);
2063 fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
2070 int has_bt = *p & TYPE_ALLOC_BT;
2071 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2072 intptr_t ptrdiff = decode_sleb128 (p, &p);
2073 intptr_t objdiff = decode_sleb128 (p, &p);
2076 MethodDesc* sframes [8];
2077 MethodDesc** frames = sframes;
2078 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
2079 len = decode_uleb128 (p, &p);
2080 LOG_TIME (time_base, tdiff);
2083 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
2086 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2088 fprintf (outfile, "Cannot load backtrace\n");
2092 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
2095 cd->alloc_size += len;
2097 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
2099 bt = add_trace_thread (thread, &cd->traces, len);
2100 if (find_size && len >= find_size) {
2101 if (!find_name || strstr (cd->name, find_name))
2102 found_object (OBJ_ADDR (objdiff));
2103 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
2104 found_object (OBJ_ADDR (objdiff));
2106 if (num_tracked_objects)
2107 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
2109 if (frames != sframes)
2114 int subtype = *p & 0xf0;
2115 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2116 int64_t ptrdiff = decode_sleb128 (p, &p);
2117 LOG_TIME (time_base, tdiff);
2119 method_base += ptrdiff;
2120 if (subtype == TYPE_JIT) {
2121 intptr_t codediff = decode_sleb128 (p, &p);
2122 int codelen = decode_uleb128 (p, &p);
2123 MethodDesc *jitted_method;
2125 fprintf (outfile, "jitted method %p (%s), size: %d, code: %p\n", (void*)(method_base), p, codelen, (void*)(ptr_base + codediff));
2126 jitted_method = add_method (method_base, (char*)p, ptr_base + codediff, codelen);
2127 if (!(time_base >= time_from && time_base < time_to))
2128 jitted_method->ignore_jit = 1;
2133 if ((thread_filter && thread_filter != thread->thread_id))
2135 if (!(time_base >= time_from && time_base < time_to))
2137 method = lookup_method (method_base);
2138 if (subtype == TYPE_ENTER) {
2139 add_trace_thread (thread, &method->traces, 1);
2140 push_method (thread, method, time_base);
2142 pop_method (thread, method, time_base);
2145 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
2150 int subtype = *p & 0xf0;
2151 if (subtype == TYPE_HEAP_OBJECT) {
2154 intptr_t objdiff = decode_sleb128 (p + 1, &p);
2155 intptr_t ptrdiff = decode_sleb128 (p, &p);
2156 uint64_t size = decode_uleb128 (p, &p);
2157 uintptr_t num = decode_uleb128 (p, &p);
2158 uintptr_t ref_offset;
2159 uintptr_t last_obj_offset = 0;
2160 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
2162 HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
2163 if (collect_traces) {
2164 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
2165 add_heap_shot_obj (thread->current_heap_shot, ho);
2170 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
2172 for (i = 0; i < num; ++i) {
2173 /* FIXME: use object distance to measure how good
2174 * the GC is at keeping related objects close
2176 uintptr_t offset = ctx->data_version > 1? last_obj_offset + decode_uleb128 (p, &p): -1;
2177 intptr_t obj1diff = decode_sleb128 (p, &p);
2178 last_obj_offset = offset;
2180 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
2181 if (num_tracked_objects)
2182 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
2185 fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
2186 } else if (subtype == TYPE_HEAP_ROOT) {
2187 uintptr_t num = decode_uleb128 (p + 1, &p);
2188 uintptr_t gc_num = decode_uleb128 (p, &p);
2190 for (i = 0; i < num; ++i) {
2191 intptr_t objdiff = decode_sleb128 (p, &p);
2192 int root_type = decode_uleb128 (p, &p);
2193 /* we just discard the extra info for now */
2194 uintptr_t extra_info = decode_uleb128 (p, &p);
2196 fprintf (outfile, "object %p is a %s root\n", (void*)OBJ_ADDR (objdiff), get_root_name (root_type));
2198 thread_add_root (thread, OBJ_ADDR (objdiff), root_type, extra_info);
2200 } else if (subtype == TYPE_HEAP_END) {
2201 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2202 LOG_TIME (time_base, tdiff);
2205 fprintf (outfile, "heap shot end\n");
2206 if (collect_traces) {
2207 HeapShot *hs = thread->current_heap_shot;
2208 if (hs && thread->num_roots) {
2209 /* transfer the root ownershipt to the heapshot */
2210 hs->num_roots = thread->num_roots;
2211 hs->roots = thread->roots;
2212 hs->roots_extra = thread->roots_extra;
2213 hs->roots_types = thread->roots_types;
2215 free (thread->roots);
2216 free (thread->roots_extra);
2217 free (thread->roots_types);
2219 thread->num_roots = 0;
2220 thread->size_roots = 0;
2221 thread->roots = NULL;
2222 thread->roots_extra = NULL;
2223 thread->roots_types = NULL;
2224 heap_shot_resolve_reverse_refs (hs);
2225 heap_shot_mark_objects (hs);
2226 heap_shot_free_objects (hs);
2228 thread->current_heap_shot = NULL;
2229 } else if (subtype == TYPE_HEAP_START) {
2230 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2231 LOG_TIME (time_base, tdiff);
2234 fprintf (outfile, "heap shot start\n");
2235 thread->current_heap_shot = new_heap_shot (time_base);
2239 case TYPE_MONITOR: {
2240 int event = (*p >> 4) & 0x3;
2241 int has_bt = *p & TYPE_MONITOR_BT;
2242 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2243 intptr_t objdiff = decode_sleb128 (p, &p);
2244 MethodDesc* sframes [8];
2245 MethodDesc** frames = sframes;
2248 LOG_TIME (time_base, tdiff);
2250 record = (!thread_filter || thread_filter == thread->thread_id);
2251 if (!(time_base >= time_from && time_base < time_to))
2253 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
2254 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
2256 monitor_contention++;
2257 mdesc->contentions++;
2258 thread->monitor = mdesc;
2259 thread->contention_start = time_base;
2263 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2265 fprintf (outfile, "Cannot load backtrace\n");
2269 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
2272 add_trace_thread (thread, &mdesc->traces, 1);
2274 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
2277 if (thread->monitor && thread->contention_start) {
2278 uint64_t wait_time = time_base - thread->contention_start;
2279 if (wait_time > thread->monitor->max_wait_time)
2280 thread->monitor->max_wait_time = wait_time;
2281 thread->monitor->wait_time += wait_time;
2282 thread->monitor = NULL;
2283 thread->contention_start = 0;
2286 } else if (event == MONO_PROFILER_MONITOR_DONE) {
2289 if (thread->monitor && thread->contention_start) {
2290 uint64_t wait_time = time_base - thread->contention_start;
2291 if (wait_time > thread->monitor->max_wait_time)
2292 thread->monitor->max_wait_time = wait_time;
2293 thread->monitor->wait_time += wait_time;
2294 thread->monitor = NULL;
2295 thread->contention_start = 0;
2300 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
2301 if (frames != sframes)
2305 case TYPE_EXCEPTION: {
2306 int subtype = *p & 0x70;
2307 int has_bt = *p & TYPE_EXCEPTION_BT;
2308 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2309 MethodDesc* sframes [8];
2310 MethodDesc** frames = sframes;
2312 LOG_TIME (time_base, tdiff);
2314 record = (!thread_filter || thread_filter == thread->thread_id);
2315 if (!(time_base >= time_from && time_base < time_to))
2317 if (subtype == TYPE_CLAUSE) {
2318 int clause_type = decode_uleb128 (p, &p);
2319 int clause_num = decode_uleb128 (p, &p);
2320 int64_t ptrdiff = decode_sleb128 (p, &p);
2321 method_base += ptrdiff;
2323 clause_summary [clause_type]++;
2325 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
2327 intptr_t objdiff = decode_sleb128 (p, &p);
2332 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
2334 fprintf (outfile, "Cannot load backtrace\n");
2338 add_trace_methods (frames, has_bt, &exc_traces, 1);
2341 add_trace_thread (thread, &exc_traces, 1);
2343 if (frames != sframes)
2346 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
2351 int subtype = *p & 0xf0;
2352 if (subtype == TYPE_SAMPLE_HIT) {
2354 int sample_type = decode_uleb128 (p + 1, &p);
2355 uint64_t tstamp = decode_uleb128 (p, &p);
2356 int count = decode_uleb128 (p, &p);
2357 for (i = 0; i < count; ++i) {
2358 uintptr_t ip = ptr_base + decode_sleb128 (p, &p);
2359 if ((tstamp >= time_from && tstamp < time_to))
2360 add_stat_sample (sample_type, ip);
2362 fprintf (outfile, "sample hit, type: %d at %p\n", sample_type, (void*)ip);
2364 if (ctx->data_version > 5) {
2365 count = decode_uleb128 (p, &p);
2366 for (i = 0; i < count; ++i) {
2368 int64_t ptrdiff = decode_sleb128 (p, &p);
2369 int il_offset = decode_sleb128 (p, &p);
2370 int native_offset = decode_sleb128 (p, &p);
2371 method_base += ptrdiff;
2372 method = lookup_method (method_base);
2374 fprintf (outfile, "sample hit bt %d: %s at IL offset %d (native: %d)\n", i, method->name, il_offset, native_offset);
2377 } else if (subtype == TYPE_SAMPLE_USYM) {
2378 /* un unmanaged symbol description */
2379 uintptr_t addr = ptr_base + decode_sleb128 (p + 1, &p);
2380 uintptr_t size = decode_uleb128 (p, &p);
2382 name = pstrdup ((char*)p);
2383 add_unmanaged_symbol (addr, name, size);
2385 fprintf (outfile, "unmanaged symbol %s at %p\n", name, (void*)addr);
2388 } else if (subtype == TYPE_SAMPLE_UBIN) {
2389 /* un unmanaged binary loaded in memory */
2390 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2391 uintptr_t addr = decode_sleb128 (p, &p);
2392 uint64_t offset = decode_uleb128 (p, &p);
2393 uintptr_t size = decode_uleb128 (p, &p);
2395 LOG_TIME (time_base, tdiff);
2397 name = pstrdup ((char*)p);
2398 add_unmanaged_binary (addr, name, size);
2400 fprintf (outfile, "unmanaged binary %s at %p\n", name, (void*)addr);
2403 } else if (subtype == TYPE_SAMPLE_COUNTERS_DESC) {
2404 uint64_t i, len = decode_uleb128 (p + 1, &p);
2405 for (i = 0; i < len; i++) {
2406 uint64_t type, unit, variance, index;
2407 uint64_t section = decode_uleb128 (p, &p);
2408 char *name = pstrdup ((char*)p);
2410 type = decode_uleb128 (p, &p);
2411 unit = decode_uleb128 (p, &p);
2412 variance = decode_uleb128 (p, &p);
2413 index = decode_uleb128 (p, &p);
2414 add_counter ((int)section, name, (int)type, (int)unit, (int)variance, (int)index);
2416 } else if (subtype == TYPE_SAMPLE_COUNTERS) {
2418 CounterValue *value, *previous = NULL;
2420 uint64_t timestamp = decode_uleb128 (p + 1, &p);
2421 uint64_t time_between = timestamp / 1000 * 1000 * 1000 * 1000 + startup_time;
2423 uint64_t type, index = decode_uleb128 (p, &p);
2427 for (list = counters; list; list = list->next) {
2428 if (list->counter->index == (int)index) {
2429 previous = list->counter->values_last;
2434 type = decode_uleb128 (p, &p);
2436 value = calloc (1, sizeof (CounterValue));
2437 value->timestamp = timestamp;
2440 case MONO_COUNTER_INT:
2441 #if SIZEOF_VOID_P == 4
2442 case MONO_COUNTER_WORD:
2444 value->buffer = malloc (sizeof (int32_t));
2445 *(int32_t*)value->buffer = (int32_t)decode_sleb128 (p, &p) + (previous ? (*(int32_t*)previous->buffer) : 0);
2447 case MONO_COUNTER_UINT:
2448 value->buffer = malloc (sizeof (uint32_t));
2449 *(uint32_t*)value->buffer = (uint32_t)decode_uleb128 (p, &p) + (previous ? (*(uint32_t*)previous->buffer) : 0);
2451 case MONO_COUNTER_LONG:
2452 #if SIZEOF_VOID_P == 8
2453 case MONO_COUNTER_WORD:
2455 case MONO_COUNTER_TIME_INTERVAL:
2456 value->buffer = malloc (sizeof (int64_t));
2457 *(int64_t*)value->buffer = (int64_t)decode_sleb128 (p, &p) + (previous ? (*(int64_t*)previous->buffer) : 0);
2459 case MONO_COUNTER_ULONG:
2460 value->buffer = malloc (sizeof (uint64_t));
2461 *(uint64_t*)value->buffer = (uint64_t)decode_uleb128 (p, &p) + (previous ? (*(uint64_t*)previous->buffer) : 0);
2463 case MONO_COUNTER_DOUBLE:
2464 value->buffer = malloc (sizeof (double));
2465 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
2466 for (i = 0; i < sizeof (double); i++)
2468 for (i = sizeof (double) - 1; i >= 0; i--)
2470 value->buffer[i] = *p++;
2472 case MONO_COUNTER_STRING:
2474 value->buffer = NULL;
2476 value->buffer = (unsigned char*) pstrdup ((char*)p);
2481 if (time_between >= time_from && time_between <= time_to)
2482 add_counter_value (index, value);
2490 fprintf (outfile, "unhandled profiler event: 0x%x at file offset: %llu + %d (len: %d\n)\n", *p, file_offset, p - ctx->buf, len);
2494 thread->last_time = time_base;
2495 for (i = 0; i < thread->stack_id; ++i)
2496 thread->stack [i]->recurse_count = 0;
2501 load_file (char *name)
2504 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
2505 if (strcmp (name, "-") == 0)
2508 ctx->file = fopen (name, "rb");
2510 printf ("Cannot open file: %s\n", name);
2513 #if defined (HAVE_SYS_ZLIB)
2514 if (ctx->file != stdin)
2515 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
2517 if (!load_data (ctx, 32))
2520 if (read_int32 (p) != LOG_HEADER_ID || p [6] > LOG_DATA_VERSION)
2522 ctx->version_major = p [4];
2523 ctx->version_minor = p [5];
2524 ctx->data_version = p [6];
2525 /* reading 64 bit files on 32 bit systems not supported yet */
2526 if (p [7] > sizeof (void*))
2528 if (read_int32 (p + 20)) /* flags must be 0 */
2530 ctx->startup_time = read_int64 (p + 8);
2531 ctx->timer_overhead = read_int32 (p + 16);
2532 ctx->pid = read_int32 (p + 24);
2533 ctx->port = read_int16 (p + 28);
2541 static int alloc_sort_mode = ALLOC_SORT_BYTES;
2544 compare_class (const void *a, const void *b)
2546 ClassDesc *const*A = a;
2547 ClassDesc *const*B = b;
2548 uint64_t vala, valb;
2549 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2550 vala = (*A)->alloc_size;
2551 valb = (*B)->alloc_size;
2553 vala = (*A)->allocs;
2554 valb = (*B)->allocs;
2564 dump_header (ProfContext *ctx)
2566 time_t st = ctx->startup_time / 1000;
2567 char *t = ctime (&st);
2568 fprintf (outfile, "\nMono log profiler data\n");
2569 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
2570 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
2571 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
2572 fprintf (outfile, "\tProgram startup: %s", t);
2574 fprintf (outfile, "\tProgram ID: %d\n", ctx->pid);
2576 fprintf (outfile, "\tServer listening on: %d\n", ctx->port);
2580 dump_traces (TraceDesc *traces, const char *desc)
2587 sort_context_array (traces);
2588 for (j = 0; j < traces->count; ++j) {
2591 bt = traces->traces [j].bt;
2594 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
2595 for (k = 0; k < bt->count; ++k)
2596 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
2601 dump_threads (ProfContext *ctx)
2603 ThreadContext *thread;
2604 fprintf (outfile, "\nThread summary\n");
2605 for (thread = ctx->threads; thread; thread = thread->next) {
2606 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
2611 dump_exceptions (void)
2614 fprintf (outfile, "\nException summary\n");
2615 fprintf (outfile, "\tThrows: %llu\n", throw_count);
2616 dump_traces (&exc_traces, "throws");
2617 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
2618 if (!clause_summary [i])
2620 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
2625 compare_monitor (const void *a, const void *b)
2627 MonitorDesc *const*A = a;
2628 MonitorDesc *const*B = b;
2629 if ((*B)->wait_time == (*A)->wait_time)
2631 if ((*B)->wait_time < (*A)->wait_time)
2637 dump_monitors (void)
2639 MonitorDesc **monitors;
2643 monitors = malloc (sizeof (void*) * num_monitors);
2644 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
2645 MonitorDesc *mdesc = monitor_hash [i];
2647 monitors [j++] = mdesc;
2648 mdesc = mdesc->next;
2651 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
2652 fprintf (outfile, "\nMonitor lock summary\n");
2653 for (i = 0; i < num_monitors; ++i) {
2654 MonitorDesc *mdesc = monitors [i];
2655 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
2656 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
2657 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
2658 dump_traces (&mdesc->traces, "contentions");
2660 fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
2661 fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
2662 fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
2669 fprintf (outfile, "\nGC summary\n");
2670 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
2671 fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
2672 fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
2673 for (i = 0; i < 3; ++i) {
2674 if (!gc_info [i].count)
2676 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
2677 i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
2678 gc_info [i].total_time / gc_info [i].count / 1000);
2680 for (i = 0; i < 3; ++i) {
2681 if (!handle_info [i].max_live)
2683 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
2684 get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
2685 dump_traces (&handle_info [i].traces, "created");
2694 int compiled_methods = 0;
2696 fprintf (outfile, "\nJIT summary\n");
2697 for (i = 0; i < HASH_SIZE; ++i) {
2698 m = method_hash [i];
2699 for (m = method_hash [i]; m; m = m->next) {
2700 if (!m->code || m->ignore_jit)
2703 code_size += m->len;
2706 fprintf (outfile, "\tCompiled methods: %d\n", compiled_methods);
2707 fprintf (outfile, "\tGenerated code size: %d\n", code_size);
2711 dump_allocations (void)
2714 intptr_t allocs = 0;
2716 int header_done = 0;
2717 ClassDesc **classes = malloc (num_classes * sizeof (void*));
2720 for (i = 0; i < HASH_SIZE; ++i) {
2721 cd = class_hash [i];
2727 qsort (classes, num_classes, sizeof (void*), compare_class);
2728 for (i = 0; i < num_classes; ++i) {
2732 allocs += cd->allocs;
2733 size += cd->alloc_size;
2734 if (!header_done++) {
2735 fprintf (outfile, "\nAllocation summary\n");
2736 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
2738 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
2739 dump_traces (&cd->traces, "bytes");
2742 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
2751 static int method_sort_mode = METHOD_SORT_TOTAL;
2754 compare_method (const void *a, const void *b)
2756 MethodDesc *const*A = a;
2757 MethodDesc *const*B = b;
2758 uint64_t vala, valb;
2759 if (method_sort_mode == METHOD_SORT_SELF) {
2760 vala = (*A)->self_time;
2761 valb = (*B)->self_time;
2762 } else if (method_sort_mode == METHOD_SORT_CALLS) {
2766 vala = (*A)->total_time;
2767 valb = (*B)->total_time;
2777 dump_metadata (void)
2779 fprintf (outfile, "\nMetadata summary\n");
2780 fprintf (outfile, "\tLoaded images: %d\n", num_images);
2784 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
2785 image = image_hash [i];
2787 fprintf (outfile, "\t\t%s\n", image->filename);
2788 image = image->next;
2800 int header_done = 0;
2801 MethodDesc **methods = malloc (num_methods * sizeof (void*));
2804 for (i = 0; i < HASH_SIZE; ++i) {
2805 cd = method_hash [i];
2807 cd->total_time = cd->self_time + cd->callee_time;
2812 qsort (methods, num_methods, sizeof (void*), compare_method);
2813 for (i = 0; i < num_methods; ++i) {
2820 msecs = cd->total_time / 1000000;
2821 smsecs = (cd->total_time - cd->callee_time) / 1000000;
2822 if (!msecs && !verbose)
2824 if (!header_done++) {
2825 fprintf (outfile, "\nMethod call summary\n");
2826 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
2828 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
2829 dump_traces (&cd->traces, "calls");
2832 fprintf (outfile, "Total calls: %llu\n", calls);
2836 compare_heap_class (const void *a, const void *b)
2838 HeapClassDesc *const*A = a;
2839 HeapClassDesc *const*B = b;
2840 uint64_t vala, valb;
2841 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2842 vala = (*A)->total_size;
2843 valb = (*B)->total_size;
2856 compare_rev_class (const void *a, const void *b)
2858 const HeapClassRevRef *A = a;
2859 const HeapClassRevRef *B = b;
2860 if (B->count == A->count)
2862 if (B->count < A->count)
2868 dump_rev_claases (HeapClassRevRef *revs, int count)
2875 for (j = 0; j < count; ++j) {
2876 HeapClassDesc *cd = revs [j].klass;
2877 fprintf (outfile, "\t\t%llu references from: %s\n", revs [j].count, cd->klass->name);
2882 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
2889 HeapClassDesc **sorted;
2890 sorted = malloc (sizeof (void*) * hs->class_count);
2891 for (i = 0; i < hs->hash_size; ++i) {
2892 cd = hs->class_hash [i];
2896 size += cd->total_size;
2897 sorted [ccount++] = cd;
2899 hs->sorted = sorted;
2900 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
2901 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d, roots: %d\n",
2902 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount, hs->num_roots);
2903 if (!verbose && ccount > 30)
2905 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
2906 for (i = 0; i < ccount; ++i) {
2907 HeapClassRevRef *rev_sorted;
2909 HeapClassDesc *ocd = NULL;
2912 ocd = heap_class_lookup (last_hs, cd->klass);
2913 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
2915 int64_t bdiff = cd->total_size - ocd->total_size;
2916 int64_t cdiff = cd->count - ocd->count;
2917 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
2919 fprintf (outfile, "\n");
2921 if (!collect_traces)
2923 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
2925 for (j = 0; j < cd->rev_hash_size; ++j) {
2926 if (cd->rev_hash [j].klass)
2927 rev_sorted [k++] = cd->rev_hash [j];
2929 assert (cd->rev_count == k);
2930 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
2931 if (cd->root_references)
2932 fprintf (outfile, "\t\t%d root references (%d pinning)\n", cd->root_references, cd->pinned_references);
2933 dump_rev_claases (rev_sorted, cd->rev_count);
2940 compare_heap_shots (const void *a, const void *b)
2942 HeapShot *const*A = a;
2943 HeapShot *const*B = b;
2944 if ((*B)->timestamp == (*A)->timestamp)
2946 if ((*B)->timestamp > (*A)->timestamp)
2952 dump_heap_shots (void)
2954 HeapShot **hs_sorted;
2956 HeapShot *last_hs = NULL;
2960 hs_sorted = malloc (num_heap_shots * sizeof (void*));
2961 fprintf (outfile, "\nHeap shot summary\n");
2963 for (hs = heap_shots; hs; hs = hs->next)
2964 hs_sorted [i++] = hs;
2965 qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
2966 for (i = 0; i < num_heap_shots; ++i) {
2968 heap_shot_summary (hs, i, last_hs);
2974 flush_context (ProfContext *ctx)
2976 ThreadContext *thread;
2977 /* FIXME: sometimes there are leftovers: indagate */
2978 for (thread = ctx->threads; thread; thread = thread->next) {
2979 while (thread->stack_id) {
2981 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
2982 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
2987 static const char *reports = "header,jit,gc,sample,alloc,call,metadata,exception,monitor,thread,heapshot,counters";
2990 match_option (const char *p, const char *opt)
2992 int len = strlen (opt);
2993 if (strncmp (p, opt, len) == 0) {
3002 print_reports (ProfContext *ctx, const char *reps, int parse_only)
3006 for (p = reps; *p; p = opt) {
3007 if ((opt = match_option (p, "header")) != p) {
3012 if ((opt = match_option (p, "thread")) != p) {
3017 if ((opt = match_option (p, "gc")) != p) {
3022 if ((opt = match_option (p, "jit")) != p) {
3027 if ((opt = match_option (p, "alloc")) != p) {
3029 dump_allocations ();
3032 if ((opt = match_option (p, "call")) != p) {
3037 if ((opt = match_option (p, "metadata")) != p) {
3042 if ((opt = match_option (p, "exception")) != p) {
3047 if ((opt = match_option (p, "monitor")) != p) {
3052 if ((opt = match_option (p, "heapshot")) != p) {
3057 if ((opt = match_option (p, "sample")) != p) {
3062 if ((opt = match_option (p, "counters")) != p) {
3073 add_find_spec (const char *p)
3075 if (p [0] == 'S' && p [1] == ':') {
3077 find_size = strtoul (p + 2, &vale, 10);
3079 } else if (p [0] == 'T' && p [1] == ':') {
3089 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
3090 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
3091 printf ("FILENAME can be '-' to read from standard input.\n");
3092 printf ("Options:\n");
3093 printf ("\t--help display this help\n");
3094 printf ("\t--out=FILE write to FILE instead of stdout\n");
3095 printf ("\t--traces collect and show backtraces\n");
3096 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
3097 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
3098 printf ("\t %s\n", reports);
3099 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
3100 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
3101 printf ("\t--counters-sort=MODE sort counters according to MODE: time, category\n");
3102 printf ("\t only accessible in verbose mode\n");
3103 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
3104 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
3105 printf ("\t S:minimum_size or T:partial_name\n");
3106 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
3107 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
3108 printf ("\t--verbose increase verbosity level\n");
3109 printf ("\t--debug display decoding debug info for mprof-report devs\n");
3113 main (int argc, char *argv[])
3118 for (i = 1; i < argc; ++i) {
3119 if (strcmp ("--debug", argv [i]) == 0) {
3121 } else if (strcmp ("--help", argv [i]) == 0) {
3124 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
3125 const char *val = argv [i] + 13;
3126 if (strcmp (val, "bytes") == 0) {
3127 alloc_sort_mode = ALLOC_SORT_BYTES;
3128 } else if (strcmp (val, "count") == 0) {
3129 alloc_sort_mode = ALLOC_SORT_COUNT;
3134 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
3135 const char *val = argv [i] + 14;
3136 if (strcmp (val, "total") == 0) {
3137 method_sort_mode = METHOD_SORT_TOTAL;
3138 } else if (strcmp (val, "self") == 0) {
3139 method_sort_mode = METHOD_SORT_SELF;
3140 } else if (strcmp (val, "calls") == 0) {
3141 method_sort_mode = METHOD_SORT_CALLS;
3146 } else if (strncmp ("--counters-sort=", argv [i], 16) == 0) {
3147 const char *val = argv [i] + 16;
3148 if (strcmp (val, "time") == 0) {
3149 counters_sort_mode = COUNTERS_SORT_TIME;
3150 } else if (strcmp (val, "category") == 0) {
3151 counters_sort_mode = COUNTERS_SORT_CATEGORY;
3156 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
3157 const char *val = argv [i] + 10;
3158 if (!print_reports (NULL, val, 1)) {
3163 } else if (strncmp ("--out=", argv [i], 6) == 0) {
3164 const char *val = argv [i] + 6;
3165 outfile = fopen (val, "w");
3167 printf ("Cannot open output file: %s\n", val);
3170 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
3171 const char *val = argv [i] + 12;
3173 trace_max = strtoul (val, &vale, 10);
3174 } else if (strncmp ("--find=", argv [i], 7) == 0) {
3175 const char *val = argv [i] + 7;
3176 if (!add_find_spec (val)) {
3180 } else if (strncmp ("--track=", argv [i], 8) == 0) {
3181 const char *val = argv [i] + 8;
3184 uintptr_t tracked_obj;
3189 tracked_obj = strtoul (val, &vale, 0);
3190 found_object (tracked_obj);
3193 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
3194 const char *val = argv [i] + 9;
3196 thread_filter = strtoul (val, &vale, 0);
3197 } else if (strncmp ("--time=", argv [i], 7) == 0) {
3198 char *val = pstrdup (argv [i] + 7);
3199 double from_secs, to_secs;
3200 char *top = strchr (val, '-');
3206 from_secs = atof (val);
3207 to_secs = atof (top);
3209 if (from_secs > to_secs) {
3213 time_from = from_secs * 1000000000;
3214 time_to = to_secs * 1000000000;
3215 use_time_filter = 1;
3216 } else if (strcmp ("--verbose", argv [i]) == 0) {
3218 } else if (strcmp ("--traces", argv [i]) == 0) {
3229 ctx = load_file (argv [i]);
3231 printf ("Not a log profiler data file (or unsupported version).\n");
3234 while (decode_buffer (ctx));
3235 flush_context (ctx);
3236 if (num_tracked_objects)
3238 print_reports (ctx, reports, 0);