2 * decode.c: mprof-report program source: decode and analyze the log profiler data
5 * Paolo Molaro (lupus@ximian.com)
6 * Alex Rønne Petersen (alexrp@xamarin.com)
8 * Copyright 2010 Novell, Inc (http://www.novell.com)
12 * The Coverage XML output schema
22 * <coverage> - The root element of the documentation. It can contain any number of
23 * <assembly>, <class> or <method> elements.
25 * - version: The version number for the file format - (eg: "0.3")
26 * <assembly> - Contains data about assemblies. Has no child elements
28 * - name: The name of the assembly - (eg: "System.Xml")
29 * - guid: The GUID of the assembly
30 * - filename: The filename of the assembly
31 * - method-count: The number of methods in the assembly
32 * - full: The number of fully covered methods
33 * - partial: The number of partially covered methods
34 * <class> - Contains data about classes. Has no child elements
36 * - name: The name of the class
37 * - method-count: The number of methods in the class
38 * - full: The number of fully covered methods
39 * - partial: The number of partially covered methods
40 * <method> - Contains data about methods. Can contain any number of <statement> elements
42 * - assembly: The name of the parent assembly
43 * - class: The name of the parent class
44 * - name: The name of the method, with all it's parameters
45 * - filename: The name of the source file containing this method
47 * <statement> - Contains data about IL statements. Has no child elements
49 * - offset: The offset of the statement in the IL code after the previous
51 * - counter: 1 if the line was covered, 0 if it was not
52 * - line: The line number in the parent method's file
53 * - column: The column on the line
61 #if !defined(__APPLE__) && !defined(__FreeBSD__)
66 #if defined (HAVE_SYS_ZLIB)
70 #include <mono/metadata/profiler.h>
71 #include <mono/metadata/object.h>
72 #include <mono/metadata/debug-helpers.h>
73 #include <mono/utils/mono-counters.h>
75 #define HASH_SIZE 9371
76 #define SMALL_HASH_SIZE 31
78 #if defined(__native_client__) || defined(__native_client_codegen__)
79 volatile int __nacl_thread_suspension_needed = 0;
80 void __nacl_suspend_thread_if_needed() {}
84 static int collect_traces = 0;
85 static int show_traces = 0;
86 static int trace_max = 6;
87 static int verbose = 0;
88 static uintptr_t *tracked_objects = 0;
89 static int num_tracked_objects = 0;
90 static uintptr_t thread_filter = 0;
91 static uint64_t find_size = 0;
92 static const char* find_name = NULL;
93 static uint64_t time_from = 0;
94 static uint64_t time_to = 0xffffffffffffffffULL;
95 static int use_time_filter = 0;
96 static uint64_t startup_time = 0;
97 static FILE* outfile = NULL;
98 static FILE* coverage_outfile = NULL;
101 read_int16 (unsigned char *p)
103 int32_t value = *p++;
104 value |= (*p++) << 8;
109 read_int32 (unsigned char *p)
111 int32_t value = *p++;
112 value |= (*p++) << 8;
113 value |= (*p++) << 16;
114 value |= (uint32_t)(*p++) << 24;
119 read_int64 (unsigned char *p)
121 uint64_t value = *p++;
122 value |= (*p++) << 8;
123 value |= (*p++) << 16;
124 value |= (uint64_t)(*p++) << 24;
125 value |= (uint64_t)(*p++) << 32;
126 value |= (uint64_t)(*p++) << 40;
127 value |= (uint64_t)(*p++) << 48;
128 value |= (uint64_t)(*p++) << 54;
133 pstrdup (const char *s)
135 int len = strlen (s) + 1;
136 char *p = malloc (len);
141 typedef struct _CounterValue CounterValue;
142 struct _CounterValue {
144 unsigned char *buffer;
148 typedef struct _Counter Counter;
156 CounterValue *values;
157 CounterValue *values_last;
160 typedef struct _CounterList CounterList;
161 struct _CounterList {
166 typedef struct _CounterSection CounterSection;
167 struct _CounterSection {
169 CounterList *counters;
170 CounterList *counters_last;
171 CounterSection *next;
174 typedef struct _CounterTimestamp CounterTimestamp;
175 struct _CounterTimestamp {
177 CounterSection *sections;
178 CounterSection *sections_last;
179 CounterTimestamp *next;
182 static CounterList *counters = NULL;
183 static CounterSection *counters_sections = NULL;
184 static CounterTimestamp *counters_timestamps = NULL;
188 COUNTERS_SORT_CATEGORY
191 static int counters_sort_mode = COUNTERS_SORT_TIME;
194 add_counter_to_section (Counter *counter)
196 CounterSection *csection, *s;
199 clist = calloc (1, sizeof (CounterList));
200 clist->counter = counter;
202 for (csection = counters_sections; csection; csection = csection->next) {
203 if (strcmp (csection->value, counter->section) == 0) {
204 /* If section exist */
205 if (!csection->counters)
206 csection->counters = clist;
208 csection->counters_last->next = clist;
209 csection->counters_last = clist;
214 /* If section does not exist */
215 csection = calloc (1, sizeof (CounterSection));
216 csection->value = counter->section;
217 csection->counters = clist;
218 csection->counters_last = clist;
220 if (!counters_sections) {
221 counters_sections = csection;
223 s = counters_sections;
231 add_counter (const char *section, const char *name, int type, int unit, int variance, int index)
233 CounterList *list, *l;
236 for (list = counters; list; list = list->next)
237 if (list->counter->index == index)
240 counter = calloc (1, sizeof (Counter));
241 counter->section = section;
242 counter->name = name;
243 counter->type = type;
244 counter->unit = unit;
245 counter->variance = variance;
246 counter->index = index;
248 list = calloc (1, sizeof (CounterList));
249 list->counter = counter;
260 if (counters_sort_mode == COUNTERS_SORT_CATEGORY || !verbose)
261 add_counter_to_section (counter);
265 add_counter_to_timestamp (uint64_t timestamp, Counter *counter)
267 CounterTimestamp *ctimestamp, *t;
268 CounterSection *csection;
271 clist = calloc (1, sizeof (CounterList));
272 clist->counter = counter;
274 for (ctimestamp = counters_timestamps; ctimestamp; ctimestamp = ctimestamp->next) {
275 if (ctimestamp->value == timestamp) {
276 for (csection = ctimestamp->sections; csection; csection = csection->next) {
277 if (strcmp (csection->value, counter->section) == 0) {
278 /* if timestamp exist and section exist */
279 if (!csection->counters)
280 csection->counters = clist;
282 csection->counters_last->next = clist;
283 csection->counters_last = clist;
288 /* if timestamp exist and section does not exist */
289 csection = calloc (1, sizeof (CounterSection));
290 csection->value = counter->section;
291 csection->counters = clist;
292 csection->counters_last = clist;
294 if (!ctimestamp->sections)
295 ctimestamp->sections = csection;
297 ctimestamp->sections_last->next = csection;
298 ctimestamp->sections_last = csection;
303 /* If timestamp do not exist and section does not exist */
304 csection = calloc (1, sizeof (CounterSection));
305 csection->value = counter->section;
306 csection->counters = clist;
307 csection->counters_last = clist;
309 ctimestamp = calloc (1, sizeof (CounterTimestamp));
310 ctimestamp->value = timestamp;
311 ctimestamp->sections = csection;
312 ctimestamp->sections_last = csection;
314 if (!counters_timestamps) {
315 counters_timestamps = ctimestamp;
317 t = counters_timestamps;
320 t->next = ctimestamp;
325 add_counter_value (int index, CounterValue *value)
329 for (list = counters; list; list = list->next) {
330 if (list->counter->index == index) {
331 if (!list->counter->values)
332 list->counter->values = value;
334 list->counter->values_last->next = value;
335 list->counter->values_last = value;
337 if (counters_sort_mode == COUNTERS_SORT_TIME)
338 add_counter_to_timestamp (value->timestamp, list->counter);
346 section_name (int section)
349 case MONO_COUNTER_JIT: return "Mono JIT";
350 case MONO_COUNTER_GC: return "Mono GC";
351 case MONO_COUNTER_METADATA: return "Mono Metadata";
352 case MONO_COUNTER_GENERICS: return "Mono Generics";
353 case MONO_COUNTER_SECURITY: return "Mono Security";
354 case MONO_COUNTER_RUNTIME: return "Mono Runtime";
355 case MONO_COUNTER_SYSTEM: return "Mono System";
356 default: return "<unknown>";
364 case MONO_COUNTER_INT: return "Int";
365 case MONO_COUNTER_UINT: return "UInt";
366 case MONO_COUNTER_WORD: return "Word";
367 case MONO_COUNTER_LONG: return "Long";
368 case MONO_COUNTER_ULONG: return "ULong";
369 case MONO_COUNTER_DOUBLE: return "Double";
370 case MONO_COUNTER_STRING: return "String";
371 case MONO_COUNTER_TIME_INTERVAL: return "Time Interval";
372 default: return "<unknown>";
380 case MONO_COUNTER_RAW: return "Raw";
381 case MONO_COUNTER_BYTES: return "Bytes";
382 case MONO_COUNTER_TIME: return "Time";
383 case MONO_COUNTER_COUNT: return "Count";
384 case MONO_COUNTER_PERCENTAGE: return "Percentage";
385 default: return "<unknown>";
390 variance_name (int variance)
393 case MONO_COUNTER_MONOTONIC: return "Monotonic";
394 case MONO_COUNTER_CONSTANT: return "Constant";
395 case MONO_COUNTER_VARIABLE: return "Variable";
396 default: return "<unknown>";
401 dump_counters_value (Counter *counter, const char *key_format, const char *key, void *value)
406 snprintf (format, sizeof (format), "%s : %%s\n", key_format);
407 fprintf (outfile, format, key, "<null>");
409 switch (counter->type) {
410 case MONO_COUNTER_INT:
411 #if SIZEOF_VOID_P == 4
412 case MONO_COUNTER_WORD:
414 snprintf (format, sizeof (format), "%s : %%d\n", key_format);
415 fprintf (outfile, format, key, *(int32_t*)value);
417 case MONO_COUNTER_UINT:
418 snprintf (format, sizeof (format), "%s : %%u\n", key_format);
419 fprintf (outfile, format, key, *(uint32_t*)value);
421 case MONO_COUNTER_LONG:
422 #if SIZEOF_VOID_P == 8
423 case MONO_COUNTER_WORD:
425 case MONO_COUNTER_TIME_INTERVAL:
426 if (counter->type == MONO_COUNTER_LONG && counter->unit == MONO_COUNTER_TIME) {
427 snprintf (format, sizeof (format), "%s : %%0.3fms\n", key_format);
428 fprintf (outfile, format, key, (double)*(int64_t*)value / 10000.0);
429 } else if (counter->type == MONO_COUNTER_TIME_INTERVAL) {
430 snprintf (format, sizeof (format), "%s : %%0.3fms\n", key_format);
431 fprintf (outfile, format, key, (double)*(int64_t*)value / 1000.0);
433 snprintf (format, sizeof (format), "%s : %%u\n", key_format);
434 fprintf (outfile, format, key, *(int64_t*)value);
437 case MONO_COUNTER_ULONG:
438 snprintf (format, sizeof (format), "%s : %%llu\n", key_format);
439 fprintf (outfile, format, key, *(uint64_t*)value);
441 case MONO_COUNTER_DOUBLE:
442 snprintf (format, sizeof (format), "%s : %%f\n", key_format);
443 fprintf (outfile, format, key, *(double*)value);
445 case MONO_COUNTER_STRING:
446 snprintf (format, sizeof (format), "%s : %%s\n", key_format);
447 fprintf (outfile, format, key, *(char*)value);
457 CounterValue *cvalue;
458 CounterTimestamp *ctimestamp;
459 CounterSection *csection;
461 char strtimestamp[17];
462 int i, section_printed;
464 fprintf (outfile, "\nCounters:\n");
467 char counters_to_print[][64] = {
469 "Methods JITted using mono JIT",
470 "Methods JITted using LLVM",
471 "Total time spent JITting (sec)",
479 "CPU Load Average - 1min",
480 "CPU Load Average - 5min",
481 "CPU Load Average - 15min",
485 for (csection = counters_sections; csection; csection = csection->next) {
488 for (clist = csection->counters; clist; clist = clist->next) {
489 counter = clist->counter;
490 if (!counter->values_last)
493 for (i = 0; counters_to_print [i][0] != 0; i++) {
494 if (strcmp (counters_to_print [i], counter->name) == 0) {
495 if (!section_printed) {
496 fprintf (outfile, "\t%s:\n", csection->value);
500 dump_counters_value (counter, "\t\t%-30s", counter->name, counter->values_last->buffer);
506 } else if (counters_sort_mode == COUNTERS_SORT_TIME) {
507 for (ctimestamp = counters_timestamps; ctimestamp; ctimestamp = ctimestamp->next) {
508 fprintf (outfile, "\t%llu:%02llu:%02llu:%02llu.%03llu:\n",
509 (unsigned long long) (ctimestamp->value / 1000 / 60 / 60 / 24 % 1000),
510 (unsigned long long) (ctimestamp->value / 1000 / 60 / 60 % 24),
511 (unsigned long long) (ctimestamp->value / 1000 / 60 % 60),
512 (unsigned long long) (ctimestamp->value / 1000 % 60),
513 (unsigned long long) (ctimestamp->value % 1000));
515 for (csection = ctimestamp->sections; csection; csection = csection->next) {
516 fprintf (outfile, "\t\t%s:\n", csection->value);
518 for (clist = csection->counters; clist; clist = clist->next) {
519 counter = clist->counter;
520 for (cvalue = counter->values; cvalue; cvalue = cvalue->next) {
521 if (cvalue->timestamp != ctimestamp->value)
524 dump_counters_value (counter, "\t\t\t%-30s", counter->name, cvalue->buffer);
529 } else if (counters_sort_mode == COUNTERS_SORT_CATEGORY) {
530 for (csection = counters_sections; csection; csection = csection->next) {
531 fprintf (outfile, "\t%s:\n", csection->value);
533 for (clist = csection->counters; clist; clist = clist->next) {
534 counter = clist->counter;
535 fprintf (outfile, "\t\t%s: [type: %s, unit: %s, variance: %s]\n",
536 counter->name, type_name (counter->type), unit_name (counter->unit), variance_name (counter->variance));
538 for (cvalue = counter->values; cvalue; cvalue = cvalue->next) {
539 snprintf (strtimestamp, sizeof (strtimestamp), "%llu:%02llu:%02llu:%02llu.%03llu",
540 (unsigned long long) (cvalue->timestamp / 1000 / 60 / 60 / 24 % 1000),
541 (unsigned long long) (cvalue->timestamp / 1000 / 60 / 60 % 24),
542 (unsigned long long) (cvalue->timestamp / 1000 / 60 % 60),
543 (unsigned long long) (cvalue->timestamp / 1000 % 60),
544 (unsigned long long) (cvalue->timestamp % 1000));
546 dump_counters_value (counter, "\t\t\t%s", strtimestamp, cvalue->buffer);
553 static int num_images;
554 typedef struct _ImageDesc ImageDesc;
561 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
564 add_image (intptr_t image, char *name)
566 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
567 ImageDesc *cd = malloc (sizeof (ImageDesc));
569 cd->filename = pstrdup (name);
570 cd->next = image_hash [slot];
571 image_hash [slot] = cd;
575 typedef struct _BackTrace BackTrace;
587 typedef struct _ClassDesc ClassDesc;
597 static ClassDesc* class_hash [HASH_SIZE] = {0};
598 static int num_classes = 0;
601 add_class (intptr_t klass, const char *name)
603 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
605 cd = class_hash [slot];
606 while (cd && cd->klass != klass)
608 /* we resolved an unknown class (unless we had the code unloaded) */
610 /*printf ("resolved unknown: %s\n", name);*/
612 cd->name = pstrdup (name);
615 cd = calloc (sizeof (ClassDesc), 1);
617 cd->name = pstrdup (name);
618 cd->next = class_hash [slot];
621 cd->traces.count = 0;
623 cd->traces.traces = NULL;
624 class_hash [slot] = cd;
630 lookup_class (intptr_t klass)
632 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
633 ClassDesc *cd = class_hash [slot];
634 while (cd && cd->klass != klass)
638 snprintf (buf, sizeof (buf), "unresolved class %p", (void*)klass);
639 return add_class (klass, buf);
644 typedef struct _MethodDesc MethodDesc;
653 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 */
656 uint64_t callee_time;
661 static MethodDesc* method_hash [HASH_SIZE] = {0};
662 static int num_methods = 0;
665 add_method (intptr_t method, const char *name, intptr_t code, int len)
667 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
669 cd = method_hash [slot];
670 while (cd && cd->method != method)
672 /* we resolved an unknown method (unless we had the code unloaded) */
676 /*printf ("resolved unknown: %s\n", name);*/
678 cd->name = pstrdup (name);
681 cd = calloc (sizeof (MethodDesc), 1);
683 cd->name = pstrdup (name);
688 cd->traces.count = 0;
690 cd->traces.traces = NULL;
691 cd->next = method_hash [slot];
692 method_hash [slot] = cd;
698 lookup_method (intptr_t method)
700 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
701 MethodDesc *cd = method_hash [slot];
702 while (cd && cd->method != method)
706 snprintf (buf, sizeof (buf), "unknown method %p", (void*)method);
707 return add_method (method, buf, 0, 0);
712 static int num_stat_samples = 0;
713 static int size_stat_samples = 0;
714 uintptr_t *stat_samples = NULL;
715 int *stat_sample_desc = NULL;
718 add_stat_sample (int type, uintptr_t ip) {
719 if (num_stat_samples == size_stat_samples) {
720 size_stat_samples *= 2;
721 if (!size_stat_samples)
722 size_stat_samples = 32;
723 stat_samples = realloc (stat_samples, size_stat_samples * sizeof (uintptr_t));
724 stat_sample_desc = realloc (stat_sample_desc, size_stat_samples * sizeof (int));
726 stat_samples [num_stat_samples] = ip;
727 stat_sample_desc [num_stat_samples++] = type;
731 lookup_method_by_ip (uintptr_t ip)
736 for (i = 0; i < HASH_SIZE; ++i) {
739 //printf ("checking %p against %p-%p\n", (void*)ip, (void*)(m->code), (void*)(m->code + m->len));
740 if (ip >= (uintptr_t)m->code && ip < (uintptr_t)m->code + m->len) {
750 compare_method_samples (const void *a, const void *b)
752 MethodDesc *const*A = a;
753 MethodDesc *const*B = b;
754 if ((*A)->sample_hits == (*B)->sample_hits)
756 if ((*B)->sample_hits < (*A)->sample_hits)
761 typedef struct _UnmanagedSymbol UnmanagedSymbol;
762 struct _UnmanagedSymbol {
763 UnmanagedSymbol *parent;
768 uintptr_t sample_hits;
771 static UnmanagedSymbol **usymbols = NULL;
772 static int usymbols_size = 0;
773 static int usymbols_num = 0;
776 compare_usymbol_addr (const void *a, const void *b)
778 UnmanagedSymbol *const*A = a;
779 UnmanagedSymbol *const*B = b;
780 if ((*B)->addr == (*A)->addr)
782 if ((*B)->addr > (*A)->addr)
788 compare_usymbol_samples (const void *a, const void *b)
790 UnmanagedSymbol *const*A = a;
791 UnmanagedSymbol *const*B = b;
792 if ((*B)->sample_hits == (*A)->sample_hits)
794 if ((*B)->sample_hits < (*A)->sample_hits)
800 add_unmanaged_symbol (uintptr_t addr, char *name, uintptr_t size)
802 UnmanagedSymbol *sym;
803 if (usymbols_num == usymbols_size) {
804 int new_size = usymbols_size * 2;
807 usymbols = realloc (usymbols, sizeof (void*) * new_size);
808 usymbols_size = new_size;
810 sym = calloc (sizeof (UnmanagedSymbol), 1);
814 usymbols [usymbols_num++] = sym;
817 /* only valid after the symbols are sorted */
818 static UnmanagedSymbol*
819 lookup_unmanaged_symbol (uintptr_t addr)
821 int r = usymbols_num - 1;
823 UnmanagedSymbol *sym;
828 if (addr == sym->addr)
830 if (addr < sym->addr) {
832 } else if (addr > sym->addr) {
837 if (last_best >= 0 && (addr - usymbols [last_best]->addr) < 4096)
838 return usymbols [last_best];
842 /* we use the same structure for binaries */
843 static UnmanagedSymbol **ubinaries = NULL;
844 static int ubinaries_size = 0;
845 static int ubinaries_num = 0;
848 add_unmanaged_binary (uintptr_t addr, char *name, uintptr_t size)
850 UnmanagedSymbol *sym;
851 if (ubinaries_num == ubinaries_size) {
852 int new_size = ubinaries_size * 2;
855 ubinaries = realloc (ubinaries, sizeof (void*) * new_size);
856 ubinaries_size = new_size;
858 sym = calloc (sizeof (UnmanagedSymbol), 1);
863 ubinaries [ubinaries_num++] = sym;
866 static UnmanagedSymbol*
867 lookup_unmanaged_binary (uintptr_t addr)
870 for (i = 0; i < ubinaries_num; ++i) {
871 UnmanagedSymbol *ubin = ubinaries [i];
872 if (addr >= ubin->addr && addr < ubin->addr + ubin->size) {
880 sample_type_name (int type)
883 case SAMPLE_CYCLES: return "cycles";
884 case SAMPLE_INSTRUCTIONS: return "instructions retired";
885 case SAMPLE_CACHE_MISSES: return "cache misses";
886 case SAMPLE_CACHE_REFS: return "cache references";
887 case SAMPLE_BRANCHES: return "executed branches";
888 case SAMPLE_BRANCH_MISSES: return "unpredicted branches";
894 set_usym_parent (UnmanagedSymbol** cachedus, int count)
897 for (i = 0; i < count; ++i) {
898 UnmanagedSymbol *ubin = lookup_unmanaged_binary (cachedus [i]->addr);
899 if (ubin == cachedus [i])
901 cachedus [i]->parent = ubin;
906 print_usym (UnmanagedSymbol* um)
909 fprintf (outfile, "\t%6zd %6.2f %-36s in %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name, um->parent->name);
911 fprintf (outfile, "\t%6zd %6.2f %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name);
915 sym_percent (uintptr_t sample_hits)
920 pc = sample_hits*100.0/num_stat_samples;
928 int count = 0, msize = 0;
929 int unmanaged_hits = 0;
930 int unresolved_hits = 0;
931 MethodDesc** cachedm = NULL;
932 int ucount = 0, usize = 0;
933 UnmanagedSymbol** cachedus = NULL;
934 if (!num_stat_samples)
936 qsort (usymbols, usymbols_num, sizeof (UnmanagedSymbol*), compare_usymbol_addr);
937 for (i = 0; i < num_stat_samples; ++i) {
938 MethodDesc *m = lookup_method_by_ip (stat_samples [i]);
940 if (!m->sample_hits) {
941 if (count == msize) {
945 cachedm = realloc (cachedm, sizeof (void*) * msize);
947 cachedm [count++] = m;
951 UnmanagedSymbol *usym = lookup_unmanaged_symbol (stat_samples [i]);
954 //printf ("unmanaged hit at %p\n", (void*)stat_samples [i]);
955 usym = lookup_unmanaged_binary (stat_samples [i]);
958 if (!usym->sample_hits) {
959 if (ucount == usize) {
963 cachedus = realloc (cachedus, sizeof (void*) * usize);
965 cachedus [ucount++] = usym;
972 qsort (cachedm, count, sizeof (MethodDesc*), compare_method_samples);
973 qsort (cachedus, ucount, sizeof (UnmanagedSymbol*), compare_usymbol_samples);
974 set_usym_parent (cachedus, ucount);
975 fprintf (outfile, "\nStatistical samples summary\n");
976 fprintf (outfile, "\tSample type: %s\n", sample_type_name (stat_sample_desc [0]));
977 fprintf (outfile, "\tUnmanaged hits: %6d (%4.1f%%)\n", unmanaged_hits, (100.0*unmanaged_hits)/num_stat_samples);
978 fprintf (outfile, "\tManaged hits: %6d (%4.1f%%)\n", num_stat_samples - unmanaged_hits, (100.0*(num_stat_samples-unmanaged_hits))/num_stat_samples);
979 fprintf (outfile, "\tUnresolved hits: %6d (%4.1f%%)\n", unresolved_hits, (100.0*unresolved_hits)/num_stat_samples);
980 fprintf (outfile, "\t%6s %6s %s\n", "Hits", "%", "Method name");
983 while (i < count || u < ucount) {
985 MethodDesc *m = cachedm [i];
987 UnmanagedSymbol *um = cachedus [u];
988 if (um->sample_hits > m->sample_hits) {
989 if (!sym_percent (um->sample_hits))
996 if (!sym_percent (m->sample_hits))
998 fprintf (outfile, "\t%6d %6.2f %s\n", m->sample_hits, m->sample_hits*100.0/num_stat_samples, m->name);
1003 UnmanagedSymbol *um = cachedus [u];
1004 if (!sym_percent (um->sample_hits))
1013 typedef struct _HeapClassDesc HeapClassDesc;
1015 HeapClassDesc *klass;
1019 struct _HeapClassDesc {
1023 HeapClassRevRef *rev_hash;
1026 uintptr_t pinned_references;
1027 uintptr_t root_references;
1031 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
1034 uintptr_t start_pos;
1035 start_pos = (hklass->klass->klass >> 2) % size;
1036 assert (start_pos < size);
1039 if (rev_hash [i].klass == hklass) {
1040 rev_hash [i].count += value;
1042 } else if (!rev_hash [i].klass) {
1043 rev_hash [i].klass = hklass;
1044 rev_hash [i].count += value;
1046 for (i = 0; i < size; ++i)
1047 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
1049 assert (start_pos == 1);
1055 } while (i != start_pos);
1056 /* should not happen */
1057 printf ("failed revref store\n");
1062 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
1065 if (to->rev_count * 2 >= to->rev_hash_size) {
1067 uintptr_t old_size = to->rev_hash_size;
1068 to->rev_hash_size *= 2;
1069 if (to->rev_hash_size == 0)
1070 to->rev_hash_size = 4;
1071 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
1072 for (i = 0; i < old_size; ++i) {
1073 if (to->rev_hash [i].klass)
1074 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
1077 free (to->rev_hash);
1080 to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
1085 HeapClassDesc *hklass;
1090 typedef struct _HeapShot HeapShot;
1096 HeapClassDesc **class_hash;
1097 HeapClassDesc **sorted;
1098 HeapObjectDesc **objects_hash;
1099 uintptr_t objects_count;
1100 uintptr_t objects_hash_size;
1101 uintptr_t num_roots;
1103 uintptr_t *roots_extra;
1107 static HeapShot *heap_shots = NULL;
1108 static int num_heap_shots = 0;
1111 new_heap_shot (uint64_t timestamp)
1113 HeapShot *hs = calloc (sizeof (HeapShot), 1);
1115 hs->class_hash = calloc (sizeof (void*), hs->hash_size);
1116 hs->timestamp = timestamp;
1118 hs->next = heap_shots;
1123 static HeapClassDesc*
1124 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
1127 unsigned int start_pos;
1128 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
1131 HeapClassDesc* cd = hs->class_hash [i];
1134 if (cd->klass == klass)
1137 if (++i == hs->hash_size)
1139 } while (i != start_pos);
1144 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
1147 uintptr_t start_pos;
1148 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
1151 if (hash [i] && hash [i]->klass == klass) {
1152 hash [i]->total_size += size;
1153 hash [i]->count += count;
1156 } else if (!hash [i]) {
1161 hash [i] = calloc (sizeof (HeapClassDesc), 1);
1162 hash [i]->klass = klass;
1163 hash [i]->total_size += size;
1164 hash [i]->count += count;
1171 } while (i != start_pos);
1172 /* should not happen */
1173 printf ("failed heap class store\n");
1177 static HeapClassDesc*
1178 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
1182 if (hs->class_count * 2 >= hs->hash_size) {
1184 int old_size = hs->hash_size;
1186 if (hs->hash_size == 0)
1188 n = calloc (sizeof (void*) * hs->hash_size, 1);
1189 for (i = 0; i < old_size; ++i) {
1190 res = hs->class_hash [i];
1191 if (hs->class_hash [i])
1192 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
1195 free (hs->class_hash);
1199 hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
1200 //if (res->count == 1)
1201 // printf ("added heap class: %s\n", res->klass->name);
1205 static HeapObjectDesc*
1206 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
1208 HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
1209 ho->objaddr = objaddr;
1210 ho->hklass = hklass;
1211 ho->num_refs = num_refs;
1216 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
1219 uintptr_t start_pos;
1220 HeapObjectDesc **hash = hs->objects_hash;
1221 start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
1224 if (hash [i] && hash [i]->objaddr == objaddr) {
1226 } else if (!hash [i]) {
1230 if (++i == hs->objects_hash_size)
1232 } while (i != start_pos);
1233 /* should not happen */
1234 //printf ("failed heap obj slot\n");
1238 static HeapObjectDesc*
1239 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
1241 HeapObjectDesc **hash = hs->objects_hash;
1242 uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
1244 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
1245 *ref_offset = hash [i]->num_refs;
1246 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
1251 /* should not happen */
1252 printf ("failed heap obj update\n");
1258 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
1261 uintptr_t start_pos;
1262 start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
1265 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
1266 printf ("duplicate object!\n");
1268 } else if (!hash [i]) {
1275 } while (i != start_pos);
1276 /* should not happen */
1277 printf ("failed heap obj store\n");
1282 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
1285 if (hs->objects_count * 2 >= hs->objects_hash_size) {
1287 uintptr_t old_size = hs->objects_hash_size;
1288 hs->objects_hash_size *= 2;
1289 if (hs->objects_hash_size == 0)
1290 hs->objects_hash_size = 4;
1291 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
1292 for (i = 0; i < old_size; ++i) {
1293 if (hs->objects_hash [i])
1294 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
1296 if (hs->objects_hash)
1297 free (hs->objects_hash);
1298 hs->objects_hash = n;
1300 hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
1304 heap_shot_resolve_reverse_refs (HeapShot *hs)
1307 for (i = 0; i < hs->objects_hash_size; ++i) {
1309 HeapObjectDesc *ho = hs->objects_hash [i];
1312 for (r = 0; r < ho->num_refs; ++r) {
1313 uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
1314 add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
1320 #define MARK_BLACK 2
1323 heap_shot_mark_objects (HeapShot *hs)
1326 unsigned char *marks;
1327 HeapObjectDesc *obj, *ref;
1329 uintptr_t num_marked = 0, num_unmarked;
1330 for (i = 0; i < hs->num_roots; ++i) {
1332 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
1336 obj = hs->objects_hash [oi];
1338 if (hs->roots_types [i] & MONO_PROFILE_GC_ROOT_PINNING)
1339 cd->pinned_references++;
1340 cd->root_references++;
1344 /* consistency checks: it seems not all the objects are walked in the heap in some cases */
1345 marks = calloc (hs->objects_hash_size, 1);
1348 for (i = 0; i < hs->num_roots; ++i) {
1349 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
1351 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);
1354 obj = hs->objects_hash [oi];
1356 marks [oi] = obj->num_refs? MARK_GRAY: MARK_BLACK;
1361 while (marked_some) {
1363 for (i = 0; i < hs->objects_hash_size; ++i) {
1364 if (marks [i] != MARK_GRAY)
1366 marks [i] = MARK_BLACK;
1367 obj = hs->objects_hash [i];
1368 for (r = 0; r < obj->num_refs; ++r) {
1369 oi = heap_shot_find_obj_slot (hs, obj->refs [r]);
1371 fprintf (outfile, "referenced obj %p not found in heap\n", (void*)obj->refs [r]);
1374 ref = hs->objects_hash [oi];
1376 marks [oi] = ref->num_refs? MARK_GRAY: MARK_BLACK;
1384 for (i = 0; i < hs->objects_hash_size; ++i) {
1385 if (hs->objects_hash [i] && !marks [i]) {
1387 fprintf (outfile, "object %p (%s) unmarked\n", (void*)hs->objects_hash [i], hs->objects_hash [i]->hklass->klass->name);
1390 fprintf (outfile, "Total unmarked: %zd/%zd\n", num_unmarked, hs->objects_count);
1395 heap_shot_free_objects (HeapShot *hs)
1398 for (i = 0; i < hs->objects_hash_size; ++i) {
1399 HeapObjectDesc *ho = hs->objects_hash [i];
1403 if (hs->objects_hash)
1404 free (hs->objects_hash);
1405 hs->objects_hash = NULL;
1406 hs->objects_hash_size = 0;
1407 hs->objects_count = 0;
1416 MethodDesc *methods [1];
1419 static BackTrace *backtrace_hash [HASH_SIZE];
1420 static BackTrace **backtraces = NULL;
1421 static int num_backtraces = 0;
1422 static int next_backtrace = 0;
1425 hash_backtrace (int count, MethodDesc **methods)
1429 for (i = 0; i < count; ++i) {
1430 hash = (hash << 5) - hash + methods [i]->method;
1436 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
1439 if (bt->count != count)
1441 for (i = 0; i < count; ++i)
1442 if (methods [i] != bt->methods [i])
1448 add_backtrace (int count, MethodDesc **methods)
1450 int hash = hash_backtrace (count, methods);
1451 int slot = (hash & 0xffff) % HASH_SIZE;
1452 BackTrace *bt = backtrace_hash [slot];
1454 if (bt->hash == hash && compare_backtrace (bt, count, methods))
1458 bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
1459 bt->next = backtrace_hash [slot];
1460 backtrace_hash [slot] = bt;
1461 if (next_backtrace == num_backtraces) {
1462 num_backtraces *= 2;
1463 if (!num_backtraces)
1464 num_backtraces = 16;
1465 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
1467 bt->id = next_backtrace++;
1468 backtraces [bt->id] = bt;
1471 for (slot = 0; slot < count; ++slot)
1472 bt->methods [slot] = methods [slot];
1477 typedef struct _MonitorDesc MonitorDesc;
1478 typedef struct _ThreadContext ThreadContext;
1482 #if defined (HAVE_SYS_ZLIB)
1493 uint64_t startup_time;
1494 ThreadContext *threads;
1495 ThreadContext *current;
1498 struct _ThreadContext {
1499 ThreadContext *next;
1502 /* emulated stack */
1504 uint64_t *time_stack;
1505 uint64_t *callee_time_stack;
1507 uint64_t contention_start;
1508 MonitorDesc *monitor;
1511 HeapShot *current_heap_shot;
1512 uintptr_t num_roots;
1513 uintptr_t size_roots;
1515 uintptr_t *roots_extra;
1517 uint64_t gc_start_times [3];
1521 ensure_buffer (ProfContext *ctx, int size)
1523 if (ctx->size < size) {
1524 ctx->buf = realloc (ctx->buf, size);
1530 load_data (ProfContext *ctx, int size)
1532 ensure_buffer (ctx, size);
1533 #if defined (HAVE_SYS_ZLIB)
1535 int r = gzread (ctx->gzfile, ctx->buf, size);
1537 return size == 0? 1: 0;
1542 int r = fread (ctx->buf, size, 1, ctx->file);
1544 return size == 0? 1: 0;
1549 static ThreadContext*
1550 get_thread (ProfContext *ctx, intptr_t thread_id)
1552 ThreadContext *thread;
1553 if (ctx->current && ctx->current->thread_id == thread_id)
1554 return ctx->current;
1555 thread = ctx->threads;
1557 if (thread->thread_id == thread_id) {
1560 thread = thread->next;
1562 thread = calloc (sizeof (ThreadContext), 1);
1563 thread->next = ctx->threads;
1564 ctx->threads = thread;
1565 thread->thread_id = thread_id;
1566 thread->last_time = 0;
1567 thread->stack_id = 0;
1568 thread->stack_size = 32;
1569 thread->stack = malloc (thread->stack_size * sizeof (void*));
1570 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1571 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
1575 static ThreadContext*
1576 load_thread (ProfContext *ctx, intptr_t thread_id)
1578 ThreadContext *thread = get_thread (ctx, thread_id);
1579 ctx->current = thread;
1584 ensure_thread_stack (ThreadContext *thread)
1586 if (thread->stack_id == thread->stack_size) {
1587 thread->stack_size *= 2;
1588 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
1589 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
1590 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
1595 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
1598 unsigned int start_pos;
1599 start_pos = bt->hash % size;
1602 if (traces [i].bt == bt) {
1603 traces [i].count += value;
1605 } else if (!traces [i].bt) {
1607 traces [i].count += value;
1613 } while (i != start_pos);
1614 /* should not happen */
1615 printf ("failed trace store\n");
1620 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
1623 if (!collect_traces)
1625 if (trace->count * 2 >= trace->size) {
1627 int old_size = trace->size;
1629 if (trace->size == 0)
1631 n = calloc (sizeof (CallContext) * trace->size, 1);
1632 for (i = 0; i < old_size; ++i) {
1633 if (trace->traces [i].bt)
1634 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
1637 free (trace->traces);
1640 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
1644 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
1647 int count = thread->stack_id;
1648 if (!collect_traces)
1650 if (count > trace_max)
1652 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
1653 add_trace_bt (bt, trace, value);
1658 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
1661 if (!collect_traces)
1663 if (count > trace_max)
1665 bt = add_backtrace (count, methods);
1666 add_trace_bt (bt, trace, value);
1671 thread_add_root (ThreadContext *ctx, uintptr_t obj, int root_type, uintptr_t extra_info)
1673 if (ctx->num_roots == ctx->size_roots) {
1674 int new_size = ctx->size_roots * 2;
1677 ctx->roots = realloc (ctx->roots, new_size * sizeof (uintptr_t));
1678 ctx->roots_extra = realloc (ctx->roots_extra, new_size * sizeof (uintptr_t));
1679 ctx->roots_types = realloc (ctx->roots_types, new_size * sizeof (int));
1680 ctx->size_roots = new_size;
1682 ctx->roots_types [ctx->num_roots] = root_type;
1683 ctx->roots_extra [ctx->num_roots] = extra_info;
1684 ctx->roots [ctx->num_roots++] = obj;
1688 compare_callc (const void *a, const void *b)
1690 const CallContext *A = a;
1691 const CallContext *B = b;
1692 if (B->count == A->count)
1694 if (B->count < A->count)
1700 sort_context_array (TraceDesc* traces)
1703 for (i = 0, j = 0; i < traces->size; ++i) {
1704 if (traces->traces [i].bt) {
1705 traces->traces [j].bt = traces->traces [i].bt;
1706 traces->traces [j].count = traces->traces [i].count;
1710 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
1714 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1716 ensure_thread_stack (thread);
1717 thread->time_stack [thread->stack_id] = timestamp;
1718 thread->callee_time_stack [thread->stack_id] = 0;
1719 thread->stack [thread->stack_id++] = method;
1720 method->recurse_count++;
1724 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1726 method->recurse_count--;
1727 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
1731 if (timestamp < thread->time_stack [thread->stack_id])
1732 fprintf (outfile, "time went backwards for %s\n", method->name);
1733 tdiff = timestamp - thread->time_stack [thread->stack_id];
1734 if (thread->callee_time_stack [thread->stack_id] > tdiff)
1735 fprintf (outfile, "callee time bigger for %s\n", method->name);
1736 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
1737 method->callee_time += thread->callee_time_stack [thread->stack_id];
1738 if (thread->stack_id)
1739 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
1740 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
1742 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
1747 uint64_t total_time;
1751 static GCDesc gc_info [3];
1752 static uint64_t max_heap_size;
1753 static uint64_t gc_object_moves;
1754 static int gc_resizes;
1762 static HandleInfo handle_info [4];
1765 gc_event_name (int ev)
1768 case MONO_GC_EVENT_START: return "start";
1769 case MONO_GC_EVENT_MARK_START: return "mark start";
1770 case MONO_GC_EVENT_MARK_END: return "mark end";
1771 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
1772 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
1773 case MONO_GC_EVENT_END: return "end";
1774 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
1775 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
1776 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
1777 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
1783 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
1784 static uint64_t throw_count = 0;
1785 static TraceDesc exc_traces;
1788 clause_name (int type)
1791 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
1792 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
1793 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
1794 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
1795 default: return "invalid";
1799 static uint64_t monitor_contention;
1800 static uint64_t monitor_failed;
1801 static uint64_t monitor_acquired;
1803 struct _MonitorDesc {
1806 uintptr_t contentions;
1808 uint64_t max_wait_time;
1812 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
1813 static int num_monitors = 0;
1816 lookup_monitor (uintptr_t objid)
1818 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
1819 MonitorDesc *cd = monitor_hash [slot];
1820 while (cd && cd->objid != objid)
1823 cd = calloc (sizeof (MonitorDesc), 1);
1825 cd->next = monitor_hash [slot];
1826 monitor_hash [slot] = cd;
1833 monitor_ev_name (int ev)
1836 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
1837 case MONO_PROFILER_MONITOR_DONE: return "acquired";
1838 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
1839 default: return "invalid";
1844 get_handle_name (int htype)
1847 case 0: return "weak";
1848 case 1: return "weaktrack";
1849 case 2: return "normal";
1850 case 3: return "pinned";
1851 default: return "unknown";
1856 get_root_name (int rtype)
1858 switch (rtype & MONO_PROFILE_GC_ROOT_TYPEMASK) {
1859 case MONO_PROFILE_GC_ROOT_STACK: return "stack";
1860 case MONO_PROFILE_GC_ROOT_FINALIZER: return "finalizer";
1861 case MONO_PROFILE_GC_ROOT_HANDLE: return "handle";
1862 case MONO_PROFILE_GC_ROOT_OTHER: return "other";
1863 case MONO_PROFILE_GC_ROOT_MISC: return "misc";
1864 default: return "unknown";
1869 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
1871 MethodDesc **frames;
1873 int flags = decode_uleb128 (p, &p);
1874 int count = decode_uleb128 (p, &p);
1878 frames = malloc (count * sizeof (void*));
1881 for (i = 0; i < count; ++i) {
1882 intptr_t ptrdiff = decode_sleb128 (p, &p);
1883 frames [i] = lookup_method (ptr_base + ptrdiff);
1891 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
1894 for (i = 0; i < num_tracked_objects; ++i) {
1895 if (tracked_objects [i] != obj)
1897 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, (unsigned long long) size, (timestamp - startup_time)/1000000000.0);
1898 if (bt && bt->count) {
1900 for (k = 0; k < bt->count; ++k)
1901 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
1907 track_handle (uintptr_t obj, int htype, uint32_t handle)
1910 for (i = 0; i < num_tracked_objects; ++i) {
1911 if (tracked_objects [i] == obj)
1912 fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1917 track_move (uintptr_t src, uintptr_t dst)
1920 for (i = 0; i < num_tracked_objects; ++i) {
1921 if (tracked_objects [i] == src)
1922 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1923 else if (tracked_objects [i] == dst)
1924 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1929 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1932 for (i = 0; i < num_tracked_objects; ++i) {
1933 if (tracked_objects [i] == obj)
1934 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1939 found_object (uintptr_t obj)
1941 num_tracked_objects ++;
1942 tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1943 tracked_objects [num_tracked_objects - 1] = obj;
1946 static int num_jit_helpers = 0;
1947 static int jit_helpers_code_size = 0;
1950 code_buffer_desc (int type)
1953 case MONO_PROFILER_CODE_BUFFER_METHOD:
1955 case MONO_PROFILER_CODE_BUFFER_METHOD_TRAMPOLINE:
1956 return "method trampoline";
1957 case MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE:
1958 return "unbox trampoline";
1959 case MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE:
1960 return "imt trampoline";
1961 case MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE:
1962 return "generics trampoline";
1963 case MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE:
1964 return "specific trampoline";
1965 case MONO_PROFILER_CODE_BUFFER_HELPER:
1966 return "misc helper";
1967 case MONO_PROFILER_CODE_BUFFER_MONITOR:
1968 return "monitor/lock";
1969 case MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE:
1970 return "delegate invoke";
1971 case MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING:
1972 return "exception handling";
1974 return "unspecified";
1978 typedef struct _CoverageAssembly CoverageAssembly;
1979 struct _CoverageAssembly {
1983 int number_of_methods;
1985 int partially_covered;
1988 typedef struct _CoverageClass CoverageClass;
1989 struct _CoverageClass {
1990 char *assembly_name;
1992 int number_of_methods;
1994 int partially_covered;
1997 typedef struct _CoverageCoverage CoverageCoverage;
1998 struct _CoverageCoverage {
2006 typedef struct _CoverageMethod CoverageMethod;
2007 struct _CoverageMethod {
2008 char *assembly_name;
2011 char *method_signature;
2016 GPtrArray *coverage;
2018 static GPtrArray *coverage_assemblies = NULL;
2019 static GPtrArray *coverage_methods = NULL;
2020 static GPtrArray *coverage_statements = NULL;
2021 static GHashTable *coverage_methods_hash = NULL;
2022 static GPtrArray *coverage_classes = NULL;
2023 static GHashTable *coverage_assembly_classes = NULL;
2026 gather_coverage_statements (void)
2028 for (guint i = 0; i < coverage_statements->len; i++) {
2029 CoverageCoverage *coverage = coverage_statements->pdata[i];
2030 CoverageMethod *method = g_hash_table_lookup (coverage_methods_hash, GINT_TO_POINTER (coverage->method_id));
2031 if (method == NULL) {
2032 fprintf (outfile, "Cannot find method with ID: %d\n", coverage->method_id);
2036 g_ptr_array_add (method->coverage, coverage);
2041 coverage_add_assembly (CoverageAssembly *assembly)
2043 if (coverage_assemblies == NULL)
2044 coverage_assemblies = g_ptr_array_new ();
2046 g_ptr_array_add (coverage_assemblies, assembly);
2050 coverage_add_method (CoverageMethod *method)
2052 if (coverage_methods == NULL) {
2053 coverage_methods = g_ptr_array_new ();
2054 coverage_methods_hash = g_hash_table_new (NULL, NULL);
2057 g_ptr_array_add (coverage_methods, method);
2058 g_hash_table_insert (coverage_methods_hash, GINT_TO_POINTER (method->method_id), method);
2062 coverage_add_class (CoverageClass *klass)
2064 GPtrArray *classes = NULL;
2066 if (coverage_classes == NULL) {
2067 coverage_classes = g_ptr_array_new ();
2068 coverage_assembly_classes = g_hash_table_new (g_str_hash, g_str_equal);
2071 g_ptr_array_add (coverage_classes, klass);
2072 classes = g_hash_table_lookup (coverage_assembly_classes, klass->assembly_name);
2073 if (classes == NULL) {
2074 classes = g_ptr_array_new ();
2075 g_hash_table_insert (coverage_assembly_classes, klass->assembly_name, classes);
2077 g_ptr_array_add (classes, klass);
2081 coverage_add_coverage (CoverageCoverage *coverage)
2083 if (coverage_statements == NULL)
2084 coverage_statements = g_ptr_array_new ();
2086 g_ptr_array_add (coverage_statements, coverage);
2089 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
2090 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
2093 decode_buffer (ProfContext *ctx)
2100 intptr_t method_base;
2102 uint64_t file_offset;
2104 ThreadContext *thread;
2106 #ifdef HAVE_SYS_ZLIB
2108 file_offset = gztell (ctx->gzfile);
2111 file_offset = ftell (ctx->file);
2112 if (!load_data (ctx, 48))
2115 if (read_int32 (p) != BUF_ID) {
2116 fprintf (outfile, "Incorrect buffer id: 0x%x\n", read_int32 (p));
2117 for (i = 0; i < 48; ++i) {
2118 fprintf (outfile, "0x%x%s", p [i], i % 8?" ":"\n");
2122 len = read_int32 (p + 4);
2123 time_base = read_int64 (p + 8);
2124 ptr_base = read_int64 (p + 16);
2125 obj_base = read_int64 (p + 24);
2126 thread_id = read_int64 (p + 32);
2127 method_base = read_int64 (p + 40);
2129 fprintf (outfile, "buf: thread:%zx, len: %d, time: %llu, file offset: %llu\n", thread_id, len, (unsigned long long) time_base, (unsigned long long) file_offset);
2130 thread = load_thread (ctx, thread_id);
2131 if (!load_data (ctx, len))
2133 if (!startup_time) {
2134 startup_time = time_base;
2135 if (use_time_filter) {
2136 time_from += startup_time;
2137 time_to += startup_time;
2140 thread->name = pstrdup ("Main");
2142 for (i = 0; i < thread->stack_id; ++i)
2143 thread->stack [i]->recurse_count++;
2149 int subtype = *p & 0xf0;
2150 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2151 LOG_TIME (time_base, tdiff);
2153 if (subtype == TYPE_GC_RESIZE) {
2154 uint64_t new_size = decode_uleb128 (p, &p);
2156 fprintf (outfile, "gc heap resized to %llu\n", (unsigned long long) new_size);
2158 if (new_size > max_heap_size)
2159 max_heap_size = new_size;
2160 } else if (subtype == TYPE_GC_EVENT) {
2161 uint64_t ev = decode_uleb128 (p, &p);
2162 int gen = decode_uleb128 (p, &p);
2164 fprintf (outfile, "gc event for gen%d: %s at %llu (thread: 0x%zx)\n", gen, gc_event_name (ev), (unsigned long long) time_base, thread->thread_id);
2166 fprintf (outfile, "incorrect gc gen: %d\n", gen);
2169 if (ev == MONO_GC_EVENT_START) {
2170 thread->gc_start_times [gen] = time_base;
2171 gc_info [gen].count++;
2172 } else if (ev == MONO_GC_EVENT_END) {
2173 tdiff = time_base - thread->gc_start_times [gen];
2174 gc_info [gen].total_time += tdiff;
2175 if (tdiff > gc_info [gen].max_time)
2176 gc_info [gen].max_time = tdiff;
2178 } else if (subtype == TYPE_GC_MOVE) {
2179 int j, num = decode_uleb128 (p, &p);
2180 gc_object_moves += num / 2;
2181 for (j = 0; j < num; j += 2) {
2182 intptr_t obj1diff = decode_sleb128 (p, &p);
2183 intptr_t obj2diff = decode_sleb128 (p, &p);
2184 if (num_tracked_objects)
2185 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
2187 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
2190 } else if (subtype == TYPE_GC_HANDLE_CREATED) {
2191 int htype = decode_uleb128 (p, &p);
2192 uint32_t handle = decode_uleb128 (p, &p);
2193 intptr_t objdiff = decode_sleb128 (p, &p);
2196 handle_info [htype].created++;
2197 handle_info [htype].live++;
2198 add_trace_thread (thread, &handle_info [htype].traces, 1);
2199 /* FIXME: we don't take into account timing here */
2200 if (handle_info [htype].live > handle_info [htype].max_live)
2201 handle_info [htype].max_live = handle_info [htype].live;
2202 if (num_tracked_objects)
2203 track_handle (OBJ_ADDR (objdiff), htype, handle);
2205 fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
2206 } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
2207 int htype = decode_uleb128 (p, &p);
2208 uint32_t handle = decode_uleb128 (p, &p);
2211 handle_info [htype].destroyed ++;
2212 handle_info [htype].live--;
2214 fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
2218 case TYPE_METADATA: {
2219 int error = *p & TYPE_LOAD_ERR;
2220 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2222 intptr_t ptrdiff = decode_sleb128 (p, &p);
2223 LOG_TIME (time_base, tdiff);
2225 if (mtype == TYPE_CLASS) {
2226 intptr_t imptrdiff = decode_sleb128 (p, &p);
2227 uint64_t flags = decode_uleb128 (p, &p);
2229 fprintf (outfile, "non-zero flags in class\n");
2233 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), (unsigned long long) time_base);
2235 add_class (ptr_base + ptrdiff, (char*)p);
2238 } else if (mtype == TYPE_IMAGE) {
2239 uint64_t flags = decode_uleb128 (p, &p);
2241 fprintf (outfile, "non-zero flags in image\n");
2245 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, (unsigned long long) time_base);
2247 add_image (ptr_base + ptrdiff, (char*)p);
2250 } else if (mtype == TYPE_THREAD) {
2252 uint64_t flags = decode_uleb128 (p, &p);
2254 fprintf (outfile, "non-zero flags in thread\n");
2257 nt = get_thread (ctx, ptr_base + ptrdiff);
2258 nt->name = pstrdup ((char*)p);
2260 fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
2267 int has_bt = *p & TYPE_ALLOC_BT;
2268 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2269 intptr_t ptrdiff = decode_sleb128 (p, &p);
2270 intptr_t objdiff = decode_sleb128 (p, &p);
2273 MethodDesc* sframes [8];
2274 MethodDesc** frames = sframes;
2275 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
2276 len = decode_uleb128 (p, &p);
2277 LOG_TIME (time_base, tdiff);
2280 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), (unsigned long long) len, lookup_class (ptr_base + ptrdiff)->name, (unsigned long long) time_base);
2283 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2285 fprintf (outfile, "Cannot load backtrace\n");
2289 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
2292 cd->alloc_size += len;
2294 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
2296 bt = add_trace_thread (thread, &cd->traces, len);
2297 if (find_size && len >= find_size) {
2298 if (!find_name || strstr (cd->name, find_name))
2299 found_object (OBJ_ADDR (objdiff));
2300 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
2301 found_object (OBJ_ADDR (objdiff));
2303 if (num_tracked_objects)
2304 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
2306 if (frames != sframes)
2311 int subtype = *p & 0xf0;
2312 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2313 int64_t ptrdiff = decode_sleb128 (p, &p);
2314 LOG_TIME (time_base, tdiff);
2316 method_base += ptrdiff;
2317 if (subtype == TYPE_JIT) {
2318 intptr_t codediff = decode_sleb128 (p, &p);
2319 int codelen = decode_uleb128 (p, &p);
2320 MethodDesc *jitted_method;
2322 fprintf (outfile, "jitted method %p (%s), size: %d, code: %p\n", (void*)(method_base), p, codelen, (void*)(ptr_base + codediff));
2323 jitted_method = add_method (method_base, (char*)p, ptr_base + codediff, codelen);
2324 if (!(time_base >= time_from && time_base < time_to))
2325 jitted_method->ignore_jit = 1;
2330 if ((thread_filter && thread_filter != thread->thread_id))
2332 if (!(time_base >= time_from && time_base < time_to))
2334 method = lookup_method (method_base);
2335 if (subtype == TYPE_ENTER) {
2336 add_trace_thread (thread, &method->traces, 1);
2337 push_method (thread, method, time_base);
2339 pop_method (thread, method, time_base);
2342 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
2347 int subtype = *p & 0xf0;
2348 if (subtype == TYPE_HEAP_OBJECT) {
2349 HeapObjectDesc *ho = NULL;
2351 intptr_t objdiff = decode_sleb128 (p + 1, &p);
2352 intptr_t ptrdiff = decode_sleb128 (p, &p);
2353 uint64_t size = decode_uleb128 (p, &p);
2354 uintptr_t num = decode_uleb128 (p, &p);
2355 uintptr_t ref_offset = 0;
2356 uintptr_t last_obj_offset = 0;
2357 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
2359 HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
2360 if (collect_traces) {
2361 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
2362 add_heap_shot_obj (thread->current_heap_shot, ho);
2367 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
2369 for (i = 0; i < num; ++i) {
2370 /* FIXME: use object distance to measure how good
2371 * the GC is at keeping related objects close
2373 uintptr_t offset = ctx->data_version > 1? last_obj_offset + decode_uleb128 (p, &p): -1;
2374 intptr_t obj1diff = decode_sleb128 (p, &p);
2375 last_obj_offset = offset;
2377 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
2378 if (num_tracked_objects)
2379 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
2382 fprintf (outfile, "traced object %p, size %llu (%s), refs: %zd\n", (void*)OBJ_ADDR (objdiff), (unsigned long long) size, cd->name, num);
2383 } else if (subtype == TYPE_HEAP_ROOT) {
2384 uintptr_t num = decode_uleb128 (p + 1, &p);
2385 uintptr_t gc_num G_GNUC_UNUSED = decode_uleb128 (p, &p);
2387 for (i = 0; i < num; ++i) {
2388 intptr_t objdiff = decode_sleb128 (p, &p);
2389 int root_type = decode_uleb128 (p, &p);
2390 /* we just discard the extra info for now */
2391 uintptr_t extra_info = decode_uleb128 (p, &p);
2393 fprintf (outfile, "object %p is a %s root\n", (void*)OBJ_ADDR (objdiff), get_root_name (root_type));
2395 thread_add_root (thread, OBJ_ADDR (objdiff), root_type, extra_info);
2397 } else if (subtype == TYPE_HEAP_END) {
2398 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2399 LOG_TIME (time_base, tdiff);
2402 fprintf (outfile, "heap shot end\n");
2403 if (collect_traces) {
2404 HeapShot *hs = thread->current_heap_shot;
2405 if (hs && thread->num_roots) {
2406 /* transfer the root ownershipt to the heapshot */
2407 hs->num_roots = thread->num_roots;
2408 hs->roots = thread->roots;
2409 hs->roots_extra = thread->roots_extra;
2410 hs->roots_types = thread->roots_types;
2412 free (thread->roots);
2413 free (thread->roots_extra);
2414 free (thread->roots_types);
2416 thread->num_roots = 0;
2417 thread->size_roots = 0;
2418 thread->roots = NULL;
2419 thread->roots_extra = NULL;
2420 thread->roots_types = NULL;
2421 heap_shot_resolve_reverse_refs (hs);
2422 heap_shot_mark_objects (hs);
2423 heap_shot_free_objects (hs);
2425 thread->current_heap_shot = NULL;
2426 } else if (subtype == TYPE_HEAP_START) {
2427 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2428 LOG_TIME (time_base, tdiff);
2431 fprintf (outfile, "heap shot start\n");
2432 thread->current_heap_shot = new_heap_shot (time_base);
2436 case TYPE_MONITOR: {
2437 int event = (*p >> 4) & 0x3;
2438 int has_bt = *p & TYPE_MONITOR_BT;
2439 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2440 intptr_t objdiff = decode_sleb128 (p, &p);
2441 MethodDesc* sframes [8];
2442 MethodDesc** frames = sframes;
2445 LOG_TIME (time_base, tdiff);
2447 record = (!thread_filter || thread_filter == thread->thread_id);
2448 if (!(time_base >= time_from && time_base < time_to))
2450 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
2451 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
2453 monitor_contention++;
2454 mdesc->contentions++;
2455 thread->monitor = mdesc;
2456 thread->contention_start = time_base;
2460 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2462 fprintf (outfile, "Cannot load backtrace\n");
2466 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
2469 add_trace_thread (thread, &mdesc->traces, 1);
2471 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
2474 if (thread->monitor && thread->contention_start) {
2475 uint64_t wait_time = time_base - thread->contention_start;
2476 if (wait_time > thread->monitor->max_wait_time)
2477 thread->monitor->max_wait_time = wait_time;
2478 thread->monitor->wait_time += wait_time;
2479 thread->monitor = NULL;
2480 thread->contention_start = 0;
2483 } else if (event == MONO_PROFILER_MONITOR_DONE) {
2486 if (thread->monitor && thread->contention_start) {
2487 uint64_t wait_time = time_base - thread->contention_start;
2488 if (wait_time > thread->monitor->max_wait_time)
2489 thread->monitor->max_wait_time = wait_time;
2490 thread->monitor->wait_time += wait_time;
2491 thread->monitor = NULL;
2492 thread->contention_start = 0;
2497 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
2498 if (frames != sframes)
2502 case TYPE_EXCEPTION: {
2503 int subtype = *p & 0x70;
2504 int has_bt = *p & TYPE_EXCEPTION_BT;
2505 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2506 MethodDesc* sframes [8];
2507 MethodDesc** frames = sframes;
2509 LOG_TIME (time_base, tdiff);
2511 record = (!thread_filter || thread_filter == thread->thread_id);
2512 if (!(time_base >= time_from && time_base < time_to))
2514 if (subtype == TYPE_CLAUSE) {
2515 int clause_type = decode_uleb128 (p, &p);
2516 int clause_num = decode_uleb128 (p, &p);
2517 int64_t ptrdiff = decode_sleb128 (p, &p);
2518 method_base += ptrdiff;
2520 clause_summary [clause_type]++;
2522 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
2524 intptr_t objdiff = decode_sleb128 (p, &p);
2529 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
2531 fprintf (outfile, "Cannot load backtrace\n");
2535 add_trace_methods (frames, has_bt, &exc_traces, 1);
2538 add_trace_thread (thread, &exc_traces, 1);
2540 if (frames != sframes)
2543 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
2547 case TYPE_RUNTIME: {
2548 int subtype = *p & 0xf0;
2549 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2550 LOG_TIME (time_base, tdiff);
2552 if (subtype == TYPE_JITHELPER) {
2553 int type = decode_uleb128 (p, &p);
2554 intptr_t codediff = decode_sleb128 (p, &p);
2555 int codelen = decode_uleb128 (p, &p);
2557 if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
2562 name = code_buffer_desc (type);
2565 jit_helpers_code_size += codelen;
2567 fprintf (outfile, "jit helper %s, size: %d, code: %p\n", name, codelen, (void*)(ptr_base + codediff));
2572 int subtype = *p & 0xf0;
2573 if (subtype == TYPE_SAMPLE_HIT) {
2575 int sample_type = decode_uleb128 (p + 1, &p);
2576 uint64_t tstamp = decode_uleb128 (p, &p);
2577 int count = decode_uleb128 (p, &p);
2578 for (i = 0; i < count; ++i) {
2579 uintptr_t ip = ptr_base + decode_sleb128 (p, &p);
2580 if ((tstamp >= time_from && tstamp < time_to))
2581 add_stat_sample (sample_type, ip);
2583 fprintf (outfile, "sample hit, type: %d at %p\n", sample_type, (void*)ip);
2585 if (ctx->data_version > 5) {
2586 count = decode_uleb128 (p, &p);
2587 for (i = 0; i < count; ++i) {
2589 int64_t ptrdiff = decode_sleb128 (p, &p);
2590 int il_offset = decode_sleb128 (p, &p);
2591 int native_offset = decode_sleb128 (p, &p);
2592 method_base += ptrdiff;
2593 method = lookup_method (method_base);
2595 fprintf (outfile, "sample hit bt %d: %s at IL offset %d (native: %d)\n", i, method->name, il_offset, native_offset);
2598 } else if (subtype == TYPE_SAMPLE_USYM) {
2599 /* un unmanaged symbol description */
2600 uintptr_t addr = ptr_base + decode_sleb128 (p + 1, &p);
2601 uintptr_t size = decode_uleb128 (p, &p);
2603 name = pstrdup ((char*)p);
2604 add_unmanaged_symbol (addr, name, size);
2606 fprintf (outfile, "unmanaged symbol %s at %p\n", name, (void*)addr);
2609 } else if (subtype == TYPE_SAMPLE_UBIN) {
2610 /* un unmanaged binary loaded in memory */
2611 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2612 uintptr_t addr = decode_sleb128 (p, &p);
2613 uint64_t offset G_GNUC_UNUSED = decode_uleb128 (p, &p);
2614 uintptr_t size = decode_uleb128 (p, &p);
2616 LOG_TIME (time_base, tdiff);
2618 name = pstrdup ((char*)p);
2619 add_unmanaged_binary (addr, name, size);
2621 fprintf (outfile, "unmanaged binary %s at %p\n", name, (void*)addr);
2624 } else if (subtype == TYPE_SAMPLE_COUNTERS_DESC) {
2625 uint64_t i, len = decode_uleb128 (p + 1, &p);
2626 for (i = 0; i < len; i++) {
2627 uint64_t type, unit, variance, index;
2628 uint64_t section = decode_uleb128 (p, &p);
2629 char *section_str, *name;
2630 if (section != MONO_COUNTER_PERFCOUNTERS) {
2631 section_str = (char*) section_name (section);
2633 section_str = pstrdup ((char*)p);
2636 name = pstrdup ((char*)p);
2638 type = decode_uleb128 (p, &p);
2639 unit = decode_uleb128 (p, &p);
2640 variance = decode_uleb128 (p, &p);
2641 index = decode_uleb128 (p, &p);
2642 add_counter (section_str, name, (int)type, (int)unit, (int)variance, (int)index);
2644 } else if (subtype == TYPE_SAMPLE_COUNTERS) {
2646 CounterValue *value, *previous = NULL;
2648 uint64_t timestamp = decode_uleb128 (p + 1, &p);
2649 uint64_t time_between = timestamp / 1000 * 1000 * 1000 * 1000 + startup_time;
2651 uint64_t type, index = decode_uleb128 (p, &p);
2655 for (list = counters; list; list = list->next) {
2656 if (list->counter->index == (int)index) {
2657 previous = list->counter->values_last;
2662 type = decode_uleb128 (p, &p);
2664 value = calloc (1, sizeof (CounterValue));
2665 value->timestamp = timestamp;
2668 case MONO_COUNTER_INT:
2669 #if SIZEOF_VOID_P == 4
2670 case MONO_COUNTER_WORD:
2672 value->buffer = malloc (sizeof (int32_t));
2673 *(int32_t*)value->buffer = (int32_t)decode_sleb128 (p, &p) + (previous ? (*(int32_t*)previous->buffer) : 0);
2675 case MONO_COUNTER_UINT:
2676 value->buffer = malloc (sizeof (uint32_t));
2677 *(uint32_t*)value->buffer = (uint32_t)decode_uleb128 (p, &p) + (previous ? (*(uint32_t*)previous->buffer) : 0);
2679 case MONO_COUNTER_LONG:
2680 #if SIZEOF_VOID_P == 8
2681 case MONO_COUNTER_WORD:
2683 case MONO_COUNTER_TIME_INTERVAL:
2684 value->buffer = malloc (sizeof (int64_t));
2685 *(int64_t*)value->buffer = (int64_t)decode_sleb128 (p, &p) + (previous ? (*(int64_t*)previous->buffer) : 0);
2687 case MONO_COUNTER_ULONG:
2688 value->buffer = malloc (sizeof (uint64_t));
2689 *(uint64_t*)value->buffer = (uint64_t)decode_uleb128 (p, &p) + (previous ? (*(uint64_t*)previous->buffer) : 0);
2691 case MONO_COUNTER_DOUBLE:
2692 value->buffer = malloc (sizeof (double));
2693 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
2694 for (i = 0; i < sizeof (double); i++)
2696 for (i = sizeof (double) - 1; i >= 0; i--)
2698 value->buffer[i] = *p++;
2700 case MONO_COUNTER_STRING:
2702 value->buffer = NULL;
2704 value->buffer = (unsigned char*) pstrdup ((char*)p);
2709 if (time_between >= time_from && time_between <= time_to)
2710 add_counter_value (index, value);
2717 case TYPE_COVERAGE:{
2718 int subtype = *p & 0xf0;
2720 case TYPE_COVERAGE_METHOD: {
2721 CoverageMethod *method = g_new0 (CoverageMethod, 1);
2722 const char *assembly, *klass, *name, *sig, *filename;
2723 int token, n_offsets, method_id;
2726 assembly = (void *)p; while (*p) p++; p++;
2727 klass = (void *)p; while (*p) p++; p++;
2728 name = (void *)p; while (*p) p++; p++;
2729 sig = (void *)p; while (*p) p++; p++;
2730 filename = (void *)p; while (*p) p++; p++;
2732 token = decode_uleb128 (p, &p);
2733 method_id = decode_uleb128 (p, &p);
2734 n_offsets = decode_uleb128 (p, &p);
2736 method->assembly_name = g_strdup (assembly);
2737 method->class_name = g_strdup (klass);
2738 method->method_name = g_strdup (name);
2739 method->method_signature = g_strdup (sig);
2740 method->filename = g_strdup (filename);
2741 method->token = token;
2742 method->n_statements = n_offsets;
2743 method->coverage = g_ptr_array_new ();
2744 method->method_id = method_id;
2746 coverage_add_method (method);
2750 case TYPE_COVERAGE_STATEMENT: {
2751 CoverageCoverage *coverage = g_new0 (CoverageCoverage, 1);
2752 int offset, count, line, column, method_id;
2755 method_id = decode_uleb128 (p, &p);
2756 offset = decode_uleb128 (p, &p);
2757 count = decode_uleb128 (p, &p);
2758 line = decode_uleb128 (p, &p);
2759 column = decode_uleb128 (p, &p);
2761 coverage->method_id = method_id;
2762 coverage->offset = offset;
2763 coverage->count = count;
2764 coverage->line = line;
2765 coverage->column = column;
2767 coverage_add_coverage (coverage);
2770 case TYPE_COVERAGE_ASSEMBLY: {
2771 CoverageAssembly *assembly = g_new0 (CoverageAssembly, 1);
2772 char *name, *guid, *filename;
2773 int number_of_methods, fully_covered, partially_covered;
2776 name = (void *)p; while (*p) p++; p++;
2777 guid = (void *)p; while (*p) p++; p++;
2778 filename = (void *)p; while (*p) p++; p++;
2779 number_of_methods = decode_uleb128 (p, &p);
2780 fully_covered = decode_uleb128 (p, &p);
2781 partially_covered = decode_uleb128 (p, &p);
2783 assembly->name = g_strdup (name);
2784 assembly->guid = g_strdup (guid);
2785 assembly->filename = g_strdup (filename);
2786 assembly->number_of_methods = number_of_methods;
2787 assembly->fully_covered = fully_covered;
2788 assembly->partially_covered = partially_covered;
2790 coverage_add_assembly (assembly);
2793 case TYPE_COVERAGE_CLASS: {
2794 CoverageClass *klass = g_new0 (CoverageClass, 1);
2795 char *assembly_name, *class_name;
2796 int number_of_methods, fully_covered, partially_covered;
2799 assembly_name = (void *)p; while (*p) p++; p++;
2800 class_name = (void *)p; while (*p) p++; p++;
2801 number_of_methods = decode_uleb128 (p, &p);
2802 fully_covered = decode_uleb128 (p, &p);
2803 partially_covered = decode_uleb128 (p, &p);
2805 klass->assembly_name = g_strdup (assembly_name);
2806 klass->class_name = g_strdup (class_name);
2807 klass->number_of_methods = number_of_methods;
2808 klass->fully_covered = fully_covered;
2809 klass->partially_covered = partially_covered;
2811 coverage_add_class (klass);
2821 fprintf (outfile, "unhandled profiler event: 0x%x at file offset: %llu + %lld (len: %d\n)\n", *p, (unsigned long long) file_offset, (long long) (p - ctx->buf), len);
2825 thread->last_time = time_base;
2826 for (i = 0; i < thread->stack_id; ++i)
2827 thread->stack [i]->recurse_count = 0;
2832 load_file (char *name)
2835 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
2836 if (strcmp (name, "-") == 0)
2839 ctx->file = fopen (name, "rb");
2841 printf ("Cannot open file: %s\n", name);
2844 #if defined (HAVE_SYS_ZLIB)
2845 if (ctx->file != stdin)
2846 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
2848 if (!load_data (ctx, 32))
2851 if (read_int32 (p) != LOG_HEADER_ID || p [6] > LOG_DATA_VERSION)
2853 ctx->version_major = p [4];
2854 ctx->version_minor = p [5];
2855 ctx->data_version = p [6];
2856 /* reading 64 bit files on 32 bit systems not supported yet */
2857 if (p [7] > sizeof (void*))
2859 if (read_int32 (p + 20)) /* flags must be 0 */
2861 ctx->startup_time = read_int64 (p + 8);
2862 ctx->timer_overhead = read_int32 (p + 16);
2863 ctx->pid = read_int32 (p + 24);
2864 ctx->port = read_int16 (p + 28);
2872 static int alloc_sort_mode = ALLOC_SORT_BYTES;
2875 compare_class (const void *a, const void *b)
2877 ClassDesc *const*A = a;
2878 ClassDesc *const*B = b;
2879 uint64_t vala, valb;
2880 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
2881 vala = (*A)->alloc_size;
2882 valb = (*B)->alloc_size;
2884 vala = (*A)->allocs;
2885 valb = (*B)->allocs;
2895 dump_header (ProfContext *ctx)
2897 time_t st = ctx->startup_time / 1000;
2898 char *t = ctime (&st);
2899 fprintf (outfile, "\nMono log profiler data\n");
2900 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
2901 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
2902 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
2903 fprintf (outfile, "\tProgram startup: %s", t);
2905 fprintf (outfile, "\tProgram ID: %d\n", ctx->pid);
2907 fprintf (outfile, "\tServer listening on: %d\n", ctx->port);
2911 dump_traces (TraceDesc *traces, const char *desc)
2918 sort_context_array (traces);
2919 for (j = 0; j < traces->count; ++j) {
2922 bt = traces->traces [j].bt;
2925 fprintf (outfile, "\t%llu %s from:\n", (unsigned long long) traces->traces [j].count, desc);
2926 for (k = 0; k < bt->count; ++k)
2927 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
2932 dump_threads (ProfContext *ctx)
2934 ThreadContext *thread;
2935 fprintf (outfile, "\nThread summary\n");
2936 for (thread = ctx->threads; thread; thread = thread->next) {
2937 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
2942 dump_exceptions (void)
2945 fprintf (outfile, "\nException summary\n");
2946 fprintf (outfile, "\tThrows: %llu\n", (unsigned long long) throw_count);
2947 dump_traces (&exc_traces, "throws");
2948 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
2949 if (!clause_summary [i])
2951 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), (unsigned long long) clause_summary [i]);
2956 compare_monitor (const void *a, const void *b)
2958 MonitorDesc *const*A = a;
2959 MonitorDesc *const*B = b;
2960 if ((*B)->wait_time == (*A)->wait_time)
2962 if ((*B)->wait_time < (*A)->wait_time)
2968 dump_monitors (void)
2970 MonitorDesc **monitors;
2974 monitors = malloc (sizeof (void*) * num_monitors);
2975 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
2976 MonitorDesc *mdesc = monitor_hash [i];
2978 monitors [j++] = mdesc;
2979 mdesc = mdesc->next;
2982 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
2983 fprintf (outfile, "\nMonitor lock summary\n");
2984 for (i = 0; i < num_monitors; ++i) {
2985 MonitorDesc *mdesc = monitors [i];
2986 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
2987 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
2988 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
2989 dump_traces (&mdesc->traces, "contentions");
2991 fprintf (outfile, "\tLock contentions: %llu\n", (unsigned long long) monitor_contention);
2992 fprintf (outfile, "\tLock acquired: %llu\n", (unsigned long long) monitor_acquired);
2993 fprintf (outfile, "\tLock failures: %llu\n", (unsigned long long) monitor_failed);
3000 fprintf (outfile, "\nGC summary\n");
3001 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
3002 fprintf (outfile, "\tMax heap size: %llu\n", (unsigned long long) max_heap_size);
3003 fprintf (outfile, "\tObject moves: %llu\n", (unsigned long long) gc_object_moves);
3004 for (i = 0; i < 3; ++i) {
3005 if (!gc_info [i].count)
3007 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
3008 i, gc_info [i].count,
3009 (unsigned long long) (gc_info [i].max_time / 1000),
3010 (unsigned long long) (gc_info [i].total_time / 1000),
3011 (unsigned long long) (gc_info [i].total_time / gc_info [i].count / 1000));
3013 for (i = 0; i < 3; ++i) {
3014 if (!handle_info [i].max_live)
3016 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
3017 get_handle_name (i),
3018 (unsigned long long) (handle_info [i].created),
3019 (unsigned long long) (handle_info [i].destroyed),
3020 (unsigned long long) (handle_info [i].max_live));
3021 dump_traces (&handle_info [i].traces, "created");
3030 int compiled_methods = 0;
3032 fprintf (outfile, "\nJIT summary\n");
3033 for (i = 0; i < HASH_SIZE; ++i) {
3034 m = method_hash [i];
3035 for (m = method_hash [i]; m; m = m->next) {
3036 if (!m->code || m->ignore_jit)
3039 code_size += m->len;
3042 fprintf (outfile, "\tCompiled methods: %d\n", compiled_methods);
3043 fprintf (outfile, "\tGenerated code size: %d\n", code_size);
3044 fprintf (outfile, "\tJIT helpers: %d\n", num_jit_helpers);
3045 fprintf (outfile, "\tJIT helpers code size: %d\n", jit_helpers_code_size);
3049 dump_allocations (void)
3052 intptr_t allocs = 0;
3054 int header_done = 0;
3055 ClassDesc **classes = malloc (num_classes * sizeof (void*));
3058 for (i = 0; i < HASH_SIZE; ++i) {
3059 cd = class_hash [i];
3065 qsort (classes, num_classes, sizeof (void*), compare_class);
3066 for (i = 0; i < num_classes; ++i) {
3070 allocs += cd->allocs;
3071 size += cd->alloc_size;
3072 if (!header_done++) {
3073 fprintf (outfile, "\nAllocation summary\n");
3074 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
3076 fprintf (outfile, "%10llu %10zd %8llu %s\n",
3077 (unsigned long long) (cd->alloc_size),
3079 (unsigned long long) (cd->alloc_size / cd->allocs),
3081 dump_traces (&cd->traces, "bytes");
3084 fprintf (outfile, "Total memory allocated: %llu bytes in %zd objects\n", (unsigned long long) size, allocs);
3093 static int method_sort_mode = METHOD_SORT_TOTAL;
3096 compare_method (const void *a, const void *b)
3098 MethodDesc *const*A = a;
3099 MethodDesc *const*B = b;
3100 uint64_t vala, valb;
3101 if (method_sort_mode == METHOD_SORT_SELF) {
3102 vala = (*A)->self_time;
3103 valb = (*B)->self_time;
3104 } else if (method_sort_mode == METHOD_SORT_CALLS) {
3108 vala = (*A)->total_time;
3109 valb = (*B)->total_time;
3119 dump_metadata (void)
3121 fprintf (outfile, "\nMetadata summary\n");
3122 fprintf (outfile, "\tLoaded images: %d\n", num_images);
3126 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
3127 image = image_hash [i];
3129 fprintf (outfile, "\t\t%s\n", image->filename);
3130 image = image->next;
3142 int header_done = 0;
3143 MethodDesc **methods = malloc (num_methods * sizeof (void*));
3146 for (i = 0; i < HASH_SIZE; ++i) {
3147 cd = method_hash [i];
3149 cd->total_time = cd->self_time + cd->callee_time;
3154 qsort (methods, num_methods, sizeof (void*), compare_method);
3155 for (i = 0; i < num_methods; ++i) {
3162 msecs = cd->total_time / 1000000;
3163 smsecs = (cd->total_time - cd->callee_time) / 1000000;
3164 if (!msecs && !verbose)
3166 if (!header_done++) {
3167 fprintf (outfile, "\nMethod call summary\n");
3168 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
3170 fprintf (outfile, "%8llu %8llu %10llu %s\n",
3171 (unsigned long long) (msecs),
3172 (unsigned long long) (smsecs),
3173 (unsigned long long) (cd->calls),
3175 dump_traces (&cd->traces, "calls");
3178 fprintf (outfile, "Total calls: %llu\n", (unsigned long long) calls);
3182 compare_heap_class (const void *a, const void *b)
3184 HeapClassDesc *const*A = a;
3185 HeapClassDesc *const*B = b;
3186 uint64_t vala, valb;
3187 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
3188 vala = (*A)->total_size;
3189 valb = (*B)->total_size;
3202 compare_rev_class (const void *a, const void *b)
3204 const HeapClassRevRef *A = a;
3205 const HeapClassRevRef *B = b;
3206 if (B->count == A->count)
3208 if (B->count < A->count)
3214 dump_rev_claases (HeapClassRevRef *revs, int count)
3221 for (j = 0; j < count; ++j) {
3222 HeapClassDesc *cd = revs [j].klass;
3223 fprintf (outfile, "\t\t%llu references from: %s\n",
3224 (unsigned long long) (revs [j].count),
3230 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
3237 HeapClassDesc **sorted;
3238 sorted = malloc (sizeof (void*) * hs->class_count);
3239 for (i = 0; i < hs->hash_size; ++i) {
3240 cd = hs->class_hash [i];
3244 size += cd->total_size;
3245 sorted [ccount++] = cd;
3247 hs->sorted = sorted;
3248 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
3249 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d, roots: %zd\n",
3251 (hs->timestamp - startup_time)/1000000000.0,
3252 (unsigned long long) (size),
3253 (unsigned long long) (count),
3254 ccount, hs->num_roots);
3255 if (!verbose && ccount > 30)
3257 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
3258 for (i = 0; i < ccount; ++i) {
3259 HeapClassRevRef *rev_sorted;
3261 HeapClassDesc *ocd = NULL;
3264 ocd = heap_class_lookup (last_hs, cd->klass);
3265 fprintf (outfile, "\t%10llu %10llu %8llu %s",
3266 (unsigned long long) (cd->total_size),
3267 (unsigned long long) (cd->count),
3268 (unsigned long long) (cd->total_size / cd->count),
3271 int64_t bdiff = cd->total_size - ocd->total_size;
3272 int64_t cdiff = cd->count - ocd->count;
3273 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", (long long) bdiff, (long long) cdiff);
3275 fprintf (outfile, "\n");
3277 if (!collect_traces)
3279 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
3281 for (j = 0; j < cd->rev_hash_size; ++j) {
3282 if (cd->rev_hash [j].klass)
3283 rev_sorted [k++] = cd->rev_hash [j];
3285 assert (cd->rev_count == k);
3286 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
3287 if (cd->root_references)
3288 fprintf (outfile, "\t\t%zd root references (%zd pinning)\n", cd->root_references, cd->pinned_references);
3289 dump_rev_claases (rev_sorted, cd->rev_count);
3296 compare_heap_shots (const void *a, const void *b)
3298 HeapShot *const*A = a;
3299 HeapShot *const*B = b;
3300 if ((*B)->timestamp == (*A)->timestamp)
3302 if ((*B)->timestamp > (*A)->timestamp)
3308 dump_heap_shots (void)
3310 HeapShot **hs_sorted;
3312 HeapShot *last_hs = NULL;
3316 hs_sorted = malloc (num_heap_shots * sizeof (void*));
3317 fprintf (outfile, "\nHeap shot summary\n");
3319 for (hs = heap_shots; hs; hs = hs->next)
3320 hs_sorted [i++] = hs;
3321 qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
3322 for (i = 0; i < num_heap_shots; ++i) {
3324 heap_shot_summary (hs, i, last_hs);
3329 /* This is a very basic escape function that escapes < > and &
3330 Ideally we'd use g_markup_escape_string but that function isn't
3331 available in Mono's eglib. This was written without looking at the
3332 source of that function in glib. */
3334 escape_string_for_xml (const char *string)
3336 GString *string_builder = g_string_new (NULL);
3337 const char *start, *p;
3341 while (*p && *p != '&' && *p != '<' && *p != '>')
3344 g_string_append_len (string_builder, start, p - start);
3351 g_string_append (string_builder, "<");
3355 g_string_append (string_builder, ">");
3359 g_string_append (string_builder, "&");
3370 return g_string_free (string_builder, FALSE);
3374 sort_assemblies (gconstpointer a, gconstpointer b)
3376 CoverageAssembly *assembly_a = *(CoverageAssembly **)a;
3377 CoverageAssembly *assembly_b = *(CoverageAssembly **)b;
3379 if (assembly_a->name == NULL && assembly_b->name == NULL)
3381 else if (assembly_a->name == NULL)
3383 else if (assembly_b->name == NULL)
3386 return strcmp (assembly_a->name, assembly_b->name);
3390 dump_coverage (void)
3392 if (!coverage_methods && !coverage_assemblies)
3395 gather_coverage_statements ();
3396 fprintf (outfile, "\nCoverage Summary:\n");
3398 if (coverage_outfile) {
3399 fprintf (coverage_outfile, "<?xml version=\"1.0\"?>\n");
3400 fprintf (coverage_outfile, "<coverage version=\"0.3\">\n");
3403 g_ptr_array_sort (coverage_assemblies, sort_assemblies);
3405 for (guint i = 0; i < coverage_assemblies->len; i++) {
3406 CoverageAssembly *assembly = coverage_assemblies->pdata[i];
3409 if (assembly->number_of_methods != 0) {
3410 int percentage = ((assembly->fully_covered + assembly->partially_covered) * 100) / assembly->number_of_methods;
3411 fprintf (outfile, "\t%s (%s) %d%% covered (%d methods - %d covered)\n", assembly->name, assembly->filename, percentage, assembly->number_of_methods, assembly->fully_covered);
3413 fprintf (outfile, "\t%s (%s) ?%% covered (%d methods - %d covered)\n", assembly->name, assembly->filename, assembly->number_of_methods, assembly->fully_covered);
3415 if (coverage_outfile) {
3416 char *escaped_name, *escaped_filename;
3417 escaped_name = escape_string_for_xml (assembly->name);
3418 escaped_filename = escape_string_for_xml (assembly->filename);
3420 fprintf (coverage_outfile, "\t<assembly name=\"%s\" guid=\"%s\" filename=\"%s\" method-count=\"%d\" full=\"%d\" partial=\"%d\"/>\n", escaped_name, assembly->guid, escaped_filename, assembly->number_of_methods, assembly->fully_covered, assembly->partially_covered);
3422 g_free (escaped_name);
3423 g_free (escaped_filename);
3426 classes = g_hash_table_lookup (coverage_assembly_classes, assembly->name);
3428 for (guint j = 0; j < classes->len; j++) {
3429 CoverageClass *klass = classes->pdata[j];
3431 if (klass->number_of_methods > 0) {
3432 int percentage = ((klass->fully_covered + klass->partially_covered) * 100) / klass->number_of_methods;
3433 fprintf (outfile, "\t\t%s %d%% covered (%d methods - %d covered)\n", klass->class_name, percentage, klass->number_of_methods, klass->fully_covered);
3435 fprintf (outfile, "\t\t%s ?%% covered (%d methods - %d covered)\n", klass->class_name, klass->number_of_methods, klass->fully_covered);
3437 if (coverage_outfile) {
3439 escaped_name = escape_string_for_xml (klass->class_name);
3441 fprintf (coverage_outfile, "\t\t<class name=\"%s\" method-count=\"%d\" full=\"%d\" partial=\"%d\"/>\n", escaped_name, klass->number_of_methods, klass->fully_covered, klass->partially_covered);
3442 g_free (escaped_name);
3448 for (guint i = 0; i < coverage_methods->len; i++) {
3449 CoverageMethod *method = coverage_methods->pdata[i];
3451 if (coverage_outfile) {
3452 char *escaped_assembly, *escaped_class, *escaped_method, *escaped_sig, *escaped_filename;
3454 escaped_assembly = escape_string_for_xml (method->assembly_name);
3455 escaped_class = escape_string_for_xml (method->class_name);
3456 escaped_method = escape_string_for_xml (method->method_name);
3457 escaped_sig = escape_string_for_xml (method->method_signature);
3458 escaped_filename = escape_string_for_xml (method->filename);
3460 fprintf (coverage_outfile, "\t<method assembly=\"%s\" class=\"%s\" name=\"%s (%s)\" filename=\"%s\" token=\"%d\">\n", escaped_assembly, escaped_class, escaped_method, escaped_sig, escaped_filename, method->token);
3462 g_free (escaped_assembly);
3463 g_free (escaped_class);
3464 g_free (escaped_method);
3465 g_free (escaped_sig);
3466 g_free (escaped_filename);
3468 for (guint j = 0; j < method->coverage->len; j++) {
3469 CoverageCoverage *coverage = method->coverage->pdata[j];
3470 fprintf (coverage_outfile, "\t\t<statement offset=\"%d\" counter=\"%d\" line=\"%d\" column=\"%d\"/>\n", coverage->offset, coverage->count, coverage->line, coverage->column);
3472 fprintf (coverage_outfile, "\t</method>\n");
3476 if (coverage_outfile) {
3477 fprintf (coverage_outfile, "</coverage>\n");
3478 fclose (coverage_outfile);
3479 coverage_outfile = NULL;
3484 flush_context (ProfContext *ctx)
3486 ThreadContext *thread;
3487 /* FIXME: sometimes there are leftovers: indagate */
3488 for (thread = ctx->threads; thread; thread = thread->next) {
3489 while (thread->stack_id) {
3491 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
3492 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
3497 static const char *reports = "header,jit,gc,sample,alloc,call,metadata,exception,monitor,thread,heapshot,counters,coverage";
3500 match_option (const char *p, const char *opt)
3502 int len = strlen (opt);
3503 if (strncmp (p, opt, len) == 0) {
3512 print_reports (ProfContext *ctx, const char *reps, int parse_only)
3516 for (p = reps; *p; p = opt) {
3517 if ((opt = match_option (p, "header")) != p) {
3522 if ((opt = match_option (p, "thread")) != p) {
3527 if ((opt = match_option (p, "gc")) != p) {
3532 if ((opt = match_option (p, "jit")) != p) {
3537 if ((opt = match_option (p, "alloc")) != p) {
3539 dump_allocations ();
3542 if ((opt = match_option (p, "call")) != p) {
3547 if ((opt = match_option (p, "metadata")) != p) {
3552 if ((opt = match_option (p, "exception")) != p) {
3557 if ((opt = match_option (p, "monitor")) != p) {
3562 if ((opt = match_option (p, "heapshot")) != p) {
3567 if ((opt = match_option (p, "sample")) != p) {
3572 if ((opt = match_option (p, "counters")) != p) {
3577 if ((opt = match_option (p, "coverage")) != p) {
3588 add_find_spec (const char *p)
3590 if (p [0] == 'S' && p [1] == ':') {
3592 find_size = strtoul (p + 2, &vale, 10);
3594 } else if (p [0] == 'T' && p [1] == ':') {
3604 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
3605 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
3606 printf ("FILENAME can be '-' to read from standard input.\n");
3607 printf ("Options:\n");
3608 printf ("\t--help display this help\n");
3609 printf ("\t--out=FILE write to FILE instead of stdout\n");
3610 printf ("\t--traces collect and show backtraces\n");
3611 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
3612 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
3613 printf ("\t %s\n", reports);
3614 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
3615 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
3616 printf ("\t--counters-sort=MODE sort counters according to MODE: time, category\n");
3617 printf ("\t only accessible in verbose mode\n");
3618 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
3619 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
3620 printf ("\t S:minimum_size or T:partial_name\n");
3621 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
3622 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
3623 printf ("\t--verbose increase verbosity level\n");
3624 printf ("\t--debug display decoding debug info for mprof-report devs\n");
3625 printf ("\t--coverage-out=FILE write the coverage info to FILE as XML\n");
3629 main (int argc, char *argv[])
3634 for (i = 1; i < argc; ++i) {
3635 if (strcmp ("--debug", argv [i]) == 0) {
3637 } else if (strcmp ("--help", argv [i]) == 0) {
3640 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
3641 const char *val = argv [i] + 13;
3642 if (strcmp (val, "bytes") == 0) {
3643 alloc_sort_mode = ALLOC_SORT_BYTES;
3644 } else if (strcmp (val, "count") == 0) {
3645 alloc_sort_mode = ALLOC_SORT_COUNT;
3650 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
3651 const char *val = argv [i] + 14;
3652 if (strcmp (val, "total") == 0) {
3653 method_sort_mode = METHOD_SORT_TOTAL;
3654 } else if (strcmp (val, "self") == 0) {
3655 method_sort_mode = METHOD_SORT_SELF;
3656 } else if (strcmp (val, "calls") == 0) {
3657 method_sort_mode = METHOD_SORT_CALLS;
3662 } else if (strncmp ("--counters-sort=", argv [i], 16) == 0) {
3663 const char *val = argv [i] + 16;
3664 if (strcmp (val, "time") == 0) {
3665 counters_sort_mode = COUNTERS_SORT_TIME;
3666 } else if (strcmp (val, "category") == 0) {
3667 counters_sort_mode = COUNTERS_SORT_CATEGORY;
3672 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
3673 const char *val = argv [i] + 10;
3674 if (!print_reports (NULL, val, 1)) {
3679 } else if (strncmp ("--out=", argv [i], 6) == 0) {
3680 const char *val = argv [i] + 6;
3681 outfile = fopen (val, "w");
3683 printf ("Cannot open output file: %s\n", val);
3686 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
3687 const char *val = argv [i] + 12;
3689 trace_max = strtoul (val, &vale, 10);
3690 } else if (strncmp ("--find=", argv [i], 7) == 0) {
3691 const char *val = argv [i] + 7;
3692 if (!add_find_spec (val)) {
3696 } else if (strncmp ("--track=", argv [i], 8) == 0) {
3697 const char *val = argv [i] + 8;
3700 uintptr_t tracked_obj;
3705 tracked_obj = strtoul (val, &vale, 0);
3706 found_object (tracked_obj);
3709 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
3710 const char *val = argv [i] + 9;
3712 thread_filter = strtoul (val, &vale, 0);
3713 } else if (strncmp ("--time=", argv [i], 7) == 0) {
3714 char *val = pstrdup (argv [i] + 7);
3715 double from_secs, to_secs;
3716 char *top = strchr (val, '-');
3722 from_secs = atof (val);
3723 to_secs = atof (top);
3725 if (from_secs > to_secs) {
3729 time_from = from_secs * 1000000000;
3730 time_to = to_secs * 1000000000;
3731 use_time_filter = 1;
3732 } else if (strcmp ("--verbose", argv [i]) == 0) {
3734 } else if (strcmp ("--traces", argv [i]) == 0) {
3737 } else if (strncmp ("--coverage-out=", argv [i], 15) == 0) {
3738 const char *val = argv [i] + 15;
3739 coverage_outfile = fopen (val, "w");
3740 if (!coverage_outfile) {
3741 printf ("Cannot open output file: %s\n", val);
3752 ctx = load_file (argv [i]);
3754 printf ("Not a log profiler data file (or unsupported version).\n");
3757 while (decode_buffer (ctx));
3758 flush_context (ctx);
3759 if (num_tracked_objects)
3761 print_reports (ctx, reports, 0);