| 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 void DeserializeHistogramAndAddSamples(PickleIterator* iter) { |
| 60 HistogramBase* histogram = DeserializeHistogramInfo(iter); |
| 61 if (!histogram) |
| 62 return; |
| 63 |
| 64 if (histogram->flags() & base::HistogramBase::kIPCSerializationSourceFlag) { |
| 65 DVLOG(1) << "Single process mode, histogram observed and not copied: " |
| 66 << histogram->histogram_name(); |
| 67 return; |
| 68 } |
| 69 histogram->AddSamplesFromPickle(iter); |
| 70 } |
| 71 |
| 72 |
| 34 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX; | 73 const HistogramBase::Sample HistogramBase::kSampleType_MAX = INT_MAX; |
| 35 | 74 |
| 36 HistogramBase::HistogramBase(const std::string& name) | 75 HistogramBase::HistogramBase(const std::string& name) |
| 37 : histogram_name_(name), | 76 : histogram_name_(name), |
| 38 flags_(kNoFlags) {} | 77 flags_(kNoFlags) {} |
| 39 | 78 |
| 40 HistogramBase::~HistogramBase() {} | 79 HistogramBase::~HistogramBase() {} |
| 41 | 80 |
| 42 void HistogramBase::SetFlags(int32 flags) { | 81 void HistogramBase::SetFlags(int32 flags) { |
| 43 flags_ |= flags; | 82 flags_ |= flags; |
| 44 } | 83 } |
| 45 | 84 |
| 46 void HistogramBase::ClearFlags(int32 flags) { | 85 void HistogramBase::ClearFlags(int32 flags) { |
| 47 flags_ &= ~flags; | 86 flags_ &= ~flags; |
| 48 } | 87 } |
| 49 | 88 |
| 89 bool HistogramBase::SerializeInfo(Pickle* pickle) const { |
| 90 if (!pickle->WriteInt(GetHistogramType())) |
| 91 return false; |
| 92 return SerializeInfoImpl(pickle); |
| 93 } |
| 94 |
| 50 void HistogramBase::WriteJSON(std::string* output) const { | 95 void HistogramBase::WriteJSON(std::string* output) const { |
| 51 Count count; | 96 Count count; |
| 52 scoped_ptr<ListValue> buckets(new ListValue()); | 97 scoped_ptr<ListValue> buckets(new ListValue()); |
| 53 GetCountAndBucketData(&count, buckets.get()); | 98 GetCountAndBucketData(&count, buckets.get()); |
| 54 scoped_ptr<DictionaryValue> parameters(new DictionaryValue()); | 99 scoped_ptr<DictionaryValue> parameters(new DictionaryValue()); |
| 55 GetParameters(parameters.get()); | 100 GetParameters(parameters.get()); |
| 56 | 101 |
| 57 JSONStringValueSerializer serializer(output); | 102 JSONStringValueSerializer serializer(output); |
| 58 DictionaryValue root; | 103 DictionaryValue root; |
| 59 root.SetString("name", histogram_name()); | 104 root.SetString("name", histogram_name()); |
| 60 root.SetInteger("count", count); | 105 root.SetInteger("count", count); |
| 61 root.SetInteger("flags", flags()); | 106 root.SetInteger("flags", flags()); |
| 62 root.Set("params", parameters.release()); | 107 root.Set("params", parameters.release()); |
| 63 root.Set("buckets", buckets.release()); | 108 root.Set("buckets", buckets.release()); |
| 64 serializer.Serialize(root); | 109 serializer.Serialize(root); |
| 65 } | 110 } |
| 66 | 111 |
| 67 } // namespace base | 112 } // namespace base |
| OLD | NEW |