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

Side by Side Diff: base/metrics/chromeos_metrics.cc

Issue 227873002: Create a histogram serialization mechanism in components/metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactoring to use string instead of char buffers 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 <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/file.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11
12 #include <list>
13 #include <memory>
14 #include <string>
15
16 #include "base/file_util.h"
17 #include "base/files/file_path.h"
18 #include "base/logging.h"
19 #include "base/memory/scoped_vector.h"
20 #include "base/metrics/chromeos_metrics.h"
21 #include "base/metrics/crash_sample.h"
22 #include "base/metrics/histogram_sample.h"
23 #include "base/metrics/linearhistogram_sample.h"
24 #include "base/metrics/metric_sample.h"
25 #include "base/metrics/sparsehistogram_sample.h"
26 #include "base/metrics/useraction_sample.h"
27 #include "base/strings/string_split.h"
28 #include "base/strings/string_util.h"
29
30 using std::string;
31 using std::vector;
32
33 namespace base {
34 namespace ChromeOSMetrics {
35 MetricSample* ReadSample(const string& sample) {
36 vector<string> vector;
37 SplitString(sample, '\0', &vector);
38 // We should have a two null terminated strings so split should produce
39 // three chuncks.
40 if (vector.size() != 3) {
41 DLOG(ERROR) << "wrong length" << vector.size();
42 return NULL;
43 }
44 const string name = vector[0];
45 const string value = vector[1];
46
47 if (LowerCaseEqualsASCII(name, "crash")) {
48 return CrashSample::ReadCrash(value);
49 } else if (LowerCaseEqualsASCII(name, "histogram")) {
50 return HistogramSample::ReadHistogram(value);
51 } else if (LowerCaseEqualsASCII(name, "linearhistogram")) {
52 return LinearHistogramSample::ReadLinearHistogram(value);
53 } else if (LowerCaseEqualsASCII(name, "sparsehistogram")) {
54 return SparseHistogramSample::ReadSparseHistogram(value);
55 } else if (LowerCaseEqualsASCII(name, "useraction")) {
56 return UserActionSample::ReadUserAction(value);
57 } else {
58 DLOG(ERROR) << "invalid event type: " << name;
59 }
60 return NULL;
61 }
62
63 bool WriteMetricToFile(const MetricSample& sample,
64 const std::string& filename) {
65 if (!sample.isValid()) {
66 return false;
67 }
68 File file =
69 File(FilePath(filename), File::FLAG_OPEN_ALWAYS | File::FLAG_APPEND);
70 File::Error err = file.Lock();
71 if (err != File::FILE_OK) {
72 DLOG(ERROR) << "could not lock the file";
73 return false;
74 }
75
76 const string msg = sample.toString();
77 int32_t size = msg.length() + sizeof(int32_t);
78 file.WriteAtCurrentPos(reinterpret_cast<char*>(&size), sizeof(int32_t));
79 file.WriteAtCurrentPos(msg.c_str(), msg.length());
80
81 return true;
82 }
83
84 void ReadAndTruncateMetricsFromFile(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)) {
98 break;
99 }
100
101 message_length -= sizeof(message_length);
102 char serialized_sample[kMessageMaxLength];
103 read = file.ReadAtCurrentPos(serialized_sample, message_length);
104 if (read != message_length) {
105 DLOG(ERROR) << "could not read message" << read;
106 break;
107 }
108 metrics->push_back(ReadSample(string(serialized_sample, message_length)));
109 }
110 file.SetLength(0);
111 }
112 } // namespace ChromeOSMetrics
113 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698