Bug 1471588 - Use format strings for TRACE_COMMENT. r?padenot draft
authorAndreas Pehrson <pehrsons@mozilla.com>
Wed, 27 Jun 2018 15:35:14 +0200
changeset 819611 b8940d9553317c0bba8c8b4807aa2d9b2565b05e
parent 819610 33eaca3a491d7874aa500c046adb750a9fca8ba9
child 819612 7d2b1dccee2bfcae86a05002761aa2a5e4dfa187
push id116598
push userbmo:apehrson@mozilla.com
push dateWed, 18 Jul 2018 08:46:17 +0000
reviewerspadenot
bugs1471588
milestone63.0a1
Bug 1471588 - Use format strings for TRACE_COMMENT. r?padenot MozReview-Commit-ID: 8MJs3YnfdqG
dom/media/Tracing.cpp
dom/media/Tracing.h
--- a/dom/media/Tracing.cpp
+++ b/dom/media/Tracing.cpp
@@ -2,19 +2,17 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "Tracing.h"
 
 #include <inttypes.h>
-#include <cstdio>
 
-#include "AsyncLogger.h"
 #include "mozilla/TimeStamp.h"
 
 using namespace mozilla;
 
 uint64_t
 AutoTracer::NowInUs()
 {
   static TimeStamp base = TimeStamp::Now();
--- a/dom/media/Tracing.h
+++ b/dom/media/Tracing.h
@@ -2,21 +2,24 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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/. */
 
 #ifndef TRACING_H
 #define TRACING_H
 
+#include <algorithm>
 #include <cstdint>
+#include <cstdio>
 
 #include "AsyncLogger.h"
 
-#include <mozilla/Attributes.h>
+#include "mozilla/Attributes.h"
+#include "mozilla/UniquePtr.h"
 
 #if defined(_WIN32)
 #include <process.h>
 #define getpid() _getpid()
 #else
 #include <unistd.h>
 #endif
 
@@ -42,50 +45,78 @@
   #define TRACE_AUDIO_CALLBACK()                                               \
     AutoTracer trace(gMSGTraceLogger, FUNCTION_SIGNATURE, getpid(), 0);
   #define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate)                    \
     AutoTracer budget(gMSGTraceLogger, "Real-time budget", getpid(), 1,        \
                       AutoTracer::EventType::BUDGET, aFrames, aSampleRate);
   #define TRACE()                                                              \
     AutoTracer trace(gMSGTraceLogger, FUNCTION_SIGNATURE, getpid(),            \
                      std::hash<std::thread::id>{}(std::this_thread::get_id()));
-  #define TRACE_COMMENT(aComment)                                              \
+  #define TRACE_COMMENT(aFmt, ...)                                             \
     AutoTracer trace(gMSGTraceLogger, FUNCTION_SIGNATURE, getpid(),            \
                      std::hash<std::thread::id>{}(std::this_thread::get_id()), \
                      AutoTracer::EventType::DURATION,                          \
-                     aComment);
+                     aFmt, ##__VA_ARGS__);
 #else
   #define TRACE_AUDIO_CALLBACK()
   #define TRACE_AUDIO_CALLBACK_BUDGET(aFrames, aSampleRate)
   #define TRACE()
-  #define TRACE_COMMENT(aComment)
+  #define TRACE_COMMENT(aFmt, ...)
 #endif
 
 class MOZ_RAII AutoTracer
 {
 public:
+  static const int32_t BUFFER_SIZE = mozilla::AsyncLogger::MAX_MESSAGE_LENGTH / 2;
+
   enum class EventType
   {
     DURATION,
     BUDGET
   };
 
   AutoTracer(mozilla::AsyncLogger& aLogger,
              const char* aLocation,
              uint64_t aPID,
              uint64_t aTID,
              EventType aEventType = EventType::DURATION,
              const char* aComment = nullptr);
+
+  template<typename... Args>
+  AutoTracer(mozilla::AsyncLogger& aLogger,
+             const char* aLocation,
+             uint64_t aPID,
+             uint64_t aTID,
+             EventType aEventType,
+             const char* aFormat,
+             Args... aArgs)
+    : mLogger(aLogger)
+    , mLocation(aLocation)
+    , mComment(mBuffer)
+    , mEventType(aEventType)
+    , mPID(aPID)
+    , mTID(aTID)
+  {
+    MOZ_ASSERT(aEventType == EventType::DURATION);
+    if (aLogger.Enabled()) {
+      int32_t size = snprintf(mBuffer, BUFFER_SIZE, aFormat, aArgs...);
+      size = std::min(size, BUFFER_SIZE - 1);
+      mBuffer[size] = 0;
+      PrintEvent(aLocation, "perf", mComment, TracingPhase::BEGIN, NowInUs(), aPID, aTID);
+    }
+  }
+
   AutoTracer(mozilla::AsyncLogger& aLogger,
              const char* aLocation,
              uint64_t aPID,
              uint64_t aTID,
              EventType aEventType,
              uint64_t aFrames,
              uint64_t aSampleRate);
+
   ~AutoTracer();
 private:
   uint64_t NowInUs();
 
   enum class TracingPhase
   {
     BEGIN,
     END,
@@ -118,16 +149,18 @@ private:
   // instance of this class traces.
   mozilla::AsyncLogger& mLogger;
   // The location for this trace point, arbitrary string literal, often the
   // name of the calling function, with a static lifetime.
   const char* mLocation;
   // A comment for this trace point, abitrary string literal with a static
   // lifetime.
   const char* mComment;
+  // A buffer used to hold string-formatted traces.
+  char mBuffer[BUFFER_SIZE];
   // The event type, for now either a budget or a duration.
   const EventType mEventType;
   // The process ID of the calling process. Traces are grouped by PID in the
   // vizualizer.
   const uint64_t mPID;
   // The thread ID of the calling thread, will be displayed in a separate
   // section in the trace visualizer.
   const uint64_t mTID;