| 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/chromeos/external_metrics.h" | 5 #include "chrome/browser/chromeos/external_metrics.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
| 26 #include "chrome/browser/metrics/metrics_service.h" | 26 #include "chrome/browser/metrics/metrics_service.h" |
| 27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/user_metrics.h" | 28 #include "content/public/browser/user_metrics.h" |
| 29 | 29 |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 using content::UserMetricsAction; | 31 using content::UserMetricsAction; |
| 32 | 32 |
| 33 namespace chromeos { | 33 namespace chromeos { |
| 34 | 34 |
| 35 namespace { |
| 36 |
| 37 bool CheckValues(const std::string& name, |
| 38 int minimum, |
| 39 int maximum, |
| 40 size_t bucket_count) { |
| 41 if (!base::Histogram::InspectConstructionArguments( |
| 42 name, &minimum, &maximum, &bucket_count)) |
| 43 return false; |
| 44 base::HistogramBase* histogram = |
| 45 base::StatisticsRecorder::FindHistogram(name); |
| 46 if (!histogram) |
| 47 return true; |
| 48 return histogram->HasConstructionArguments(minimum, maximum, bucket_count); |
| 49 } |
| 50 |
| 51 bool CheckLinearValues(const std::string& name, int maximum) { |
| 52 return CheckValues(name, 1, maximum, maximum + 1); |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 35 // The interval between external metrics collections in seconds | 57 // The interval between external metrics collections in seconds |
| 36 static const int kExternalMetricsCollectionIntervalSeconds = 30; | 58 static const int kExternalMetricsCollectionIntervalSeconds = 30; |
| 37 | 59 |
| 38 class SystemHistogram : public base::Histogram { | |
| 39 public: | |
| 40 static bool CheckValues(const std::string& name, | |
| 41 int minimum, | |
| 42 int maximum, | |
| 43 size_t bucket_count) { | |
| 44 if (!base::Histogram::InspectConstructionArguments( | |
| 45 name, &minimum, &maximum, &bucket_count)) | |
| 46 return false; | |
| 47 Histogram* histogram = base::StatisticsRecorder::FindHistogram(name); | |
| 48 if (!histogram) | |
| 49 return true; | |
| 50 return histogram->HasConstructionArguments(minimum, maximum, bucket_count); | |
| 51 } | |
| 52 static bool CheckLinearValues(const std::string& name, int maximum) { | |
| 53 return CheckValues(name, 1, maximum, maximum + 1); | |
| 54 } | |
| 55 }; | |
| 56 | |
| 57 ExternalMetrics::ExternalMetrics() | 60 ExternalMetrics::ExternalMetrics() |
| 58 : test_recorder_(NULL) { | 61 : test_recorder_(NULL) { |
| 59 } | 62 } |
| 60 | 63 |
| 61 ExternalMetrics::~ExternalMetrics() {} | 64 ExternalMetrics::~ExternalMetrics() {} |
| 62 | 65 |
| 63 void ExternalMetrics::Start() { | 66 void ExternalMetrics::Start() { |
| 64 // Register user actions external to the browser. | 67 // Register user actions external to the browser. |
| 65 // chrome/tools/extract_actions.py won't understand these lines, so all of | 68 // chrome/tools/extract_actions.py won't understand these lines, so all of |
| 66 // these are explicitly added in that script. | 69 // these are explicitly added in that script. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 void ExternalMetrics::RecordHistogram(const char* histogram_data) { | 106 void ExternalMetrics::RecordHistogram(const char* histogram_data) { |
| 104 int sample, min, max, nbuckets; | 107 int sample, min, max, nbuckets; |
| 105 char name[128]; // length must be consistent with sscanf format below. | 108 char name[128]; // length must be consistent with sscanf format below. |
| 106 int n = sscanf(histogram_data, "%127s %d %d %d %d", | 109 int n = sscanf(histogram_data, "%127s %d %d %d %d", |
| 107 name, &sample, &min, &max, &nbuckets); | 110 name, &sample, &min, &max, &nbuckets); |
| 108 if (n != 5) { | 111 if (n != 5) { |
| 109 LOG(ERROR) << "bad histogram request: " << histogram_data; | 112 LOG(ERROR) << "bad histogram request: " << histogram_data; |
| 110 return; | 113 return; |
| 111 } | 114 } |
| 112 | 115 |
| 113 if (!SystemHistogram::CheckValues(name, min, max, nbuckets)) { | 116 if (!CheckValues(name, min, max, nbuckets)) { |
| 114 LOG(ERROR) << "Invalid histogram " << name | 117 LOG(ERROR) << "Invalid histogram " << name |
| 115 << ", min=" << min | 118 << ", min=" << min |
| 116 << ", max=" << max | 119 << ", max=" << max |
| 117 << ", nbuckets=" << nbuckets; | 120 << ", nbuckets=" << nbuckets; |
| 118 return; | 121 return; |
| 119 } | 122 } |
| 120 // Do not use the UMA_HISTOGRAM_... macros here. They cache the Histogram | 123 // Do not use the UMA_HISTOGRAM_... macros here. They cache the Histogram |
| 121 // instance and thus only work if |name| is constant. | 124 // instance and thus only work if |name| is constant. |
| 122 base::Histogram* counter = base::Histogram::FactoryGet( | 125 base::Histogram* counter = base::Histogram::FactoryGet( |
| 123 name, min, max, nbuckets, base::Histogram::kUmaTargetedHistogramFlag); | 126 name, min, max, nbuckets, base::Histogram::kUmaTargetedHistogramFlag); |
| 124 counter->Add(sample); | 127 counter->Add(sample); |
| 125 } | 128 } |
| 126 | 129 |
| 127 void ExternalMetrics::RecordLinearHistogram(const char* histogram_data) { | 130 void ExternalMetrics::RecordLinearHistogram(const char* histogram_data) { |
| 128 int sample, max; | 131 int sample, max; |
| 129 char name[128]; // length must be consistent with sscanf format below. | 132 char name[128]; // length must be consistent with sscanf format below. |
| 130 int n = sscanf(histogram_data, "%127s %d %d", name, &sample, &max); | 133 int n = sscanf(histogram_data, "%127s %d %d", name, &sample, &max); |
| 131 if (n != 3) { | 134 if (n != 3) { |
| 132 LOG(ERROR) << "bad linear histogram request: " << histogram_data; | 135 LOG(ERROR) << "bad linear histogram request: " << histogram_data; |
| 133 return; | 136 return; |
| 134 } | 137 } |
| 135 | 138 |
| 136 if (!SystemHistogram::CheckLinearValues(name, max)) { | 139 if (!CheckLinearValues(name, max)) { |
| 137 LOG(ERROR) << "Invalid linear histogram " << name | 140 LOG(ERROR) << "Invalid linear histogram " << name |
| 138 << ", max=" << max; | 141 << ", max=" << max; |
| 139 return; | 142 return; |
| 140 } | 143 } |
| 141 // Do not use the UMA_HISTOGRAM_... macros here. They cache the Histogram | 144 // Do not use the UMA_HISTOGRAM_... macros here. They cache the Histogram |
| 142 // instance and thus only work if |name| is constant. | 145 // instance and thus only work if |name| is constant. |
| 143 base::Histogram* counter = base::LinearHistogram::FactoryGet( | 146 base::Histogram* counter = base::LinearHistogram::FactoryGet( |
| 144 name, 1, max, max + 1, base::Histogram::kUmaTargetedHistogramFlag); | 147 name, 1, max, max + 1, base::Histogram::kUmaTargetedHistogramFlag); |
| 145 counter->Add(sample); | 148 counter->Add(sample); |
| 146 } | 149 } |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 void ExternalMetrics::ScheduleCollector() { | 269 void ExternalMetrics::ScheduleCollector() { |
| 267 bool result; | 270 bool result; |
| 268 result = BrowserThread::PostDelayedTask( | 271 result = BrowserThread::PostDelayedTask( |
| 269 BrowserThread::FILE, FROM_HERE, | 272 BrowserThread::FILE, FROM_HERE, |
| 270 base::Bind(&chromeos::ExternalMetrics::CollectEventsAndReschedule, this), | 273 base::Bind(&chromeos::ExternalMetrics::CollectEventsAndReschedule, this), |
| 271 base::TimeDelta::FromSeconds(kExternalMetricsCollectionIntervalSeconds)); | 274 base::TimeDelta::FromSeconds(kExternalMetricsCollectionIntervalSeconds)); |
| 272 DCHECK(result); | 275 DCHECK(result); |
| 273 } | 276 } |
| 274 | 277 |
| 275 } // namespace chromeos | 278 } // namespace chromeos |
| OLD | NEW |