}
/*
- * These macros should be used when writing an event to a log buffer. They take
- * care of a bunch of stuff that can be repetitive and error-prone, such as
- * acquiring/releasing the buffer lock, incrementing the event counter,
- * expanding the log buffer, processing requests, etc. They also create a scope
- * so that it's harder to leak the LogBuffer pointer, which can be problematic
- * as the pointer is unstable when the buffer lock isn't acquired.
+ * These macros should be used when writing an event to a log buffer. They
+ * take care of a bunch of stuff that can be repetitive and error-prone, such
+ * as attaching the current thread, acquiring/releasing the buffer lock,
+ * incrementing the event counter, expanding the log buffer, etc. They also
+ * create a scope so that it's harder to leak the LogBuffer pointer, which can
+ * be problematic as the pointer is unstable when the buffer lock isn't
+ * acquired.
+ *
+ * If the calling thread is already attached, these macros will not alter its
+ * attach mode (i.e. whether it's added to the LLS). If the thread is not
+ * attached, init_thread () will be called with add_to_lls = TRUE.
*/
#define ENTER_LOG(COUNTER, BUFFER, SIZE) \
do { \
- MonoProfilerThread *thread__ = PROF_TLS_GET (); \
+ MonoProfilerThread *thread__ = get_thread (); \
if (thread__->attached) \
buffer_lock (); \
g_assert (!thread__->busy && "Why are we trying to write a new event while already writing one?"); \
char *name;
};
+static MonoProfiler *log_profiler;
+
struct _MonoProfiler {
FILE* file;
#if defined (HAVE_SYS_ZLIB)
uint64_t time;
} MethodInfo;
+// Do not use these TLS macros directly unless you know what you're doing.
+
#ifdef HOST_WIN32
#define PROF_TLS_SET(VAL) (TlsSetValue (profiler_tls, (VAL)))
PROF_TLS_SET (NULL);
}
+static MonoProfilerThread *
+get_thread (void)
+{
+ return init_thread (log_profiler, TRUE);
+}
+
// Only valid if init_thread () was called with add_to_lls = FALSE.
static LogBuffer *
ensure_logbuf_unsafe (MonoProfilerThread *thread, int bytes)
static void
register_method_local (MonoMethod *method, MonoJitInfo *ji)
{
- MonoProfilerThread *thread = PROF_TLS_GET ();
+ MonoProfilerThread *thread = get_thread ();
if (!mono_conc_hashtable_lookup (thread->profiler->method_table, method)) {
MethodInfo *info = (MethodInfo *) g_malloc (sizeof (MethodInfo));
static void
gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
{
- init_thread (prof, TRUE);
-
int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces) ? TYPE_ALLOC_BT : 0;
FrameData data;
uintptr_t len = mono_object_get_size (obj);
{
process_method_enter_coverage (prof, method);
- if (!only_coverage && PROF_TLS_GET ()->call_depth++ <= max_call_depth) {
+ if (!only_coverage && get_thread ()->call_depth++ <= max_call_depth) {
ENTER_LOG (&method_entries_ctr, logbuffer,
EVENT_SIZE /* event */ +
LEB128_SIZE /* method */
static void
method_leave (MonoProfiler *prof, MonoMethod *method)
{
- if (!only_coverage && --PROF_TLS_GET ()->call_depth <= max_call_depth) {
+ if (!only_coverage && --get_thread ()->call_depth <= max_call_depth) {
ENTER_LOG (&method_exits_ctr, logbuffer,
EVENT_SIZE /* event */ +
LEB128_SIZE /* method */
static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method)
{
- if (!only_coverage && !nocalls && --PROF_TLS_GET ()->call_depth <= max_call_depth) {
+ if (!only_coverage && !nocalls && --get_thread ()->call_depth <= max_call_depth) {
ENTER_LOG (&method_exception_exits_ctr, logbuffer,
EVENT_SIZE /* event */ +
LEB128_SIZE /* method */
static void
thread_start (MonoProfiler *prof, uintptr_t tid)
{
- init_thread (prof, TRUE);
-
if (ENABLED (PROFLOG_THREAD_EVENTS)) {
ENTER_LOG (&thread_starts_ctr, logbuffer,
EVENT_SIZE /* event */ +
EXIT_LOG_EXPLICIT (NO_SEND);
}
- MonoProfilerThread *thread = PROF_TLS_GET ();
+ MonoProfilerThread *thread = get_thread ();
thread->ended = TRUE;
remove_thread (thread);
g_ptr_array_free (entry->methods, TRUE);
if (wrote_methods) {
- dump_buffer_threadless (prof, PROF_TLS_GET ()->buffer);
- init_buffer_state (PROF_TLS_GET ());
+ MonoProfilerThread *thread = PROF_TLS_GET ();
+
+ dump_buffer_threadless (prof, thread->buffer);
+ init_buffer_state (thread);
}
no_methods:
start_dumper_thread (profiler);
}
-static MonoProfiler*
+static void
create_profiler (const char *args, const char *filename, GPtrArray *filters)
{
- MonoProfiler *prof;
char *nf;
int force_delete = 0;
- prof = (MonoProfiler *) g_calloc (1, sizeof (MonoProfiler));
- prof->args = pstrdup (args);
- prof->command_port = command_port;
+ log_profiler = (MonoProfiler *) g_calloc (1, sizeof (MonoProfiler));
+ log_profiler->args = pstrdup (args);
+ log_profiler->command_port = command_port;
+
if (filename && *filename == '-') {
force_delete = 1;
filename++;
}
}
if (*nf == '|') {
- prof->file = popen (nf + 1, "w");
- prof->pipe_output = 1;
+ log_profiler->file = popen (nf + 1, "w");
+ log_profiler->pipe_output = 1;
} else if (*nf == '#') {
int fd = strtol (nf + 1, NULL, 10);
- prof->file = fdopen (fd, "a");
+ log_profiler->file = fdopen (fd, "a");
} else {
if (force_delete)
unlink (nf);
- prof->file = fopen (nf, "wb");
+ log_profiler->file = fopen (nf, "wb");
}
- if (!prof->file) {
+ if (!log_profiler->file) {
fprintf (stderr, "Cannot create profiler output: %s\n", nf);
exit (1);
}
#if defined (HAVE_SYS_ZLIB)
if (use_zip)
- prof->gzfile = gzdopen (fileno (prof->file), "wb");
+ log_profiler->gzfile = gzdopen (fileno (log_profiler->file), "wb");
#endif
/*
g_assert (SAMPLE_SLOT_SIZE (MAX_FRAMES) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (SAMPLE_BLOCK_SIZE));
// FIXME: We should free this stuff too.
- mono_lock_free_allocator_init_size_class (&prof->sample_size_class, SAMPLE_SLOT_SIZE (num_frames), SAMPLE_BLOCK_SIZE);
- mono_lock_free_allocator_init_allocator (&prof->sample_allocator, &prof->sample_size_class, MONO_MEM_ACCOUNT_PROFILER);
+ mono_lock_free_allocator_init_size_class (&log_profiler->sample_size_class, SAMPLE_SLOT_SIZE (num_frames), SAMPLE_BLOCK_SIZE);
+ mono_lock_free_allocator_init_allocator (&log_profiler->sample_allocator, &log_profiler->sample_size_class, MONO_MEM_ACCOUNT_PROFILER);
- mono_lock_free_queue_init (&prof->sample_reuse_queue);
+ mono_lock_free_queue_init (&log_profiler->sample_reuse_queue);
g_assert (sizeof (WriterQueueEntry) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (WRITER_ENTRY_BLOCK_SIZE));
// FIXME: We should free this stuff too.
- mono_lock_free_allocator_init_size_class (&prof->writer_entry_size_class, sizeof (WriterQueueEntry), WRITER_ENTRY_BLOCK_SIZE);
- mono_lock_free_allocator_init_allocator (&prof->writer_entry_allocator, &prof->writer_entry_size_class, MONO_MEM_ACCOUNT_PROFILER);
+ mono_lock_free_allocator_init_size_class (&log_profiler->writer_entry_size_class, sizeof (WriterQueueEntry), WRITER_ENTRY_BLOCK_SIZE);
+ mono_lock_free_allocator_init_allocator (&log_profiler->writer_entry_allocator, &log_profiler->writer_entry_size_class, MONO_MEM_ACCOUNT_PROFILER);
- mono_lock_free_queue_init (&prof->writer_queue);
- mono_os_sem_init (&prof->writer_queue_sem, 0);
+ mono_lock_free_queue_init (&log_profiler->writer_queue);
+ mono_os_sem_init (&log_profiler->writer_queue_sem, 0);
- mono_lock_free_queue_init (&prof->dumper_queue);
- mono_os_sem_init (&prof->dumper_queue_sem, 0);
+ mono_lock_free_queue_init (&log_profiler->dumper_queue);
+ mono_os_sem_init (&log_profiler->dumper_queue_sem, 0);
- mono_os_mutex_init (&prof->method_table_mutex);
- prof->method_table = mono_conc_hashtable_new (NULL, NULL);
+ mono_os_mutex_init (&log_profiler->method_table_mutex);
+ log_profiler->method_table = mono_conc_hashtable_new (NULL, NULL);
if (do_coverage)
- coverage_init (prof);
- prof->coverage_filters = filters;
+ coverage_init (log_profiler);
+ log_profiler->coverage_filters = filters;
- prof->startup_time = current_time ();
- return prof;
+ log_profiler->startup_time = current_time ();
}
/*
mono_profiler_startup (const char *desc)
{
GPtrArray *filters = NULL;
- MonoProfiler *prof;
proflog_parse_args (&config, desc [3] == ':' ? desc + 4 : "");
PROF_TLS_INIT ();
- prof = create_profiler (desc, config.output_filename, filters);
- if (!prof) {
- PROF_TLS_FREE ();
- return;
- }
+ create_profiler (desc, config.output_filename, filters);
mono_lls_init (&profiler_thread_list, NULL);
- init_thread (prof, TRUE);
-
//This two events are required for the profiler to work
int events = MONO_PROFILE_THREADS | MONO_PROFILE_GC;
//Required callbacks
- mono_profiler_install (prof, log_shutdown);
+ mono_profiler_install (log_profiler, log_shutdown);
mono_profiler_install_runtime_initialized (runtime_initialized);
mono_profiler_install_gc (gc_event, gc_resize);