From: Anders Björklund Date: Mon, 22 Apr 2019 15:19:27 +0000 (+0200) Subject: Upgrade minitrace to latest master (#387) X-Git-Tag: v3.7~5 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=aab2c726fa484bad3d74310b97b483f26fcd1ac1;p=thirdparty%2Fccache.git Upgrade minitrace to latest master (#387) --- diff --git a/src/minitrace.c b/src/minitrace.c index cbbd28ca3..df1a3ac3c 100644 --- a/src/minitrace.c +++ b/src/minitrace.c @@ -160,14 +160,14 @@ void mtr_register_sigint_handler() { #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); @@ -175,6 +175,13 @@ void mtr_init(const char *json_file) { 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 @@ -280,8 +287,8 @@ void mtr_flush() { 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; @@ -318,9 +325,9 @@ void internal_mtr_raw_event(const char *category, const char *name, char ph, voi 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]; @@ -342,6 +349,7 @@ void internal_mtr_raw_event(const char *category, const char *name, char ph, voi } 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) { @@ -358,9 +366,9 @@ void internal_mtr_raw_event_arg(const char *category, const char *name, char ph, } 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]; diff --git a/src/minitrace.h b/src/minitrace.h index 697bf92ab..2047eedb7 100644 --- a/src/minitrace.h +++ b/src/minitrace.h @@ -18,6 +18,9 @@ // More: // http://www.altdevblogaday.com/2012/08/21/using-chrometracing-to-view-your-inline-profiling-data/ +#ifndef MINITRACE_H +#define MINITRACE_H + #include // If MTR_ENABLED is not defined, Minitrace does nothing and has near zero overhead. @@ -34,8 +37,12 @@ extern "C" { #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); @@ -259,3 +266,5 @@ private: #endif #endif + +#endif