OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/metrics/metrics_log.h" | 5 #include "components/metrics/metrics_log.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/bind.h" | |
14 #include "base/cpu.h" | 13 #include "base/cpu.h" |
15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/metrics/histogram.h" | |
16 #include "base/metrics/histogram_samples.h" | |
16 #include "base/prefs/pref_registry_simple.h" | 17 #include "base/prefs/pref_registry_simple.h" |
17 #include "base/prefs/pref_service.h" | 18 #include "base/prefs/pref_service.h" |
18 #include "base/sha1.h" | 19 #include "base/sha1.h" |
19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
22 #include "base/sys_info.h" | 23 #include "base/sys_info.h" |
23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
25 #include "components/metrics/metrics_hashes.h" | |
24 #include "components/metrics/metrics_pref_names.h" | 26 #include "components/metrics/metrics_pref_names.h" |
25 #include "components/metrics/metrics_provider.h" | 27 #include "components/metrics/metrics_provider.h" |
26 #include "components/metrics/metrics_service_client.h" | 28 #include "components/metrics/metrics_service_client.h" |
29 #include "components/metrics/proto/histogram_event.pb.h" | |
27 #include "components/metrics/proto/system_profile.pb.h" | 30 #include "components/metrics/proto/system_profile.pb.h" |
31 #include "components/metrics/proto/user_action_event.pb.h" | |
28 #include "components/variations/active_field_trials.h" | 32 #include "components/variations/active_field_trials.h" |
29 | 33 |
30 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
31 #include "base/android/build_info.h" | 35 #include "base/android/build_info.h" |
32 #endif | 36 #endif |
33 | 37 |
34 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
35 #include "base/win/metro.h" | 39 #include "base/win/metro.h" |
36 | 40 |
37 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx | 41 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
38 extern "C" IMAGE_DOS_HEADER __ImageBase; | 42 extern "C" IMAGE_DOS_HEADER __ImageBase; |
39 #endif | 43 #endif |
40 | 44 |
41 using metrics::MetricsLogBase; | 45 using base::SampleCountIterator; |
46 using metrics::HistogramEventProto; | |
42 using metrics::ProfilerEventProto; | 47 using metrics::ProfilerEventProto; |
43 using metrics::SystemProfileProto; | 48 using metrics::SystemProfileProto; |
49 using metrics::UserActionEventProto; | |
44 typedef variations::ActiveGroupId ActiveGroupId; | 50 typedef variations::ActiveGroupId ActiveGroupId; |
45 | 51 |
46 namespace { | 52 namespace { |
47 | 53 |
54 // Any id less than 16 bytes is considered to be a testing id. | |
55 bool IsTestingID(const std::string& id) { | |
56 return id.size() < 16; | |
57 } | |
58 | |
48 // Returns the date at which the current metrics client ID was created as | 59 // Returns the date at which the current metrics client ID was created as |
49 // a string containing seconds since the epoch, or "0" if none was found. | 60 // a string containing seconds since the epoch, or "0" if none was found. |
50 std::string GetMetricsEnabledDate(PrefService* pref) { | 61 std::string GetMetricsEnabledDate(PrefService* pref) { |
51 if (!pref) { | 62 if (!pref) { |
52 NOTREACHED(); | 63 NOTREACHED(); |
53 return "0"; | 64 return "0"; |
54 } | 65 } |
55 | 66 |
56 return pref->GetString(metrics::prefs::kMetricsReportingEnabledTimestamp); | 67 return pref->GetString(metrics::prefs::kMetricsReportingEnabledTimestamp); |
57 } | 68 } |
(...skipping 22 matching lines...) Expand all Loading... | |
80 return 3600 * (time_in_seconds / 3600); | 91 return 3600 * (time_in_seconds / 3600); |
81 } | 92 } |
82 | 93 |
83 } // namespace | 94 } // namespace |
84 | 95 |
85 MetricsLog::MetricsLog(const std::string& client_id, | 96 MetricsLog::MetricsLog(const std::string& client_id, |
86 int session_id, | 97 int session_id, |
87 LogType log_type, | 98 LogType log_type, |
88 metrics::MetricsServiceClient* client, | 99 metrics::MetricsServiceClient* client, |
89 PrefService* local_state) | 100 PrefService* local_state) |
90 : MetricsLogBase(client_id, | 101 : locked_(false), |
91 session_id, | 102 log_type_(log_type), |
92 log_type, | |
93 client->GetVersionString()), | |
94 client_(client), | 103 client_(client), |
95 creation_time_(base::TimeTicks::Now()), | 104 creation_time_(base::TimeTicks::Now()), |
96 local_state_(local_state) { | 105 local_state_(local_state) { |
97 uma_proto()->mutable_system_profile()->set_channel(client_->GetChannel()); | 106 if (IsTestingID(client_id)) |
107 uma_proto_.set_client_id(0); | |
108 else | |
109 uma_proto_.set_client_id(Hash(client_id)); | |
Ilya Sherman
2014/06/06 19:37:01
Optional nit: I prefer to leave blank lines after
Alexei Svitkine (slow)
2014/06/06 21:45:42
Done.
| |
110 uma_proto_.set_session_id(session_id); | |
111 | |
112 SystemProfileProto* system_profile = uma_proto_.mutable_system_profile(); | |
113 system_profile->set_build_timestamp(GetBuildTime()); | |
114 system_profile->set_app_version(client_->GetVersionString()); | |
115 system_profile->set_channel(client_->GetChannel()); | |
98 } | 116 } |
99 | 117 |
100 MetricsLog::~MetricsLog() {} | 118 MetricsLog::~MetricsLog() { |
119 } | |
101 | 120 |
102 // static | 121 // static |
103 void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) { | 122 void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) { |
104 registry->RegisterIntegerPref(metrics::prefs::kStabilityLaunchCount, 0); | 123 registry->RegisterIntegerPref(metrics::prefs::kStabilityLaunchCount, 0); |
105 registry->RegisterIntegerPref(metrics::prefs::kStabilityCrashCount, 0); | 124 registry->RegisterIntegerPref(metrics::prefs::kStabilityCrashCount, 0); |
106 registry->RegisterIntegerPref( | 125 registry->RegisterIntegerPref( |
107 metrics::prefs::kStabilityIncompleteSessionEndCount, 0); | 126 metrics::prefs::kStabilityIncompleteSessionEndCount, 0); |
108 registry->RegisterIntegerPref( | 127 registry->RegisterIntegerPref( |
109 metrics::prefs::kStabilityBreakpadRegistrationFail, 0); | 128 metrics::prefs::kStabilityBreakpadRegistrationFail, 0); |
110 registry->RegisterIntegerPref( | 129 registry->RegisterIntegerPref( |
111 metrics::prefs::kStabilityBreakpadRegistrationSuccess, 0); | 130 metrics::prefs::kStabilityBreakpadRegistrationSuccess, 0); |
112 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerPresent, 0); | 131 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerPresent, 0); |
113 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerNotPresent, | 132 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerNotPresent, |
114 0); | 133 0); |
115 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfile, | 134 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfile, |
116 std::string()); | 135 std::string()); |
117 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfileHash, | 136 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfileHash, |
118 std::string()); | 137 std::string()); |
119 } | 138 } |
120 | 139 |
140 // static | |
141 uint64 MetricsLog::Hash(const std::string& value) { | |
142 uint64 hash = metrics::HashMetricName(value); | |
143 | |
144 // The following log is VERY helpful when folks add some named histogram into | |
145 // the code, but forgot to update the descriptive list of histograms. When | |
146 // that happens, all we get to see (server side) is a hash of the histogram | |
147 // name. We can then use this logging to find out what histogram name was | |
148 // being hashed to a given MD5 value by just running the version of Chromium | |
149 // in question with --enable-logging. | |
150 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; | |
151 | |
152 return hash; | |
153 } | |
154 | |
155 // static | |
156 int64 MetricsLog::GetBuildTime() { | |
157 static int64 integral_build_time = 0; | |
158 if (!integral_build_time) { | |
159 base::Time time; | |
160 static const char kDateTime[] = __DATE__ " " __TIME__ " GMT"; | |
161 bool result = base::Time::FromString(kDateTime, &time); | |
162 DCHECK(result); | |
163 integral_build_time = static_cast<int64>(time.ToTimeT()); | |
164 } | |
165 return integral_build_time; | |
166 } | |
167 | |
168 // static | |
169 int64 MetricsLog::GetCurrentTime() { | |
170 return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds(); | |
171 } | |
172 | |
173 void MetricsLog::RecordUserAction(const std::string& key) { | |
174 DCHECK(!locked_); | |
175 | |
176 UserActionEventProto* user_action = uma_proto_.add_user_action_event(); | |
177 user_action->set_name_hash(Hash(key)); | |
178 user_action->set_time(GetCurrentTime()); | |
179 } | |
180 | |
181 void MetricsLog::RecordHistogramDelta(const std::string& histogram_name, | |
182 const base::HistogramSamples& snapshot) { | |
183 DCHECK(!locked_); | |
184 DCHECK_NE(0, snapshot.TotalCount()); | |
185 | |
186 // We will ignore the MAX_INT/infinite value in the last element of range[]. | |
187 | |
188 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event(); | |
189 histogram_proto->set_name_hash(Hash(histogram_name)); | |
190 histogram_proto->set_sum(snapshot.sum()); | |
191 | |
192 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done(); | |
193 it->Next()) { | |
194 base::Histogram::Sample min; | |
195 base::Histogram::Sample max; | |
196 base::Histogram::Count count; | |
197 it->Get(&min, &max, &count); | |
198 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket(); | |
199 bucket->set_min(min); | |
200 bucket->set_max(max); | |
201 bucket->set_count(count); | |
202 } | |
203 | |
204 // Omit fields to save space (see rules in histogram_event.proto comments). | |
205 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { | |
206 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); | |
207 if (i + 1 < histogram_proto->bucket_size() && | |
208 bucket->max() == histogram_proto->bucket(i + 1).min()) { | |
209 bucket->clear_max(); | |
210 } else if (bucket->max() == bucket->min() + 1) { | |
211 bucket->clear_min(); | |
212 } | |
213 } | |
214 } | |
215 | |
121 void MetricsLog::RecordStabilityMetrics( | 216 void MetricsLog::RecordStabilityMetrics( |
122 const std::vector<metrics::MetricsProvider*>& metrics_providers, | 217 const std::vector<metrics::MetricsProvider*>& metrics_providers, |
123 base::TimeDelta incremental_uptime, | 218 base::TimeDelta incremental_uptime, |
124 base::TimeDelta uptime) { | 219 base::TimeDelta uptime) { |
125 DCHECK(!locked()); | 220 DCHECK(!locked()); |
126 DCHECK(HasEnvironment()); | 221 DCHECK(HasEnvironment()); |
127 DCHECK(!HasStabilityMetrics()); | 222 DCHECK(!HasStabilityMetrics()); |
128 | 223 |
129 PrefService* pref = local_state_; | 224 PrefService* pref = local_state_; |
130 DCHECK(pref); | 225 DCHECK(pref); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfile); | 419 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfile); |
325 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfileHash); | 420 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfileHash); |
326 | 421 |
327 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); | 422 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
328 std::string serialied_system_profile; | 423 std::string serialied_system_profile; |
329 return base::Base64Decode(base64_system_profile, &serialied_system_profile) && | 424 return base::Base64Decode(base64_system_profile, &serialied_system_profile) && |
330 ComputeSHA1(serialied_system_profile) == system_profile_hash && | 425 ComputeSHA1(serialied_system_profile) == system_profile_hash && |
331 system_profile->ParseFromString(serialied_system_profile); | 426 system_profile->ParseFromString(serialied_system_profile); |
332 } | 427 } |
333 | 428 |
429 void MetricsLog::CloseLog() { | |
430 DCHECK(!locked_); | |
Ilya Sherman
2014/06/06 19:37:01
Optional nit: Maybe we should rename "locked_" to
Alexei Svitkine (slow)
2014/06/06 21:45:42
Done.
| |
431 locked_ = true; | |
432 } | |
433 | |
434 void MetricsLog::GetEncodedLog(std::string* encoded_log) { | |
435 DCHECK(locked_); | |
436 uma_proto_.SerializeToString(encoded_log); | |
437 } | |
OLD | NEW |