OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/metrics/profiler_metrics_provider.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/tracked_objects.h" |
| 10 #include "components/metrics/metrics_log_base.h" |
| 11 #include "components/nacl/common/nacl_process_type.h" |
| 12 #include "content/public/common/process_type.h" |
| 13 |
| 14 using metrics::MetricsLogBase; |
| 15 using metrics::ProfilerEventProto; |
| 16 using tracked_objects::ProcessDataSnapshot; |
| 17 |
| 18 namespace { |
| 19 |
| 20 ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType( |
| 21 int process_type) { |
| 22 switch (process_type) { |
| 23 case content::PROCESS_TYPE_BROWSER: |
| 24 return ProfilerEventProto::TrackedObject::BROWSER; |
| 25 case content::PROCESS_TYPE_RENDERER: |
| 26 return ProfilerEventProto::TrackedObject::RENDERER; |
| 27 case content::PROCESS_TYPE_PLUGIN: |
| 28 return ProfilerEventProto::TrackedObject::PLUGIN; |
| 29 case content::PROCESS_TYPE_WORKER: |
| 30 return ProfilerEventProto::TrackedObject::WORKER; |
| 31 case content::PROCESS_TYPE_UTILITY: |
| 32 return ProfilerEventProto::TrackedObject::UTILITY; |
| 33 case content::PROCESS_TYPE_ZYGOTE: |
| 34 return ProfilerEventProto::TrackedObject::ZYGOTE; |
| 35 case content::PROCESS_TYPE_SANDBOX_HELPER: |
| 36 return ProfilerEventProto::TrackedObject::SANDBOX_HELPER; |
| 37 case content::PROCESS_TYPE_GPU: |
| 38 return ProfilerEventProto::TrackedObject::GPU; |
| 39 case content::PROCESS_TYPE_PPAPI_PLUGIN: |
| 40 return ProfilerEventProto::TrackedObject::PPAPI_PLUGIN; |
| 41 case content::PROCESS_TYPE_PPAPI_BROKER: |
| 42 return ProfilerEventProto::TrackedObject::PPAPI_BROKER; |
| 43 case PROCESS_TYPE_NACL_LOADER: |
| 44 return ProfilerEventProto::TrackedObject::NACL_LOADER; |
| 45 case PROCESS_TYPE_NACL_BROKER: |
| 46 return ProfilerEventProto::TrackedObject::NACL_BROKER; |
| 47 default: |
| 48 NOTREACHED(); |
| 49 return ProfilerEventProto::TrackedObject::UNKNOWN; |
| 50 } |
| 51 } |
| 52 |
| 53 // Maps a thread name by replacing trailing sequence of digits with "*". |
| 54 // Examples: |
| 55 // 1. "BrowserBlockingWorker1/23857" => "BrowserBlockingWorker1/*" |
| 56 // 2. "Chrome_IOThread" => "Chrome_IOThread" |
| 57 std::string MapThreadName(const std::string& thread_name) { |
| 58 size_t i = thread_name.length(); |
| 59 |
| 60 while (i > 0 && isdigit(thread_name[i - 1])) { |
| 61 --i; |
| 62 } |
| 63 |
| 64 if (i == thread_name.length()) |
| 65 return thread_name; |
| 66 |
| 67 return thread_name.substr(0, i) + '*'; |
| 68 } |
| 69 |
| 70 // Normalizes a source filename (which is platform- and build-method-dependent) |
| 71 // by extracting the last component of the full file name. |
| 72 // Example: "c:\b\build\slave\win\build\src\chrome\app\chrome_main.cc" => |
| 73 // "chrome_main.cc". |
| 74 std::string NormalizeFileName(const std::string& file_name) { |
| 75 const size_t offset = file_name.find_last_of("\\/"); |
| 76 return offset != std::string::npos ? file_name.substr(offset + 1) : file_name; |
| 77 } |
| 78 |
| 79 void WriteProfilerData(const ProcessDataSnapshot& profiler_data, |
| 80 int process_type, |
| 81 ProfilerEventProto* performance_profile) { |
| 82 for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it = |
| 83 profiler_data.tasks.begin(); |
| 84 it != profiler_data.tasks.end(); |
| 85 ++it) { |
| 86 const tracked_objects::DeathDataSnapshot& death_data = it->death_data; |
| 87 ProfilerEventProto::TrackedObject* tracked_object = |
| 88 performance_profile->add_tracked_object(); |
| 89 tracked_object->set_birth_thread_name_hash( |
| 90 MetricsLogBase::Hash(MapThreadName(it->birth.thread_name))); |
| 91 tracked_object->set_exec_thread_name_hash( |
| 92 MetricsLogBase::Hash(MapThreadName(it->death_thread_name))); |
| 93 tracked_object->set_source_file_name_hash( |
| 94 MetricsLogBase::Hash(NormalizeFileName(it->birth.location.file_name))); |
| 95 tracked_object->set_source_function_name_hash( |
| 96 MetricsLogBase::Hash(it->birth.location.function_name)); |
| 97 tracked_object->set_source_line_number(it->birth.location.line_number); |
| 98 tracked_object->set_exec_count(death_data.count); |
| 99 tracked_object->set_exec_time_total(death_data.run_duration_sum); |
| 100 tracked_object->set_exec_time_sampled(death_data.run_duration_sample); |
| 101 tracked_object->set_queue_time_total(death_data.queue_duration_sum); |
| 102 tracked_object->set_queue_time_sampled(death_data.queue_duration_sample); |
| 103 tracked_object->set_process_type(AsProtobufProcessType(process_type)); |
| 104 tracked_object->set_process_id(profiler_data.process_id); |
| 105 } |
| 106 } |
| 107 |
| 108 } // namespace |
| 109 |
| 110 ProfilerMetricsProvider::ProfilerMetricsProvider() { |
| 111 } |
| 112 |
| 113 ProfilerMetricsProvider::~ProfilerMetricsProvider() { |
| 114 } |
| 115 |
| 116 void ProfilerMetricsProvider::ProvideGeneralMetrics( |
| 117 metrics::ChromeUserMetricsExtension* uma_proto) { |
| 118 if (tracked_objects::GetTimeSourceType() != |
| 119 tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) { |
| 120 // We currently only support the default time source, wall clock time. |
| 121 return; |
| 122 } |
| 123 |
| 124 ProfilerEventProto* profile; |
| 125 if (!uma_proto->profiler_event_size()) { |
| 126 // For the first process's data, add a new field to the protocol buffer. |
| 127 profile = uma_proto->add_profiler_event(); |
| 128 profile->set_profile_type(ProfilerEventProto::STARTUP_PROFILE); |
| 129 profile->set_time_source(ProfilerEventProto::WALL_CLOCK_TIME); |
| 130 } else { |
| 131 // For the remaining calls, re-use the existing field. |
| 132 profile = uma_proto->mutable_profiler_event(0); |
| 133 } |
| 134 |
| 135 // Note that |profile| might already contain data, so it's important to do a |
| 136 // merge rather than an overwrite here. |
| 137 profile->MergeFrom(profiler_event_cache_); |
| 138 |
| 139 // Clear the cache so that this data doesn't get propagated to any different |
| 140 // log that later calls ProvideGeneralMetrics(). |
| 141 profiler_event_cache_.Clear(); |
| 142 } |
| 143 |
| 144 void ProfilerMetricsProvider::RecordProfilerData( |
| 145 const tracked_objects::ProcessDataSnapshot& process_data, |
| 146 int process_type) { |
| 147 // TODO(blundell): What to do about this? |
| 148 // DCHECK(!locked()); |
| 149 |
| 150 if (tracked_objects::GetTimeSourceType() != |
| 151 tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) { |
| 152 // We currently only support the default time source, wall clock time. |
| 153 return; |
| 154 } |
| 155 |
| 156 WriteProfilerData(process_data, process_type, &profiler_event_cache_); |
| 157 } |
OLD | NEW |