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)
15 #if !defined(__APPLE__)
23 #if defined (HAVE_ZLIB)
26 #include <mono/metadata/profiler.h>
27 #include <mono/metadata/object.h>
28 #include <mono/metadata/debug-helpers.h>
30 #define HASH_SIZE 9371
31 #define SMALL_HASH_SIZE 31
34 static int collect_traces = 0;
35 static int show_traces = 0;
36 static int trace_max = 6;
37 static int verbose = 0;
38 static uintptr_t *tracked_objects = 0;
39 static int num_tracked_objects = 0;
40 static uintptr_t thread_filter = 0;
41 static uint64_t find_size = 0;
42 static const char* find_name = NULL;
43 static uint64_t time_from = 0;
44 static uint64_t time_to = 0xffffffffffffffffULL;
45 static uint64_t startup_time = 0;
46 static FILE* outfile = NULL;
49 read_int32 (unsigned char *p)
53 value |= (*p++) << 16;
54 value |= (uint32_t)(*p++) << 24;
59 read_int64 (unsigned char *p)
61 uint64_t value = *p++;
63 value |= (*p++) << 16;
64 value |= (uint64_t)(*p++) << 24;
65 value |= (uint64_t)(*p++) << 32;
66 value |= (uint64_t)(*p++) << 40;
67 value |= (uint64_t)(*p++) << 48;
68 value |= (uint64_t)(*p++) << 54;
73 pstrdup (const char *s)
75 int len = strlen (s) + 1;
76 char *p = malloc (len);
81 static int num_images;
82 typedef struct _ImageDesc ImageDesc;
89 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
92 add_image (intptr_t image, char *name)
94 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
95 ImageDesc *cd = malloc (sizeof (ImageDesc));
97 cd->filename = pstrdup (name);
98 cd->next = image_hash [slot];
99 image_hash [slot] = cd;
103 typedef struct _BackTrace BackTrace;
115 typedef struct _ClassDesc ClassDesc;
125 static ClassDesc* class_hash [HASH_SIZE] = {0};
126 static int num_classes = 0;
129 add_class (intptr_t klass, const char *name)
131 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
133 cd = class_hash [slot];
134 while (cd && cd->klass != klass)
136 /* we resolved an unknown class (unless we had the code unloaded) */
138 /*printf ("resolved unknown: %s\n", name);*/
140 cd->name = pstrdup (name);
143 cd = calloc (sizeof (ClassDesc), 1);
145 cd->name = pstrdup (name);
146 cd->next = class_hash [slot];
149 cd->traces.count = 0;
151 cd->traces.traces = NULL;
152 class_hash [slot] = cd;
158 lookup_class (intptr_t klass)
160 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
161 ClassDesc *cd = class_hash [slot];
162 while (cd && cd->klass != klass)
165 return add_class (klass, "unresolved class");
169 typedef struct _MethodDesc MethodDesc;
179 uint64_t callee_time;
184 static MethodDesc* method_hash [HASH_SIZE] = {0};
185 static int num_methods = 0;
188 add_method (intptr_t method, const char *name, intptr_t code, int len)
190 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
192 cd = method_hash [slot];
193 while (cd && cd->method != method)
195 /* we resolved an unknown method (unless we had the code unloaded) */
199 /*printf ("resolved unknown: %s\n", name);*/
201 cd->name = pstrdup (name);
204 cd = calloc (sizeof (MethodDesc), 1);
206 cd->name = pstrdup (name);
211 cd->traces.count = 0;
213 cd->traces.traces = NULL;
214 cd->next = method_hash [slot];
215 method_hash [slot] = cd;
221 lookup_method (intptr_t method)
223 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
224 MethodDesc *cd = method_hash [slot];
225 while (cd && cd->method != method)
228 return add_method (method, "unknown method", 0, 0);
232 typedef struct _HeapClassDesc HeapClassDesc;
234 HeapClassDesc *klass;
238 struct _HeapClassDesc {
242 HeapClassRevRef *rev_hash;
248 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
252 start_pos = (hklass->klass->klass >> 2) % size;
253 assert (start_pos < size);
256 if (rev_hash [i].klass == hklass) {
257 rev_hash [i].count += value;
259 } else if (!rev_hash [i].klass) {
260 rev_hash [i].klass = hklass;
261 rev_hash [i].count += value;
263 for (i = 0; i < size; ++i)
264 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
266 assert (start_pos == 1);
272 } while (i != start_pos);
273 /* should not happen */
274 printf ("failed revref store\n");
279 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
282 if (to->rev_count * 2 >= to->rev_hash_size) {
284 uintptr_t old_size = to->rev_hash_size;
285 to->rev_hash_size *= 2;
286 if (to->rev_hash_size == 0)
287 to->rev_hash_size = 4;
288 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
289 for (i = 0; i < old_size; ++i) {
290 if (to->rev_hash [i].klass)
291 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
297 to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
302 HeapClassDesc *hklass;
307 typedef struct _HeapShot HeapShot;
313 HeapClassDesc **class_hash;
314 HeapClassDesc **sorted;
315 HeapObjectDesc **objects_hash;
316 uintptr_t objects_count;
317 uintptr_t objects_hash_size;
320 static HeapShot *heap_shots = NULL;
321 static int num_heap_shots = 0;
324 new_heap_shot (uint64_t timestamp)
326 HeapShot *hs = calloc (sizeof (HeapShot), 1);
328 hs->class_hash = calloc (sizeof (void*), hs->hash_size);
329 hs->timestamp = timestamp;
331 hs->next = heap_shots;
336 static HeapClassDesc*
337 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
340 unsigned int start_pos;
341 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
344 HeapClassDesc* cd = hs->class_hash [i];
347 if (cd->klass == klass)
350 if (++i == hs->hash_size)
352 } while (i != start_pos);
357 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
361 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
364 if (hash [i] && hash [i]->klass == klass) {
365 hash [i]->total_size += size;
366 hash [i]->count += count;
369 } else if (!hash [i]) {
374 hash [i] = calloc (sizeof (HeapClassDesc), 1);
375 hash [i]->klass = klass;
376 hash [i]->total_size += size;
377 hash [i]->count += count;
384 } while (i != start_pos);
385 /* should not happen */
386 printf ("failed heap class store\n");
390 static HeapClassDesc*
391 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
395 if (hs->class_count * 2 >= hs->hash_size) {
397 int old_size = hs->hash_size;
399 if (hs->hash_size == 0)
401 n = calloc (sizeof (void*) * hs->hash_size, 1);
402 for (i = 0; i < old_size; ++i) {
403 res = hs->class_hash [i];
404 if (hs->class_hash [i])
405 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
408 free (hs->class_hash);
412 hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
413 //if (res->count == 1)
414 // printf ("added heap class: %s\n", res->klass->name);
418 static HeapObjectDesc*
419 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
421 HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
422 ho->objaddr = objaddr;
424 ho->num_refs = num_refs;
429 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
433 HeapObjectDesc **hash = hs->objects_hash;
434 start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
437 if (hash [i] && hash [i]->objaddr == objaddr) {
439 } else if (!hash [i]) {
443 if (++i == hs->objects_hash_size)
445 } while (i != start_pos);
446 /* should not happen */
447 printf ("failed heap obj update\n");
451 static HeapObjectDesc*
452 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
454 HeapObjectDesc **hash = hs->objects_hash;
455 uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
457 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
458 *ref_offset = hash [i]->num_refs;
459 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
464 /* should not happen */
465 printf ("failed heap obj update\n");
471 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
475 start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
478 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
479 printf ("duplicate object!\n");
481 } else if (!hash [i]) {
488 } while (i != start_pos);
489 /* should not happen */
490 printf ("failed heap obj store\n");
495 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
498 if (hs->objects_count * 2 >= hs->objects_hash_size) {
500 uintptr_t old_size = hs->objects_hash_size;
501 hs->objects_hash_size *= 2;
502 if (hs->objects_hash_size == 0)
503 hs->objects_hash_size = 4;
504 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
505 for (i = 0; i < old_size; ++i) {
506 if (hs->objects_hash [i])
507 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
509 if (hs->objects_hash)
510 free (hs->objects_hash);
511 hs->objects_hash = n;
513 hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
517 heap_shot_resolve_reverse_refs (HeapShot *hs)
520 for (i = 0; i < hs->objects_hash_size; ++i) {
522 HeapObjectDesc *ho = hs->objects_hash [i];
525 for (r = 0; r < ho->num_refs; ++r) {
526 uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
527 add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
533 heap_shot_free_objects (HeapShot *hs)
536 for (i = 0; i < hs->objects_hash_size; ++i) {
537 HeapObjectDesc *ho = hs->objects_hash [i];
541 if (hs->objects_hash)
542 free (hs->objects_hash);
543 hs->objects_hash = NULL;
544 hs->objects_hash_size = 0;
545 hs->objects_count = 0;
553 MethodDesc *methods [1];
556 static BackTrace *backtrace_hash [HASH_SIZE];
557 static BackTrace **backtraces = NULL;
558 static int num_backtraces = 0;
559 static int next_backtrace = 0;
562 hash_backtrace (int count, MethodDesc **methods)
566 for (i = 0; i < count; ++i) {
567 hash = (hash << 5) - hash + methods [i]->method;
573 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
576 if (bt->count != count)
578 for (i = 0; i < count; ++i)
579 if (methods [i] != bt->methods [i])
585 add_backtrace (int count, MethodDesc **methods)
587 int hash = hash_backtrace (count, methods);
588 int slot = (hash & 0xffff) % HASH_SIZE;
589 BackTrace *bt = backtrace_hash [slot];
591 if (bt->hash == hash && compare_backtrace (bt, count, methods))
595 bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
596 bt->next = backtrace_hash [slot];
597 backtrace_hash [slot] = bt;
598 if (next_backtrace == num_backtraces) {
602 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
604 bt->id = next_backtrace++;
605 backtraces [bt->id] = bt;
608 for (slot = 0; slot < count; ++slot)
609 bt->methods [slot] = methods [slot];
614 typedef struct _MonitorDesc MonitorDesc;
615 typedef struct _ThreadContext ThreadContext;
619 #if defined (HAVE_ZLIB)
628 uint64_t startup_time;
629 ThreadContext *threads;
630 ThreadContext *current;
633 struct _ThreadContext {
639 uint64_t *time_stack;
640 uint64_t *callee_time_stack;
642 uint64_t contention_start;
643 MonitorDesc *monitor;
646 HeapShot *current_heap_shot;
650 ensure_buffer (ProfContext *ctx, int size)
652 if (ctx->size < size) {
653 ctx->buf = realloc (ctx->buf, size);
659 load_data (ProfContext *ctx, int size)
661 ensure_buffer (ctx, size);
662 #if defined (HAVE_ZLIB)
664 return gzread (ctx->gzfile, ctx->buf, size) == size;
667 return fread (ctx->buf, size, 1, ctx->file);
670 static ThreadContext*
671 get_thread (ProfContext *ctx, intptr_t thread_id)
673 ThreadContext *thread;
674 if (ctx->current && ctx->current->thread_id == thread_id)
676 thread = ctx->threads;
678 if (thread->thread_id == thread_id) {
681 thread = thread->next;
683 thread = calloc (sizeof (ThreadContext), 1);
684 thread->next = ctx->threads;
685 ctx->threads = thread;
686 thread->thread_id = thread_id;
687 thread->last_time = 0;
688 thread->stack_id = 0;
689 thread->stack_size = 32;
690 thread->stack = malloc (thread->stack_size * sizeof (void*));
691 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
692 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
696 static ThreadContext*
697 load_thread (ProfContext *ctx, intptr_t thread_id)
699 ThreadContext *thread = get_thread (ctx, thread_id);
700 ctx->current = thread;
705 ensure_thread_stack (ThreadContext *thread)
707 if (thread->stack_id == thread->stack_size) {
708 thread->stack_size *= 2;
709 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
710 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
711 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
716 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
719 unsigned int start_pos;
720 start_pos = bt->hash % size;
723 if (traces [i].bt == bt) {
724 traces [i].count += value;
726 } else if (!traces [i].bt) {
728 traces [i].count += value;
734 } while (i != start_pos);
735 /* should not happen */
736 printf ("failed trace store\n");
741 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
746 if (trace->count * 2 >= trace->size) {
748 int old_size = trace->size;
750 if (trace->size == 0)
752 n = calloc (sizeof (CallContext) * trace->size, 1);
753 for (i = 0; i < old_size; ++i) {
754 if (trace->traces [i].bt)
755 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
758 free (trace->traces);
761 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
765 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
768 int count = thread->stack_id;
771 if (count > trace_max)
773 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
774 add_trace_bt (bt, trace, value);
779 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
784 if (count > trace_max)
786 bt = add_backtrace (count, methods);
787 add_trace_bt (bt, trace, value);
792 compare_callc (const void *a, const void *b)
794 const CallContext *A = a;
795 const CallContext *B = b;
796 if (B->count == A->count)
798 if (B->count < A->count)
804 sort_context_array (TraceDesc* traces)
807 for (i = 0, j = 0; i < traces->size; ++i) {
808 if (traces->traces [i].bt) {
809 traces->traces [j].bt = traces->traces [i].bt;
810 traces->traces [j].count = traces->traces [i].count;
814 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
818 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
820 ensure_thread_stack (thread);
821 thread->time_stack [thread->stack_id] = timestamp;
822 thread->callee_time_stack [thread->stack_id] = 0;
823 thread->stack [thread->stack_id++] = method;
824 method->recurse_count++;
828 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
830 method->recurse_count--;
831 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
835 if (timestamp < thread->time_stack [thread->stack_id])
836 fprintf (outfile, "time went backwards for %s\n", method->name);
837 tdiff = timestamp - thread->time_stack [thread->stack_id];
838 if (thread->callee_time_stack [thread->stack_id] > tdiff)
839 fprintf (outfile, "callee time bigger for %s\n", method->name);
840 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
841 method->callee_time += thread->callee_time_stack [thread->stack_id];
842 if (thread->stack_id)
843 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
844 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
846 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
851 uint64_t start_time; /* move this per-thread? */
856 static GCDesc gc_info [3];
857 static uint64_t max_heap_size;
858 static uint64_t gc_object_moves;
859 static int gc_resizes;
866 static HandleInfo handle_info [4];
869 gc_event_name (int ev)
872 case MONO_GC_EVENT_START: return "start";
873 case MONO_GC_EVENT_MARK_START: return "mark start";
874 case MONO_GC_EVENT_MARK_END: return "mark end";
875 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
876 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
877 case MONO_GC_EVENT_END: return "end";
878 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
879 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
880 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
881 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
887 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
888 static uint64_t throw_count = 0;
889 static TraceDesc exc_traces;
892 clause_name (int type)
895 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
896 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
897 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
898 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
899 default: return "invalid";
903 static uint64_t monitor_contention;
904 static uint64_t monitor_failed;
905 static uint64_t monitor_acquired;
907 struct _MonitorDesc {
910 uintptr_t contentions;
912 uint64_t max_wait_time;
916 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
917 static int num_monitors = 0;
920 lookup_monitor (uintptr_t objid)
922 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
923 MonitorDesc *cd = monitor_hash [slot];
924 while (cd && cd->objid != objid)
927 cd = calloc (sizeof (MonitorDesc), 1);
929 cd->next = monitor_hash [slot];
930 monitor_hash [slot] = cd;
937 monitor_ev_name (int ev)
940 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
941 case MONO_PROFILER_MONITOR_DONE: return "acquired";
942 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
943 default: return "invalid";
948 get_handle_name (int htype)
951 case 0: return "weak";
952 case 1: return "weaktrack";
953 case 2: return "normal";
954 case 3: return "pinned";
955 default: return "unknown";
960 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
964 int flags = decode_uleb128 (p, &p);
965 int count = decode_uleb128 (p, &p);
969 frames = malloc (count * sizeof (void*));
972 for (i = 0; i < count; ++i) {
973 intptr_t ptrdiff = decode_sleb128 (p, &p);
974 frames [i] = lookup_method (ptr_base + ptrdiff);
982 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
985 for (i = 0; i < num_tracked_objects; ++i) {
986 if (tracked_objects [i] != obj)
988 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
989 if (bt && bt->count) {
991 for (k = 0; k < bt->count; ++k)
992 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
998 track_handle (uintptr_t obj, int htype, uint32_t handle)
1001 for (i = 0; i < num_tracked_objects; ++i) {
1002 if (tracked_objects [i] == obj)
1003 fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1008 track_move (uintptr_t src, uintptr_t dst)
1011 for (i = 0; i < num_tracked_objects; ++i) {
1012 if (tracked_objects [i] == src)
1013 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1014 else if (tracked_objects [i] == dst)
1015 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1020 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1023 for (i = 0; i < num_tracked_objects; ++i) {
1024 if (tracked_objects [i] == obj)
1025 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1030 found_object (uintptr_t obj)
1032 num_tracked_objects ++;
1033 tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1034 tracked_objects [num_tracked_objects - 1] = obj;
1037 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
1038 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
1041 decode_buffer (ProfContext *ctx)
1048 intptr_t method_base;
1050 uint64_t file_offset;
1052 ThreadContext *thread;
1054 file_offset = ftell (ctx->file);
1055 if (!load_data (ctx, 48))
1058 if (read_int32 (p) != BUF_ID)
1060 len = read_int32 (p + 4);
1061 time_base = read_int64 (p + 8);
1062 ptr_base = read_int64 (p + 16);
1063 obj_base = read_int64 (p + 24);
1064 thread_id = read_int64 (p + 32);
1065 method_base = read_int64 (p + 40);
1067 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
1068 thread = load_thread (ctx, thread_id);
1069 if (!load_data (ctx, len))
1071 if (!startup_time) {
1072 startup_time = time_base;
1074 time_from += startup_time;
1075 time_to += startup_time;
1078 thread->name = pstrdup ("Main");
1080 for (i = 0; i < thread->stack_id; ++i)
1081 thread->stack [i]->recurse_count++;
1087 int subtype = *p & 0xf0;
1088 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1089 LOG_TIME (time_base, tdiff);
1091 if (subtype == TYPE_GC_RESIZE) {
1092 uint64_t new_size = decode_uleb128 (p, &p);
1094 fprintf (outfile, "gc heap resized to %llu\n", new_size);
1096 if (new_size > max_heap_size)
1097 max_heap_size = new_size;
1098 } else if (subtype == TYPE_GC_EVENT) {
1099 uint64_t ev = decode_uleb128 (p, &p);
1100 int gen = decode_uleb128 (p, &p);
1102 fprintf (outfile, "gc event for gen%d: %s at %llu\n", gen - 1, gc_event_name (ev), time_base);
1104 fprintf (outfile, "incorrect gc gen: %d\n", gen);
1107 if (ev == MONO_GC_EVENT_START) {
1108 gc_info [gen].start_time = time_base;
1109 gc_info [gen].count++;
1110 } else if (ev == MONO_GC_EVENT_END) {
1111 tdiff = time_base - gc_info [gen].start_time;
1112 gc_info [gen].total_time += tdiff;
1113 if (tdiff > gc_info [gen].max_time)
1114 gc_info [gen].max_time = tdiff;
1116 } else if (subtype == TYPE_GC_MOVE) {
1117 int j, num = decode_uleb128 (p, &p);
1118 gc_object_moves += num / 2;
1119 for (j = 0; j < num; j += 2) {
1120 intptr_t obj1diff = decode_sleb128 (p, &p);
1121 intptr_t obj2diff = decode_sleb128 (p, &p);
1122 if (num_tracked_objects)
1123 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
1125 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
1128 } else if (subtype == TYPE_GC_HANDLE_CREATED) {
1129 int htype = decode_uleb128 (p, &p);
1130 uint32_t handle = decode_uleb128 (p, &p);
1131 intptr_t objdiff = decode_sleb128 (p, &p);
1134 handle_info [htype].created++;
1135 add_trace_thread (thread, &handle_info [htype].traces, 1);
1136 /* FIXME: we don't take into account timing here */
1137 if (handle_info [htype].created > handle_info [htype].max_live)
1138 handle_info [htype].max_live = handle_info [htype].created;
1139 if (num_tracked_objects)
1140 track_handle (OBJ_ADDR (objdiff), htype, handle);
1142 fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
1143 } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
1144 int htype = decode_uleb128 (p, &p);
1145 uint32_t handle = decode_uleb128 (p, &p);
1148 handle_info [htype].created--;
1150 fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
1154 case TYPE_METADATA: {
1155 int error = *p & TYPE_LOAD_ERR;
1156 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1158 intptr_t ptrdiff = decode_sleb128 (p, &p);
1159 LOG_TIME (time_base, tdiff);
1161 if (mtype == TYPE_CLASS) {
1162 intptr_t imptrdiff = decode_sleb128 (p, &p);
1163 uint64_t flags = decode_uleb128 (p, &p);
1165 fprintf (outfile, "non-zero flags in class\n");
1169 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
1171 add_class (ptr_base + ptrdiff, (char*)p);
1174 } else if (mtype == TYPE_IMAGE) {
1175 uint64_t flags = decode_uleb128 (p, &p);
1177 fprintf (outfile, "non-zero flags in image\n");
1181 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
1183 add_image (ptr_base + ptrdiff, (char*)p);
1186 } else if (mtype == TYPE_THREAD) {
1188 uint64_t flags = decode_uleb128 (p, &p);
1190 fprintf (outfile, "non-zero flags in thread\n");
1193 nt = get_thread (ctx, ptr_base * ptrdiff);
1194 nt->name = pstrdup ((char*)p);
1196 fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
1203 int has_bt = *p & TYPE_ALLOC_BT;
1204 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1205 intptr_t ptrdiff = decode_sleb128 (p, &p);
1206 intptr_t objdiff = decode_sleb128 (p, &p);
1209 MethodDesc* sframes [8];
1210 MethodDesc** frames = sframes;
1211 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1212 len = decode_uleb128 (p, &p);
1213 LOG_TIME (time_base, tdiff);
1216 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
1219 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1221 fprintf (outfile, "Cannot load backtrace\n");
1225 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
1228 cd->alloc_size += len;
1230 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
1232 bt = add_trace_thread (thread, &cd->traces, len);
1233 if (find_size && len >= find_size) {
1234 if (!find_name || strstr (cd->name, find_name))
1235 found_object (OBJ_ADDR (objdiff));
1236 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
1237 found_object (OBJ_ADDR (objdiff));
1239 if (num_tracked_objects)
1240 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
1242 if (frames != sframes)
1247 int subtype = *p & 0xf0;
1248 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1249 int64_t ptrdiff = decode_sleb128 (p, &p);
1250 LOG_TIME (time_base, tdiff);
1252 method_base += ptrdiff;
1253 if (subtype == TYPE_JIT) {
1254 intptr_t codediff = decode_sleb128 (p, &p);
1255 int codelen = decode_uleb128 (p, &p);
1257 fprintf (outfile, "jitted method %p (%s), size: %d\n", (void*)(method_base), p, codelen);
1258 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
1263 if ((thread_filter && thread_filter != thread->thread_id))
1265 method = lookup_method (method_base);
1266 if (subtype == TYPE_ENTER) {
1267 add_trace_thread (thread, &method->traces, 1);
1268 push_method (thread, method, time_base);
1270 pop_method (thread, method, time_base);
1273 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
1278 int subtype = *p & 0xf0;
1279 if (subtype == TYPE_HEAP_OBJECT) {
1282 intptr_t objdiff = decode_sleb128 (p + 1, &p);
1283 intptr_t ptrdiff = decode_sleb128 (p, &p);
1284 uint64_t size = decode_uleb128 (p, &p);
1285 uintptr_t num = decode_uleb128 (p, &p);
1286 uintptr_t ref_offset;
1287 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1289 HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
1290 if (collect_traces) {
1291 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
1292 add_heap_shot_obj (thread->current_heap_shot, ho);
1297 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
1299 for (i = 0; i < num; ++i) {
1300 /* FIXME: use object distance to measure how good
1301 * the GC is at keeping related objects close
1303 intptr_t obj1diff = decode_sleb128 (p, &p);
1305 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
1306 if (num_tracked_objects)
1307 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
1310 fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
1311 } else if (subtype == TYPE_HEAP_END) {
1312 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1313 LOG_TIME (time_base, tdiff);
1316 fprintf (outfile, "heap shot end\n");
1317 if (collect_traces) {
1318 heap_shot_resolve_reverse_refs (thread->current_heap_shot);
1319 heap_shot_free_objects (thread->current_heap_shot);
1321 thread->current_heap_shot = NULL;
1322 } else if (subtype == TYPE_HEAP_START) {
1323 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1324 LOG_TIME (time_base, tdiff);
1327 fprintf (outfile, "heap shot start\n");
1328 thread->current_heap_shot = new_heap_shot (time_base);
1332 case TYPE_MONITOR: {
1333 int event = (*p >> 4) & 0x3;
1334 int has_bt = *p & TYPE_MONITOR_BT;
1335 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1336 intptr_t objdiff = decode_sleb128 (p, &p);
1337 MethodDesc* sframes [8];
1338 MethodDesc** frames = sframes;
1341 LOG_TIME (time_base, tdiff);
1343 record = (!thread_filter || thread_filter == thread->thread_id);
1344 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
1345 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
1347 monitor_contention++;
1348 mdesc->contentions++;
1349 thread->monitor = mdesc;
1350 thread->contention_start = time_base;
1354 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1356 fprintf (outfile, "Cannot load backtrace\n");
1360 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1363 add_trace_thread (thread, &mdesc->traces, 1);
1365 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1368 if (thread->monitor && thread->contention_start) {
1369 uint64_t wait_time = time_base - thread->contention_start;
1370 if (wait_time > thread->monitor->max_wait_time)
1371 thread->monitor->max_wait_time = wait_time;
1372 thread->monitor->wait_time += wait_time;
1373 thread->monitor = NULL;
1374 thread->contention_start = 0;
1377 } else if (event == MONO_PROFILER_MONITOR_DONE) {
1380 if (thread->monitor && thread->contention_start) {
1381 uint64_t wait_time = time_base - thread->contention_start;
1382 if (wait_time > thread->monitor->max_wait_time)
1383 thread->monitor->max_wait_time = wait_time;
1384 thread->monitor->wait_time += wait_time;
1385 thread->monitor = NULL;
1386 thread->contention_start = 0;
1391 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1392 if (frames != sframes)
1396 case TYPE_EXCEPTION: {
1397 int subtype = *p & 0x70;
1398 int has_bt = *p & TYPE_EXCEPTION_BT;
1399 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1400 MethodDesc* sframes [8];
1401 MethodDesc** frames = sframes;
1403 LOG_TIME (time_base, tdiff);
1405 record = (!thread_filter || thread_filter == thread->thread_id);
1406 if (subtype == TYPE_CLAUSE) {
1407 int clause_type = decode_uleb128 (p, &p);
1408 int clause_num = decode_uleb128 (p, &p);
1409 int64_t ptrdiff = decode_sleb128 (p, &p);
1410 method_base += ptrdiff;
1412 clause_summary [clause_type]++;
1414 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1416 intptr_t objdiff = decode_sleb128 (p, &p);
1421 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1423 fprintf (outfile, "Cannot load backtrace\n");
1427 add_trace_methods (frames, has_bt, &exc_traces, 1);
1430 add_trace_thread (thread, &exc_traces, 1);
1432 if (frames != sframes)
1435 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1440 fprintf (outfile, "unhandled profiler event: 0x%x\n", *p);
1444 thread->last_time = time_base;
1445 for (i = 0; i < thread->stack_id; ++i)
1446 thread->stack [i]->recurse_count = 0;
1451 load_file (char *name)
1454 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1455 if (strcmp (name, "-") == 0)
1458 ctx->file = fopen (name, "rb");
1460 printf ("Cannot open file: %s\n", name);
1463 #if defined (HAVE_ZLIB)
1464 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
1466 if (!load_data (ctx, 32))
1469 if (read_int32 (p) != LOG_HEADER_ID || p [6] != LOG_DATA_VERSION)
1471 ctx->version_major = p [4];
1472 ctx->version_minor = p [5];
1473 ctx->data_version = p [6];
1474 /* reading 64 bit files on 32 bit systems not supported yet */
1475 if (p [7] > sizeof (void*))
1477 if (read_int32 (p + 20)) /* flags must be 0 */
1479 ctx->startup_time = read_int64 (p + 8);
1480 ctx->timer_overhead = read_int32 (p + 16);
1488 static int alloc_sort_mode = ALLOC_SORT_BYTES;
1491 compare_class (const void *a, const void *b)
1493 ClassDesc *const*A = a;
1494 ClassDesc *const*B = b;
1495 uint64_t vala, valb;
1496 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1497 vala = (*A)->alloc_size;
1498 valb = (*B)->alloc_size;
1500 vala = (*A)->allocs;
1501 valb = (*B)->allocs;
1511 dump_header (ProfContext *ctx)
1513 time_t st = ctx->startup_time / 1000;
1514 char *t = ctime (&st);
1515 fprintf (outfile, "\nMono log profiler data\n");
1516 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
1517 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
1518 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
1519 fprintf (outfile, "\tProgram startup: %s\n", t);
1523 dump_traces (TraceDesc *traces, const char *desc)
1530 sort_context_array (traces);
1531 for (j = 0; j < traces->count; ++j) {
1534 bt = traces->traces [j].bt;
1537 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
1538 for (k = 0; k < bt->count; ++k)
1539 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
1544 dump_threads (ProfContext *ctx)
1546 ThreadContext *thread;
1547 fprintf (outfile, "\nThread summary\n");
1548 for (thread = ctx->threads; thread; thread = thread->next) {
1549 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
1554 dump_exceptions (void)
1557 fprintf (outfile, "\nException summary\n");
1558 fprintf (outfile, "\tThrows: %llu\n", throw_count);
1559 dump_traces (&exc_traces, "throws");
1560 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
1561 if (!clause_summary [i])
1563 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
1568 compare_monitor (const void *a, const void *b)
1570 MonitorDesc *const*A = a;
1571 MonitorDesc *const*B = b;
1572 if ((*B)->wait_time == (*A)->wait_time)
1574 if ((*B)->wait_time < (*A)->wait_time)
1580 dump_monitors (void)
1582 MonitorDesc **monitors;
1586 monitors = malloc (sizeof (void*) * num_monitors);
1587 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
1588 MonitorDesc *mdesc = monitor_hash [i];
1590 monitors [j++] = mdesc;
1591 mdesc = mdesc->next;
1594 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
1595 fprintf (outfile, "\nMonitor lock summary\n");
1596 for (i = 0; i < num_monitors; ++i) {
1597 MonitorDesc *mdesc = monitors [i];
1598 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
1599 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
1600 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
1601 dump_traces (&mdesc->traces, "contentions");
1603 fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
1604 fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
1605 fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
1612 fprintf (outfile, "\nGC summary\n");
1613 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
1614 fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
1615 fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
1616 for (i = 0; i < 3; ++i) {
1617 if (!gc_info [i].count)
1619 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
1620 i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
1621 gc_info [i].total_time / gc_info [i].count / 1000);
1623 for (i = 0; i < 3; ++i) {
1624 if (!handle_info [i].max_live)
1626 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
1627 get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
1628 dump_traces (&handle_info [i].traces, "created");
1633 dump_allocations (void)
1636 intptr_t allocs = 0;
1638 int header_done = 0;
1639 ClassDesc **classes = malloc (num_classes * sizeof (void*));
1642 for (i = 0; i < HASH_SIZE; ++i) {
1643 cd = class_hash [i];
1649 qsort (classes, num_classes, sizeof (void*), compare_class);
1650 for (i = 0; i < num_classes; ++i) {
1654 allocs += cd->allocs;
1655 size += cd->alloc_size;
1656 if (!header_done++) {
1657 fprintf (outfile, "\nAllocation summary\n");
1658 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
1660 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
1661 dump_traces (&cd->traces, "bytes");
1664 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
1673 static int method_sort_mode = METHOD_SORT_TOTAL;
1676 compare_method (const void *a, const void *b)
1678 MethodDesc *const*A = a;
1679 MethodDesc *const*B = b;
1680 uint64_t vala, valb;
1681 if (method_sort_mode == METHOD_SORT_SELF) {
1682 vala = (*A)->self_time;
1683 valb = (*B)->self_time;
1684 } else if (method_sort_mode == METHOD_SORT_CALLS) {
1688 vala = (*A)->total_time;
1689 valb = (*B)->total_time;
1699 dump_metadata (void)
1701 fprintf (outfile, "\nMetadata summary\n");
1702 fprintf (outfile, "\tLoaded images: %d\n", num_images);
1706 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
1707 image = image_hash [i];
1709 fprintf (outfile, "\t\t%s\n", image->filename);
1710 image = image->next;
1722 int header_done = 0;
1723 MethodDesc **methods = malloc (num_methods * sizeof (void*));
1726 for (i = 0; i < HASH_SIZE; ++i) {
1727 cd = method_hash [i];
1729 cd->total_time = cd->self_time + cd->callee_time;
1734 qsort (methods, num_methods, sizeof (void*), compare_method);
1735 for (i = 0; i < num_methods; ++i) {
1742 msecs = cd->total_time / 1000000;
1743 smsecs = (cd->total_time - cd->callee_time) / 1000000;
1744 if (!msecs && !verbose)
1746 if (!header_done++) {
1747 fprintf (outfile, "\nMethod call summary\n");
1748 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
1750 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
1751 dump_traces (&cd->traces, "calls");
1754 fprintf (outfile, "Total calls: %llu\n", calls);
1758 compare_heap_class (const void *a, const void *b)
1760 HeapClassDesc *const*A = a;
1761 HeapClassDesc *const*B = b;
1762 uint64_t vala, valb;
1763 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1764 vala = (*A)->total_size;
1765 valb = (*B)->total_size;
1778 compare_rev_class (const void *a, const void *b)
1780 const HeapClassRevRef *A = a;
1781 const HeapClassRevRef *B = b;
1782 if (B->count == A->count)
1784 if (B->count < A->count)
1790 dump_rev_claases (HeapClassRevRef *revs, int count)
1797 for (j = 0; j < count; ++j) {
1798 HeapClassDesc *cd = revs [j].klass;
1799 fprintf (outfile, "\t\t%llu references from: %s\n", revs [j].count, cd->klass->name);
1804 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
1811 HeapClassDesc **sorted;
1812 sorted = malloc (sizeof (void*) * hs->class_count);
1813 for (i = 0; i < hs->hash_size; ++i) {
1814 cd = hs->class_hash [i];
1818 size += cd->total_size;
1819 sorted [ccount++] = cd;
1821 hs->sorted = sorted;
1822 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
1823 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d\n",
1824 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount);
1825 if (!verbose && ccount > 30)
1827 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
1828 for (i = 0; i < ccount; ++i) {
1829 HeapClassRevRef *rev_sorted;
1831 HeapClassDesc *ocd = NULL;
1834 ocd = heap_class_lookup (last_hs, cd->klass);
1835 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
1837 int64_t bdiff = cd->total_size - ocd->total_size;
1838 int64_t cdiff = cd->count - ocd->count;
1839 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
1841 fprintf (outfile, "\n");
1843 if (!collect_traces)
1845 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
1847 for (j = 0; j < cd->rev_hash_size; ++j) {
1848 if (cd->rev_hash [j].klass)
1849 rev_sorted [k++] = cd->rev_hash [j];
1851 assert (cd->rev_count == k);
1852 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
1853 dump_rev_claases (rev_sorted, cd->rev_count);
1860 compare_heap_shots (const void *a, const void *b)
1862 HeapShot *const*A = a;
1863 HeapShot *const*B = b;
1864 if ((*B)->timestamp == (*A)->timestamp)
1866 if ((*B)->timestamp > (*A)->timestamp)
1872 dump_heap_shots (void)
1874 HeapShot **hs_sorted;
1876 HeapShot *last_hs = NULL;
1880 hs_sorted = malloc (num_heap_shots * sizeof (void*));
1881 fprintf (outfile, "\nHeap shot summary\n");
1883 for (hs = heap_shots; hs; hs = hs->next)
1884 hs_sorted [i++] = hs;
1885 qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
1886 for (i = 0; i < num_heap_shots; ++i) {
1888 heap_shot_summary (hs, i, last_hs);
1894 flush_context (ProfContext *ctx)
1896 ThreadContext *thread;
1897 /* FIXME: sometimes there are leftovers: indagate */
1898 for (thread = ctx->threads; thread; thread = thread->next) {
1899 while (thread->stack_id) {
1901 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
1902 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
1907 static const char *reports = "header,gc,alloc,call,metadata,exception,monitor,thread,heapshot";
1910 match_option (const char *p, const char *opt)
1912 int len = strlen (opt);
1913 if (strncmp (p, opt, len) == 0) {
1922 print_reports (ProfContext *ctx, const char *reps, int parse_only)
1926 for (p = reps; *p; p = opt) {
1927 if ((opt = match_option (p, "header")) != p) {
1932 if ((opt = match_option (p, "thread")) != p) {
1937 if ((opt = match_option (p, "gc")) != p) {
1942 if ((opt = match_option (p, "alloc")) != p) {
1944 dump_allocations ();
1947 if ((opt = match_option (p, "call")) != p) {
1952 if ((opt = match_option (p, "metadata")) != p) {
1957 if ((opt = match_option (p, "exception")) != p) {
1962 if ((opt = match_option (p, "monitor")) != p) {
1967 if ((opt = match_option (p, "heapshot")) != p) {
1978 add_find_spec (const char *p)
1980 if (p [0] == 'S' && p [1] == ':') {
1982 find_size = strtoul (p + 2, &vale, 10);
1984 } else if (p [0] == 'T' && p [1] == ':') {
1994 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
1995 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
1996 printf ("FILENAME can be '-' to read from standard input.\n");
1997 printf ("Options:\n");
1998 printf ("\t--help display this help\n");
1999 printf ("\t--out=FILE write to FILE instead of stdout\n");
2000 printf ("\t--traces collect and show backtraces\n");
2001 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
2002 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
2003 printf ("\t %s\n", reports);
2004 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
2005 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
2006 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
2007 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
2008 printf ("\t S:minimum_size or T:partial_name\n");
2009 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
2010 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
2011 printf ("\t--verbose increase verbosity level\n");
2012 printf ("\t--debug display decoding debug info for mprof-report devs\n");
2016 main (int argc, char *argv[])
2021 for (i = 1; i < argc; ++i) {
2022 if (strcmp ("--debug", argv [i]) == 0) {
2024 } else if (strcmp ("--help", argv [i]) == 0) {
2027 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
2028 const char *val = argv [i] + 13;
2029 if (strcmp (val, "bytes") == 0) {
2030 alloc_sort_mode = ALLOC_SORT_BYTES;
2031 } else if (strcmp (val, "count") == 0) {
2032 alloc_sort_mode = ALLOC_SORT_COUNT;
2037 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
2038 const char *val = argv [i] + 14;
2039 if (strcmp (val, "total") == 0) {
2040 method_sort_mode = METHOD_SORT_TOTAL;
2041 } else if (strcmp (val, "self") == 0) {
2042 method_sort_mode = METHOD_SORT_SELF;
2043 } else if (strcmp (val, "calls") == 0) {
2044 method_sort_mode = METHOD_SORT_CALLS;
2049 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
2050 const char *val = argv [i] + 10;
2051 if (!print_reports (NULL, val, 1)) {
2056 } else if (strncmp ("--out=", argv [i], 6) == 0) {
2057 const char *val = argv [i] + 6;
2058 outfile = fopen (val, "w");
2060 printf ("Cannot open output file: %s\n", val);
2063 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
2064 const char *val = argv [i] + 12;
2066 trace_max = strtoul (val, &vale, 10);
2067 } else if (strncmp ("--find=", argv [i], 7) == 0) {
2068 const char *val = argv [i] + 7;
2069 if (!add_find_spec (val)) {
2073 } else if (strncmp ("--track=", argv [i], 8) == 0) {
2074 const char *val = argv [i] + 8;
2077 uintptr_t tracked_obj;
2082 tracked_obj = strtoul (val, &vale, 0);
2083 found_object (tracked_obj);
2086 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
2087 const char *val = argv [i] + 9;
2089 thread_filter = strtoul (val, &vale, 0);
2090 } else if (strncmp ("--time=", argv [i], 7) == 0) {
2091 char *val = pstrdup (argv [i] + 7);
2092 double from_secs, to_secs;
2093 char *top = strchr (val, '-');
2099 from_secs = atof (val);
2100 to_secs = atof (top);
2102 if (from_secs > to_secs) {
2106 time_from = from_secs * 1000000000;
2107 time_to = to_secs * 1000000000;
2108 } else if (strcmp ("--verbose", argv [i]) == 0) {
2110 } else if (strcmp ("--traces", argv [i]) == 0) {
2121 ctx = load_file (argv [i]);
2123 printf ("Not a log profiler data file (or unsupported version).\n");
2126 while (decode_buffer (ctx));
2127 flush_context (ctx);
2128 if (num_tracked_objects)
2130 print_reports (ctx, reports, 0);