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__)
20 #if defined (HAVE_SYS_ZLIB)
23 #include <mono/metadata/profiler.h>
24 #include <mono/metadata/object.h>
25 #include <mono/metadata/debug-helpers.h>
27 #define HASH_SIZE 9371
28 #define SMALL_HASH_SIZE 31
31 static int collect_traces = 0;
32 static int show_traces = 0;
33 static int trace_max = 6;
34 static int verbose = 0;
35 static uintptr_t *tracked_objects = 0;
36 static int num_tracked_objects = 0;
37 static uintptr_t thread_filter = 0;
38 static uint64_t find_size = 0;
39 static const char* find_name = NULL;
40 static uint64_t time_from = 0;
41 static uint64_t time_to = 0xffffffffffffffffULL;
42 static uint64_t startup_time = 0;
43 static FILE* outfile = NULL;
46 read_int32 (unsigned char *p)
50 value |= (*p++) << 16;
51 value |= (uint32_t)(*p++) << 24;
56 read_int64 (unsigned char *p)
58 uint64_t value = *p++;
60 value |= (*p++) << 16;
61 value |= (uint64_t)(*p++) << 24;
62 value |= (uint64_t)(*p++) << 32;
63 value |= (uint64_t)(*p++) << 40;
64 value |= (uint64_t)(*p++) << 48;
65 value |= (uint64_t)(*p++) << 54;
70 pstrdup (const char *s)
72 int len = strlen (s) + 1;
73 char *p = malloc (len);
78 static int num_images;
79 typedef struct _ImageDesc ImageDesc;
86 static ImageDesc* image_hash [SMALL_HASH_SIZE] = {0};
89 add_image (intptr_t image, char *name)
91 int slot = ((image >> 2) & 0xffff) % SMALL_HASH_SIZE;
92 ImageDesc *cd = malloc (sizeof (ImageDesc));
94 cd->filename = pstrdup (name);
95 cd->next = image_hash [slot];
96 image_hash [slot] = cd;
100 typedef struct _BackTrace BackTrace;
112 typedef struct _ClassDesc ClassDesc;
122 static ClassDesc* class_hash [HASH_SIZE] = {0};
123 static int num_classes = 0;
126 add_class (intptr_t klass, const char *name)
128 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
130 cd = class_hash [slot];
131 while (cd && cd->klass != klass)
133 /* we resolved an unknown class (unless we had the code unloaded) */
135 /*printf ("resolved unknown: %s\n", name);*/
137 cd->name = pstrdup (name);
140 cd = calloc (sizeof (ClassDesc), 1);
142 cd->name = pstrdup (name);
143 cd->next = class_hash [slot];
146 cd->traces.count = 0;
148 cd->traces.traces = NULL;
149 class_hash [slot] = cd;
155 lookup_class (intptr_t klass)
157 int slot = ((klass >> 2) & 0xffff) % HASH_SIZE;
158 ClassDesc *cd = class_hash [slot];
159 while (cd && cd->klass != klass)
162 return add_class (klass, "unresolved class");
166 typedef struct _MethodDesc MethodDesc;
176 uint64_t callee_time;
181 static MethodDesc* method_hash [HASH_SIZE] = {0};
182 static int num_methods = 0;
185 add_method (intptr_t method, const char *name, intptr_t code, int len)
187 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
189 cd = method_hash [slot];
190 while (cd && cd->method != method)
192 /* we resolved an unknown method (unless we had the code unloaded) */
196 /*printf ("resolved unknown: %s\n", name);*/
198 cd->name = pstrdup (name);
201 cd = calloc (sizeof (MethodDesc), 1);
203 cd->name = pstrdup (name);
208 cd->traces.count = 0;
210 cd->traces.traces = NULL;
211 cd->next = method_hash [slot];
212 method_hash [slot] = cd;
218 lookup_method (intptr_t method)
220 int slot = ((method >> 2) & 0xffff) % HASH_SIZE;
221 MethodDesc *cd = method_hash [slot];
222 while (cd && cd->method != method)
225 return add_method (method, "unknown method", 0, 0);
229 typedef struct _HeapClassDesc HeapClassDesc;
231 HeapClassDesc *klass;
235 struct _HeapClassDesc {
239 HeapClassRevRef *rev_hash;
245 add_rev_class_hashed (HeapClassRevRef *rev_hash, uintptr_t size, HeapClassDesc *hklass, uint64_t value)
249 start_pos = (hklass->klass->klass >> 2) % size;
250 assert (start_pos < size);
253 if (rev_hash [i].klass == hklass) {
254 rev_hash [i].count += value;
256 } else if (!rev_hash [i].klass) {
257 rev_hash [i].klass = hklass;
258 rev_hash [i].count += value;
260 for (i = 0; i < size; ++i)
261 if (rev_hash [i].klass && rev_hash [i].klass->klass == hklass->klass)
263 assert (start_pos == 1);
269 } while (i != start_pos);
270 /* should not happen */
271 printf ("failed revref store\n");
276 add_heap_class_rev (HeapClassDesc *from, HeapClassDesc *to)
279 if (to->rev_count * 2 >= to->rev_hash_size) {
281 uintptr_t old_size = to->rev_hash_size;
282 to->rev_hash_size *= 2;
283 if (to->rev_hash_size == 0)
284 to->rev_hash_size = 4;
285 n = calloc (sizeof (HeapClassRevRef) * to->rev_hash_size, 1);
286 for (i = 0; i < old_size; ++i) {
287 if (to->rev_hash [i].klass)
288 add_rev_class_hashed (n, to->rev_hash_size, to->rev_hash [i].klass, to->rev_hash [i].count);
294 to->rev_count += add_rev_class_hashed (to->rev_hash, to->rev_hash_size, from, 1);
299 HeapClassDesc *hklass;
304 typedef struct _HeapShot HeapShot;
310 HeapClassDesc **class_hash;
311 HeapClassDesc **sorted;
312 HeapObjectDesc **objects_hash;
313 uintptr_t objects_count;
314 uintptr_t objects_hash_size;
317 static HeapShot *heap_shots = NULL;
318 static int num_heap_shots = 0;
321 new_heap_shot (uint64_t timestamp)
323 HeapShot *hs = calloc (sizeof (HeapShot), 1);
325 hs->class_hash = calloc (sizeof (void*), hs->hash_size);
326 hs->timestamp = timestamp;
328 hs->next = heap_shots;
333 static HeapClassDesc*
334 heap_class_lookup (HeapShot *hs, ClassDesc *klass)
337 unsigned int start_pos;
338 start_pos = ((uintptr_t)klass->klass >> 2) % hs->hash_size;
341 HeapClassDesc* cd = hs->class_hash [i];
344 if (cd->klass == klass)
347 if (++i == hs->hash_size)
349 } while (i != start_pos);
354 add_heap_hashed (HeapClassDesc **hash, HeapClassDesc **retv, uintptr_t hsize, ClassDesc *klass, uint64_t size, uint64_t count)
358 start_pos = ((uintptr_t)klass->klass >> 2) % hsize;
361 if (hash [i] && hash [i]->klass == klass) {
362 hash [i]->total_size += size;
363 hash [i]->count += count;
366 } else if (!hash [i]) {
371 hash [i] = calloc (sizeof (HeapClassDesc), 1);
372 hash [i]->klass = klass;
373 hash [i]->total_size += size;
374 hash [i]->count += count;
381 } while (i != start_pos);
382 /* should not happen */
383 printf ("failed heap class store\n");
387 static HeapClassDesc*
388 add_heap_shot_class (HeapShot *hs, ClassDesc *klass, uint64_t size)
392 if (hs->class_count * 2 >= hs->hash_size) {
394 int old_size = hs->hash_size;
396 if (hs->hash_size == 0)
398 n = calloc (sizeof (void*) * hs->hash_size, 1);
399 for (i = 0; i < old_size; ++i) {
400 res = hs->class_hash [i];
401 if (hs->class_hash [i])
402 add_heap_hashed (n, &res, hs->hash_size, hs->class_hash [i]->klass, hs->class_hash [i]->total_size, hs->class_hash [i]->count);
405 free (hs->class_hash);
409 hs->class_count += add_heap_hashed (hs->class_hash, &res, hs->hash_size, klass, size, 1);
410 //if (res->count == 1)
411 // printf ("added heap class: %s\n", res->klass->name);
415 static HeapObjectDesc*
416 alloc_heap_obj (uintptr_t objaddr, HeapClassDesc *hklass, uintptr_t num_refs)
418 HeapObjectDesc* ho = calloc (sizeof (HeapObjectDesc) + num_refs * sizeof (uintptr_t), 1);
419 ho->objaddr = objaddr;
421 ho->num_refs = num_refs;
426 heap_shot_find_obj_slot (HeapShot *hs, uintptr_t objaddr)
430 HeapObjectDesc **hash = hs->objects_hash;
431 start_pos = ((uintptr_t)objaddr >> 3) % hs->objects_hash_size;
434 if (hash [i] && hash [i]->objaddr == objaddr) {
436 } else if (!hash [i]) {
440 if (++i == hs->objects_hash_size)
442 } while (i != start_pos);
443 /* should not happen */
444 printf ("failed heap obj update\n");
448 static HeapObjectDesc*
449 heap_shot_obj_add_refs (HeapShot *hs, uintptr_t objaddr, uintptr_t num, uintptr_t *ref_offset)
451 HeapObjectDesc **hash = hs->objects_hash;
452 uintptr_t i = heap_shot_find_obj_slot (hs, objaddr);
454 HeapObjectDesc* ho = alloc_heap_obj (objaddr, hash [i]->hklass, hash [i]->num_refs + num);
455 *ref_offset = hash [i]->num_refs;
456 memcpy (ho->refs, hash [i]->refs, hash [i]->num_refs * sizeof (uintptr_t));
461 /* should not happen */
462 printf ("failed heap obj update\n");
468 add_heap_hashed_obj (HeapObjectDesc **hash, uintptr_t hsize, HeapObjectDesc *obj)
472 start_pos = ((uintptr_t)obj->objaddr >> 3) % hsize;
475 if (hash [i] && hash [i]->objaddr == obj->objaddr) {
476 printf ("duplicate object!\n");
478 } else if (!hash [i]) {
485 } while (i != start_pos);
486 /* should not happen */
487 printf ("failed heap obj store\n");
492 add_heap_shot_obj (HeapShot *hs, HeapObjectDesc *obj)
495 if (hs->objects_count * 2 >= hs->objects_hash_size) {
497 uintptr_t old_size = hs->objects_hash_size;
498 hs->objects_hash_size *= 2;
499 if (hs->objects_hash_size == 0)
500 hs->objects_hash_size = 4;
501 n = calloc (sizeof (void*) * hs->objects_hash_size, 1);
502 for (i = 0; i < old_size; ++i) {
503 if (hs->objects_hash [i])
504 add_heap_hashed_obj (n, hs->objects_hash_size, hs->objects_hash [i]);
506 if (hs->objects_hash)
507 free (hs->objects_hash);
508 hs->objects_hash = n;
510 hs->objects_count += add_heap_hashed_obj (hs->objects_hash, hs->objects_hash_size, obj);
514 heap_shot_resolve_reverse_refs (HeapShot *hs)
517 for (i = 0; i < hs->objects_hash_size; ++i) {
519 HeapObjectDesc *ho = hs->objects_hash [i];
522 for (r = 0; r < ho->num_refs; ++r) {
523 uintptr_t oi = heap_shot_find_obj_slot (hs, ho->refs [r]);
524 add_heap_class_rev (ho->hklass, hs->objects_hash [oi]->hklass);
530 heap_shot_free_objects (HeapShot *hs)
533 for (i = 0; i < hs->objects_hash_size; ++i) {
534 HeapObjectDesc *ho = hs->objects_hash [i];
538 if (hs->objects_hash)
539 free (hs->objects_hash);
540 hs->objects_hash = NULL;
541 hs->objects_hash_size = 0;
542 hs->objects_count = 0;
550 MethodDesc *methods [1];
553 static BackTrace *backtrace_hash [HASH_SIZE];
554 static BackTrace **backtraces = NULL;
555 static int num_backtraces = 0;
556 static int next_backtrace = 0;
559 hash_backtrace (int count, MethodDesc **methods)
563 for (i = 0; i < count; ++i) {
564 hash = (hash << 5) - hash + methods [i]->method;
570 compare_backtrace (BackTrace *bt, int count, MethodDesc **methods)
573 if (bt->count != count)
575 for (i = 0; i < count; ++i)
576 if (methods [i] != bt->methods [i])
582 add_backtrace (int count, MethodDesc **methods)
584 int hash = hash_backtrace (count, methods);
585 int slot = (hash & 0xffff) % HASH_SIZE;
586 BackTrace *bt = backtrace_hash [slot];
588 if (bt->hash == hash && compare_backtrace (bt, count, methods))
592 bt = malloc (sizeof (BackTrace) + ((count - 1) * sizeof (void*)));
593 bt->next = backtrace_hash [slot];
594 backtrace_hash [slot] = bt;
595 if (next_backtrace == num_backtraces) {
599 backtraces = realloc (backtraces, sizeof (void*) * num_backtraces);
601 bt->id = next_backtrace++;
602 backtraces [bt->id] = bt;
605 for (slot = 0; slot < count; ++slot)
606 bt->methods [slot] = methods [slot];
611 typedef struct _MonitorDesc MonitorDesc;
612 typedef struct _ThreadContext ThreadContext;
616 #if defined (HAVE_SYS_ZLIB)
626 uint64_t startup_time;
627 ThreadContext *threads;
628 ThreadContext *current;
631 struct _ThreadContext {
637 uint64_t *time_stack;
638 uint64_t *callee_time_stack;
640 uint64_t contention_start;
641 MonitorDesc *monitor;
644 HeapShot *current_heap_shot;
648 ensure_buffer (ProfContext *ctx, int size)
650 if (ctx->size < size) {
651 ctx->buf = realloc (ctx->buf, size);
657 load_data (ProfContext *ctx, int size)
659 ensure_buffer (ctx, size);
660 #if defined (HAVE_SYS_ZLIB)
662 int r = gzread (ctx->gzfile, ctx->buf, size);
664 return size == 0? 1: 0;
668 int r = fread (ctx->buf, size, 1, ctx->file);
670 return size == 0? 1: 0;
675 static ThreadContext*
676 get_thread (ProfContext *ctx, intptr_t thread_id)
678 ThreadContext *thread;
679 if (ctx->current && ctx->current->thread_id == thread_id)
681 thread = ctx->threads;
683 if (thread->thread_id == thread_id) {
686 thread = thread->next;
688 thread = calloc (sizeof (ThreadContext), 1);
689 thread->next = ctx->threads;
690 ctx->threads = thread;
691 thread->thread_id = thread_id;
692 thread->last_time = 0;
693 thread->stack_id = 0;
694 thread->stack_size = 32;
695 thread->stack = malloc (thread->stack_size * sizeof (void*));
696 thread->time_stack = malloc (thread->stack_size * sizeof (uint64_t));
697 thread->callee_time_stack = malloc (thread->stack_size * sizeof (uint64_t));
701 static ThreadContext*
702 load_thread (ProfContext *ctx, intptr_t thread_id)
704 ThreadContext *thread = get_thread (ctx, thread_id);
705 ctx->current = thread;
710 ensure_thread_stack (ThreadContext *thread)
712 if (thread->stack_id == thread->stack_size) {
713 thread->stack_size *= 2;
714 thread->stack = realloc (thread->stack, thread->stack_size * sizeof (void*));
715 thread->time_stack = realloc (thread->time_stack, thread->stack_size * sizeof (uint64_t));
716 thread->callee_time_stack = realloc (thread->callee_time_stack, thread->stack_size * sizeof (uint64_t));
721 add_trace_hashed (CallContext *traces, int size, BackTrace *bt, uint64_t value)
724 unsigned int start_pos;
725 start_pos = bt->hash % size;
728 if (traces [i].bt == bt) {
729 traces [i].count += value;
731 } else if (!traces [i].bt) {
733 traces [i].count += value;
739 } while (i != start_pos);
740 /* should not happen */
741 printf ("failed trace store\n");
746 add_trace_bt (BackTrace *bt, TraceDesc *trace, uint64_t value)
751 if (trace->count * 2 >= trace->size) {
753 int old_size = trace->size;
755 if (trace->size == 0)
757 n = calloc (sizeof (CallContext) * trace->size, 1);
758 for (i = 0; i < old_size; ++i) {
759 if (trace->traces [i].bt)
760 add_trace_hashed (n, trace->size, trace->traces [i].bt, trace->traces [i].count);
763 free (trace->traces);
766 trace->count += add_trace_hashed (trace->traces, trace->size, bt, value);
770 add_trace_thread (ThreadContext *thread, TraceDesc *trace, uint64_t value)
773 int count = thread->stack_id;
776 if (count > trace_max)
778 bt = add_backtrace (count, thread->stack + thread->stack_id - count);
779 add_trace_bt (bt, trace, value);
784 add_trace_methods (MethodDesc **methods, int count, TraceDesc *trace, uint64_t value)
789 if (count > trace_max)
791 bt = add_backtrace (count, methods);
792 add_trace_bt (bt, trace, value);
797 compare_callc (const void *a, const void *b)
799 const CallContext *A = a;
800 const CallContext *B = b;
801 if (B->count == A->count)
803 if (B->count < A->count)
809 sort_context_array (TraceDesc* traces)
812 for (i = 0, j = 0; i < traces->size; ++i) {
813 if (traces->traces [i].bt) {
814 traces->traces [j].bt = traces->traces [i].bt;
815 traces->traces [j].count = traces->traces [i].count;
819 qsort (traces->traces, traces->count, sizeof (CallContext), compare_callc);
823 push_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
825 ensure_thread_stack (thread);
826 thread->time_stack [thread->stack_id] = timestamp;
827 thread->callee_time_stack [thread->stack_id] = 0;
828 thread->stack [thread->stack_id++] = method;
829 method->recurse_count++;
833 pop_method (ThreadContext *thread, MethodDesc *method, uint64_t timestamp)
835 method->recurse_count--;
836 if (thread->stack_id > 0 && thread->stack [thread->stack_id - 1] == method) {
840 if (timestamp < thread->time_stack [thread->stack_id])
841 fprintf (outfile, "time went backwards for %s\n", method->name);
842 tdiff = timestamp - thread->time_stack [thread->stack_id];
843 if (thread->callee_time_stack [thread->stack_id] > tdiff)
844 fprintf (outfile, "callee time bigger for %s\n", method->name);
845 method->self_time += tdiff - thread->callee_time_stack [thread->stack_id];
846 method->callee_time += thread->callee_time_stack [thread->stack_id];
847 if (thread->stack_id)
848 thread->callee_time_stack [thread->stack_id - 1] += tdiff;
849 //fprintf (outfile, "method %s took %d\n", method->name, (int)(tdiff/1000));
851 fprintf (outfile, "unmatched leave at stack pos: %d for method %s\n", thread->stack_id, method->name);
856 uint64_t start_time; /* move this per-thread? */
861 static GCDesc gc_info [3];
862 static uint64_t max_heap_size;
863 static uint64_t gc_object_moves;
864 static int gc_resizes;
871 static HandleInfo handle_info [4];
874 gc_event_name (int ev)
877 case MONO_GC_EVENT_START: return "start";
878 case MONO_GC_EVENT_MARK_START: return "mark start";
879 case MONO_GC_EVENT_MARK_END: return "mark end";
880 case MONO_GC_EVENT_RECLAIM_START: return "reclaim start";
881 case MONO_GC_EVENT_RECLAIM_END: return "reclaim end";
882 case MONO_GC_EVENT_END: return "end";
883 case MONO_GC_EVENT_PRE_STOP_WORLD: return "pre stop";
884 case MONO_GC_EVENT_POST_STOP_WORLD: return "post stop";
885 case MONO_GC_EVENT_PRE_START_WORLD: return "pre start";
886 case MONO_GC_EVENT_POST_START_WORLD: return "post start";
892 static uint64_t clause_summary [MONO_EXCEPTION_CLAUSE_FAULT + 1];
893 static uint64_t throw_count = 0;
894 static TraceDesc exc_traces;
897 clause_name (int type)
900 case MONO_EXCEPTION_CLAUSE_NONE: return "catch";
901 case MONO_EXCEPTION_CLAUSE_FILTER: return "filter";
902 case MONO_EXCEPTION_CLAUSE_FINALLY: return "finally";
903 case MONO_EXCEPTION_CLAUSE_FAULT: return "fault";
904 default: return "invalid";
908 static uint64_t monitor_contention;
909 static uint64_t monitor_failed;
910 static uint64_t monitor_acquired;
912 struct _MonitorDesc {
915 uintptr_t contentions;
917 uint64_t max_wait_time;
921 static MonitorDesc* monitor_hash [SMALL_HASH_SIZE] = {0};
922 static int num_monitors = 0;
925 lookup_monitor (uintptr_t objid)
927 int slot = ((objid >> 3) & 0xffff) % SMALL_HASH_SIZE;
928 MonitorDesc *cd = monitor_hash [slot];
929 while (cd && cd->objid != objid)
932 cd = calloc (sizeof (MonitorDesc), 1);
934 cd->next = monitor_hash [slot];
935 monitor_hash [slot] = cd;
942 monitor_ev_name (int ev)
945 case MONO_PROFILER_MONITOR_CONTENTION: return "contended";
946 case MONO_PROFILER_MONITOR_DONE: return "acquired";
947 case MONO_PROFILER_MONITOR_FAIL: return "not taken";
948 default: return "invalid";
953 get_handle_name (int htype)
956 case 0: return "weak";
957 case 1: return "weaktrack";
958 case 2: return "normal";
959 case 3: return "pinned";
960 default: return "unknown";
965 decode_bt (MethodDesc** sframes, int *size, unsigned char *p, unsigned char **endp, intptr_t ptr_base)
969 int flags = decode_uleb128 (p, &p);
970 int count = decode_uleb128 (p, &p);
974 frames = malloc (count * sizeof (void*));
977 for (i = 0; i < count; ++i) {
978 intptr_t ptrdiff = decode_sleb128 (p, &p);
979 frames [i] = lookup_method (ptr_base + ptrdiff);
987 tracked_creation (uintptr_t obj, ClassDesc *cd, uint64_t size, BackTrace *bt, uint64_t timestamp)
990 for (i = 0; i < num_tracked_objects; ++i) {
991 if (tracked_objects [i] != obj)
993 fprintf (outfile, "Object %p created (%s, %llu bytes) at %.3f secs.\n", (void*)obj, cd->name, size, (timestamp - startup_time)/1000000000.0);
994 if (bt && bt->count) {
996 for (k = 0; k < bt->count; ++k)
997 fprintf (outfile, "\t%s\n", bt->methods [k]->name);
1003 track_handle (uintptr_t obj, int htype, uint32_t handle)
1006 for (i = 0; i < num_tracked_objects; ++i) {
1007 if (tracked_objects [i] == obj)
1008 fprintf (outfile, "Object %p referenced from handle %u\n", (void*)obj, handle);
1013 track_move (uintptr_t src, uintptr_t dst)
1016 for (i = 0; i < num_tracked_objects; ++i) {
1017 if (tracked_objects [i] == src)
1018 fprintf (outfile, "Object %p moved to %p\n", (void*)src, (void*)dst);
1019 else if (tracked_objects [i] == dst)
1020 fprintf (outfile, "Object %p moved from %p\n", (void*)dst, (void*)src);
1025 track_obj_reference (uintptr_t obj, uintptr_t parent, ClassDesc *cd)
1028 for (i = 0; i < num_tracked_objects; ++i) {
1029 if (tracked_objects [i] == obj)
1030 fprintf (outfile, "Object %p referenced from %p (%s).\n", (void*)obj, (void*)parent, cd->name);
1035 found_object (uintptr_t obj)
1037 num_tracked_objects ++;
1038 tracked_objects = realloc (tracked_objects, num_tracked_objects * sizeof (tracked_objects [0]));
1039 tracked_objects [num_tracked_objects - 1] = obj;
1042 #define OBJ_ADDR(diff) ((obj_base + diff) << 3)
1043 #define LOG_TIME(base,diff) /*fprintf("outfile, time %llu + %llu near offset %d\n", base, diff, p - ctx->buf)*/
1046 decode_buffer (ProfContext *ctx)
1053 intptr_t method_base;
1055 uint64_t file_offset;
1057 ThreadContext *thread;
1059 #ifdef HAVE_SYS_ZLIB
1061 file_offset = gztell (ctx->gzfile);
1064 file_offset = ftell (ctx->file);
1065 if (!load_data (ctx, 48))
1068 if (read_int32 (p) != BUF_ID) {
1069 fprintf (outfile, "Incorrect buffer id: 0x%x\n", read_int32 (p));
1070 for (i = 0; i < 48; ++i) {
1071 fprintf (outfile, "0x%x%s", p [i], i % 8?" ":"\n");
1075 len = read_int32 (p + 4);
1076 time_base = read_int64 (p + 8);
1077 ptr_base = read_int64 (p + 16);
1078 obj_base = read_int64 (p + 24);
1079 thread_id = read_int64 (p + 32);
1080 method_base = read_int64 (p + 40);
1082 fprintf (outfile, "buf: thread:%x, len: %d, time: %llu, file offset: %llu\n", thread_id, len, time_base, file_offset);
1083 thread = load_thread (ctx, thread_id);
1084 if (!load_data (ctx, len))
1086 if (!startup_time) {
1087 startup_time = time_base;
1089 time_from += startup_time;
1090 time_to += startup_time;
1093 thread->name = pstrdup ("Main");
1095 for (i = 0; i < thread->stack_id; ++i)
1096 thread->stack [i]->recurse_count++;
1102 int subtype = *p & 0xf0;
1103 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1104 LOG_TIME (time_base, tdiff);
1106 if (subtype == TYPE_GC_RESIZE) {
1107 uint64_t new_size = decode_uleb128 (p, &p);
1109 fprintf (outfile, "gc heap resized to %llu\n", new_size);
1111 if (new_size > max_heap_size)
1112 max_heap_size = new_size;
1113 } else if (subtype == TYPE_GC_EVENT) {
1114 uint64_t ev = decode_uleb128 (p, &p);
1115 int gen = decode_uleb128 (p, &p);
1117 fprintf (outfile, "gc event for gen%d: %s at %llu\n", gen - 1, gc_event_name (ev), time_base);
1119 fprintf (outfile, "incorrect gc gen: %d\n", gen);
1122 if (ev == MONO_GC_EVENT_START) {
1123 gc_info [gen].start_time = time_base;
1124 gc_info [gen].count++;
1125 } else if (ev == MONO_GC_EVENT_END) {
1126 tdiff = time_base - gc_info [gen].start_time;
1127 gc_info [gen].total_time += tdiff;
1128 if (tdiff > gc_info [gen].max_time)
1129 gc_info [gen].max_time = tdiff;
1131 } else if (subtype == TYPE_GC_MOVE) {
1132 int j, num = decode_uleb128 (p, &p);
1133 gc_object_moves += num / 2;
1134 for (j = 0; j < num; j += 2) {
1135 intptr_t obj1diff = decode_sleb128 (p, &p);
1136 intptr_t obj2diff = decode_sleb128 (p, &p);
1137 if (num_tracked_objects)
1138 track_move (OBJ_ADDR (obj1diff), OBJ_ADDR (obj2diff));
1140 fprintf (outfile, "moved obj %p to %p\n", (void*)OBJ_ADDR (obj1diff), (void*)OBJ_ADDR (obj2diff));
1143 } else if (subtype == TYPE_GC_HANDLE_CREATED) {
1144 int htype = decode_uleb128 (p, &p);
1145 uint32_t handle = decode_uleb128 (p, &p);
1146 intptr_t objdiff = decode_sleb128 (p, &p);
1149 handle_info [htype].created++;
1150 add_trace_thread (thread, &handle_info [htype].traces, 1);
1151 /* FIXME: we don't take into account timing here */
1152 if (handle_info [htype].created > handle_info [htype].max_live)
1153 handle_info [htype].max_live = handle_info [htype].created;
1154 if (num_tracked_objects)
1155 track_handle (OBJ_ADDR (objdiff), htype, handle);
1157 fprintf (outfile, "handle (%s) %u created for object %p\n", get_handle_name (htype), handle, (void*)OBJ_ADDR (objdiff));
1158 } else if (subtype == TYPE_GC_HANDLE_DESTROYED) {
1159 int htype = decode_uleb128 (p, &p);
1160 uint32_t handle = decode_uleb128 (p, &p);
1163 handle_info [htype].created--;
1165 fprintf (outfile, "handle (%s) %u destroyed\n", get_handle_name (htype), handle);
1169 case TYPE_METADATA: {
1170 int error = *p & TYPE_LOAD_ERR;
1171 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1173 intptr_t ptrdiff = decode_sleb128 (p, &p);
1174 LOG_TIME (time_base, tdiff);
1176 if (mtype == TYPE_CLASS) {
1177 intptr_t imptrdiff = decode_sleb128 (p, &p);
1178 uint64_t flags = decode_uleb128 (p, &p);
1180 fprintf (outfile, "non-zero flags in class\n");
1184 fprintf (outfile, "loaded class %p (%s in %p) at %llu\n", (void*)(ptr_base + ptrdiff), p, (void*)(ptr_base + imptrdiff), time_base);
1186 add_class (ptr_base + ptrdiff, (char*)p);
1189 } else if (mtype == TYPE_IMAGE) {
1190 uint64_t flags = decode_uleb128 (p, &p);
1192 fprintf (outfile, "non-zero flags in image\n");
1196 fprintf (outfile, "loaded image %p (%s) at %llu\n", (void*)(ptr_base + ptrdiff), p, time_base);
1198 add_image (ptr_base + ptrdiff, (char*)p);
1201 } else if (mtype == TYPE_THREAD) {
1203 uint64_t flags = decode_uleb128 (p, &p);
1205 fprintf (outfile, "non-zero flags in thread\n");
1208 nt = get_thread (ctx, ptr_base * ptrdiff);
1209 nt->name = pstrdup ((char*)p);
1211 fprintf (outfile, "thread %p named: %s\n", (void*)(ptr_base + ptrdiff), p);
1218 int has_bt = *p & TYPE_ALLOC_BT;
1219 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1220 intptr_t ptrdiff = decode_sleb128 (p, &p);
1221 intptr_t objdiff = decode_sleb128 (p, &p);
1224 MethodDesc* sframes [8];
1225 MethodDesc** frames = sframes;
1226 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1227 len = decode_uleb128 (p, &p);
1228 LOG_TIME (time_base, tdiff);
1231 fprintf (outfile, "alloced object %p, size %llu (%s) at %llu\n", (void*)OBJ_ADDR (objdiff), len, lookup_class (ptr_base + ptrdiff)->name, time_base);
1234 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1236 fprintf (outfile, "Cannot load backtrace\n");
1240 if ((thread_filter && thread_filter == thread->thread_id) || (time_base >= time_from && time_base < time_to)) {
1243 cd->alloc_size += len;
1245 bt = add_trace_methods (frames, num_bt, &cd->traces, len);
1247 bt = add_trace_thread (thread, &cd->traces, len);
1248 if (find_size && len >= find_size) {
1249 if (!find_name || strstr (cd->name, find_name))
1250 found_object (OBJ_ADDR (objdiff));
1251 } else if (!find_size && find_name && strstr (cd->name, find_name)) {
1252 found_object (OBJ_ADDR (objdiff));
1254 if (num_tracked_objects)
1255 tracked_creation (OBJ_ADDR (objdiff), cd, len, bt, time_base);
1257 if (frames != sframes)
1262 int subtype = *p & 0xf0;
1263 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1264 int64_t ptrdiff = decode_sleb128 (p, &p);
1265 LOG_TIME (time_base, tdiff);
1267 method_base += ptrdiff;
1268 if (subtype == TYPE_JIT) {
1269 intptr_t codediff = decode_sleb128 (p, &p);
1270 int codelen = decode_uleb128 (p, &p);
1272 fprintf (outfile, "jitted method %p (%s), size: %d\n", (void*)(method_base), p, codelen);
1273 add_method (method_base, (char*)p, ptr_base + codediff, codelen);
1278 if ((thread_filter && thread_filter != thread->thread_id))
1280 method = lookup_method (method_base);
1281 if (subtype == TYPE_ENTER) {
1282 add_trace_thread (thread, &method->traces, 1);
1283 push_method (thread, method, time_base);
1285 pop_method (thread, method, time_base);
1288 fprintf (outfile, "%s method %s\n", subtype == TYPE_ENTER? "enter": subtype == TYPE_EXC_LEAVE? "exleave": "leave", method->name);
1293 int subtype = *p & 0xf0;
1294 if (subtype == TYPE_HEAP_OBJECT) {
1297 intptr_t objdiff = decode_sleb128 (p + 1, &p);
1298 intptr_t ptrdiff = decode_sleb128 (p, &p);
1299 uint64_t size = decode_uleb128 (p, &p);
1300 uintptr_t num = decode_uleb128 (p, &p);
1301 uintptr_t ref_offset;
1302 uintptr_t last_obj_offset = 0;
1303 ClassDesc *cd = lookup_class (ptr_base + ptrdiff);
1305 HeapClassDesc *hcd = add_heap_shot_class (thread->current_heap_shot, cd, size);
1306 if (collect_traces) {
1307 ho = alloc_heap_obj (OBJ_ADDR (objdiff), hcd, num);
1308 add_heap_shot_obj (thread->current_heap_shot, ho);
1313 ho = heap_shot_obj_add_refs (thread->current_heap_shot, OBJ_ADDR (objdiff), num, &ref_offset);
1315 for (i = 0; i < num; ++i) {
1316 /* FIXME: use object distance to measure how good
1317 * the GC is at keeping related objects close
1319 uintptr_t offset = ctx->data_version > 1? last_obj_offset + decode_uleb128 (p, &p): -1;
1320 intptr_t obj1diff = decode_sleb128 (p, &p);
1321 last_obj_offset = offset;
1323 ho->refs [ref_offset + i] = OBJ_ADDR (obj1diff);
1324 if (num_tracked_objects)
1325 track_obj_reference (OBJ_ADDR (obj1diff), OBJ_ADDR (objdiff), cd);
1328 fprintf (outfile, "traced object %p, size %llu (%s), refs: %d\n", (void*)OBJ_ADDR (objdiff), size, cd->name, num);
1329 } else if (subtype == TYPE_HEAP_END) {
1330 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1331 LOG_TIME (time_base, tdiff);
1334 fprintf (outfile, "heap shot end\n");
1335 if (collect_traces) {
1336 heap_shot_resolve_reverse_refs (thread->current_heap_shot);
1337 heap_shot_free_objects (thread->current_heap_shot);
1339 thread->current_heap_shot = NULL;
1340 } else if (subtype == TYPE_HEAP_START) {
1341 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1342 LOG_TIME (time_base, tdiff);
1345 fprintf (outfile, "heap shot start\n");
1346 thread->current_heap_shot = new_heap_shot (time_base);
1350 case TYPE_MONITOR: {
1351 int event = (*p >> 4) & 0x3;
1352 int has_bt = *p & TYPE_MONITOR_BT;
1353 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1354 intptr_t objdiff = decode_sleb128 (p, &p);
1355 MethodDesc* sframes [8];
1356 MethodDesc** frames = sframes;
1359 LOG_TIME (time_base, tdiff);
1361 record = (!thread_filter || thread_filter == thread->thread_id);
1362 if (event == MONO_PROFILER_MONITOR_CONTENTION) {
1363 MonitorDesc *mdesc = lookup_monitor (OBJ_ADDR (objdiff));
1365 monitor_contention++;
1366 mdesc->contentions++;
1367 thread->monitor = mdesc;
1368 thread->contention_start = time_base;
1372 frames = decode_bt (sframes, &num_bt, p, &p, ptr_base);
1374 fprintf (outfile, "Cannot load backtrace\n");
1378 add_trace_methods (frames, num_bt, &mdesc->traces, 1);
1381 add_trace_thread (thread, &mdesc->traces, 1);
1383 } else if (event == MONO_PROFILER_MONITOR_FAIL) {
1386 if (thread->monitor && thread->contention_start) {
1387 uint64_t wait_time = time_base - thread->contention_start;
1388 if (wait_time > thread->monitor->max_wait_time)
1389 thread->monitor->max_wait_time = wait_time;
1390 thread->monitor->wait_time += wait_time;
1391 thread->monitor = NULL;
1392 thread->contention_start = 0;
1395 } else if (event == MONO_PROFILER_MONITOR_DONE) {
1398 if (thread->monitor && thread->contention_start) {
1399 uint64_t wait_time = time_base - thread->contention_start;
1400 if (wait_time > thread->monitor->max_wait_time)
1401 thread->monitor->max_wait_time = wait_time;
1402 thread->monitor->wait_time += wait_time;
1403 thread->monitor = NULL;
1404 thread->contention_start = 0;
1409 fprintf (outfile, "monitor %s for object %p\n", monitor_ev_name (event), (void*)OBJ_ADDR (objdiff));
1410 if (frames != sframes)
1414 case TYPE_EXCEPTION: {
1415 int subtype = *p & 0x70;
1416 int has_bt = *p & TYPE_EXCEPTION_BT;
1417 uint64_t tdiff = decode_uleb128 (p + 1, &p);
1418 MethodDesc* sframes [8];
1419 MethodDesc** frames = sframes;
1421 LOG_TIME (time_base, tdiff);
1423 record = (!thread_filter || thread_filter == thread->thread_id);
1424 if (subtype == TYPE_CLAUSE) {
1425 int clause_type = decode_uleb128 (p, &p);
1426 int clause_num = decode_uleb128 (p, &p);
1427 int64_t ptrdiff = decode_sleb128 (p, &p);
1428 method_base += ptrdiff;
1430 clause_summary [clause_type]++;
1432 fprintf (outfile, "clause %s (%d) in method %s\n", clause_name (clause_type), clause_num, lookup_method (method_base)->name);
1434 intptr_t objdiff = decode_sleb128 (p, &p);
1439 frames = decode_bt (sframes, &has_bt, p, &p, ptr_base);
1441 fprintf (outfile, "Cannot load backtrace\n");
1445 add_trace_methods (frames, has_bt, &exc_traces, 1);
1448 add_trace_thread (thread, &exc_traces, 1);
1450 if (frames != sframes)
1453 fprintf (outfile, "throw %p\n", (void*)OBJ_ADDR (objdiff));
1458 fprintf (outfile, "unhandled profiler event: 0x%x at file offset: %llu + %d (len: %d\n)\n", *p, file_offset, p - ctx->buf, len);
1462 thread->last_time = time_base;
1463 for (i = 0; i < thread->stack_id; ++i)
1464 thread->stack [i]->recurse_count = 0;
1469 load_file (char *name)
1472 ProfContext *ctx = calloc (sizeof (ProfContext), 1);
1473 if (strcmp (name, "-") == 0)
1476 ctx->file = fopen (name, "rb");
1478 printf ("Cannot open file: %s\n", name);
1481 #if defined (HAVE_SYS_ZLIB)
1482 if (ctx->file != stdin)
1483 ctx->gzfile = gzdopen (fileno (ctx->file), "rb");
1485 if (!load_data (ctx, 32))
1488 if (read_int32 (p) != LOG_HEADER_ID || p [6] > LOG_DATA_VERSION)
1490 ctx->version_major = p [4];
1491 ctx->version_minor = p [5];
1492 ctx->data_version = p [6];
1493 /* reading 64 bit files on 32 bit systems not supported yet */
1494 if (p [7] > sizeof (void*))
1496 if (read_int32 (p + 20)) /* flags must be 0 */
1498 ctx->startup_time = read_int64 (p + 8);
1499 ctx->timer_overhead = read_int32 (p + 16);
1500 ctx->pid = read_int32 (p + 24);
1508 static int alloc_sort_mode = ALLOC_SORT_BYTES;
1511 compare_class (const void *a, const void *b)
1513 ClassDesc *const*A = a;
1514 ClassDesc *const*B = b;
1515 uint64_t vala, valb;
1516 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1517 vala = (*A)->alloc_size;
1518 valb = (*B)->alloc_size;
1520 vala = (*A)->allocs;
1521 valb = (*B)->allocs;
1531 dump_header (ProfContext *ctx)
1533 time_t st = ctx->startup_time / 1000;
1534 char *t = ctime (&st);
1535 fprintf (outfile, "\nMono log profiler data\n");
1536 fprintf (outfile, "\tProfiler version: %d.%d\n", ctx->version_major, ctx->version_minor);
1537 fprintf (outfile, "\tData version: %d\n", ctx->data_version);
1538 fprintf (outfile, "\tMean timer overhead: %d nanoseconds\n", ctx->timer_overhead);
1539 fprintf (outfile, "\tProgram startup: %s", t);
1541 fprintf (outfile, "\tProgram ID: %d\n", ctx->pid);
1545 dump_traces (TraceDesc *traces, const char *desc)
1552 sort_context_array (traces);
1553 for (j = 0; j < traces->count; ++j) {
1556 bt = traces->traces [j].bt;
1559 fprintf (outfile, "\t%llu %s from:\n", traces->traces [j].count, desc);
1560 for (k = 0; k < bt->count; ++k)
1561 fprintf (outfile, "\t\t%s\n", bt->methods [k]->name);
1566 dump_threads (ProfContext *ctx)
1568 ThreadContext *thread;
1569 fprintf (outfile, "\nThread summary\n");
1570 for (thread = ctx->threads; thread; thread = thread->next) {
1571 fprintf (outfile, "\tThread: %p, name: \"%s\"\n", (void*)thread->thread_id, thread->name? thread->name: "");
1576 dump_exceptions (void)
1579 fprintf (outfile, "\nException summary\n");
1580 fprintf (outfile, "\tThrows: %llu\n", throw_count);
1581 dump_traces (&exc_traces, "throws");
1582 for (i = 0; i <= MONO_EXCEPTION_CLAUSE_FAULT; ++i) {
1583 if (!clause_summary [i])
1585 fprintf (outfile, "\tExecuted %s clauses: %llu\n", clause_name (i), clause_summary [i]);
1590 compare_monitor (const void *a, const void *b)
1592 MonitorDesc *const*A = a;
1593 MonitorDesc *const*B = b;
1594 if ((*B)->wait_time == (*A)->wait_time)
1596 if ((*B)->wait_time < (*A)->wait_time)
1602 dump_monitors (void)
1604 MonitorDesc **monitors;
1608 monitors = malloc (sizeof (void*) * num_monitors);
1609 for (i = 0, j = 0; i < SMALL_HASH_SIZE; ++i) {
1610 MonitorDesc *mdesc = monitor_hash [i];
1612 monitors [j++] = mdesc;
1613 mdesc = mdesc->next;
1616 qsort (monitors, num_monitors, sizeof (void*), compare_monitor);
1617 fprintf (outfile, "\nMonitor lock summary\n");
1618 for (i = 0; i < num_monitors; ++i) {
1619 MonitorDesc *mdesc = monitors [i];
1620 fprintf (outfile, "\tLock object %p: %d contentions\n", (void*)mdesc->objid, (int)mdesc->contentions);
1621 fprintf (outfile, "\t\t%.6f secs total wait time, %.6f max, %.6f average\n",
1622 mdesc->wait_time/1000000000.0, mdesc->max_wait_time/1000000000.0, mdesc->wait_time/1000000000.0/mdesc->contentions);
1623 dump_traces (&mdesc->traces, "contentions");
1625 fprintf (outfile, "\tLock contentions: %llu\n", monitor_contention);
1626 fprintf (outfile, "\tLock acquired: %llu\n", monitor_acquired);
1627 fprintf (outfile, "\tLock failures: %llu\n", monitor_failed);
1634 fprintf (outfile, "\nGC summary\n");
1635 fprintf (outfile, "\tGC resizes: %d\n", gc_resizes);
1636 fprintf (outfile, "\tMax heap size: %llu\n", max_heap_size);
1637 fprintf (outfile, "\tObject moves: %llu\n", gc_object_moves);
1638 for (i = 0; i < 3; ++i) {
1639 if (!gc_info [i].count)
1641 fprintf (outfile, "\tGen%d collections: %d, max time: %lluus, total time: %lluus, average: %lluus\n",
1642 i, gc_info [i].count, gc_info [i].max_time / 1000, gc_info [i].total_time / 1000,
1643 gc_info [i].total_time / gc_info [i].count / 1000);
1645 for (i = 0; i < 3; ++i) {
1646 if (!handle_info [i].max_live)
1648 fprintf (outfile, "\tGC handles %s: created: %llu, destroyed: %llu, max: %llu\n",
1649 get_handle_name (i), handle_info [i].created, handle_info [i].destroyed, handle_info [i].max_live);
1650 dump_traces (&handle_info [i].traces, "created");
1655 dump_allocations (void)
1658 intptr_t allocs = 0;
1660 int header_done = 0;
1661 ClassDesc **classes = malloc (num_classes * sizeof (void*));
1664 for (i = 0; i < HASH_SIZE; ++i) {
1665 cd = class_hash [i];
1671 qsort (classes, num_classes, sizeof (void*), compare_class);
1672 for (i = 0; i < num_classes; ++i) {
1676 allocs += cd->allocs;
1677 size += cd->alloc_size;
1678 if (!header_done++) {
1679 fprintf (outfile, "\nAllocation summary\n");
1680 fprintf (outfile, "%10s %10s %8s Type name\n", "Bytes", "Count", "Average");
1682 fprintf (outfile, "%10llu %10d %8llu %s\n", cd->alloc_size, cd->allocs, cd->alloc_size / cd->allocs, cd->name);
1683 dump_traces (&cd->traces, "bytes");
1686 fprintf (outfile, "Total memory allocated: %llu bytes in %d objects\n", size, allocs);
1695 static int method_sort_mode = METHOD_SORT_TOTAL;
1698 compare_method (const void *a, const void *b)
1700 MethodDesc *const*A = a;
1701 MethodDesc *const*B = b;
1702 uint64_t vala, valb;
1703 if (method_sort_mode == METHOD_SORT_SELF) {
1704 vala = (*A)->self_time;
1705 valb = (*B)->self_time;
1706 } else if (method_sort_mode == METHOD_SORT_CALLS) {
1710 vala = (*A)->total_time;
1711 valb = (*B)->total_time;
1721 dump_metadata (void)
1723 fprintf (outfile, "\nMetadata summary\n");
1724 fprintf (outfile, "\tLoaded images: %d\n", num_images);
1728 for (i = 0; i < SMALL_HASH_SIZE; ++i) {
1729 image = image_hash [i];
1731 fprintf (outfile, "\t\t%s\n", image->filename);
1732 image = image->next;
1744 int header_done = 0;
1745 MethodDesc **methods = malloc (num_methods * sizeof (void*));
1748 for (i = 0; i < HASH_SIZE; ++i) {
1749 cd = method_hash [i];
1751 cd->total_time = cd->self_time + cd->callee_time;
1756 qsort (methods, num_methods, sizeof (void*), compare_method);
1757 for (i = 0; i < num_methods; ++i) {
1764 msecs = cd->total_time / 1000000;
1765 smsecs = (cd->total_time - cd->callee_time) / 1000000;
1766 if (!msecs && !verbose)
1768 if (!header_done++) {
1769 fprintf (outfile, "\nMethod call summary\n");
1770 fprintf (outfile, "%8s %8s %10s Method name\n", "Total(ms)", "Self(ms)", "Calls");
1772 fprintf (outfile, "%8llu %8llu %10llu %s\n", msecs, smsecs, cd->calls, cd->name);
1773 dump_traces (&cd->traces, "calls");
1776 fprintf (outfile, "Total calls: %llu\n", calls);
1780 compare_heap_class (const void *a, const void *b)
1782 HeapClassDesc *const*A = a;
1783 HeapClassDesc *const*B = b;
1784 uint64_t vala, valb;
1785 if (alloc_sort_mode == ALLOC_SORT_BYTES) {
1786 vala = (*A)->total_size;
1787 valb = (*B)->total_size;
1800 compare_rev_class (const void *a, const void *b)
1802 const HeapClassRevRef *A = a;
1803 const HeapClassRevRef *B = b;
1804 if (B->count == A->count)
1806 if (B->count < A->count)
1812 dump_rev_claases (HeapClassRevRef *revs, int count)
1819 for (j = 0; j < count; ++j) {
1820 HeapClassDesc *cd = revs [j].klass;
1821 fprintf (outfile, "\t\t%llu references from: %s\n", revs [j].count, cd->klass->name);
1826 heap_shot_summary (HeapShot *hs, int hs_num, HeapShot *last_hs)
1833 HeapClassDesc **sorted;
1834 sorted = malloc (sizeof (void*) * hs->class_count);
1835 for (i = 0; i < hs->hash_size; ++i) {
1836 cd = hs->class_hash [i];
1840 size += cd->total_size;
1841 sorted [ccount++] = cd;
1843 hs->sorted = sorted;
1844 qsort (sorted, ccount, sizeof (void*), compare_heap_class);
1845 fprintf (outfile, "\n\tHeap shot %d at %.3f secs: size: %llu, object count: %llu, class count: %d\n",
1846 hs_num, (hs->timestamp - startup_time)/1000000000.0, size, count, ccount);
1847 if (!verbose && ccount > 30)
1849 fprintf (outfile, "\t%10s %10s %8s Class name\n", "Bytes", "Count", "Average");
1850 for (i = 0; i < ccount; ++i) {
1851 HeapClassRevRef *rev_sorted;
1853 HeapClassDesc *ocd = NULL;
1856 ocd = heap_class_lookup (last_hs, cd->klass);
1857 fprintf (outfile, "\t%10llu %10llu %8llu %s", cd->total_size, cd->count, cd->total_size / cd->count, cd->klass->name);
1859 int64_t bdiff = cd->total_size - ocd->total_size;
1860 int64_t cdiff = cd->count - ocd->count;
1861 fprintf (outfile, " (bytes: %+lld, count: %+lld)\n", bdiff, cdiff);
1863 fprintf (outfile, "\n");
1865 if (!collect_traces)
1867 rev_sorted = malloc (cd->rev_count * sizeof (HeapClassRevRef));
1869 for (j = 0; j < cd->rev_hash_size; ++j) {
1870 if (cd->rev_hash [j].klass)
1871 rev_sorted [k++] = cd->rev_hash [j];
1873 assert (cd->rev_count == k);
1874 qsort (rev_sorted, cd->rev_count, sizeof (HeapClassRevRef), compare_rev_class);
1875 dump_rev_claases (rev_sorted, cd->rev_count);
1882 compare_heap_shots (const void *a, const void *b)
1884 HeapShot *const*A = a;
1885 HeapShot *const*B = b;
1886 if ((*B)->timestamp == (*A)->timestamp)
1888 if ((*B)->timestamp > (*A)->timestamp)
1894 dump_heap_shots (void)
1896 HeapShot **hs_sorted;
1898 HeapShot *last_hs = NULL;
1902 hs_sorted = malloc (num_heap_shots * sizeof (void*));
1903 fprintf (outfile, "\nHeap shot summary\n");
1905 for (hs = heap_shots; hs; hs = hs->next)
1906 hs_sorted [i++] = hs;
1907 qsort (hs_sorted, num_heap_shots, sizeof (void*), compare_heap_shots);
1908 for (i = 0; i < num_heap_shots; ++i) {
1910 heap_shot_summary (hs, i, last_hs);
1916 flush_context (ProfContext *ctx)
1918 ThreadContext *thread;
1919 /* FIXME: sometimes there are leftovers: indagate */
1920 for (thread = ctx->threads; thread; thread = thread->next) {
1921 while (thread->stack_id) {
1923 fprintf (outfile, "thread %p has %d items on stack\n", (void*)thread->thread_id, thread->stack_id);
1924 pop_method (thread, thread->stack [thread->stack_id - 1], thread->last_time);
1929 static const char *reports = "header,gc,alloc,call,metadata,exception,monitor,thread,heapshot";
1932 match_option (const char *p, const char *opt)
1934 int len = strlen (opt);
1935 if (strncmp (p, opt, len) == 0) {
1944 print_reports (ProfContext *ctx, const char *reps, int parse_only)
1948 for (p = reps; *p; p = opt) {
1949 if ((opt = match_option (p, "header")) != p) {
1954 if ((opt = match_option (p, "thread")) != p) {
1959 if ((opt = match_option (p, "gc")) != p) {
1964 if ((opt = match_option (p, "alloc")) != p) {
1966 dump_allocations ();
1969 if ((opt = match_option (p, "call")) != p) {
1974 if ((opt = match_option (p, "metadata")) != p) {
1979 if ((opt = match_option (p, "exception")) != p) {
1984 if ((opt = match_option (p, "monitor")) != p) {
1989 if ((opt = match_option (p, "heapshot")) != p) {
2000 add_find_spec (const char *p)
2002 if (p [0] == 'S' && p [1] == ':') {
2004 find_size = strtoul (p + 2, &vale, 10);
2006 } else if (p [0] == 'T' && p [1] == ':') {
2016 printf ("Mono log profiler report version %d.%d\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR);
2017 printf ("Usage: mprof-report [OPTIONS] FILENAME\n");
2018 printf ("FILENAME can be '-' to read from standard input.\n");
2019 printf ("Options:\n");
2020 printf ("\t--help display this help\n");
2021 printf ("\t--out=FILE write to FILE instead of stdout\n");
2022 printf ("\t--traces collect and show backtraces\n");
2023 printf ("\t--maxframes=NUM limit backtraces to NUM entries\n");
2024 printf ("\t--reports=R1[,R2...] print the specified reports. Defaults are:\n");
2025 printf ("\t %s\n", reports);
2026 printf ("\t--method-sort=MODE sort methods according to MODE: total, self, calls\n");
2027 printf ("\t--alloc-sort=MODE sort allocations according to MODE: bytes, count\n");
2028 printf ("\t--track=OB1[,OB2...] track what happens to objects OBJ1, O2 etc.\n");
2029 printf ("\t--find=FINDSPEC find and track objects matching FINFSPEC, where FINDSPEC is:\n");
2030 printf ("\t S:minimum_size or T:partial_name\n");
2031 printf ("\t--thread=THREADID consider just the data for thread THREADID\n");
2032 printf ("\t--time=FROM-TO consider data FROM seconds from startup up to TO seconds\n");
2033 printf ("\t--verbose increase verbosity level\n");
2034 printf ("\t--debug display decoding debug info for mprof-report devs\n");
2038 main (int argc, char *argv[])
2043 for (i = 1; i < argc; ++i) {
2044 if (strcmp ("--debug", argv [i]) == 0) {
2046 } else if (strcmp ("--help", argv [i]) == 0) {
2049 } else if (strncmp ("--alloc-sort=", argv [i], 13) == 0) {
2050 const char *val = argv [i] + 13;
2051 if (strcmp (val, "bytes") == 0) {
2052 alloc_sort_mode = ALLOC_SORT_BYTES;
2053 } else if (strcmp (val, "count") == 0) {
2054 alloc_sort_mode = ALLOC_SORT_COUNT;
2059 } else if (strncmp ("--method-sort=", argv [i], 14) == 0) {
2060 const char *val = argv [i] + 14;
2061 if (strcmp (val, "total") == 0) {
2062 method_sort_mode = METHOD_SORT_TOTAL;
2063 } else if (strcmp (val, "self") == 0) {
2064 method_sort_mode = METHOD_SORT_SELF;
2065 } else if (strcmp (val, "calls") == 0) {
2066 method_sort_mode = METHOD_SORT_CALLS;
2071 } else if (strncmp ("--reports=", argv [i], 10) == 0) {
2072 const char *val = argv [i] + 10;
2073 if (!print_reports (NULL, val, 1)) {
2078 } else if (strncmp ("--out=", argv [i], 6) == 0) {
2079 const char *val = argv [i] + 6;
2080 outfile = fopen (val, "w");
2082 printf ("Cannot open output file: %s\n", val);
2085 } else if (strncmp ("--maxframes=", argv [i], 12) == 0) {
2086 const char *val = argv [i] + 12;
2088 trace_max = strtoul (val, &vale, 10);
2089 } else if (strncmp ("--find=", argv [i], 7) == 0) {
2090 const char *val = argv [i] + 7;
2091 if (!add_find_spec (val)) {
2095 } else if (strncmp ("--track=", argv [i], 8) == 0) {
2096 const char *val = argv [i] + 8;
2099 uintptr_t tracked_obj;
2104 tracked_obj = strtoul (val, &vale, 0);
2105 found_object (tracked_obj);
2108 } else if (strncmp ("--thread=", argv [i], 9) == 0) {
2109 const char *val = argv [i] + 9;
2111 thread_filter = strtoul (val, &vale, 0);
2112 } else if (strncmp ("--time=", argv [i], 7) == 0) {
2113 char *val = pstrdup (argv [i] + 7);
2114 double from_secs, to_secs;
2115 char *top = strchr (val, '-');
2121 from_secs = atof (val);
2122 to_secs = atof (top);
2124 if (from_secs > to_secs) {
2128 time_from = from_secs * 1000000000;
2129 time_to = to_secs * 1000000000;
2130 } else if (strcmp ("--verbose", argv [i]) == 0) {
2132 } else if (strcmp ("--traces", argv [i]) == 0) {
2143 ctx = load_file (argv [i]);
2145 printf ("Not a log profiler data file (or unsupported version).\n");
2148 while (decode_buffer (ctx));
2149 flush_context (ctx);
2150 if (num_tracked_objects)
2152 print_reports (ctx, reports, 0);