Bug 1273855: TraceLogger - Include PID in the log names in order to support browser with e10s, r=bbouvier
authorHannes Verschore <hv1989@gmail.com>
Mon, 23 May 2016 17:19:11 +0200
changeset 323024 10a48aecf9947f3173641fffc1c390be0c28df99
parent 323023 17e20404362d916a9034c6f67895748878e599dd
child 323025 0f74961b048ebdcccd92c8a89239a5bba422dc77
push id9671
push userraliiev@mozilla.com
push dateMon, 06 Jun 2016 20:27:52 +0000
treeherdermozilla-aurora@cea65ca3d0bd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbbouvier
bugs1273855
milestone49.0a1
Bug 1273855: TraceLogger - Include PID in the log names in order to support browser with e10s, r=bbouvier
js/src/vm/TraceLoggingGraph.cpp
js/src/vm/TraceLoggingGraph.h
--- a/js/src/vm/TraceLoggingGraph.cpp
+++ b/js/src/vm/TraceLoggingGraph.cpp
@@ -1,16 +1,23 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  * vim: set ts=8 sts=4 et sw=4 tw=99:
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "vm/TraceLoggingGraph.h"
 
+#ifdef XP_WIN
+#include <process.h>
+#define getpid _getpid
+#else
+#include <unistd.h>
+#endif
+
 #include "mozilla/Endian.h"
 
 #include "jsstr.h"
 
 #include "threading/LockGuard.h"
 #include "vm/TraceLogging.h"
 
 #ifndef TRACE_LOG_DIR
@@ -23,22 +30,33 @@
 
 using mozilla::NativeEndian;
 
 TraceLoggerGraphState* traceLoggerGraphState = nullptr;
 
 bool
 TraceLoggerGraphState::init()
 {
-    out = fopen(TRACE_LOG_DIR "tl-data.json", "w");
+    pid_ = (uint32_t) getpid();
+
+    char filename[sizeof TRACE_LOG_DIR "tl-data.4294967295.json"];
+    sprintf(filename, TRACE_LOG_DIR "tl-data.%u.json", pid_);
+    out = fopen(filename, "w");
     if (!out)
         return false;
 
     fprintf(out, "[");
 
+    // Write the last tl-data.*.json file to tl-data.json.
+    // In most cases that is the wanted file.
+    if (FILE* last = fopen(TRACE_LOG_DIR "tl-data.json", "w")) {
+        fprintf(last, "\"tl-data.%u.json\"", pid_);
+        fclose(last);
+    }
+
 #ifdef DEBUG
     initialized = true;
 #endif
     return true;
 }
 
 TraceLoggerGraphState::~TraceLoggerGraphState()
 {
@@ -68,19 +86,19 @@ TraceLoggerGraphState::nextLoggerId()
     if (numLoggers > 0) {
         int written = fprintf(out, ",\n");
         if (written < 0) {
             fprintf(stderr, "TraceLogging: Error while writing.\n");
             return uint32_t(-1);
         }
     }
 
-    int written = fprintf(out, "{\"tree\":\"tl-tree.%d.tl\", \"events\":\"tl-event.%d.tl\", "
-                               "\"dict\":\"tl-dict.%d.json\", \"treeFormat\":\"64,64,31,1,32\"}",
-                          numLoggers, numLoggers, numLoggers);
+    int written = fprintf(out, "{\"tree\":\"tl-tree.%u.%d.tl\", \"events\":\"tl-event.%u.%d.tl\", "
+                               "\"dict\":\"tl-dict.%u.%d.json\", \"treeFormat\":\"64,64,31,1,32\"}",
+                          pid_, numLoggers, pid_, numLoggers, pid_, numLoggers);
     if (written < 0) {
         fprintf(stderr, "TraceLogging: Error while writing.\n");
         return uint32_t(-1);
     }
 
     return numLoggers++;
 }
 
@@ -129,36 +147,38 @@ TraceLoggerGraph::init(uint64_t startTim
     }
 
     uint32_t loggerId = traceLoggerGraphState->nextLoggerId();
     if (loggerId == uint32_t(-1)) {
         failed = true;
         return false;
     }
 
-    char dictFilename[sizeof TRACE_LOG_DIR "tl-dict.100.json"];
-    sprintf(dictFilename, TRACE_LOG_DIR "tl-dict.%d.json", loggerId);
+    uint32_t pid = traceLoggerGraphState->pid();
+
+    char dictFilename[sizeof TRACE_LOG_DIR "tl-dict.4294967295.100.json"];
+    sprintf(dictFilename, TRACE_LOG_DIR "tl-dict.%u.%d.json", pid, loggerId);
     dictFile = fopen(dictFilename, "w");
     if (!dictFile) {
         failed = true;
         return false;
     }
 
-    char treeFilename[sizeof TRACE_LOG_DIR "tl-tree.100.tl"];
-    sprintf(treeFilename, TRACE_LOG_DIR "tl-tree.%d.tl", loggerId);
+    char treeFilename[sizeof TRACE_LOG_DIR "tl-tree.4294967295.100.tl"];
+    sprintf(treeFilename, TRACE_LOG_DIR "tl-tree.%u.%d.tl", pid, loggerId);
     treeFile = fopen(treeFilename, "w+b");
     if (!treeFile) {
         fclose(dictFile);
         dictFile = nullptr;
         failed = true;
         return false;
     }
 
-    char eventFilename[sizeof TRACE_LOG_DIR "tl-event.100.tl"];
-    sprintf(eventFilename, TRACE_LOG_DIR "tl-event.%d.tl", loggerId);
+    char eventFilename[sizeof TRACE_LOG_DIR "tl-event.4294967295.100.tl"];
+    sprintf(eventFilename, TRACE_LOG_DIR "tl-event.%u.%d.tl", pid, loggerId);
     eventFile = fopen(eventFilename, "wb");
     if (!eventFile) {
         fclose(dictFile);
         fclose(treeFile);
         dictFile = nullptr;
         treeFile = nullptr;
         failed = true;
         return false;
@@ -565,8 +585,10 @@ TraceLoggerGraph::addTextId(uint32_t id,
             failed = true;
             return;
         }
     }
 
     if (!js::FileEscapedString(dictFile, text, strlen(text), '"'))
         failed = true;
 }
+
+#undef getpid
--- a/js/src/vm/TraceLoggingGraph.h
+++ b/js/src/vm/TraceLoggingGraph.h
@@ -63,40 +63,43 @@
 
 namespace js {
 void DestroyTraceLoggerGraphState();
 } // namespace js
 
 class TraceLoggerGraphState
 {
     uint32_t numLoggers;
+    uint32_t pid_;
 
     // File pointer to the "tl-data.json" file. (Explained above).
     FILE* out;
 
 #ifdef DEBUG
     bool initialized;
 #endif
 
   public:
     js::Mutex lock;
 
   public:
     TraceLoggerGraphState()
-      : numLoggers(0)
-      , out(nullptr)
+      : numLoggers(0),
+        pid_(0),
+        out(nullptr)
 #ifdef DEBUG
       , initialized(false)
 #endif
     {}
 
     bool init();
     ~TraceLoggerGraphState();
 
     uint32_t nextLoggerId();
+    uint32_t pid() { return pid_; }
 };
 
 class TraceLoggerGraph
 {
     // The layout of the tree in memory and in the log file. Readable by JS
     // using TypedArrays.
     struct TreeEntry {
         uint64_t start_;