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__)
19 #if defined (HAVE_ZLIB)
22 #include <mono/metadata/profiler.h>
23 #include <mono/metadata/object.h>
24 #include <mono/metadata/debug-helpers.h>
26 #define HASH_SIZE 9371
27 #define SMALL_HASH_SIZE 31
30 static int collect_traces = 0;
31 static int show_traces = 0;
32 static int trace_max = 6;
33 static int verbose = 0;
34 static uintptr_t tracked_obj = 0;
35 static uintptr_t thread_filter = 0;
36 static uint64_t time_from = 0;
37 static uint64_t time_to = 0xffffffffffffffffULL;
38 static uint64_t startup_time = 0;
39 static FILE* outfile = NULL;
42 read_int32 (unsigned char *p)
46 value |= (*p++) << 16;
47 value |= (uint32_t)(*p++) << 24;
52 read_int64 (unsigned char *p)
54 uint64_t value = *p++;
56 value |= (*p++) << 16;
57 value |= (uint64_t)(*p++) << 24;
58 value |= (uint64_t)(*p++) << 32;
59 value |= (uint64_t)(*p++) << 40;
60 value |= (uint64_t)(*p++) << 48;
61 value |= (uint64_t)(*p++) << 54;
66 pstrdup (const char *s)
68 int len = strlen (s) + 1;
69 char *p = malloc (len);
74 static int num_images;
75 typedef struct _ImageDesc ImageDesc;
82 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
85 add_image (intptr_t image, char *name)
87 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
88 ImageDesc *cd = malloc (sizeof (ImageDesc));
90 cd->filename = pstrdup (name);
91 cd->next = image_hash [slot];
92 image_hash [slot] = cd;
96 typedef struct _BackTrace BackTrace;
108 typedef struct _ClassDesc ClassDesc;
118 static ClassDesc* class_hash [HASH_SIZE] = {0};
119 static int num_classes = 0;
122 add_class (intptr_t klass, const char *name)
124 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
126 cd = class_hash [slot];
127 while (cd && cd->klass != klass)
129 /* we resolved an unknown class (unless we had the code unloaded) */
131 /*printf ("resolved unknown: %s\n", name);*/
133 cd->name = pstrdup (name);
136 cd = calloc (sizeof (ClassDesc), 1);
138 cd->name = pstrdup (name);
139 cd->next = class_hash [slot];
142 cd->traces.count = 0;
144 cd->traces.traces = NULL;
145 class_hash [slot] = cd;
151 lookup_class (intptr_t klass)
153 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
154 ClassDesc *cd = class_hash [slot];
155 while (cd && cd->klass != klass)
158 return add_class (klass, "unresolved class");
162 typedef struct _MethodDesc MethodDesc;
172 uint64_t callee_time;
177 static MethodDesc* method_hash [HASH_SIZE] = {0};
178 static int num_methods = 0;
181 add_method (intptr_t method, const char *name, intptr_t code, int len)
183 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
185 cd = method_hash [slot];
186 while (cd && cd->method != method)
188 /* we resolved an unknown method (unless we had the code unloaded) */
192 /*printf ("resolved unknown: %s\n", name);*/
194 cd->name = pstrdup (name);
197 cd = calloc (sizeof (MethodDesc), 1);
199 cd->name = pstrdup (name);
204 cd->traces.count = 0;
206 cd->traces.traces = NULL;
207 cd->next = method_hash [slot];
208 method_hash [slot] = cd;
214 lookup_method (intptr_t method)
216 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
217 MethodDesc *cd = method_hash [slot];
218 while (cd && cd->method != method)
221 return add_method (method, "unknown method", 0, 0);
231 typedef struct _HeapShot HeapShot;
237 HeapClassDesc **class_hash;
238 HeapClassDesc **sorted;
241 static HeapShot *heap_shots = NULL;
242 static HeapShot *current_heap_shot = NULL;
245 new_heap_shot (uint64_t timestamp)
248 HeapShot *hs = calloc (sizeof (HeapShot), 1);
250 hs->class_hash = calloc (sizeof (void*), hs->hash_size);
251 hs->timestamp = timestamp;
252 /* append it to the list */
263 static HeapClassDesc*
264 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
267 unsigned int start_pos;
268 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
271 HeapClassDesc* cd = hs->class_hash [i];
274 if (cd->klass == klass)
277 if (++i == hs->hash_size)
279 } while (i != start_pos);
284 add_heap_hashed (HeapClassDesc **hash, int hsize, ClassDesc *klass, uint64_t size, uint64_t count)
287 unsigned int start_pos;
288 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
291 if (hash [i] && hash [i]->klass == klass) {
292 hash [i]->total_size += size;
293 hash [i]->count += count;
295 } else if (!hash [i]) {
296 hash [i] = calloc (sizeof (HeapClassDesc), 1);
297 hash [i]->klass = klass;
298 hash [i]->total_size += size;
299 hash [i]->count += count;
305 } while (i != start_pos);
306 /* should not happen */
307 printf ("failed heap class store\n");
312 add_heap_shot_obj (HeapShot *hs, ClassDesc *klass, uint64_t size)
315 if (hs->class_count * 2 >= hs->hash_size) {
317 int old_size = hs->hash_size;
319 if (hs->hash_size == 0)
321 n = calloc (sizeof (void*) * hs->hash_size, 1);
322 for (i = 0; i < old_size; ++i) {
323 if (hs->class_hash [i])
324 add_heap_hashed (n, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
327 free (hs->class_hash);
330 hs->class_count += add_heap_hashed (hs->class_hash, hs->hash_size, klass, size, 1);
338 MethodDesc *methods [1];
341 static BackTrace *backtrace_hash [HASH_SIZE];
342 static BackTrace **backtraces = NULL;
343 static int num_backtraces = 0;
344 static int next_backtrace = 0;
347 hash_backtrace (int count, MethodDesc **methods)
351 for (i = 0; i < count; ++i) {
352 hash = (hash << 5) - hash + methods [i]->method;
358 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
361 if (bt->count != count)
363 for (i = 0; i < count; ++i)
364 if (methods [i] != bt->methods [i])
370 add_backtrace (int count, MethodDesc **methods)
372 int hash = hash_backtrace (count, methods);
373 int slot = (hash & 0xffff) % HASH_SIZE;
374 BackTrace *bt = backtrace_hash [slot];
376 if (bt->hash == hash && compare_backtrace (bt, count, methods))
380 bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
381 bt->next = backtrace_hash [slot];
382 backtrace_hash [slot] = bt;
383 if (next_backtrace == num_backtraces) {
387 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
389 bt->id = next_backtrace++;
390 backtraces [bt->id] = bt;
393 for (slot = 0; slot < count; ++slot)
394 bt->methods [slot] = methods [slot];
399 typedef struct _MonitorDesc MonitorDesc;
400 typedef struct _ThreadContext ThreadContext;
404 #if defined (HAVE_ZLIB)
413 uint64_t startup_time;
414 ThreadContext *threads;
415 ThreadContext *current;
418 struct _ThreadContext {
423 uint64_t *time_stack;
424 uint64_t *callee_time_stack;
426 uint64_t contention_start;
427 MonitorDesc *monitor;
433 ensure_buffer (ProfContext *ctx, int size)
435 if (ctx->size < size) {
436 ctx->buf = realloc (ctx->buf, size);
442 load_data (ProfContext *ctx, int size)
444 ensure_buffer (ctx, size);
445 #if defined (HAVE_ZLIB)
447 return gzread (ctx->gzfile, ctx->buf, size) == size;
450 return fread (ctx->buf, size, 1, ctx->file);
453 static ThreadContext*
454 load_thread (ProfContext *ctx, intptr_t thread_id)
456 ThreadContext *thread;
457 if (ctx->current && ctx->current->thread_id == thread_id)
459 thread = ctx->threads;
461 if (thread->thread_id == thread_id) {
462 ctx->current = thread;
465 thread = thread->next;
467 thread = calloc (sizeof (ThreadContext), 1);
468 thread->next = ctx->threads;
469 ctx->threads = thread;
470 ctx->current = thread;
471 thread->thread_id = thread_id;
472 thread->last_time = 0;
473 thread->stack_id = 0;
474 thread->stack_size = 32;
475 thread->stack = malloc (thread->stack_size * sizeof (void*));
476 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
477 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
482 ensure_thread_stack (ThreadContext *thread)
484 if (thread->stack_id == thread->stack_size) {
485 thread->stack_size *= 2;
486 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
487 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
488 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
493 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
496 unsigned int start_pos;
497 start_pos = bt->hash % size;
500 if (traces [i].bt == bt) {
501 traces [i].count += value;
503 } else if (!traces [i].bt) {
505 traces [i].count += value;
511 } while (i != start_pos);
512 /* should not happen */
513 printf ("failed trace store\n");
518 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
523 if (trace->count * 2 >= trace->size) {
525 int old_size = trace->size;
527 if (trace->size == 0)
529 n = calloc (sizeof (CallContext) * trace->size, 1);
530 for (i = 0; i < old_size; ++i) {
531 if (trace->traces [i].bt)
532 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
535 free (trace->traces);
538 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
542 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
545 int count = thread->stack_id;
548 if (count > trace_max)
550 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
551 add_trace_bt (bt, trace, value);
556 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
561 if (count > trace_max)
563 bt = add_backtrace (count, methods);
564 add_trace_bt (bt, trace, value);
569 compare_callc (const void *a, const void *b)
571 const CallContext *A = a;
572 const CallContext *B = b;
573 if (B->count == A->count)
575 if (B->count < A->count)
581 sort_context_array (TraceDesc* traces)
584 for (i = 0, j = 0; i < traces->size; ++i) {
585 if (traces->traces [i].bt) {
586 traces->traces [j].bt = traces->traces [i].bt;
587 traces->traces [j].count = traces->traces [i].count;
591 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
595 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
597 ensure_thread_stack (thread);
598 thread->time_stack [thread->stack_id] = timestamp;
599 thread->callee_time_stack [thread->stack_id] = 0;
600 thread->stack [thread->stack_id++] = method;
601 method->recurse_count++;
605 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
607 method->recurse_count--;
608 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
612 if (timestamp < thread->time_stack [thread->stack_id])
613 fprintf (outfile, "time went backwards for %s\n", method->name);
614 tdiff = timestamp - thread->time_stack [thread->stack_id];
615 if (thread->callee_time_stack [thread->stack_id] > tdiff)
616 fprintf (outfile, "callee time bigger for %s\n", method->name);
617 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
618 method->callee_time += thread->callee_time_stack [thread->stack_id];
619 if (thread->stack_id)
620 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
621 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
623 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
628 uint64_t start_time; /* move this per-thread? */
633 static GCDesc gc_info [3];
634 static uint64_t max_heap_size;
635 static uint64_t gc_object_moves;
636 static int gc_resizes;
639 gc_event_name (int ev)
642 case MONO_GC_EVENT_START: return "start";
643 case MONO_GC_EVENT_MARK_START: return "mark start";
644 case MONO_GC_EVENT_MARK_END: return "mark end";
645 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
646 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
647 case MONO_GC_EVENT_END: return "end";
648 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
649 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
650 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
651 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
657 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
658 static uint64_t throw_count = 0;
659 static TraceDesc exc_traces;
662 clause_name (int type)
665 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
666 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
667 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
668 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
669 default: return "invalid";
673 static uint64_t monitor_contention;
674 static uint64_t monitor_failed;
675 static uint64_t monitor_acquired;
677 struct _MonitorDesc {
680 uintptr_t contentions;
682 uint64_t max_wait_time;
686 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
687 static int num_monitors = 0;
690 lookup_monitor (uintptr_t objid)
692 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
693 MonitorDesc *cd = monitor_hash [slot];
694 while (cd && cd->objid != objid)
697 cd = calloc (sizeof (MonitorDesc), 1);
699 cd->next = monitor_hash [slot];
700 monitor_hash [slot] = cd;
707 monitor_ev_name (int ev)
710 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
711 case MONO_PROFILER_MONITOR_DONE: return "acquired";
712 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
713 default: return "invalid";
718 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
722 int flags = decode_uleb128 (p, &p);
723 int count = decode_uleb128 (p, &p);
727 frames = malloc (count * sizeof (void*));
730 for (i = 0; i < count; ++i) {
731 intptr_t ptrdiff = decode_sleb128 (p, &p);
732 frames [i] = lookup_method (ptr_base + ptrdiff);
740 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
742 fprintf (outfile, "Object %p (%s, %llu bytes) created at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
743 if (bt && bt->count) {
745 for (k = 0; k < bt->count; ++k)
746 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
750 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
751 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
754 decode_buffer (ProfContext *ctx)
761 intptr_t method_base;
763 uint64_t file_offset;
765 ThreadContext *thread;
767 file_offset = ftell (ctx->file);
768 if (!load_data (ctx, 48))
771 if (read_int32 (p) != BUF_ID)
773 len = read_int32 (p + 4);
774 time_base = read_int64 (p + 8);
775 ptr_base = read_int64 (p + 16);
776 obj_base = read_int64 (p + 24);
777 thread_id = read_int64 (p + 32);
778 method_base = read_int64 (p + 40);
780 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
781 thread = load_thread (ctx, thread_id);
782 if (!load_data (ctx, len))
785 startup_time = time_base;
787 time_from += startup_time;
788 time_to += startup_time;
791 for (i = 0; i < thread->stack_id; ++i)
792 thread->stack [i]->recurse_count++;
798 int subtype = *p & 0xf0;
799 uint64_t tdiff = decode_uleb128 (p + 1, &p);
800 LOG_TIME (time_base, tdiff);
802 if (subtype == TYPE_GC_RESIZE) {
803 uint64_t new_size = decode_uleb128 (p, &p);
805 fprintf (outfile, "gc heap resized to %llu\n", new_size);
807 if (new_size > max_heap_size)
808 max_heap_size = new_size;
809 } else if (subtype == TYPE_GC_EVENT) {
810 uint64_t ev = decode_uleb128 (p, &p);
811 int gen = decode_uleb128 (p, &p);
813 fprintf (outfile, "gc event for gen%d: %s at %llu\n", gen - 1, gc_event_name (ev), time_base);
815 fprintf (outfile, "incorrect gc gen: %d\n", gen);
818 if (ev == MONO_GC_EVENT_START) {
819 gc_info [gen].start_time = time_base;
820 gc_info [gen].count++;
821 } else if (ev == MONO_GC_EVENT_END) {
822 tdiff = time_base - gc_info [gen].start_time;
823 gc_info [gen].total_time += tdiff;
824 if (tdiff > gc_info [gen].max_time)
825 gc_info [gen].max_time = tdiff;
827 } else if (subtype == TYPE_GC_MOVE) {
828 int j, num = decode_uleb128 (p, &p);
829 gc_object_moves += num / 2;
830 for (j = 0; j < num; j += 2) {
831 intptr_t obj1diff = decode_sleb128 (p, &p);
832 intptr_t obj2diff = decode_sleb128 (p, &p);
833 if (tracked_obj == OBJ_ADDR (obj1diff))
834 fprintf (outfile, "Object %p moved to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
835 else if (tracked_obj == OBJ_ADDR (obj2diff))
836 fprintf (outfile, "Object %p moved from %p\n", (void*)OBJ_ADDR (obj2diff), (void*)OBJ_ADDR (obj1diff));
838 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
844 case TYPE_METADATA: {
845 int error = *p & TYPE_LOAD_ERR;
846 uint64_t tdiff = decode_uleb128 (p + 1, &p);
848 intptr_t ptrdiff = decode_sleb128 (p, &p);
849 LOG_TIME (time_base, tdiff);
851 if (mtype == TYPE_CLASS) {
852 intptr_t imptrdiff = decode_sleb128 (p, &p);
853 uint64_t flags = decode_uleb128 (p, &p);
855 fprintf (outfile, "non-zero flags in class\n");
859 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
861 add_class (ptr_base + ptrdiff, (char*)p);
864 } else if (mtype == TYPE_IMAGE) {
865 uint64_t flags = decode_uleb128 (p, &p);
867 fprintf (outfile, "non-zero flags in image\n");
871 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
873 add_image (ptr_base + ptrdiff, (char*)p);
880 int has_bt = *p & TYPE_ALLOC_BT;
881 uint64_t tdiff = decode_uleb128 (p + 1, &p);
882 intptr_t ptrdiff = decode_sleb128 (p, &p);
883 intptr_t objdiff = decode_sleb128 (p, &p);
886 MethodDesc* sframes [8];
887 MethodDesc** frames = sframes;
888 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
889 len = decode_uleb128 (p, &p);
890 LOG_TIME (time_base, tdiff);
893 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
896 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
898 fprintf (outfile, "Cannot load backtrace\n");
902 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
905 cd->alloc_size += len;
907 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
909 bt = add_trace_thread (thread, &cd->traces, len);
910 if (tracked_obj == OBJ_ADDR (objdiff))
911 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
913 if (frames != sframes)
918 int subtype = *p & 0xf0;
919 uint64_t tdiff = decode_uleb128 (p + 1, &p);
920 int64_t ptrdiff = decode_sleb128 (p, &p);
921 LOG_TIME (time_base, tdiff);
923 method_base += ptrdiff;
924 if (subtype == TYPE_JIT) {
925 intptr_t codediff = decode_sleb128 (p, &p);
926 int codelen = decode_uleb128 (p, &p);
928 fprintf (outfile, "jitted method %p (%s), size: %d\n", (void*)(method_base), p, codelen);
929 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
934 if ((thread_filter && thread_filter != thread->thread_id))
936 method = lookup_method (method_base);
937 if (subtype == TYPE_ENTER) {
938 add_trace_thread (thread, &method->traces, 1);
939 push_method (thread, method, time_base);
941 pop_method (thread, method, time_base);
944 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
949 int subtype = *p & 0xf0;
950 if (subtype == TYPE_HEAP_OBJECT) {
952 intptr_t objdiff = decode_sleb128 (p + 1, &p);
953 intptr_t ptrdiff = decode_sleb128 (p, &p);
954 uint64_t size = decode_uleb128 (p, &p);
955 int num = decode_uleb128 (p, &p);
956 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
957 for (i = 0; i < num; ++i) {
958 /* FIXME: use object distance to measure how good
959 * the GC is at keeping related objects close
961 decode_sleb128 (p, &p);
964 fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
966 add_heap_shot_obj (current_heap_shot, cd, size);
967 } else if (subtype == TYPE_HEAP_END) {
968 uint64_t tdiff = decode_uleb128 (p + 1, &p);
969 LOG_TIME (time_base, tdiff);
972 fprintf (outfile, "heap shot end\n");
973 current_heap_shot = NULL;
974 } else if (subtype == TYPE_HEAP_START) {
975 uint64_t tdiff = decode_uleb128 (p + 1, &p);
976 LOG_TIME (time_base, tdiff);
979 fprintf (outfile, "heap shot start\n");
980 current_heap_shot = new_heap_shot (time_base);
985 int event = (*p >> 4) & 0x3;
986 int has_bt = *p & TYPE_MONITOR_BT;
987 uint64_t tdiff = decode_uleb128 (p + 1, &p);
988 intptr_t objdiff = decode_sleb128 (p, &p);
989 MethodDesc* sframes [8];
990 MethodDesc** frames = sframes;
993 LOG_TIME (time_base, tdiff);
995 record = (!thread_filter || thread_filter == thread->thread_id);
996 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
997 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
999 monitor_contention++;
1000 mdesc->contentions++;
1001 thread->monitor = mdesc;
1002 thread->contention_start = time_base;
1006 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1008 fprintf (outfile, "Cannot load backtrace\n");
1012 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1015 add_trace_thread (thread, &mdesc->traces, 1);
1017 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1020 if (thread->monitor && thread->contention_start) {
1021 uint64_t wait_time = time_base - thread->contention_start;
1022 if (wait_time > thread->monitor->max_wait_time)
1023 thread->monitor->max_wait_time = wait_time;
1024 thread->monitor->wait_time += wait_time;
1025 thread->monitor = NULL;
1026 thread->contention_start = 0;
1029 } else if (event == MONO_PROFILER_MONITOR_DONE) {
1032 if (thread->monitor && thread->contention_start) {
1033 uint64_t wait_time = time_base - thread->contention_start;
1034 if (wait_time > thread->monitor->max_wait_time)
1035 thread->monitor->max_wait_time = wait_time;
1036 thread->monitor->wait_time += wait_time;
1037 thread->monitor = NULL;
1038 thread->contention_start = 0;
1043 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1044 if (frames != sframes)
1048 case TYPE_EXCEPTION: {
1049 int subtype = *p & 0x70;
1050 int has_bt = *p & TYPE_EXCEPTION_BT;
1051 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1052 MethodDesc* sframes [8];
1053 MethodDesc** frames = sframes;
1055 LOG_TIME (time_base, tdiff);
1057 record = (!thread_filter || thread_filter == thread->thread_id);
1058 if (subtype == TYPE_CLAUSE) {
1059 int clause_type = decode_uleb128 (p, &p);
1060 int clause_num = decode_uleb128 (p, &p);
1061 int64_t ptrdiff = decode_sleb128 (p, &p);
1062 method_base += ptrdiff;
1064 clause_summary [clause_type]++;
1066 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1068 intptr_t objdiff = decode_sleb128 (p, &p);
1073 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1075 fprintf (outfile, "Cannot load backtrace\n");
1079 add_trace_methods (frames, has_bt, &exc_traces, 1);
1082 add_trace_thread (thread, &exc_traces, 1);
1084 if (frames != sframes)
1087 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1092 fprintf (outfile, "unhandled profiler event: 0x%x\n", *p);
1096 thread->last_time = time_base;
1097 for (i = 0; i < thread->stack_id; ++i)
1098 thread->stack [i]->recurse_count = 0;
1103 load_file (char *name)
1106 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1107 if (strcmp (name, "-") == 0)
1110 ctx->file = fopen (name, "rb");
1112 printf ("Cannot open file: %s\n", name);
1115 #if defined (HAVE_ZLIB)
1116 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
1118 if (!load_data (ctx, 32))
1121 if (read_int32 (p) != LOG_HEADER_ID || p [6] != LOG_DATA_VERSION)
1123 ctx->version_major = p [4];
1124 ctx->version_minor = p [5];
1125 ctx->data_version = p [6];
1126 /* reading 64 bit files on 32 bit systems not supported yet */
1127 if (p [7] > sizeof (void*))
1129 if (read_int32 (p + 20)) /* flags must be 0 */
1131 ctx->startup_time = read_int64 (p + 8);
1132 ctx->timer_overhead = read_int32 (p + 16);
1140 static int alloc_sort_mode = ALLOC_SORT_BYTES;
1143 compare_class (const void *a, const void *b)
1145 ClassDesc *const*A = a;
1146 ClassDesc *const*B = b;
1147 uint64_t vala, valb;
1148 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1149 vala = (*A)->alloc_size;
1150 valb = (*B)->alloc_size;
1152 vala = (*A)->allocs;
1153 valb = (*B)->allocs;
1163 dump_header (ProfContext *ctx)
1165 time_t st = ctx->startup_time / 1000;
1166 char *t = ctime (&st);
1167 fprintf (outfile, "\nMono log profiler data\n");
1168 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
1169 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
1170 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
1171 fprintf (outfile, "\tProgram startup: %s\n", t);
1175 dump_traces (TraceDesc *traces, const char *desc)
1182 sort_context_array (traces);
1183 for (j = 0; j < traces->count; ++j) {
1186 bt = traces->traces [j].bt;
1189 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
1190 for (k = 0; k < bt->count; ++k)
1191 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
1196 dump_threads (ProfContext *ctx)
1198 ThreadContext *thread;
1199 fprintf (outfile, "\nThread summary\n");
1200 for (thread = ctx->threads; thread; thread = thread->next) {
1201 fprintf (outfile, "\tThread: %p\n", (void*)thread->thread_id);
1206 dump_exceptions (void)
1209 fprintf (outfile, "\nException summary\n");
1210 fprintf (outfile, "\tThrows: %llu\n", throw_count);
1211 dump_traces (&exc_traces, "throws");
1212 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
1213 if (!clause_summary [i])
1215 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
1220 compare_monitor (const void *a, const void *b)
1222 MonitorDesc *const*A = a;
1223 MonitorDesc *const*B = b;
1224 if ((*B)->wait_time == (*A)->wait_time)
1226 if ((*B)->wait_time < (*A)->wait_time)
1232 dump_monitors (void)
1234 MonitorDesc **monitors;
1238 monitors = malloc (sizeof (void*) * num_monitors);
1239 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
1240 MonitorDesc *mdesc = monitor_hash [i];
1242 monitors [j++] = mdesc;
1243 mdesc = mdesc->next;
1246 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
1247 fprintf (outfile, "\nMonitor lock summary\n");
1248 for (i = 0; i < num_monitors; ++i) {
1249 MonitorDesc *mdesc = monitors [i];
1250 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
1251 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
1252 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
1253 dump_traces (&mdesc->traces, "contentions");
1255 fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
1256 fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
1257 fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
1264 fprintf (outfile, "\nGC summary\n");
1265 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
1266 fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
1267 fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
1268 for (i = 0; i < 3; ++i) {
1269 if (!gc_info [i].count)
1271 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
1272 i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
1273 gc_info [i].total_time / gc_info [i].count / 1000);
1278 dump_allocations (void)
1281 intptr_t allocs = 0;
1283 int header_done = 0;
1284 ClassDesc **classes = malloc (num_classes * sizeof (void*));
1287 for (i = 0; i < HASH_SIZE; ++i) {
1288 cd = class_hash [i];
1294 qsort (classes, num_classes, sizeof (void*), compare_class);
1295 for (i = 0; i < num_classes; ++i) {
1299 allocs += cd->allocs;
1300 size += cd->alloc_size;
1301 if (!header_done++) {
1302 fprintf (outfile, "\nAllocation summary\n");
1303 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
1305 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
1306 dump_traces (&cd->traces, "bytes");
1309 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
1318 static int method_sort_mode = METHOD_SORT_TOTAL;
1321 compare_method (const void *a, const void *b)
1323 MethodDesc *const*A = a;
1324 MethodDesc *const*B = b;
1325 uint64_t vala, valb;
1326 if (method_sort_mode == METHOD_SORT_SELF) {
1327 vala = (*A)->self_time;
1328 valb = (*B)->self_time;
1329 } else if (method_sort_mode == METHOD_SORT_CALLS) {
1333 vala = (*A)->total_time;
1334 valb = (*B)->total_time;
1344 dump_metadata (void)
1346 fprintf (outfile, "\nMetadata summary\n");
1347 fprintf (outfile, "\tLoaded images: %d\n", num_images);
1351 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
1352 image = image_hash [i];
1354 fprintf (outfile, "\t\t%s\n", image->filename);
1355 image = image->next;
1367 int header_done = 0;
1368 MethodDesc **methods = malloc (num_methods * sizeof (void*));
1371 for (i = 0; i < HASH_SIZE; ++i) {
1372 cd = method_hash [i];
1374 cd->total_time = cd->self_time + cd->callee_time;
1379 qsort (methods, num_methods, sizeof (void*), compare_method);
1380 for (i = 0; i < num_methods; ++i) {
1387 msecs = cd->total_time / 1000000;
1388 smsecs = (cd->total_time - cd->callee_time) / 1000000;
1389 if (!msecs && !verbose)
1391 if (!header_done++) {
1392 fprintf (outfile, "\nMethod call summary\n");
1393 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
1395 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
1396 dump_traces (&cd->traces, "calls");
1399 fprintf (outfile, "Total calls: %llu\n", calls);
1403 compare_heap_class (const void *a, const void *b)
1405 HeapClassDesc *const*A = a;
1406 HeapClassDesc *const*B = b;
1407 uint64_t vala, valb;
1408 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1409 vala = (*A)->total_size;
1410 valb = (*B)->total_size;
1423 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
1430 HeapClassDesc **sorted;
1431 sorted = malloc (sizeof (void*) * hs->class_count);
1432 for (i = 0; i < hs->hash_size; ++i) {
1433 cd = hs->class_hash [i];
1437 size += cd->total_size;
1438 sorted [ccount++] = cd;
1440 hs->sorted = sorted;
1441 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
1442 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d\n",
1443 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount);
1444 if (!verbose && ccount > 30)
1446 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
1447 for (i = 0; i < ccount; ++i) {
1448 HeapClassDesc *ocd = NULL;
1451 ocd = heap_class_lookup (last_hs, cd->klass);
1452 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
1454 int64_t bdiff = cd->total_size - ocd->total_size;
1455 int64_t cdiff = cd->count - ocd->count;
1456 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
1458 fprintf (outfile, "\n");
1464 dump_heap_shots (void)
1467 HeapShot *last_hs = NULL;
1471 fprintf (outfile, "\nHeap shot summary\n");
1473 for (hs = heap_shots; hs; hs = hs->next) {
1474 heap_shot_summary (hs, i++, last_hs);
1480 flush_context (ProfContext *ctx)
1482 ThreadContext *thread;
1483 /* FIXME: sometimes there are leftovers: indagate */
1484 for (thread = ctx->threads; thread; thread = thread->next) {
1485 while (thread->stack_id) {
1487 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
1488 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
1493 static const char *reports = "header,gc,alloc,call,metadata,exception,monitor,thread,heapshot";
1496 match_option (const char *p, const char *opt)
1498 int len = strlen (opt);
1499 if (strncmp (p, opt, len) == 0) {
1508 print_reports (ProfContext *ctx, const char *reps, int parse_only)
1512 for (p = reps; *p; p = opt) {
1513 if ((opt = match_option (p, "header")) != p) {
1518 if ((opt = match_option (p, "thread")) != p) {
1523 if ((opt = match_option (p, "gc")) != p) {
1528 if ((opt = match_option (p, "alloc")) != p) {
1530 dump_allocations ();
1533 if ((opt = match_option (p, "call")) != p) {
1538 if ((opt = match_option (p, "metadata")) != p) {
1543 if ((opt = match_option (p, "exception")) != p) {
1548 if ((opt = match_option (p, "monitor")) != p) {
1553 if ((opt = match_option (p, "heapshot")) != p) {
1566 // FIXME: add --find="FINDSPEC"
1567 // obj=addr size=<>num type=name
1568 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
1569 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
1570 printf ("FILENAME can be '-' to read from standard input.\n");
1571 printf ("Options:\n");
1572 printf ("\t--help display this help\n");
1573 printf ("\t--out=FILE write to FILE instead of stdout\n");
1574 printf ("\t--traces collect and show backtraces\n");
1575 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
1576 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
1577 printf ("\t %s\n", reports);
1578 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
1579 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
1580 printf ("\t--track=OBJID track what happens to object OBJID\n");
1581 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
1582 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
1583 printf ("\t--verbose increase verbosity level\n");
1584 printf ("\t--debug display decoding debug info for mprof-report devs\n");
1588 main (int argc, char *argv[])
1593 for (i = 1; i < argc; ++i) {
1594 if (strcmp ("--debug", argv [i]) == 0) {
1596 } else if (strcmp ("--help", argv [i]) == 0) {
1599 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
1600 const char *val = argv [i] + 13;
1601 if (strcmp (val, "bytes") == 0) {
1602 alloc_sort_mode = ALLOC_SORT_BYTES;
1603 } else if (strcmp (val, "count") == 0) {
1604 alloc_sort_mode = ALLOC_SORT_COUNT;
1609 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
1610 const char *val = argv [i] + 14;
1611 if (strcmp (val, "total") == 0) {
1612 method_sort_mode = METHOD_SORT_TOTAL;
1613 } else if (strcmp (val, "self") == 0) {
1614 method_sort_mode = METHOD_SORT_SELF;
1615 } else if (strcmp (val, "calls") == 0) {
1616 method_sort_mode = METHOD_SORT_CALLS;
1621 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
1622 const char *val = argv [i] + 10;
1623 if (!print_reports (NULL, val, 1)) {
1628 } else if (strncmp ("--out=", argv [i], 6) == 0) {
1629 const char *val = argv [i] + 6;
1630 outfile = fopen (val, "w");
1632 printf ("Cannot open output file: %s\n", val);
1635 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
1636 const char *val = argv [i] + 12;
1638 trace_max = strtoul (val, &vale, 10);
1639 } else if (strncmp ("--track=", argv [i], 8) == 0) {
1640 const char *val = argv [i] + 8;
1642 tracked_obj = strtoul (val, &vale, 0);
1643 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
1644 const char *val = argv [i] + 9;
1646 thread_filter = strtoul (val, &vale, 0);
1647 } else if (strncmp ("--time=", argv [i], 7) == 0) {
1648 char *val = pstrdup (argv [i] + 7);
1649 double from_secs, to_secs;
1650 char *top = strchr (val, '-');
1656 from_secs = atof (val);
1657 to_secs = atof (top);
1659 if (from_secs > to_secs) {
1663 time_from = from_secs * 1000000000;
1664 time_to = to_secs * 1000000000;
1665 } else if (strcmp ("--verbose", argv [i]) == 0) {
1667 } else if (strcmp ("--traces", argv [i]) == 0) {
1678 ctx = load_file (argv [i]);
1680 printf ("Not a log profiler data file (or unsupported version).\n");
1683 while (decode_buffer (ctx));
1684 flush_context (ctx);
1687 print_reports (ctx, reports, 0);