Index: include/llvm/Support/Timer.h =================================================================== --- include/llvm/Support/Timer.h +++ include/llvm/Support/Timer.h @@ -10,6 +10,7 @@ #ifndef LLVM_SUPPORT_TIMER_H #define LLVM_SUPPORT_TIMER_H +#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/DataTypes.h" #include @@ -194,6 +195,10 @@ public: explicit TimerGroup(StringRef Name, StringRef Description); + + explicit TimerGroup(StringRef Name, StringRef Description, + const StringMap &Records); + ~TimerGroup(); void setName(StringRef NewName, StringRef NewDescription) { @@ -207,6 +212,8 @@ /// This static method prints all timers and clears them all out. static void printAll(raw_ostream &OS); + const char *printJSONValues(raw_ostream &OS, const char *delim); + /// Prints all timers as JSON key/value pairs, and clears them all out. static const char *printAllJSONValues(raw_ostream &OS, const char *delim); @@ -223,7 +230,6 @@ void PrintQueuedTimers(raw_ostream &OS); void printJSONValue(raw_ostream &OS, const PrintRecord &R, const char *suffix, double Value); - const char *printJSONValues(raw_ostream &OS, const char *delim); }; } // end namespace llvm Index: lib/Support/Timer.cpp =================================================================== --- lib/Support/Timer.cpp +++ lib/Support/Timer.cpp @@ -22,6 +22,8 @@ #include "llvm/Support/Process.h" #include "llvm/Support/YAMLTraits.h" #include "llvm/Support/raw_ostream.h" +#include + using namespace llvm; // This ugly hack is brought to you courtesy of constructor/destructor ordering @@ -234,6 +236,15 @@ TimerGroupList = this; } +TimerGroup::TimerGroup(StringRef Name, StringRef Description, + const StringMap &Records) + : TimerGroup(Name, Description) { + TimersToPrint.reserve(Records.size()); + for (const auto &P : Records) + TimersToPrint.emplace_back(P.getValue(), P.getKey(), P.getKey()); + assert(TimersToPrint.size() == Records.size() && "Size mismatch"); +} + TimerGroup::~TimerGroup() { // If the timer group is destroyed before the timers it owns, accumulate and // print the timing data. @@ -367,13 +378,17 @@ void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R, const char *suffix, double Value) { assert(yaml::needsQuotes(Name) == yaml::QuotingType::None && - "TimerGroup name needs no quotes"); + "TimerGroup name should not need quotes"); assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None && - "Timer name needs no quotes"); - OS << "\t\"time." << Name << '.' << R.Name << suffix << "\": " << Value; + "Timer name should not need quotes"); + constexpr auto max_digits10 = std::numeric_limits::max_digits10; + OS << "\t\"time." << Name << '.' << R.Name << suffix + << "\": " << format("%.*e", max_digits10 - 1, Value); } const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) { + sys::SmartScopedLock L(*TimerLock); + prepareToPrintList(); for (const PrintRecord &R : TimersToPrint) { OS << delim;