summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/timing/performance_user_timing.cc
blob: 595cb53dc12b80f1254c74bd80f123896ffc2483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (C) 2012 Intel Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/core/timing/performance_user_timing.h"

#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/core/timing/performance.h"
#include "third_party/blink/renderer/core/timing/performance_mark.h"
#include "third_party/blink/renderer/core/timing/performance_measure.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/histogram.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"

namespace blink {

namespace {

using RestrictedKeyMap = HashMap<AtomicString, NavigationTimingFunction>;

const RestrictedKeyMap& GetRestrictedKeyMap() {
  DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<RestrictedKeyMap>, map, ());
  if (!map.IsSet()) {
    *map = {
        {"navigationStart", &PerformanceTiming::navigationStart},
        {"unloadEventStart", &PerformanceTiming::unloadEventStart},
        {"unloadEventEnd", &PerformanceTiming::unloadEventEnd},
        {"redirectStart", &PerformanceTiming::redirectStart},
        {"redirectEnd", &PerformanceTiming::redirectEnd},
        {"fetchStart", &PerformanceTiming::fetchStart},
        {"domainLookupStart", &PerformanceTiming::domainLookupStart},
        {"domainLookupEnd", &PerformanceTiming::domainLookupEnd},
        {"connectStart", &PerformanceTiming::connectStart},
        {"connectEnd", &PerformanceTiming::connectEnd},
        {"secureConnectionStart", &PerformanceTiming::secureConnectionStart},
        {"requestStart", &PerformanceTiming::requestStart},
        {"responseStart", &PerformanceTiming::responseStart},
        {"responseEnd", &PerformanceTiming::responseEnd},
        {"domLoading", &PerformanceTiming::domLoading},
        {"domInteractive", &PerformanceTiming::domInteractive},
        {"domContentLoadedEventStart",
         &PerformanceTiming::domContentLoadedEventStart},
        {"domContentLoadedEventEnd",
         &PerformanceTiming::domContentLoadedEventEnd},
        {"domComplete", &PerformanceTiming::domComplete},
        {"loadEventStart", &PerformanceTiming::loadEventStart},
        {"loadEventEnd", &PerformanceTiming::loadEventEnd},
    };
  }
  return *map;
}

}  // namespace

UserTiming::UserTiming(Performance& performance) : performance_(&performance) {}

static void InsertPerformanceEntry(PerformanceEntryMap& performance_entry_map,
                                   PerformanceEntry& entry) {
  PerformanceEntryMap::iterator it = performance_entry_map.find(entry.name());
  if (it != performance_entry_map.end()) {
    it->value.push_back(&entry);
  } else {
    PerformanceEntryVector vector(1);
    vector[0] = Member<PerformanceEntry>(entry);
    performance_entry_map.Set(entry.name(), vector);
  }
}

static void ClearPeformanceEntries(PerformanceEntryMap& performance_entry_map,
                                   const AtomicString& name) {
  if (name.IsNull()) {
    performance_entry_map.clear();
    return;
  }

  if (performance_entry_map.Contains(name))
    performance_entry_map.erase(name);
}

PerformanceMark* UserTiming::Mark(ScriptState* script_state,
                                  const AtomicString& mark_name,
                                  const DOMHighResTimeStamp& start_time,
                                  const ScriptValue& detail,
                                  ExceptionState& exception_state) {
  if (GetRestrictedKeyMap().Contains(mark_name)) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kSyntaxError,
        "'" + mark_name +
            "' is part of the PerformanceTiming interface, and "
            "cannot be used as a mark name.");
    return nullptr;
  }

  TRACE_EVENT_COPY_MARK("blink.user_timing", mark_name.Utf8().data());
  PerformanceMark* mark =
      PerformanceMark::Create(script_state, mark_name, start_time, detail);
  InsertPerformanceEntry(marks_map_, *mark);
  DEFINE_THREAD_SAFE_STATIC_LOCAL(CustomCountHistogram,
                                  user_timing_mark_histogram,
                                  ("PLT.UserTiming_Mark", 0, 600000, 100));
  user_timing_mark_histogram.Count(static_cast<int>(start_time));
  return mark;
}

void UserTiming::ClearMarks(const AtomicString& mark_name) {
  ClearPeformanceEntries(marks_map_, mark_name);
}

double UserTiming::FindExistingMarkStartTime(const AtomicString& mark_name,
                                             ExceptionState& exception_state) {
  if (marks_map_.Contains(mark_name))
    return marks_map_.at(mark_name).back()->startTime();

  if (GetRestrictedKeyMap().Contains(mark_name) && performance_->timing()) {
    double value = static_cast<double>(
        (performance_->timing()->*(GetRestrictedKeyMap().at(mark_name)))());
    if (!value) {
      exception_state.ThrowDOMException(
          DOMExceptionCode::kInvalidAccessError,
          "'" + mark_name +
              "' is empty: either the event hasn't "
              "happened yet, or it would provide "
              "cross-origin timing information.");
      return 0.0;
    }
    return value - performance_->timing()->navigationStart();
  }

  exception_state.ThrowDOMException(
      DOMExceptionCode::kSyntaxError,
      "The mark '" + mark_name + "' does not exist.");
  return 0.0;
}

double UserTiming::FindStartMarkOrTime(const StringOrDouble& start,
                                       ExceptionState& exception_state) {
  if (start.IsString()) {
    return FindExistingMarkStartTime(AtomicString(start.GetAsString()),
                                     exception_state);
  }
  if (start.IsDouble())
    return start.GetAsDouble();
  NOTREACHED();
  return 0;
}

PerformanceMeasure* UserTiming::Measure(ScriptState* script_state,
                                        const AtomicString& measure_name,
                                        const StringOrDouble& start,
                                        const StringOrDouble& end,
                                        const ScriptValue& detail,
                                        ExceptionState& exception_state) {
  double start_time = 0.0;
  double end_time = 0.0;

  if (start.IsNull()) {
    end_time = performance_->now();
  } else if (end.IsNull()) {
    end_time = performance_->now();
    start_time = FindStartMarkOrTime(start, exception_state);
    if (exception_state.HadException())
      return nullptr;
  } else {
    end_time = FindStartMarkOrTime(end, exception_state);
    if (exception_state.HadException())
      return nullptr;
    start_time = FindStartMarkOrTime(start, exception_state);
    if (exception_state.HadException())
      return nullptr;
  }

  // User timing events are stored as integer milliseconds from the start of
  // navigation, whereas trace events accept double seconds based off of
  // CurrentTime::monotonicallyIncreasingTime().
  double start_time_monotonic =
      performance_->GetTimeOrigin() + start_time / 1000.0;
  double end_time_monotonic = performance_->GetTimeOrigin() + end_time / 1000.0;

  TRACE_EVENT_COPY_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
      "blink.user_timing", measure_name.Utf8().data(),
      WTF::StringHash::GetHash(measure_name),
      TraceEvent::ToTraceTimestamp(start_time_monotonic));
  TRACE_EVENT_COPY_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
      "blink.user_timing", measure_name.Utf8().data(),
      WTF::StringHash::GetHash(measure_name),
      TraceEvent::ToTraceTimestamp(end_time_monotonic));

  PerformanceMeasure* measure = PerformanceMeasure::Create(
      script_state, measure_name, start_time, end_time, detail);
  InsertPerformanceEntry(measures_map_, *measure);
  if (end_time >= start_time) {
    DEFINE_THREAD_SAFE_STATIC_LOCAL(
        CustomCountHistogram, measure_duration_histogram,
        ("PLT.UserTiming_MeasureDuration", 0, 600000, 100));
    measure_duration_histogram.Count(static_cast<int>(end_time - start_time));
  }
  return measure;
}

void UserTiming::ClearMeasures(const AtomicString& measure_name) {
  ClearPeformanceEntries(measures_map_, measure_name);
}

static PerformanceEntryVector ConvertToEntrySequence(
    const PerformanceEntryMap& performance_entry_map) {
  PerformanceEntryVector entries;

  for (const auto& entry : performance_entry_map)
    entries.AppendVector(entry.value);

  return entries;
}

static PerformanceEntryVector GetEntrySequenceByName(
    const PerformanceEntryMap& performance_entry_map,
    const AtomicString& name) {
  PerformanceEntryVector entries;

  PerformanceEntryMap::const_iterator it = performance_entry_map.find(name);
  if (it != performance_entry_map.end())
    entries.AppendVector(it->value);

  return entries;
}

PerformanceEntryVector UserTiming::GetMarks() const {
  return ConvertToEntrySequence(marks_map_);
}

PerformanceEntryVector UserTiming::GetMarks(const AtomicString& name) const {
  return GetEntrySequenceByName(marks_map_, name);
}

PerformanceEntryVector UserTiming::GetMeasures() const {
  return ConvertToEntrySequence(measures_map_);
}

PerformanceEntryVector UserTiming::GetMeasures(const AtomicString& name) const {
  return GetEntrySequenceByName(measures_map_, name);
}

void UserTiming::Trace(blink::Visitor* visitor) {
  visitor->Trace(performance_);
  visitor->Trace(marks_map_);
  visitor->Trace(measures_map_);
}

}  // namespace blink