[m-rev.] diff: ThreadScope: Reused contexts no-longer have reallocated IDs.

Paul Bone pbone at csse.unimelb.edu.au
Thu Jun 2 15:58:56 AEST 2011


In ThreadScope grades each context has a unique ID.  Previously when a context
was re-used (as apposed to created from scratch) we would re-assign it's ID, so
that it was clear to see when a new computation was started.  This is no-longer
necessary and prevents anyone using ThreadScope from understanding how contexts
are re-used.

This change also adds a new ThreadScope event that marks when a context is
released back to the free context pool.

runtime/mercury_context.c:
    Only allocate new context IDs for new contexts (not re-used contexts

    Use the new release_context event.

    Fixed spelling mistake.

runtime/mercury_threadscope.h:
runtime/mercury_threadscope.c:
    Add support for the release_context event.

Index: runtime/mercury_context.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_context.c,v
retrieving revision 1.94
diff -u -p -b -r1.94 mercury_context.c
--- runtime/mercury_context.c	24 May 2011 04:16:48 -0000	1.94
+++ runtime/mercury_context.c	2 Jun 2011 05:42:14 -0000
@@ -816,10 +816,10 @@ MR_create_context(const char *id, MR_Con
 #ifdef MR_USE_TRAIL
         c->MR_ctxt_trail_zone = NULL;
 #endif
-    }
 #ifdef MR_THREADSCOPE
     c->MR_ctxt_num_id = allocate_context_id();
 #endif
+    }
 
 #ifdef MR_DEBUG_CONTEXT_CREATION_SPEED
     MR_debug_log_message("Calling MR_init_context_maybe_generator");
@@ -829,15 +829,15 @@ MR_create_context(const char *id, MR_Con
 }
 
 /*
-** TODO: We should gc the cached contexts, or otherwise not cache to many.
+** TODO: We should gc the cached contexts, or otherwise not cache too many.
 */
 void
 MR_destroy_context(MR_Context *c)
 {
     MR_assert(c);
 
-#ifdef MR_DEBUG_STACK_SEGMENTS
-    MR_debug_log_message("Caching old context: %p", c);
+#ifdef MR_THREADSCOPE
+    MR_threadscope_post_release_context(c);
 #endif
 
 #ifdef MR_THREAD_SAFE
Index: runtime/mercury_threadscope.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_threadscope.c,v
retrieving revision 1.14
diff -u -p -b -r1.14 mercury_threadscope.c
--- runtime/mercury_threadscope.c	31 May 2011 03:14:21 -0000	1.14
+++ runtime/mercury_threadscope.c	2 Jun 2011 05:42:04 -0000
@@ -176,7 +176,8 @@
 #define MR_TS_MER_EVENT_LOOKING_FOR_GLOBAL_CONTEXT \
                                             108 /* () */
 #define MR_TS_MER_EVENT_WORK_STEALING       109 /* () */
-#define MR_TS_NUM_MER_EVENTS                10
+#define MR_TS_MER_EVENT_RELEASE_CONTEXT     110 /* (context id) */
+#define MR_TS_NUM_MER_EVENTS                11
 
 #if 0  /* DEPRECATED EVENTS: */
 #define EVENT_CREATE_SPARK        13 /* (cap, thread) */
@@ -494,6 +495,11 @@ static EventTypeDesc event_type_descs[] 
         0
     },
     {
+        MR_TS_MER_EVENT_RELEASE_CONTEXT,
+        "Release this context to the free context pool",
+        SZ_CONTEXT_ID
+    },
+    {
         /* Mark the end of this array. */
         MR_TS_NUM_EVENT_TAGS,
         NULL,
@@ -1135,6 +1141,27 @@ MR_threadscope_post_create_context_for_s
 }
 
 void
+MR_threadscope_post_release_context(MR_Context *context)
+{
+    struct MR_threadscope_event_buffer *buffer = MR_ENGINE(MR_eng_ts_buffer);
+
+    MR_US_SPIN_LOCK(&(buffer->MR_tsbuffer_lock));
+
+    if (!enough_room_for_event(buffer, MR_TS_MER_EVENT_RELEASE_CONTEXT)) {
+        flush_event_buffer(buffer);
+        open_block(buffer, MR_ENGINE(MR_eng_id));
+    } else if (!block_is_open(buffer)) {
+        open_block(buffer, MR_ENGINE(MR_eng_id));
+    }
+
+    put_event_header(buffer, MR_TS_MER_EVENT_RELEASE_CONTEXT,
+        get_current_time_nanosecs());
+    put_context_id(buffer, context->MR_ctxt_num_id);
+
+    MR_US_UNLOCK(&(buffer->MR_tsbuffer_lock));
+}
+
+void
 MR_threadscope_post_context_runnable(MR_Context *context)
 {
     struct MR_threadscope_event_buffer *buffer = MR_ENGINE(MR_eng_ts_buffer);
Index: runtime/mercury_threadscope.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_threadscope.h,v
retrieving revision 1.11
diff -u -p -b -r1.11 mercury_threadscope.h
--- runtime/mercury_threadscope.h	31 May 2011 03:14:21 -0000	1.11
+++ runtime/mercury_threadscope.h	2 Jun 2011 05:42:04 -0000
@@ -105,6 +105,14 @@ extern void MR_threadscope_post_create_c
                 struct MR_Context_Struct *ctxt);
 
 /*
+** This context is being released (back into a pool of free contexts).  We may
+** see a new create_context or create_context_for_spark message with the same
+** context ID, such a message indicates that the context is being re-used.
+*/
+extern void MR_threadscope_post_release_context(
+                struct MR_Context_Struct *context);
+
+/*
 ** This message says the context is now ready to run.  Such as it's being
 ** placed on the run queue after being blocked
 */
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 489 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20110602/3ec426a8/attachment.sig>


More information about the reviews mailing list