isherman@chromium.org | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
ben@chromium.org | cd1adc2 | 2009-01-16 01:29:22 | [diff] [blame] | 5 | #include "chrome/browser/metrics/metrics_log.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
girard@chromium.org | 76869ff | 2013-01-15 16:13:47 | [diff] [blame] | 7 | #include <algorithm> |
thestig@chromium.org | 1eeb5e0 | 2010-07-20 23:02:11 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 11 | #include "base/base64.h" |
deanm@chromium.org | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 12 | #include "base/basictypes.h" |
keybuk@chromium.org | 611ae29a | 2013-04-29 21:32:19 | [diff] [blame] | 13 | #include "base/bind.h" |
simonjam@chromium.org | 5c8f89f69 | 2013-07-18 11:13:28 | [diff] [blame] | 14 | #include "base/cpu.h" |
levin@chromium.org | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
joi@chromium.org | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 16 | #include "base/prefs/pref_registry_simple.h" |
| 17 | #include "base/prefs/pref_service.h" |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 18 | #include "base/profiler/alternate_timer.h" |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 19 | #include "base/sha1.h" |
brettw@chromium.org | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
avi@chromium.org | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 21 | #include "base/strings/string_util.h" |
avi@chromium.org | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 22 | #include "base/strings/utf_string_conversions.h" |
deanm@chromium.org | fadf97f | 2008-09-18 12:18:14 | [diff] [blame] | 23 | #include "base/sys_info.h" |
jar@chromium.org | 37f39e4 | 2010-01-06 17:35:17 | [diff] [blame] | 24 | #include "base/third_party/nspr/prtime.h" |
avi@chromium.org | 8481347 | 2013-06-28 00:25:19 | [diff] [blame] | 25 | #include "base/time/time.h" |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 26 | #include "base/tracked_objects.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | #include "chrome/browser/browser_process.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | #include "chrome/common/pref_names.h" |
asvitkine@chromium.org | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 29 | #include "components/metrics/metrics_provider.h" |
isherman@chromium.org | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 30 | #include "components/metrics/metrics_service_client.h" |
bsimonnet@chromium.org | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 31 | #include "components/metrics/proto/profiler_event.pb.h" |
| 32 | #include "components/metrics/proto/system_profile.pb.h" |
yael.aharon@intel.com | d5d38325 | 2013-07-04 14:44:32 | [diff] [blame] | 33 | #include "components/nacl/common/nacl_process_type.h" |
asvitkine@chromium.org | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 34 | #include "components/variations/active_field_trials.h" |
isherman@chromium.org | 1a838cc | 2012-04-24 22:06:49 | [diff] [blame] | 35 | #include "content/public/common/content_client.h" |
tfarina@chromium.org | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 36 | #include "url/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | |
dfalcantara@chromium.org | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 38 | #if defined(OS_ANDROID) |
| 39 | #include "base/android/build_info.h" |
| 40 | #endif |
| 41 | |
deanm@chromium.org | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 42 | #if defined(OS_WIN) |
isherman@chromium.org | 1bb25e0 | 2012-08-03 22:39:39 | [diff] [blame] | 43 | #include "base/win/metro.h" |
| 44 | |
| 45 | // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
deanm@chromium.org | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 46 | extern "C" IMAGE_DOS_HEADER __ImageBase; |
| 47 | #endif |
| 48 | |
bsimonnet@chromium.org | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 49 | using metrics::MetricsLogBase; |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 50 | using metrics::ProfilerEventProto; |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 51 | using metrics::SystemProfileProto; |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 52 | using tracked_objects::ProcessDataSnapshot; |
asvitkine@chromium.org | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 53 | typedef variations::ActiveGroupId ActiveGroupId; |
jam@chromium.org | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 54 | |
isherman@chromium.org | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 55 | namespace { |
| 56 | |
| 57 | // Returns the date at which the current metrics client ID was created as |
isherman@chromium.org | ca5ac4b | 2012-12-14 07:46:40 | [diff] [blame] | 58 | // a string containing seconds since the epoch, or "0" if none was found. |
isherman@chromium.org | cc5d7f4 | 2012-10-30 00:30:09 | [diff] [blame] | 59 | std::string GetMetricsEnabledDate(PrefService* pref) { |
isherman@chromium.org | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 60 | if (!pref) { |
isherman@chromium.org | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 61 | NOTREACHED(); |
| 62 | return "0"; |
| 63 | } |
isherman@chromium.org | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 64 | |
asvitkine@chromium.org | 1f74e80 | 2014-03-29 03:08:35 | [diff] [blame] | 65 | return pref->GetString(prefs::kMetricsReportingEnabledTimestamp); |
isherman@chromium.org | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 66 | } |
| 67 | |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 68 | ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType( |
jam@chromium.org | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 69 | int process_type) { |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 70 | switch (process_type) { |
| 71 | case content::PROCESS_TYPE_BROWSER: |
| 72 | return ProfilerEventProto::TrackedObject::BROWSER; |
| 73 | case content::PROCESS_TYPE_RENDERER: |
| 74 | return ProfilerEventProto::TrackedObject::RENDERER; |
| 75 | case content::PROCESS_TYPE_PLUGIN: |
| 76 | return ProfilerEventProto::TrackedObject::PLUGIN; |
| 77 | case content::PROCESS_TYPE_WORKER: |
| 78 | return ProfilerEventProto::TrackedObject::WORKER; |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 79 | case content::PROCESS_TYPE_UTILITY: |
| 80 | return ProfilerEventProto::TrackedObject::UTILITY; |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 81 | case content::PROCESS_TYPE_ZYGOTE: |
| 82 | return ProfilerEventProto::TrackedObject::ZYGOTE; |
| 83 | case content::PROCESS_TYPE_SANDBOX_HELPER: |
| 84 | return ProfilerEventProto::TrackedObject::SANDBOX_HELPER; |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 85 | case content::PROCESS_TYPE_GPU: |
| 86 | return ProfilerEventProto::TrackedObject::GPU; |
| 87 | case content::PROCESS_TYPE_PPAPI_PLUGIN: |
| 88 | return ProfilerEventProto::TrackedObject::PPAPI_PLUGIN; |
| 89 | case content::PROCESS_TYPE_PPAPI_BROKER: |
| 90 | return ProfilerEventProto::TrackedObject::PPAPI_BROKER; |
jam@chromium.org | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 91 | case PROCESS_TYPE_NACL_LOADER: |
| 92 | return ProfilerEventProto::TrackedObject::NACL_LOADER; |
| 93 | case PROCESS_TYPE_NACL_BROKER: |
| 94 | return ProfilerEventProto::TrackedObject::NACL_BROKER; |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 95 | default: |
| 96 | NOTREACHED(); |
| 97 | return ProfilerEventProto::TrackedObject::UNKNOWN; |
| 98 | } |
| 99 | } |
| 100 | |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 101 | // Computes a SHA-1 hash of |data| and returns it as a hex string. |
| 102 | std::string ComputeSHA1(const std::string& data) { |
| 103 | const std::string sha1 = base::SHA1HashString(data); |
| 104 | return base::HexEncode(sha1.data(), sha1.size()); |
| 105 | } |
| 106 | |
asvitkine@chromium.org | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 107 | void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids, |
isherman@chromium.org | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 108 | SystemProfileProto* system_profile) { |
asvitkine@chromium.org | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 109 | for (std::vector<ActiveGroupId>::const_iterator it = |
stevet@chromium.org | ad2461c | 2012-04-27 21:11:03 | [diff] [blame] | 110 | field_trial_ids.begin(); it != field_trial_ids.end(); ++it) { |
isherman@chromium.org | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 111 | SystemProfileProto::FieldTrial* field_trial = |
| 112 | system_profile->add_field_trial(); |
| 113 | field_trial->set_name_id(it->name); |
| 114 | field_trial->set_group_id(it->group); |
| 115 | } |
| 116 | } |
| 117 | |
vadimt@chromium.org | 0018f72 | 2014-02-08 03:08:51 | [diff] [blame] | 118 | // Maps a thread name by replacing trailing sequence of digits with "*". |
| 119 | // Examples: |
| 120 | // 1. "BrowserBlockingWorker1/23857" => "BrowserBlockingWorker1/*" |
| 121 | // 2. "Chrome_IOThread" => "Chrome_IOThread" |
| 122 | std::string MapThreadName(const std::string& thread_name) { |
| 123 | size_t i = thread_name.length(); |
| 124 | |
| 125 | while (i > 0 && isdigit(thread_name[i - 1])) { |
| 126 | --i; |
| 127 | } |
| 128 | |
| 129 | if (i == thread_name.length()) |
| 130 | return thread_name; |
| 131 | |
| 132 | return thread_name.substr(0, i) + '*'; |
| 133 | } |
| 134 | |
vadimt@chromium.org | 87b9bf1 | 2014-04-03 15:25:00 | [diff] [blame] | 135 | // Normalizes a source filename (which is platform- and build-method-dependent) |
| 136 | // by extracting the last component of the full file name. |
| 137 | // Example: "c:\b\build\slave\win\build\src\chrome\app\chrome_main.cc" => |
| 138 | // "chrome_main.cc". |
| 139 | std::string NormalizeFileName(const std::string& file_name) { |
| 140 | const size_t offset = file_name.find_last_of("\\/"); |
| 141 | return offset != std::string::npos ? file_name.substr(offset + 1) : file_name; |
| 142 | } |
| 143 | |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 144 | void WriteProfilerData(const ProcessDataSnapshot& profiler_data, |
jam@chromium.org | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 145 | int process_type, |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 146 | ProfilerEventProto* performance_profile) { |
| 147 | for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it = |
| 148 | profiler_data.tasks.begin(); |
| 149 | it != profiler_data.tasks.end(); ++it) { |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 150 | const tracked_objects::DeathDataSnapshot& death_data = it->death_data; |
| 151 | ProfilerEventProto::TrackedObject* tracked_object = |
| 152 | performance_profile->add_tracked_object(); |
isherman@chromium.org | db7fec83 | 2013-07-18 09:41:41 | [diff] [blame] | 153 | tracked_object->set_birth_thread_name_hash( |
vadimt@chromium.org | 0018f72 | 2014-02-08 03:08:51 | [diff] [blame] | 154 | MetricsLogBase::Hash(MapThreadName(it->birth.thread_name))); |
isherman@chromium.org | db7fec83 | 2013-07-18 09:41:41 | [diff] [blame] | 155 | tracked_object->set_exec_thread_name_hash( |
vadimt@chromium.org | 0018f72 | 2014-02-08 03:08:51 | [diff] [blame] | 156 | MetricsLogBase::Hash(MapThreadName(it->death_thread_name))); |
isherman@chromium.org | db7fec83 | 2013-07-18 09:41:41 | [diff] [blame] | 157 | tracked_object->set_source_file_name_hash( |
vadimt@chromium.org | 87b9bf1 | 2014-04-03 15:25:00 | [diff] [blame] | 158 | MetricsLogBase::Hash(NormalizeFileName(it->birth.location.file_name))); |
isherman@chromium.org | db7fec83 | 2013-07-18 09:41:41 | [diff] [blame] | 159 | tracked_object->set_source_function_name_hash( |
| 160 | MetricsLogBase::Hash(it->birth.location.function_name)); |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 161 | tracked_object->set_source_line_number(it->birth.location.line_number); |
| 162 | tracked_object->set_exec_count(death_data.count); |
| 163 | tracked_object->set_exec_time_total(death_data.run_duration_sum); |
| 164 | tracked_object->set_exec_time_sampled(death_data.run_duration_sample); |
| 165 | tracked_object->set_queue_time_total(death_data.queue_duration_sum); |
| 166 | tracked_object->set_queue_time_sampled(death_data.queue_duration_sample); |
| 167 | tracked_object->set_process_type(AsProtobufProcessType(process_type)); |
| 168 | tracked_object->set_process_id(profiler_data.process_id); |
| 169 | } |
| 170 | } |
| 171 | |
rkaplow@chromium.org | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 172 | // Round a timestamp measured in seconds since epoch to one with a granularity |
| 173 | // of an hour. This can be used before uploaded potentially sensitive |
| 174 | // timestamps. |
| 175 | int64 RoundSecondsToHour(int64 time_in_seconds) { |
| 176 | return 3600 * (time_in_seconds / 3600); |
| 177 | } |
| 178 | |
isherman@chromium.org | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 179 | } // namespace |
| 180 | |
asvitkine@chromium.org | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 181 | MetricsLog::MetricsLog(const std::string& client_id, |
| 182 | int session_id, |
isherman@chromium.org | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 183 | LogType log_type, |
blundell@chromium.org | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 184 | metrics::MetricsServiceClient* client, |
| 185 | PrefService* local_state) |
isherman@chromium.org | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 186 | : MetricsLogBase(client_id, |
| 187 | session_id, |
| 188 | log_type, |
| 189 | client->GetVersionString()), |
| 190 | client_(client), |
blundell@chromium.org | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 191 | creation_time_(base::TimeTicks::Now()), |
| 192 | local_state_(local_state) { |
isherman@chromium.org | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 193 | uma_proto()->mutable_system_profile()->set_channel(client_->GetChannel()); |
tengs@chromium.org | afc03f0 | 2013-10-11 06:01:35 | [diff] [blame] | 194 | } |
jar@chromium.org | 5ed7d457 | 2009-12-23 17:42:41 | [diff] [blame] | 195 | |
ananta@chromium.org | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 196 | MetricsLog::~MetricsLog() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 197 | |
asvitkine@chromium.org | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 198 | void MetricsLog::RecordStabilityMetrics( |
| 199 | const std::vector<metrics::MetricsProvider*>& metrics_providers, |
| 200 | base::TimeDelta incremental_uptime, |
| 201 | base::TimeDelta uptime) { |
isherman@chromium.org | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 202 | DCHECK(!locked()); |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 203 | DCHECK(HasEnvironment()); |
| 204 | DCHECK(!HasStabilityMetrics()); |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 205 | |
blundell@chromium.org | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 206 | PrefService* pref = local_state_; |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 207 | DCHECK(pref); |
| 208 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | // Get stability attributes out of Local State, zeroing out stored values. |
| 210 | // NOTE: This could lead to some data loss if this report isn't successfully |
| 211 | // sent, but that's true for all the metrics. |
| 212 | |
jar@google.com | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 213 | WriteRequiredStabilityAttributes(pref); |
asvitkine@chromium.org | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 214 | |
| 215 | // Record recent delta for critical stability metrics. We can't wait for a |
| 216 | // restart to gather these, as that delay biases our observation away from |
| 217 | // users that run happily for a looooong time. We send increments with each |
| 218 | // uma log upload, just as we send histogram data. |
mpearson@chromium.org | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 219 | WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | |
asvitkine@chromium.org | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 221 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
asvitkine@chromium.org | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 222 | for (size_t i = 0; i < metrics_providers.size(); ++i) |
asvitkine@chromium.org | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 223 | metrics_providers[i]->ProvideStabilityMetrics(system_profile); |
asvitkine@chromium.org | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 224 | |
asvitkine@chromium.org | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 225 | // Omit some stats unless this is the initial stability log. |
asvitkine@chromium.org | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 226 | if (log_type() != INITIAL_STABILITY_LOG) |
asvitkine@chromium.org | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 227 | return; |
| 228 | |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 229 | int incomplete_shutdown_count = |
| 230 | pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount); |
cpu@chromium.org | e324f6b | 2012-02-28 05:43:37 | [diff] [blame] | 231 | pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 232 | int breakpad_registration_success_count = |
| 233 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess); |
cpu@chromium.org | e324f6b | 2012-02-28 05:43:37 | [diff] [blame] | 234 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 235 | int breakpad_registration_failure_count = |
| 236 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail); |
cpu@chromium.org | e324f6b | 2012-02-28 05:43:37 | [diff] [blame] | 237 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 238 | int debugger_present_count = |
| 239 | pref->GetInteger(prefs::kStabilityDebuggerPresent); |
cpu@chromium.org | e324f6b | 2012-02-28 05:43:37 | [diff] [blame] | 240 | pref->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 241 | int debugger_not_present_count = |
| 242 | pref->GetInteger(prefs::kStabilityDebuggerNotPresent); |
cpu@chromium.org | e324f6b | 2012-02-28 05:43:37 | [diff] [blame] | 243 | pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
isherman@chromium.org | b2a4812d | 2012-02-28 05:31:31 | [diff] [blame] | 244 | |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 245 | // TODO(jar): The following are all optional, so we *could* optimize them for |
| 246 | // values of zero (and not include them). |
asvitkine@chromium.org | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 247 | SystemProfileProto::Stability* stability = |
| 248 | system_profile->mutable_stability(); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 249 | stability->set_incomplete_shutdown_count(incomplete_shutdown_count); |
| 250 | stability->set_breakpad_registration_success_count( |
| 251 | breakpad_registration_success_count); |
| 252 | stability->set_breakpad_registration_failure_count( |
| 253 | breakpad_registration_failure_count); |
| 254 | stability->set_debugger_present_count(debugger_present_count); |
| 255 | stability->set_debugger_not_present_count(debugger_not_present_count); |
asvitkine@chromium.org | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 256 | } |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 257 | |
asvitkine@chromium.org | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 258 | void MetricsLog::RecordGeneralMetrics( |
| 259 | const std::vector<metrics::MetricsProvider*>& metrics_providers) { |
| 260 | for (size_t i = 0; i < metrics_providers.size(); ++i) |
| 261 | metrics_providers[i]->ProvideGeneralMetrics(uma_proto()); |
| 262 | } |
| 263 | |
asvitkine@chromium.org | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 264 | void MetricsLog::GetFieldTrialIds( |
| 265 | std::vector<ActiveGroupId>* field_trial_ids) const { |
asvitkine@chromium.org | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 266 | variations::GetFieldTrialActiveGroupIds(field_trial_ids); |
jar@google.com | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 267 | } |
| 268 | |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 269 | bool MetricsLog::HasEnvironment() const { |
| 270 | return uma_proto()->system_profile().has_uma_enabled_date(); |
| 271 | } |
| 272 | |
| 273 | bool MetricsLog::HasStabilityMetrics() const { |
| 274 | return uma_proto()->system_profile().stability().has_launch_count(); |
| 275 | } |
| 276 | |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 277 | // The server refuses data that doesn't have certain values. crashcount and |
| 278 | // launchcount are currently "required" in the "stability" group. |
| 279 | // TODO(isherman): Stop writing these attributes specially once the migration to |
| 280 | // protobufs is complete. |
jar@google.com | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 281 | void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) { |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 282 | int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount); |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 283 | pref->SetInteger(prefs::kStabilityLaunchCount, 0); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 284 | int crash_count = pref->GetInteger(prefs::kStabilityCrashCount); |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 285 | pref->SetInteger(prefs::kStabilityCrashCount, 0); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 286 | |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 287 | SystemProfileProto::Stability* stability = |
isherman@chromium.org | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 288 | uma_proto()->mutable_system_profile()->mutable_stability(); |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 289 | stability->set_launch_count(launch_count); |
| 290 | stability->set_crash_count(crash_count); |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 291 | } |
| 292 | |
asvitkine@chromium.org | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 293 | void MetricsLog::WriteRealtimeStabilityAttributes( |
| 294 | PrefService* pref, |
mpearson@chromium.org | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 295 | base::TimeDelta incremental_uptime, |
| 296 | base::TimeDelta uptime) { |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 297 | // Update the stats which are critical for real-time stability monitoring. |
| 298 | // Since these are "optional," only list ones that are non-zero, as the counts |
asvitkine@chromium.org | 250f7b66 | 2013-11-23 02:36:51 | [diff] [blame] | 299 | // are aggregated (summed) server side. |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 300 | |
isherman@chromium.org | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 301 | SystemProfileProto::Stability* stability = |
isherman@chromium.org | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 302 | uma_proto()->mutable_system_profile()->mutable_stability(); |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 303 | |
mpearson@chromium.org | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 304 | const uint64 incremental_uptime_sec = incremental_uptime.InSeconds(); |
| 305 | if (incremental_uptime_sec) |
| 306 | stability->set_incremental_uptime_sec(incremental_uptime_sec); |
| 307 | const uint64 uptime_sec = uptime.InSeconds(); |
asvitkine@chromium.org | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 308 | if (uptime_sec) |
| 309 | stability->set_uptime_sec(uptime_sec); |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 310 | } |
| 311 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 312 | void MetricsLog::RecordEnvironment( |
asvitkine@chromium.org | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 313 | const std::vector<metrics::MetricsProvider*>& metrics_providers, |
asvitkine@chromium.org | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 314 | const std::vector<variations::ActiveGroupId>& synthetic_trials) { |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 315 | DCHECK(!HasEnvironment()); |
| 316 | |
isherman@chromium.org | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 317 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
mpearson@chromium.org | 24b9bb39 | 2013-01-29 20:29:29 | [diff] [blame] | 318 | |
| 319 | std::string brand_code; |
isherman@chromium.org | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 320 | if (client_->GetBrand(&brand_code)) |
mpearson@chromium.org | 24b9bb39 | 2013-01-29 20:29:29 | [diff] [blame] | 321 | system_profile->set_brand_code(brand_code); |
| 322 | |
isherman@chromium.org | cc5d7f4 | 2012-10-30 00:30:09 | [diff] [blame] | 323 | int enabled_date; |
blundell@chromium.org | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 324 | bool success = |
| 325 | base::StringToInt(GetMetricsEnabledDate(local_state_), &enabled_date); |
isherman@chromium.org | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 326 | DCHECK(success); |
rkaplow@chromium.org | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 327 | |
| 328 | // Reduce granularity of the enabled_date field to nearest hour. |
| 329 | system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date)); |
| 330 | |
blundell@chromium.org | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 331 | int64 install_date = local_state_->GetInt64(prefs::kInstallDate); |
rkaplow@chromium.org | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 332 | |
| 333 | // Reduce granularity of the install_date field to nearest hour. |
| 334 | system_profile->set_install_date(RoundSecondsToHour(install_date)); |
isherman@chromium.org | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 335 | |
isherman@chromium.org | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 336 | system_profile->set_application_locale(client_->GetApplicationLocale()); |
isherman@chromium.org | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 337 | |
| 338 | SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware(); |
blundell@chromium.org | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame^] | 339 | |
| 340 | // By default, the hardware class is empty (i.e., unknown). |
| 341 | hardware->set_hardware_class(std::string()); |
| 342 | |
thestig@chromium.org | 0b6a4fb | 2012-10-16 01:58:21 | [diff] [blame] | 343 | hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture()); |
isherman@chromium.org | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 344 | hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB()); |
| 345 | #if defined(OS_WIN) |
| 346 | hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase)); |
| 347 | #endif |
| 348 | |
isherman@chromium.org | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 349 | SystemProfileProto::OS* os = system_profile->mutable_os(); |
isherman@chromium.org | 1bb25e0 | 2012-08-03 22:39:39 | [diff] [blame] | 350 | std::string os_name = base::SysInfo::OperatingSystemName(); |
| 351 | #if defined(OS_WIN) |
| 352 | // TODO(mad): This only checks whether the main process is a Metro process at |
| 353 | // upload time; not whether the collected metrics were all gathered from |
| 354 | // Metro. This is ok as an approximation for now, since users will rarely be |
| 355 | // switching from Metro to Desktop mode; but we should re-evaluate whether we |
| 356 | // can distinguish metrics more cleanly in the future: http://crbug.com/140568 |
| 357 | if (base::win::IsMetroProcess()) |
| 358 | os_name += " (Metro)"; |
| 359 | #endif |
| 360 | os->set_name(os_name); |
isherman@chromium.org | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 361 | os->set_version(base::SysInfo::OperatingSystemVersion()); |
dfalcantara@chromium.org | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 362 | #if defined(OS_ANDROID) |
| 363 | os->set_fingerprint( |
| 364 | base::android::BuildInfo::GetInstance()->android_build_fp()); |
| 365 | #endif |
isherman@chromium.org | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 366 | |
simonjam@chromium.org | 5c8f89f69 | 2013-07-18 11:13:28 | [diff] [blame] | 367 | base::CPU cpu_info; |
| 368 | SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu(); |
| 369 | cpu->set_vendor_name(cpu_info.vendor_name()); |
| 370 | cpu->set_signature(cpu_info.signature()); |
| 371 | |
asvitkine@chromium.org | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 372 | std::vector<ActiveGroupId> field_trial_ids; |
isherman@chromium.org | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 373 | GetFieldTrialIds(&field_trial_ids); |
| 374 | WriteFieldTrials(field_trial_ids, system_profile); |
bengr@chromium.org | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 375 | WriteFieldTrials(synthetic_trials, system_profile); |
asharif@chromium.org | f65859e | 2013-02-04 20:00:25 | [diff] [blame] | 376 | |
asvitkine@chromium.org | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 377 | for (size_t i = 0; i < metrics_providers.size(); ++i) |
| 378 | metrics_providers[i]->ProvideSystemProfileMetrics(system_profile); |
| 379 | |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 380 | std::string serialied_system_profile; |
| 381 | std::string base64_system_profile; |
vadimt@chromium.org | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 382 | if (system_profile->SerializeToString(&serialied_system_profile)) { |
| 383 | base::Base64Encode(serialied_system_profile, &base64_system_profile); |
blundell@chromium.org | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 384 | PrefService* local_state = local_state_; |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 385 | local_state->SetString(prefs::kStabilitySavedSystemProfile, |
| 386 | base64_system_profile); |
| 387 | local_state->SetString(prefs::kStabilitySavedSystemProfileHash, |
| 388 | ComputeSHA1(serialied_system_profile)); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | bool MetricsLog::LoadSavedEnvironmentFromPrefs() { |
blundell@chromium.org | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 393 | PrefService* local_state = local_state_; |
asvitkine@chromium.org | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 394 | const std::string base64_system_profile = |
| 395 | local_state->GetString(prefs::kStabilitySavedSystemProfile); |
| 396 | if (base64_system_profile.empty()) |
| 397 | return false; |
| 398 | |
| 399 | const std::string system_profile_hash = |
| 400 | local_state->GetString(prefs::kStabilitySavedSystemProfileHash); |
| 401 | local_state->ClearPref(prefs::kStabilitySavedSystemProfile); |
| 402 | local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash); |
| 403 | |
| 404 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
| 405 | std::string serialied_system_profile; |
| 406 | return base::Base64Decode(base64_system_profile, &serialied_system_profile) && |
| 407 | ComputeSHA1(serialied_system_profile) == system_profile_hash && |
| 408 | system_profile->ParseFromString(serialied_system_profile); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | } |
| 410 | |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 411 | void MetricsLog::RecordProfilerData( |
| 412 | const tracked_objects::ProcessDataSnapshot& process_data, |
jam@chromium.org | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 413 | int process_type) { |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 414 | DCHECK(!locked()); |
| 415 | |
isherman@chromium.org | 8f82968 | 2012-04-27 00:36:49 | [diff] [blame] | 416 | if (tracked_objects::GetTimeSourceType() != |
| 417 | tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) { |
isherman@chromium.org | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 418 | // We currently only support the default time source, wall clock time. |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | ProfilerEventProto* profile; |
| 423 | if (!uma_proto()->profiler_event_size()) { |
| 424 | // For the first process's data, add a new field to the protocol buffer. |
| 425 | profile = uma_proto()->add_profiler_event(); |
| 426 | profile->set_profile_type(ProfilerEventProto::STARTUP_PROFILE); |
| 427 | profile->set_time_source(ProfilerEventProto::WALL_CLOCK_TIME); |
| 428 | } else { |
| 429 | // For the remaining calls, re-use the existing field. |
| 430 | profile = uma_proto()->mutable_profiler_event(0); |
| 431 | } |
| 432 | |
| 433 | WriteProfilerData(process_data, process_type, profile); |
| 434 | } |