Chromium Code Reviews| Index: components/metrics/chromeos/metrics_utils.cc |
| diff --git a/components/metrics/chromeos/metrics_utils.cc b/components/metrics/chromeos/metrics_utils.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b2d83587275cf858005324673ffd8450af99418a |
| --- /dev/null |
| +++ b/components/metrics/chromeos/metrics_utils.cc |
| @@ -0,0 +1,117 @@ |
| +// Copyright 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 "components/metrics/chromeos/metrics_utils.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/file_util.h" |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| +#include "components/metrics/chromeos/metric_sample.h" |
| + |
| +namespace metrics { |
| + |
| +scoped_ptr<MetricSample> MetricsUtils::ReadSample(const std::string& sample) { |
| + std::vector<std::string> parts; |
| + base::SplitString(sample, '\0', &parts); |
| + // We should have two null terminated strings so split should produce |
| + // three chunks. |
| + if (parts.size() != 3) { |
| + DLOG(ERROR) << "splitting message on \\0 produced " << parts.size() |
| + << " parts (expected 3)"; |
| + return scoped_ptr<MetricSample>(); |
| + } |
| + const std::string& name = parts[0]; |
| + const std::string& value = parts[1]; |
| + |
| + if (LowerCaseEqualsASCII(name, "crash")) { |
| + return MetricSample::CrashSample(name); |
| + } else if (LowerCaseEqualsASCII(name, "histogram")) { |
| + return MetricSample::ReadHistogram(value); |
| + } else if (LowerCaseEqualsASCII(name, "linearhistogram")) { |
| + return MetricSample::ReadLinearHistogram(value); |
| + } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) { |
| + return MetricSample::ReadSparseHistogram(value); |
| + } else if (LowerCaseEqualsASCII(name, "useraction")) { |
| + return MetricSample::UserActionSample(name); |
| + } else { |
| + DLOG(ERROR) << "invalid event type: " << name << ", value: " << value; |
| + } |
| + return scoped_ptr<MetricSample>(); |
| +} |
| + |
| +bool MetricsUtils::WriteMetricToFile(MetricSample* sample, |
| + const std::string& filename) { |
| + if (!sample->IsValid()) |
| + return false; |
| + base::File file(base::FilePath(filename), |
| + base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); |
| + base::File::Error err = file.Lock(); |
|
Luigi Semenzato
2014/05/12 23:04:48
I assume file.Lock() waits for a lock to be releas
bsimonnet
2014/05/13 00:13:59
I thought it did but it does not (it is using fcnt
|
| + if (err != base::File::FILE_OK) { |
| + DLOG(ERROR) << "could not lock the file"; |
|
Luigi Semenzato
2014/05/12 23:04:48
Is it possible to print the errno here? Or maybe
|
| + return false; |
| + } |
| + |
| + const std::string msg = sample->ToString(); |
| + int32_t size = msg.length() + sizeof(int32_t); |
| + if (msg.length() > size_t(kMessageMaxLength)) { |
| + DLOG(ERROR) << "cannot write message: too long"; |
| + return false; |
| + } |
| + file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t)); |
| + file.WriteAtCurrentPos(msg.c_str(), msg.length()); |
| + |
| + return true; |
| +} |
| + |
| +void MetricsUtils::ReadAndTruncateMetricsFromFile( |
| + const std::string& filename, |
| + ScopedVector<MetricSample>* metrics) { |
| + base::File file( |
| + base::FilePath(filename), |
| + base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE); |
| + base::File::Error err = file.Lock(); |
| + if (err != base::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); |
| + if (message_length > kMessageMaxLength) { |
| + DLOG(ERROR) << "cannot read message: too long"; |
| + |
| + // Skip the invalid message and continue reading. |
| + if (file.Seek(base::File::FROM_CURRENT, message_length) != -1) |
| + continue; |
| + |
| + DLOG(ERROR) << "cannot skip long message"; |
| + // We do not know the size of the message. The rest of the file is not |
| + // readable. Truncate the file and exit. |
| + break; |
| + } |
| + 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(std::string(serialized_sample, message_length)).release()); |
| + } |
| + file.SetLength(0); |
|
Luigi Semenzato
2014/05/12 23:04:48
Where is the file unlocked? Is it possible to unl
bsimonnet
2014/05/13 00:13:59
The file is closed when |file| is destroyed which
|
| +} |
| + |
| +} // namespace metrics |