diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp --- a/llvm/lib/Support/TimeProfiler.cpp +++ b/llvm/lib/Support/TimeProfiler.cpp @@ -51,7 +51,9 @@ } typedef duration DurationType; -typedef std::pair NameAndDuration; +typedef std::pair CountAndDurationType; +typedef std::pair + NameAndCountAndDurationType; struct Entry { time_point Start; @@ -89,8 +91,9 @@ if (std::find_if(++Stack.rbegin(), Stack.rend(), [&](const Entry &Val) { return Val.Name == E.Name; }) == Stack.rend()) { - TotalPerName[E.Name] += E.Duration; - CountPerName[E.Name]++; + auto &CountAndTotal = CountAndTotalPerName[E.Name]; + CountAndTotal.first++; + CountAndTotal.second += E.Duration; } Stack.pop_back(); @@ -115,23 +118,24 @@ // Emit totals by section name as additional "thread" events, sorted from // longest one. int Tid = 1; - std::vector SortedTotals; - SortedTotals.reserve(TotalPerName.size()); - for (const auto &E : TotalPerName) { - SortedTotals.push_back(E); + std::vector SortedTotals; + SortedTotals.reserve(CountAndTotalPerName.size()); + for (const auto &E : CountAndTotalPerName) { + SortedTotals.emplace_back(E.getKey(), E.getValue()); } std::sort(SortedTotals.begin(), SortedTotals.end(), - [](const NameAndDuration &A, const NameAndDuration &B) { - return A.second > B.second; + [](const NameAndCountAndDurationType &A, + const NameAndCountAndDurationType &B) { + return A.second.second > B.second.second; }); for (const auto &E : SortedTotals) { - auto DurUs = duration_cast(E.second).count(); + auto DurUs = duration_cast(E.second.second).count(); *OS << "{ \"pid\":1, \"tid\":" << Tid << ", \"ph\":\"X\", \"ts\":" << 0 << ", \"dur\":" << DurUs << ", \"name\":\"Total " << escapeString(E.first) - << "\", \"args\":{ \"count\":" << CountPerName[E.first] - << ", \"avg ms\":" << (DurUs / CountPerName[E.first] / 1000) - << "} },\n"; + << "\", \"args\":{ \"count\":" << CountAndTotalPerName[E.first].first + << ", \"avg ms\":" + << (DurUs / CountAndTotalPerName[E.first].first / 1000) << "} },\n"; ++Tid; } @@ -143,8 +147,7 @@ std::vector Stack; std::vector Entries; - std::unordered_map TotalPerName; - std::unordered_map CountPerName; + StringMap CountAndTotalPerName; time_point StartTime; };