| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/performance_monitor/metric.h" | 5 #include "chrome/browser/performance_monitor/metric.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 10 #include "base/time.h" | |
| 11 #include "chrome/browser/performance_monitor/constants.h" | |
| 12 | 9 |
| 13 namespace performance_monitor { | 10 namespace performance_monitor { |
| 14 | 11 |
| 15 namespace { | |
| 16 | |
| 17 // For certain metrics (for instance, bytes read), it is possible that there is | |
| 18 // no maximum value which we can safely assume. | |
| 19 const double kNoMaximum = -1.0; | |
| 20 | |
| 21 // These constants are designed to keep metrics reasonable. However, due to the | |
| 22 // variety of system configurations which can run chrome, these values may not | |
| 23 // catch *all* erroneous values. For instance, on a one-CPU machine, any CPU | |
| 24 // usage > 100 is erroneous, but on a 16-CPU machine, it's perfectly normal. | |
| 25 // These are "best-guesses" in order to weed out obviously-false values. A | |
| 26 // metric is valid if it is greater than or equal to the minimum and less than | |
| 27 // the maximum, i.e. if it falls in the range [min, max). | |
| 28 const double kMinUndefined = 0.0; | |
| 29 const double kMaxUndefined = 0.0; // No undefined metric is valid. | |
| 30 const double kMinCpuUsage = 0.0; | |
| 31 const double kMaxCpuUsage = 100000.0; // 100% on a 1000-CPU machine. | |
| 32 const double kMinPrivateMemoryUsage = 0.0; | |
| 33 const double kMaxPrivateMemoryUsage = kBytesPerTerabyte; | |
| 34 const double kMinSharedMemoryUsage = 0.0; | |
| 35 const double kMaxSharedMemoryUsage = kBytesPerTerabyte; | |
| 36 const double kMinStartupTime = 0.0; | |
| 37 const double kMaxStartupTime = base::Time::kMicrosecondsPerMinute * 15.0; | |
| 38 const double kMinTestStartupTime = 0.0; | |
| 39 const double kMaxTestStartupTime = base::Time::kMicrosecondsPerMinute * 15.0; | |
| 40 const double kMinSessionRestoreTime = 0.0; | |
| 41 const double kMaxSessionRestoreTime = base::Time::kMicrosecondsPerMinute * 15.0; | |
| 42 const double kMinPageLoadTime = 0.0; | |
| 43 const double kMaxPageLoadTime = base::Time::kMicrosecondsPerMinute * 15.0; | |
| 44 const double kMinNetworkBytesRead = 0.0; | |
| 45 const double kMaxNetworkBytesRead = kNoMaximum; | |
| 46 | |
| 47 struct MetricBound { | |
| 48 double min; | |
| 49 double max; | |
| 50 }; | |
| 51 | |
| 52 const MetricBound kMetricBounds[] = { | |
| 53 { kMinUndefined, kMaxUndefined }, | |
| 54 { kMinCpuUsage, kMaxCpuUsage }, | |
| 55 { kMinPrivateMemoryUsage, kMaxPrivateMemoryUsage }, | |
| 56 { kMinSharedMemoryUsage, kMaxSharedMemoryUsage }, | |
| 57 { kMinStartupTime, kMaxStartupTime }, | |
| 58 { kMinTestStartupTime, kMaxTestStartupTime }, | |
| 59 { kMinSessionRestoreTime, kMaxSessionRestoreTime }, | |
| 60 { kMinPageLoadTime, kMaxPageLoadTime }, | |
| 61 { kMinNetworkBytesRead, kMaxNetworkBytesRead }, | |
| 62 }; | |
| 63 | |
| 64 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMetricBounds) == METRIC_NUMBER_OF_METRICS, | |
| 65 metric_bounds_size_doesnt_match_metric_count); | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 Metric::Metric() { | 12 Metric::Metric() { |
| 70 value = 0.0; | 13 value = 0.0; |
| 71 } | 14 } |
| 72 | 15 |
| 73 Metric::Metric(MetricType metric_type, | 16 Metric::Metric(const base::Time& metric_time, const double metric_value) |
| 74 const base::Time& metric_time, | 17 : time(metric_time), value(metric_value) { |
| 75 const double metric_value) | |
| 76 : type(metric_type), time(metric_time), value(metric_value) { | |
| 77 } | 18 } |
| 78 | 19 |
| 79 Metric::Metric(MetricType metric_type, | 20 Metric::Metric(const std::string& metric_time, |
| 80 const std::string& metric_time, | 21 const std::string& metric_value) { |
| 81 const std::string& metric_value) : type(metric_type) { | |
| 82 int64 conversion = 0; | 22 int64 conversion = 0; |
| 83 base::StringToInt64(metric_time, &conversion); | 23 base::StringToInt64(metric_time, &conversion); |
| 84 time = base::Time::FromInternalValue(conversion); | 24 time = base::Time::FromInternalValue(conversion); |
| 85 CHECK(base::StringToDouble(metric_value, &value)); | 25 CHECK(base::StringToDouble(metric_value, &value)); |
| 86 } | 26 } |
| 87 | 27 |
| 88 Metric::~Metric() { | 28 Metric::~Metric() { |
| 89 } | 29 } |
| 90 | 30 |
| 91 bool Metric::IsValid() const { | |
| 92 return type < METRIC_NUMBER_OF_METRICS && | |
| 93 (value < kMetricBounds[type].max || | |
| 94 kMetricBounds[type].max == kNoMaximum) && | |
| 95 value >= kMetricBounds[type].min; | |
| 96 } | |
| 97 | |
| 98 std::string Metric::ValueAsString() const { | |
| 99 return base::DoubleToString(value); | |
| 100 } | |
| 101 | |
| 102 } // namespace performance_monitor | 31 } // namespace performance_monitor |
| OLD | NEW |