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

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: Running tests only for chromeos. Created 6 years, 8 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_vector.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "components/metrics/export/crash_sample.h"
17 #include "components/metrics/export/histogram_sample.h"
18 #include "components/metrics/export/linearhistogram_sample.h"
19 #include "components/metrics/export/metric_sample.h"
20 #include "components/metrics/export/sparsehistogram_sample.h"
21 #include "components/metrics/export/useraction_sample.h"
22
23 using base::File;
24 using base::FilePath;
25 using base::SplitString;
26 using std::string;
27 using std::vector;
28
29 namespace metrics {
30
31 MetricSample* MetricsUtils::ReadSample(const string& sample) {
32 vector<string> vector;
33 SplitString(sample, '\0', &vector);
34 // We should have a two null terminated strings so split should produce
35 // three chuncks.
36 if (vector.size() != 3) {
37 DLOG(ERROR) << "wrong length" << vector.size();
38 return NULL;
39 }
40 const string& name = vector[0];
41 const string& value = vector[1];
42
43 if (LowerCaseEqualsASCII(name, "crash")) {
44 return CrashSample::ReadCrash(value);
45 } else if (LowerCaseEqualsASCII(name, "histogram")) {
46 return HistogramSample::ReadHistogram(value);
47 } else if (LowerCaseEqualsASCII(name, "linearhistogram")) {
48 return LinearHistogramSample::ReadLinearHistogram(value);
49 } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) {
50 return SparseHistogramSample::ReadSparseHistogram(value);
51 } else if (LowerCaseEqualsASCII(name, "useraction")) {
52 return UserActionSample::ReadUserAction(value);
53 } else {
54 DLOG(ERROR) << "invalid event type: " << name
55 << ", value: " << value;
56 }
57 return NULL;
58 }
59
60 bool MetricsUtils::WriteMetricToFile(const MetricSample& sample,
61 const std::string& filename) {
62 if (!sample.IsValid()) return false;
Alexei Svitkine (slow) 2014/04/30 15:25:24 Put return on next line.
bsimonnet 2014/04/30 20:28:40 Done.
63 File file =
64 File(FilePath(filename), File::FLAG_OPEN_ALWAYS | File::FLAG_APPEND);
Alexei Svitkine (slow) 2014/04/30 15:25:24 Nit: You can just do: File file(FilePath(filename
bsimonnet 2014/04/30 20:28:40 Done.
65 File::Error err = file.Lock();
66 if (err != File::FILE_OK) {
67 DLOG(ERROR) << "could not lock the file";
68 return false;
69 }
70
71 const string msg = sample.ToString();
72 int32_t size = msg.length() + sizeof(int32_t);
73 if (msg.length() > size_t(kMessageMaxLength)) {
74 DLOG(ERROR) << "cannot write message: too long";
75 return false;
76 }
77 file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t));
78 file.WriteAtCurrentPos(msg.c_str(), msg.length());
79
80 return true;
81 }
82
83 void MetricsUtils::ReadAndTruncateMetricsFromFile(
84 const std::string& filename,
85 ScopedVector<MetricSample>* metrics) {
86 File file = File(FilePath(filename),
87 File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE);
88 File::Error err = file.Lock();
89 if (err != File::FILE_OK) {
90 DLOG(ERROR) << "could not lock the file";
91 return;
92 }
93 for (;;) {
94 int32 message_length;
95 int read = file.ReadAtCurrentPos(reinterpret_cast<char*>(&message_length),
96 sizeof(message_length));
97 if (read != sizeof(message_length)) break;
98
99 message_length -= sizeof(message_length);
100 if (message_length > kMessageMaxLength) {
101 DLOG(ERROR) << "cannot read message: too long";
102
103 // Skip the invalid message and continue reading.
104 if (file.Seek(File::FROM_CURRENT, message_length) != -1) continue;
105
106 DLOG(ERROR) << "cannot skip long message";
107 // We do not know the size of the message. The rest of the file is not
108 // readable. Truncate the file and exit.
109 break;
110 }
111 char serialized_sample[kMessageMaxLength];
112 read = file.ReadAtCurrentPos(serialized_sample, message_length);
113 if (read != message_length) {
114 DLOG(ERROR) << "could not read message" << read;
115 break;
116 }
117 metrics->push_back(ReadSample(string(serialized_sample, message_length)));
118 }
119 file.SetLength(0);
120 }
121
122 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698