#endif
-void mtr_init(const char *json_file) {
+void mtr_init_from_stream(void *stream) {
#ifndef MTR_ENABLED
return;
#endif
buffer = (raw_event_t *)malloc(INTERNAL_MINITRACE_BUFFER_SIZE * sizeof(raw_event_t));
is_tracing = 1;
count = 0;
- f = fopen(json_file, "wb");
+ f = (FILE *)stream;
const char *header = "{\"traceEvents\":[\n";
fwrite(header, 1, strlen(header), f);
time_offset = (uint64_t)(mtr_time_s() * 1000000);
pthread_mutex_init(&mutex, 0);
}
+void mtr_init(const char *json_file) {
+#ifndef MTR_ENABLED
+ return;
+#endif
+ mtr_init_from_stream(fopen(json_file, "wb"));
+}
+
void mtr_shutdown() {
int i;
#ifndef MTR_ENABLED
const char *cat = raw->cat;
#ifdef _WIN32
// On Windows, we often end up with backslashes in category.
+ char temp[256];
{
- char temp[256];
int len = (int)strlen(cat);
int i;
if (len > 255) len = 255;
cur_process_id = get_cur_process_id();
}
-#if 0 && _WIN32 // TODO: This needs testing
- int bufPos = InterlockedIncrement(&count);
- raw_event_t *ev = &buffer[count - 1];
+#if 0 && _WIN32 // This should work, feel free to enable if you're adventurous and need performance.
+ int bufPos = InterlockedExchangeAdd((LONG volatile *)&count, 1);
+ raw_event_t *ev = &buffer[bufPos];
#else
pthread_mutex_lock(&mutex);
raw_event_t *ev = &buffer[count];
}
ev->tid = cur_thread_id;
ev->pid = cur_process_id;
+ ev->arg_type = MTR_ARG_TYPE_NONE;
}
void internal_mtr_raw_event_arg(const char *category, const char *name, char ph, void *id, mtr_arg_type arg_type, const char *arg_name, void *arg_value) {
}
double ts = mtr_time_s();
-#if 0 && _WIN32 // TODO: This needs testing
- int bufPos = InterlockedIncrement(&count);
- raw_event_t *ev = &buffer[count - 1];
+#if 0 && _WIN32 // This should work, feel free to enable if you're adventurous and need performance.
+ int bufPos = InterlockedExchangeAdd((LONG volatile *)&count, 1);
+ raw_event_t *ev = &buffer[bufPos];
#else
pthread_mutex_lock(&mutex);
raw_event_t *ev = &buffer[count];
// More:
// http://www.altdevblogaday.com/2012/08/21/using-chrometracing-to-view-your-inline-profiling-data/
+#ifndef MINITRACE_H
+#define MINITRACE_H
+
#include <inttypes.h>
// If MTR_ENABLED is not defined, Minitrace does nothing and has near zero overhead.
#endif
// Initializes Minitrace. Must be called very early during startup of your executable,
-// before any MTR_ statements..
+// before any MTR_ statements.
void mtr_init(const char *json_file);
+// Same as above, but allows passing in a custom stream (FILE *), as returned by
+// fopen(). It should be opened for writing, preferably in binary mode to avoid
+// processing of line endings (i.e. the "wb" mode).
+void mtr_init_from_stream(void *stream);
// Shuts down minitrace cleanly, flushing the trace buffer.
void mtr_shutdown(void);
#endif
#endif
+
+#endif