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.
5 #include "chrome/common/metrics/metrics_log_base.h"
7 #include "base/base64.h"
8 #include "base/basictypes.h"
10 #include "base/metrics/histogram_base.h"
11 #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"
15 #include "chrome/common/logging_chrome.h"
16 #include "chrome/common/metrics/proto/histogram_event.pb.h"
17 #include "chrome/common/metrics/proto/system_profile.pb.h"
18 #include "chrome/common/metrics/proto/user_action_event.pb.h"
20 using base::Histogram
;
21 using base::HistogramBase
;
22 using base::HistogramSamples
;
23 using base::SampleCountIterator
;
25 using base::TimeDelta
;
26 using metrics::HistogramEventProto
;
27 using metrics::SystemProfileProto
;
28 using metrics::UserActionEventProto
;
32 // Any id less than 16 bytes is considered to be a testing id.
33 bool IsTestingID(const std::string
& id
) {
34 return id
.size() < 16;
37 // Converts the 8-byte prefix of an MD5 hash into a uint64 value.
38 inline uint64
HashToUInt64(const std::string
& hash
) {
40 DCHECK_GE(hash
.size(), sizeof(value
));
41 memcpy(&value
, hash
.data(), sizeof(value
));
42 return base::HostToNet64(value
);
45 SystemProfileProto::Channel
AsProtobufChannel(
46 chrome::VersionInfo::Channel channel
) {
48 case chrome::VersionInfo::CHANNEL_UNKNOWN
:
49 return SystemProfileProto::CHANNEL_UNKNOWN
;
50 case chrome::VersionInfo::CHANNEL_CANARY
:
51 return SystemProfileProto::CHANNEL_CANARY
;
52 case chrome::VersionInfo::CHANNEL_DEV
:
53 return SystemProfileProto::CHANNEL_DEV
;
54 case chrome::VersionInfo::CHANNEL_BETA
:
55 return SystemProfileProto::CHANNEL_BETA
;
56 case chrome::VersionInfo::CHANNEL_STABLE
:
57 return SystemProfileProto::CHANNEL_STABLE
;
60 return SystemProfileProto::CHANNEL_UNKNOWN
;
66 MetricsLogBase::MetricsLogBase(const std::string
& client_id
, int session_id
,
67 const std::string
& version_string
)
70 if (IsTestingID(client_id
))
71 uma_proto_
.set_client_id(0);
73 uma_proto_
.set_client_id(Hash(client_id
));
75 uma_proto_
.set_session_id(session_id
);
76 uma_proto_
.mutable_system_profile()->set_build_timestamp(GetBuildTime());
77 uma_proto_
.mutable_system_profile()->set_app_version(version_string
);
78 uma_proto_
.mutable_system_profile()->set_channel(
79 AsProtobufChannel(chrome::VersionInfo::GetChannel()));
82 MetricsLogBase::~MetricsLogBase() {}
85 uint64
MetricsLogBase::Hash(const std::string
& value
) {
86 // Create an MD5 hash of the given |value|, represented as a byte buffer
87 // encoded as an std::string.
88 base::MD5Context context
;
89 base::MD5Init(&context
);
90 base::MD5Update(&context
, value
);
92 base::MD5Digest digest
;
93 base::MD5Final(&digest
, &context
);
95 std::string
hash_str(reinterpret_cast<char*>(digest
.a
), arraysize(digest
.a
));
96 uint64 hash
= HashToUInt64(hash_str
);
98 // 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
100 // 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
102 // being hashed to a given MD5 value by just running the version of Chromium
103 // in question with --enable-logging.
104 DVLOG(1) << "Metrics: Hash numeric [" << value
<< "]=[" << hash
<< "]";
110 int64
MetricsLogBase::GetBuildTime() {
111 static int64 integral_build_time
= 0;
112 if (!integral_build_time
) {
114 const char* kDateTime
= __DATE__
" " __TIME__
" GMT";
115 bool result
= Time::FromString(kDateTime
, &time
);
117 integral_build_time
= static_cast<int64
>(time
.ToTimeT());
119 return integral_build_time
;
123 int64
MetricsLogBase::GetCurrentTime() {
124 return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds();
127 void MetricsLogBase::CloseLog() {
132 void MetricsLogBase::GetEncodedLog(std::string
* encoded_log
) {
134 uma_proto_
.SerializeToString(encoded_log
);
137 void MetricsLogBase::RecordUserAction(const char* key
) {
140 UserActionEventProto
* user_action
= uma_proto_
.add_user_action_event();
141 user_action
->set_name_hash(Hash(key
));
142 user_action
->set_time(GetCurrentTime());
147 void MetricsLogBase::RecordHistogramDelta(const std::string
& histogram_name
,
148 const HistogramSamples
& snapshot
) {
150 DCHECK_NE(0, snapshot
.TotalCount());
152 // We will ignore the MAX_INT/infinite value in the last element of range[].
154 HistogramEventProto
* histogram_proto
= uma_proto_
.add_histogram_event();
155 histogram_proto
->set_name_hash(Hash(histogram_name
));
156 histogram_proto
->set_sum(snapshot
.sum());
158 for (scoped_ptr
<SampleCountIterator
> it
= snapshot
.Iterator();
161 HistogramBase::Sample min
;
162 HistogramBase::Sample max
;
163 HistogramBase::Count count
;
164 it
->Get(&min
, &max
, &count
);
165 HistogramEventProto::Bucket
* bucket
= histogram_proto
->add_bucket();
166 bucket
->set_min(min
);
167 bucket
->set_max(max
);
168 bucket
->set_count(count
);
171 // Omit fields to save space (see rules in histogram_event.proto comments).
172 for (int i
= 0; i
< histogram_proto
->bucket_size(); ++i
) {
173 HistogramEventProto::Bucket
* bucket
= histogram_proto
->mutable_bucket(i
);
174 if (i
+ 1 < histogram_proto
->bucket_size() &&
175 bucket
->max() == histogram_proto
->bucket(i
+ 1).min()) {
177 } else if (bucket
->max() == bucket
->min() + 1) {