2 * decode.c: mprof-report program source: decode and analyze the log profiler data
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2010 Novell, Inc (http://www.novell.com)
14 #if !defined(__APPLE__)
22 #if defined (HAVE_ZLIB)
25 #include <mono/metadata/profiler.h>
26 #include <mono/metadata/object.h>
27 #include <mono/metadata/debug-helpers.h>
29 #define HASH_SIZE 9371
30 #define SMALL_HASH_SIZE 31
33 static int collect_traces = 0;
34 static int show_traces = 0;
35 static int trace_max = 6;
36 static int verbose = 0;
37 static uintptr_t *tracked_objects = 0;
38 static int num_tracked_objects = 0;
39 static uintptr_t thread_filter = 0;
40 static uint64_t find_size = 0;
41 static const char* find_name = NULL;
42 static uint64_t time_from = 0;
43 static uint64_t time_to = 0xffffffffffffffffULL;
44 static uint64_t startup_time = 0;
45 static FILE* outfile = NULL;
48 read_int32 (unsigned char *p)
52 value |= (*p++) << 16;
53 value |= (uint32_t)(*p++) << 24;
58 read_int64 (unsigned char *p)
60 uint64_t value = *p++;
62 value |= (*p++) << 16;
63 value |= (uint64_t)(*p++) << 24;
64 value |= (uint64_t)(*p++) << 32;
65 value |= (uint64_t)(*p++) << 40;
66 value |= (uint64_t)(*p++) << 48;
67 value |= (uint64_t)(*p++) << 54;
72 pstrdup (const char *s)
74 int len = strlen (s) + 1;
75 char *p = malloc (len);
80 static int num_images;
81 typedef struct _ImageDesc ImageDesc;
88 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
91 add_image (intptr_t image, char *name)
93 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
94 ImageDesc *cd = malloc (sizeof (ImageDesc));
96 cd->filename = pstrdup (name);
97 cd->next = image_hash [slot];
98 image_hash [slot] = cd;
102 typedef struct _BackTrace BackTrace;
114 typedef struct _ClassDesc ClassDesc;
124 static ClassDesc* class_hash [HASH_SIZE] = {0};
125 static int num_classes = 0;
128 add_class (intptr_t klass, const char *name)
130 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
132 cd = class_hash [slot];
133 while (cd && cd->klass != klass)
135 /* we resolved an unknown class (unless we had the code unloaded) */
137 /*printf ("resolved unknown: %s\n", name);*/
139 cd->name = pstrdup (name);
142 cd = calloc (sizeof (ClassDesc), 1);
144 cd->name = pstrdup (name);
145 cd->next = class_hash [slot];
148 cd->traces.count = 0;
150 cd->traces.traces = NULL;
151 class_hash [slot] = cd;
157 lookup_class (intptr_t klass)
159 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
160 ClassDesc *cd = class_hash [slot];
161 while (cd && cd->klass != klass)
164 return add_class (klass, "unresolved class");
168 typedef struct _MethodDesc MethodDesc;
178 uint64_t callee_time;
183 static MethodDesc* method_hash [HASH_SIZE] = {0};
184 static int num_methods = 0;
187 add_method (intptr_t method, const char *name, intptr_t code, int len)
189 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
191 cd = method_hash [slot];
192 while (cd && cd->method != method)
194 /* we resolved an unknown method (unless we had the code unloaded) */
198 /*printf ("resolved unknown: %s\n", name);*/
200 cd->name = pstrdup (name);
203 cd = calloc (sizeof (MethodDesc), 1);
205 cd->name = pstrdup (name);
210 cd->traces.count = 0;
212 cd->traces.traces = NULL;
213 cd->next = method_hash [slot];
214 method_hash [slot] = cd;
220 lookup_method (intptr_t method)
222 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
223 MethodDesc *cd = method_hash [slot];
224 while (cd && cd->method != method)
227 return add_method (method, "unknown method", 0, 0);
237 typedef struct _HeapShot HeapShot;
243 HeapClassDesc **class_hash;
244 HeapClassDesc **sorted;
247 static HeapShot *heap_shots = NULL;
248 static HeapShot *current_heap_shot = NULL;
251 new_heap_shot (uint64_t timestamp)
254 HeapShot *hs = calloc (sizeof (HeapShot), 1);
256 hs->class_hash = calloc (sizeof (void*), hs->hash_size);
257 hs->timestamp = timestamp;
258 /* append it to the list */
269 static HeapClassDesc*
270 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
273 unsigned int start_pos;
274 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
277 HeapClassDesc* cd = hs->class_hash [i];
280 if (cd->klass == klass)
283 if (++i == hs->hash_size)
285 } while (i != start_pos);
290 add_heap_hashed (HeapClassDesc **hash, int hsize, ClassDesc *klass, uint64_t size, uint64_t count)
293 unsigned int start_pos;
294 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
297 if (hash [i] && hash [i]->klass == klass) {
298 hash [i]->total_size += size;
299 hash [i]->count += count;
301 } else if (!hash [i]) {
302 hash [i] = calloc (sizeof (HeapClassDesc), 1);
303 hash [i]->klass = klass;
304 hash [i]->total_size += size;
305 hash [i]->count += count;
311 } while (i != start_pos);
312 /* should not happen */
313 printf ("failed heap class store\n");
318 add_heap_shot_obj (HeapShot *hs, ClassDesc *klass, uint64_t size)
321 if (hs->class_count * 2 >= hs->hash_size) {
323 int old_size = hs->hash_size;
325 if (hs->hash_size == 0)
327 n = calloc (sizeof (void*) * hs->hash_size, 1);
328 for (i = 0; i < old_size; ++i) {
329 if (hs->class_hash [i])
330 add_heap_hashed (n, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
333 free (hs->class_hash);
336 hs->class_count += add_heap_hashed (hs->class_hash, hs->hash_size, klass, size, 1);
344 MethodDesc *methods [1];
347 static BackTrace *backtrace_hash [HASH_SIZE];
348 static BackTrace **backtraces = NULL;
349 static int num_backtraces = 0;
350 static int next_backtrace = 0;
353 hash_backtrace (int count, MethodDesc **methods)
357 for (i = 0; i < count; ++i) {
358 hash = (hash << 5) - hash + methods [i]->method;
364 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
367 if (bt->count != count)
369 for (i = 0; i < count; ++i)
370 if (methods [i] != bt->methods [i])
376 add_backtrace (int count, MethodDesc **methods)
378 int hash = hash_backtrace (count, methods);
379 int slot = (hash & 0xffff) % HASH_SIZE;
380 BackTrace *bt = backtrace_hash [slot];
382 if (bt->hash == hash && compare_backtrace (bt, count, methods))
386 bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
387 bt->next = backtrace_hash [slot];
388 backtrace_hash [slot] = bt;
389 if (next_backtrace == num_backtraces) {
393 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
395 bt->id = next_backtrace++;
396 backtraces [bt->id] = bt;
399 for (slot = 0; slot < count; ++slot)
400 bt->methods [slot] = methods [slot];
405 typedef struct _MonitorDesc MonitorDesc;
406 typedef struct _ThreadContext ThreadContext;
410 #if defined (HAVE_ZLIB)
419 uint64_t startup_time;
420 ThreadContext *threads;
421 ThreadContext *current;
424 struct _ThreadContext {
429 uint64_t *time_stack;
430 uint64_t *callee_time_stack;
432 uint64_t contention_start;
433 MonitorDesc *monitor;
439 ensure_buffer (ProfContext *ctx, int size)
441 if (ctx->size < size) {
442 ctx->buf = realloc (ctx->buf, size);
448 load_data (ProfContext *ctx, int size)
450 ensure_buffer (ctx, size);
451 #if defined (HAVE_ZLIB)
453 return gzread (ctx->gzfile, ctx->buf, size) == size;
456 return fread (ctx->buf, size, 1, ctx->file);
459 static ThreadContext*
460 load_thread (ProfContext *ctx, intptr_t thread_id)
462 ThreadContext *thread;
463 if (ctx->current && ctx->current->thread_id == thread_id)
465 thread = ctx->threads;
467 if (thread->thread_id == thread_id) {
468 ctx->current = thread;
471 thread = thread->next;
473 thread = calloc (sizeof (ThreadContext), 1);
474 thread->next = ctx->threads;
475 ctx->threads = thread;
476 ctx->current = thread;
477 thread->thread_id = thread_id;
478 thread->last_time = 0;
479 thread->stack_id = 0;
480 thread->stack_size = 32;
481 thread->stack = malloc (thread->stack_size * sizeof (void*));
482 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
483 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
488 ensure_thread_stack (ThreadContext *thread)
490 if (thread->stack_id == thread->stack_size) {
491 thread->stack_size *= 2;
492 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
493 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
494 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
499 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
502 unsigned int start_pos;
503 start_pos = bt->hash % size;
506 if (traces [i].bt == bt) {
507 traces [i].count += value;
509 } else if (!traces [i].bt) {
511 traces [i].count += value;
517 } while (i != start_pos);
518 /* should not happen */
519 printf ("failed trace store\n");
524 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
529 if (trace->count * 2 >= trace->size) {
531 int old_size = trace->size;
533 if (trace->size == 0)
535 n = calloc (sizeof (CallContext) * trace->size, 1);
536 for (i = 0; i < old_size; ++i) {
537 if (trace->traces [i].bt)
538 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
541 free (trace->traces);
544 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
548 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
551 int count = thread->stack_id;
554 if (count > trace_max)
556 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
557 add_trace_bt (bt, trace, value);
562 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
567 if (count > trace_max)
569 bt = add_backtrace (count, methods);
570 add_trace_bt (bt, trace, value);
575 compare_callc (const void *a, const void *b)
577 const CallContext *A = a;
578 const CallContext *B = b;
579 if (B->count == A->count)
581 if (B->count < A->count)
587 sort_context_array (TraceDesc* traces)
590 for (i = 0, j = 0; i < traces->size; ++i) {
591 if (traces->traces [i].bt) {
592 traces->traces [j].bt = traces->traces [i].bt;
593 traces->traces [j].count = traces->traces [i].count;
597 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
601 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
603 ensure_thread_stack (thread);
604 thread->time_stack [thread->stack_id] = timestamp;
605 thread->callee_time_stack [thread->stack_id] = 0;
606 thread->stack [thread->stack_id++] = method;
607 method->recurse_count++;
611 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
613 method->recurse_count--;
614 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
618 if (timestamp < thread->time_stack [thread->stack_id])
619 fprintf (outfile, "time went backwards for %s\n", method->name);
620 tdiff = timestamp - thread->time_stack [thread->stack_id];
621 if (thread->callee_time_stack [thread->stack_id] > tdiff)
622 fprintf (outfile, "callee time bigger for %s\n", method->name);
623 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
624 method->callee_time += thread->callee_time_stack [thread->stack_id];
625 if (thread->stack_id)
626 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
627 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
629 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
634 uint64_t start_time; /* move this per-thread? */
639 static GCDesc gc_info [3];
640 static uint64_t max_heap_size;
641 static uint64_t gc_object_moves;
642 static int gc_resizes;
645 gc_event_name (int ev)
648 case MONO_GC_EVENT_START: return "start";
649 case MONO_GC_EVENT_MARK_START: return "mark start";
650 case MONO_GC_EVENT_MARK_END: return "mark end";
651 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
652 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
653 case MONO_GC_EVENT_END: return "end";
654 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
655 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
656 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
657 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
663 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
664 static uint64_t throw_count = 0;
665 static TraceDesc exc_traces;
668 clause_name (int type)
671 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
672 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
673 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
674 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
675 default: return "invalid";
679 static uint64_t monitor_contention;
680 static uint64_t monitor_failed;
681 static uint64_t monitor_acquired;
683 struct _MonitorDesc {
686 uintptr_t contentions;
688 uint64_t max_wait_time;
692 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
693 static int num_monitors = 0;
696 lookup_monitor (uintptr_t objid)
698 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
699 MonitorDesc *cd = monitor_hash [slot];
700 while (cd && cd->objid != objid)
703 cd = calloc (sizeof (MonitorDesc), 1);
705 cd->next = monitor_hash [slot];
706 monitor_hash [slot] = cd;
713 monitor_ev_name (int ev)
716 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
717 case MONO_PROFILER_MONITOR_DONE: return "acquired";
718 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
719 default: return "invalid";
724 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
728 int flags = decode_uleb128 (p, &p);
729 int count = decode_uleb128 (p, &p);
733 frames = malloc (count * sizeof (void*));
736 for (i = 0; i < count; ++i) {
737 intptr_t ptrdiff = decode_sleb128 (p, &p);
738 frames [i] = lookup_method (ptr_base + ptrdiff);
746 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
749 for (i = 0; i < num_tracked_objects; ++i) {
750 if (tracked_objects [i] != obj)
752 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
753 if (bt && bt->count) {
755 for (k = 0; k < bt->count; ++k)
756 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
762 track_move (uintptr_t src, uintptr_t dst)
765 for (i = 0; i < num_tracked_objects; ++i) {
766 if (tracked_objects [i] == src)
767 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
768 else if (tracked_objects [i] == dst)
769 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
774 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
777 for (i = 0; i < num_tracked_objects; ++i) {
778 if (tracked_objects [i] == obj)
779 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
784 found_object (uintptr_t obj)
786 num_tracked_objects ++;
787 tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
788 tracked_objects [num_tracked_objects - 1] = obj;
791 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
792 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
795 decode_buffer (ProfContext *ctx)
802 intptr_t method_base;
804 uint64_t file_offset;
806 ThreadContext *thread;
808 file_offset = ftell (ctx->file);
809 if (!load_data (ctx, 48))
812 if (read_int32 (p) != BUF_ID)
814 len = read_int32 (p + 4);
815 time_base = read_int64 (p + 8);
816 ptr_base = read_int64 (p + 16);
817 obj_base = read_int64 (p + 24);
818 thread_id = read_int64 (p + 32);
819 method_base = read_int64 (p + 40);
821 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
822 thread = load_thread (ctx, thread_id);
823 if (!load_data (ctx, len))
826 startup_time = time_base;
828 time_from += startup_time;
829 time_to += startup_time;
832 for (i = 0; i < thread->stack_id; ++i)
833 thread->stack [i]->recurse_count++;
839 int subtype = *p & 0xf0;
840 uint64_t tdiff = decode_uleb128 (p + 1, &p);
841 LOG_TIME (time_base, tdiff);
843 if (subtype == TYPE_GC_RESIZE) {
844 uint64_t new_size = decode_uleb128 (p, &p);
846 fprintf (outfile, "gc heap resized to %llu\n", new_size);
848 if (new_size > max_heap_size)
849 max_heap_size = new_size;
850 } else if (subtype == TYPE_GC_EVENT) {
851 uint64_t ev = decode_uleb128 (p, &p);
852 int gen = decode_uleb128 (p, &p);
854 fprintf (outfile, "gc event for gen%d: %s at %llu\n", gen - 1, gc_event_name (ev), time_base);
856 fprintf (outfile, "incorrect gc gen: %d\n", gen);
859 if (ev == MONO_GC_EVENT_START) {
860 gc_info [gen].start_time = time_base;
861 gc_info [gen].count++;
862 } else if (ev == MONO_GC_EVENT_END) {
863 tdiff = time_base - gc_info [gen].start_time;
864 gc_info [gen].total_time += tdiff;
865 if (tdiff > gc_info [gen].max_time)
866 gc_info [gen].max_time = tdiff;
868 } else if (subtype == TYPE_GC_MOVE) {
869 int j, num = decode_uleb128 (p, &p);
870 gc_object_moves += num / 2;
871 for (j = 0; j < num; j += 2) {
872 intptr_t obj1diff = decode_sleb128 (p, &p);
873 intptr_t obj2diff = decode_sleb128 (p, &p);
874 if (num_tracked_objects)
875 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
877 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
883 case TYPE_METADATA: {
884 int error = *p & TYPE_LOAD_ERR;
885 uint64_t tdiff = decode_uleb128 (p + 1, &p);
887 intptr_t ptrdiff = decode_sleb128 (p, &p);
888 LOG_TIME (time_base, tdiff);
890 if (mtype == TYPE_CLASS) {
891 intptr_t imptrdiff = decode_sleb128 (p, &p);
892 uint64_t flags = decode_uleb128 (p, &p);
894 fprintf (outfile, "non-zero flags in class\n");
898 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
900 add_class (ptr_base + ptrdiff, (char*)p);
903 } else if (mtype == TYPE_IMAGE) {
904 uint64_t flags = decode_uleb128 (p, &p);
906 fprintf (outfile, "non-zero flags in image\n");
910 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
912 add_image (ptr_base + ptrdiff, (char*)p);
919 int has_bt = *p & TYPE_ALLOC_BT;
920 uint64_t tdiff = decode_uleb128 (p + 1, &p);
921 intptr_t ptrdiff = decode_sleb128 (p, &p);
922 intptr_t objdiff = decode_sleb128 (p, &p);
925 MethodDesc* sframes [8];
926 MethodDesc** frames = sframes;
927 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
928 len = decode_uleb128 (p, &p);
929 LOG_TIME (time_base, tdiff);
932 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
935 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
937 fprintf (outfile, "Cannot load backtrace\n");
941 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
944 cd->alloc_size += len;
946 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
948 bt = add_trace_thread (thread, &cd->traces, len);
949 if (find_size && len >= find_size) {
950 if (!find_name || strstr (cd->name, find_name))
951 found_object (OBJ_ADDR (objdiff));
952 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
953 found_object (OBJ_ADDR (objdiff));
955 if (num_tracked_objects)
956 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
958 if (frames != sframes)
963 int subtype = *p & 0xf0;
964 uint64_t tdiff = decode_uleb128 (p + 1, &p);
965 int64_t ptrdiff = decode_sleb128 (p, &p);
966 LOG_TIME (time_base, tdiff);
968 method_base += ptrdiff;
969 if (subtype == TYPE_JIT) {
970 intptr_t codediff = decode_sleb128 (p, &p);
971 int codelen = decode_uleb128 (p, &p);
973 fprintf (outfile, "jitted method %p (%s), size: %d\n", (void*)(method_base), p, codelen);
974 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
979 if ((thread_filter && thread_filter != thread->thread_id))
981 method = lookup_method (method_base);
982 if (subtype == TYPE_ENTER) {
983 add_trace_thread (thread, &method->traces, 1);
984 push_method (thread, method, time_base);
986 pop_method (thread, method, time_base);
989 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
994 int subtype = *p & 0xf0;
995 if (subtype == TYPE_HEAP_OBJECT) {
997 intptr_t objdiff = decode_sleb128 (p + 1, &p);
998 intptr_t ptrdiff = decode_sleb128 (p, &p);
999 uint64_t size = decode_uleb128 (p, &p);
1000 int num = decode_uleb128 (p, &p);
1001 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1002 for (i = 0; i < num; ++i) {
1003 /* FIXME: use object distance to measure how good
1004 * the GC is at keeping related objects close
1006 intptr_t obj1diff = decode_sleb128 (p, &p);
1007 if (num_tracked_objects)
1008 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
1011 fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
1013 add_heap_shot_obj (current_heap_shot, cd, size);
1014 } else if (subtype == TYPE_HEAP_END) {
1015 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1016 LOG_TIME (time_base, tdiff);
1019 fprintf (outfile, "heap shot end\n");
1020 current_heap_shot = NULL;
1021 } else if (subtype == TYPE_HEAP_START) {
1022 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1023 LOG_TIME (time_base, tdiff);
1026 fprintf (outfile, "heap shot start\n");
1027 current_heap_shot = new_heap_shot (time_base);
1031 case TYPE_MONITOR: {
1032 int event = (*p >> 4) & 0x3;
1033 int has_bt = *p & TYPE_MONITOR_BT;
1034 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1035 intptr_t objdiff = decode_sleb128 (p, &p);
1036 MethodDesc* sframes [8];
1037 MethodDesc** frames = sframes;
1040 LOG_TIME (time_base, tdiff);
1042 record = (!thread_filter || thread_filter == thread->thread_id);
1043 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
1044 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
1046 monitor_contention++;
1047 mdesc->contentions++;
1048 thread->monitor = mdesc;
1049 thread->contention_start = time_base;
1053 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1055 fprintf (outfile, "Cannot load backtrace\n");
1059 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1062 add_trace_thread (thread, &mdesc->traces, 1);
1064 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1067 if (thread->monitor && thread->contention_start) {
1068 uint64_t wait_time = time_base - thread->contention_start;
1069 if (wait_time > thread->monitor->max_wait_time)
1070 thread->monitor->max_wait_time = wait_time;
1071 thread->monitor->wait_time += wait_time;
1072 thread->monitor = NULL;
1073 thread->contention_start = 0;
1076 } else if (event == MONO_PROFILER_MONITOR_DONE) {
1079 if (thread->monitor && thread->contention_start) {
1080 uint64_t wait_time = time_base - thread->contention_start;
1081 if (wait_time > thread->monitor->max_wait_time)
1082 thread->monitor->max_wait_time = wait_time;
1083 thread->monitor->wait_time += wait_time;
1084 thread->monitor = NULL;
1085 thread->contention_start = 0;
1090 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1091 if (frames != sframes)
1095 case TYPE_EXCEPTION: {
1096 int subtype = *p & 0x70;
1097 int has_bt = *p & TYPE_EXCEPTION_BT;
1098 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1099 MethodDesc* sframes [8];
1100 MethodDesc** frames = sframes;
1102 LOG_TIME (time_base, tdiff);
1104 record = (!thread_filter || thread_filter == thread->thread_id);
1105 if (subtype == TYPE_CLAUSE) {
1106 int clause_type = decode_uleb128 (p, &p);
1107 int clause_num = decode_uleb128 (p, &p);
1108 int64_t ptrdiff = decode_sleb128 (p, &p);
1109 method_base += ptrdiff;
1111 clause_summary [clause_type]++;
1113 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1115 intptr_t objdiff = decode_sleb128 (p, &p);
1120 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1122 fprintf (outfile, "Cannot load backtrace\n");
1126 add_trace_methods (frames, has_bt, &exc_traces, 1);
1129 add_trace_thread (thread, &exc_traces, 1);
1131 if (frames != sframes)
1134 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1139 fprintf (outfile, "unhandled profiler event: 0x%x\n", *p);
1143 thread->last_time = time_base;
1144 for (i = 0; i < thread->stack_id; ++i)
1145 thread->stack [i]->recurse_count = 0;
1150 load_file (char *name)
1153 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1154 if (strcmp (name, "-") == 0)
1157 ctx->file = fopen (name, "rb");
1159 printf ("Cannot open file: %s\n", name);
1162 #if defined (HAVE_ZLIB)
1163 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
1165 if (!load_data (ctx, 32))
1168 if (read_int32 (p) != LOG_HEADER_ID || p [6] != LOG_DATA_VERSION)
1170 ctx->version_major = p [4];
1171 ctx->version_minor = p [5];
1172 ctx->data_version = p [6];
1173 /* reading 64 bit files on 32 bit systems not supported yet */
1174 if (p [7] > sizeof (void*))
1176 if (read_int32 (p + 20)) /* flags must be 0 */
1178 ctx->startup_time = read_int64 (p + 8);
1179 ctx->timer_overhead = read_int32 (p + 16);
1187 static int alloc_sort_mode = ALLOC_SORT_BYTES;
1190 compare_class (const void *a, const void *b)
1192 ClassDesc *const*A = a;
1193 ClassDesc *const*B = b;
1194 uint64_t vala, valb;
1195 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1196 vala = (*A)->alloc_size;
1197 valb = (*B)->alloc_size;
1199 vala = (*A)->allocs;
1200 valb = (*B)->allocs;
1210 dump_header (ProfContext *ctx)
1212 time_t st = ctx->startup_time / 1000;
1213 char *t = ctime (&st);
1214 fprintf (outfile, "\nMono log profiler data\n");
1215 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
1216 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
1217 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
1218 fprintf (outfile, "\tProgram startup: %s\n", t);
1222 dump_traces (TraceDesc *traces, const char *desc)
1229 sort_context_array (traces);
1230 for (j = 0; j < traces->count; ++j) {
1233 bt = traces->traces [j].bt;
1236 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
1237 for (k = 0; k < bt->count; ++k)
1238 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
1243 dump_threads (ProfContext *ctx)
1245 ThreadContext *thread;
1246 fprintf (outfile, "\nThread summary\n");
1247 for (thread = ctx->threads; thread; thread = thread->next) {
1248 fprintf (outfile, "\tThread: %p\n", (void*)thread->thread_id);
1253 dump_exceptions (void)
1256 fprintf (outfile, "\nException summary\n");
1257 fprintf (outfile, "\tThrows: %llu\n", throw_count);
1258 dump_traces (&exc_traces, "throws");
1259 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
1260 if (!clause_summary [i])
1262 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
1267 compare_monitor (const void *a, const void *b)
1269 MonitorDesc *const*A = a;
1270 MonitorDesc *const*B = b;
1271 if ((*B)->wait_time == (*A)->wait_time)
1273 if ((*B)->wait_time < (*A)->wait_time)
1279 dump_monitors (void)
1281 MonitorDesc **monitors;
1285 monitors = malloc (sizeof (void*) * num_monitors);
1286 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
1287 MonitorDesc *mdesc = monitor_hash [i];
1289 monitors [j++] = mdesc;
1290 mdesc = mdesc->next;
1293 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
1294 fprintf (outfile, "\nMonitor lock summary\n");
1295 for (i = 0; i < num_monitors; ++i) {
1296 MonitorDesc *mdesc = monitors [i];
1297 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
1298 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
1299 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
1300 dump_traces (&mdesc->traces, "contentions");
1302 fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
1303 fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
1304 fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
1311 fprintf (outfile, "\nGC summary\n");
1312 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
1313 fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
1314 fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
1315 for (i = 0; i < 3; ++i) {
1316 if (!gc_info [i].count)
1318 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
1319 i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
1320 gc_info [i].total_time / gc_info [i].count / 1000);
1325 dump_allocations (void)
1328 intptr_t allocs = 0;
1330 int header_done = 0;
1331 ClassDesc **classes = malloc (num_classes * sizeof (void*));
1334 for (i = 0; i < HASH_SIZE; ++i) {
1335 cd = class_hash [i];
1341 qsort (classes, num_classes, sizeof (void*), compare_class);
1342 for (i = 0; i < num_classes; ++i) {
1346 allocs += cd->allocs;
1347 size += cd->alloc_size;
1348 if (!header_done++) {
1349 fprintf (outfile, "\nAllocation summary\n");
1350 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
1352 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
1353 dump_traces (&cd->traces, "bytes");
1356 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
1365 static int method_sort_mode = METHOD_SORT_TOTAL;
1368 compare_method (const void *a, const void *b)
1370 MethodDesc *const*A = a;
1371 MethodDesc *const*B = b;
1372 uint64_t vala, valb;
1373 if (method_sort_mode == METHOD_SORT_SELF) {
1374 vala = (*A)->self_time;
1375 valb = (*B)->self_time;
1376 } else if (method_sort_mode == METHOD_SORT_CALLS) {
1380 vala = (*A)->total_time;
1381 valb = (*B)->total_time;
1391 dump_metadata (void)
1393 fprintf (outfile, "\nMetadata summary\n");
1394 fprintf (outfile, "\tLoaded images: %d\n", num_images);
1398 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
1399 image = image_hash [i];
1401 fprintf (outfile, "\t\t%s\n", image->filename);
1402 image = image->next;
1414 int header_done = 0;
1415 MethodDesc **methods = malloc (num_methods * sizeof (void*));
1418 for (i = 0; i < HASH_SIZE; ++i) {
1419 cd = method_hash [i];
1421 cd->total_time = cd->self_time + cd->callee_time;
1426 qsort (methods, num_methods, sizeof (void*), compare_method);
1427 for (i = 0; i < num_methods; ++i) {
1434 msecs = cd->total_time / 1000000;
1435 smsecs = (cd->total_time - cd->callee_time) / 1000000;
1436 if (!msecs && !verbose)
1438 if (!header_done++) {
1439 fprintf (outfile, "\nMethod call summary\n");
1440 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
1442 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
1443 dump_traces (&cd->traces, "calls");
1446 fprintf (outfile, "Total calls: %llu\n", calls);
1450 compare_heap_class (const void *a, const void *b)
1452 HeapClassDesc *const*A = a;
1453 HeapClassDesc *const*B = b;
1454 uint64_t vala, valb;
1455 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1456 vala = (*A)->total_size;
1457 valb = (*B)->total_size;
1470 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
1477 HeapClassDesc **sorted;
1478 sorted = malloc (sizeof (void*) * hs->class_count);
1479 for (i = 0; i < hs->hash_size; ++i) {
1480 cd = hs->class_hash [i];
1484 size += cd->total_size;
1485 sorted [ccount++] = cd;
1487 hs->sorted = sorted;
1488 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
1489 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d\n",
1490 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount);
1491 if (!verbose && ccount > 30)
1493 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
1494 for (i = 0; i < ccount; ++i) {
1495 HeapClassDesc *ocd = NULL;
1498 ocd = heap_class_lookup (last_hs, cd->klass);
1499 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
1501 int64_t bdiff = cd->total_size - ocd->total_size;
1502 int64_t cdiff = cd->count - ocd->count;
1503 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
1505 fprintf (outfile, "\n");
1511 dump_heap_shots (void)
1514 HeapShot *last_hs = NULL;
1518 fprintf (outfile, "\nHeap shot summary\n");
1520 for (hs = heap_shots; hs; hs = hs->next) {
1521 heap_shot_summary (hs, i++, last_hs);
1527 flush_context (ProfContext *ctx)
1529 ThreadContext *thread;
1530 /* FIXME: sometimes there are leftovers: indagate */
1531 for (thread = ctx->threads; thread; thread = thread->next) {
1532 while (thread->stack_id) {
1534 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
1535 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
1540 static const char *reports = "header,gc,alloc,call,metadata,exception,monitor,thread,heapshot";
1543 match_option (const char *p, const char *opt)
1545 int len = strlen (opt);
1546 if (strncmp (p, opt, len) == 0) {
1555 print_reports (ProfContext *ctx, const char *reps, int parse_only)
1559 for (p = reps; *p; p = opt) {
1560 if ((opt = match_option (p, "header")) != p) {
1565 if ((opt = match_option (p, "thread")) != p) {
1570 if ((opt = match_option (p, "gc")) != p) {
1575 if ((opt = match_option (p, "alloc")) != p) {
1577 dump_allocations ();
1580 if ((opt = match_option (p, "call")) != p) {
1585 if ((opt = match_option (p, "metadata")) != p) {
1590 if ((opt = match_option (p, "exception")) != p) {
1595 if ((opt = match_option (p, "monitor")) != p) {
1600 if ((opt = match_option (p, "heapshot")) != p) {
1611 add_find_spec (const char *p)
1613 if (p [0] == 'S' && p [1] == ':') {
1615 find_size = strtoul (p + 2, &vale, 10);
1617 } else if (p [0] == 'T' && p [1] == ':') {
1627 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
1628 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
1629 printf ("FILENAME can be '-' to read from standard input.\n");
1630 printf ("Options:\n");
1631 printf ("\t--help display this help\n");
1632 printf ("\t--out=FILE write to FILE instead of stdout\n");
1633 printf ("\t--traces collect and show backtraces\n");
1634 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
1635 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
1636 printf ("\t %s\n", reports);
1637 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
1638 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
1639 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
1640 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
1641 printf ("\t S:minimum_size or T:partial_name\n");
1642 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
1643 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
1644 printf ("\t--verbose increase verbosity level\n");
1645 printf ("\t--debug display decoding debug info for mprof-report devs\n");
1649 main (int argc, char *argv[])
1654 for (i = 1; i < argc; ++i) {
1655 if (strcmp ("--debug", argv [i]) == 0) {
1657 } else if (strcmp ("--help", argv [i]) == 0) {
1660 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
1661 const char *val = argv [i] + 13;
1662 if (strcmp (val, "bytes") == 0) {
1663 alloc_sort_mode = ALLOC_SORT_BYTES;
1664 } else if (strcmp (val, "count") == 0) {
1665 alloc_sort_mode = ALLOC_SORT_COUNT;
1670 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
1671 const char *val = argv [i] + 14;
1672 if (strcmp (val, "total") == 0) {
1673 method_sort_mode = METHOD_SORT_TOTAL;
1674 } else if (strcmp (val, "self") == 0) {
1675 method_sort_mode = METHOD_SORT_SELF;
1676 } else if (strcmp (val, "calls") == 0) {
1677 method_sort_mode = METHOD_SORT_CALLS;
1682 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
1683 const char *val = argv [i] + 10;
1684 if (!print_reports (NULL, val, 1)) {
1689 } else if (strncmp ("--out=", argv [i], 6) == 0) {
1690 const char *val = argv [i] + 6;
1691 outfile = fopen (val, "w");
1693 printf ("Cannot open output file: %s\n", val);
1696 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
1697 const char *val = argv [i] + 12;
1699 trace_max = strtoul (val, &vale, 10);
1700 } else if (strncmp ("--find=", argv [i], 7) == 0) {
1701 const char *val = argv [i] + 7;
1702 if (!add_find_spec (val)) {
1706 } else if (strncmp ("--track=", argv [i], 8) == 0) {
1707 const char *val = argv [i] + 8;
1710 uintptr_t tracked_obj;
1715 tracked_obj = strtoul (val, &vale, 0);
1716 found_object (tracked_obj);
1719 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
1720 const char *val = argv [i] + 9;
1722 thread_filter = strtoul (val, &vale, 0);
1723 } else if (strncmp ("--time=", argv [i], 7) == 0) {
1724 char *val = pstrdup (argv [i] + 7);
1725 double from_secs, to_secs;
1726 char *top = strchr (val, '-');
1732 from_secs = atof (val);
1733 to_secs = atof (top);
1735 if (from_secs > to_secs) {
1739 time_from = from_secs * 1000000000;
1740 time_to = to_secs * 1000000000;
1741 } else if (strcmp ("--verbose", argv [i]) == 0) {
1743 } else if (strcmp ("--traces", argv [i]) == 0) {
1754 ctx = load_file (argv [i]);
1756 printf ("Not a log profiler data file (or unsupported version).\n");
1759 while (decode_buffer (ctx));
1760 flush_context (ctx);
1761 if (num_tracked_objects)
1763 print_reports (ctx, reports, 0);