Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/TimeProfiler.cpp
Line
Count
Source
1
//===-- TimeProfiler.cpp - Hierarchical Time Profiler ---------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements hierarchical time profiler.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Support/TimeProfiler.h"
14
#include "llvm/ADT/StringMap.h"
15
#include "llvm/Support/CommandLine.h"
16
#include "llvm/Support/FileSystem.h"
17
#include "llvm/Support/JSON.h"
18
#include <cassert>
19
#include <chrono>
20
#include <string>
21
#include <vector>
22
23
using namespace std::chrono;
24
25
namespace llvm {
26
27
static cl::opt<unsigned> TimeTraceGranularity(
28
    "time-trace-granularity",
29
    cl::desc(
30
        "Minimum time granularity (in microseconds) traced by time profiler"),
31
    cl::init(500));
32
33
TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
34
35
typedef duration<steady_clock::rep, steady_clock::period> DurationType;
36
typedef std::pair<size_t, DurationType> CountAndDurationType;
37
typedef std::pair<std::string, CountAndDurationType>
38
    NameAndCountAndDurationType;
39
40
struct Entry {
41
  time_point<steady_clock> Start;
42
  DurationType Duration;
43
  std::string Name;
44
  std::string Detail;
45
46
  Entry(time_point<steady_clock> &&S, DurationType &&D, std::string &&N,
47
        std::string &&Dt)
48
      : Start(std::move(S)), Duration(std::move(D)), Name(std::move(N)),
49
63
        Detail(std::move(Dt)){};
50
};
51
52
struct TimeTraceProfiler {
53
1
  TimeTraceProfiler() {
54
1
    StartTime = steady_clock::now();
55
1
  }
56
57
63
  void begin(std::string Name, llvm::function_ref<std::string()> Detail) {
58
63
    Stack.emplace_back(steady_clock::now(), DurationType{}, std::move(Name),
59
63
                       Detail());
60
63
  }
61
62
63
  void end() {
63
63
    assert(!Stack.empty() && "Must call begin() first");
64
63
    auto &E = Stack.back();
65
63
    E.Duration = steady_clock::now() - E.Start;
66
63
67
63
    // Only include sections longer than TimeTraceGranularity msec.
68
63
    if (duration_cast<microseconds>(E.Duration).count() > TimeTraceGranularity)
69
63
      Entries.emplace_back(E);
70
63
71
63
    // Track total time taken by each "name", but only the topmost levels of
72
63
    // them; e.g. if there's a template instantiation that instantiates other
73
63
    // templates from within, we only want to add the topmost one. "topmost"
74
63
    // happens to be the ones that don't have any currently open entries above
75
63
    // itself.
76
269
    if (
std::find_if(++Stack.rbegin(), Stack.rend(), [&](const Entry &Val) 63
{
77
269
          return Val.Name == E.Name;
78
269
        }) == Stack.rend()) {
79
61
      auto &CountAndTotal = CountAndTotalPerName[E.Name];
80
61
      CountAndTotal.first++;
81
61
      CountAndTotal.second += E.Duration;
82
61
    }
83
63
84
63
    Stack.pop_back();
85
63
  }
86
87
1
  void Write(raw_pwrite_stream &OS) {
88
1
    assert(Stack.empty() &&
89
1
           "All profiler sections should be ended when calling Write");
90
1
    json::OStream J(OS);
91
1
    J.objectBegin();
92
1
    J.attributeBegin("traceEvents");
93
1
    J.arrayBegin();
94
1
95
1
    // Emit all events for the main flame graph.
96
63
    for (const auto &E : Entries) {
97
63
      auto StartUs = duration_cast<microseconds>(E.Start - StartTime).count();
98
63
      auto DurUs = duration_cast<microseconds>(E.Duration).count();
99
63
100
63
      J.object([&]{
101
63
        J.attribute("pid", 1);
102
63
        J.attribute("tid", 0);
103
63
        J.attribute("ph", "X");
104
63
        J.attribute("ts", StartUs);
105
63
        J.attribute("dur", DurUs);
106
63
        J.attribute("name", E.Name);
107
63
        J.attributeObject("args", [&] { J.attribute("detail", E.Detail); });
108
63
      });
109
63
    }
110
1
111
1
    // Emit totals by section name as additional "thread" events, sorted from
112
1
    // longest one.
113
1
    int Tid = 1;
114
1
    std::vector<NameAndCountAndDurationType> SortedTotals;
115
1
    SortedTotals.reserve(CountAndTotalPerName.size());
116
1
    for (const auto &E : CountAndTotalPerName)
117
10
      SortedTotals.emplace_back(E.getKey(), E.getValue());
118
1
119
1
    llvm::sort(SortedTotals.begin(), SortedTotals.end(),
120
1
               [](const NameAndCountAndDurationType &A,
121
24
                  const NameAndCountAndDurationType &B) {
122
24
                 return A.second.second > B.second.second;
123
24
               });
124
10
    for (const auto &E : SortedTotals) {
125
10
      auto DurUs = duration_cast<microseconds>(E.second.second).count();
126
10
      auto Count = CountAndTotalPerName[E.first].first;
127
10
128
10
      J.object([&]{
129
10
        J.attribute("pid", 1);
130
10
        J.attribute("tid", Tid);
131
10
        J.attribute("ph", "X");
132
10
        J.attribute("ts", 0);
133
10
        J.attribute("dur", DurUs);
134
10
        J.attribute("name", "Total " + E.first);
135
10
        J.attributeObject("args", [&] {
136
10
          J.attribute("count", int64_t(Count));
137
10
          J.attribute("avg ms", int64_t(DurUs / Count / 1000));
138
10
        });
139
10
      });
140
10
141
10
      ++Tid;
142
10
    }
143
1
144
1
    // Emit metadata event with process name.
145
1
    J.object([&] {
146
1
      J.attribute("cat", "");
147
1
      J.attribute("pid", 1);
148
1
      J.attribute("tid", 0);
149
1
      J.attribute("ts", 0);
150
1
      J.attribute("ph", "M");
151
1
      J.attribute("name", "process_name");
152
1
      J.attributeObject("args", [&] { J.attribute("name", "clang"); });
153
1
    });
154
1
155
1
    J.arrayEnd();
156
1
    J.attributeEnd();
157
1
    J.objectEnd();
158
1
  }
159
160
  SmallVector<Entry, 16> Stack;
161
  SmallVector<Entry, 128> Entries;
162
  StringMap<CountAndDurationType> CountAndTotalPerName;
163
  time_point<steady_clock> StartTime;
164
};
165
166
1
void timeTraceProfilerInitialize() {
167
1
  assert(TimeTraceProfilerInstance == nullptr &&
168
1
         "Profiler should not be initialized");
169
1
  TimeTraceProfilerInstance = new TimeTraceProfiler();
170
1
}
171
172
1
void timeTraceProfilerCleanup() {
173
1
  delete TimeTraceProfilerInstance;
174
1
  TimeTraceProfilerInstance = nullptr;
175
1
}
176
177
1
void timeTraceProfilerWrite(raw_pwrite_stream &OS) {
178
1
  assert(TimeTraceProfilerInstance != nullptr &&
179
1
         "Profiler object can't be null");
180
1
  TimeTraceProfilerInstance->Write(OS);
181
1
}
182
183
60
void timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
184
60
  if (TimeTraceProfilerInstance != nullptr)
185
60
    TimeTraceProfilerInstance->begin(Name, [&]() { return Detail; });
186
60
}
187
188
void timeTraceProfilerBegin(StringRef Name,
189
3
                            llvm::function_ref<std::string()> Detail) {
190
3
  if (TimeTraceProfilerInstance != nullptr)
191
3
    TimeTraceProfilerInstance->begin(Name, Detail);
192
3
}
193
194
63
void timeTraceProfilerEnd() {
195
63
  if (TimeTraceProfilerInstance != nullptr)
196
63
    TimeTraceProfilerInstance->end();
197
63
}
198
199
} // namespace llvm