Roll src/third_party/WebKit f9e4789:c8f1b7f (svn 202029:202030)
[chromium-blink-merge.git] / cc / base / histograms.cc
blobba6f4f66794cea22115046a1ef313a0ab9771e51
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/base/histograms.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <cstring>
10 #include <limits>
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "base/synchronization/lock.h"
17 namespace cc {
19 // Global data tracking the client name that was set.
20 // Both of these variables are protected by the lock.
21 static base::LazyInstance<base::Lock>::Leaky g_client_name_lock =
22 LAZY_INSTANCE_INITIALIZER;
23 static const char* g_client_name = nullptr;
24 static bool g_multiple_client_names_set = false;
26 void SetClientNameForMetrics(const char* client_name) {
27 base::AutoLock auto_lock(g_client_name_lock.Get());
29 // Only warn once.
30 if (g_multiple_client_names_set)
31 return;
33 // If a different name is set, return nullptr from now on and log a warning.
34 const char* old_client_name = g_client_name;
35 if (old_client_name && strcmp(old_client_name, client_name)) {
36 g_client_name = nullptr;
37 g_multiple_client_names_set = true;
38 LOG(WARNING) << "Started multiple compositor clients (" << old_client_name
39 << ", " << client_name
40 << ") in one process. Some metrics will be disabled.";
41 return;
44 // If the client name is being set for the first time, store it.
45 if (!old_client_name)
46 g_client_name = client_name;
49 const char* GetClientNameForMetrics() {
50 base::AutoLock auto_lock(g_client_name_lock.Get());
51 return g_client_name;
54 // Minimum elapsed time of 1us to limit weighting of fast calls.
55 static const int64 kMinimumTimeMicroseconds = 1;
57 ScopedUMAHistogramAreaTimerBase::ScopedUMAHistogramAreaTimerBase() : area_(0) {
60 ScopedUMAHistogramAreaTimerBase::~ScopedUMAHistogramAreaTimerBase() {
63 bool ScopedUMAHistogramAreaTimerBase::GetHistogramValues(
64 Sample* time_microseconds,
65 Sample* pixels_per_ms) const {
66 return GetHistogramValues(
67 timer_.Elapsed(), area_.ValueOrDefault(std::numeric_limits<int>::max()),
68 time_microseconds, pixels_per_ms);
71 // static
72 bool ScopedUMAHistogramAreaTimerBase::GetHistogramValues(
73 base::TimeDelta elapsed,
74 int area,
75 Sample* time_microseconds,
76 Sample* pixels_per_ms) {
77 elapsed = std::max(
78 elapsed, base::TimeDelta::FromMicroseconds(kMinimumTimeMicroseconds));
79 double area_per_time = area / elapsed.InMillisecondsF();
80 // It is not clear how NaN can get here, but we've gotten crashes from
81 // saturated_cast. http://crbug.com/486214
82 if (std::isnan(area_per_time))
83 return false;
84 *time_microseconds = base::saturated_cast<Sample>(elapsed.InMicroseconds());
85 *pixels_per_ms = base::saturated_cast<Sample>(area_per_time);
86 return true;
89 } // namespace cc