2 * sgen-stw.c: Stop the world functionality
5 * Paolo Molaro (lupus@ximian.com)
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * Copyright 2005-2011 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10 * Copyright 2011 Xamarin, Inc.
11 * Copyright (C) 2012 Xamarin Inc
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License 2.0 as published by the Free Software Foundation;
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License 2.0 along with this library; if not, write to the Free
24 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include "metadata/sgen-gc.h"
31 #include "metadata/sgen-protocol.h"
32 #include "metadata/sgen-memory-governor.h"
33 #include "metadata/profiler-private.h"
34 #include "utils/mono-time.h"
35 #include "utils/dtrace.h"
37 #define TV_DECLARE SGEN_TV_DECLARE
38 #define TV_GETTIME SGEN_TV_GETTIME
39 #define TV_ELAPSED SGEN_TV_ELAPSED
40 #define TV_ELAPSED_MS SGEN_TV_ELAPSED_MS
43 align_pointer (void *ptr)
46 p += sizeof (gpointer) - 1;
47 p &= ~ (sizeof (gpointer) - 1);
52 static MonoContext cur_thread_ctx = {0};
54 static mword cur_thread_regs [ARCH_NUM_REGS] = {0};
58 update_current_thread_stack (void *start)
61 #if !defined(USE_MONO_CTX) && !defined(_MSC_VER)
62 void *reg_ptr = cur_thread_regs;
64 SgenThreadInfo *info = mono_thread_info_current ();
66 info->stack_start = align_pointer (&stack_guard);
67 g_assert (info->stack_start >= info->stack_start_limit && info->stack_start < info->stack_end);
69 MONO_CONTEXT_GET_CURRENT (cur_thread_ctx);
70 memcpy (&info->ctx, &cur_thread_ctx, sizeof (MonoContext));
71 if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
72 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL, &info->ctx);
75 ARCH_STORE_REGS (cur_thread_regs);
76 memcpy (&info->regs, cur_thread_regs, sizeof (info->regs));
78 ARCH_STORE_REGS (reg_ptr);
79 memcpy (&info->regs, reg_ptr, sizeof (info->regs));
81 if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
82 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL, NULL);
87 is_ip_in_managed_allocator (MonoDomain *domain, gpointer ip)
91 if (!mono_thread_internal_current ())
92 /* Happens during thread attach */
97 if (!sgen_has_critical_method ())
99 ji = mono_jit_info_table_find (domain, ip);
103 return sgen_is_critical_method (ji->method);
107 restart_threads_until_none_in_managed_allocator (void)
109 SgenThreadInfo *info;
110 int num_threads_died = 0;
111 int sleep_duration = -1;
114 int restart_count = 0, restarted_count = 0;
115 /* restart all threads that stopped in the
117 FOREACH_THREAD_SAFE (info) {
119 if (info->skip || info->gc_disabled || !info->joined_stw)
121 if (!info->thread_is_dying && (!info->stack_start || info->in_critical_region || info->info.inside_critical_region ||
122 is_ip_in_managed_allocator (info->stopped_domain, info->stopped_ip))) {
123 binary_protocol_thread_restart ((gpointer)mono_thread_info_get_tid (info));
124 SGEN_LOG (3, "thread %p resumed.", (void*) (size_t) info->info.native_handle);
125 result = sgen_resume_thread (info);
132 /* we set the stopped_ip to
133 NULL for threads which
134 we're not restarting so
135 that we can easily identify
137 info->stopped_ip = NULL;
138 info->stopped_domain = NULL;
140 } END_FOREACH_THREAD_SAFE
141 /* if no threads were restarted, we're done */
142 if (restart_count == 0)
145 /* wait for the threads to signal their restart */
146 sgen_wait_for_suspend_ack (restart_count);
148 if (sleep_duration < 0) {
156 g_usleep (sleep_duration);
157 sleep_duration += 10;
160 /* stop them again */
161 FOREACH_THREAD (info) {
163 if (info->skip || info->stopped_ip == NULL)
165 result = sgen_suspend_thread (info);
173 /* some threads might have died */
174 num_threads_died += restart_count - restarted_count;
175 /* wait for the threads to signal their suspension
177 sgen_wait_for_suspend_ack (restarted_count);
180 return num_threads_died;
184 acquire_gc_locks (void)
187 mono_thread_info_suspend_lock ();
191 release_gc_locks (void)
193 mono_thread_info_suspend_unlock ();
198 stw_bridge_process (void)
200 sgen_bridge_processing_stw_step ();
204 bridge_process (int generation)
206 sgen_bridge_processing_finish (generation);
209 static TV_DECLARE (stop_world_time);
210 static unsigned long max_pause_usec = 0;
212 /* LOCKING: assumes the GC lock is held */
214 sgen_stop_world (int generation)
218 /*XXX this is the right stop, thought might not be the nicest place to put it*/
219 sgen_process_togglerefs ();
221 mono_profiler_gc_event (MONO_GC_EVENT_PRE_STOP_WORLD, generation);
222 MONO_GC_WORLD_STOP_BEGIN ();
225 update_current_thread_stack (&count);
227 sgen_global_stop_count++;
228 SGEN_LOG (3, "stopping world n %d from %p %p", sgen_global_stop_count, mono_thread_info_current (), (gpointer)mono_native_thread_id_get ());
229 TV_GETTIME (stop_world_time);
230 count = sgen_thread_handshake (TRUE);
231 dead = restart_threads_until_none_in_managed_allocator ();
233 g_error ("More threads have died (%d) that been initialy suspended %d", dead, count);
236 SGEN_LOG (3, "world stopped %d thread(s)", count);
237 mono_profiler_gc_event (MONO_GC_EVENT_POST_STOP_WORLD, generation);
238 MONO_GC_WORLD_STOP_END ();
240 sgen_memgov_collection_start (generation);
241 sgen_bridge_reset_data ();
246 /* LOCKING: assumes the GC lock is held */
248 sgen_restart_world (int generation, GGTimingInfo *timing)
251 SgenThreadInfo *info;
253 TV_DECLARE (end_bridge);
254 unsigned long usec, bridge_usec;
256 /* notify the profiler of the leftovers */
257 /* FIXME this is the wrong spot at we can STW for non collection reasons. */
258 if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_GC_MOVES))
259 sgen_gc_event_moves ();
260 mono_profiler_gc_event (MONO_GC_EVENT_PRE_START_WORLD, generation);
261 MONO_GC_WORLD_RESTART_BEGIN (generation);
262 FOREACH_THREAD (info) {
263 info->stack_start = NULL;
265 memset (&info->ctx, 0, sizeof (MonoContext));
267 memset (&info->regs, 0, sizeof (info->regs));
271 stw_bridge_process ();
274 count = sgen_thread_handshake (FALSE);
276 usec = TV_ELAPSED (stop_world_time, end_sw);
277 max_pause_usec = MAX (usec, max_pause_usec);
278 SGEN_LOG (2, "restarted %d thread(s) (pause time: %d usec, max: %d)", count, (int)usec, (int)max_pause_usec);
279 mono_profiler_gc_event (MONO_GC_EVENT_POST_START_WORLD, generation);
280 MONO_GC_WORLD_RESTART_END (generation);
282 mono_thread_hazardous_try_free_some ();
284 bridge_process (generation);
286 TV_GETTIME (end_bridge);
287 bridge_usec = TV_ELAPSED (end_sw, end_bridge);
290 timing [0].stw_time = usec;
291 timing [0].bridge_time = bridge_usec;
294 sgen_memgov_collection_end (generation, timing, timing ? 2 : 0);