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

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: Rewriting the file manipulation to using blocking locks (using flock) 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/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "components/metrics/chromeos/metric_sample.h"
20
21 #define READ_WRITE_ALL_FILE_FLAGS \
22 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
23 #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
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(name);
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(name);
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
61 int file_descriptor = HANDLE_EINTR(open(filename.c_str(),
62 O_WRONLY | O_APPEND | O_CREAT,
63 READ_WRITE_ALL_FILE_FLAGS));
64
65 if (file_descriptor < 0) {
66 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.
67 }
68
69 fchmod(file_descriptor, READ_WRITE_ALL_FILE_FLAGS);
70 // Grab a lock to avoid chrome truncating the file
71 // underneath us. Keep the file locked as briefly as possible.
72 if (HANDLE_EINTR(flock(file_descriptor, LOCK_EX)) < 0) {
73 DLOG(ERROR) << "error locking" << filename << " : " << errno;
74 close(file_descriptor);
75 return false;
76 }
77
78 std::string msg = sample->ToString();
79 int32_t size = msg.length() + sizeof(int32_t);
80 if (msg.length() > size_t(kMessageMaxLength)) {
81 DLOG(ERROR) << "cannot write message: too long";
82 return false;
83 }
84
85 msg = std::string(reinterpret_cast<char*>(&size), sizeof(int32_t)) + msg;
86
87 bool success = true;
88 if (base::WriteFileDescriptor(file_descriptor, msg.c_str(), size) != size) {
89 DLOG(ERROR) << "error writing to " << filename << " : " << errno;
90 success = false;
91 }
92
93 // Close the file and release the
94 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.
95 return success;
96 }
97
98 void MetricsUtils::ReadAndTruncateMetricsFromFile(
99 const std::string& filename,
100 ScopedVector<MetricSample>* metrics) {
101 struct stat stat_buf;
102 int result;
103
104 result = stat(filename.c_str(), &stat_buf);
105 if (result < 0) {
106 if (errno != ENOENT) {
107 DPLOG(ERROR) << filename << ": bad metrics file stat";
108 }
109 // Nothing to collect---try later.
110 return;
111 }
112 if (stat_buf.st_size == 0) {
113 // Also nothing to collect.
114 return;
115 }
116 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.
117 if (fd < 0) {
118 DPLOG(ERROR) << filename << ": cannot open";
119 return;
120 }
121 result = flock(fd, LOCK_EX);
122 if (result < 0) {
123 DPLOG(ERROR) << filename << ": cannot lock";
124 close(fd);
125 return;
126 }
127 // This processes all messages in the log. Each message starts with a 4-byte
128 // field containing the length of the entire message. The length is followed
129 // by a name-value pair of null-terminated strings. When all messages are
130 // read and processed, or an error occurs, truncate the file to zero size.
131 for (;;) {
132 int32 message_size;
133 result = HANDLE_EINTR(read(fd, &message_size, sizeof(message_size)));
134 if (result < 0) {
135 DPLOG(ERROR) << "reading metrics message header";
136 break;
137 }
138 if (result == 0) { // This indicates a normal EOF.
139 break;
140 }
141 if (result < static_cast<int>(sizeof(message_size))) {
142 DLOG(ERROR) << "bad read size " << result << ", expecting "
143 << sizeof(message_size);
144 break;
145 }
146 // kMessageMaxLength applies to the entire message: the 4-byte
147 // length field and the two null-terminated strings.
148 if (message_size < 2 + static_cast<int>(sizeof(message_size))) {
149 DLOG(ERROR) << "bad message size " << message_size;
150 break;
151 }
152
153 if (message_size > kMessageMaxLength) {
154 DLOG(ERROR) << "message too long : " << message_size;
155 if (HANDLE_EINTR(lseek(fd, message_size - 4, SEEK_CUR)) == -1) {
156 DLOG(ERROR) << "error while skipping message. abort";
157 break;
158 }
159 continue;
160 }
161
162 message_size -= sizeof(message_size); // The message size includes itself.
163 char buffer[kMessageMaxLength];
164 result = HANDLE_EINTR(read(fd, buffer, message_size));
165 if (result < 0) {
166 DPLOG(ERROR) << "reading metrics message body";
167 break;
168 }
169 if (result < message_size) {
170 DLOG(ERROR) << "message too short: length " << result << ", expected "
171 << message_size;
172 break;
173 }
174
175 metrics->push_back(ReadSample(std::string(buffer, message_size)).release());
176 }
177
178 result = ftruncate(fd, 0);
179 if (result < 0) {
180 DPLOG(ERROR) << "truncate metrics log";
181 }
182 result = flock(fd, LOCK_UN);
183 if (result < 0) {
184 DPLOG(ERROR) << "unlock metrics log";
185 }
186 result = close(fd);
187 if (result < 0) {
188 DPLOG(ERROR) << "close metrics log";
189 }
190 }
191
192 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698