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..bd3e7d3c589cc611d6f9cfaf8e66d7db3f336552 |
| --- /dev/null |
| +++ b/components/metrics/chromeos/metrics_utils.cc |
| @@ -0,0 +1,192 @@ |
| +// 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 <sys/file.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" |
| + |
| +#define READ_WRITE_ALL_FILE_FLAGS \ |
| + (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) |
| +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
|
Alexei Svitkine (slow)
2014/05/13 15:36:26
Please use arraysize() or ARRAYSIZE_UNSAFE() from
bsimonnet
2014/05/13 17:59:14
It is a remainder from the refactoring, I deleted
|
| + |
| +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(const MetricSample* sample, |
| + const std::string& filename) { |
| + if (!sample->IsValid()) |
| + return false; |
| + |
| + int file_descriptor = HANDLE_EINTR(open(filename.c_str(), |
| + O_WRONLY | O_APPEND | O_CREAT, |
| + READ_WRITE_ALL_FILE_FLAGS)); |
| + |
| + if (file_descriptor < 0) { |
| + DLOG(ERROR) << "error openning the file"; |
|
Alexei Svitkine (slow)
2014/05/13 15:36:26
return false here?
bsimonnet
2014/05/13 17:59:14
Done.
|
| + } |
| + |
| + fchmod(file_descriptor, READ_WRITE_ALL_FILE_FLAGS); |
| + // Grab a lock to avoid chrome truncating the file |
| + // underneath us. Keep the file locked as briefly as possible. |
| + if (HANDLE_EINTR(flock(file_descriptor, LOCK_EX)) < 0) { |
| + DLOG(ERROR) << "error locking" << filename << " : " << errno; |
| + close(file_descriptor); |
| + return false; |
| + } |
| + |
| + 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; |
| + } |
| + |
| + msg = std::string(reinterpret_cast<char*>(&size), sizeof(int32_t)) + msg; |
| + |
| + bool success = true; |
| + if (base::WriteFileDescriptor(file_descriptor, msg.c_str(), size) != size) { |
| + DLOG(ERROR) << "error writing to " << filename << " : " << errno; |
| + success = false; |
| + } |
| + |
| + // Close the file and release the |
| + close(file_descriptor); |
|
Alexei Svitkine (slow)
2014/05/13 15:36:26
Can you use ScopedFD from base/files/scoped_file.h
bsimonnet
2014/05/13 17:59:14
Done.
|
| + return success; |
| +} |
| + |
| +void MetricsUtils::ReadAndTruncateMetricsFromFile( |
| + const std::string& filename, |
| + ScopedVector<MetricSample>* metrics) { |
| + struct stat stat_buf; |
| + int result; |
| + |
| + result = stat(filename.c_str(), &stat_buf); |
| + if (result < 0) { |
| + if (errno != ENOENT) { |
| + DPLOG(ERROR) << filename << ": bad metrics file stat"; |
| + } |
| + // Nothing to collect---try later. |
| + return; |
| + } |
| + if (stat_buf.st_size == 0) { |
| + // Also nothing to collect. |
| + return; |
| + } |
| + int fd = open(filename.c_str(), O_RDWR); |
|
Alexei Svitkine (slow)
2014/05/13 15:36:26
Can you use ScopedFD from base/files/scoped_file.h
bsimonnet
2014/05/13 17:59:14
Done.
|
| + if (fd < 0) { |
| + DPLOG(ERROR) << filename << ": cannot open"; |
| + return; |
| + } |
| + result = flock(fd, LOCK_EX); |
| + if (result < 0) { |
| + DPLOG(ERROR) << filename << ": cannot lock"; |
| + close(fd); |
| + return; |
| + } |
| + // This processes all messages in the log. Each message starts with a 4-byte |
| + // field containing the length of the entire message. The length is followed |
| + // by a name-value pair of null-terminated strings. When all messages are |
| + // read and processed, or an error occurs, truncate the file to zero size. |
| + for (;;) { |
| + int32 message_size; |
| + result = HANDLE_EINTR(read(fd, &message_size, sizeof(message_size))); |
| + if (result < 0) { |
| + DPLOG(ERROR) << "reading metrics message header"; |
| + break; |
| + } |
| + if (result == 0) { // This indicates a normal EOF. |
| + break; |
| + } |
| + if (result < static_cast<int>(sizeof(message_size))) { |
| + DLOG(ERROR) << "bad read size " << result << ", expecting " |
| + << sizeof(message_size); |
| + break; |
| + } |
| + // kMessageMaxLength 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))) { |
| + DLOG(ERROR) << "bad message size " << message_size; |
| + break; |
| + } |
| + |
| + if (message_size > kMessageMaxLength) { |
| + DLOG(ERROR) << "message too long : " << message_size; |
| + if (HANDLE_EINTR(lseek(fd, message_size - 4, SEEK_CUR)) == -1) { |
| + DLOG(ERROR) << "error while skipping message. abort"; |
| + break; |
| + } |
| + continue; |
| + } |
| + |
| + message_size -= sizeof(message_size); // The message size includes itself. |
| + char buffer[kMessageMaxLength]; |
| + result = HANDLE_EINTR(read(fd, buffer, message_size)); |
| + if (result < 0) { |
| + DPLOG(ERROR) << "reading metrics message body"; |
| + break; |
| + } |
| + if (result < message_size) { |
| + DLOG(ERROR) << "message too short: length " << result << ", expected " |
| + << message_size; |
| + break; |
| + } |
| + |
| + metrics->push_back(ReadSample(std::string(buffer, message_size)).release()); |
| + } |
| + |
| + result = ftruncate(fd, 0); |
| + if (result < 0) { |
| + DPLOG(ERROR) << "truncate metrics log"; |
| + } |
| + result = flock(fd, LOCK_UN); |
| + if (result < 0) { |
| + DPLOG(ERROR) << "unlock metrics log"; |
| + } |
| + result = close(fd); |
| + if (result < 0) { |
| + DPLOG(ERROR) << "close metrics log"; |
| + } |
| +} |
| + |
| +} // namespace metrics |