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 : closed_(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)); |
| 110 |
| 111 uma_proto_.set_session_id(session_id); |
| 112 |
| 113 SystemProfileProto* system_profile = uma_proto_.mutable_system_profile(); |
| 114 system_profile->set_build_timestamp(GetBuildTime()); |
| 115 system_profile->set_app_version(client_->GetVersionString()); |
| 116 system_profile->set_channel(client_->GetChannel()); |
98 } | 117 } |
99 | 118 |
100 MetricsLog::~MetricsLog() {} | 119 MetricsLog::~MetricsLog() { |
| 120 } |
101 | 121 |
102 // static | 122 // static |
103 void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) { | 123 void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) { |
104 registry->RegisterIntegerPref(metrics::prefs::kStabilityLaunchCount, 0); | 124 registry->RegisterIntegerPref(metrics::prefs::kStabilityLaunchCount, 0); |
105 registry->RegisterIntegerPref(metrics::prefs::kStabilityCrashCount, 0); | 125 registry->RegisterIntegerPref(metrics::prefs::kStabilityCrashCount, 0); |
106 registry->RegisterIntegerPref( | 126 registry->RegisterIntegerPref( |
107 metrics::prefs::kStabilityIncompleteSessionEndCount, 0); | 127 metrics::prefs::kStabilityIncompleteSessionEndCount, 0); |
108 registry->RegisterIntegerPref( | 128 registry->RegisterIntegerPref( |
109 metrics::prefs::kStabilityBreakpadRegistrationFail, 0); | 129 metrics::prefs::kStabilityBreakpadRegistrationFail, 0); |
110 registry->RegisterIntegerPref( | 130 registry->RegisterIntegerPref( |
111 metrics::prefs::kStabilityBreakpadRegistrationSuccess, 0); | 131 metrics::prefs::kStabilityBreakpadRegistrationSuccess, 0); |
112 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerPresent, 0); | 132 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerPresent, 0); |
113 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerNotPresent, | 133 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerNotPresent, |
114 0); | 134 0); |
115 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfile, | 135 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfile, |
116 std::string()); | 136 std::string()); |
117 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfileHash, | 137 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfileHash, |
118 std::string()); | 138 std::string()); |
119 } | 139 } |
120 | 140 |
| 141 // static |
| 142 uint64 MetricsLog::Hash(const std::string& value) { |
| 143 uint64 hash = metrics::HashMetricName(value); |
| 144 |
| 145 // The following log is VERY helpful when folks add some named histogram into |
| 146 // the code, but forgot to update the descriptive list of histograms. When |
| 147 // that happens, all we get to see (server side) is a hash of the histogram |
| 148 // name. We can then use this logging to find out what histogram name was |
| 149 // being hashed to a given MD5 value by just running the version of Chromium |
| 150 // in question with --enable-logging. |
| 151 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; |
| 152 |
| 153 return hash; |
| 154 } |
| 155 |
| 156 // static |
| 157 int64 MetricsLog::GetBuildTime() { |
| 158 static int64 integral_build_time = 0; |
| 159 if (!integral_build_time) { |
| 160 base::Time time; |
| 161 static const char kDateTime[] = __DATE__ " " __TIME__ " GMT"; |
| 162 bool result = base::Time::FromString(kDateTime, &time); |
| 163 DCHECK(result); |
| 164 integral_build_time = static_cast<int64>(time.ToTimeT()); |
| 165 } |
| 166 return integral_build_time; |
| 167 } |
| 168 |
| 169 // static |
| 170 int64 MetricsLog::GetCurrentTime() { |
| 171 return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds(); |
| 172 } |
| 173 |
| 174 void MetricsLog::RecordUserAction(const std::string& key) { |
| 175 DCHECK(!closed_); |
| 176 |
| 177 UserActionEventProto* user_action = uma_proto_.add_user_action_event(); |
| 178 user_action->set_name_hash(Hash(key)); |
| 179 user_action->set_time(GetCurrentTime()); |
| 180 } |
| 181 |
| 182 void MetricsLog::RecordHistogramDelta(const std::string& histogram_name, |
| 183 const base::HistogramSamples& snapshot) { |
| 184 DCHECK(!closed_); |
| 185 DCHECK_NE(0, snapshot.TotalCount()); |
| 186 |
| 187 // We will ignore the MAX_INT/infinite value in the last element of range[]. |
| 188 |
| 189 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event(); |
| 190 histogram_proto->set_name_hash(Hash(histogram_name)); |
| 191 histogram_proto->set_sum(snapshot.sum()); |
| 192 |
| 193 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done(); |
| 194 it->Next()) { |
| 195 base::Histogram::Sample min; |
| 196 base::Histogram::Sample max; |
| 197 base::Histogram::Count count; |
| 198 it->Get(&min, &max, &count); |
| 199 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket(); |
| 200 bucket->set_min(min); |
| 201 bucket->set_max(max); |
| 202 bucket->set_count(count); |
| 203 } |
| 204 |
| 205 // Omit fields to save space (see rules in histogram_event.proto comments). |
| 206 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { |
| 207 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); |
| 208 if (i + 1 < histogram_proto->bucket_size() && |
| 209 bucket->max() == histogram_proto->bucket(i + 1).min()) { |
| 210 bucket->clear_max(); |
| 211 } else if (bucket->max() == bucket->min() + 1) { |
| 212 bucket->clear_min(); |
| 213 } |
| 214 } |
| 215 } |
| 216 |
121 void MetricsLog::RecordStabilityMetrics( | 217 void MetricsLog::RecordStabilityMetrics( |
122 const std::vector<metrics::MetricsProvider*>& metrics_providers, | 218 const std::vector<metrics::MetricsProvider*>& metrics_providers, |
123 base::TimeDelta incremental_uptime, | 219 base::TimeDelta incremental_uptime, |
124 base::TimeDelta uptime) { | 220 base::TimeDelta uptime) { |
125 DCHECK(!locked()); | 221 DCHECK(!closed_); |
126 DCHECK(HasEnvironment()); | 222 DCHECK(HasEnvironment()); |
127 DCHECK(!HasStabilityMetrics()); | 223 DCHECK(!HasStabilityMetrics()); |
128 | 224 |
129 PrefService* pref = local_state_; | 225 PrefService* pref = local_state_; |
130 DCHECK(pref); | 226 DCHECK(pref); |
131 | 227 |
132 // Get stability attributes out of Local State, zeroing out stored values. | 228 // Get stability attributes out of Local State, zeroing out stored values. |
133 // NOTE: This could lead to some data loss if this report isn't successfully | 229 // NOTE: This could lead to some data loss if this report isn't successfully |
134 // sent, but that's true for all the metrics. | 230 // sent, but that's true for all the metrics. |
135 | 231 |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfile); | 420 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfile); |
325 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfileHash); | 421 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfileHash); |
326 | 422 |
327 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); | 423 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
328 std::string serialied_system_profile; | 424 std::string serialied_system_profile; |
329 return base::Base64Decode(base64_system_profile, &serialied_system_profile) && | 425 return base::Base64Decode(base64_system_profile, &serialied_system_profile) && |
330 ComputeSHA1(serialied_system_profile) == system_profile_hash && | 426 ComputeSHA1(serialied_system_profile) == system_profile_hash && |
331 system_profile->ParseFromString(serialied_system_profile); | 427 system_profile->ParseFromString(serialied_system_profile); |
332 } | 428 } |
333 | 429 |
| 430 void MetricsLog::CloseLog() { |
| 431 DCHECK(!closed_); |
| 432 closed_ = true; |
| 433 } |
| 434 |
| 435 void MetricsLog::GetEncodedLog(std::string* encoded_log) { |
| 436 DCHECK(closed_); |
| 437 uma_proto_.SerializeToString(encoded_log); |
| 438 } |
OLD | NEW |