diff options
author | semenzato@chromium.org <semenzato@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-07 08:23:40 +0000 |
---|---|---|
committer | semenzato@chromium.org <semenzato@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-07 08:23:40 +0000 |
commit | 85bbf09119661731bdc0138a67a97a3b9c1b2706 (patch) | |
tree | 0dbaeb7aa523aaf00dcbcaa4d675ad4281544a67 /chrome/browser/chromeos/external_metrics.cc | |
parent | 40a82edd332493e5d3d841afc4ff5ae6a281856a (diff) | |
download | chromium_src-85bbf09119661731bdc0138a67a97a3b9c1b2706.zip chromium_src-85bbf09119661731bdc0138a67a97a3b9c1b2706.tar.gz chromium_src-85bbf09119661731bdc0138a67a97a3b9c1b2706.tar.bz2 |
Export sparse UMA histograms to Chrome OS.
Title says it all. This change extends the current external
metrics interface so it supports sparse histograms, for which
Chrome OS has an immediate need.
BUG=222189
TEST=compiled
Review URL: https://chromiumcodereview.appspot.com/12623028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@198667 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/external_metrics.cc')
-rw-r--r-- | chrome/browser/chromeos/external_metrics.cc | 110 |
1 files changed, 63 insertions, 47 deletions
diff --git a/chrome/browser/chromeos/external_metrics.cc b/chrome/browser/chromeos/external_metrics.cc index 71edb3b..fe24805 100644 --- a/chrome/browser/chromeos/external_metrics.cc +++ b/chrome/browser/chromeos/external_metrics.cc @@ -18,6 +18,7 @@ #include "base/basictypes.h" #include "base/bind.h" #include "base/metrics/histogram.h" +#include "base/metrics/sparse_histogram.h" #include "base/metrics/statistics_recorder.h" #include "base/perftimer.h" #include "base/posix/eintr_wrapper.h" @@ -57,9 +58,7 @@ bool CheckLinearValues(const std::string& name, int maximum) { // The interval between external metrics collections in seconds static const int kExternalMetricsCollectionIntervalSeconds = 30; -ExternalMetrics::ExternalMetrics() - : test_recorder_(NULL) { -} +ExternalMetrics::ExternalMetrics() : test_recorder_(NULL) {} ExternalMetrics::~ExternalMetrics() {} @@ -80,7 +79,7 @@ void ExternalMetrics::RecordActionUI(std::string action_string) { if (valid_user_actions_.count(action_string)) { content::RecordComputedAction(action_string); } else { - LOG(ERROR) << "undefined UMA action: " << action_string; + DLOG(ERROR) << "undefined UMA action: " << action_string; } } @@ -109,15 +108,15 @@ void ExternalMetrics::RecordHistogram(const char* histogram_data) { int n = sscanf(histogram_data, "%127s %d %d %d %d", name, &sample, &min, &max, &nbuckets); if (n != 5) { - LOG(ERROR) << "bad histogram request: " << histogram_data; + DLOG(ERROR) << "bad histogram request: " << histogram_data; return; } if (!CheckValues(name, min, max, nbuckets)) { - LOG(ERROR) << "Invalid histogram " << name - << ", min=" << min - << ", max=" << max - << ", nbuckets=" << nbuckets; + DLOG(ERROR) << "Invalid histogram " << name + << ", min=" << min + << ", max=" << max + << ", nbuckets=" << nbuckets; return; } // Do not use the UMA_HISTOGRAM_... macros here. They cache the Histogram @@ -132,13 +131,13 @@ void ExternalMetrics::RecordLinearHistogram(const char* histogram_data) { char name[128]; // length must be consistent with sscanf format below. int n = sscanf(histogram_data, "%127s %d %d", name, &sample, &max); if (n != 3) { - LOG(ERROR) << "bad linear histogram request: " << histogram_data; + DLOG(ERROR) << "bad linear histogram request: " << histogram_data; return; } if (!CheckLinearValues(name, max)) { - LOG(ERROR) << "Invalid linear histogram " << name - << ", max=" << max; + DLOG(ERROR) << "Invalid linear histogram " << name + << ", max=" << max; return; } // Do not use the UMA_HISTOGRAM_... macros here. They cache the Histogram @@ -148,6 +147,22 @@ void ExternalMetrics::RecordLinearHistogram(const char* histogram_data) { counter->Add(sample); } +void ExternalMetrics::RecordSparseHistogram(const char* histogram_data) { + int sample; + char name[128]; // length must be consistent with sscanf format below. + int n = sscanf(histogram_data, "%127s %d", name, &sample); + if (n != 2) { + DLOG(ERROR) << "bad sparse histogram request: " << histogram_data; + return; + } + + // Do not use the UMA_HISTOGRAM_... macros here. They cache the Histogram + // instance and thus only work if |name| is constant. + base::HistogramBase* counter = base::SparseHistogram::FactoryGet( + name, base::HistogramBase::kUmaTargetedHistogramFlag); + counter->Add(sample); +} + void ExternalMetrics::CollectEvents() { const char* event_file_path = "/var/log/metrics/uma-events"; struct stat stat_buf; @@ -158,7 +173,7 @@ void ExternalMetrics::CollectEvents() { result = stat(event_file_path, &stat_buf); if (result < 0) { if (errno != ENOENT) { - PLOG(ERROR) << event_file_path << ": bad metrics file stat"; + DPLOG(ERROR) << event_file_path << ": bad metrics file stat"; } // Nothing to collect---try later. return; @@ -169,12 +184,12 @@ void ExternalMetrics::CollectEvents() { } int fd = open(event_file_path, O_RDWR); if (fd < 0) { - PLOG(ERROR) << event_file_path << ": cannot open"; + DPLOG(ERROR) << event_file_path << ": cannot open"; return; } result = flock(fd, LOCK_EX); if (result < 0) { - PLOG(ERROR) << event_file_path << ": cannot lock"; + DPLOG(ERROR) << event_file_path << ": cannot lock"; close(fd); return; } @@ -186,34 +201,34 @@ void ExternalMetrics::CollectEvents() { int32 message_size; result = HANDLE_EINTR(read(fd, &message_size, sizeof(message_size))); if (result < 0) { - PLOG(ERROR) << "reading metrics message header"; + DPLOG(ERROR) << "reading metrics message header"; break; } - if (result == 0) { // normal EOF + if (result == 0) { // This indicates a normal EOF. break; } if (result < static_cast<int>(sizeof(message_size))) { - LOG(ERROR) << "bad read size " << result << - ", expecting " << sizeof(message_size); + DLOG(ERROR) << "bad read size " << result << + ", expecting " << sizeof(message_size); break; } // kMetricsMessageMaxLength applies to the entire message: the 4-byte // length field and the two null-terminated strings. if (message_size < 2 + static_cast<int>(sizeof(message_size)) || message_size > static_cast<int>(kMetricsMessageMaxLength)) { - LOG(ERROR) << "bad message size " << message_size; + DLOG(ERROR) << "bad message size " << message_size; break; } - message_size -= sizeof(message_size); // already read this much + message_size -= sizeof(message_size); // The message size includes itself. uint8 buffer[kMetricsMessageMaxLength]; result = HANDLE_EINTR(read(fd, buffer, message_size)); if (result < 0) { - PLOG(ERROR) << "reading metrics message body"; + DPLOG(ERROR) << "reading metrics message body"; break; } if (result < message_size) { - LOG(ERROR) << "message too short: length " << result << - ", expected " << message_size; + DLOG(ERROR) << "message too short: length " << result << + ", expected " << message_size; break; } // The buffer should now contain a pair of null-terminated strings. @@ -221,41 +236,42 @@ void ExternalMetrics::CollectEvents() { uint8* q = NULL; if (p != NULL) { q = reinterpret_cast<uint8*>( - memchr(p + 1, '\0', message_size - (p + 1 - buffer))); + memchr(p + 1, '\0', message_size - (p + 1 - buffer))); } if (q == NULL) { - LOG(ERROR) << "bad name-value pair for metrics"; + DLOG(ERROR) << "bad name-value pair for metrics"; break; + } + char* name = reinterpret_cast<char*>(buffer); + char* value = reinterpret_cast<char*>(p + 1); + if (test_recorder_ != NULL) { + test_recorder_(name, value); + } else if (strcmp(name, "crash") == 0) { + RecordCrash(value); + } else if (strcmp(name, "histogram") == 0) { + RecordHistogram(value); + } else if (strcmp(name, "linearhistogram") == 0) { + RecordLinearHistogram(value); + } else if (strcmp(name, "sparsehistogram") == 0) { + RecordSparseHistogram(value); + } else if (strcmp(name, "useraction") == 0) { + RecordAction(value); } else { - char* name = reinterpret_cast<char*>(buffer); - char* value = reinterpret_cast<char*>(p + 1); - if (test_recorder_ != NULL) { - test_recorder_(name, value); - } else if (strcmp(name, "crash") == 0) { - RecordCrash(value); - } else if (strcmp(name, "histogram") == 0) { - RecordHistogram(value); - } else if (strcmp(name, "linearhistogram") == 0) { - RecordLinearHistogram(value); - } else if (strcmp(name, "useraction") == 0) { - RecordAction(value); - } else { - LOG(ERROR) << "invalid event type: " << name; - } + DLOG(ERROR) << "invalid event type: " << name; } } result = ftruncate(fd, 0); if (result < 0) { - PLOG(ERROR) << "truncate metrics log"; + DPLOG(ERROR) << "truncate metrics log"; } result = flock(fd, LOCK_UN); if (result < 0) { - PLOG(ERROR) << "unlock metrics log"; + DPLOG(ERROR) << "unlock metrics log"; } result = close(fd); if (result < 0) { - PLOG(ERROR) << "close metrics log"; + DPLOG(ERROR) << "close metrics log"; } } @@ -269,9 +285,9 @@ void ExternalMetrics::CollectEventsAndReschedule() { void ExternalMetrics::ScheduleCollector() { bool result; result = BrowserThread::PostDelayedTask( - BrowserThread::FILE, FROM_HERE, - base::Bind(&chromeos::ExternalMetrics::CollectEventsAndReschedule, this), - base::TimeDelta::FromSeconds(kExternalMetricsCollectionIntervalSeconds)); + BrowserThread::FILE, FROM_HERE, + base::Bind(&chromeos::ExternalMetrics::CollectEventsAndReschedule, this), + base::TimeDelta::FromSeconds(kExternalMetricsCollectionIntervalSeconds)); DCHECK(result); } |