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

Side by Side Diff: components/metrics/chromeos/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: Update components_test.gyp to match metrics_export. 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/chromeos/metrics_utils.h"
6
7 #include <sys/file.h>
8
9 #include <string>
10 #include <vector>
11
12 #include "base/file_util.h"
13 #include "base/files/file_path.h"
14 #include "base/files/scoped_file.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/scoped_vector.h"
18 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h"
20 #include "components/metrics/chromeos/metric_sample.h"
21
22 #define READ_WRITE_ALL_FILE_FLAGS \
23 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
24
25 namespace metrics {
26
27 scoped_ptr<MetricSample> MetricsUtils::ReadSample(const std::string& sample) {
28 std::vector<std::string> parts;
29 base::SplitString(sample, '\0', &parts);
30 // We should have two null terminated strings so split should produce
31 // three chunks.
32 if (parts.size() != 3) {
33 DLOG(ERROR) << "splitting message on \\0 produced " << parts.size()
34 << " parts (expected 3)";
35 return scoped_ptr<MetricSample>();
36 }
37 const std::string& name = parts[0];
38 const std::string& value = parts[1];
39
40 if (LowerCaseEqualsASCII(name, "crash")) {
41 return MetricSample::CrashSample(value);
42 } else if (LowerCaseEqualsASCII(name, "histogram")) {
43 return MetricSample::ReadHistogram(value);
44 } else if (LowerCaseEqualsASCII(name, "linearhistogram")) {
45 return MetricSample::ReadLinearHistogram(value);
46 } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) {
47 return MetricSample::ReadSparseHistogram(value);
48 } else if (LowerCaseEqualsASCII(name, "useraction")) {
49 return MetricSample::UserActionSample(value);
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,
Alexei Svitkine (slow) 2014/05/15 13:43:26 Can this take a const& sample?
bsimonnet 2014/05/15 19:23:40 Done.
57 const std::string& filename) {
58 if (!sample->IsValid())
59 return false;
60
61 base::ScopedFD file_descriptor(open(filename.c_str(),
62 O_WRONLY | O_APPEND | O_CREAT,
63 READ_WRITE_ALL_FILE_FLAGS));
64
65 if (file_descriptor.get() < 0) {
66 DLOG(ERROR) << "error openning the file";
67 return false;
68 }
69
70 fchmod(file_descriptor.get(), READ_WRITE_ALL_FILE_FLAGS);
71 // Grab a lock to avoid chrome truncating the file
72 // underneath us. Keep the file locked as briefly as possible.
73 // Freeing file_descriptor will close the file and and remove the lock.
74 if (HANDLE_EINTR(flock(file_descriptor.get(), LOCK_EX)) < 0) {
75 DLOG(ERROR) << "error locking" << filename << " : " << errno;
76 return false;
77 }
78
79 std::string msg = sample->ToString();
80 int32 size = msg.length() + sizeof(int32);
81 if (size > kMessageMaxLength) {
82 DLOG(ERROR) << "cannot write message: too long";
83 return false;
84 }
85
86 // The file containing the metrics samples will only be read by programs on
87 // the same device so we do not check endianness.
88 if (base::WriteFileDescriptor(file_descriptor.get(),
89 reinterpret_cast<char*>(&size),
90 sizeof(size)) != sizeof(size)) {
91 DLOG(ERROR) << "error writing message length " << errno;
92 return false;
93 }
94
95 if (base::WriteFileDescriptor(
96 file_descriptor.get(), msg.c_str(), msg.length()) !=
97 static_cast<int>(msg.length())) {
98 DLOG(ERROR) << "error writing message" << errno;
99 return false;
100 }
101
102 return true;
103 }
104
105 void MetricsUtils::ReadAndTruncateMetricsFromFile(
106 const std::string& filename,
107 ScopedVector<MetricSample>* metrics) {
108 struct stat stat_buf;
109 int result;
110
111 result = stat(filename.c_str(), &stat_buf);
112 if (result < 0) {
113 if (errno != ENOENT) {
Alexei Svitkine (slow) 2014/05/15 13:43:26 Nit: No {}'s
bsimonnet 2014/05/15 19:23:40 Done.
114 DPLOG(ERROR) << filename << ": bad metrics file stat";
115 }
116 // Nothing to collect---try later.
117 return;
118 }
119 if (stat_buf.st_size == 0) {
120 // Also nothing to collect.
121 return;
122 }
123 base::ScopedFD fd(open(filename.c_str(), O_RDWR));
124 if (fd.get() < 0) {
125 DPLOG(ERROR) << filename << ": cannot open";
126 return;
127 }
128 result = flock(fd.get(), LOCK_EX);
129 if (result < 0) {
130 DPLOG(ERROR) << filename << ": cannot lock";
131 return;
132 }
133 // This processes all messages in the log. Each message starts with a 4-byte
Alexei Svitkine (slow) 2014/05/15 13:43:26 Nit: Add an empty line before this.
bsimonnet 2014/05/15 19:23:40 Done.
134 // field containing the length of the entire message. The length is followed
135 // by a name-value pair of null-terminated strings. When all messages are
136 // read and processed, or an error occurs, truncate the file to zero size.
137 for (;;) {
138 int32 message_size;
139 // The file containing the metrics do not leave the device so the writer and
140 // the reader will always have the same endianness.
141 result = HANDLE_EINTR(read(fd.get(), &message_size, sizeof(message_size)));
142 if (result < 0) {
143 DPLOG(ERROR) << "reading metrics message header";
144 break;
145 }
146 if (result == 0) { // This indicates a normal EOF.
Alexei Svitkine (slow) 2014/05/15 13:43:26 Nit: No {}'s. Or Keep {}'s and move comment to the
bsimonnet 2014/05/15 19:23:40 Done.
147 break;
148 }
149 if (result < static_cast<int>(sizeof(message_size))) {
150 DLOG(ERROR) << "bad read size " << result << ", expecting "
151 << sizeof(message_size);
152 break;
153 }
154 // kMessageMaxLength applies to the entire message: the 4-byte
Alexei Svitkine (slow) 2014/05/15 13:43:26 This comment seems misplaced since the check direc
bsimonnet 2014/05/15 19:23:40 Done.
155 // length field and the two null-terminated strings.
156 if (message_size < 2 + static_cast<int>(sizeof(message_size))) {
Alexei Svitkine (slow) 2014/05/15 13:43:26 Do we really need the magic number 2 here? It seem
bsimonnet 2014/05/15 19:23:40 Done.
157 DLOG(ERROR) << "bad message size " << message_size;
158 break;
159 }
160
161 if (message_size > kMessageMaxLength) {
162 DLOG(ERROR) << "message too long : " << message_size;
163 if (HANDLE_EINTR(lseek(fd.get(), message_size - 4, SEEK_CUR)) == -1) {
164 DLOG(ERROR) << "error while skipping message. abort";
165 break;
166 }
167 continue;
168 }
169
170 message_size -= sizeof(message_size); // The message size includes itself.
171 char buffer[kMessageMaxLength];
172 result = HANDLE_EINTR(read(fd.get(), buffer, message_size));
173 if (result < 0) {
174 DPLOG(ERROR) << "reading metrics message body";
175 break;
176 }
177 if (result < message_size) {
178 DLOG(ERROR) << "message too short: length " << result << ", expected "
179 << message_size;
180 break;
181 }
Alexei Svitkine (slow) 2014/05/15 13:43:26 Nit: Consider making a helper function for reading
bsimonnet 2014/05/15 19:23:40 Done.
182 scoped_ptr<MetricSample> sample =
183 ReadSample(std::string(buffer, message_size));
184 if (sample.get() == NULL) {
Alexei Svitkine (slow) 2014/05/15 13:43:26 Nit: just !sample and remove {}'s
bsimonnet 2014/05/15 19:23:40 Done.
185 break;
186 }
187 metrics->push_back(sample.release());
188 }
189
190 result = ftruncate(fd.get(), 0);
191 if (result < 0) {
192 DPLOG(ERROR) << "truncate metrics log";
193 }
194 result = flock(fd.get(), LOCK_UN);
195 if (result < 0) {
196 DPLOG(ERROR) << "unlock metrics log";
197 }
198 }
199
200 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698