| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/metrics/metrics_log_base.h" | |
| 6 | |
| 7 #include "base/metrics/histogram_base.h" | |
| 8 #include "base/metrics/histogram_samples.h" | |
| 9 #include "chrome/common/chrome_version_info.h" | |
| 10 #include "chrome/common/metrics/proto/histogram_event.pb.h" | |
| 11 #include "chrome/common/metrics/proto/system_profile.pb.h" | |
| 12 #include "chrome/common/metrics/proto/user_action_event.pb.h" | |
| 13 #include "components/metrics/metrics_hashes.h" | |
| 14 | |
| 15 using base::Histogram; | |
| 16 using base::HistogramBase; | |
| 17 using base::HistogramSamples; | |
| 18 using base::SampleCountIterator; | |
| 19 using base::Time; | |
| 20 using base::TimeDelta; | |
| 21 using metrics::HistogramEventProto; | |
| 22 using metrics::SystemProfileProto; | |
| 23 using metrics::UserActionEventProto; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Any id less than 16 bytes is considered to be a testing id. | |
| 28 bool IsTestingID(const std::string& id) { | |
| 29 return id.size() < 16; | |
| 30 } | |
| 31 | |
| 32 SystemProfileProto::Channel AsProtobufChannel( | |
| 33 chrome::VersionInfo::Channel channel) { | |
| 34 switch (channel) { | |
| 35 case chrome::VersionInfo::CHANNEL_UNKNOWN: | |
| 36 return SystemProfileProto::CHANNEL_UNKNOWN; | |
| 37 case chrome::VersionInfo::CHANNEL_CANARY: | |
| 38 return SystemProfileProto::CHANNEL_CANARY; | |
| 39 case chrome::VersionInfo::CHANNEL_DEV: | |
| 40 return SystemProfileProto::CHANNEL_DEV; | |
| 41 case chrome::VersionInfo::CHANNEL_BETA: | |
| 42 return SystemProfileProto::CHANNEL_BETA; | |
| 43 case chrome::VersionInfo::CHANNEL_STABLE: | |
| 44 return SystemProfileProto::CHANNEL_STABLE; | |
| 45 default: | |
| 46 NOTREACHED(); | |
| 47 return SystemProfileProto::CHANNEL_UNKNOWN; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 MetricsLogBase::MetricsLogBase(const std::string& client_id, | |
| 54 int session_id, | |
| 55 LogType log_type, | |
| 56 const std::string& version_string) | |
| 57 : num_events_(0), | |
| 58 locked_(false), | |
| 59 log_type_(log_type) { | |
| 60 DCHECK_NE(NO_LOG, log_type); | |
| 61 if (IsTestingID(client_id)) | |
| 62 uma_proto_.set_client_id(0); | |
| 63 else | |
| 64 uma_proto_.set_client_id(Hash(client_id)); | |
| 65 | |
| 66 uma_proto_.set_session_id(session_id); | |
| 67 uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime()); | |
| 68 uma_proto_.mutable_system_profile()->set_app_version(version_string); | |
| 69 uma_proto_.mutable_system_profile()->set_channel( | |
| 70 AsProtobufChannel(chrome::VersionInfo::GetChannel())); | |
| 71 } | |
| 72 | |
| 73 MetricsLogBase::~MetricsLogBase() {} | |
| 74 | |
| 75 // static | |
| 76 uint64 MetricsLogBase::Hash(const std::string& value) { | |
| 77 uint64 hash = metrics::HashMetricName(value); | |
| 78 | |
| 79 // The following log is VERY helpful when folks add some named histogram into | |
| 80 // the code, but forgot to update the descriptive list of histograms. When | |
| 81 // that happens, all we get to see (server side) is a hash of the histogram | |
| 82 // name. We can then use this logging to find out what histogram name was | |
| 83 // being hashed to a given MD5 value by just running the version of Chromium | |
| 84 // in question with --enable-logging. | |
| 85 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; | |
| 86 | |
| 87 return hash; | |
| 88 } | |
| 89 | |
| 90 // static | |
| 91 int64 MetricsLogBase::GetBuildTime() { | |
| 92 static int64 integral_build_time = 0; | |
| 93 if (!integral_build_time) { | |
| 94 Time time; | |
| 95 const char* kDateTime = __DATE__ " " __TIME__ " GMT"; | |
| 96 bool result = Time::FromString(kDateTime, &time); | |
| 97 DCHECK(result); | |
| 98 integral_build_time = static_cast<int64>(time.ToTimeT()); | |
| 99 } | |
| 100 return integral_build_time; | |
| 101 } | |
| 102 | |
| 103 // static | |
| 104 int64 MetricsLogBase::GetCurrentTime() { | |
| 105 return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds(); | |
| 106 } | |
| 107 | |
| 108 void MetricsLogBase::CloseLog() { | |
| 109 DCHECK(!locked_); | |
| 110 locked_ = true; | |
| 111 } | |
| 112 | |
| 113 void MetricsLogBase::GetEncodedLog(std::string* encoded_log) { | |
| 114 DCHECK(locked_); | |
| 115 uma_proto_.SerializeToString(encoded_log); | |
| 116 } | |
| 117 | |
| 118 void MetricsLogBase::RecordUserAction(const std::string& key) { | |
| 119 DCHECK(!locked_); | |
| 120 | |
| 121 UserActionEventProto* user_action = uma_proto_.add_user_action_event(); | |
| 122 user_action->set_name_hash(Hash(key)); | |
| 123 user_action->set_time(GetCurrentTime()); | |
| 124 | |
| 125 ++num_events_; | |
| 126 } | |
| 127 | |
| 128 void MetricsLogBase::RecordHistogramDelta(const std::string& histogram_name, | |
| 129 const HistogramSamples& snapshot) { | |
| 130 DCHECK(!locked_); | |
| 131 DCHECK_NE(0, snapshot.TotalCount()); | |
| 132 | |
| 133 // We will ignore the MAX_INT/infinite value in the last element of range[]. | |
| 134 | |
| 135 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event(); | |
| 136 histogram_proto->set_name_hash(Hash(histogram_name)); | |
| 137 histogram_proto->set_sum(snapshot.sum()); | |
| 138 | |
| 139 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); | |
| 140 !it->Done(); | |
| 141 it->Next()) { | |
| 142 HistogramBase::Sample min; | |
| 143 HistogramBase::Sample max; | |
| 144 HistogramBase::Count count; | |
| 145 it->Get(&min, &max, &count); | |
| 146 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket(); | |
| 147 bucket->set_min(min); | |
| 148 bucket->set_max(max); | |
| 149 bucket->set_count(count); | |
| 150 } | |
| 151 | |
| 152 // Omit fields to save space (see rules in histogram_event.proto comments). | |
| 153 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { | |
| 154 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); | |
| 155 if (i + 1 < histogram_proto->bucket_size() && | |
| 156 bucket->max() == histogram_proto->bucket(i + 1).min()) { | |
| 157 bucket->clear_max(); | |
| 158 } else if (bucket->max() == bucket->min() + 1) { | |
| 159 bucket->clear_min(); | |
| 160 } | |
| 161 } | |
| 162 } | |
| OLD | NEW |