OLD | NEW |
---|---|
(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/serialization_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> SerializationUtils::ParseSample( | |
28 const std::string& sample) { | |
29 if (sample.empty()) | |
30 return scoped_ptr<MetricSample>(); | |
31 | |
32 std::vector<std::string> parts; | |
33 base::SplitString(sample, '\0', &parts); | |
34 // We should have two null terminated strings so split should produce | |
35 // three chunks. | |
36 if (parts.size() != 3) { | |
37 DLOG(ERROR) << "splitting message on \\0 produced " << parts.size() | |
38 << " parts (expected 3)"; | |
39 return scoped_ptr<MetricSample>(); | |
40 } | |
41 const std::string& name = parts[0]; | |
42 const std::string& value = parts[1]; | |
43 | |
44 if (LowerCaseEqualsASCII(name, "crash")) { | |
45 return MetricSample::CrashSample(value); | |
46 } else if (LowerCaseEqualsASCII(name, "histogram")) { | |
47 return MetricSample::ParseHistogram(value); | |
48 } else if (LowerCaseEqualsASCII(name, "linearhistogram")) { | |
49 return MetricSample::ParseLinearHistogram(value); | |
50 } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) { | |
51 return MetricSample::ParseSparseHistogram(value); | |
52 } else if (LowerCaseEqualsASCII(name, "useraction")) { | |
53 return MetricSample::UserActionSample(value); | |
54 } else { | |
55 DLOG(ERROR) << "invalid event type: " << name << ", value: " << value; | |
56 } | |
57 return scoped_ptr<MetricSample>(); | |
58 } | |
59 | |
60 void SerializationUtils::ReadAndTruncateMetricsFromFile( | |
61 const std::string& filename, | |
62 ScopedVector<MetricSample>* metrics) { | |
63 struct stat stat_buf; | |
64 int result; | |
65 | |
66 result = stat(filename.c_str(), &stat_buf); | |
67 if (result < 0) { | |
68 if (errno != ENOENT) | |
69 DPLOG(ERROR) << filename << ": bad metrics file stat"; | |
70 | |
71 // Nothing to collect---try later. | |
72 return; | |
73 } | |
74 if (stat_buf.st_size == 0) { | |
75 // Also nothing to collect. | |
76 return; | |
77 } | |
78 base::ScopedFD fd(open(filename.c_str(), O_RDWR)); | |
79 if (fd.get() < 0) { | |
80 DPLOG(ERROR) << filename << ": cannot open"; | |
81 return; | |
82 } | |
83 result = flock(fd.get(), LOCK_EX); | |
84 if (result < 0) { | |
85 DPLOG(ERROR) << filename << ": cannot lock"; | |
86 return; | |
87 } | |
88 | |
89 // This processes all messages in the log. When all messages are | |
90 // read and processed, or an error occurs, truncate the file to zero size. | |
91 for (;;) { | |
92 std::string message; | |
93 | |
94 if (!ReadMessage(fd.get(), &message)) | |
95 break; | |
96 | |
97 scoped_ptr<MetricSample> sample = ParseSample(message); | |
98 if (sample) | |
99 metrics->push_back(sample.release()); | |
100 } | |
101 | |
102 result = ftruncate(fd.get(), 0); | |
103 if (result < 0) | |
104 DPLOG(ERROR) << "truncate metrics log"; | |
105 | |
106 result = flock(fd.get(), LOCK_UN); | |
107 if (result < 0) | |
108 DPLOG(ERROR) << "unlock metrics log"; | |
109 } | |
110 | |
111 bool SerializationUtils::ReadMessage(int file_descriptor, | |
Alexei Svitkine (slow)
2014/05/15 19:41:44
Nit: Put this function into an anon namespace at t
bsimonnet
2014/05/15 21:19:42
Done.
| |
112 std::string* message) { | |
113 int result; | |
114 int32 message_size; | |
115 // The file containing the metrics do not leave the device so the writer and | |
116 // the reader will always have the same endianness. | |
117 result = | |
118 HANDLE_EINTR(read(file_descriptor, &message_size, sizeof(message_size))); | |
119 if (result < 0) { | |
120 DPLOG(ERROR) << "reading metrics message header"; | |
121 return false; | |
122 } | |
123 if (result == 0) { | |
124 // This indicates a normal EOF. | |
125 return false; | |
126 } | |
127 if (result < static_cast<int>(sizeof(message_size))) { | |
128 DLOG(ERROR) << "bad read size " << result << ", expecting " | |
129 << sizeof(message_size); | |
130 return false; | |
131 } | |
132 | |
133 // kMessageMaxLength applies to the entire message: the 4-byte | |
134 // length field and the content. | |
135 if (message_size > kMessageMaxLength) { | |
136 DLOG(ERROR) << "message too long : " << message_size; | |
137 if (HANDLE_EINTR(lseek(file_descriptor, message_size - 4, SEEK_CUR)) == | |
Alexei Svitkine (slow)
2014/05/15 19:41:44
Nit: I think changing |file_descriptor| -> |fd| wi
bsimonnet
2014/05/15 21:19:42
Done.
| |
138 -1) { | |
139 DLOG(ERROR) << "error while skipping message. abort"; | |
140 return false; | |
141 } | |
142 *message = ""; | |
Alexei Svitkine (slow)
2014/05/15 19:41:44
Nit: This isn't needed, right?
bsimonnet
2014/05/15 20:50:21
Not in the current format, this is meant to be a p
bsimonnet
2014/05/15 21:19:42
Imposing constraint on the pointer used to store t
Alexei Svitkine (slow)
2014/05/15 22:40:50
Sorry, my comment was actually incorrect - I had m
| |
143 return true; | |
144 } | |
145 | |
146 message_size -= sizeof(message_size); // The message size includes itself. | |
147 char buffer[kMessageMaxLength]; | |
148 if (!base::ReadFromFD(file_descriptor, buffer, message_size)) { | |
149 DPLOG(ERROR) << "reading metrics message body"; | |
150 return false; | |
151 } | |
152 *message = std::string(buffer, message_size); | |
153 return true; | |
154 } | |
155 | |
156 bool SerializationUtils::WriteMetricToFile(const MetricSample& sample, | |
157 const std::string& filename) { | |
158 if (!sample.IsValid()) | |
159 return false; | |
160 | |
161 base::ScopedFD file_descriptor(open(filename.c_str(), | |
162 O_WRONLY | O_APPEND | O_CREAT, | |
163 READ_WRITE_ALL_FILE_FLAGS)); | |
164 | |
165 if (file_descriptor.get() < 0) { | |
166 DLOG(ERROR) << "error openning the file"; | |
167 return false; | |
168 } | |
169 | |
170 fchmod(file_descriptor.get(), READ_WRITE_ALL_FILE_FLAGS); | |
171 // Grab a lock to avoid chrome truncating the file | |
172 // underneath us. Keep the file locked as briefly as possible. | |
173 // Freeing file_descriptor will close the file and and remove the lock. | |
174 if (HANDLE_EINTR(flock(file_descriptor.get(), LOCK_EX)) < 0) { | |
175 DLOG(ERROR) << "error locking" << filename << " : " << errno; | |
176 return false; | |
177 } | |
178 | |
179 std::string msg = sample.ToString(); | |
180 int32 size = msg.length() + sizeof(int32); | |
181 if (size > kMessageMaxLength) { | |
182 DLOG(ERROR) << "cannot write message: too long"; | |
183 return false; | |
184 } | |
185 | |
186 // The file containing the metrics samples will only be read by programs on | |
187 // the same device so we do not check endianness. | |
188 if (base::WriteFileDescriptor(file_descriptor.get(), | |
189 reinterpret_cast<char*>(&size), | |
190 sizeof(size)) != sizeof(size)) { | |
191 DLOG(ERROR) << "error writing message length " << errno; | |
192 return false; | |
193 } | |
194 | |
195 if (base::WriteFileDescriptor( | |
196 file_descriptor.get(), msg.c_str(), msg.length()) != | |
197 static_cast<int>(msg.length())) { | |
198 DLOG(ERROR) << "error writing message" << errno; | |
199 return false; | |
200 } | |
201 | |
202 return true; | |
203 } | |
204 | |
205 } // namespace metrics | |
OLD | NEW |