OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/histogram_base.h" | 5 #include "base/metrics/histogram_base.h" |
6 | 6 |
7 #include <climits> | 7 #include <climits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/histogram_samples.h" |
| 14 #include "base/metrics/sparse_histogram.h" |
| 15 #include "base/pickle.h" |
12 #include "base/values.h" | 16 #include "base/values.h" |
13 | 17 |
14 namespace base { | 18 namespace base { |
15 | 19 |
16 std::string HistogramTypeToString(HistogramType type) { | 20 std::string HistogramTypeToString(HistogramType type) { |
17 switch(type) { | 21 switch(type) { |
18 case HISTOGRAM: | 22 case HISTOGRAM: |
19 return "HISTOGRAM"; | 23 return "HISTOGRAM"; |
20 case LINEAR_HISTOGRAM: | 24 case LINEAR_HISTOGRAM: |
21 return "LINEAR_HISTOGRAM"; | 25 return "LINEAR_HISTOGRAM"; |
22 case BOOLEAN_HISTOGRAM: | 26 case BOOLEAN_HISTOGRAM: |
23 return "BOOLEAN_HISTOGRAM"; | 27 return "BOOLEAN_HISTOGRAM"; |
24 case CUSTOM_HISTOGRAM: | 28 case CUSTOM_HISTOGRAM: |
25 return "CUSTOM_HISTOGRAM"; | 29 return "CUSTOM_HISTOGRAM"; |
26 case SPARSE_HISTOGRAM: | 30 case SPARSE_HISTOGRAM: |
27 return "SPARSE_HISTOGRAM"; | 31 return "SPARSE_HISTOGRAM"; |
28 default: | 32 default: |
29 NOTREACHED(); | 33 NOTREACHED(); |
30 } | 34 } |
31 return "UNKNOWN"; | 35 return "UNKNOWN"; |
32 } | 36 } |
33 | 37 |
| 38 HistogramBase* DeserializeHistogramInfo(PickleIterator* iter) { |
| 39 int type; |
| 40 if (!iter->ReadInt(&type)) |
| 41 return NULL; |
| 42 |
| 43 switch (type) { |
| 44 case HISTOGRAM: |
| 45 return Histogram::DeserializeInfoImpl(iter); |
| 46 case LINEAR_HISTOGRAM: |
| 47 return LinearHistogram::DeserializeInfoImpl(iter); |
| 48 case BOOLEAN_HISTOGRAM: |
| 49 return BooleanHistogram::DeserializeInfoImpl(iter); |
| 50 case CUSTOM_HISTOGRAM: |
| 51 return CustomHistogram::DeserializeInfoImpl(iter); |
| 52 case SPARSE_HISTOGRAM: |
| 53 return SparseHistogram::DeserializeInfoImpl(iter); |
| 54 default: |
| 55 return NULL; |
| 56 } |
| 57 } |
| 58 |
| 59 bool DeserializeHistogramAndAddSamples(PickleIterator* iter) { |
| 60 HistogramBase* histogram = DeserializeHistogramInfo(iter); |
| 61 |
| 62 if (!histogram) |
| 63 return false; |
| 64 |
| 65 if (histogram->flags() & base::HistogramBase::kIPCSerializationSourceFlag) { |
| 66 DVLOG(1) << "Single process mode, histogram observed and not copied: " |
| 67 << histogram->histogram_name(); |
| 68 return true; |
| 69 } |
| 70 |
| 71 histogram->AddSamplesFromPickle(iter); |
| 72 return true; |
| 73 } |
| 74 |
| 75 |
34 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX; | 76 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX; |
35 | 77 |
36 HistogramBase::HistogramBase(const std::string& name) | 78 HistogramBase::HistogramBase(const std::string& name) |
37 : histogram_name_(name), | 79 : histogram_name_(name), |
38 flags_(kNoFlags) {} | 80 flags_(kNoFlags) {} |
39 | 81 |
40 HistogramBase::~HistogramBase() {} | 82 HistogramBase::~HistogramBase() {} |
41 | 83 |
42 void HistogramBase::SetFlags(int32 flags) { | 84 void HistogramBase::SetFlags(int32 flags) { |
43 flags_ |= flags; | 85 flags_ |= flags; |
44 } | 86 } |
45 | 87 |
46 void HistogramBase::ClearFlags(int32 flags) { | 88 void HistogramBase::ClearFlags(int32 flags) { |
47 flags_ &= ~flags; | 89 flags_ &= ~flags; |
48 } | 90 } |
49 | 91 |
| 92 bool HistogramBase::SerializeInfo(Pickle* pickle) const { |
| 93 if (!pickle->WriteInt(GetHistogramType())) |
| 94 return false; |
| 95 return SerializeInfoImpl(pickle); |
| 96 } |
| 97 |
50 void HistogramBase::WriteJSON(std::string* output) const { | 98 void HistogramBase::WriteJSON(std::string* output) const { |
51 Count count; | 99 Count count; |
52 scoped_ptr<ListValue> buckets(new ListValue()); | 100 scoped_ptr<ListValue> buckets(new ListValue()); |
53 GetCountAndBucketData(&count, buckets.get()); | 101 GetCountAndBucketData(&count, buckets.get()); |
54 scoped_ptr<DictionaryValue> parameters(new DictionaryValue()); | 102 scoped_ptr<DictionaryValue> parameters(new DictionaryValue()); |
55 GetParameters(parameters.get()); | 103 GetParameters(parameters.get()); |
56 | 104 |
57 JSONStringValueSerializer serializer(output); | 105 JSONStringValueSerializer serializer(output); |
58 DictionaryValue root; | 106 DictionaryValue root; |
59 root.SetString("name", histogram_name()); | 107 root.SetString("name", histogram_name()); |
60 root.SetInteger("count", count); | 108 root.SetInteger("count", count); |
61 root.SetInteger("flags", flags()); | 109 root.SetInteger("flags", flags()); |
62 root.Set("params", parameters.release()); | 110 root.Set("params", parameters.release()); |
63 root.Set("buckets", buckets.release()); | 111 root.Set("buckets", buckets.release()); |
64 serializer.Serialize(root); | 112 serializer.Serialize(root); |
65 } | 113 } |
66 | 114 |
67 } // namespace base | 115 } // namespace base |
OLD | NEW |