]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Upgrade minitrace to latest master (#387)
authorAnders Björklund <anders.f.bjorklund@gmail.com>
Mon, 22 Apr 2019 15:19:27 +0000 (17:19 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 22 Apr 2019 15:19:27 +0000 (17:19 +0200)
src/minitrace.c
src/minitrace.h

index cbbd28ca334020a0b794feee76784921248baa06..df1a3ac3cdce9ace964454a5403b564b422c538f 100644 (file)
@@ -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];
index 697bf92aba9c67c585e50f25f5240c1acf37b94a..2047eedb78c1596e82a75bba756fc08091f2db69 100644 (file)
@@ -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 <inttypes.h>
 
 // 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