Landing Recent QUIC changes until 8/19/2015 17:00 UTC.
[chromium-blink-merge.git] / cc / base / histograms.cc
blob9fe1515bc2ac726c6ba8e9ca5d794a1207783ad9
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 <limits>
11 #include "base/numerics/safe_conversions.h"
13 namespace cc {
15 // Minimum elapsed time of 1us to limit weighting of fast calls.
16 static const int64 kMinimumTimeMicroseconds = 1;
18 ScopedUMAHistogramAreaTimerBase::ScopedUMAHistogramAreaTimerBase() : area_(0) {
21 ScopedUMAHistogramAreaTimerBase::~ScopedUMAHistogramAreaTimerBase() {
24 bool ScopedUMAHistogramAreaTimerBase::GetHistogramValues(
25 Sample* time_microseconds,
26 Sample* pixels_per_ms) const {
27 return GetHistogramValues(
28 timer_.Elapsed(), area_.ValueOrDefault(std::numeric_limits<int>::max()),
29 time_microseconds, pixels_per_ms);
32 // static
33 bool ScopedUMAHistogramAreaTimerBase::GetHistogramValues(
34 base::TimeDelta elapsed,
35 int area,
36 Sample* time_microseconds,
37 Sample* pixels_per_ms) {
38 elapsed = std::max(
39 elapsed, base::TimeDelta::FromMicroseconds(kMinimumTimeMicroseconds));
40 double area_per_time = area / elapsed.InMillisecondsF();
41 // It is not clear how NaN can get here, but we've gotten crashes from
42 // saturated_cast. http://crbug.com/486214
43 if (std::isnan(area_per_time))
44 return false;
45 *time_microseconds = base::saturated_cast<Sample>(elapsed.InMicroseconds());
46 *pixels_per_ms = base::saturated_cast<Sample>(area_per_time);
47 return true;
50 } // namespace cc