| Index: base/metrics/chromeos_metrics.cc
|
| diff --git a/base/metrics/chromeos_metrics.cc b/base/metrics/chromeos_metrics.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5fcd4aa50b2026d2ff83ed455a69d677b84ddc9a
|
| --- /dev/null
|
| +++ b/base/metrics/chromeos_metrics.cc
|
| @@ -0,0 +1,113 @@
|
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include <fcntl.h>
|
| +#include <stdio.h>
|
| +#include <stdlib.h>
|
| +#include <sys/file.h>
|
| +#include <sys/stat.h>
|
| +#include <unistd.h>
|
| +
|
| +#include <list>
|
| +#include <memory>
|
| +#include <string>
|
| +
|
| +#include "base/file_util.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/logging.h"
|
| +#include "base/memory/scoped_vector.h"
|
| +#include "base/metrics/chromeos_metrics.h"
|
| +#include "base/metrics/crash_sample.h"
|
| +#include "base/metrics/histogram_sample.h"
|
| +#include "base/metrics/linearhistogram_sample.h"
|
| +#include "base/metrics/metric_sample.h"
|
| +#include "base/metrics/sparsehistogram_sample.h"
|
| +#include "base/metrics/useraction_sample.h"
|
| +#include "base/strings/string_split.h"
|
| +#include "base/strings/string_util.h"
|
| +
|
| +using std::string;
|
| +using std::vector;
|
| +
|
| +namespace base {
|
| +namespace ChromeOSMetrics {
|
| +MetricSample* ReadSample(const string& sample) {
|
| + vector<string> vector;
|
| + SplitString(sample, '\0', &vector);
|
| + // We should have a two null terminated strings so split should produce
|
| + // three chuncks.
|
| + if (vector.size() != 3) {
|
| + DLOG(ERROR) << "wrong length" << vector.size();
|
| + return NULL;
|
| + }
|
| + const string name = vector[0];
|
| + const string value = vector[1];
|
| +
|
| + if (LowerCaseEqualsASCII(name, "crash")) {
|
| + return CrashSample::ReadCrash(value);
|
| + } else if (LowerCaseEqualsASCII(name, "histogram")) {
|
| + return HistogramSample::ReadHistogram(value);
|
| + } else if (LowerCaseEqualsASCII(name, "linearhistogram")) {
|
| + return LinearHistogramSample::ReadLinearHistogram(value);
|
| + } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) {
|
| + return SparseHistogramSample::ReadSparseHistogram(value);
|
| + } else if (LowerCaseEqualsASCII(name, "useraction")) {
|
| + return UserActionSample::ReadUserAction(value);
|
| + } else {
|
| + DLOG(ERROR) << "invalid event type: " << name;
|
| + }
|
| + return NULL;
|
| +}
|
| +
|
| +bool WriteMetricToFile(const MetricSample& sample,
|
| + const std::string& filename) {
|
| + if (!sample.isValid()) {
|
| + return false;
|
| + }
|
| + File file =
|
| + File(FilePath(filename), File::FLAG_OPEN_ALWAYS | File::FLAG_APPEND);
|
| + File::Error err = file.Lock();
|
| + if (err != File::FILE_OK) {
|
| + DLOG(ERROR) << "could not lock the file";
|
| + return false;
|
| + }
|
| +
|
| + const string msg = sample.toString();
|
| + int32_t size = msg.length() + sizeof(int32_t);
|
| + file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t));
|
| + file.WriteAtCurrentPos(msg.c_str(), msg.length());
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void ReadAndTruncateMetricsFromFile(const std::string& filename,
|
| + ScopedVector<MetricSample>* metrics) {
|
| + File file = File(FilePath(filename),
|
| + File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE);
|
| + File::Error err = file.Lock();
|
| + if (err != File::FILE_OK) {
|
| + DLOG(ERROR) << "could not lock the file";
|
| + return;
|
| + }
|
| + for (;;) {
|
| + int32 message_length;
|
| + int read = file.ReadAtCurrentPos(reinterpret_cast<char*>(&message_length),
|
| + sizeof(message_length));
|
| + if (read != sizeof(message_length)) {
|
| + break;
|
| + }
|
| +
|
| + message_length -= sizeof(message_length);
|
| + char serialized_sample[kMessageMaxLength];
|
| + read = file.ReadAtCurrentPos(serialized_sample, message_length);
|
| + if (read != message_length) {
|
| + DLOG(ERROR) << "could not read message" << read;
|
| + break;
|
| + }
|
| + metrics->push_back(ReadSample(string(serialized_sample, message_length)));
|
| + }
|
| + file.SetLength(0);
|
| +}
|
| +} // namespace ChromeOSMetrics
|
| +} // namespace base
|
|
|