| 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 "chrome/common/metrics/metrics_log_base.h" | 5 #include "chrome/common/metrics/metrics_log_base.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/md5.h" | |
| 10 #include "base/metrics/histogram_base.h" | 7 #include "base/metrics/histogram_base.h" |
| 11 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
| 12 #include "base/sys_byteorder.h" | |
| 13 #include "base/sys_info.h" | |
| 14 #include "chrome/common/chrome_version_info.h" | 9 #include "chrome/common/chrome_version_info.h" |
| 15 #include "chrome/common/logging_chrome.h" | |
| 16 #include "chrome/common/metrics/proto/histogram_event.pb.h" | 10 #include "chrome/common/metrics/proto/histogram_event.pb.h" |
| 17 #include "chrome/common/metrics/proto/system_profile.pb.h" | 11 #include "chrome/common/metrics/proto/system_profile.pb.h" |
| 18 #include "chrome/common/metrics/proto/user_action_event.pb.h" | 12 #include "chrome/common/metrics/proto/user_action_event.pb.h" |
| 13 #include "components/metrics/metrics_hashes.h" |
| 19 | 14 |
| 20 using base::Histogram; | 15 using base::Histogram; |
| 21 using base::HistogramBase; | 16 using base::HistogramBase; |
| 22 using base::HistogramSamples; | 17 using base::HistogramSamples; |
| 23 using base::SampleCountIterator; | 18 using base::SampleCountIterator; |
| 24 using base::Time; | 19 using base::Time; |
| 25 using base::TimeDelta; | 20 using base::TimeDelta; |
| 26 using metrics::HistogramEventProto; | 21 using metrics::HistogramEventProto; |
| 27 using metrics::SystemProfileProto; | 22 using metrics::SystemProfileProto; |
| 28 using metrics::UserActionEventProto; | 23 using metrics::UserActionEventProto; |
| 29 | 24 |
| 30 namespace { | 25 namespace { |
| 31 | 26 |
| 32 // Any id less than 16 bytes is considered to be a testing id. | 27 // Any id less than 16 bytes is considered to be a testing id. |
| 33 bool IsTestingID(const std::string& id) { | 28 bool IsTestingID(const std::string& id) { |
| 34 return id.size() < 16; | 29 return id.size() < 16; |
| 35 } | 30 } |
| 36 | 31 |
| 37 // Converts the 8-byte prefix of an MD5 hash into a uint64 value. | |
| 38 inline uint64 HashToUInt64(const std::string& hash) { | |
| 39 uint64 value; | |
| 40 DCHECK_GE(hash.size(), sizeof(value)); | |
| 41 memcpy(&value, hash.data(), sizeof(value)); | |
| 42 return base::HostToNet64(value); | |
| 43 } | |
| 44 | |
| 45 SystemProfileProto::Channel AsProtobufChannel( | 32 SystemProfileProto::Channel AsProtobufChannel( |
| 46 chrome::VersionInfo::Channel channel) { | 33 chrome::VersionInfo::Channel channel) { |
| 47 switch (channel) { | 34 switch (channel) { |
| 48 case chrome::VersionInfo::CHANNEL_UNKNOWN: | 35 case chrome::VersionInfo::CHANNEL_UNKNOWN: |
| 49 return SystemProfileProto::CHANNEL_UNKNOWN; | 36 return SystemProfileProto::CHANNEL_UNKNOWN; |
| 50 case chrome::VersionInfo::CHANNEL_CANARY: | 37 case chrome::VersionInfo::CHANNEL_CANARY: |
| 51 return SystemProfileProto::CHANNEL_CANARY; | 38 return SystemProfileProto::CHANNEL_CANARY; |
| 52 case chrome::VersionInfo::CHANNEL_DEV: | 39 case chrome::VersionInfo::CHANNEL_DEV: |
| 53 return SystemProfileProto::CHANNEL_DEV; | 40 return SystemProfileProto::CHANNEL_DEV; |
| 54 case chrome::VersionInfo::CHANNEL_BETA: | 41 case chrome::VersionInfo::CHANNEL_BETA: |
| (...skipping 21 matching lines...) Expand all Loading... |
| 76 uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime()); | 63 uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime()); |
| 77 uma_proto_.mutable_system_profile()->set_app_version(version_string); | 64 uma_proto_.mutable_system_profile()->set_app_version(version_string); |
| 78 uma_proto_.mutable_system_profile()->set_channel( | 65 uma_proto_.mutable_system_profile()->set_channel( |
| 79 AsProtobufChannel(chrome::VersionInfo::GetChannel())); | 66 AsProtobufChannel(chrome::VersionInfo::GetChannel())); |
| 80 } | 67 } |
| 81 | 68 |
| 82 MetricsLogBase::~MetricsLogBase() {} | 69 MetricsLogBase::~MetricsLogBase() {} |
| 83 | 70 |
| 84 // static | 71 // static |
| 85 uint64 MetricsLogBase::Hash(const std::string& value) { | 72 uint64 MetricsLogBase::Hash(const std::string& value) { |
| 86 // Create an MD5 hash of the given |value|, represented as a byte buffer | 73 uint64 hash = metrics::HashMetricName(value); |
| 87 // encoded as an std::string. | |
| 88 base::MD5Context context; | |
| 89 base::MD5Init(&context); | |
| 90 base::MD5Update(&context, value); | |
| 91 | |
| 92 base::MD5Digest digest; | |
| 93 base::MD5Final(&digest, &context); | |
| 94 | |
| 95 std::string hash_str(reinterpret_cast<char*>(digest.a), arraysize(digest.a)); | |
| 96 uint64 hash = HashToUInt64(hash_str); | |
| 97 | 74 |
| 98 // The following log is VERY helpful when folks add some named histogram into | 75 // The following log is VERY helpful when folks add some named histogram into |
| 99 // the code, but forgot to update the descriptive list of histograms. When | 76 // the code, but forgot to update the descriptive list of histograms. When |
| 100 // that happens, all we get to see (server side) is a hash of the histogram | 77 // that happens, all we get to see (server side) is a hash of the histogram |
| 101 // name. We can then use this logging to find out what histogram name was | 78 // name. We can then use this logging to find out what histogram name was |
| 102 // being hashed to a given MD5 value by just running the version of Chromium | 79 // being hashed to a given MD5 value by just running the version of Chromium |
| 103 // in question with --enable-logging. | 80 // in question with --enable-logging. |
| 104 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; | 81 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; |
| 105 | 82 |
| 106 return hash; | 83 return hash; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { | 149 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { |
| 173 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); | 150 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); |
| 174 if (i + 1 < histogram_proto->bucket_size() && | 151 if (i + 1 < histogram_proto->bucket_size() && |
| 175 bucket->max() == histogram_proto->bucket(i + 1).min()) { | 152 bucket->max() == histogram_proto->bucket(i + 1).min()) { |
| 176 bucket->clear_max(); | 153 bucket->clear_max(); |
| 177 } else if (bucket->max() == bucket->min() + 1) { | 154 } else if (bucket->max() == bucket->min() + 1) { |
| 178 bucket->clear_min(); | 155 bucket->clear_min(); |
| 179 } | 156 } |
| 180 } | 157 } |
| 181 } | 158 } |
| OLD | NEW |