2 * sgen-protocol.c: Binary protocol of internal activity, to aid
5 * Copyright 2001-2003 Ximian, Inc
6 * Copyright 2003-2010 Novell, Inc.
7 * Copyright (C) 2012 Xamarin Inc
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;
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.
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.
26 #include "sgen-conf.h"
28 #include "sgen-protocol.h"
29 #include "sgen-memory-governor.h"
30 #include "sgen-thread-pool.h"
31 #include "sgen-client.h"
32 #include "mono/utils/mono-membar.h"
41 /* FIXME Implement binary protocol IO on systems that don't have unistd */
43 /* If valid, dump binary protocol to this file */
44 static int binary_protocol_file = -1;
46 /* We set this to -1 to indicate an exclusive lock */
47 static volatile int binary_protocol_use_count = 0;
49 #define BINARY_PROTOCOL_BUFFER_SIZE (65536 - 2 * 8)
51 typedef struct _BinaryProtocolBuffer BinaryProtocolBuffer;
52 struct _BinaryProtocolBuffer {
53 BinaryProtocolBuffer * volatile next;
55 unsigned char buffer [BINARY_PROTOCOL_BUFFER_SIZE];
58 static BinaryProtocolBuffer * volatile binary_protocol_buffers = NULL;
60 static char* filename_or_prefix = NULL;
61 static int current_file_index = 0;
62 static long long current_file_size = 0;
63 static long long file_size_limit;
66 filename_for_index (int index)
70 SGEN_ASSERT (0, file_size_limit > 0, "Indexed binary protocol filename must only be used with file size limit");
72 filename = (char *)sgen_alloc_internal_dynamic (strlen (filename_or_prefix) + 32, INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
73 sprintf (filename, "%s.%d", filename_or_prefix, index);
79 free_filename (char *filename)
81 SGEN_ASSERT (0, file_size_limit > 0, "Indexed binary protocol filename must only be used with file size limit");
83 sgen_free_internal_dynamic (filename, strlen (filename_or_prefix) + 32, INTERNAL_MEM_BINARY_PROTOCOL);
87 binary_protocol_open_file (void)
91 if (file_size_limit > 0)
92 filename = filename_for_index (current_file_index);
94 filename = filename_or_prefix;
97 binary_protocol_file = open (filename, O_CREAT|O_WRONLY|O_TRUNC, 0644);
98 if (binary_protocol_file == -1 && errno != EINTR)
100 } while (binary_protocol_file == -1);
102 if (file_size_limit > 0)
103 free_filename (filename);
108 binary_protocol_init (const char *filename, long long limit)
111 filename_or_prefix = (char *)sgen_alloc_internal_dynamic (strlen (filename) + 1, INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
112 strcpy (filename_or_prefix, filename);
114 file_size_limit = limit;
116 binary_protocol_open_file ();
121 binary_protocol_is_enabled (void)
124 return binary_protocol_file != -1;
133 close_binary_protocol_file (void)
135 while (close (binary_protocol_file) == -1 && errno == EINTR)
137 binary_protocol_file = -1;
141 try_lock_exclusive (void)
144 if (binary_protocol_use_count)
146 } while (InterlockedCompareExchange (&binary_protocol_use_count, -1, 0) != 0);
147 mono_memory_barrier ();
152 unlock_exclusive (void)
154 mono_memory_barrier ();
155 SGEN_ASSERT (0, binary_protocol_use_count == -1, "Exclusively locked count must be -1");
156 if (InterlockedCompareExchange (&binary_protocol_use_count, 0, -1) != -1)
157 SGEN_ASSERT (0, FALSE, "Somebody messed with the exclusive lock");
161 lock_recursive (void)
166 old_count = binary_protocol_use_count;
168 /* Exclusively locked - retry */
169 /* FIXME: short back-off */
172 } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count + 1, old_count) != old_count);
173 mono_memory_barrier ();
177 unlock_recursive (void)
180 mono_memory_barrier ();
182 old_count = binary_protocol_use_count;
183 SGEN_ASSERT (0, old_count > 0, "Locked use count must be at least 1");
184 } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count - 1, old_count) != old_count);
188 binary_protocol_flush_buffer (BinaryProtocolBuffer *buffer)
191 size_t to_write = buffer->index;
193 g_assert (buffer->index > 0);
195 while (written < to_write) {
196 ret = write (binary_protocol_file, buffer->buffer + written, to_write - written);
199 else if (errno == EINTR)
202 close_binary_protocol_file ();
205 current_file_size += buffer->index;
207 sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
211 binary_protocol_check_file_overflow (void)
213 if (file_size_limit <= 0 || current_file_size < file_size_limit)
216 close_binary_protocol_file ();
218 if (current_file_index > 0) {
219 char *filename = filename_for_index (current_file_index - 1);
221 free_filename (filename);
224 ++current_file_index;
225 current_file_size = 0;
227 binary_protocol_open_file ();
232 * Flushing buffers takes an exclusive lock, so it must only be done when the world is
233 * stopped, otherwise we might end up with a deadlock because a stopped thread owns the
236 * The protocol entries that do flush have `FLUSH()` in their definition.
239 binary_protocol_flush_buffers (gboolean force)
242 int num_buffers = 0, i;
243 BinaryProtocolBuffer *buf;
244 BinaryProtocolBuffer **bufs;
246 if (binary_protocol_file == -1)
249 if (!force && !try_lock_exclusive ())
252 for (buf = binary_protocol_buffers; buf != NULL; buf = buf->next)
254 bufs = (BinaryProtocolBuffer **)sgen_alloc_internal_dynamic (num_buffers * sizeof (BinaryProtocolBuffer*), INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
255 for (buf = binary_protocol_buffers, i = 0; buf != NULL; buf = buf->next, i++)
257 SGEN_ASSERT (0, i == num_buffers, "Binary protocol buffer count error");
259 binary_protocol_buffers = NULL;
261 for (i = num_buffers - 1; i >= 0; --i) {
262 binary_protocol_flush_buffer (bufs [i]);
263 binary_protocol_check_file_overflow ();
266 sgen_free_internal_dynamic (buf, num_buffers * sizeof (BinaryProtocolBuffer*), INTERNAL_MEM_BINARY_PROTOCOL);
274 static BinaryProtocolBuffer*
275 binary_protocol_get_buffer (int length)
277 BinaryProtocolBuffer *buffer, *new_buffer;
279 buffer = binary_protocol_buffers;
280 if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
283 new_buffer = (BinaryProtocolBuffer *)sgen_alloc_os_memory (sizeof (BinaryProtocolBuffer), (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "debugging memory");
284 new_buffer->next = buffer;
285 new_buffer->index = 0;
287 if (InterlockedCompareExchangePointer ((void**)&binary_protocol_buffers, new_buffer, buffer) != buffer) {
288 sgen_free_os_memory (new_buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
297 protocol_entry (unsigned char type, gpointer data, int size)
301 BinaryProtocolBuffer *buffer;
303 if (binary_protocol_file == -1)
306 if (sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()))
312 buffer = binary_protocol_get_buffer (size + 1);
314 index = buffer->index;
315 if (index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
318 if (InterlockedCompareExchange (&buffer->index, index + 1 + size, index) != index)
319 goto retry_same_buffer;
321 /* FIXME: if we're interrupted at this point, we have a buffer
322 entry that contains random data. */
324 buffer->buffer [index++] = type;
325 memcpy (buffer->buffer + index, data, size);
328 g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
335 #define TYPE_LONGLONG long long
336 #define TYPE_SIZE size_t
337 #define TYPE_POINTER gpointer
338 #define TYPE_BOOL gboolean
340 #define BEGIN_PROTOCOL_ENTRY0(method) \
341 void method (void) { \
342 int __type = PROTOCOL_ID(method); \
343 gpointer __data = NULL; \
345 CLIENT_PROTOCOL_NAME (method) ();
346 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
347 void method (t1 f1) { \
348 PROTOCOL_STRUCT(method) __entry = { f1 }; \
349 int __type = PROTOCOL_ID(method); \
350 gpointer __data = &__entry; \
351 int __size = sizeof (PROTOCOL_STRUCT(method)); \
352 CLIENT_PROTOCOL_NAME (method) (f1);
353 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
354 void method (t1 f1, t2 f2) { \
355 PROTOCOL_STRUCT(method) __entry = { f1, f2 }; \
356 int __type = PROTOCOL_ID(method); \
357 gpointer __data = &__entry; \
358 int __size = sizeof (PROTOCOL_STRUCT(method)); \
359 CLIENT_PROTOCOL_NAME (method) (f1, f2);
360 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
361 void method (t1 f1, t2 f2, t3 f3) { \
362 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3 }; \
363 int __type = PROTOCOL_ID(method); \
364 gpointer __data = &__entry; \
365 int __size = sizeof (PROTOCOL_STRUCT(method)); \
366 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3);
367 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
368 void method (t1 f1, t2 f2, t3 f3, t4 f4) { \
369 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4 }; \
370 int __type = PROTOCOL_ID(method); \
371 gpointer __data = &__entry; \
372 int __size = sizeof (PROTOCOL_STRUCT(method)); \
373 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4);
374 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
375 void method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5) { \
376 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4, f5 }; \
377 int __type = PROTOCOL_ID(method); \
378 gpointer __data = &__entry; \
379 int __size = sizeof (PROTOCOL_STRUCT(method)); \
380 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4, f5);
381 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
382 void method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5, t6 f6) { \
383 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4, f5, f6 }; \
384 int __type = PROTOCOL_ID(method); \
385 gpointer __data = &__entry; \
386 int __size = sizeof (PROTOCOL_STRUCT(method)); \
387 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4, f5, f6);
389 #define DEFAULT_PRINT()
390 #define CUSTOM_PRINT(_)
392 #define IS_ALWAYS_MATCH(_)
393 #define MATCH_INDEX(_)
394 #define IS_VTABLE_MATCH(_)
396 #define END_PROTOCOL_ENTRY \
397 protocol_entry (__type, __data, __size); \
400 #define END_PROTOCOL_ENTRY_FLUSH \
401 protocol_entry (__type, __data, __size); \
402 binary_protocol_flush_buffers (FALSE); \
405 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
406 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
407 BEGIN_PROTOCOL_ENTRY0 (method)
408 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
409 BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
410 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
411 BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
412 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
413 BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
414 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
415 BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
416 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
417 BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
418 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
419 BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
421 #define END_PROTOCOL_ENTRY_HEAVY \
424 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method)
425 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1)
426 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2)
427 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3)
428 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4)
429 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
430 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
432 #define END_PROTOCOL_ENTRY_HEAVY
435 #include "sgen-protocol-def.h"
443 #endif /* HAVE_SGEN_GC */