New test.
[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 "sgen-protocol.h"
26
27 #ifdef SGEN_BINARY_PROTOCOL
28
29 /* If not null, dump binary protocol to this file */
30 static FILE *binary_protocol_file = NULL;
31
32 static int binary_protocol_use_count = 0;
33
34 #define BINARY_PROTOCOL_BUFFER_SIZE     (65536 - 2 * 8)
35
36 typedef struct _BinaryProtocolBuffer BinaryProtocolBuffer;
37 struct _BinaryProtocolBuffer {
38         BinaryProtocolBuffer *next;
39         int index;
40         unsigned char buffer [BINARY_PROTOCOL_BUFFER_SIZE];
41 };
42
43 static BinaryProtocolBuffer *binary_protocol_buffers = NULL;
44 LOCK_DECLARE (binary_protocol_mutex);
45
46 static void
47 binary_protocol_flush_buffers_rec (BinaryProtocolBuffer *buffer)
48 {
49         if (!buffer)
50                 return;
51
52         binary_protocol_flush_buffers_rec (buffer->next);
53
54         g_assert (buffer->index > 0);
55         fwrite (buffer->buffer, 1, buffer->index, binary_protocol_file);
56
57         mono_sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer));
58 }
59
60 static void
61 binary_protocol_flush_buffers (gboolean force)
62 {
63         if (!binary_protocol_file)
64                 return;
65
66         if (!force && binary_protocol_use_count != 0)
67                 return;
68
69         binary_protocol_flush_buffers_rec (binary_protocol_buffers);
70         binary_protocol_buffers = NULL;
71
72         fflush (binary_protocol_file);
73 }
74
75 static BinaryProtocolBuffer*
76 binary_protocol_get_buffer (int length)
77 {
78         BinaryProtocolBuffer *buffer, *new_buffer;
79
80  retry:
81         buffer = binary_protocol_buffers;
82         if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
83                 return buffer;
84
85         new_buffer = mono_sgen_alloc_os_memory (sizeof (BinaryProtocolBuffer), TRUE);
86         new_buffer->next = buffer;
87         new_buffer->index = 0;
88
89         if (InterlockedCompareExchangePointer ((void**)&binary_protocol_buffers, new_buffer, buffer) != buffer) {
90                 mono_sgen_free_os_memory (new_buffer, sizeof (BinaryProtocolBuffer));
91                 goto retry;
92         }
93
94         return new_buffer;
95 }
96
97
98 static void
99 protocol_entry (unsigned char type, gpointer data, int size)
100 {
101         int index;
102         BinaryProtocolBuffer *buffer;
103         int old_count;
104
105         if (!binary_protocol_file)
106                 return;
107
108         do {
109                 old_count = binary_protocol_use_count;
110                 g_assert (old_count >= 0);
111         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count + 1, old_count) != old_count);
112
113  retry:
114         buffer = binary_protocol_get_buffer (size + 1);
115  retry_same_buffer:
116         index = buffer->index;
117         if (index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
118                 goto retry;
119
120         if (InterlockedCompareExchange (&buffer->index, index + 1 + size, index) != index)
121                 goto retry_same_buffer;
122
123         /* FIXME: if we're interrupted at this point, we have a buffer
124            entry that contains random data. */
125
126         buffer->buffer [index++] = type;
127         memcpy (buffer->buffer + index, data, size);
128         index += size;
129
130         g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
131
132         do {
133                 old_count = binary_protocol_use_count;
134                 g_assert (old_count > 0);
135         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count - 1, old_count) != old_count);
136 }
137
138 void
139 binary_protocol_collection (int generation)
140 {
141         SGenProtocolCollection entry = { generation };
142         binary_protocol_flush_buffers (FALSE);
143         protocol_entry (SGEN_PROTOCOL_COLLECTION, &entry, sizeof (SGenProtocolCollection));
144 }
145
146 void
147 binary_protocol_alloc (gpointer obj, gpointer vtable, int size)
148 {
149         SGenProtocolAlloc entry = { obj, vtable, size };
150         protocol_entry (SGEN_PROTOCOL_ALLOC, &entry, sizeof (SGenProtocolAlloc));
151 }
152
153 void
154 binary_protocol_alloc_pinned (gpointer obj, gpointer vtable, int size)
155 {
156         SGenProtocolAlloc entry = { obj, vtable, size };
157         protocol_entry (SGEN_PROTOCOL_ALLOC_PINNED, &entry, sizeof (SGenProtocolAlloc));
158 }
159
160 void
161 binary_protocol_alloc_degraded (gpointer obj, gpointer vtable, int size)
162 {
163         SGenProtocolAlloc entry = { obj, vtable, size };
164         protocol_entry (SGEN_PROTOCOL_ALLOC_DEGRADED, &entry, sizeof (SGenProtocolAlloc));
165 }
166
167 void
168 binary_protocol_copy (gpointer from, gpointer to, gpointer vtable, int size)
169 {
170         SGenProtocolCopy entry = { from, to, vtable, size };
171         protocol_entry (SGEN_PROTOCOL_COPY, &entry, sizeof (SGenProtocolCopy));
172 }
173
174 void
175 binary_protocol_pin (gpointer obj, gpointer vtable, int size)
176 {
177         SGenProtocolPin entry = { obj, vtable, size };
178         protocol_entry (SGEN_PROTOCOL_PIN, &entry, sizeof (SGenProtocolPin));
179 }
180
181 void
182 binary_protocol_mark (gpointer obj, gpointer vtable, int size)
183 {
184         SGenProtocolMark entry = { obj, vtable, size };
185         protocol_entry (SGEN_PROTOCOL_MARK, &entry, sizeof (SGenProtocolMark));
186 }
187
188 void
189 binary_protocol_wbarrier (gpointer ptr, gpointer value, gpointer value_vtable)
190 {
191         SGenProtocolWBarrier entry = { ptr, value, value_vtable };
192         protocol_entry (SGEN_PROTOCOL_WBARRIER, &entry, sizeof (SGenProtocolWBarrier));
193 }
194
195 void
196 binary_protocol_global_remset (gpointer ptr, gpointer value, gpointer value_vtable)
197 {
198         SGenProtocolGlobalRemset entry = { ptr, value, value_vtable };
199         protocol_entry (SGEN_PROTOCOL_GLOBAL_REMSET, &entry, sizeof (SGenProtocolGlobalRemset));
200 }
201
202 void
203 binary_protocol_ptr_update (gpointer ptr, gpointer old_value, gpointer new_value, gpointer vtable, int size)
204 {
205         SGenProtocolPtrUpdate entry = { ptr, old_value, new_value, vtable, size };
206         protocol_entry (SGEN_PROTOCOL_PTR_UPDATE, &entry, sizeof (SGenProtocolPtrUpdate));
207 }
208
209 void
210 binary_protocol_cleanup (gpointer ptr, gpointer vtable, int size)
211 {
212         SGenProtocolCleanup entry = { ptr, vtable, size };
213         protocol_entry (SGEN_PROTOCOL_CLEANUP, &entry, sizeof (SGenProtocolCleanup));
214 }
215
216 void
217 binary_protocol_empty (gpointer start, int size)
218 {
219         SGenProtocolEmpty entry = { start, size };
220         protocol_entry (SGEN_PROTOCOL_EMPTY, &entry, sizeof (SGenProtocolEmpty));
221 }
222
223 void
224 binary_protocol_thread_restart (gpointer thread)
225 {
226         SGenProtocolThreadRestart entry = { thread };
227         protocol_entry (SGEN_PROTOCOL_THREAD_RESTART, &entry, sizeof (SGenProtocolThreadRestart));
228
229 }
230
231 void
232 binary_protocol_thread_register (gpointer thread)
233 {
234         SGenProtocolThreadRegister entry = { thread };
235         protocol_entry (SGEN_PROTOCOL_THREAD_REGISTER, &entry, sizeof (SGenProtocolThreadRegister));
236
237 }
238
239 void
240 binary_protocol_thread_unregister (gpointer thread)
241 {
242         SGenProtocolThreadUnregister entry = { thread };
243         protocol_entry (SGEN_PROTOCOL_THREAD_UNREGISTER, &entry, sizeof (SGenProtocolThreadUnregister));
244
245 }
246
247 void
248 binary_protocol_missing_remset (gpointer obj, gpointer obj_vtable, int offset, gpointer value, gpointer value_vtable, int value_pinned)
249 {
250         SGenProtocolMissingRemset entry = { obj, obj_vtable, offset, value, value_vtable, value_pinned };
251         protocol_entry (SGEN_PROTOCOL_MISSING_REMSET, &entry, sizeof (SGenProtocolMissingRemset));
252
253 }
254
255 #endif