Introduce a callback API to have notifications when runtime resources exceed preset...
[mono.git] / mono / metadata / sgen-protocol.c
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "config.h"
26 #include "sgen-protocol.h"
27
28 #ifdef SGEN_BINARY_PROTOCOL
29
30 /* If not null, dump binary protocol to this file */
31 static FILE *binary_protocol_file = NULL;
32
33 static int binary_protocol_use_count = 0;
34
35 #define BINARY_PROTOCOL_BUFFER_SIZE     (65536 - 2 * 8)
36
37 typedef struct _BinaryProtocolBuffer BinaryProtocolBuffer;
38 struct _BinaryProtocolBuffer {
39         BinaryProtocolBuffer *next;
40         int index;
41         unsigned char buffer [BINARY_PROTOCOL_BUFFER_SIZE];
42 };
43
44 static BinaryProtocolBuffer *binary_protocol_buffers = NULL;
45 LOCK_DECLARE (binary_protocol_mutex);
46
47 void
48 binary_protocol_init (const char *filename)
49 {
50         binary_protocol_file = fopen (filename, "w");
51 }
52
53 gboolean
54 binary_protocol_is_enabled (void)
55 {
56         return binary_protocol_file != NULL;
57 }
58
59 static void
60 binary_protocol_flush_buffers_rec (BinaryProtocolBuffer *buffer)
61 {
62         if (!buffer)
63                 return;
64
65         binary_protocol_flush_buffers_rec (buffer->next);
66
67         g_assert (buffer->index > 0);
68         fwrite (buffer->buffer, 1, buffer->index, binary_protocol_file);
69
70         mono_sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer));
71 }
72
73 void
74 binary_protocol_flush_buffers (gboolean force)
75 {
76         if (!binary_protocol_file)
77                 return;
78
79         if (!force && binary_protocol_use_count != 0)
80                 return;
81
82         binary_protocol_flush_buffers_rec (binary_protocol_buffers);
83         binary_protocol_buffers = NULL;
84
85         fflush (binary_protocol_file);
86 }
87
88 static BinaryProtocolBuffer*
89 binary_protocol_get_buffer (int length)
90 {
91         BinaryProtocolBuffer *buffer, *new_buffer;
92
93  retry:
94         buffer = binary_protocol_buffers;
95         if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
96                 return buffer;
97
98         new_buffer = mono_sgen_alloc_os_memory (sizeof (BinaryProtocolBuffer), TRUE);
99         new_buffer->next = buffer;
100         new_buffer->index = 0;
101
102         if (InterlockedCompareExchangePointer ((void**)&binary_protocol_buffers, new_buffer, buffer) != buffer) {
103                 mono_sgen_free_os_memory (new_buffer, sizeof (BinaryProtocolBuffer));
104                 goto retry;
105         }
106
107         return new_buffer;
108 }
109
110
111 static void
112 protocol_entry (unsigned char type, gpointer data, int size)
113 {
114         int index;
115         BinaryProtocolBuffer *buffer;
116         int old_count;
117
118         if (!binary_protocol_file)
119                 return;
120
121         do {
122                 old_count = binary_protocol_use_count;
123                 g_assert (old_count >= 0);
124         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count + 1, old_count) != old_count);
125
126  retry:
127         buffer = binary_protocol_get_buffer (size + 1);
128  retry_same_buffer:
129         index = buffer->index;
130         if (index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
131                 goto retry;
132
133         if (InterlockedCompareExchange (&buffer->index, index + 1 + size, index) != index)
134                 goto retry_same_buffer;
135
136         /* FIXME: if we're interrupted at this point, we have a buffer
137            entry that contains random data. */
138
139         buffer->buffer [index++] = type;
140         memcpy (buffer->buffer + index, data, size);
141         index += size;
142
143         g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
144
145         do {
146                 old_count = binary_protocol_use_count;
147                 g_assert (old_count > 0);
148         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count - 1, old_count) != old_count);
149 }
150
151 void
152 binary_protocol_collection (int generation)
153 {
154         SGenProtocolCollection entry = { generation };
155         binary_protocol_flush_buffers (FALSE);
156         protocol_entry (SGEN_PROTOCOL_COLLECTION, &entry, sizeof (SGenProtocolCollection));
157 }
158
159 void
160 binary_protocol_alloc (gpointer obj, gpointer vtable, int size)
161 {
162         SGenProtocolAlloc entry = { obj, vtable, size };
163         protocol_entry (SGEN_PROTOCOL_ALLOC, &entry, sizeof (SGenProtocolAlloc));
164 }
165
166 void
167 binary_protocol_alloc_pinned (gpointer obj, gpointer vtable, int size)
168 {
169         SGenProtocolAlloc entry = { obj, vtable, size };
170         protocol_entry (SGEN_PROTOCOL_ALLOC_PINNED, &entry, sizeof (SGenProtocolAlloc));
171 }
172
173 void
174 binary_protocol_alloc_degraded (gpointer obj, gpointer vtable, int size)
175 {
176         SGenProtocolAlloc entry = { obj, vtable, size };
177         protocol_entry (SGEN_PROTOCOL_ALLOC_DEGRADED, &entry, sizeof (SGenProtocolAlloc));
178 }
179
180 void
181 binary_protocol_copy (gpointer from, gpointer to, gpointer vtable, int size)
182 {
183         SGenProtocolCopy entry = { from, to, vtable, size };
184         protocol_entry (SGEN_PROTOCOL_COPY, &entry, sizeof (SGenProtocolCopy));
185 }
186
187 void
188 binary_protocol_pin (gpointer obj, gpointer vtable, int size)
189 {
190         SGenProtocolPin entry = { obj, vtable, size };
191         protocol_entry (SGEN_PROTOCOL_PIN, &entry, sizeof (SGenProtocolPin));
192 }
193
194 void
195 binary_protocol_mark (gpointer obj, gpointer vtable, int size)
196 {
197         SGenProtocolMark entry = { obj, vtable, size };
198         protocol_entry (SGEN_PROTOCOL_MARK, &entry, sizeof (SGenProtocolMark));
199 }
200
201 void
202 binary_protocol_wbarrier (gpointer ptr, gpointer value, gpointer value_vtable)
203 {
204         SGenProtocolWBarrier entry = { ptr, value, value_vtable };
205         protocol_entry (SGEN_PROTOCOL_WBARRIER, &entry, sizeof (SGenProtocolWBarrier));
206 }
207
208 void
209 binary_protocol_global_remset (gpointer ptr, gpointer value, gpointer value_vtable)
210 {
211         SGenProtocolGlobalRemset entry = { ptr, value, value_vtable };
212         protocol_entry (SGEN_PROTOCOL_GLOBAL_REMSET, &entry, sizeof (SGenProtocolGlobalRemset));
213 }
214
215 void
216 binary_protocol_ptr_update (gpointer ptr, gpointer old_value, gpointer new_value, gpointer vtable, int size)
217 {
218         SGenProtocolPtrUpdate entry = { ptr, old_value, new_value, vtable, size };
219         protocol_entry (SGEN_PROTOCOL_PTR_UPDATE, &entry, sizeof (SGenProtocolPtrUpdate));
220 }
221
222 void
223 binary_protocol_cleanup (gpointer ptr, gpointer vtable, int size)
224 {
225         SGenProtocolCleanup entry = { ptr, vtable, size };
226         protocol_entry (SGEN_PROTOCOL_CLEANUP, &entry, sizeof (SGenProtocolCleanup));
227 }
228
229 void
230 binary_protocol_empty (gpointer start, int size)
231 {
232         SGenProtocolEmpty entry = { start, size };
233         protocol_entry (SGEN_PROTOCOL_EMPTY, &entry, sizeof (SGenProtocolEmpty));
234 }
235
236 void
237 binary_protocol_thread_restart (gpointer thread)
238 {
239         SGenProtocolThreadRestart entry = { thread };
240         protocol_entry (SGEN_PROTOCOL_THREAD_RESTART, &entry, sizeof (SGenProtocolThreadRestart));
241
242 }
243
244 void
245 binary_protocol_thread_register (gpointer thread)
246 {
247         SGenProtocolThreadRegister entry = { thread };
248         protocol_entry (SGEN_PROTOCOL_THREAD_REGISTER, &entry, sizeof (SGenProtocolThreadRegister));
249
250 }
251
252 void
253 binary_protocol_thread_unregister (gpointer thread)
254 {
255         SGenProtocolThreadUnregister entry = { thread };
256         protocol_entry (SGEN_PROTOCOL_THREAD_UNREGISTER, &entry, sizeof (SGenProtocolThreadUnregister));
257
258 }
259
260 void
261 binary_protocol_missing_remset (gpointer obj, gpointer obj_vtable, int offset, gpointer value, gpointer value_vtable, int value_pinned)
262 {
263         SGenProtocolMissingRemset entry = { obj, obj_vtable, offset, value, value_vtable, value_pinned };
264         protocol_entry (SGEN_PROTOCOL_MISSING_REMSET, &entry, sizeof (SGenProtocolMissingRemset));
265
266 }
267
268 #endif