Merge pull request #3487 from marek-safar/rs-Threading
[mono.git] / mono / metadata / sgen-stw.c
index fd2e8565b7a49dad2c737dbeea6a6e2b3b48a069..60e5933450fba844a1888577df232f4255102127 100644 (file)
 static void sgen_unified_suspend_restart_world (void);
 static void sgen_unified_suspend_stop_world (void);
 
+static TV_DECLARE (end_of_last_stw);
+
+guint64 mono_time_since_last_stw ()
+{
+       if (end_of_last_stw == 0)
+               return 0;
+
+       TV_DECLARE (current_time);
+       TV_GETTIME (current_time);
+       return TV_ELAPSED (end_of_last_stw, current_time);
+}
+
 unsigned int sgen_global_stop_count = 0;
 
 inline static void*
@@ -44,19 +56,22 @@ align_pointer (void *ptr)
        return (void*)p;
 }
 
-static MonoContext cur_thread_ctx;
-
 static void
 update_current_thread_stack (void *start)
 {
        int stack_guard = 0;
        SgenThreadInfo *info = mono_thread_info_current ();
-       
+
        info->client_info.stack_start = align_pointer (&stack_guard);
        g_assert (info->client_info.stack_start);
        g_assert (info->client_info.stack_start >= info->client_info.stack_start_limit && info->client_info.stack_start < info->client_info.stack_end);
-       MONO_CONTEXT_GET_CURRENT (cur_thread_ctx);
-       memcpy (&info->client_info.ctx, &cur_thread_ctx, sizeof (MonoContext));
+
+#if !defined(MONO_CROSS_COMPILE) && MONO_ARCH_HAS_MONO_CONTEXT
+       MONO_CONTEXT_GET_CURRENT (info->client_info.ctx);
+#else
+       g_error ("Sgen STW requires a working mono-context");
+#endif
+
        if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
                mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, NULL, &info->client_info.ctx);
 }
@@ -194,6 +209,8 @@ sgen_client_stop_world (int generation)
 
        acquire_gc_locks ();
 
+       mono_profiler_gc_event (MONO_GC_EVENT_PRE_STOP_WORLD_LOCKED, generation);
+
        /* We start to scan after locks are taking, this ensures we won't be interrupted. */
        sgen_process_togglerefs ();
 
@@ -225,12 +242,11 @@ sgen_client_stop_world (int generation)
 
 /* LOCKING: assumes the GC lock is held */
 void
-sgen_client_restart_world (int generation, GGTimingInfo *timing)
+sgen_client_restart_world (int generation, gint64 *stw_time)
 {
        TV_DECLARE (end_sw);
        TV_DECLARE (start_handshake);
-       TV_DECLARE (end_bridge);
-       unsigned long usec, bridge_usec;
+       unsigned long usec;
 
        /* notify the profiler of the leftovers */
        /* FIXME this is the wrong spot at we can STW for non collection reasons. */
@@ -253,6 +269,7 @@ sgen_client_restart_world (int generation, GGTimingInfo *timing)
        time_restart_world += TV_ELAPSED (start_handshake, end_sw);
        usec = TV_ELAPSED (stop_world_time, end_sw);
        max_pause_usec = MAX (usec, max_pause_usec);
+       end_of_last_stw = end_sw;
 
        SGEN_LOG (2, "restarted (pause time: %d usec, max: %d)", (int)usec, (int)max_pause_usec);
 
@@ -268,13 +285,9 @@ sgen_client_restart_world (int generation, GGTimingInfo *timing)
         */
        release_gc_locks ();
 
-       TV_GETTIME (end_bridge);
-       bridge_usec = TV_ELAPSED (end_sw, end_bridge);
+       mono_profiler_gc_event (MONO_GC_EVENT_POST_START_WORLD_UNLOCKED, generation);
 
-       if (timing) {
-               timing [0].stw_time = usec;
-               timing [0].bridge_time = bridge_usec;
-       }
+       *stw_time = usec;
 }
 
 void
@@ -342,25 +355,6 @@ sgen_is_thread_in_current_stw (SgenThreadInfo *info, int *reason)
        return TRUE;
 }
 
-static void
-update_sgen_info (SgenThreadInfo *info)
-{
-       char *stack_start;
-
-       /* Once we remove the old suspend code, we should move sgen to directly access the state in MonoThread */
-       info->client_info.stopped_domain = (MonoDomain *)mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
-       info->client_info.stopped_ip = (gpointer) MONO_CONTEXT_GET_IP (&mono_thread_info_get_suspend_state (info)->ctx);
-       stack_start = (char*)MONO_CONTEXT_GET_SP (&mono_thread_info_get_suspend_state (info)->ctx) - REDZONE_SIZE;
-
-       /* altstack signal handler, sgen can't handle them, mono-threads should have handled this. */
-       if (stack_start < (char*)info->client_info.stack_start_limit || stack_start >= (char*)info->client_info.stack_end)
-               g_error ("BAD STACK");
-
-       info->client_info.stack_start = stack_start;
-       g_assert (info->client_info.stack_start);
-       info->client_info.ctx = mono_thread_info_get_suspend_state (info)->ctx;
-}
-
 static void
 sgen_unified_suspend_stop_world (void)
 {
@@ -400,10 +394,7 @@ sgen_unified_suspend_stop_world (void)
                        - We haven't accepted the previous suspend as good.
                        - We haven't gave up on it for this STW (it's either bad or asked not to)
                        */
-                       if (!mono_thread_info_check_suspend_result (info)) {
-                               THREADS_STW_DEBUG ("[GC-STW-RESTART] SKIP thread %p failed to finish to suspend\n", mono_thread_info_get_tid (info));
-                               info->client_info.skip = TRUE;
-                       } else if (mono_thread_info_in_critical_location (info)) {
+                       if (mono_thread_info_in_critical_location (info)) {
                                gboolean res;
                                gint suspend_count = mono_thread_info_suspend_count (info);
                                if (!(suspend_count == 1))
@@ -454,9 +445,36 @@ sgen_unified_suspend_stop_world (void)
        FOREACH_THREAD (info) {
                int reason = 0;
                if (sgen_is_thread_in_current_stw (info, &reason)) {
+                       MonoThreadUnwindState *state;
+
                        THREADS_STW_DEBUG ("[GC-STW-SUSPEND-END] thread %p is suspended\n", mono_thread_info_get_tid (info));
                        g_assert (info->client_info.suspend_done);
-                       update_sgen_info (info);
+
+                       state = mono_thread_info_get_suspend_state (info);
+
+                       info->client_info.ctx = state->ctx;
+
+                       if (!state->unwind_data [MONO_UNWIND_DATA_DOMAIN] || !state->unwind_data [MONO_UNWIND_DATA_LMF]) {
+                               /* thread is starting or detaching, nothing to scan here */
+                               info->client_info.stopped_domain = NULL;
+                               info->client_info.stopped_ip = NULL;
+                               info->client_info.stack_start = NULL;
+                       } else {
+                               /* Once we remove the old suspend code, we should move sgen to directly access the state in MonoThread */
+                               info->client_info.stopped_domain = (MonoDomain*) mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
+                               info->client_info.stopped_ip = (gpointer) (MONO_CONTEXT_GET_IP (&info->client_info.ctx));
+                               info->client_info.stack_start = (gpointer) ((char*)MONO_CONTEXT_GET_SP (&info->client_info.ctx) - REDZONE_SIZE);
+
+                               /* altstack signal handler, sgen can't handle them, mono-threads should have handled this. */
+                               if (!info->client_info.stack_start
+                                        || info->client_info.stack_start < info->client_info.stack_start_limit
+                                        || info->client_info.stack_start >= info->client_info.stack_end) {
+                                       g_error ("BAD STACK: stack_start = %p, stack_start_limit = %p, stack_end = %p",
+                                               info->client_info.stack_start, info->client_info.stack_start_limit, info->client_info.stack_end);
+                               }
+                       }
+
+                       binary_protocol_thread_suspend ((gpointer) mono_thread_info_get_tid (info), info->client_info.stopped_ip);
                } else {
                        THREADS_STW_DEBUG ("[GC-STW-SUSPEND-END] thread %p is NOT suspended, reason %d\n", mono_thread_info_get_tid (info), reason);
                        g_assert (!info->client_info.suspend_done || info == mono_thread_info_current ());
@@ -473,6 +491,8 @@ sgen_unified_suspend_restart_world (void)
                if (sgen_is_thread_in_current_stw (info, &reason)) {
                        g_assert (mono_thread_info_begin_resume (info));
                        THREADS_STW_DEBUG ("[GC-STW-RESUME-WORLD] RESUME thread %p\n", mono_thread_info_get_tid (info));
+
+                       binary_protocol_thread_restart ((gpointer) mono_thread_info_get_tid (info));
                } else {
                        THREADS_STW_DEBUG ("[GC-STW-RESUME-WORLD] IGNORE thread %p, reason %d\n", mono_thread_info_get_tid (info), reason);
                }