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

Side by Side Diff: components/metrics/metrics_utils_chromeos.cc

Issue 227873002: Create a histogram serialization mechanism in components/metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moving to components instead of base. Adding sanity checks. Fixing lint 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/metrics_utils_chromeos.h"
6
7 #include <string>
8
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "components/metrics/crash_sample_chromeos.h"
16 #include "components/metrics/histogram_sample_chromeos.h"
17 #include "components/metrics/linearhistogram_sample_chromeos.h"
18 #include "components/metrics/metric_sample_chromeos.h"
19 #include "components/metrics/sparsehistogram_sample_chromeos.h"
20 #include "components/metrics/useraction_sample_chromeos.h"
21
22 using base::File;
23 using base::FilePath;
24 using base::SplitString;
25 using std::string;
26 using std::vector;
27
28 namespace metrics {
29
30 MetricSample* MetricsUtils::ReadSample(const string& sample) {
31 vector<string> vector;
32 SplitString(sample, '\0', &vector);
33 // We should have a two null terminated strings so split should produce
34 // three chuncks.
35 if (vector.size() != 3) {
36 DLOG(ERROR) << "wrong length" << vector.size();
37 return NULL;
38 }
39 const string name = vector[0];
Ben Chan 2014/04/18 20:27:08 const string&
40 const string value = vector[1];
41
42 if (LowerCaseEqualsASCII(name, "crash")) {
43 return CrashSample::ReadCrash(value);
44 } else if (LowerCaseEqualsASCII(name, "histogram")) {
45 return HistogramSample::ReadHistogram(value);
46 } else if (LowerCaseEqualsASCII(name, "linearhistogram")) {
47 return LinearHistogramSample::ReadLinearHistogram(value);
48 } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) {
49 return SparseHistogramSample::ReadSparseHistogram(value);
50 } else if (LowerCaseEqualsASCII(name, "useraction")) {
51 return UserActionSample::ReadUserAction(value);
52 } else {
53 DLOG(ERROR) << "invalid event type: " << name;
Ben Chan 2014/04/18 20:27:08 would it be useful to print the value as well?
54 }
55 return NULL;
56 }
57
58 bool MetricsUtils::WriteMetricToFile(const MetricSample& sample,
59 const std::string& filename) {
60 if (!sample.IsValid()) return false;
61 File file =
62 File(FilePath(filename), File::FLAG_OPEN_ALWAYS | File::FLAG_APPEND);
63 File::Error err = file.Lock();
64 if (err != File::FILE_OK) {
65 DLOG(ERROR) << "could not lock the file";
66 return false;
67 }
68
69 const string msg = sample.ToString();
70 int32_t size = msg.length() + sizeof(int32_t);
71 if (msg.length() > size_t(kMessageMaxLength)) {
72 DLOG(ERROR) << "cannot write message: too long";
73 return false;
74 }
75 file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t));
76 file.WriteAtCurrentPos(msg.c_str(), msg.length());
77
78 return true;
79 }
80
81 void MetricsUtils::ReadAndTruncateMetricsFromFile(
82 const std::string& filename,
83 ScopedVector<MetricSample>* metrics) {
84 File file = File(FilePath(filename),
85 File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE);
86 File::Error err = file.Lock();
87 if (err != File::FILE_OK) {
88 DLOG(ERROR) << "could not lock the file";
89 return;
90 }
91 for (;;) {
92 int32 message_length;
93 int read = file.ReadAtCurrentPos(reinterpret_cast<char*>(&message_length),
94 sizeof(message_length));
95 if (read != sizeof(message_length)) break;
96
97 message_length -= sizeof(message_length);
98 if (message_length > kMessageMaxLength) {
99 DLOG(ERROR) << "cannot read message: too long";
100
101 // Skip the invalid message and continue reading.
102 if (file.Seek(File::FROM_CURRENT, message_length) != -1) continue;
103
104 DLOG(ERROR) << "cannot skip long message";
105 // We do not know the size of the message. The rest of the file is not
106 // readable. Truncate the file and exit.
107 break;
108 }
109 char serialized_sample[kMessageMaxLength];
110 read = file.ReadAtCurrentPos(serialized_sample, message_length);
111 if (read != message_length) {
112 DLOG(ERROR) << "could not read message" << read;
113 break;
114 }
115 metrics->push_back(ReadSample(string(serialized_sample, message_length)));
116 }
117 file.SetLength(0);
118 }
119
120 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698