#include "utils/mono-mmap.h"
#include "utils/mono-threads.h"
-/* If not null, dump binary protocol to this file */
-static FILE *binary_protocol_file = NULL;
+#include <errno.h>
+#ifdef HAVE_UNISTD_H
+#include <fcntl.h>
+#endif
+
+/* FIXME Implement binary protocol IO on systems that don't have unistd */
+#ifdef HAVE_UNISTD_H
+/* If valid, dump binary protocol to this file */
+static int binary_protocol_file = -1;
/* We set this to -1 to indicate an exclusive lock */
static volatile int binary_protocol_use_count = 0;
static BinaryProtocolBuffer * volatile binary_protocol_buffers = NULL;
+static char* filename_or_prefix = NULL;
+static int current_file_index = 0;
+static long long current_file_size = 0;
+static long long file_size_limit;
+
+static char*
+filename_for_index (int index)
+{
+ char *filename;
+
+ SGEN_ASSERT (0, file_size_limit > 0, "Indexed binary protocol filename must only be used with file size limit");
+
+ filename = sgen_alloc_internal_dynamic (strlen (filename_or_prefix) + 32, INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
+ sprintf (filename, "%s.%d", filename_or_prefix, index);
+
+ return filename;
+}
+
+static void
+free_filename (char *filename)
+{
+ SGEN_ASSERT (0, file_size_limit > 0, "Indexed binary protocol filename must only be used with file size limit");
+
+ sgen_free_internal_dynamic (filename, strlen (filename_or_prefix) + 32, INTERNAL_MEM_BINARY_PROTOCOL);
+}
+
+static void
+binary_protocol_open_file (void)
+{
+ char *filename;
+
+ if (file_size_limit > 0)
+ filename = filename_for_index (current_file_index);
+ else
+ filename = filename_or_prefix;
+
+ do {
+ binary_protocol_file = open (filename, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+ if (binary_protocol_file == -1 && errno != EINTR)
+ break; /* Failed */
+ } while (binary_protocol_file == -1);
+
+ if (file_size_limit > 0)
+ free_filename (filename);
+}
+#endif
+
void
-binary_protocol_init (const char *filename)
+binary_protocol_init (const char *filename, long long limit)
{
- binary_protocol_file = fopen (filename, "w");
+#ifdef HAVE_UNISTD_H
+ filename_or_prefix = sgen_alloc_internal_dynamic (strlen (filename) + 1, INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
+ strcpy (filename_or_prefix, filename);
+
+ file_size_limit = limit;
+
+ binary_protocol_open_file ();
+#endif
}
gboolean
binary_protocol_is_enabled (void)
{
- return binary_protocol_file != NULL;
+#ifdef HAVE_UNISTD_H
+ return binary_protocol_file != -1;
+#else
+ return FALSE;
+#endif
+}
+
+#ifdef HAVE_UNISTD_H
+
+static void
+close_binary_protocol_file (void)
+{
+ while (close (binary_protocol_file) == -1 && errno == EINTR)
+ ;
+ binary_protocol_file = -1;
}
static gboolean
static void
binary_protocol_flush_buffer (BinaryProtocolBuffer *buffer)
{
+ ssize_t ret;
+ size_t to_write = buffer->index;
+ size_t written = 0;
g_assert (buffer->index > 0);
- fwrite (buffer->buffer, 1, buffer->index, binary_protocol_file);
+
+ while (written < to_write) {
+ ret = write (binary_protocol_file, buffer->buffer + written, to_write - written);
+ if (ret >= 0)
+ written += ret;
+ else if (errno == EINTR)
+ continue;
+ else
+ close_binary_protocol_file ();
+ }
+
+ current_file_size += buffer->index;
sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
}
+static void
+binary_protocol_check_file_overflow (void)
+{
+ if (file_size_limit <= 0 || current_file_size < file_size_limit)
+ return;
+
+ close_binary_protocol_file ();
+
+ if (current_file_index > 0) {
+ char *filename = filename_for_index (current_file_index - 1);
+ unlink (filename);
+ free_filename (filename);
+ }
+
+ ++current_file_index;
+ current_file_size = 0;
+
+ binary_protocol_open_file ();
+}
+#endif
+
void
binary_protocol_flush_buffers (gboolean force)
{
+#ifdef HAVE_UNISTD_H
int num_buffers = 0, i;
BinaryProtocolBuffer *buf;
BinaryProtocolBuffer **bufs;
- if (!binary_protocol_file)
+ if (binary_protocol_file == -1)
return;
if (!force && !try_lock_exclusive ())
binary_protocol_buffers = NULL;
- for (i = num_buffers - 1; i >= 0; --i)
+ for (i = num_buffers - 1; i >= 0; --i) {
binary_protocol_flush_buffer (bufs [i]);
+ binary_protocol_check_file_overflow ();
+ }
sgen_free_internal_dynamic (buf, num_buffers * sizeof (BinaryProtocolBuffer*), INTERNAL_MEM_BINARY_PROTOCOL);
if (!force)
unlock_exclusive ();
-
- fflush (binary_protocol_file);
+#endif
}
+#ifdef HAVE_UNISTD_H
static BinaryProtocolBuffer*
binary_protocol_get_buffer (int length)
{
BinaryProtocolBuffer *buffer, *new_buffer;
-
retry:
buffer = binary_protocol_buffers;
if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
return new_buffer;
}
-
+#endif
static void
protocol_entry (unsigned char type, gpointer data, int size)
{
+#ifdef HAVE_UNISTD_H
int index;
BinaryProtocolBuffer *buffer;
- if (!binary_protocol_file)
+ if (binary_protocol_file == -1)
return;
if (sgen_is_worker_thread (mono_native_thread_id_get ()))
g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
unlock_recursive ();
+#endif
}
void
void
binary_protocol_collection_begin (int index, int generation)
{
- SGenProtocolCollection entry = { index, generation };
+ SGenProtocolCollectionBegin entry = { index, generation };
binary_protocol_flush_buffers (FALSE);
- protocol_entry (SGEN_PROTOCOL_COLLECTION_BEGIN, &entry, sizeof (SGenProtocolCollection));
+ protocol_entry (SGEN_PROTOCOL_COLLECTION_BEGIN, &entry, sizeof (SGenProtocolCollectionBegin));
}
void
-binary_protocol_collection_end (int index, int generation)
+binary_protocol_collection_end (int index, int generation, long long num_objects_scanned, long long num_unique_objects_scanned)
{
- SGenProtocolCollection entry = { index, generation };
+ SGenProtocolCollectionEnd entry = { index, generation, num_objects_scanned, num_unique_objects_scanned };
binary_protocol_flush_buffers (FALSE);
- protocol_entry (SGEN_PROTOCOL_COLLECTION_END, &entry, sizeof (SGenProtocolCollection));
+ protocol_entry (SGEN_PROTOCOL_COLLECTION_END, &entry, sizeof (SGenProtocolCollectionEnd));
}
void
protocol_entry (SGEN_PROTOCOL_COPY, &entry, sizeof (SGenProtocolCopy));
}
+void
+binary_protocol_pin_stage (gpointer addr_ptr, gpointer addr)
+{
+ SGenProtocolPinStage entry = { addr_ptr, addr };
+ protocol_entry (SGEN_PROTOCOL_PIN_STAGE, &entry, sizeof (SGenProtocolPinStage));
+}
+
void
binary_protocol_pin (gpointer obj, gpointer vtable, int size)
{
protocol_entry (SGEN_PROTOCOL_SCAN_VTYPE_BEGIN, &entry, sizeof (SGenProtocolScanVTypeBegin));
}
+void
+binary_protocol_scan_process_reference (gpointer obj, gpointer ptr, gpointer value)
+{
+ SGenProtocolScanProcessReference entry = { obj, ptr, value };
+ protocol_entry (SGEN_PROTOCOL_SCAN_PROCESS_REFERENCE, &entry, sizeof (SGenProtocolScanProcessReference));
+}
+
void
binary_protocol_wbarrier (gpointer ptr, gpointer value, gpointer value_vtable)
{
SGenProtocolDislinkProcessStaged entry = { link, obj, index };
protocol_entry (SGEN_PROTOCOL_DISLINK_PROCESS_STAGED, &entry, sizeof (SGenProtocolDislinkProcessStaged));
}
+
+void
+binary_protocol_gray_enqueue (gpointer queue, gpointer cursor, gpointer value)
+{
+ SGenProtocolGrayQueue entry = { queue, cursor, value };
+ protocol_entry (SGEN_PROTOCOL_GRAY_ENQUEUE, &entry, sizeof (SGenProtocolGrayQueue));
+}
+
+void
+binary_protocol_gray_dequeue (gpointer queue, gpointer cursor, gpointer value)
+{
+ SGenProtocolGrayQueue entry = { queue, cursor, value };
+ protocol_entry (SGEN_PROTOCOL_GRAY_DEQUEUE, &entry, sizeof (SGenProtocolGrayQueue));
+}
#endif
#endif /* HAVE_SGEN_GC */