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 {
430 uint64_t *time_stack;
431 uint64_t *callee_time_stack;
433 uint64_t contention_start;
434 MonitorDesc *monitor;
440 ensure_buffer (ProfContext *ctx, int size)
442 if (ctx->size < size) {
443 ctx->buf = realloc (ctx->buf, size);
449 load_data (ProfContext *ctx, int size)
451 ensure_buffer (ctx, size);
452 #if defined (HAVE_ZLIB)
454 return gzread (ctx->gzfile, ctx->buf, size) == size;
457 return fread (ctx->buf, size, 1, ctx->file);
460 static ThreadContext*
461 get_thread (ProfContext *ctx, intptr_t thread_id)
463 ThreadContext *thread;
464 if (ctx->current && ctx->current->thread_id == thread_id)
466 thread = ctx->threads;
468 if (thread->thread_id == thread_id) {
471 thread = thread->next;
473 thread = calloc (sizeof (ThreadContext), 1);
474 thread->next = ctx->threads;
475 ctx->threads = thread;
476 thread->thread_id = thread_id;
477 thread->last_time = 0;
478 thread->stack_id = 0;
479 thread->stack_size = 32;
480 thread->stack = malloc (thread->stack_size * sizeof (void*));
481 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
482 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
486 static ThreadContext*
487 load_thread (ProfContext *ctx, intptr_t thread_id)
489 ThreadContext *thread = get_thread (ctx, thread_id);
490 ctx->current = thread;
495 ensure_thread_stack (ThreadContext *thread)
497 if (thread->stack_id == thread->stack_size) {
498 thread->stack_size *= 2;
499 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
500 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
501 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
506 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
509 unsigned int start_pos;
510 start_pos = bt->hash % size;
513 if (traces [i].bt == bt) {
514 traces [i].count += value;
516 } else if (!traces [i].bt) {
518 traces [i].count += value;
524 } while (i != start_pos);
525 /* should not happen */
526 printf ("failed trace store\n");
531 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
536 if (trace->count * 2 >= trace->size) {
538 int old_size = trace->size;
540 if (trace->size == 0)
542 n = calloc (sizeof (CallContext) * trace->size, 1);
543 for (i = 0; i < old_size; ++i) {
544 if (trace->traces [i].bt)
545 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
548 free (trace->traces);
551 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
555 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
558 int count = thread->stack_id;
561 if (count > trace_max)
563 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
564 add_trace_bt (bt, trace, value);
569 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
574 if (count > trace_max)
576 bt = add_backtrace (count, methods);
577 add_trace_bt (bt, trace, value);
582 compare_callc (const void *a, const void *b)
584 const CallContext *A = a;
585 const CallContext *B = b;
586 if (B->count == A->count)
588 if (B->count < A->count)
594 sort_context_array (TraceDesc* traces)
597 for (i = 0, j = 0; i < traces->size; ++i) {
598 if (traces->traces [i].bt) {
599 traces->traces [j].bt = traces->traces [i].bt;
600 traces->traces [j].count = traces->traces [i].count;
604 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
608 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
610 ensure_thread_stack (thread);
611 thread->time_stack [thread->stack_id] = timestamp;
612 thread->callee_time_stack [thread->stack_id] = 0;
613 thread->stack [thread->stack_id++] = method;
614 method->recurse_count++;
618 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
620 method->recurse_count--;
621 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
625 if (timestamp < thread->time_stack [thread->stack_id])
626 fprintf (outfile, "time went backwards for %s\n", method->name);
627 tdiff = timestamp - thread->time_stack [thread->stack_id];
628 if (thread->callee_time_stack [thread->stack_id] > tdiff)
629 fprintf (outfile, "callee time bigger for %s\n", method->name);
630 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
631 method->callee_time += thread->callee_time_stack [thread->stack_id];
632 if (thread->stack_id)
633 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
634 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
636 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
641 uint64_t start_time; /* move this per-thread? */
646 static GCDesc gc_info [3];
647 static uint64_t max_heap_size;
648 static uint64_t gc_object_moves;
649 static int gc_resizes;
656 static HandleInfo handle_info [4];
659 gc_event_name (int ev)
662 case MONO_GC_EVENT_START: return "start";
663 case MONO_GC_EVENT_MARK_START: return "mark start";
664 case MONO_GC_EVENT_MARK_END: return "mark end";
665 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
666 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
667 case MONO_GC_EVENT_END: return "end";
668 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
669 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
670 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
671 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
677 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
678 static uint64_t throw_count = 0;
679 static TraceDesc exc_traces;
682 clause_name (int type)
685 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
686 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
687 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
688 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
689 default: return "invalid";
693 static uint64_t monitor_contention;
694 static uint64_t monitor_failed;
695 static uint64_t monitor_acquired;
697 struct _MonitorDesc {
700 uintptr_t contentions;
702 uint64_t max_wait_time;
706 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
707 static int num_monitors = 0;
710 lookup_monitor (uintptr_t objid)
712 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
713 MonitorDesc *cd = monitor_hash [slot];
714 while (cd && cd->objid != objid)
717 cd = calloc (sizeof (MonitorDesc), 1);
719 cd->next = monitor_hash [slot];
720 monitor_hash [slot] = cd;
727 monitor_ev_name (int ev)
730 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
731 case MONO_PROFILER_MONITOR_DONE: return "acquired";
732 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
733 default: return "invalid";
738 get_handle_name (int htype)
741 case 0: return "weak";
742 case 1: return "weaktrack";
743 case 2: return "normal";
744 case 3: return "pinned";
745 default: return "unknown";
750 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
754 int flags = decode_uleb128 (p, &p);
755 int count = decode_uleb128 (p, &p);
759 frames = malloc (count * sizeof (void*));
762 for (i = 0; i < count; ++i) {
763 intptr_t ptrdiff = decode_sleb128 (p, &p);
764 frames [i] = lookup_method (ptr_base + ptrdiff);
772 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
775 for (i = 0; i < num_tracked_objects; ++i) {
776 if (tracked_objects [i] != obj)
778 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
779 if (bt && bt->count) {
781 for (k = 0; k < bt->count; ++k)
782 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
788 track_handle (uintptr_t obj, int htype, uint32_t handle)
791 for (i = 0; i < num_tracked_objects; ++i) {
792 if (tracked_objects [i] == obj)
793 fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
798 track_move (uintptr_t src, uintptr_t dst)
801 for (i = 0; i < num_tracked_objects; ++i) {
802 if (tracked_objects [i] == src)
803 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
804 else if (tracked_objects [i] == dst)
805 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
810 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
813 for (i = 0; i < num_tracked_objects; ++i) {
814 if (tracked_objects [i] == obj)
815 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
820 found_object (uintptr_t obj)
822 num_tracked_objects ++;
823 tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
824 tracked_objects [num_tracked_objects - 1] = obj;
827 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
828 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
831 decode_buffer (ProfContext *ctx)
838 intptr_t method_base;
840 uint64_t file_offset;
842 ThreadContext *thread;
844 file_offset = ftell (ctx->file);
845 if (!load_data (ctx, 48))
848 if (read_int32 (p) != BUF_ID)
850 len = read_int32 (p + 4);
851 time_base = read_int64 (p + 8);
852 ptr_base = read_int64 (p + 16);
853 obj_base = read_int64 (p + 24);
854 thread_id = read_int64 (p + 32);
855 method_base = read_int64 (p + 40);
857 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
858 thread = load_thread (ctx, thread_id);
859 if (!load_data (ctx, len))
862 startup_time = time_base;
864 time_from += startup_time;
865 time_to += startup_time;
868 thread->name = pstrdup ("Main");
870 for (i = 0; i < thread->stack_id; ++i)
871 thread->stack [i]->recurse_count++;
877 int subtype = *p & 0xf0;
878 uint64_t tdiff = decode_uleb128 (p + 1, &p);
879 LOG_TIME (time_base, tdiff);
881 if (subtype == TYPE_GC_RESIZE) {
882 uint64_t new_size = decode_uleb128 (p, &p);
884 fprintf (outfile, "gc heap resized to %llu\n", new_size);
886 if (new_size > max_heap_size)
887 max_heap_size = new_size;
888 } else if (subtype == TYPE_GC_EVENT) {
889 uint64_t ev = decode_uleb128 (p, &p);
890 int gen = decode_uleb128 (p, &p);
892 fprintf (outfile, "gc event for gen%d: %s at %llu\n", gen - 1, gc_event_name (ev), time_base);
894 fprintf (outfile, "incorrect gc gen: %d\n", gen);
897 if (ev == MONO_GC_EVENT_START) {
898 gc_info [gen].start_time = time_base;
899 gc_info [gen].count++;
900 } else if (ev == MONO_GC_EVENT_END) {
901 tdiff = time_base - gc_info [gen].start_time;
902 gc_info [gen].total_time += tdiff;
903 if (tdiff > gc_info [gen].max_time)
904 gc_info [gen].max_time = tdiff;
906 } else if (subtype == TYPE_GC_MOVE) {
907 int j, num = decode_uleb128 (p, &p);
908 gc_object_moves += num / 2;
909 for (j = 0; j < num; j += 2) {
910 intptr_t obj1diff = decode_sleb128 (p, &p);
911 intptr_t obj2diff = decode_sleb128 (p, &p);
912 if (num_tracked_objects)
913 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
915 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
918 } else if (subtype == TYPE_GC_HANDLE_CREATED) {
919 int htype = decode_uleb128 (p, &p);
920 uint32_t handle = decode_uleb128 (p, &p);
921 intptr_t objdiff = decode_sleb128 (p, &p);
924 handle_info [htype].created++;
925 add_trace_thread (thread, &handle_info [htype].traces, 1);
926 /* FIXME: we don't take into account timing here */
927 if (handle_info [htype].created > handle_info [htype].max_live)
928 handle_info [htype].max_live = handle_info [htype].created;
929 if (num_tracked_objects)
930 track_handle (OBJ_ADDR (objdiff), htype, handle);
932 fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
933 } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
934 int htype = decode_uleb128 (p, &p);
935 uint32_t handle = decode_uleb128 (p, &p);
938 handle_info [htype].created--;
940 fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
944 case TYPE_METADATA: {
945 int error = *p & TYPE_LOAD_ERR;
946 uint64_t tdiff = decode_uleb128 (p + 1, &p);
948 intptr_t ptrdiff = decode_sleb128 (p, &p);
949 LOG_TIME (time_base, tdiff);
951 if (mtype == TYPE_CLASS) {
952 intptr_t imptrdiff = decode_sleb128 (p, &p);
953 uint64_t flags = decode_uleb128 (p, &p);
955 fprintf (outfile, "non-zero flags in class\n");
959 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
961 add_class (ptr_base + ptrdiff, (char*)p);
964 } else if (mtype == TYPE_IMAGE) {
965 uint64_t flags = decode_uleb128 (p, &p);
967 fprintf (outfile, "non-zero flags in image\n");
971 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
973 add_image (ptr_base + ptrdiff, (char*)p);
976 } else if (mtype == TYPE_THREAD) {
978 uint64_t flags = decode_uleb128 (p, &p);
980 fprintf (outfile, "non-zero flags in thread\n");
983 nt = get_thread (ctx, ptr_base * ptrdiff);
984 nt->name = pstrdup ((char*)p);
986 fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
993 int has_bt = *p & TYPE_ALLOC_BT;
994 uint64_t tdiff = decode_uleb128 (p + 1, &p);
995 intptr_t ptrdiff = decode_sleb128 (p, &p);
996 intptr_t objdiff = decode_sleb128 (p, &p);
999 MethodDesc* sframes [8];
1000 MethodDesc** frames = sframes;
1001 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1002 len = decode_uleb128 (p, &p);
1003 LOG_TIME (time_base, tdiff);
1006 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
1009 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1011 fprintf (outfile, "Cannot load backtrace\n");
1015 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
1018 cd->alloc_size += len;
1020 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
1022 bt = add_trace_thread (thread, &cd->traces, len);
1023 if (find_size && len >= find_size) {
1024 if (!find_name || strstr (cd->name, find_name))
1025 found_object (OBJ_ADDR (objdiff));
1026 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
1027 found_object (OBJ_ADDR (objdiff));
1029 if (num_tracked_objects)
1030 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
1032 if (frames != sframes)
1037 int subtype = *p & 0xf0;
1038 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1039 int64_t ptrdiff = decode_sleb128 (p, &p);
1040 LOG_TIME (time_base, tdiff);
1042 method_base += ptrdiff;
1043 if (subtype == TYPE_JIT) {
1044 intptr_t codediff = decode_sleb128 (p, &p);
1045 int codelen = decode_uleb128 (p, &p);
1047 fprintf (outfile, "jitted method %p (%s), size: %d\n", (void*)(method_base), p, codelen);
1048 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
1053 if ((thread_filter && thread_filter != thread->thread_id))
1055 method = lookup_method (method_base);
1056 if (subtype == TYPE_ENTER) {
1057 add_trace_thread (thread, &method->traces, 1);
1058 push_method (thread, method, time_base);
1060 pop_method (thread, method, time_base);
1063 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
1068 int subtype = *p & 0xf0;
1069 if (subtype == TYPE_HEAP_OBJECT) {
1071 intptr_t objdiff = decode_sleb128 (p + 1, &p);
1072 intptr_t ptrdiff = decode_sleb128 (p, &p);
1073 uint64_t size = decode_uleb128 (p, &p);
1074 int num = decode_uleb128 (p, &p);
1075 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1076 for (i = 0; i < num; ++i) {
1077 /* FIXME: use object distance to measure how good
1078 * the GC is at keeping related objects close
1080 intptr_t obj1diff = decode_sleb128 (p, &p);
1081 if (num_tracked_objects)
1082 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
1085 fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
1087 add_heap_shot_obj (current_heap_shot, cd, size);
1088 } else if (subtype == TYPE_HEAP_END) {
1089 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1090 LOG_TIME (time_base, tdiff);
1093 fprintf (outfile, "heap shot end\n");
1094 current_heap_shot = NULL;
1095 } else if (subtype == TYPE_HEAP_START) {
1096 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1097 LOG_TIME (time_base, tdiff);
1100 fprintf (outfile, "heap shot start\n");
1101 current_heap_shot = new_heap_shot (time_base);
1105 case TYPE_MONITOR: {
1106 int event = (*p >> 4) & 0x3;
1107 int has_bt = *p & TYPE_MONITOR_BT;
1108 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1109 intptr_t objdiff = decode_sleb128 (p, &p);
1110 MethodDesc* sframes [8];
1111 MethodDesc** frames = sframes;
1114 LOG_TIME (time_base, tdiff);
1116 record = (!thread_filter || thread_filter == thread->thread_id);
1117 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
1118 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
1120 monitor_contention++;
1121 mdesc->contentions++;
1122 thread->monitor = mdesc;
1123 thread->contention_start = time_base;
1127 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1129 fprintf (outfile, "Cannot load backtrace\n");
1133 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1136 add_trace_thread (thread, &mdesc->traces, 1);
1138 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1141 if (thread->monitor && thread->contention_start) {
1142 uint64_t wait_time = time_base - thread->contention_start;
1143 if (wait_time > thread->monitor->max_wait_time)
1144 thread->monitor->max_wait_time = wait_time;
1145 thread->monitor->wait_time += wait_time;
1146 thread->monitor = NULL;
1147 thread->contention_start = 0;
1150 } else if (event == MONO_PROFILER_MONITOR_DONE) {
1153 if (thread->monitor && thread->contention_start) {
1154 uint64_t wait_time = time_base - thread->contention_start;
1155 if (wait_time > thread->monitor->max_wait_time)
1156 thread->monitor->max_wait_time = wait_time;
1157 thread->monitor->wait_time += wait_time;
1158 thread->monitor = NULL;
1159 thread->contention_start = 0;
1164 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1165 if (frames != sframes)
1169 case TYPE_EXCEPTION: {
1170 int subtype = *p & 0x70;
1171 int has_bt = *p & TYPE_EXCEPTION_BT;
1172 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1173 MethodDesc* sframes [8];
1174 MethodDesc** frames = sframes;
1176 LOG_TIME (time_base, tdiff);
1178 record = (!thread_filter || thread_filter == thread->thread_id);
1179 if (subtype == TYPE_CLAUSE) {
1180 int clause_type = decode_uleb128 (p, &p);
1181 int clause_num = decode_uleb128 (p, &p);
1182 int64_t ptrdiff = decode_sleb128 (p, &p);
1183 method_base += ptrdiff;
1185 clause_summary [clause_type]++;
1187 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1189 intptr_t objdiff = decode_sleb128 (p, &p);
1194 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1196 fprintf (outfile, "Cannot load backtrace\n");
1200 add_trace_methods (frames, has_bt, &exc_traces, 1);
1203 add_trace_thread (thread, &exc_traces, 1);
1205 if (frames != sframes)
1208 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1213 fprintf (outfile, "unhandled profiler event: 0x%x\n", *p);
1217 thread->last_time = time_base;
1218 for (i = 0; i < thread->stack_id; ++i)
1219 thread->stack [i]->recurse_count = 0;
1224 load_file (char *name)
1227 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1228 if (strcmp (name, "-") == 0)
1231 ctx->file = fopen (name, "rb");
1233 printf ("Cannot open file: %s\n", name);
1236 #if defined (HAVE_ZLIB)
1237 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
1239 if (!load_data (ctx, 32))
1242 if (read_int32 (p) != LOG_HEADER_ID || p [6] != LOG_DATA_VERSION)
1244 ctx->version_major = p [4];
1245 ctx->version_minor = p [5];
1246 ctx->data_version = p [6];
1247 /* reading 64 bit files on 32 bit systems not supported yet */
1248 if (p [7] > sizeof (void*))
1250 if (read_int32 (p + 20)) /* flags must be 0 */
1252 ctx->startup_time = read_int64 (p + 8);
1253 ctx->timer_overhead = read_int32 (p + 16);
1261 static int alloc_sort_mode = ALLOC_SORT_BYTES;
1264 compare_class (const void *a, const void *b)
1266 ClassDesc *const*A = a;
1267 ClassDesc *const*B = b;
1268 uint64_t vala, valb;
1269 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1270 vala = (*A)->alloc_size;
1271 valb = (*B)->alloc_size;
1273 vala = (*A)->allocs;
1274 valb = (*B)->allocs;
1284 dump_header (ProfContext *ctx)
1286 time_t st = ctx->startup_time / 1000;
1287 char *t = ctime (&st);
1288 fprintf (outfile, "\nMono log profiler data\n");
1289 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
1290 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
1291 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
1292 fprintf (outfile, "\tProgram startup: %s\n", t);
1296 dump_traces (TraceDesc *traces, const char *desc)
1303 sort_context_array (traces);
1304 for (j = 0; j < traces->count; ++j) {
1307 bt = traces->traces [j].bt;
1310 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
1311 for (k = 0; k < bt->count; ++k)
1312 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
1317 dump_threads (ProfContext *ctx)
1319 ThreadContext *thread;
1320 fprintf (outfile, "\nThread summary\n");
1321 for (thread = ctx->threads; thread; thread = thread->next) {
1322 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
1327 dump_exceptions (void)
1330 fprintf (outfile, "\nException summary\n");
1331 fprintf (outfile, "\tThrows: %llu\n", throw_count);
1332 dump_traces (&exc_traces, "throws");
1333 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
1334 if (!clause_summary [i])
1336 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
1341 compare_monitor (const void *a, const void *b)
1343 MonitorDesc *const*A = a;
1344 MonitorDesc *const*B = b;
1345 if ((*B)->wait_time == (*A)->wait_time)
1347 if ((*B)->wait_time < (*A)->wait_time)
1353 dump_monitors (void)
1355 MonitorDesc **monitors;
1359 monitors = malloc (sizeof (void*) * num_monitors);
1360 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
1361 MonitorDesc *mdesc = monitor_hash [i];
1363 monitors [j++] = mdesc;
1364 mdesc = mdesc->next;
1367 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
1368 fprintf (outfile, "\nMonitor lock summary\n");
1369 for (i = 0; i < num_monitors; ++i) {
1370 MonitorDesc *mdesc = monitors [i];
1371 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
1372 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
1373 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
1374 dump_traces (&mdesc->traces, "contentions");
1376 fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
1377 fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
1378 fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
1385 fprintf (outfile, "\nGC summary\n");
1386 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
1387 fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
1388 fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
1389 for (i = 0; i < 3; ++i) {
1390 if (!gc_info [i].count)
1392 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
1393 i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
1394 gc_info [i].total_time / gc_info [i].count / 1000);
1396 for (i = 0; i < 3; ++i) {
1397 if (!handle_info [i].max_live)
1399 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
1400 get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
1401 dump_traces (&handle_info [i].traces, "created");
1406 dump_allocations (void)
1409 intptr_t allocs = 0;
1411 int header_done = 0;
1412 ClassDesc **classes = malloc (num_classes * sizeof (void*));
1415 for (i = 0; i < HASH_SIZE; ++i) {
1416 cd = class_hash [i];
1422 qsort (classes, num_classes, sizeof (void*), compare_class);
1423 for (i = 0; i < num_classes; ++i) {
1427 allocs += cd->allocs;
1428 size += cd->alloc_size;
1429 if (!header_done++) {
1430 fprintf (outfile, "\nAllocation summary\n");
1431 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
1433 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
1434 dump_traces (&cd->traces, "bytes");
1437 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
1446 static int method_sort_mode = METHOD_SORT_TOTAL;
1449 compare_method (const void *a, const void *b)
1451 MethodDesc *const*A = a;
1452 MethodDesc *const*B = b;
1453 uint64_t vala, valb;
1454 if (method_sort_mode == METHOD_SORT_SELF) {
1455 vala = (*A)->self_time;
1456 valb = (*B)->self_time;
1457 } else if (method_sort_mode == METHOD_SORT_CALLS) {
1461 vala = (*A)->total_time;
1462 valb = (*B)->total_time;
1472 dump_metadata (void)
1474 fprintf (outfile, "\nMetadata summary\n");
1475 fprintf (outfile, "\tLoaded images: %d\n", num_images);
1479 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
1480 image = image_hash [i];
1482 fprintf (outfile, "\t\t%s\n", image->filename);
1483 image = image->next;
1495 int header_done = 0;
1496 MethodDesc **methods = malloc (num_methods * sizeof (void*));
1499 for (i = 0; i < HASH_SIZE; ++i) {
1500 cd = method_hash [i];
1502 cd->total_time = cd->self_time + cd->callee_time;
1507 qsort (methods, num_methods, sizeof (void*), compare_method);
1508 for (i = 0; i < num_methods; ++i) {
1515 msecs = cd->total_time / 1000000;
1516 smsecs = (cd->total_time - cd->callee_time) / 1000000;
1517 if (!msecs && !verbose)
1519 if (!header_done++) {
1520 fprintf (outfile, "\nMethod call summary\n");
1521 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
1523 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
1524 dump_traces (&cd->traces, "calls");
1527 fprintf (outfile, "Total calls: %llu\n", calls);
1531 compare_heap_class (const void *a, const void *b)
1533 HeapClassDesc *const*A = a;
1534 HeapClassDesc *const*B = b;
1535 uint64_t vala, valb;
1536 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1537 vala = (*A)->total_size;
1538 valb = (*B)->total_size;
1551 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
1558 HeapClassDesc **sorted;
1559 sorted = malloc (sizeof (void*) * hs->class_count);
1560 for (i = 0; i < hs->hash_size; ++i) {
1561 cd = hs->class_hash [i];
1565 size += cd->total_size;
1566 sorted [ccount++] = cd;
1568 hs->sorted = sorted;
1569 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
1570 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d\n",
1571 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount);
1572 if (!verbose && ccount > 30)
1574 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
1575 for (i = 0; i < ccount; ++i) {
1576 HeapClassDesc *ocd = NULL;
1579 ocd = heap_class_lookup (last_hs, cd->klass);
1580 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
1582 int64_t bdiff = cd->total_size - ocd->total_size;
1583 int64_t cdiff = cd->count - ocd->count;
1584 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
1586 fprintf (outfile, "\n");
1592 dump_heap_shots (void)
1595 HeapShot *last_hs = NULL;
1599 fprintf (outfile, "\nHeap shot summary\n");
1601 for (hs = heap_shots; hs; hs = hs->next) {
1602 heap_shot_summary (hs, i++, last_hs);
1608 flush_context (ProfContext *ctx)
1610 ThreadContext *thread;
1611 /* FIXME: sometimes there are leftovers: indagate */
1612 for (thread = ctx->threads; thread; thread = thread->next) {
1613 while (thread->stack_id) {
1615 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
1616 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
1621 static const char *reports = "header,gc,alloc,call,metadata,exception,monitor,thread,heapshot";
1624 match_option (const char *p, const char *opt)
1626 int len = strlen (opt);
1627 if (strncmp (p, opt, len) == 0) {
1636 print_reports (ProfContext *ctx, const char *reps, int parse_only)
1640 for (p = reps; *p; p = opt) {
1641 if ((opt = match_option (p, "header")) != p) {
1646 if ((opt = match_option (p, "thread")) != p) {
1651 if ((opt = match_option (p, "gc")) != p) {
1656 if ((opt = match_option (p, "alloc")) != p) {
1658 dump_allocations ();
1661 if ((opt = match_option (p, "call")) != p) {
1666 if ((opt = match_option (p, "metadata")) != p) {
1671 if ((opt = match_option (p, "exception")) != p) {
1676 if ((opt = match_option (p, "monitor")) != p) {
1681 if ((opt = match_option (p, "heapshot")) != p) {
1692 add_find_spec (const char *p)
1694 if (p [0] == 'S' && p [1] == ':') {
1696 find_size = strtoul (p + 2, &vale, 10);
1698 } else if (p [0] == 'T' && p [1] == ':') {
1708 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
1709 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
1710 printf ("FILENAME can be '-' to read from standard input.\n");
1711 printf ("Options:\n");
1712 printf ("\t--help display this help\n");
1713 printf ("\t--out=FILE write to FILE instead of stdout\n");
1714 printf ("\t--traces collect and show backtraces\n");
1715 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
1716 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
1717 printf ("\t %s\n", reports);
1718 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
1719 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
1720 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
1721 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
1722 printf ("\t S:minimum_size or T:partial_name\n");
1723 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
1724 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
1725 printf ("\t--verbose increase verbosity level\n");
1726 printf ("\t--debug display decoding debug info for mprof-report devs\n");
1730 main (int argc, char *argv[])
1735 for (i = 1; i < argc; ++i) {
1736 if (strcmp ("--debug", argv [i]) == 0) {
1738 } else if (strcmp ("--help", argv [i]) == 0) {
1741 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
1742 const char *val = argv [i] + 13;
1743 if (strcmp (val, "bytes") == 0) {
1744 alloc_sort_mode = ALLOC_SORT_BYTES;
1745 } else if (strcmp (val, "count") == 0) {
1746 alloc_sort_mode = ALLOC_SORT_COUNT;
1751 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
1752 const char *val = argv [i] + 14;
1753 if (strcmp (val, "total") == 0) {
1754 method_sort_mode = METHOD_SORT_TOTAL;
1755 } else if (strcmp (val, "self") == 0) {
1756 method_sort_mode = METHOD_SORT_SELF;
1757 } else if (strcmp (val, "calls") == 0) {
1758 method_sort_mode = METHOD_SORT_CALLS;
1763 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
1764 const char *val = argv [i] + 10;
1765 if (!print_reports (NULL, val, 1)) {
1770 } else if (strncmp ("--out=", argv [i], 6) == 0) {
1771 const char *val = argv [i] + 6;
1772 outfile = fopen (val, "w");
1774 printf ("Cannot open output file: %s\n", val);
1777 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
1778 const char *val = argv [i] + 12;
1780 trace_max = strtoul (val, &vale, 10);
1781 } else if (strncmp ("--find=", argv [i], 7) == 0) {
1782 const char *val = argv [i] + 7;
1783 if (!add_find_spec (val)) {
1787 } else if (strncmp ("--track=", argv [i], 8) == 0) {
1788 const char *val = argv [i] + 8;
1791 uintptr_t tracked_obj;
1796 tracked_obj = strtoul (val, &vale, 0);
1797 found_object (tracked_obj);
1800 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
1801 const char *val = argv [i] + 9;
1803 thread_filter = strtoul (val, &vale, 0);
1804 } else if (strncmp ("--time=", argv [i], 7) == 0) {
1805 char *val = pstrdup (argv [i] + 7);
1806 double from_secs, to_secs;
1807 char *top = strchr (val, '-');
1813 from_secs = atof (val);
1814 to_secs = atof (top);
1816 if (from_secs > to_secs) {
1820 time_from = from_secs * 1000000000;
1821 time_to = to_secs * 1000000000;
1822 } else if (strcmp ("--verbose", argv [i]) == 0) {
1824 } else if (strcmp ("--traces", argv [i]) == 0) {
1835 ctx = load_file (argv [i]);
1837 printf ("Not a log profiler data file (or unsupported version).\n");
1840 while (decode_buffer (ctx));
1841 flush_context (ctx);
1842 if (num_tracked_objects)
1844 print_reports (ctx, reports, 0);