Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(655)

Side by Side Diff: components/metrics/export/metrics_utils.cc

Issue 227873002: Create a histogram serialization mechanism in components/metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Convert to use scoped_ptr. Remove using declarations. Fixed linting. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/export/metrics_utils.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "components/metrics/export/crash_sample.h"
18 #include "components/metrics/export/histogram_sample.h"
19 #include "components/metrics/export/linearhistogram_sample.h"
20 #include "components/metrics/export/metric_sample.h"
21 #include "components/metrics/export/sparsehistogram_sample.h"
22 #include "components/metrics/export/useraction_sample.h"
23
24 namespace metrics {
25
26 scoped_ptr<MetricSample> MetricsUtils::ReadSample(const std::string& sample) {
27 std::vector<std::string> parts;
28 base::SplitString(sample, '\0', &parts);
29 // We should have a two null terminated strings so split should produce
30 // three chuncks.
31 if (parts.size() != 3) {
32 DLOG(ERROR) << "wrong length" << parts.size();
33 return scoped_ptr<MetricSample>();
34 }
35 const std::string& name = parts[0];
36 const std::string& value = parts[1];
37
38 if (LowerCaseEqualsASCII(name, "crash")) {
39 return CrashSample::ReadCrash(value).PassAs<MetricSample>();
40 } else if (LowerCaseEqualsASCII(name, "histogram")) {
41 return HistogramSample::ReadHistogram(value).PassAs<MetricSample>();
42 } else if (LowerCaseEqualsASCII(name, "linearhistogram")) {
43 return LinearHistogramSample::ReadLinearHistogram(value)
44 .PassAs<MetricSample>();
45 } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) {
46 return SparseHistogramSample::ReadSparseHistogram(value)
47 .PassAs<MetricSample>();
48 } else if (LowerCaseEqualsASCII(name, "useraction")) {
49 return UserActionSample::ReadUserAction(value).PassAs<MetricSample>();
50 } else {
51 DLOG(ERROR) << "invalid event type: " << name << ", value: " << value;
52 }
53 return scoped_ptr<MetricSample>();
54 }
55
56 bool MetricsUtils::WriteMetricToFile(const MetricSample& sample,
57 const std::string& filename) {
58 if (!sample.IsValid())
59 return false;
60 base::File file(base::FilePath(filename),
61 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND);
62 base::File::Error err = file.Lock();
63 if (err != base::File::FILE_OK) {
64 DLOG(ERROR) << "could not lock the file";
65 return false;
66 }
67
68 const std::string msg = sample.ToString();
69 int32_t size = msg.length() + sizeof(int32_t);
70 if (msg.length() > size_t(kMessageMaxLength)) {
71 DLOG(ERROR) << "cannot write message: too long";
72 return false;
73 }
74 file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t));
75 file.WriteAtCurrentPos(msg.c_str(), msg.length());
76
77 return true;
78 }
79
80 void MetricsUtils::ReadAndTruncateMetricsFromFile(
81 const std::string& filename,
82 ScopedVector<MetricSample>* metrics) {
83 base::File file = base::File(
Alexei Svitkine (slow) 2014/05/01 15:57:09 Nit: Just do base::File file(...);
bsimonnet 2014/05/01 20:34:21 Done.
84 base::FilePath(filename),
85 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE);
86 base::File::Error err = file.Lock();
87 if (err != base::File::FILE_OK) {
88 DLOG(ERROR) << "could not lock the file";
89 return;
90 }
91 for (;;) {
92 int32 message_length;
93 int read = file.ReadAtCurrentPos(reinterpret_cast<char*>(&message_length),
94 sizeof(message_length));
95 if (read != sizeof(message_length)) break;
Alexei Svitkine (slow) 2014/05/01 15:57:09 Nit: Put on next line.
bsimonnet 2014/05/01 20:34:21 Done.
96
97 message_length -= sizeof(message_length);
98 if (message_length > kMessageMaxLength) {
99 DLOG(ERROR) << "cannot read message: too long";
100
101 // Skip the invalid message and continue reading.
102 if (file.Seek(base::File::FROM_CURRENT, message_length) != -1) continue;
103
104 DLOG(ERROR) << "cannot skip long message";
105 // We do not know the size of the message. The rest of the file is not
106 // readable. Truncate the file and exit.
107 break;
108 }
109 char serialized_sample[kMessageMaxLength];
110 read = file.ReadAtCurrentPos(serialized_sample, message_length);
111 if (read != message_length) {
112 DLOG(ERROR) << "could not read message" << read;
113 break;
114 }
115 metrics->push_back(
116 ReadSample(std::string(serialized_sample, message_length)).release());
117 }
118 file.SetLength(0);
119 }
120
121 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698