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)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 * The Coverage XML output schema
23 * <coverage> - The root element of the documentation. It can contain any number of
24 * <assembly>, <class> or <method> elements.
26 * - version: The version number for the file format - (eg: "0.3")
27 * <assembly> - Contains data about assemblies. Has no child elements
29 * - name: The name of the assembly - (eg: "System.Xml")
30 * - guid: The GUID of the assembly
31 * - filename: The filename of the assembly
32 * - method-count: The number of methods in the assembly
33 * - full: The number of fully covered methods
34 * - partial: The number of partially covered methods
35 * <class> - Contains data about classes. Has no child elements
37 * - name: The name of the class
38 * - method-count: The number of methods in the class
39 * - full: The number of fully covered methods
40 * - partial: The number of partially covered methods
41 * <method> - Contains data about methods. Can contain any number of <statement> elements
43 * - assembly: The name of the parent assembly
44 * - class: The name of the parent class
45 * - name: The name of the method, with all it's parameters
46 * - filename: The name of the source file containing this method
48 * <statement> - Contains data about IL statements. Has no child elements
50 * - offset: The offset of the statement in the IL code after the previous
52 * - counter: 1 if the line was covered, 0 if it was not
53 * - line: The line number in the parent method's file
54 * - column: The column on the line
62 #if !defined(__APPLE__) && !defined(__FreeBSD__)
67 #if defined (HAVE_SYS_ZLIB)
71 #include <mono/metadata/profiler.h>
72 #include <mono/metadata/object.h>
73 #include <mono/metadata/debug-helpers.h>
74 #include <mono/utils/mono-counters.h>
76 #define HASH_SIZE 9371
77 #define SMALL_HASH_SIZE 31
79 #if defined(__native_client__) || defined(__native_client_codegen__)
80 volatile int __nacl_thread_suspension_needed = 0;
81 void __nacl_suspend_thread_if_needed() {}
85 static int collect_traces = 0;
86 static int show_traces = 0;
87 static int trace_max = 6;
88 static int verbose = 0;
89 static uintptr_t *tracked_objects = 0;
90 static int num_tracked_objects = 0;
91 static uintptr_t thread_filter = 0;
92 static uint64_t find_size = 0;
93 static const char* find_name = NULL;
94 static uint64_t time_from = 0;
95 static uint64_t time_to = 0xffffffffffffffffULL;
96 static int use_time_filter = 0;
97 static uint64_t startup_time = 0;
98 static FILE* outfile = NULL;
99 static FILE* coverage_outfile = NULL;
102 read_int16 (unsigned char *p)
104 int32_t value = *p++;
105 value |= (*p++) << 8;
110 read_int32 (unsigned char *p)
112 int32_t value = *p++;
113 value |= (*p++) << 8;
114 value |= (*p++) << 16;
115 value |= (uint32_t)(*p++) << 24;
120 read_int64 (unsigned char *p)
122 uint64_t value = *p++;
123 value |= (*p++) << 8;
124 value |= (*p++) << 16;
125 value |= (uint64_t)(*p++) << 24;
126 value |= (uint64_t)(*p++) << 32;
127 value |= (uint64_t)(*p++) << 40;
128 value |= (uint64_t)(*p++) << 48;
129 value |= (uint64_t)(*p++) << 54;
134 pstrdup (const char *s)
136 int len = strlen (s) + 1;
137 char *p = (char *)malloc (len);
142 typedef struct _CounterValue CounterValue;
143 struct _CounterValue {
145 unsigned char *buffer;
149 typedef struct _Counter Counter;
157 CounterValue *values;
158 CounterValue *values_last;
161 typedef struct _CounterList CounterList;
162 struct _CounterList {
167 typedef struct _CounterSection CounterSection;
168 struct _CounterSection {
170 CounterList *counters;
171 CounterList *counters_last;
172 CounterSection *next;
175 typedef struct _CounterTimestamp CounterTimestamp;
176 struct _CounterTimestamp {
178 CounterSection *sections;
179 CounterSection *sections_last;
180 CounterTimestamp *next;
183 static CounterList *counters = NULL;
184 static CounterSection *counters_sections = NULL;
185 static CounterTimestamp *counters_timestamps = NULL;
189 COUNTERS_SORT_CATEGORY
192 static int counters_sort_mode = COUNTERS_SORT_TIME;
195 add_counter_to_section (Counter *counter)
197 CounterSection *csection, *s;
200 clist = (CounterList *)calloc (1, sizeof (CounterList));
201 clist->counter = counter;
203 for (csection = counters_sections; csection; csection = csection->next) {
204 if (strcmp (csection->value, counter->section) == 0) {
205 /* If section exist */
206 if (!csection->counters)
207 csection->counters = clist;
209 csection->counters_last->next = clist;
210 csection->counters_last = clist;
215 /* If section does not exist */
216 csection = (CounterSection *)calloc (1, sizeof (CounterSection));
217 csection->value = counter->section;
218 csection->counters = clist;
219 csection->counters_last = clist;
221 if (!counters_sections) {
222 counters_sections = csection;
224 s = counters_sections;
232 add_counter (const char *section, const char *name, int type, int unit, int variance, int index)
234 CounterList *list, *l;
237 for (list = counters; list; list = list->next)
238 if (list->counter->index == index)
241 counter = (Counter *)calloc (1, sizeof (Counter));
242 counter->section = section;
243 counter->name = name;
244 counter->type = type;
245 counter->unit = unit;
246 counter->variance = variance;
247 counter->index = index;
249 list = (CounterList *)calloc (1, sizeof (CounterList));
250 list->counter = counter;
261 if (counters_sort_mode == COUNTERS_SORT_CATEGORY || !verbose)
262 add_counter_to_section (counter);
266 add_counter_to_timestamp (uint64_t timestamp, Counter *counter)
268 CounterTimestamp *ctimestamp, *t;
269 CounterSection *csection;
272 clist = (CounterList *)calloc (1, sizeof (CounterList));
273 clist->counter = counter;
275 for (ctimestamp = counters_timestamps; ctimestamp; ctimestamp = ctimestamp->next) {
276 if (ctimestamp->value == timestamp) {
277 for (csection = ctimestamp->sections; csection; csection = csection->next) {
278 if (strcmp (csection->value, counter->section) == 0) {
279 /* if timestamp exist and section exist */
280 if (!csection->counters)
281 csection->counters = clist;
283 csection->counters_last->next = clist;
284 csection->counters_last = clist;
289 /* if timestamp exist and section does not exist */
290 csection = (CounterSection *)calloc (1, sizeof (CounterSection));
291 csection->value = counter->section;
292 csection->counters = clist;
293 csection->counters_last = clist;
295 if (!ctimestamp->sections)
296 ctimestamp->sections = csection;
298 ctimestamp->sections_last->next = csection;
299 ctimestamp->sections_last = csection;
304 /* If timestamp do not exist and section does not exist */
305 csection = (CounterSection *)calloc (1, sizeof (CounterSection));
306 csection->value = counter->section;
307 csection->counters = clist;
308 csection->counters_last = clist;
310 ctimestamp = (CounterTimestamp *)calloc (1, sizeof (CounterTimestamp));
311 ctimestamp->value = timestamp;
312 ctimestamp->sections = csection;
313 ctimestamp->sections_last = csection;
315 if (!counters_timestamps) {
316 counters_timestamps = ctimestamp;
318 t = counters_timestamps;
321 t->next = ctimestamp;
326 add_counter_value (int index, CounterValue *value)
330 for (list = counters; list; list = list->next) {
331 if (list->counter->index == index) {
332 if (!list->counter->values)
333 list->counter->values = value;
335 list->counter->values_last->next = value;
336 list->counter->values_last = value;
338 if (counters_sort_mode == COUNTERS_SORT_TIME)
339 add_counter_to_timestamp (value->timestamp, list->counter);
347 section_name (int section)
350 case MONO_COUNTER_JIT: return "Mono JIT";
351 case MONO_COUNTER_GC: return "Mono GC";
352 case MONO_COUNTER_METADATA: return "Mono Metadata";
353 case MONO_COUNTER_GENERICS: return "Mono Generics";
354 case MONO_COUNTER_SECURITY: return "Mono Security";
355 case MONO_COUNTER_RUNTIME: return "Mono Runtime";
356 case MONO_COUNTER_SYSTEM: return "Mono System";
357 default: return "<unknown>";
365 case MONO_COUNTER_INT: return "Int";
366 case MONO_COUNTER_UINT: return "UInt";
367 case MONO_COUNTER_WORD: return "Word";
368 case MONO_COUNTER_LONG: return "Long";
369 case MONO_COUNTER_ULONG: return "ULong";
370 case MONO_COUNTER_DOUBLE: return "Double";
371 case MONO_COUNTER_STRING: return "String";
372 case MONO_COUNTER_TIME_INTERVAL: return "Time Interval";
373 default: return "<unknown>";
381 case MONO_COUNTER_RAW: return "Raw";
382 case MONO_COUNTER_BYTES: return "Bytes";
383 case MONO_COUNTER_TIME: return "Time";
384 case MONO_COUNTER_COUNT: return "Count";
385 case MONO_COUNTER_PERCENTAGE: return "Percentage";
386 default: return "<unknown>";
391 variance_name (int variance)
394 case MONO_COUNTER_MONOTONIC: return "Monotonic";
395 case MONO_COUNTER_CONSTANT: return "Constant";
396 case MONO_COUNTER_VARIABLE: return "Variable";
397 default: return "<unknown>";
402 dump_counters_value (Counter *counter, const char *key_format, const char *key, void *value)
407 snprintf (format, sizeof (format), "%s : %%s\n", key_format);
408 fprintf (outfile, format, key, "<null>");
410 switch (counter->type) {
411 case MONO_COUNTER_INT:
412 #if SIZEOF_VOID_P == 4
413 case MONO_COUNTER_WORD:
415 snprintf (format, sizeof (format), "%s : %%d\n", key_format);
416 fprintf (outfile, format, key, *(int32_t*)value);
418 case MONO_COUNTER_UINT:
419 snprintf (format, sizeof (format), "%s : %%u\n", key_format);
420 fprintf (outfile, format, key, *(uint32_t*)value);
422 case MONO_COUNTER_LONG:
423 #if SIZEOF_VOID_P == 8
424 case MONO_COUNTER_WORD:
426 case MONO_COUNTER_TIME_INTERVAL:
427 if (counter->type == MONO_COUNTER_LONG && counter->unit == MONO_COUNTER_TIME) {
428 snprintf (format, sizeof (format), "%s : %%0.3fms\n", key_format);
429 fprintf (outfile, format, key, (double)*(int64_t*)value / 10000.0);
430 } else if (counter->type == MONO_COUNTER_TIME_INTERVAL) {
431 snprintf (format, sizeof (format), "%s : %%0.3fms\n", key_format);
432 fprintf (outfile, format, key, (double)*(int64_t*)value / 1000.0);
434 snprintf (format, sizeof (format), "%s : %%u\n", key_format);
435 fprintf (outfile, format, key, *(int64_t*)value);
438 case MONO_COUNTER_ULONG:
439 snprintf (format, sizeof (format), "%s : %%llu\n", key_format);
440 fprintf (outfile, format, key, *(uint64_t*)value);
442 case MONO_COUNTER_DOUBLE:
443 snprintf (format, sizeof (format), "%s : %%f\n", key_format);
444 fprintf (outfile, format, key, *(double*)value);
446 case MONO_COUNTER_STRING:
447 snprintf (format, sizeof (format), "%s : %%s\n", key_format);
448 fprintf (outfile, format, key, *(char*)value);
458 CounterValue *cvalue;
459 CounterTimestamp *ctimestamp;
460 CounterSection *csection;
462 char strtimestamp[17];
463 int i, section_printed;
465 fprintf (outfile, "\nCounters:\n");
468 char counters_to_print[][64] = {
470 "Methods JITted using mono JIT",
471 "Methods JITted using LLVM",
472 "Total time spent JITting (sec)",
480 "CPU Load Average - 1min",
481 "CPU Load Average - 5min",
482 "CPU Load Average - 15min",
486 for (csection = counters_sections; csection; csection = csection->next) {
489 for (clist = csection->counters; clist; clist = clist->next) {
490 counter = clist->counter;
491 if (!counter->values_last)
494 for (i = 0; counters_to_print [i][0] != 0; i++) {
495 if (strcmp (counters_to_print [i], counter->name) == 0) {
496 if (!section_printed) {
497 fprintf (outfile, "\t%s:\n", csection->value);
501 dump_counters_value (counter, "\t\t%-30s", counter->name, counter->values_last->buffer);
507 } else if (counters_sort_mode == COUNTERS_SORT_TIME) {
508 for (ctimestamp = counters_timestamps; ctimestamp; ctimestamp = ctimestamp->next) {
509 fprintf (outfile, "\t%llu:%02llu:%02llu:%02llu.%03llu:\n",
510 (unsigned long long) (ctimestamp->value / 1000 / 60 / 60 / 24 % 1000),
511 (unsigned long long) (ctimestamp->value / 1000 / 60 / 60 % 24),
512 (unsigned long long) (ctimestamp->value / 1000 / 60 % 60),
513 (unsigned long long) (ctimestamp->value / 1000 % 60),
514 (unsigned long long) (ctimestamp->value % 1000));
516 for (csection = ctimestamp->sections; csection; csection = csection->next) {
517 fprintf (outfile, "\t\t%s:\n", csection->value);
519 for (clist = csection->counters; clist; clist = clist->next) {
520 counter = clist->counter;
521 for (cvalue = counter->values; cvalue; cvalue = cvalue->next) {
522 if (cvalue->timestamp != ctimestamp->value)
525 dump_counters_value (counter, "\t\t\t%-30s", counter->name, cvalue->buffer);
530 } else if (counters_sort_mode == COUNTERS_SORT_CATEGORY) {
531 for (csection = counters_sections; csection; csection = csection->next) {
532 fprintf (outfile, "\t%s:\n", csection->value);
534 for (clist = csection->counters; clist; clist = clist->next) {
535 counter = clist->counter;
536 fprintf (outfile, "\t\t%s: [type: %s, unit: %s, variance: %s]\n",
537 counter->name, type_name (counter->type), unit_name (counter->unit), variance_name (counter->variance));
539 for (cvalue = counter->values; cvalue; cvalue = cvalue->next) {
540 snprintf (strtimestamp, sizeof (strtimestamp), "%llu:%02llu:%02llu:%02llu.%03llu",
541 (unsigned long long) (cvalue->timestamp / 1000 / 60 / 60 / 24 % 1000),
542 (unsigned long long) (cvalue->timestamp / 1000 / 60 / 60 % 24),
543 (unsigned long long) (cvalue->timestamp / 1000 / 60 % 60),
544 (unsigned long long) (cvalue->timestamp / 1000 % 60),
545 (unsigned long long) (cvalue->timestamp % 1000));
547 dump_counters_value (counter, "\t\t\t%s", strtimestamp, cvalue->buffer);
554 static int num_images;
555 typedef struct _ImageDesc ImageDesc;
562 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
565 add_image (intptr_t image, char *name)
567 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
568 ImageDesc *cd = (ImageDesc *)malloc (sizeof (ImageDesc));
570 cd->filename = pstrdup (name);
571 cd->next = image_hash [slot];
572 image_hash [slot] = cd;
576 static int num_assemblies;
578 typedef struct _AssemblyDesc AssemblyDesc;
579 struct _AssemblyDesc {
585 static AssemblyDesc* assembly_hash [SMALL_HASH_SIZE] = {0};
588 add_assembly (intptr_t assembly, char *name)
590 int slot = ((assembly >> 2) & 0xffff) % SMALL_HASH_SIZE;
591 AssemblyDesc *cd = (AssemblyDesc *)malloc (sizeof (AssemblyDesc));
592 cd->assembly = assembly;
593 cd->asmname = pstrdup (name);
594 cd->next = assembly_hash [slot];
595 assembly_hash [slot] = cd;
599 typedef struct _BackTrace BackTrace;
611 typedef struct _ClassDesc ClassDesc;
621 static ClassDesc* class_hash [HASH_SIZE] = {0};
622 static int num_classes = 0;
625 add_class (intptr_t klass, const char *name)
627 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
629 cd = class_hash [slot];
630 while (cd && cd->klass != klass)
632 /* we resolved an unknown class (unless we had the code unloaded) */
634 /*printf ("resolved unknown: %s\n", name);*/
636 cd->name = pstrdup (name);
639 cd = (ClassDesc *)calloc (sizeof (ClassDesc), 1);
641 cd->name = pstrdup (name);
642 cd->next = class_hash [slot];
645 cd->traces.count = 0;
647 cd->traces.traces = NULL;
648 class_hash [slot] = cd;
654 lookup_class (intptr_t klass)
656 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
657 ClassDesc *cd = class_hash [slot];
658 while (cd && cd->klass != klass)
662 snprintf (buf, sizeof (buf), "unresolved class %p", (void*)klass);
663 return add_class (klass, buf);
668 typedef struct _MethodDesc MethodDesc;
677 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 */
680 uint64_t callee_time;
685 static MethodDesc* method_hash [HASH_SIZE] = {0};
686 static int num_methods = 0;
689 add_method (intptr_t method, const char *name, intptr_t code, int len)
691 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
693 cd = method_hash [slot];
694 while (cd && cd->method != method)
696 /* we resolved an unknown method (unless we had the code unloaded) */
700 /*printf ("resolved unknown: %s\n", name);*/
702 cd->name = pstrdup (name);
705 cd = (MethodDesc *)calloc (sizeof (MethodDesc), 1);
707 cd->name = pstrdup (name);
712 cd->traces.count = 0;
714 cd->traces.traces = NULL;
715 cd->next = method_hash [slot];
716 method_hash [slot] = cd;
722 lookup_method (intptr_t method)
724 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
725 MethodDesc *cd = method_hash [slot];
726 while (cd && cd->method != method)
730 snprintf (buf, sizeof (buf), "unknown method %p", (void*)method);
731 return add_method (method, buf, 0, 0);
736 static int num_stat_samples = 0;
737 static int size_stat_samples = 0;
738 uintptr_t *stat_samples = NULL;
739 int *stat_sample_desc = NULL;
742 add_stat_sample (int type, uintptr_t ip) {
743 if (num_stat_samples == size_stat_samples) {
744 size_stat_samples *= 2;
745 if (!size_stat_samples)
746 size_stat_samples = 32;
747 stat_samples = (uintptr_t *)realloc (stat_samples, size_stat_samples * sizeof (uintptr_t));
748 stat_sample_desc = (int *)realloc (stat_sample_desc, size_stat_samples * sizeof (int));
750 stat_samples [num_stat_samples] = ip;
751 stat_sample_desc [num_stat_samples++] = type;
755 lookup_method_by_ip (uintptr_t ip)
760 for (i = 0; i < HASH_SIZE; ++i) {
763 //printf ("checking %p against %p-%p\n", (void*)ip, (void*)(m->code), (void*)(m->code + m->len));
764 if (ip >= (uintptr_t)m->code && ip < (uintptr_t)m->code + m->len) {
774 compare_method_samples (const void *a, const void *b)
776 MethodDesc *const *A = (MethodDesc *const *)a;
777 MethodDesc *const *B = (MethodDesc *const *)b;
778 if ((*A)->sample_hits == (*B)->sample_hits)
780 if ((*B)->sample_hits < (*A)->sample_hits)
785 typedef struct _UnmanagedSymbol UnmanagedSymbol;
786 struct _UnmanagedSymbol {
787 UnmanagedSymbol *parent;
792 uintptr_t sample_hits;
795 static UnmanagedSymbol **usymbols = NULL;
796 static int usymbols_size = 0;
797 static int usymbols_num = 0;
800 compare_usymbol_addr (const void *a, const void *b)
802 UnmanagedSymbol *const *A = (UnmanagedSymbol *const *)a;
803 UnmanagedSymbol *const *B = (UnmanagedSymbol *const *)b;
804 if ((*B)->addr == (*A)->addr)
806 if ((*B)->addr > (*A)->addr)
812 compare_usymbol_samples (const void *a, const void *b)
814 UnmanagedSymbol *const *A = (UnmanagedSymbol *const *)a;
815 UnmanagedSymbol *const *B = (UnmanagedSymbol *const *)b;
816 if ((*B)->sample_hits == (*A)->sample_hits)
818 if ((*B)->sample_hits < (*A)->sample_hits)
824 add_unmanaged_symbol (uintptr_t addr, char *name, uintptr_t size)
826 UnmanagedSymbol *sym;
827 if (usymbols_num == usymbols_size) {
828 int new_size = usymbols_size * 2;
831 usymbols = (UnmanagedSymbol **)realloc (usymbols, sizeof (void*) * new_size);
832 usymbols_size = new_size;
834 sym = (UnmanagedSymbol *)calloc (sizeof (UnmanagedSymbol), 1);
838 usymbols [usymbols_num++] = sym;
841 /* only valid after the symbols are sorted */
842 static UnmanagedSymbol*
843 lookup_unmanaged_symbol (uintptr_t addr)
845 int r = usymbols_num - 1;
847 UnmanagedSymbol *sym;
852 if (addr == sym->addr)
854 if (addr < sym->addr) {
856 } else if (addr > sym->addr) {
861 if (last_best >= 0 && (addr - usymbols [last_best]->addr) < 4096)
862 return usymbols [last_best];
866 /* we use the same structure for binaries */
867 static UnmanagedSymbol **ubinaries = NULL;
868 static int ubinaries_size = 0;
869 static int ubinaries_num = 0;
872 add_unmanaged_binary (uintptr_t addr, char *name, uintptr_t size)
874 UnmanagedSymbol *sym;
875 if (ubinaries_num == ubinaries_size) {
876 int new_size = ubinaries_size * 2;
879 ubinaries = (UnmanagedSymbol **)realloc (ubinaries, sizeof (void*) * new_size);
880 ubinaries_size = new_size;
882 sym = (UnmanagedSymbol *)calloc (sizeof (UnmanagedSymbol), 1);
887 ubinaries [ubinaries_num++] = sym;
890 static UnmanagedSymbol*
891 lookup_unmanaged_binary (uintptr_t addr)
894 for (i = 0; i < ubinaries_num; ++i) {
895 UnmanagedSymbol *ubin = ubinaries [i];
896 if (addr >= ubin->addr && addr < ubin->addr + ubin->size) {
904 sample_type_name (int type)
907 case SAMPLE_CYCLES: return "cycles";
908 case SAMPLE_INSTRUCTIONS: return "instructions retired";
909 case SAMPLE_CACHE_MISSES: return "cache misses";
910 case SAMPLE_CACHE_REFS: return "cache references";
911 case SAMPLE_BRANCHES: return "executed branches";
912 case SAMPLE_BRANCH_MISSES: return "unpredicted branches";
918 set_usym_parent (UnmanagedSymbol** cachedus, int count)
921 for (i = 0; i < count; ++i) {
922 UnmanagedSymbol *ubin = lookup_unmanaged_binary (cachedus [i]->addr);
923 if (ubin == cachedus [i])
925 cachedus [i]->parent = ubin;
930 print_usym (UnmanagedSymbol* um)
933 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);
935 fprintf (outfile, "\t%6zd %6.2f %s\n", um->sample_hits, um->sample_hits*100.0/num_stat_samples, um->name);
939 sym_percent (uintptr_t sample_hits)
944 pc = sample_hits*100.0/num_stat_samples;
952 int count = 0, msize = 0;
953 int unmanaged_hits = 0;
954 int unresolved_hits = 0;
955 MethodDesc** cachedm = NULL;
956 int ucount = 0, usize = 0;
957 UnmanagedSymbol** cachedus = NULL;
958 if (!num_stat_samples)
960 qsort (usymbols, usymbols_num, sizeof (UnmanagedSymbol*), compare_usymbol_addr);
961 for (i = 0; i < num_stat_samples; ++i) {
962 MethodDesc *m = lookup_method_by_ip (stat_samples [i]);
964 if (!m->sample_hits) {
965 if (count == msize) {
969 cachedm = (MethodDesc **)realloc (cachedm, sizeof (void*) * msize);
971 cachedm [count++] = m;
975 UnmanagedSymbol *usym = lookup_unmanaged_symbol (stat_samples [i]);
978 //printf ("unmanaged hit at %p\n", (void*)stat_samples [i]);
979 usym = lookup_unmanaged_binary (stat_samples [i]);
982 if (!usym->sample_hits) {
983 if (ucount == usize) {
987 cachedus = (UnmanagedSymbol **)realloc (cachedus, sizeof (void*) * usize);
989 cachedus [ucount++] = usym;
996 qsort (cachedm, count, sizeof (MethodDesc*), compare_method_samples);
997 qsort (cachedus, ucount, sizeof (UnmanagedSymbol*), compare_usymbol_samples);
998 set_usym_parent (cachedus, ucount);
999 fprintf (outfile, "\nStatistical samples summary\n");
1000 fprintf (outfile, "\tSample type: %s\n", sample_type_name (stat_sample_desc [0]));
1001 fprintf (outfile, "\tUnmanaged hits: %6d (%4.1f%%)\n", unmanaged_hits, (100.0*unmanaged_hits)/num_stat_samples);
1002 fprintf (outfile, "\tManaged hits: %6d (%4.1f%%)\n", num_stat_samples - unmanaged_hits, (100.0*(num_stat_samples-unmanaged_hits))/num_stat_samples);
1003 fprintf (outfile, "\tUnresolved hits: %6d (%4.1f%%)\n", unresolved_hits, (100.0*unresolved_hits)/num_stat_samples);
1004 fprintf (outfile, "\t%6s %6s %s\n", "Hits", "%", "Method name");
1007 while (i < count || u < ucount) {
1009 MethodDesc *m = cachedm [i];
1011 UnmanagedSymbol *um = cachedus [u];
1012 if (um->sample_hits > m->sample_hits) {
1013 if (!sym_percent (um->sample_hits))
1020 if (!sym_percent (m->sample_hits))
1022 fprintf (outfile, "\t%6d %6.2f %s\n", m->sample_hits, m->sample_hits*100.0/num_stat_samples, m->name);
1027 UnmanagedSymbol *um = cachedus [u];
1028 if (!sym_percent (um->sample_hits))
1037 typedef struct _HeapClassDesc HeapClassDesc;
1039 HeapClassDesc *klass;
1043 struct _HeapClassDesc {
1047 HeapClassRevRef *rev_hash;
1050 uintptr_t pinned_references;
1051 uintptr_t root_references;
1055 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
1058 uintptr_t start_pos;
1059 start_pos = (hklass->klass->klass >> 2) % size;
1060 assert (start_pos < size);
1063 if (rev_hash [i].klass == hklass) {
1064 rev_hash [i].count += value;
1066 } else if (!rev_hash [i].klass) {
1067 rev_hash [i].klass = hklass;
1068 rev_hash [i].count += value;
1070 for (i = 0; i < size; ++i)
1071 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
1073 assert (start_pos == 1);
1079 } while (i != start_pos);
1080 /* should not happen */
1081 printf ("failed revref store\n");
1086 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
1089 if (to->rev_count * 2 >= to->rev_hash_size) {
1091 uintptr_t old_size = to->rev_hash_size;
1092 to->rev_hash_size *= 2;
1093 if (to->rev_hash_size == 0)
1094 to->rev_hash_size = 4;
1095 n = (HeapClassRevRef *)calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
1096 for (i = 0; i < old_size; ++i) {
1097 if (to->rev_hash [i].klass)
1098 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
1101 free (to->rev_hash);
1104 to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
1109 HeapClassDesc *hklass;
1114 typedef struct _HeapShot HeapShot;
1120 HeapClassDesc **class_hash;
1121 HeapClassDesc **sorted;
1122 HeapObjectDesc **objects_hash;
1123 uintptr_t objects_count;
1124 uintptr_t objects_hash_size;
1125 uintptr_t num_roots;
1127 uintptr_t *roots_extra;
1131 static HeapShot *heap_shots = NULL;
1132 static int num_heap_shots = 0;
1135 new_heap_shot (uint64_t timestamp)
1137 HeapShot *hs = (HeapShot *)calloc (sizeof (HeapShot), 1);
1139 hs->class_hash = (HeapClassDesc **)calloc (sizeof (void*), hs->hash_size);
1140 hs->timestamp = timestamp;
1142 hs->next = heap_shots;
1147 static HeapClassDesc*
1148 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
1151 unsigned int start_pos;
1152 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
1155 HeapClassDesc* cd = hs->class_hash [i];
1158 if (cd->klass == klass)
1161 if (++i == hs->hash_size)
1163 } while (i != start_pos);
1168 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
1171 uintptr_t start_pos;
1172 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
1175 if (hash [i] && hash [i]->klass == klass) {
1176 hash [i]->total_size += size;
1177 hash [i]->count += count;
1180 } else if (!hash [i]) {
1185 hash [i] = (HeapClassDesc *)calloc (sizeof (HeapClassDesc), 1);
1186 hash [i]->klass = klass;
1187 hash [i]->total_size += size;
1188 hash [i]->count += count;
1195 } while (i != start_pos);
1196 /* should not happen */
1197 printf ("failed heap class store\n");
1201 static HeapClassDesc*
1202 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
1206 if (hs->class_count * 2 >= hs->hash_size) {
1208 int old_size = hs->hash_size;
1210 if (hs->hash_size == 0)
1212 n = (HeapClassDesc **)calloc (sizeof (void*) * hs->hash_size, 1);
1213 for (i = 0; i < old_size; ++i) {
1214 res = hs->class_hash [i];
1215 if (hs->class_hash [i])
1216 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
1219 free (hs->class_hash);
1223 hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
1224 //if (res->count == 1)
1225 // printf ("added heap class: %s\n", res->klass->name);
1229 static HeapObjectDesc*
1230 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
1232 HeapObjectDesc* ho = (HeapObjectDesc *)calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
1233 ho->objaddr = objaddr;
1234 ho->hklass = hklass;
1235 ho->num_refs = num_refs;
1240 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
1243 uintptr_t start_pos;
1244 HeapObjectDesc **hash = hs->objects_hash;
1245 start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
1248 if (hash [i] && hash [i]->objaddr == objaddr) {
1250 } else if (!hash [i]) {
1254 if (++i == hs->objects_hash_size)
1256 } while (i != start_pos);
1257 /* should not happen */
1258 //printf ("failed heap obj slot\n");
1262 static HeapObjectDesc*
1263 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
1265 HeapObjectDesc **hash = hs->objects_hash;
1266 uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
1268 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
1269 *ref_offset = hash [i]->num_refs;
1270 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
1275 /* should not happen */
1276 printf ("failed heap obj update\n");
1282 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
1285 uintptr_t start_pos;
1286 start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
1289 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
1290 printf ("duplicate object!\n");
1292 } else if (!hash [i]) {
1299 } while (i != start_pos);
1300 /* should not happen */
1301 printf ("failed heap obj store\n");
1306 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
1309 if (hs->objects_count * 2 >= hs->objects_hash_size) {
1311 uintptr_t old_size = hs->objects_hash_size;
1312 hs->objects_hash_size *= 2;
1313 if (hs->objects_hash_size == 0)
1314 hs->objects_hash_size = 4;
1315 n = (HeapObjectDesc **)calloc (sizeof (void*) * hs->objects_hash_size, 1);
1316 for (i = 0; i < old_size; ++i) {
1317 if (hs->objects_hash [i])
1318 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
1320 if (hs->objects_hash)
1321 free (hs->objects_hash);
1322 hs->objects_hash = n;
1324 hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
1328 heap_shot_resolve_reverse_refs (HeapShot *hs)
1331 for (i = 0; i < hs->objects_hash_size; ++i) {
1333 HeapObjectDesc *ho = hs->objects_hash [i];
1336 for (r = 0; r < ho->num_refs; ++r) {
1337 uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
1338 add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
1344 #define MARK_BLACK 2
1347 heap_shot_mark_objects (HeapShot *hs)
1350 unsigned char *marks;
1351 HeapObjectDesc *obj, *ref;
1353 uintptr_t num_marked = 0, num_unmarked;
1354 for (i = 0; i < hs->num_roots; ++i) {
1356 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
1360 obj = hs->objects_hash [oi];
1362 if (hs->roots_types [i] & MONO_PROFILE_GC_ROOT_PINNING)
1363 cd->pinned_references++;
1364 cd->root_references++;
1368 /* consistency checks: it seems not all the objects are walked in the heap in some cases */
1369 marks = (unsigned char *)calloc (hs->objects_hash_size, 1);
1372 for (i = 0; i < hs->num_roots; ++i) {
1373 oi = heap_shot_find_obj_slot (hs, hs->roots [i]);
1375 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);
1378 obj = hs->objects_hash [oi];
1380 marks [oi] = obj->num_refs? MARK_GRAY: MARK_BLACK;
1385 while (marked_some) {
1387 for (i = 0; i < hs->objects_hash_size; ++i) {
1388 if (marks [i] != MARK_GRAY)
1390 marks [i] = MARK_BLACK;
1391 obj = hs->objects_hash [i];
1392 for (r = 0; r < obj->num_refs; ++r) {
1393 oi = heap_shot_find_obj_slot (hs, obj->refs [r]);
1395 fprintf (outfile, "referenced obj %p not found in heap\n", (void*)obj->refs [r]);
1398 ref = hs->objects_hash [oi];
1400 marks [oi] = ref->num_refs? MARK_GRAY: MARK_BLACK;
1408 for (i = 0; i < hs->objects_hash_size; ++i) {
1409 if (hs->objects_hash [i] && !marks [i]) {
1411 fprintf (outfile, "object %p (%s) unmarked\n", (void*)hs->objects_hash [i], hs->objects_hash [i]->hklass->klass->name);
1414 fprintf (outfile, "Total unmarked: %zd/%zd\n", num_unmarked, hs->objects_count);
1419 heap_shot_free_objects (HeapShot *hs)
1422 for (i = 0; i < hs->objects_hash_size; ++i) {
1423 HeapObjectDesc *ho = hs->objects_hash [i];
1427 if (hs->objects_hash)
1428 free (hs->objects_hash);
1429 hs->objects_hash = NULL;
1430 hs->objects_hash_size = 0;
1431 hs->objects_count = 0;
1440 MethodDesc *methods [1];
1443 static BackTrace *backtrace_hash [HASH_SIZE];
1444 static BackTrace **backtraces = NULL;
1445 static int num_backtraces = 0;
1446 static int next_backtrace = 0;
1449 hash_backtrace (int count, MethodDesc **methods)
1453 for (i = 0; i < count; ++i) {
1454 hash = (hash << 5) - hash + methods [i]->method;
1460 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
1463 if (bt->count != count)
1465 for (i = 0; i < count; ++i)
1466 if (methods [i] != bt->methods [i])
1472 add_backtrace (int count, MethodDesc **methods)
1474 int hash = hash_backtrace (count, methods);
1475 int slot = (hash & 0xffff) % HASH_SIZE;
1476 BackTrace *bt = backtrace_hash [slot];
1478 if (bt->hash == hash && compare_backtrace (bt, count, methods))
1482 bt = (BackTrace *)malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
1483 bt->next = backtrace_hash [slot];
1484 backtrace_hash [slot] = bt;
1485 if (next_backtrace == num_backtraces) {
1486 num_backtraces *= 2;
1487 if (!num_backtraces)
1488 num_backtraces = 16;
1489 backtraces = (BackTrace **)realloc (backtraces, sizeof (void*) * num_backtraces);
1491 bt->id = next_backtrace++;
1492 backtraces [bt->id] = bt;
1495 for (slot = 0; slot < count; ++slot)
1496 bt->methods [slot] = methods [slot];
1501 typedef struct _MonitorDesc MonitorDesc;
1502 typedef struct _ThreadContext ThreadContext;
1503 typedef struct _DomainContext DomainContext;
1504 typedef struct _RemCtxContext RemCtxContext;
1508 #if defined (HAVE_SYS_ZLIB)
1519 uint64_t startup_time;
1520 ThreadContext *threads;
1521 ThreadContext *current_thread;
1522 DomainContext *domains;
1523 DomainContext *current_domain;
1524 RemCtxContext *remctxs;
1525 RemCtxContext *current_remctx;
1528 struct _ThreadContext {
1529 ThreadContext *next;
1532 /* emulated stack */
1534 uint64_t *time_stack;
1535 uint64_t *callee_time_stack;
1537 uint64_t contention_start;
1538 MonitorDesc *monitor;
1541 HeapShot *current_heap_shot;
1542 uintptr_t num_roots;
1543 uintptr_t size_roots;
1545 uintptr_t *roots_extra;
1547 uint64_t gc_start_times [3];
1550 struct _DomainContext {
1551 DomainContext *next;
1553 const char *friendly_name;
1556 struct _RemCtxContext {
1557 RemCtxContext *next;
1563 ensure_buffer (ProfContext *ctx, int size)
1565 if (ctx->size < size) {
1566 ctx->buf = (unsigned char *)realloc (ctx->buf, size);
1572 load_data (ProfContext *ctx, int size)
1574 ensure_buffer (ctx, size);
1575 #if defined (HAVE_SYS_ZLIB)
1577 int r = gzread (ctx->gzfile, ctx->buf, size);
1579 return size == 0? 1: 0;
1584 int r = fread (ctx->buf, size, 1, ctx->file);
1586 return size == 0? 1: 0;
1591 static ThreadContext*
1592 get_thread (ProfContext *ctx, intptr_t thread_id)
1594 ThreadContext *thread;
1595 if (ctx->current_thread && ctx->current_thread->thread_id == thread_id)
1596 return ctx->current_thread;
1597 thread = ctx->threads;
1599 if (thread->thread_id == thread_id) {
1602 thread = thread->next;
1604 thread = (ThreadContext *)calloc (sizeof (ThreadContext), 1);
1605 thread->next = ctx->threads;
1606 ctx->threads = thread;
1607 thread->thread_id = thread_id;
1608 thread->last_time = 0;
1609 thread->stack_id = 0;
1610 thread->stack_size = 32;
1611 thread->stack = (MethodDesc **)malloc (thread->stack_size * sizeof (void*));
1612 thread->time_stack = (uint64_t *)malloc (thread->stack_size * sizeof (uint64_t));
1613 thread->callee_time_stack = (uint64_t *)malloc (thread->stack_size * sizeof (uint64_t));
1617 static DomainContext *
1618 get_domain (ProfContext *ctx, intptr_t domain_id)
1620 if (ctx->current_domain && ctx->current_domain->domain_id == domain_id)
1621 return ctx->current_domain;
1623 DomainContext *domain = ctx->domains;
1626 if (domain->domain_id == domain_id)
1629 domain = domain->next;
1632 domain = (DomainContext *)calloc (sizeof (DomainContext), 1);
1633 domain->next = ctx->domains;
1634 ctx->domains = domain;
1635 domain->domain_id = domain_id;
1640 static RemCtxContext *
1641 get_remctx (ProfContext *ctx, intptr_t remctx_id)
1643 if (ctx->current_remctx && ctx->current_remctx->remctx_id == remctx_id)
1644 return ctx->current_remctx;
1646 RemCtxContext *remctx = ctx->remctxs;
1649 if (remctx->remctx_id == remctx_id)
1652 remctx = remctx->next;
1655 remctx = (RemCtxContext *)calloc (sizeof (RemCtxContext), 1);
1656 remctx->next = ctx->remctxs;
1657 ctx->remctxs = remctx;
1658 remctx->remctx_id = remctx_id;
1663 static ThreadContext*
1664 load_thread (ProfContext *ctx, intptr_t thread_id)
1666 ThreadContext *thread = get_thread (ctx, thread_id);
1667 ctx->current_thread = thread;
1672 ensure_thread_stack (ThreadContext *thread)
1674 if (thread->stack_id == thread->stack_size) {
1675 thread->stack_size *= 2;
1676 thread->stack = (MethodDesc **)realloc (thread->stack, thread->stack_size * sizeof (void*));
1677 thread->time_stack = (uint64_t *)realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
1678 thread->callee_time_stack = (uint64_t *)realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
1683 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
1686 unsigned int start_pos;
1687 start_pos = bt->hash % size;
1690 if (traces [i].bt == bt) {
1691 traces [i].count += value;
1693 } else if (!traces [i].bt) {
1695 traces [i].count += value;
1701 } while (i != start_pos);
1702 /* should not happen */
1703 printf ("failed trace store\n");
1708 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
1711 if (!collect_traces)
1713 if (trace->count * 2 >= trace->size) {
1715 int old_size = trace->size;
1717 if (trace->size == 0)
1719 n = (CallContext *)calloc (sizeof (CallContext) * trace->size, 1);
1720 for (i = 0; i < old_size; ++i) {
1721 if (trace->traces [i].bt)
1722 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
1725 free (trace->traces);
1728 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
1732 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
1735 int count = thread->stack_id;
1736 if (!collect_traces)
1738 if (count > trace_max)
1740 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
1741 add_trace_bt (bt, trace, value);
1746 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
1749 if (!collect_traces)
1751 if (count > trace_max)
1753 bt = add_backtrace (count, methods);
1754 add_trace_bt (bt, trace, value);
1759 thread_add_root (ThreadContext *ctx, uintptr_t obj, int root_type, uintptr_t extra_info)
1761 if (ctx->num_roots == ctx->size_roots) {
1762 int new_size = ctx->size_roots * 2;
1765 ctx->roots = (uintptr_t *)realloc (ctx->roots, new_size * sizeof (uintptr_t));
1766 ctx->roots_extra = (uintptr_t *)realloc (ctx->roots_extra, new_size * sizeof (uintptr_t));
1767 ctx->roots_types = (int *)realloc (ctx->roots_types, new_size * sizeof (int));
1768 ctx->size_roots = new_size;
1770 ctx->roots_types [ctx->num_roots] = root_type;
1771 ctx->roots_extra [ctx->num_roots] = extra_info;
1772 ctx->roots [ctx->num_roots++] = obj;
1776 compare_callc (const void *a, const void *b)
1778 const CallContext *A = (const CallContext *)a;
1779 const CallContext *B = (const CallContext *)b;
1780 if (B->count == A->count)
1782 if (B->count < A->count)
1788 sort_context_array (TraceDesc* traces)
1791 for (i = 0, j = 0; i < traces->size; ++i) {
1792 if (traces->traces [i].bt) {
1793 traces->traces [j].bt = traces->traces [i].bt;
1794 traces->traces [j].count = traces->traces [i].count;
1798 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
1802 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1804 ensure_thread_stack (thread);
1805 thread->time_stack [thread->stack_id] = timestamp;
1806 thread->callee_time_stack [thread->stack_id] = 0;
1807 thread->stack [thread->stack_id++] = method;
1808 method->recurse_count++;
1812 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
1814 method->recurse_count--;
1815 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
1819 if (timestamp < thread->time_stack [thread->stack_id])
1820 fprintf (outfile, "time went backwards for %s\n", method->name);
1821 tdiff = timestamp - thread->time_stack [thread->stack_id];
1822 if (thread->callee_time_stack [thread->stack_id] > tdiff)
1823 fprintf (outfile, "callee time bigger for %s\n", method->name);
1824 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
1825 method->callee_time += thread->callee_time_stack [thread->stack_id];
1826 if (thread->stack_id)
1827 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
1828 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
1830 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
1835 uint64_t total_time;
1839 static GCDesc gc_info [3];
1840 static uint64_t max_heap_size;
1841 static uint64_t gc_object_moves;
1842 static int gc_resizes;
1849 TraceDesc destroy_traces;
1851 static HandleInfo handle_info [4];
1854 gc_event_name (int ev)
1857 case MONO_GC_EVENT_START: return "start";
1858 case MONO_GC_EVENT_MARK_START: return "mark start";
1859 case MONO_GC_EVENT_MARK_END: return "mark end";
1860 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
1861 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
1862 case MONO_GC_EVENT_END: return "end";
1863 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
1864 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
1865 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
1866 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
1872 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
1873 static uint64_t throw_count = 0;
1874 static TraceDesc exc_traces;
1877 clause_name (int type)
1880 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
1881 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
1882 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
1883 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
1884 default: return "invalid";
1888 static uint64_t monitor_contention;
1889 static uint64_t monitor_failed;
1890 static uint64_t monitor_acquired;
1892 struct _MonitorDesc {
1895 uintptr_t contentions;
1897 uint64_t max_wait_time;
1901 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
1902 static int num_monitors = 0;
1905 lookup_monitor (uintptr_t objid)
1907 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
1908 MonitorDesc *cd = monitor_hash [slot];
1909 while (cd && cd->objid != objid)
1912 cd = (MonitorDesc *)calloc (sizeof (MonitorDesc), 1);
1914 cd->next = monitor_hash [slot];
1915 monitor_hash [slot] = cd;
1922 monitor_ev_name (int ev)
1925 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
1926 case MONO_PROFILER_MONITOR_DONE: return "acquired";
1927 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
1928 default: return "invalid";
1933 get_handle_name (int htype)
1936 case 0: return "weak";
1937 case 1: return "weaktrack";
1938 case 2: return "normal";
1939 case 3: return "pinned";
1940 default: return "unknown";
1945 get_root_name (int rtype)
1947 switch (rtype & MONO_PROFILE_GC_ROOT_TYPEMASK) {
1948 case MONO_PROFILE_GC_ROOT_STACK: return "stack";
1949 case MONO_PROFILE_GC_ROOT_FINALIZER: return "finalizer";
1950 case MONO_PROFILE_GC_ROOT_HANDLE: return "handle";
1951 case MONO_PROFILE_GC_ROOT_OTHER: return "other";
1952 case MONO_PROFILE_GC_ROOT_MISC: return "misc";
1953 default: return "unknown";
1958 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
1960 MethodDesc **frames;
1962 int flags = decode_uleb128 (p, &p);
1963 int count = decode_uleb128 (p, &p);
1967 frames = (MethodDesc **)malloc (count * sizeof (void*));
1970 for (i = 0; i < count; ++i) {
1971 intptr_t ptrdiff = decode_sleb128 (p, &p);
1972 frames [i] = lookup_method (ptr_base + ptrdiff);
1980 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
1983 for (i = 0; i < num_tracked_objects; ++i) {
1984 if (tracked_objects [i] != obj)
1986 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);
1987 if (bt && bt->count) {
1989 for (k = 0; k < bt->count; ++k)
1990 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
1996 track_handle (uintptr_t obj, int htype, uint32_t handle, BackTrace *bt, uint64_t timestamp)
1999 for (i = 0; i < num_tracked_objects; ++i) {
2000 if (tracked_objects [i] != obj)
2002 fprintf (outfile, "Object %p referenced from handle %u at %.3f secs.\n", (void*)obj, handle, (timestamp - startup_time) / 1000000000.0);
2003 if (bt && bt->count) {
2005 for (k = 0; k < bt->count; ++k)
2006 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
2012 track_move (uintptr_t src, uintptr_t dst)
2015 for (i = 0; i < num_tracked_objects; ++i) {
2016 if (tracked_objects [i] == src)
2017 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
2018 else if (tracked_objects [i] == dst)
2019 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
2024 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
2027 for (i = 0; i < num_tracked_objects; ++i) {
2028 if (tracked_objects [i] == obj)
2029 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
2034 found_object (uintptr_t obj)
2036 num_tracked_objects ++;
2037 tracked_objects = (uintptr_t *)realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
2038 tracked_objects [num_tracked_objects - 1] = obj;
2041 static int num_jit_helpers = 0;
2042 static int jit_helpers_code_size = 0;
2045 code_buffer_desc (int type)
2048 case MONO_PROFILER_CODE_BUFFER_METHOD:
2050 case MONO_PROFILER_CODE_BUFFER_METHOD_TRAMPOLINE:
2051 return "method trampoline";
2052 case MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE:
2053 return "unbox trampoline";
2054 case MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE:
2055 return "imt trampoline";
2056 case MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE:
2057 return "generics trampoline";
2058 case MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE:
2059 return "specific trampoline";
2060 case MONO_PROFILER_CODE_BUFFER_HELPER:
2061 return "misc helper";
2062 case MONO_PROFILER_CODE_BUFFER_MONITOR:
2063 return "monitor/lock";
2064 case MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE:
2065 return "delegate invoke";
2066 case MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING:
2067 return "exception handling";
2069 return "unspecified";
2073 typedef struct _CoverageAssembly CoverageAssembly;
2074 struct _CoverageAssembly {
2078 int number_of_methods;
2080 int partially_covered;
2083 typedef struct _CoverageClass CoverageClass;
2084 struct _CoverageClass {
2085 char *assembly_name;
2087 int number_of_methods;
2089 int partially_covered;
2092 typedef struct _CoverageCoverage CoverageCoverage;
2093 struct _CoverageCoverage {
2101 typedef struct _CoverageMethod CoverageMethod;
2102 struct _CoverageMethod {
2103 char *assembly_name;
2106 char *method_signature;
2111 GPtrArray *coverage;
2113 static GPtrArray *coverage_assemblies = NULL;
2114 static GPtrArray *coverage_methods = NULL;
2115 static GPtrArray *coverage_statements = NULL;
2116 static GHashTable *coverage_methods_hash = NULL;
2117 static GPtrArray *coverage_classes = NULL;
2118 static GHashTable *coverage_assembly_classes = NULL;
2121 gather_coverage_statements (void)
2123 for (guint i = 0; i < coverage_statements->len; i++) {
2124 CoverageCoverage *coverage = (CoverageCoverage *)coverage_statements->pdata[i];
2125 CoverageMethod *method = (CoverageMethod *)g_hash_table_lookup (coverage_methods_hash, GINT_TO_POINTER (coverage->method_id));
2126 if (method == NULL) {
2127 fprintf (outfile, "Cannot find method with ID: %d\n", coverage->method_id);
2131 g_ptr_array_add (method->coverage, coverage);
2136 coverage_add_assembly (CoverageAssembly *assembly)
2138 if (coverage_assemblies == NULL)
2139 coverage_assemblies = g_ptr_array_new ();
2141 g_ptr_array_add (coverage_assemblies, assembly);
2145 coverage_add_method (CoverageMethod *method)
2147 if (coverage_methods == NULL) {
2148 coverage_methods = g_ptr_array_new ();
2149 coverage_methods_hash = g_hash_table_new (NULL, NULL);
2152 g_ptr_array_add (coverage_methods, method);
2153 g_hash_table_insert (coverage_methods_hash, GINT_TO_POINTER (method->method_id), method);
2157 coverage_add_class (CoverageClass *klass)
2159 GPtrArray *classes = NULL;
2161 if (coverage_classes == NULL) {
2162 coverage_classes = g_ptr_array_new ();
2163 coverage_assembly_classes = g_hash_table_new (g_str_hash, g_str_equal);
2166 g_ptr_array_add (coverage_classes, klass);
2167 classes = (GPtrArray *)g_hash_table_lookup (coverage_assembly_classes, klass->assembly_name);
2168 if (classes == NULL) {
2169 classes = g_ptr_array_new ();
2170 g_hash_table_insert (coverage_assembly_classes, klass->assembly_name, classes);
2172 g_ptr_array_add (classes, klass);
2176 coverage_add_coverage (CoverageCoverage *coverage)
2178 if (coverage_statements == NULL)
2179 coverage_statements = g_ptr_array_new ();
2181 g_ptr_array_add (coverage_statements, coverage);
2184 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
2185 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
2189 #define BUFFER_HEADER_SIZE 48
2192 int count, min_size, max_size, bytes;
2195 static int buffer_count;
2196 static EventStat stats [256];
2199 record_event_stats (int type, int size)
2201 ++stats [type].count;
2202 if (!stats [type].min_size)
2203 stats [type].min_size = size;
2204 stats [type].min_size = MIN (stats [type].min_size, size);
2205 stats [type].max_size = MAX (stats [type].max_size, size);
2206 stats [type].bytes += size;
2210 decode_buffer (ProfContext *ctx)
2217 intptr_t method_base;
2219 uint64_t file_offset;
2221 ThreadContext *thread;
2223 #ifdef HAVE_SYS_ZLIB
2225 file_offset = gztell (ctx->gzfile);
2228 file_offset = ftell (ctx->file);
2229 if (!load_data (ctx, 48))
2232 if (read_int32 (p) != BUF_ID) {
2233 fprintf (outfile, "Incorrect buffer id: 0x%x\n", read_int32 (p));
2234 for (i = 0; i < 48; ++i) {
2235 fprintf (outfile, "0x%x%s", p [i], i % 8?" ":"\n");
2239 len = read_int32 (p + 4);
2240 time_base = read_int64 (p + 8);
2241 ptr_base = read_int64 (p + 16);
2242 obj_base = read_int64 (p + 24);
2243 thread_id = read_int64 (p + 32);
2244 method_base = read_int64 (p + 40);
2246 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);
2247 thread = load_thread (ctx, thread_id);
2248 if (!load_data (ctx, len))
2253 if (!startup_time) {
2254 startup_time = time_base;
2255 if (use_time_filter) {
2256 time_from += startup_time;
2257 time_to += startup_time;
2260 thread->name = pstrdup ("Main");
2262 for (i = 0; i < thread->stack_id; ++i)
2263 thread->stack [i]->recurse_count++;
2267 unsigned char *start = p;
2268 unsigned char event = *p;
2271 int subtype = *p & 0xf0;
2272 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2273 LOG_TIME (time_base, tdiff);
2275 if (subtype == TYPE_GC_RESIZE) {
2276 uint64_t new_size = decode_uleb128 (p, &p);
2278 fprintf (outfile, "gc heap resized to %llu\n", (unsigned long long) new_size);
2280 if (new_size > max_heap_size)
2281 max_heap_size = new_size;
2282 } else if (subtype == TYPE_GC_EVENT) {
2283 uint64_t ev = decode_uleb128 (p, &p);
2284 int gen = decode_uleb128 (p, &p);
2286 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);
2288 fprintf (outfile, "incorrect gc gen: %d\n", gen);
2291 if (ev == MONO_GC_EVENT_START) {
2292 thread->gc_start_times [gen] = time_base;
2293 gc_info [gen].count++;
2294 } else if (ev == MONO_GC_EVENT_END) {
2295 tdiff = time_base - thread->gc_start_times [gen];
2296 gc_info [gen].total_time += tdiff;
2297 if (tdiff > gc_info [gen].max_time)
2298 gc_info [gen].max_time = tdiff;
2300 } else if (subtype == TYPE_GC_MOVE) {
2301 int j, num = decode_uleb128 (p, &p);
2302 gc_object_moves += num / 2;
2303 for (j = 0; j < num; j += 2) {
2304 intptr_t obj1diff = decode_sleb128 (p, &p);
2305 intptr_t obj2diff = decode_sleb128 (p, &p);
2306 if (num_tracked_objects)
2307 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
2309 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
2312 } else if (subtype == TYPE_GC_HANDLE_CREATED || subtype == TYPE_GC_HANDLE_CREATED_BT) {
2313 int has_bt = subtype == TYPE_GC_HANDLE_CREATED_BT;
2315 MethodDesc *sframes [8];
2316 MethodDesc **frames = sframes;
2317 int htype = decode_uleb128 (p, &p);
2318 uint32_t handle = decode_uleb128 (p, &p);
2319 intptr_t objdiff = decode_sleb128 (p, &p);
2322 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2324 fprintf (outfile, "Cannot load backtrace\n");
2330 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
2331 handle_info [htype].created++;
2332 handle_info [htype].live++;
2333 if (handle_info [htype].live > handle_info [htype].max_live)
2334 handle_info [htype].max_live = handle_info [htype].live;
2337 bt = add_trace_methods (frames, num_bt, &handle_info [htype].traces, 1);
2339 bt = add_trace_thread (thread, &handle_info [htype].traces, 1);
2340 if (num_tracked_objects)
2341 track_handle (OBJ_ADDR (objdiff), htype, handle, bt, time_base);
2344 fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
2345 if (frames != sframes)
2347 } else if (subtype == TYPE_GC_HANDLE_DESTROYED || subtype == TYPE_GC_HANDLE_DESTROYED_BT) {
2348 int has_bt = subtype == TYPE_GC_HANDLE_DESTROYED_BT;
2350 MethodDesc *sframes [8];
2351 MethodDesc **frames = sframes;
2352 int htype = decode_uleb128 (p, &p);
2353 uint32_t handle = decode_uleb128 (p, &p);
2356 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2358 fprintf (outfile, "Cannot load backtrace\n");
2364 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
2365 handle_info [htype].destroyed ++;
2366 handle_info [htype].live--;
2369 bt = add_trace_methods (frames, num_bt, &handle_info [htype].destroy_traces, 1);
2371 bt = add_trace_thread (thread, &handle_info [htype].destroy_traces, 1);
2372 /* TODO: track_handle_free () - would need to record and keep track of the associated object address... */
2375 fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
2376 if (frames != sframes)
2381 case TYPE_METADATA: {
2382 int subtype = *p & 0xf0;
2383 const char *load_str = subtype == TYPE_END_LOAD ? "loaded" : "unloaded";
2384 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2386 intptr_t ptrdiff = decode_sleb128 (p, &p);
2387 LOG_TIME (time_base, tdiff);
2389 if (mtype == TYPE_CLASS) {
2390 intptr_t imptrdiff = decode_sleb128 (p, &p);
2391 uint64_t flags = decode_uleb128 (p, &p);
2393 fprintf (outfile, "non-zero flags in class\n");
2397 fprintf (outfile, "%s class %p (%s in %p) at %llu\n", load_str, (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), (unsigned long long) time_base);
2398 if (subtype == TYPE_END_LOAD)
2399 add_class (ptr_base + ptrdiff, (char*)p);
2402 } else if (mtype == TYPE_IMAGE) {
2403 uint64_t flags = decode_uleb128 (p, &p);
2405 fprintf (outfile, "non-zero flags in image\n");
2409 fprintf (outfile, "%s image %p (%s) at %llu\n", load_str, (void*)(ptr_base + ptrdiff), p, (unsigned long long) time_base);
2410 if (subtype == TYPE_END_LOAD)
2411 add_image (ptr_base + ptrdiff, (char*)p);
2414 } else if (mtype == TYPE_ASSEMBLY) {
2415 uint64_t flags = decode_uleb128 (p, &p);
2417 fprintf (outfile, "non-zero flags in assembly\n");
2421 fprintf (outfile, "%s assembly %p (%s) at %llu\n", load_str, (void*)(ptr_base + ptrdiff), p, (unsigned long long) time_base);
2422 if (subtype == TYPE_END_LOAD)
2423 add_assembly (ptr_base + ptrdiff, (char*)p);
2426 } else if (mtype == TYPE_DOMAIN) {
2427 uint64_t flags = decode_uleb128 (p, &p);
2429 fprintf (outfile, "non-zero flags in domain\n");
2432 DomainContext *nd = get_domain (ctx, ptr_base + ptrdiff);
2433 /* no subtype means it's a name event, rather than start/stop */
2435 nd->friendly_name = pstrdup ((char *) p);
2438 fprintf (outfile, "domain %p named at %llu: %s\n", (void *) (ptr_base + ptrdiff), (unsigned long long) time_base, p);
2440 fprintf (outfile, "%s thread %p at %llu\n", load_str, (void *) (ptr_base + ptrdiff), (unsigned long long) time_base);
2446 } else if (mtype == TYPE_CONTEXT) {
2447 uint64_t flags = decode_uleb128 (p, &p);
2449 fprintf (outfile, "non-zero flags in context\n");
2452 intptr_t domaindiff = decode_sleb128 (p, &p);
2454 fprintf (outfile, "%s context %p (%p) at %llu\n", load_str, (void*)(ptr_base + ptrdiff), (void *) (ptr_base + domaindiff), (unsigned long long) time_base);
2455 if (subtype == TYPE_END_LOAD)
2456 get_remctx (ctx, ptr_base + ptrdiff)->domain_id = ptr_base + domaindiff;
2457 } else if (mtype == TYPE_THREAD) {
2458 uint64_t flags = decode_uleb128 (p, &p);
2460 fprintf (outfile, "non-zero flags in thread\n");
2463 ThreadContext *nt = get_thread (ctx, ptr_base + ptrdiff);
2464 /* no subtype means it's a name event, rather than start/stop */
2466 nt->name = pstrdup ((char*)p);
2469 fprintf (outfile, "thread %p named at %llu: %s\n", (void*)(ptr_base + ptrdiff), (unsigned long long) time_base, p);
2471 fprintf (outfile, "%s thread %p at %llu\n", load_str, (void *) (ptr_base + ptrdiff), (unsigned long long) time_base);
2481 int has_bt = *p & TYPE_ALLOC_BT;
2482 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2483 intptr_t ptrdiff = decode_sleb128 (p, &p);
2484 intptr_t objdiff = decode_sleb128 (p, &p);
2487 MethodDesc* sframes [8];
2488 MethodDesc** frames = sframes;
2489 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
2490 len = decode_uleb128 (p, &p);
2491 LOG_TIME (time_base, tdiff);
2494 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);
2497 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2499 fprintf (outfile, "Cannot load backtrace\n");
2503 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
2506 cd->alloc_size += len;
2508 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
2510 bt = add_trace_thread (thread, &cd->traces, len);
2511 if (find_size && len >= find_size) {
2512 if (!find_name || strstr (cd->name, find_name))
2513 found_object (OBJ_ADDR (objdiff));
2514 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
2515 found_object (OBJ_ADDR (objdiff));
2517 if (num_tracked_objects)
2518 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
2520 if (frames != sframes)
2525 int subtype = *p & 0xf0;
2526 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2527 int64_t ptrdiff = decode_sleb128 (p, &p);
2528 LOG_TIME (time_base, tdiff);
2530 method_base += ptrdiff;
2531 if (subtype == TYPE_JIT) {
2532 intptr_t codediff = decode_sleb128 (p, &p);
2533 int codelen = decode_uleb128 (p, &p);
2534 MethodDesc *jitted_method;
2536 fprintf (outfile, "jitted method %p (%s), size: %d, code: %p\n", (void*)(method_base), p, codelen, (void*)(ptr_base + codediff));
2537 jitted_method = add_method (method_base, (char*)p, ptr_base + codediff, codelen);
2538 if (!(time_base >= time_from && time_base < time_to))
2539 jitted_method->ignore_jit = 1;
2544 if ((thread_filter && thread_filter != thread->thread_id))
2546 if (!(time_base >= time_from && time_base < time_to))
2548 method = lookup_method (method_base);
2549 if (subtype == TYPE_ENTER) {
2550 add_trace_thread (thread, &method->traces, 1);
2551 push_method (thread, method, time_base);
2553 pop_method (thread, method, time_base);
2556 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
2561 int subtype = *p & 0xf0;
2562 if (subtype == TYPE_HEAP_OBJECT) {
2563 HeapObjectDesc *ho = NULL;
2565 intptr_t objdiff = decode_sleb128 (p + 1, &p);
2566 intptr_t ptrdiff = decode_sleb128 (p, &p);
2567 uint64_t size = decode_uleb128 (p, &p);
2568 uintptr_t num = decode_uleb128 (p, &p);
2569 uintptr_t ref_offset = 0;
2570 uintptr_t last_obj_offset = 0;
2571 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
2573 HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
2574 if (collect_traces) {
2575 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
2576 add_heap_shot_obj (thread->current_heap_shot, ho);
2581 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
2583 for (i = 0; i < num; ++i) {
2584 /* FIXME: use object distance to measure how good
2585 * the GC is at keeping related objects close
2587 uintptr_t offset = ctx->data_version > 1? last_obj_offset + decode_uleb128 (p, &p): -1;
2588 intptr_t obj1diff = decode_sleb128 (p, &p);
2589 last_obj_offset = offset;
2591 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
2592 if (num_tracked_objects)
2593 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
2596 fprintf (outfile, "traced object %p, size %llu (%s), refs: %zd\n", (void*)OBJ_ADDR (objdiff), (unsigned long long) size, cd->name, num);
2597 } else if (subtype == TYPE_HEAP_ROOT) {
2598 uintptr_t num = decode_uleb128 (p + 1, &p);
2599 uintptr_t gc_num G_GNUC_UNUSED = decode_uleb128 (p, &p);
2601 for (i = 0; i < num; ++i) {
2602 intptr_t objdiff = decode_sleb128 (p, &p);
2603 int root_type = decode_uleb128 (p, &p);
2604 /* we just discard the extra info for now */
2605 uintptr_t extra_info = decode_uleb128 (p, &p);
2607 fprintf (outfile, "object %p is a %s root\n", (void*)OBJ_ADDR (objdiff), get_root_name (root_type));
2609 thread_add_root (thread, OBJ_ADDR (objdiff), root_type, extra_info);
2611 } else if (subtype == TYPE_HEAP_END) {
2612 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2613 LOG_TIME (time_base, tdiff);
2616 fprintf (outfile, "heap shot end\n");
2617 if (collect_traces) {
2618 HeapShot *hs = thread->current_heap_shot;
2619 if (hs && thread->num_roots) {
2620 /* transfer the root ownershipt to the heapshot */
2621 hs->num_roots = thread->num_roots;
2622 hs->roots = thread->roots;
2623 hs->roots_extra = thread->roots_extra;
2624 hs->roots_types = thread->roots_types;
2626 free (thread->roots);
2627 free (thread->roots_extra);
2628 free (thread->roots_types);
2630 thread->num_roots = 0;
2631 thread->size_roots = 0;
2632 thread->roots = NULL;
2633 thread->roots_extra = NULL;
2634 thread->roots_types = NULL;
2635 heap_shot_resolve_reverse_refs (hs);
2636 heap_shot_mark_objects (hs);
2637 heap_shot_free_objects (hs);
2639 thread->current_heap_shot = NULL;
2640 } else if (subtype == TYPE_HEAP_START) {
2641 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2642 LOG_TIME (time_base, tdiff);
2645 fprintf (outfile, "heap shot start\n");
2646 thread->current_heap_shot = new_heap_shot (time_base);
2650 case TYPE_MONITOR: {
2651 int event = (*p >> 4) & 0x3;
2652 int has_bt = *p & TYPE_MONITOR_BT;
2653 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2654 intptr_t objdiff = decode_sleb128 (p, &p);
2655 MethodDesc* sframes [8];
2656 MethodDesc** frames = sframes;
2659 LOG_TIME (time_base, tdiff);
2661 record = (!thread_filter || thread_filter == thread->thread_id);
2662 if (!(time_base >= time_from && time_base < time_to))
2664 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
2665 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
2667 monitor_contention++;
2668 mdesc->contentions++;
2669 thread->monitor = mdesc;
2670 thread->contention_start = time_base;
2674 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
2676 fprintf (outfile, "Cannot load backtrace\n");
2680 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
2683 add_trace_thread (thread, &mdesc->traces, 1);
2685 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
2688 if (thread->monitor && thread->contention_start) {
2689 uint64_t wait_time = time_base - thread->contention_start;
2690 if (wait_time > thread->monitor->max_wait_time)
2691 thread->monitor->max_wait_time = wait_time;
2692 thread->monitor->wait_time += wait_time;
2693 thread->monitor = NULL;
2694 thread->contention_start = 0;
2697 } else if (event == MONO_PROFILER_MONITOR_DONE) {
2700 if (thread->monitor && thread->contention_start) {
2701 uint64_t wait_time = time_base - thread->contention_start;
2702 if (wait_time > thread->monitor->max_wait_time)
2703 thread->monitor->max_wait_time = wait_time;
2704 thread->monitor->wait_time += wait_time;
2705 thread->monitor = NULL;
2706 thread->contention_start = 0;
2711 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
2712 if (frames != sframes)
2716 case TYPE_EXCEPTION: {
2717 int subtype = *p & 0x70;
2718 int has_bt = *p & TYPE_EXCEPTION_BT;
2719 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2720 MethodDesc* sframes [8];
2721 MethodDesc** frames = sframes;
2723 LOG_TIME (time_base, tdiff);
2725 record = (!thread_filter || thread_filter == thread->thread_id);
2726 if (!(time_base >= time_from && time_base < time_to))
2728 if (subtype == TYPE_CLAUSE) {
2729 int clause_type = decode_uleb128 (p, &p);
2730 int clause_num = decode_uleb128 (p, &p);
2731 int64_t ptrdiff = decode_sleb128 (p, &p);
2732 method_base += ptrdiff;
2734 clause_summary [clause_type]++;
2736 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
2738 intptr_t objdiff = decode_sleb128 (p, &p);
2743 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
2745 fprintf (outfile, "Cannot load backtrace\n");
2749 add_trace_methods (frames, has_bt, &exc_traces, 1);
2752 add_trace_thread (thread, &exc_traces, 1);
2754 if (frames != sframes)
2757 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
2761 case TYPE_RUNTIME: {
2762 int subtype = *p & 0xf0;
2763 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2764 LOG_TIME (time_base, tdiff);
2766 if (subtype == TYPE_JITHELPER) {
2767 int type = decode_uleb128 (p, &p);
2768 intptr_t codediff = decode_sleb128 (p, &p);
2769 int codelen = decode_uleb128 (p, &p);
2771 if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
2772 name = (const char *)p;
2776 name = code_buffer_desc (type);
2779 jit_helpers_code_size += codelen;
2781 fprintf (outfile, "jit helper %s, size: %d, code: %p\n", name, codelen, (void*)(ptr_base + codediff));
2786 int subtype = *p & 0xf0;
2787 if (subtype == TYPE_SAMPLE_HIT) {
2789 int sample_type = decode_uleb128 (p + 1, &p);
2790 uint64_t tstamp = decode_uleb128 (p, &p);
2791 void *tid = (void *) thread_id;
2792 if (ctx->data_version > 10)
2793 tid = (void *) (ptr_base + decode_sleb128 (p, &p));
2794 int count = decode_uleb128 (p, &p);
2795 for (i = 0; i < count; ++i) {
2796 uintptr_t ip = ptr_base + decode_sleb128 (p, &p);
2797 if ((tstamp >= time_from && tstamp < time_to))
2798 add_stat_sample (sample_type, ip);
2800 fprintf (outfile, "sample hit, type: %d at %p for thread %p\n", sample_type, (void*)ip, tid);
2802 if (ctx->data_version > 5) {
2803 count = decode_uleb128 (p, &p);
2804 for (i = 0; i < count; ++i) {
2806 int64_t ptrdiff = decode_sleb128 (p, &p);
2807 int il_offset = decode_sleb128 (p, &p);
2808 int native_offset = decode_sleb128 (p, &p);
2809 method_base += ptrdiff;
2810 method = lookup_method (method_base);
2812 fprintf (outfile, "sample hit bt %d: %s at IL offset %d (native: %d)\n", i, method->name, il_offset, native_offset);
2815 } else if (subtype == TYPE_SAMPLE_USYM) {
2816 /* un unmanaged symbol description */
2817 uintptr_t addr = ptr_base + decode_sleb128 (p + 1, &p);
2818 uintptr_t size = decode_uleb128 (p, &p);
2820 name = pstrdup ((char*)p);
2821 add_unmanaged_symbol (addr, name, size);
2823 fprintf (outfile, "unmanaged symbol %s at %p\n", name, (void*)addr);
2826 } else if (subtype == TYPE_SAMPLE_UBIN) {
2827 /* un unmanaged binary loaded in memory */
2828 uint64_t tdiff = decode_uleb128 (p + 1, &p);
2829 uintptr_t addr = decode_sleb128 (p, &p);
2830 uint64_t offset G_GNUC_UNUSED = decode_uleb128 (p, &p);
2831 uintptr_t size = decode_uleb128 (p, &p);
2833 LOG_TIME (time_base, tdiff);
2835 name = pstrdup ((char*)p);
2836 add_unmanaged_binary (addr, name, size);
2838 fprintf (outfile, "unmanaged binary %s at %p\n", name, (void*)addr);
2841 } else if (subtype == TYPE_SAMPLE_COUNTERS_DESC) {
2842 uint64_t i, len = decode_uleb128 (p + 1, &p);
2843 for (i = 0; i < len; i++) {
2844 uint64_t type, unit, variance, index;
2845 uint64_t section = decode_uleb128 (p, &p);
2846 char *section_str, *name;
2847 if (section != MONO_COUNTER_PERFCOUNTERS) {
2848 section_str = (char*) section_name (section);
2850 section_str = pstrdup ((char*)p);
2853 name = pstrdup ((char*)p);
2855 type = decode_uleb128 (p, &p);
2856 unit = decode_uleb128 (p, &p);
2857 variance = decode_uleb128 (p, &p);
2858 index = decode_uleb128 (p, &p);
2859 add_counter (section_str, name, (int)type, (int)unit, (int)variance, (int)index);
2861 } else if (subtype == TYPE_SAMPLE_COUNTERS) {
2863 CounterValue *value, *previous = NULL;
2865 uint64_t timestamp = decode_uleb128 (p + 1, &p);
2866 uint64_t time_between = timestamp / 1000 * 1000 * 1000 * 1000 + startup_time;
2868 uint64_t type, index = decode_uleb128 (p, &p);
2872 for (list = counters; list; list = list->next) {
2873 if (list->counter->index == (int)index) {
2874 previous = list->counter->values_last;
2879 type = decode_uleb128 (p, &p);
2881 value = (CounterValue *)calloc (1, sizeof (CounterValue));
2882 value->timestamp = timestamp;
2885 case MONO_COUNTER_INT:
2886 #if SIZEOF_VOID_P == 4
2887 case MONO_COUNTER_WORD:
2889 value->buffer = (unsigned char *)malloc (sizeof (int32_t));
2890 *(int32_t*)value->buffer = (int32_t)decode_sleb128 (p, &p) + (previous ? (*(int32_t*)previous->buffer) : 0);
2892 case MONO_COUNTER_UINT:
2893 value->buffer = (unsigned char *)malloc (sizeof (uint32_t));
2894 *(uint32_t*)value->buffer = (uint32_t)decode_uleb128 (p, &p) + (previous ? (*(uint32_t*)previous->buffer) : 0);
2896 case MONO_COUNTER_LONG:
2897 #if SIZEOF_VOID_P == 8
2898 case MONO_COUNTER_WORD:
2900 case MONO_COUNTER_TIME_INTERVAL:
2901 value->buffer = (unsigned char *)malloc (sizeof (int64_t));
2902 *(int64_t*)value->buffer = (int64_t)decode_sleb128 (p, &p) + (previous ? (*(int64_t*)previous->buffer) : 0);
2904 case MONO_COUNTER_ULONG:
2905 value->buffer = (unsigned char *)malloc (sizeof (uint64_t));
2906 *(uint64_t*)value->buffer = (uint64_t)decode_uleb128 (p, &p) + (previous ? (*(uint64_t*)previous->buffer) : 0);
2908 case MONO_COUNTER_DOUBLE:
2909 value->buffer = (unsigned char *)malloc (sizeof (double));
2910 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
2911 for (i = 0; i < sizeof (double); i++)
2913 for (i = sizeof (double) - 1; i >= 0; i--)
2915 value->buffer[i] = *p++;
2917 case MONO_COUNTER_STRING:
2919 value->buffer = NULL;
2921 value->buffer = (unsigned char*) pstrdup ((char*)p);
2926 if (time_between >= time_from && time_between <= time_to)
2927 add_counter_value (index, value);
2934 case TYPE_COVERAGE:{
2935 int subtype = *p & 0xf0;
2937 case TYPE_COVERAGE_METHOD: {
2938 CoverageMethod *method = g_new0 (CoverageMethod, 1);
2939 const char *assembly, *klass, *name, *sig, *filename;
2940 int token, n_offsets, method_id;
2943 assembly = (const char *)p; while (*p) p++; p++;
2944 klass = (const char *)p; while (*p) p++; p++;
2945 name = (const char *)p; while (*p) p++; p++;
2946 sig = (const char *)p; while (*p) p++; p++;
2947 filename = (const char *)p; while (*p) p++; p++;
2949 token = decode_uleb128 (p, &p);
2950 method_id = decode_uleb128 (p, &p);
2951 n_offsets = decode_uleb128 (p, &p);
2953 method->assembly_name = g_strdup (assembly);
2954 method->class_name = g_strdup (klass);
2955 method->method_name = g_strdup (name);
2956 method->method_signature = g_strdup (sig);
2957 method->filename = g_strdup (filename);
2958 method->token = token;
2959 method->n_statements = n_offsets;
2960 method->coverage = g_ptr_array_new ();
2961 method->method_id = method_id;
2963 coverage_add_method (method);
2967 case TYPE_COVERAGE_STATEMENT: {
2968 CoverageCoverage *coverage = g_new0 (CoverageCoverage, 1);
2969 int offset, count, line, column, method_id;
2972 method_id = decode_uleb128 (p, &p);
2973 offset = decode_uleb128 (p, &p);
2974 count = decode_uleb128 (p, &p);
2975 line = decode_uleb128 (p, &p);
2976 column = decode_uleb128 (p, &p);
2978 coverage->method_id = method_id;
2979 coverage->offset = offset;
2980 coverage->count = count;
2981 coverage->line = line;
2982 coverage->column = column;
2984 coverage_add_coverage (coverage);
2987 case TYPE_COVERAGE_ASSEMBLY: {
2988 CoverageAssembly *assembly = g_new0 (CoverageAssembly, 1);
2989 char *name, *guid, *filename;
2990 int number_of_methods, fully_covered, partially_covered;
2993 name = (char *)p; while (*p) p++; p++;
2994 guid = (char *)p; while (*p) p++; p++;
2995 filename = (char *)p; while (*p) p++; p++;
2996 number_of_methods = decode_uleb128 (p, &p);
2997 fully_covered = decode_uleb128 (p, &p);
2998 partially_covered = decode_uleb128 (p, &p);
3000 assembly->name = g_strdup (name);
3001 assembly->guid = g_strdup (guid);
3002 assembly->filename = g_strdup (filename);
3003 assembly->number_of_methods = number_of_methods;
3004 assembly->fully_covered = fully_covered;
3005 assembly->partially_covered = partially_covered;
3007 coverage_add_assembly (assembly);
3010 case TYPE_COVERAGE_CLASS: {
3011 CoverageClass *klass = g_new0 (CoverageClass, 1);
3012 char *assembly_name, *class_name;
3013 int number_of_methods, fully_covered, partially_covered;
3016 assembly_name = (char *)p; while (*p) p++; p++;
3017 class_name = (char *)p; while (*p) p++; p++;
3018 number_of_methods = decode_uleb128 (p, &p);
3019 fully_covered = decode_uleb128 (p, &p);
3020 partially_covered = decode_uleb128 (p, &p);
3022 klass->assembly_name = g_strdup (assembly_name);
3023 klass->class_name = g_strdup (class_name);
3024 klass->number_of_methods = number_of_methods;
3025 klass->fully_covered = fully_covered;
3026 klass->partially_covered = partially_covered;
3028 coverage_add_class (klass);
3038 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);
3041 record_event_stats (event, p - start);
3043 thread->last_time = time_base;
3044 for (i = 0; i < thread->stack_id; ++i)
3045 thread->stack [i]->recurse_count = 0;
3050 load_file (char *name)
3053 ProfContext *ctx = (ProfContext *)calloc (sizeof (ProfContext), 1);
3054 if (strcmp (name, "-") == 0)
3057 ctx->file = fopen (name, "rb");
3059 printf ("Cannot open file: %s\n", name);
3062 #if defined (HAVE_SYS_ZLIB)
3063 if (ctx->file != stdin)
3064 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
3066 if (!load_data (ctx, 32))
3069 if (read_int32 (p) != LOG_HEADER_ID || p [6] > LOG_DATA_VERSION)
3071 ctx->version_major = p [4];
3072 ctx->version_minor = p [5];
3073 ctx->data_version = p [6];
3074 /* reading 64 bit files on 32 bit systems not supported yet */
3075 if (p [7] > sizeof (void*))
3077 if (read_int32 (p + 20)) /* flags must be 0 */
3079 ctx->startup_time = read_int64 (p + 8);
3080 ctx->timer_overhead = read_int32 (p + 16);
3081 ctx->pid = read_int32 (p + 24);
3082 ctx->port = read_int16 (p + 28);
3090 static int alloc_sort_mode = ALLOC_SORT_BYTES;
3093 compare_class (const void *a, const void *b)
3095 ClassDesc *const *A = (ClassDesc *const *)a;
3096 ClassDesc *const *B = (ClassDesc *const *)b;
3097 uint64_t vala, valb;
3098 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
3099 vala = (*A)->alloc_size;
3100 valb = (*B)->alloc_size;
3102 vala = (*A)->allocs;
3103 valb = (*B)->allocs;
3113 dump_header (ProfContext *ctx)
3115 time_t st = ctx->startup_time / 1000;
3116 char *t = ctime (&st);
3117 fprintf (outfile, "\nMono log profiler data\n");
3118 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
3119 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
3120 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
3121 fprintf (outfile, "\tProgram startup: %s", t);
3123 fprintf (outfile, "\tProgram ID: %d\n", ctx->pid);
3125 fprintf (outfile, "\tServer listening on: %d\n", ctx->port);
3129 dump_traces (TraceDesc *traces, const char *desc)
3136 sort_context_array (traces);
3137 for (j = 0; j < traces->count; ++j) {
3140 bt = traces->traces [j].bt;
3143 fprintf (outfile, "\t%llu %s from:\n", (unsigned long long) traces->traces [j].count, desc);
3144 for (k = 0; k < bt->count; ++k)
3145 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
3150 dump_threads (ProfContext *ctx)
3152 ThreadContext *thread;
3153 fprintf (outfile, "\nThread summary\n");
3154 for (thread = ctx->threads; thread; thread = thread->next) {
3155 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
3160 dump_domains (ProfContext *ctx)
3162 fprintf (outfile, "\nDomain summary\n");
3164 for (DomainContext *domain = ctx->domains; domain; domain = domain->next)
3165 fprintf (outfile, "\tDomain: %p, friendly name: \"%s\"\n", (void *) domain->domain_id, domain->friendly_name);
3169 dump_remctxs (ProfContext *ctx)
3171 fprintf (outfile, "\nContext summary\n");
3173 for (RemCtxContext *remctx = ctx->remctxs; remctx; remctx = remctx->next)
3174 fprintf (outfile, "\tContext: %p, domain: %p\n", (void *) remctx->remctx_id, (void *) remctx->domain_id);
3178 dump_exceptions (void)
3181 fprintf (outfile, "\nException summary\n");
3182 fprintf (outfile, "\tThrows: %llu\n", (unsigned long long) throw_count);
3183 dump_traces (&exc_traces, "throws");
3184 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
3185 if (!clause_summary [i])
3187 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), (unsigned long long) clause_summary [i]);
3192 compare_monitor (const void *a, const void *b)
3194 MonitorDesc *const *A = (MonitorDesc *const *)a;
3195 MonitorDesc *const *B = (MonitorDesc *const *)b;
3196 if ((*B)->wait_time == (*A)->wait_time)
3198 if ((*B)->wait_time < (*A)->wait_time)
3204 dump_monitors (void)
3206 MonitorDesc **monitors;
3210 monitors = (MonitorDesc **)malloc (sizeof (void*) * num_monitors);
3211 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
3212 MonitorDesc *mdesc = monitor_hash [i];
3214 monitors [j++] = mdesc;
3215 mdesc = mdesc->next;
3218 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
3219 fprintf (outfile, "\nMonitor lock summary\n");
3220 for (i = 0; i < num_monitors; ++i) {
3221 MonitorDesc *mdesc = monitors [i];
3222 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
3223 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
3224 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
3225 dump_traces (&mdesc->traces, "contentions");
3227 fprintf (outfile, "\tLock contentions: %llu\n", (unsigned long long) monitor_contention);
3228 fprintf (outfile, "\tLock acquired: %llu\n", (unsigned long long) monitor_acquired);
3229 fprintf (outfile, "\tLock failures: %llu\n", (unsigned long long) monitor_failed);
3236 fprintf (outfile, "\nGC summary\n");
3237 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
3238 fprintf (outfile, "\tMax heap size: %llu\n", (unsigned long long) max_heap_size);
3239 fprintf (outfile, "\tObject moves: %llu\n", (unsigned long long) gc_object_moves);
3240 for (i = 0; i < 3; ++i) {
3241 if (!gc_info [i].count)
3243 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
3244 i, gc_info [i].count,
3245 (unsigned long long) (gc_info [i].max_time / 1000),
3246 (unsigned long long) (gc_info [i].total_time / 1000),
3247 (unsigned long long) (gc_info [i].total_time / gc_info [i].count / 1000));
3249 for (i = 0; i < 3; ++i) {
3250 if (!handle_info [i].max_live)
3252 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
3253 get_handle_name (i),
3254 (unsigned long long) (handle_info [i].created),
3255 (unsigned long long) (handle_info [i].destroyed),
3256 (unsigned long long) (handle_info [i].max_live));
3257 dump_traces (&handle_info [i].traces, "created");
3258 dump_traces (&handle_info [i].destroy_traces, "destroyed");
3267 int compiled_methods = 0;
3269 fprintf (outfile, "\nJIT summary\n");
3270 for (i = 0; i < HASH_SIZE; ++i) {
3271 m = method_hash [i];
3272 for (m = method_hash [i]; m; m = m->next) {
3273 if (!m->code || m->ignore_jit)
3276 code_size += m->len;
3279 fprintf (outfile, "\tCompiled methods: %d\n", compiled_methods);
3280 fprintf (outfile, "\tGenerated code size: %d\n", code_size);
3281 fprintf (outfile, "\tJIT helpers: %d\n", num_jit_helpers);
3282 fprintf (outfile, "\tJIT helpers code size: %d\n", jit_helpers_code_size);
3286 dump_allocations (void)
3289 intptr_t allocs = 0;
3291 int header_done = 0;
3292 ClassDesc **classes = (ClassDesc **)malloc (num_classes * sizeof (void*));
3295 for (i = 0; i < HASH_SIZE; ++i) {
3296 cd = class_hash [i];
3302 qsort (classes, num_classes, sizeof (void*), compare_class);
3303 for (i = 0; i < num_classes; ++i) {
3307 allocs += cd->allocs;
3308 size += cd->alloc_size;
3309 if (!header_done++) {
3310 fprintf (outfile, "\nAllocation summary\n");
3311 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
3313 fprintf (outfile, "%10llu %10zd %8llu %s\n",
3314 (unsigned long long) (cd->alloc_size),
3316 (unsigned long long) (cd->alloc_size / cd->allocs),
3318 dump_traces (&cd->traces, "bytes");
3321 fprintf (outfile, "Total memory allocated: %llu bytes in %zd objects\n", (unsigned long long) size, allocs);
3330 static int method_sort_mode = METHOD_SORT_TOTAL;
3333 compare_method (const void *a, const void *b)
3335 MethodDesc *const *A = (MethodDesc *const *)a;
3336 MethodDesc *const *B = (MethodDesc *const *)b;
3337 uint64_t vala, valb;
3338 if (method_sort_mode == METHOD_SORT_SELF) {
3339 vala = (*A)->self_time;
3340 valb = (*B)->self_time;
3341 } else if (method_sort_mode == METHOD_SORT_CALLS) {
3345 vala = (*A)->total_time;
3346 valb = (*B)->total_time;
3356 dump_metadata (void)
3358 fprintf (outfile, "\nMetadata summary\n");
3359 fprintf (outfile, "\tLoaded images: %d\n", num_images);
3363 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
3364 image = image_hash [i];
3366 fprintf (outfile, "\t\t%s\n", image->filename);
3367 image = image->next;
3371 fprintf (outfile, "\tLoaded assemblies: %d\n", num_assemblies);
3373 AssemblyDesc *assembly;
3375 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
3376 assembly = assembly_hash [i];
3378 fprintf (outfile, "\t\t%s\n", assembly->asmname);
3379 assembly = assembly->next;
3390 int header_done = 0;
3391 MethodDesc **methods = (MethodDesc **)malloc (num_methods * sizeof (void*));
3394 for (i = 0; i < HASH_SIZE; ++i) {
3395 cd = method_hash [i];
3397 cd->total_time = cd->self_time + cd->callee_time;
3402 qsort (methods, num_methods, sizeof (void*), compare_method);
3403 for (i = 0; i < num_methods; ++i) {
3410 msecs = cd->total_time / 1000000;
3411 smsecs = (cd->total_time - cd->callee_time) / 1000000;
3412 if (!msecs && !verbose)
3414 if (!header_done++) {
3415 fprintf (outfile, "\nMethod call summary\n");
3416 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
3418 fprintf (outfile, "%8llu %8llu %10llu %s\n",
3419 (unsigned long long) (msecs),
3420 (unsigned long long) (smsecs),
3421 (unsigned long long) (cd->calls),
3423 dump_traces (&cd->traces, "calls");
3426 fprintf (outfile, "Total calls: %llu\n", (unsigned long long) calls);
3430 compare_heap_class (const void *a, const void *b)
3432 HeapClassDesc *const *A = (HeapClassDesc *const *)a;
3433 HeapClassDesc *const *B = (HeapClassDesc *const *)b;
3434 uint64_t vala, valb;
3435 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
3436 vala = (*A)->total_size;
3437 valb = (*B)->total_size;
3450 compare_rev_class (const void *a, const void *b)
3452 const HeapClassRevRef *A = (const HeapClassRevRef *)a;
3453 const HeapClassRevRef *B = (const HeapClassRevRef *)b;
3454 if (B->count == A->count)
3456 if (B->count < A->count)
3462 dump_rev_claases (HeapClassRevRef *revs, int count)
3469 for (j = 0; j < count; ++j) {
3470 HeapClassDesc *cd = revs [j].klass;
3471 fprintf (outfile, "\t\t%llu references from: %s\n",
3472 (unsigned long long) (revs [j].count),
3478 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
3485 HeapClassDesc **sorted;
3486 sorted = (HeapClassDesc **)malloc (sizeof (void*) * hs->class_count);
3487 for (i = 0; i < hs->hash_size; ++i) {
3488 cd = hs->class_hash [i];
3492 size += cd->total_size;
3493 sorted [ccount++] = cd;
3495 hs->sorted = sorted;
3496 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
3497 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d, roots: %zd\n",
3499 (hs->timestamp - startup_time)/1000000000.0,
3500 (unsigned long long) (size),
3501 (unsigned long long) (count),
3502 ccount, hs->num_roots);
3503 if (!verbose && ccount > 30)
3505 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
3506 for (i = 0; i < ccount; ++i) {
3507 HeapClassRevRef *rev_sorted;
3509 HeapClassDesc *ocd = NULL;
3512 ocd = heap_class_lookup (last_hs, cd->klass);
3513 fprintf (outfile, "\t%10llu %10llu %8llu %s",
3514 (unsigned long long) (cd->total_size),
3515 (unsigned long long) (cd->count),
3516 (unsigned long long) (cd->total_size / cd->count),
3519 int64_t bdiff = cd->total_size - ocd->total_size;
3520 int64_t cdiff = cd->count - ocd->count;
3521 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", (long long) bdiff, (long long) cdiff);
3523 fprintf (outfile, "\n");
3525 if (!collect_traces)
3527 rev_sorted = (HeapClassRevRef *)malloc (cd->rev_count * sizeof (HeapClassRevRef));
3529 for (j = 0; j < cd->rev_hash_size; ++j) {
3530 if (cd->rev_hash [j].klass)
3531 rev_sorted [k++] = cd->rev_hash [j];
3533 assert (cd->rev_count == k);
3534 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
3535 if (cd->root_references)
3536 fprintf (outfile, "\t\t%zd root references (%zd pinning)\n", cd->root_references, cd->pinned_references);
3537 dump_rev_claases (rev_sorted, cd->rev_count);
3544 compare_heap_shots (const void *a, const void *b)
3546 HeapShot *const *A = (HeapShot *const *)a;
3547 HeapShot *const *B = (HeapShot *const *)b;
3548 if ((*B)->timestamp == (*A)->timestamp)
3550 if ((*B)->timestamp > (*A)->timestamp)
3556 dump_heap_shots (void)
3558 HeapShot **hs_sorted;
3560 HeapShot *last_hs = NULL;
3564 hs_sorted = (HeapShot **)malloc (num_heap_shots * sizeof (void*));
3565 fprintf (outfile, "\nHeap shot summary\n");
3567 for (hs = heap_shots; hs; hs = hs->next)
3568 hs_sorted [i++] = hs;
3569 qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
3570 for (i = 0; i < num_heap_shots; ++i) {
3572 heap_shot_summary (hs, i, last_hs);
3577 /* This is a very basic escape function that escapes < > and &
3578 Ideally we'd use g_markup_escape_string but that function isn't
3579 available in Mono's eglib. This was written without looking at the
3580 source of that function in glib. */
3582 escape_string_for_xml (const char *string)
3584 GString *string_builder = g_string_new (NULL);
3585 const char *start, *p;
3589 while (*p && *p != '&' && *p != '<' && *p != '>')
3592 g_string_append_len (string_builder, start, p - start);
3599 g_string_append (string_builder, "<");
3603 g_string_append (string_builder, ">");
3607 g_string_append (string_builder, "&");
3618 return g_string_free (string_builder, FALSE);
3622 sort_assemblies (gconstpointer a, gconstpointer b)
3624 CoverageAssembly *assembly_a = *(CoverageAssembly **)a;
3625 CoverageAssembly *assembly_b = *(CoverageAssembly **)b;
3627 if (assembly_a->name == NULL && assembly_b->name == NULL)
3629 else if (assembly_a->name == NULL)
3631 else if (assembly_b->name == NULL)
3634 return strcmp (assembly_a->name, assembly_b->name);
3638 dump_coverage (void)
3640 if (!coverage_methods && !coverage_assemblies)
3643 gather_coverage_statements ();
3644 fprintf (outfile, "\nCoverage Summary:\n");
3646 if (coverage_outfile) {
3647 fprintf (coverage_outfile, "<?xml version=\"1.0\"?>\n");
3648 fprintf (coverage_outfile, "<coverage version=\"0.3\">\n");
3651 g_ptr_array_sort (coverage_assemblies, sort_assemblies);
3653 for (guint i = 0; i < coverage_assemblies->len; i++) {
3654 CoverageAssembly *assembly = (CoverageAssembly *)coverage_assemblies->pdata[i];
3657 if (assembly->number_of_methods != 0) {
3658 int percentage = ((assembly->fully_covered + assembly->partially_covered) * 100) / assembly->number_of_methods;
3659 fprintf (outfile, "\t%s (%s) %d%% covered (%d methods - %d covered)\n", assembly->name, assembly->filename, percentage, assembly->number_of_methods, assembly->fully_covered);
3661 fprintf (outfile, "\t%s (%s) ?%% covered (%d methods - %d covered)\n", assembly->name, assembly->filename, assembly->number_of_methods, assembly->fully_covered);
3663 if (coverage_outfile) {
3664 char *escaped_name, *escaped_filename;
3665 escaped_name = escape_string_for_xml (assembly->name);
3666 escaped_filename = escape_string_for_xml (assembly->filename);
3668 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);
3670 g_free (escaped_name);
3671 g_free (escaped_filename);
3674 classes = (GPtrArray *)g_hash_table_lookup (coverage_assembly_classes, assembly->name);
3676 for (guint j = 0; j < classes->len; j++) {
3677 CoverageClass *klass = (CoverageClass *)classes->pdata [j];
3679 if (klass->number_of_methods > 0) {
3680 int percentage = ((klass->fully_covered + klass->partially_covered) * 100) / klass->number_of_methods;
3681 fprintf (outfile, "\t\t%s %d%% covered (%d methods - %d covered)\n", klass->class_name, percentage, klass->number_of_methods, klass->fully_covered);
3683 fprintf (outfile, "\t\t%s ?%% covered (%d methods - %d covered)\n", klass->class_name, klass->number_of_methods, klass->fully_covered);
3685 if (coverage_outfile) {
3687 escaped_name = escape_string_for_xml (klass->class_name);
3689 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);
3690 g_free (escaped_name);
3696 for (guint i = 0; i < coverage_methods->len; i++) {
3697 CoverageMethod *method = (CoverageMethod *)coverage_methods->pdata [i];
3699 if (coverage_outfile) {
3700 char *escaped_assembly, *escaped_class, *escaped_method, *escaped_sig, *escaped_filename;
3702 escaped_assembly = escape_string_for_xml (method->assembly_name);
3703 escaped_class = escape_string_for_xml (method->class_name);
3704 escaped_method = escape_string_for_xml (method->method_name);
3705 escaped_sig = escape_string_for_xml (method->method_signature);
3706 escaped_filename = escape_string_for_xml (method->filename);
3708 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);
3710 g_free (escaped_assembly);
3711 g_free (escaped_class);
3712 g_free (escaped_method);
3713 g_free (escaped_sig);
3714 g_free (escaped_filename);
3716 for (guint j = 0; j < method->coverage->len; j++) {
3717 CoverageCoverage *coverage = (CoverageCoverage *)method->coverage->pdata [j];
3718 fprintf (coverage_outfile, "\t\t<statement offset=\"%d\" counter=\"%d\" line=\"%d\" column=\"%d\"/>\n", coverage->offset, coverage->count, coverage->line, coverage->column);
3720 fprintf (coverage_outfile, "\t</method>\n");
3724 if (coverage_outfile) {
3725 fprintf (coverage_outfile, "</coverage>\n");
3726 fclose (coverage_outfile);
3727 coverage_outfile = NULL;
3731 #define DUMP_EVENT_STAT(EVENT,SUBTYPE) dump_event (#EVENT, #SUBTYPE, EVENT, SUBTYPE);
3734 dump_event (const char *event_name, const char *subtype_name, int event, int subtype)
3736 int idx = event | subtype;
3737 EventStat evt = stats [idx];
3741 fprintf (outfile, "\t%16s\t%26s\tcount %6d\tmin %3d\tmax %6d\tbytes %d\n", event_name, subtype_name, evt.count, evt.min_size, evt.max_size, evt.bytes);
3747 fprintf (outfile, "\nMlpd statistics\n");
3748 fprintf (outfile, "\tBuffer count %d\toverhead %d (%d bytes per header)\n", buffer_count, buffer_count * BUFFER_HEADER_SIZE, BUFFER_HEADER_SIZE);
3749 fprintf (outfile, "\nEvent details:\n");
3751 DUMP_EVENT_STAT (TYPE_ALLOC, TYPE_ALLOC_NO_BT);
3752 DUMP_EVENT_STAT (TYPE_ALLOC, TYPE_ALLOC_BT);
3754 DUMP_EVENT_STAT (TYPE_GC, TYPE_GC_EVENT);
3755 DUMP_EVENT_STAT (TYPE_GC, TYPE_GC_RESIZE);
3756 DUMP_EVENT_STAT (TYPE_GC, TYPE_GC_MOVE);
3757 DUMP_EVENT_STAT (TYPE_GC, TYPE_GC_HANDLE_CREATED);
3758 DUMP_EVENT_STAT (TYPE_GC, TYPE_GC_HANDLE_DESTROYED);
3759 DUMP_EVENT_STAT (TYPE_GC, TYPE_GC_HANDLE_CREATED_BT);
3760 DUMP_EVENT_STAT (TYPE_GC, TYPE_GC_HANDLE_DESTROYED_BT);
3762 DUMP_EVENT_STAT (TYPE_METADATA, TYPE_END_LOAD);
3763 DUMP_EVENT_STAT (TYPE_METADATA, TYPE_END_UNLOAD);
3765 DUMP_EVENT_STAT (TYPE_METHOD, TYPE_LEAVE);
3766 DUMP_EVENT_STAT (TYPE_METHOD, TYPE_ENTER);
3767 DUMP_EVENT_STAT (TYPE_METHOD, TYPE_EXC_LEAVE);
3768 DUMP_EVENT_STAT (TYPE_METHOD, TYPE_JIT);
3770 DUMP_EVENT_STAT (TYPE_EXCEPTION, TYPE_THROW);
3771 DUMP_EVENT_STAT (TYPE_EXCEPTION, TYPE_CLAUSE);
3772 DUMP_EVENT_STAT (TYPE_EXCEPTION, TYPE_EXCEPTION_BT);
3774 DUMP_EVENT_STAT (TYPE_MONITOR, TYPE_MONITOR_NO_BT);
3775 DUMP_EVENT_STAT (TYPE_MONITOR, TYPE_MONITOR_BT);
3777 DUMP_EVENT_STAT (TYPE_HEAP, TYPE_HEAP_START);
3778 DUMP_EVENT_STAT (TYPE_HEAP, TYPE_HEAP_END);
3779 DUMP_EVENT_STAT (TYPE_HEAP, TYPE_HEAP_OBJECT);
3780 DUMP_EVENT_STAT (TYPE_HEAP, TYPE_HEAP_ROOT);
3782 DUMP_EVENT_STAT (TYPE_SAMPLE, TYPE_SAMPLE_HIT);
3783 DUMP_EVENT_STAT (TYPE_SAMPLE, TYPE_SAMPLE_USYM);
3784 DUMP_EVENT_STAT (TYPE_SAMPLE, TYPE_SAMPLE_UBIN);
3785 DUMP_EVENT_STAT (TYPE_SAMPLE, TYPE_SAMPLE_COUNTERS_DESC);
3786 DUMP_EVENT_STAT (TYPE_SAMPLE, TYPE_SAMPLE_COUNTERS);
3788 DUMP_EVENT_STAT (TYPE_RUNTIME, TYPE_JITHELPER);
3790 DUMP_EVENT_STAT (TYPE_COVERAGE, TYPE_COVERAGE_ASSEMBLY);
3791 DUMP_EVENT_STAT (TYPE_COVERAGE, TYPE_COVERAGE_METHOD);
3792 DUMP_EVENT_STAT (TYPE_COVERAGE, TYPE_COVERAGE_STATEMENT);
3793 DUMP_EVENT_STAT (TYPE_COVERAGE, TYPE_COVERAGE_CLASS);
3799 flush_context (ProfContext *ctx)
3801 ThreadContext *thread;
3802 /* FIXME: sometimes there are leftovers: indagate */
3803 for (thread = ctx->threads; thread; thread = thread->next) {
3804 while (thread->stack_id) {
3806 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
3807 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
3812 static const char *reports = "header,jit,gc,sample,alloc,call,metadata,exception,monitor,thread,heapshot,counters,coverage";
3815 match_option (const char *p, const char *opt)
3817 int len = strlen (opt);
3818 if (strncmp (p, opt, len) == 0) {
3827 print_reports (ProfContext *ctx, const char *reps, int parse_only)
3831 for (p = reps; *p; p = opt) {
3832 if ((opt = match_option (p, "header")) != p) {
3837 if ((opt = match_option (p, "thread")) != p) {
3842 if ((opt = match_option (p, "domain")) != p) {
3847 if ((opt = match_option (p, "context")) != p) {
3852 if ((opt = match_option (p, "gc")) != p) {
3857 if ((opt = match_option (p, "jit")) != p) {
3862 if ((opt = match_option (p, "alloc")) != p) {
3864 dump_allocations ();
3867 if ((opt = match_option (p, "call")) != p) {
3872 if ((opt = match_option (p, "metadata")) != p) {
3877 if ((opt = match_option (p, "exception")) != p) {
3882 if ((opt = match_option (p, "monitor")) != p) {
3887 if ((opt = match_option (p, "heapshot")) != p) {
3892 if ((opt = match_option (p, "sample")) != p) {
3897 if ((opt = match_option (p, "counters")) != p) {
3902 if ((opt = match_option (p, "coverage")) != p) {
3907 if ((opt = match_option (p, "stats")) != p) {
3918 add_find_spec (const char *p)
3920 if (p [0] == 'S' && p [1] == ':') {
3922 find_size = strtoul (p + 2, &vale, 10);
3924 } else if (p [0] == 'T' && p [1] == ':') {
3934 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
3935 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
3936 printf ("FILENAME can be '-' to read from standard input.\n");
3937 printf ("Options:\n");
3938 printf ("\t--help display this help\n");
3939 printf ("\t--out=FILE write to FILE instead of stdout\n");
3940 printf ("\t--traces collect and show backtraces\n");
3941 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
3942 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
3943 printf ("\t %s\n", reports);
3944 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
3945 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
3946 printf ("\t--counters-sort=MODE sort counters according to MODE: time, category\n");
3947 printf ("\t only accessible in verbose mode\n");
3948 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
3949 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
3950 printf ("\t S:minimum_size or T:partial_name\n");
3951 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
3952 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
3953 printf ("\t--verbose increase verbosity level\n");
3954 printf ("\t--debug display decoding debug info for mprof-report devs\n");
3955 printf ("\t--coverage-out=FILE write the coverage info to FILE as XML\n");
3959 main (int argc, char *argv[])
3964 for (i = 1; i < argc; ++i) {
3965 if (strcmp ("--debug", argv [i]) == 0) {
3967 } else if (strcmp ("--help", argv [i]) == 0) {
3970 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
3971 const char *val = argv [i] + 13;
3972 if (strcmp (val, "bytes") == 0) {
3973 alloc_sort_mode = ALLOC_SORT_BYTES;
3974 } else if (strcmp (val, "count") == 0) {
3975 alloc_sort_mode = ALLOC_SORT_COUNT;
3980 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
3981 const char *val = argv [i] + 14;
3982 if (strcmp (val, "total") == 0) {
3983 method_sort_mode = METHOD_SORT_TOTAL;
3984 } else if (strcmp (val, "self") == 0) {
3985 method_sort_mode = METHOD_SORT_SELF;
3986 } else if (strcmp (val, "calls") == 0) {
3987 method_sort_mode = METHOD_SORT_CALLS;
3992 } else if (strncmp ("--counters-sort=", argv [i], 16) == 0) {
3993 const char *val = argv [i] + 16;
3994 if (strcmp (val, "time") == 0) {
3995 counters_sort_mode = COUNTERS_SORT_TIME;
3996 } else if (strcmp (val, "category") == 0) {
3997 counters_sort_mode = COUNTERS_SORT_CATEGORY;
4002 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
4003 const char *val = argv [i] + 10;
4004 if (!print_reports (NULL, val, 1)) {
4009 } else if (strncmp ("--out=", argv [i], 6) == 0) {
4010 const char *val = argv [i] + 6;
4011 outfile = fopen (val, "w");
4013 printf ("Cannot open output file: %s\n", val);
4016 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
4017 const char *val = argv [i] + 12;
4019 trace_max = strtoul (val, &vale, 10);
4020 } else if (strncmp ("--find=", argv [i], 7) == 0) {
4021 const char *val = argv [i] + 7;
4022 if (!add_find_spec (val)) {
4026 } else if (strncmp ("--track=", argv [i], 8) == 0) {
4027 const char *val = argv [i] + 8;
4030 uintptr_t tracked_obj;
4035 tracked_obj = strtoul (val, &vale, 0);
4036 found_object (tracked_obj);
4039 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
4040 const char *val = argv [i] + 9;
4042 thread_filter = strtoul (val, &vale, 0);
4043 } else if (strncmp ("--time=", argv [i], 7) == 0) {
4044 char *val = pstrdup (argv [i] + 7);
4045 double from_secs, to_secs;
4046 char *top = strchr (val, '-');
4052 from_secs = atof (val);
4053 to_secs = atof (top);
4055 if (from_secs > to_secs) {
4059 time_from = from_secs * 1000000000;
4060 time_to = to_secs * 1000000000;
4061 use_time_filter = 1;
4062 } else if (strcmp ("--verbose", argv [i]) == 0) {
4064 } else if (strcmp ("--traces", argv [i]) == 0) {
4067 } else if (strncmp ("--coverage-out=", argv [i], 15) == 0) {
4068 const char *val = argv [i] + 15;
4069 coverage_outfile = fopen (val, "w");
4070 if (!coverage_outfile) {
4071 printf ("Cannot open output file: %s\n", val);
4082 ctx = load_file (argv [i]);
4084 printf ("Not a log profiler data file (or unsupported version).\n");
4087 while (decode_buffer (ctx));
4088 flush_context (ctx);
4089 if (num_tracked_objects)
4091 print_reports (ctx, reports, 0);