[sgen] Fix locking of the worker distribute gray queue.
[mono.git] / mono / metadata / sgen-protocol.c
1 /*
2  * sgen-protocol.c: Binary protocol of internal activity, to aid
3  * debugging.
4  *
5  * Copyright 2001-2003 Ximian, Inc
6  * Copyright 2003-2010 Novell, Inc.
7  * Copyright (C) 2012 Xamarin Inc
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License 2.0 as published by the Free Software Foundation;
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License 2.0 along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifdef HAVE_SGEN_GC
24
25 #include "config.h"
26 #include "sgen-gc.h"
27 #include "sgen-protocol.h"
28 #include "sgen-memory-governor.h"
29 #include "utils/mono-mmap.h"
30
31 #ifdef SGEN_BINARY_PROTOCOL
32
33 /* If not null, dump binary protocol to this file */
34 static FILE *binary_protocol_file = NULL;
35
36 static int binary_protocol_use_count = 0;
37
38 #define BINARY_PROTOCOL_BUFFER_SIZE     (65536 - 2 * 8)
39
40 typedef struct _BinaryProtocolBuffer BinaryProtocolBuffer;
41 struct _BinaryProtocolBuffer {
42         BinaryProtocolBuffer *next;
43         int index;
44         unsigned char buffer [BINARY_PROTOCOL_BUFFER_SIZE];
45 };
46
47 static BinaryProtocolBuffer *binary_protocol_buffers = NULL;
48
49 void
50 binary_protocol_init (const char *filename)
51 {
52         binary_protocol_file = fopen (filename, "w");
53 }
54
55 gboolean
56 binary_protocol_is_enabled (void)
57 {
58         return binary_protocol_file != NULL;
59 }
60
61 static void
62 binary_protocol_flush_buffers_rec (BinaryProtocolBuffer *buffer)
63 {
64         if (!buffer)
65                 return;
66
67         binary_protocol_flush_buffers_rec (buffer->next);
68
69         g_assert (buffer->index > 0);
70         fwrite (buffer->buffer, 1, buffer->index, binary_protocol_file);
71
72         sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
73 }
74
75 void
76 binary_protocol_flush_buffers (gboolean force)
77 {
78         if (!binary_protocol_file)
79                 return;
80
81         if (!force && binary_protocol_use_count != 0)
82                 return;
83
84         binary_protocol_flush_buffers_rec (binary_protocol_buffers);
85         binary_protocol_buffers = NULL;
86
87         fflush (binary_protocol_file);
88 }
89
90 static BinaryProtocolBuffer*
91 binary_protocol_get_buffer (int length)
92 {
93         BinaryProtocolBuffer *buffer, *new_buffer;
94
95  retry:
96         buffer = binary_protocol_buffers;
97         if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
98                 return buffer;
99
100         new_buffer = sgen_alloc_os_memory (sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, "debugging memory");
101         new_buffer->next = buffer;
102         new_buffer->index = 0;
103
104         if (InterlockedCompareExchangePointer ((void**)&binary_protocol_buffers, new_buffer, buffer) != buffer) {
105                 sgen_free_os_memory (new_buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
106                 goto retry;
107         }
108
109         return new_buffer;
110 }
111
112
113 static void
114 protocol_entry (unsigned char type, gpointer data, int size)
115 {
116         int index;
117         BinaryProtocolBuffer *buffer;
118         int old_count;
119
120         if (!binary_protocol_file)
121                 return;
122
123         do {
124                 old_count = binary_protocol_use_count;
125                 g_assert (old_count >= 0);
126         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count + 1, old_count) != old_count);
127
128  retry:
129         buffer = binary_protocol_get_buffer (size + 1);
130  retry_same_buffer:
131         index = buffer->index;
132         if (index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
133                 goto retry;
134
135         if (InterlockedCompareExchange (&buffer->index, index + 1 + size, index) != index)
136                 goto retry_same_buffer;
137
138         /* FIXME: if we're interrupted at this point, we have a buffer
139            entry that contains random data. */
140
141         buffer->buffer [index++] = type;
142         memcpy (buffer->buffer + index, data, size);
143         index += size;
144
145         g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
146
147         do {
148                 old_count = binary_protocol_use_count;
149                 g_assert (old_count > 0);
150         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count - 1, old_count) != old_count);
151 }
152
153 void
154 binary_protocol_collection_begin (int index, int generation)
155 {
156         SGenProtocolCollection entry = { index, generation };
157         binary_protocol_flush_buffers (FALSE);
158         protocol_entry (SGEN_PROTOCOL_COLLECTION_BEGIN, &entry, sizeof (SGenProtocolCollection));
159 }
160
161 void
162 binary_protocol_collection_end (int index, int generation)
163 {
164         SGenProtocolCollection entry = { index, generation };
165         binary_protocol_flush_buffers (FALSE);
166         protocol_entry (SGEN_PROTOCOL_COLLECTION_END, &entry, sizeof (SGenProtocolCollection));
167 }
168
169 void
170 binary_protocol_alloc (gpointer obj, gpointer vtable, int size)
171 {
172         SGenProtocolAlloc entry = { obj, vtable, size };
173         protocol_entry (SGEN_PROTOCOL_ALLOC, &entry, sizeof (SGenProtocolAlloc));
174 }
175
176 void
177 binary_protocol_alloc_pinned (gpointer obj, gpointer vtable, int size)
178 {
179         SGenProtocolAlloc entry = { obj, vtable, size };
180         protocol_entry (SGEN_PROTOCOL_ALLOC_PINNED, &entry, sizeof (SGenProtocolAlloc));
181 }
182
183 void
184 binary_protocol_alloc_degraded (gpointer obj, gpointer vtable, int size)
185 {
186         SGenProtocolAlloc entry = { obj, vtable, size };
187         protocol_entry (SGEN_PROTOCOL_ALLOC_DEGRADED, &entry, sizeof (SGenProtocolAlloc));
188 }
189
190 void
191 binary_protocol_copy (gpointer from, gpointer to, gpointer vtable, int size)
192 {
193         SGenProtocolCopy entry = { from, to, vtable, size };
194         protocol_entry (SGEN_PROTOCOL_COPY, &entry, sizeof (SGenProtocolCopy));
195 }
196
197 void
198 binary_protocol_pin (gpointer obj, gpointer vtable, int size)
199 {
200         SGenProtocolPin entry = { obj, vtable, size };
201         protocol_entry (SGEN_PROTOCOL_PIN, &entry, sizeof (SGenProtocolPin));
202 }
203
204 void
205 binary_protocol_mark (gpointer obj, gpointer vtable, int size)
206 {
207         SGenProtocolMark entry = { obj, vtable, size };
208         protocol_entry (SGEN_PROTOCOL_MARK, &entry, sizeof (SGenProtocolMark));
209 }
210
211 void
212 binary_protocol_wbarrier (gpointer ptr, gpointer value, gpointer value_vtable)
213 {
214         SGenProtocolWBarrier entry = { ptr, value, value_vtable };
215         protocol_entry (SGEN_PROTOCOL_WBARRIER, &entry, sizeof (SGenProtocolWBarrier));
216 }
217
218 void
219 binary_protocol_global_remset (gpointer ptr, gpointer value, gpointer value_vtable)
220 {
221         SGenProtocolGlobalRemset entry = { ptr, value, value_vtable };
222         protocol_entry (SGEN_PROTOCOL_GLOBAL_REMSET, &entry, sizeof (SGenProtocolGlobalRemset));
223 }
224
225 void
226 binary_protocol_ptr_update (gpointer ptr, gpointer old_value, gpointer new_value, gpointer vtable, int size)
227 {
228         SGenProtocolPtrUpdate entry = { ptr, old_value, new_value, vtable, size };
229         protocol_entry (SGEN_PROTOCOL_PTR_UPDATE, &entry, sizeof (SGenProtocolPtrUpdate));
230 }
231
232 void
233 binary_protocol_cleanup (gpointer ptr, gpointer vtable, int size)
234 {
235         SGenProtocolCleanup entry = { ptr, vtable, size };
236         protocol_entry (SGEN_PROTOCOL_CLEANUP, &entry, sizeof (SGenProtocolCleanup));
237 }
238
239 void
240 binary_protocol_empty (gpointer start, int size)
241 {
242         SGenProtocolEmpty entry = { start, size };
243         protocol_entry (SGEN_PROTOCOL_EMPTY, &entry, sizeof (SGenProtocolEmpty));
244 }
245
246 void
247 binary_protocol_thread_suspend (gpointer thread, gpointer stopped_ip)
248 {
249         SGenProtocolThreadSuspend entry = { thread, stopped_ip };
250         protocol_entry (SGEN_PROTOCOL_THREAD_SUSPEND, &entry, sizeof (SGenProtocolThreadSuspend));
251 }
252
253 void
254 binary_protocol_thread_restart (gpointer thread)
255 {
256         SGenProtocolThreadRestart entry = { thread };
257         protocol_entry (SGEN_PROTOCOL_THREAD_RESTART, &entry, sizeof (SGenProtocolThreadRestart));
258 }
259
260 void
261 binary_protocol_thread_register (gpointer thread)
262 {
263         SGenProtocolThreadRegister entry = { thread };
264         protocol_entry (SGEN_PROTOCOL_THREAD_REGISTER, &entry, sizeof (SGenProtocolThreadRegister));
265
266 }
267
268 void
269 binary_protocol_thread_unregister (gpointer thread)
270 {
271         SGenProtocolThreadUnregister entry = { thread };
272         protocol_entry (SGEN_PROTOCOL_THREAD_UNREGISTER, &entry, sizeof (SGenProtocolThreadUnregister));
273
274 }
275
276 void
277 binary_protocol_missing_remset (gpointer obj, gpointer obj_vtable, int offset, gpointer value, gpointer value_vtable, int value_pinned)
278 {
279         SGenProtocolMissingRemset entry = { obj, obj_vtable, offset, value, value_vtable, value_pinned };
280         protocol_entry (SGEN_PROTOCOL_MISSING_REMSET, &entry, sizeof (SGenProtocolMissingRemset));
281
282 }
283
284 #endif
285
286 #endif /* HAVE_SGEN_GC */