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 <ctype.h> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/tracked_objects.h" | |
12 #include "components/metrics/metrics_log.h" | |
13 #include "components/nacl/common/nacl_process_type.h" | |
14 #include "content/public/common/process_type.h" | |
15 | |
16 using metrics::ProfilerEventProto; | |
17 using tracked_objects::ProcessDataSnapshot; | |
18 | |
19 namespace { | |
20 | |
21 ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType( | |
22 int process_type) { | |
23 switch (process_type) { | |
24 case content::PROCESS_TYPE_BROWSER: | |
25 return ProfilerEventProto::TrackedObject::BROWSER; | |
26 case content::PROCESS_TYPE_RENDERER: | |
27 return ProfilerEventProto::TrackedObject::RENDERER; | |
28 case content::PROCESS_TYPE_PLUGIN: | |
29 return ProfilerEventProto::TrackedObject::PLUGIN; | |
30 case content::PROCESS_TYPE_UTILITY: | |
31 return ProfilerEventProto::TrackedObject::UTILITY; | |
32 case content::PROCESS_TYPE_ZYGOTE: | |
33 return ProfilerEventProto::TrackedObject::ZYGOTE; | |
34 case content::PROCESS_TYPE_SANDBOX_HELPER: | |
35 return ProfilerEventProto::TrackedObject::SANDBOX_HELPER; | |
36 case content::PROCESS_TYPE_GPU: | |
37 return ProfilerEventProto::TrackedObject::GPU; | |
38 case content::PROCESS_TYPE_PPAPI_PLUGIN: | |
39 return ProfilerEventProto::TrackedObject::PPAPI_PLUGIN; | |
40 case content::PROCESS_TYPE_PPAPI_BROKER: | |
41 return ProfilerEventProto::TrackedObject::PPAPI_BROKER; | |
42 case PROCESS_TYPE_NACL_LOADER: | |
43 return ProfilerEventProto::TrackedObject::NACL_LOADER; | |
44 case PROCESS_TYPE_NACL_BROKER: | |
45 return ProfilerEventProto::TrackedObject::NACL_BROKER; | |
46 default: | |
47 NOTREACHED(); | |
48 return ProfilerEventProto::TrackedObject::UNKNOWN; | |
49 } | |
50 } | |
51 | |
52 // Maps a thread name by replacing trailing sequence of digits with "*". | |
53 // Examples: | |
54 // 1. "BrowserBlockingWorker1/23857" => "BrowserBlockingWorker1/*" | |
55 // 2. "Chrome_IOThread" => "Chrome_IOThread" | |
56 std::string MapThreadName(const std::string& thread_name) { | |
57 size_t i = thread_name.length(); | |
58 | |
59 while (i > 0 && isdigit(thread_name[i - 1])) { | |
60 --i; | |
61 } | |
62 | |
63 if (i == thread_name.length()) | |
64 return thread_name; | |
65 | |
66 return thread_name.substr(0, i) + '*'; | |
67 } | |
68 | |
69 // Normalizes a source filename (which is platform- and build-method-dependent) | |
70 // by extracting the last component of the full file name. | |
71 // Example: "c:\b\build\slave\win\build\src\chrome\app\chrome_main.cc" => | |
72 // "chrome_main.cc". | |
73 std::string NormalizeFileName(const std::string& file_name) { | |
74 const size_t offset = file_name.find_last_of("\\/"); | |
75 return offset != std::string::npos ? file_name.substr(offset + 1) : file_name; | |
76 } | |
77 | |
78 void WriteProfilerData(const ProcessDataSnapshot& profiler_data, | |
79 int process_type, | |
80 ProfilerEventProto* performance_profile) { | |
81 for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it = | |
82 profiler_data.tasks.begin(); | |
83 it != profiler_data.tasks.end(); | |
84 ++it) { | |
85 const tracked_objects::DeathDataSnapshot& death_data = it->death_data; | |
86 ProfilerEventProto::TrackedObject* tracked_object = | |
87 performance_profile->add_tracked_object(); | |
88 tracked_object->set_birth_thread_name_hash( | |
89 metrics::MetricsLog::Hash(MapThreadName(it->birth.thread_name))); | |
90 tracked_object->set_exec_thread_name_hash( | |
91 metrics::MetricsLog::Hash(MapThreadName(it->death_thread_name))); | |
92 tracked_object->set_source_file_name_hash( | |
93 metrics::MetricsLog::Hash(NormalizeFileName( | |
94 it->birth.location.file_name))); | |
95 tracked_object->set_source_function_name_hash( | |
96 metrics::MetricsLog::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() : has_profiler_data_(false) { | |
111 } | |
112 | |
113 ProfilerMetricsProvider::~ProfilerMetricsProvider() { | |
114 } | |
115 | |
116 void ProfilerMetricsProvider::ProvideGeneralMetrics( | |
117 metrics::ChromeUserMetricsExtension* uma_proto) { | |
118 if (!has_profiler_data_) | |
119 return; | |
120 | |
121 DCHECK_EQ(tracked_objects::TIME_SOURCE_TYPE_WALL_TIME, | |
122 tracked_objects::GetTimeSourceType()); | |
123 | |
124 DCHECK_EQ(0, uma_proto->profiler_event_size()); | |
125 ProfilerEventProto* profile = uma_proto->add_profiler_event(); | |
126 profile->Swap(&profiler_event_cache_); | |
127 has_profiler_data_ = false; | |
128 } | |
129 | |
130 void ProfilerMetricsProvider::RecordProfilerData( | |
131 const tracked_objects::ProcessDataSnapshot& process_data, | |
132 int process_type) { | |
133 if (tracked_objects::GetTimeSourceType() != | |
134 tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) { | |
135 // We currently only support the default time source, wall clock time. | |
136 return; | |
137 } | |
138 | |
139 has_profiler_data_ = true; | |
140 profiler_event_cache_.set_profile_type(ProfilerEventProto::STARTUP_PROFILE); | |
141 profiler_event_cache_.set_time_source(ProfilerEventProto::WALL_CLOCK_TIME); | |
142 WriteProfilerData(process_data, process_type, &profiler_event_cache_); | |
143 } | |
OLD | NEW |