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/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, | |
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_t size = msg.length() + sizeof(int32_t); | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
Move |size| to right before it's used, i.e. line 8
bsimonnet
2014/05/13 21:28:00
Actually I should use size below.
| |
81 if (msg.length() > size_t(kMessageMaxLength)) { | |
82 DLOG(ERROR) << "cannot write message: too long"; | |
83 return false; | |
84 } | |
85 | |
86 msg = std::string(reinterpret_cast<char*>(&size), sizeof(int32_t)) + msg; | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
Can you use two WriteFileDescriptor() calls instea
bsimonnet
2014/05/13 21:28:00
Done
Alexei Svitkine (slow)
2014/05/13 21:30:42
I see. Are there actual big-endian ChromeOS device
Luigi Semenzato
2014/05/13 21:36:35
The producer and the consumer of this data are run
bsimonnet
2014/05/13 21:49:56
Benchan mentioned that there are no big endian mac
| |
87 | |
88 if (base::WriteFileDescriptor(file_descriptor.get(), msg.c_str(), size) != | |
89 size) { | |
90 DLOG(ERROR) << "error writing to " << filename << " : " << errno; | |
91 return false; | |
92 } | |
93 | |
94 return true; | |
95 } | |
96 | |
97 void MetricsUtils::ReadAndTruncateMetricsFromFile( | |
98 const std::string& filename, | |
99 ScopedVector<MetricSample>* metrics) { | |
100 struct stat stat_buf; | |
101 int result; | |
102 | |
103 result = stat(filename.c_str(), &stat_buf); | |
104 if (result < 0) { | |
105 if (errno != ENOENT) { | |
106 DPLOG(ERROR) << filename << ": bad metrics file stat"; | |
107 } | |
108 // Nothing to collect---try later. | |
109 return; | |
110 } | |
111 if (stat_buf.st_size == 0) { | |
112 // Also nothing to collect. | |
113 return; | |
114 } | |
115 base::ScopedFD fd(open(filename.c_str(), O_RDWR)); | |
116 if (fd.get() < 0) { | |
117 DPLOG(ERROR) << filename << ": cannot open"; | |
118 return; | |
119 } | |
120 result = flock(fd.get(), LOCK_EX); | |
121 if (result < 0) { | |
122 DPLOG(ERROR) << filename << ": cannot lock"; | |
123 return; | |
124 } | |
125 // This processes all messages in the log. Each message starts with a 4-byte | |
126 // field containing the length of the entire message. The length is followed | |
127 // by a name-value pair of null-terminated strings. When all messages are | |
128 // read and processed, or an error occurs, truncate the file to zero size. | |
129 for (;;) { | |
130 int32 message_size; | |
131 result = HANDLE_EINTR(read(fd.get(), &message_size, sizeof(message_size))); | |
132 if (result < 0) { | |
133 DPLOG(ERROR) << "reading metrics message header"; | |
134 break; | |
135 } | |
136 if (result == 0) { // This indicates a normal EOF. | |
137 break; | |
138 } | |
139 if (result < static_cast<int>(sizeof(message_size))) { | |
140 DLOG(ERROR) << "bad read size " << result << ", expecting " | |
141 << sizeof(message_size); | |
142 break; | |
143 } | |
144 // kMessageMaxLength applies to the entire message: the 4-byte | |
145 // length field and the two null-terminated strings. | |
146 if (message_size < 2 + static_cast<int>(sizeof(message_size))) { | |
147 DLOG(ERROR) << "bad message size " << message_size; | |
148 break; | |
149 } | |
150 | |
151 if (message_size > kMessageMaxLength) { | |
152 DLOG(ERROR) << "message too long : " << message_size; | |
153 if (HANDLE_EINTR(lseek(fd.get(), message_size - 4, SEEK_CUR)) == -1) { | |
154 DLOG(ERROR) << "error while skipping message. abort"; | |
155 break; | |
156 } | |
157 continue; | |
158 } | |
159 | |
160 message_size -= sizeof(message_size); // The message size includes itself. | |
161 char buffer[kMessageMaxLength]; | |
162 result = HANDLE_EINTR(read(fd.get(), buffer, message_size)); | |
163 if (result < 0) { | |
164 DPLOG(ERROR) << "reading metrics message body"; | |
165 break; | |
166 } | |
167 if (result < message_size) { | |
168 DLOG(ERROR) << "message too short: length " << result << ", expected " | |
169 << message_size; | |
170 break; | |
171 } | |
172 | |
173 metrics->push_back(ReadSample(std::string(buffer, message_size)).release()); | |
174 } | |
175 | |
176 result = ftruncate(fd.get(), 0); | |
177 if (result < 0) { | |
178 DPLOG(ERROR) << "truncate metrics log"; | |
179 } | |
180 result = flock(fd.get(), LOCK_UN); | |
181 if (result < 0) { | |
182 DPLOG(ERROR) << "unlock metrics log"; | |
183 } | |
184 } | |
185 | |
186 } // namespace metrics | |
OLD | NEW |