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

Side by Side Diff: base/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: Fixing clang complains about override keyword. 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 (c) 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 "base/metrics/metrics_utils_chromeos.h"
6
7 #include <fcntl.h>
Alexei Svitkine (slow) 2014/04/17 16:33:21 Are all these system includes needed?
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13
14 #include <list>
15 #include <memory>
16 #include <string>
17
18 #include "base/file_util.h"
19 #include "base/files/file_path.h"
20 #include "base/logging.h"
21 #include "base/memory/scoped_vector.h"
22 #include "base/metrics/crash_sample_chromeos.h"
23 #include "base/metrics/histogram_sample_chromeos.h"
24 #include "base/metrics/linearhistogram_sample_chromeos.h"
25 #include "base/metrics/metric_sample_chromeos.h"
26 #include "base/metrics/sparsehistogram_sample_chromeos.h"
27 #include "base/metrics/useraction_sample_chromeos.h"
28 #include "base/strings/string_split.h"
29 #include "base/strings/string_util.h"
30
31 using std::string;
32 using std::vector;
33
34 namespace base {
35
36 MetricSample* MetricsUtils::ReadSample(const string& sample) {
37 vector<string> vector;
38 SplitString(sample, '\0', &vector);
39 // We should have a two null terminated strings so split should produce
40 // three chuncks.
41 if (vector.size() != 3) {
42 DLOG(ERROR) << "wrong length" << vector.size();
43 return NULL;
44 }
45 const string name = vector[0];
46 const string value = vector[1];
47
48 if (LowerCaseEqualsASCII(name, "crash")) {
49 return CrashSample::ReadCrash(value);
50 } else if (LowerCaseEqualsASCII(name, "histogram")) {
51 return HistogramSample::ReadHistogram(value);
52 } else if (LowerCaseEqualsASCII(name, "linearhistogram")) {
53 return LinearHistogramSample::ReadLinearHistogram(value);
54 } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) {
55 return SparseHistogramSample::ReadSparseHistogram(value);
56 } else if (LowerCaseEqualsASCII(name, "useraction")) {
57 return UserActionSample::ReadUserAction(value);
58 } else {
59 DLOG(ERROR) << "invalid event type: " << name;
60 }
61 return NULL;
62 }
63
64 bool MetricsUtils::WriteMetricToFile(const MetricSample& sample,
65 const std::string& filename) {
66 if (!sample.IsValid()) {
67 return false;
68 }
69 File file =
70 File(FilePath(filename), File::FLAG_OPEN_ALWAYS | File::FLAG_APPEND);
71 File::Error err = file.Lock();
72 if (err != File::FILE_OK) {
73 DLOG(ERROR) << "could not lock the file";
74 return false;
75 }
76
77 const string msg = sample.ToString();
78 int32_t size = msg.length() + sizeof(int32_t);
achaulk 2014/04/17 16:45:18 Check that msg.length() <= kMaxMessageLength to av
79 file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t));
80 file.WriteAtCurrentPos(msg.c_str(), msg.length());
81
82 return true;
83 }
84
85 void MetricsUtils::ReadAndTruncateMetricsFromFile(
86 const std::string& filename,
87 ScopedVector<MetricSample>* metrics) {
88 File file = File(FilePath(filename),
89 File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE);
90 File::Error err = file.Lock();
91 if (err != File::FILE_OK) {
92 DLOG(ERROR) << "could not lock the file";
93 return;
94 }
95 for (;;) {
96 int32 message_length;
97 int read = file.ReadAtCurrentPos(reinterpret_cast<char*>(&message_length),
98 sizeof(message_length));
99 if (read != sizeof(message_length)) {
100 break;
101 }
102
103 message_length -= sizeof(message_length);
104 char serialized_sample[kMessageMaxLength];
achaulk 2014/04/17 16:45:18 Sanity check that message_length <= kMessageMaxLen
105 read = file.ReadAtCurrentPos(serialized_sample, message_length);
106 if (read != message_length) {
107 DLOG(ERROR) << "could not read message" << read;
108 break;
109 }
110 metrics->push_back(ReadSample(string(serialized_sample, message_length)));
111 }
112 file.SetLength(0);
113 }
114 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698