Author: benm
Date: 2005-03-10 16:12:41 -0500 (Thu, 10 Mar 2005)
New Revision: 41665

Added:
   trunk/heap-prof/src/desc-heap/
   trunk/heap-prof/src/desc-heap/ChangeLog
   trunk/heap-prof/src/desc-heap/Makefile.am
   trunk/heap-prof/src/desc-heap/desc-heap.c
Modified:
   trunk/heap-prof/ChangeLog
   trunk/heap-prof/configure.in
   trunk/heap-prof/src/Makefile.am
Log:
In .:
2005-03-10  Ben Maurer  <[EMAIL PROTECTED]>

        * configure.in: New hackish heap dumping profiler.

In src/desc-heap:
2005-03-10  Ben Maurer  <[EMAIL PROTECTED]>

        * desc-heap.c: A profiler for trow.



Modified: trunk/heap-prof/ChangeLog
===================================================================
--- trunk/heap-prof/ChangeLog   2005-03-10 21:03:56 UTC (rev 41664)
+++ trunk/heap-prof/ChangeLog   2005-03-10 21:12:41 UTC (rev 41665)
@@ -1,3 +1,7 @@
+2005-03-10  Ben Maurer  <[EMAIL PROTECTED]>
+
+       * configure.in: New hackish heap dumping profiler.
+
 2005-01-24  Ben Maurer  <[EMAIL PROTECTED]>
 
        * mono.patch: Don't need this any more. Just use HEAD.

Modified: trunk/heap-prof/configure.in
===================================================================
--- trunk/heap-prof/configure.in        2005-03-10 21:03:56 UTC (rev 41664)
+++ trunk/heap-prof/configure.in        2005-03-10 21:12:41 UTC (rev 41665)
@@ -24,6 +24,7 @@
 mime-data/Makefile
 src/Makefile
 src/runtime-profiler/Makefile
+src/desc-heap/Makefile
 src/memstat/Makefile
 src/viewer/Makefile
 src/viewer/mono-heap-prof-view

Modified: trunk/heap-prof/src/Makefile.am
===================================================================
--- trunk/heap-prof/src/Makefile.am     2005-03-10 21:03:56 UTC (rev 41664)
+++ trunk/heap-prof/src/Makefile.am     2005-03-10 21:12:41 UTC (rev 41665)
@@ -1,3 +1,3 @@
-SUBDIRS= viewer runtime-profiler
+SUBDIRS= viewer runtime-profiler desc-heap
 # disabled until I get something that really works
 # memstat

Added: trunk/heap-prof/src/desc-heap/ChangeLog
===================================================================
--- trunk/heap-prof/src/desc-heap/ChangeLog     2005-03-10 21:03:56 UTC (rev 
41664)
+++ trunk/heap-prof/src/desc-heap/ChangeLog     2005-03-10 21:12:41 UTC (rev 
41665)
@@ -0,0 +1,4 @@
+2005-03-10  Ben Maurer  <[EMAIL PROTECTED]>
+
+       * desc-heap.c: A profiler for trow.
+

Added: trunk/heap-prof/src/desc-heap/Makefile.am
===================================================================
--- trunk/heap-prof/src/desc-heap/Makefile.am   2005-03-10 21:03:56 UTC (rev 
41664)
+++ trunk/heap-prof/src/desc-heap/Makefile.am   2005-03-10 21:12:41 UTC (rev 
41665)
@@ -0,0 +1,7 @@
+lib_LTLIBRARIES = libmono-profiler-desc-heap.la
+
+libmono_profiler_desc_heap_la_SOURCES = desc-heap.c
+
+libmono_profiler_desc_heap_la_LIBADD = @CCOMPILE_LIBS@
+
+INCLUDES = @CCOMPILE_CFLAGS@ -Wall
\ No newline at end of file

Added: trunk/heap-prof/src/desc-heap/desc-heap.c
===================================================================
--- trunk/heap-prof/src/desc-heap/desc-heap.c   2005-03-10 21:03:56 UTC (rev 
41664)
+++ trunk/heap-prof/src/desc-heap/desc-heap.c   2005-03-10 21:12:41 UTC (rev 
41665)
@@ -0,0 +1,198 @@
+/*
+ * gc-profiler.c - A heap profiler
+ *
+ * Author:
+ *     Ben Maurer <[EMAIL PROTECTED]>
+ *
+ */
+#include <string.h>
+#include <glib.h>
+#include <mono/io-layer/io-layer.h>
+#include <mono/metadata/class.h>
+#include <mono/metadata/debug-helpers.h>
+#include <mono/metadata/object.h>
+#include <mono/metadata/profiler.h>
+#include <mono/metadata/mono-gc.h>
+#include <unistd.h>
+
+typedef struct AllocRec AllocRec;
+struct AllocRec {
+       AllocRec* next;
+       MonoObject* obj;
+       int tidx;
+       int size;
+};
+
+struct _MonoProfiler {
+       
+       GPtrArray* klass_table;
+       GHashTable* klass_to_table_idx;
+       
+       AllocRec* live_allocs;
+       guint32 t_zero;
+
+       
+       int type_live_data_size;
+       int* type_live_data;
+       
+       int total_live_bytes;
+       
+       guint64 last_checkpoint;
+};
+
+
+
+static CRITICAL_SECTION hp_lock;
+#define hp_lock_enter() EnterCriticalSection (&hp_lock)
+#define hp_lock_leave() LeaveCriticalSection (&hp_lock)
+
+static guint32
+get_delta_t (MonoProfiler *p)
+{
+       return GetTickCount () - p->t_zero;
+}
+
+#define resize_array(arr, old_size, new_size) do { \
+       gpointer __x = g_malloc0 ((new_size) * sizeof (*arr)); \
+       if (arr) \
+               memcpy (__x, arr, (old_size) * sizeof (*arr)); \
+       arr = __x; \
+       old_size = (new_size); \
+} while (0)
+
+       
+
+static guint32
+get_type_idx (MonoProfiler *p, MonoClass* klass)
+{
+       guint32 idx_plus_one;
+       
+       if (!(idx_plus_one = GPOINTER_TO_UINT (g_hash_table_lookup 
(p->klass_to_table_idx, klass)))) {
+               char* name = mono_type_get_full_name (mono_class_get_type 
(klass));
+               g_ptr_array_add (p->klass_table, name);
+               idx_plus_one = p->klass_table->len;
+               
+               g_hash_table_insert (p->klass_to_table_idx, klass, 
idx_plus_one);
+               
+               if (idx_plus_one > p->type_live_data_size)
+                       resize_array (p->type_live_data, 
p->type_live_data_size, MAX (p->type_live_data_size << 1, idx_plus_one));
+       }
+       
+       return idx_plus_one - 1;
+}
+
+static void
+record_obj (MonoProfiler* p, AllocRec* arec, gboolean is_alloc)
+{
+       if (is_alloc) {
+               p->total_live_bytes += arec->size;
+               p->type_live_data [arec->tidx] += arec->size;
+       } else {
+               p->total_live_bytes -= arec->size;
+               p->type_live_data [arec->tidx] -= arec->size;
+       }
+}
+
+
+static void
+check_point (MonoProfiler* p, char* reason)
+{
+       int i;
+       
+       printf ("Checkpoint at %d for %s\n", get_delta_t (p), reason);
+       
+       for (i = 0; i < p->klass_table->len; i ++) {
+               if (p->type_live_data [i] > (.01 * p->total_live_bytes))
+                       printf ("   %s : %d\n", g_ptr_array_index 
(p->klass_table, i), p->type_live_data [i]);
+       }
+}
+
+static void
+write_allocation (MonoProfiler *p, MonoObject *obj, MonoClass *klass)
+{
+       AllocRec* arec = g_new0 (AllocRec, 1);
+
+       hp_lock_enter ();
+       
+       arec->obj = obj;
+       arec->tidx = get_type_idx (p, klass);
+       arec->size = mono_object_get_size (obj);
+       arec->next = p->live_allocs;
+       p->live_allocs = arec;
+       
+       record_obj (p, arec, TRUE);
+       
+       hp_lock_leave ();
+}
+
+extern gboolean mono_object_is_alive (MonoObject* o);
+
+static void
+prof_gc_collection (MonoProfiler *p, MonoGCEvent e, int gen)
+{
+       AllocRec *l, *next = NULL, *prev = NULL;
+       
+       if (e != MONO_GC_EVENT_MARK_END)
+               return;
+       
+       hp_lock_enter ();
+       
+       for (l = p->live_allocs; l; l = next) {
+               next = l->next;
+               
+               if (! mono_object_is_alive (l->obj)) {
+                       
+                       record_obj (p, l, FALSE);
+                       
+                       if (prev)
+                               prev->next = next;
+                       else
+                               p->live_allocs = next;
+                       
+                       g_free (l);
+               } else 
+                       prev = l;
+       }
+       
+       check_point (p, "gc");
+       
+       hp_lock_leave ();
+}
+
+static void
+prof_heap_resize (MonoProfiler *p, gint64 new_size)
+{
+       printf ("heap resized to %lld @ %d\n", new_size, get_delta_t (p));
+       check_point (p, "heap-resize");
+}
+
+static void
+mono_heap_prof_shutdown (MonoProfiler *p)
+{
+       check_point (p, "shutdown");
+}
+
+void
+mono_profiler_startup (const char *desc)
+{
+       MonoProfiler* p = g_new0 (MonoProfiler, 1);
+       
+       InitializeCriticalSection (&hp_lock);
+       
+       g_assert (! strncmp (desc, "desc-heap", 9));
+
+       p->klass_to_table_idx  = g_hash_table_new (NULL, NULL);
+       
+       p->klass_table  = g_ptr_array_new ();
+       
+       
+       
+       p->t_zero = GetTickCount ();
+
+       
+       mono_profiler_install_allocation (write_allocation);
+       mono_profiler_install_gc (prof_gc_collection, prof_heap_resize);
+       mono_profiler_set_events (MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_GC);
+       
+       mono_profiler_install (p, mono_heap_prof_shutdown);
+}

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to