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 "components/metrics/profiler/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 tracked_objects::ProcessDataSnapshot;
17
18 namespace metrics {
19
20 namespace {
21
AsProtobufProcessType(int process_type)22 ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType(
23 int process_type) {
24 switch (process_type) {
25 case content::PROCESS_TYPE_BROWSER:
26 return ProfilerEventProto::TrackedObject::BROWSER;
27 case content::PROCESS_TYPE_RENDERER:
28 return ProfilerEventProto::TrackedObject::RENDERER;
29 case content::PROCESS_TYPE_PLUGIN:
30 return ProfilerEventProto::TrackedObject::PLUGIN;
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"
MapThreadName(const std::string & thread_name)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".
NormalizeFileName(const std::string & file_name)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
WriteProfilerData(const ProcessDataSnapshot & profiler_data,int process_type,ProfilerEventProto * performance_profile)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 MetricsLog::Hash(MapThreadName(it->birth.thread_name)));
91 tracked_object->set_exec_thread_name_hash(
92 MetricsLog::Hash(MapThreadName(it->death_thread_name)));
93 tracked_object->set_source_file_name_hash(
94 MetricsLog::Hash(NormalizeFileName(
95 it->birth.location.file_name)));
96 tracked_object->set_source_function_name_hash(
97 MetricsLog::Hash(it->birth.location.function_name));
98 tracked_object->set_source_line_number(it->birth.location.line_number);
99 tracked_object->set_exec_count(death_data.count);
100 tracked_object->set_exec_time_total(death_data.run_duration_sum);
101 tracked_object->set_exec_time_sampled(death_data.run_duration_sample);
102 tracked_object->set_queue_time_total(death_data.queue_duration_sum);
103 tracked_object->set_queue_time_sampled(death_data.queue_duration_sample);
104 tracked_object->set_process_type(AsProtobufProcessType(process_type));
105 tracked_object->set_process_id(profiler_data.process_id);
106 }
107 }
108
109 } // namespace
110
ProfilerMetricsProvider()111 ProfilerMetricsProvider::ProfilerMetricsProvider() : has_profiler_data_(false) {
112 }
113
~ProfilerMetricsProvider()114 ProfilerMetricsProvider::~ProfilerMetricsProvider() {
115 }
116
ProvideGeneralMetrics(ChromeUserMetricsExtension * uma_proto)117 void ProfilerMetricsProvider::ProvideGeneralMetrics(
118 ChromeUserMetricsExtension* uma_proto) {
119 if (!has_profiler_data_)
120 return;
121
122 DCHECK_EQ(tracked_objects::TIME_SOURCE_TYPE_WALL_TIME,
123 tracked_objects::GetTimeSourceType());
124
125 DCHECK_EQ(0, uma_proto->profiler_event_size());
126 ProfilerEventProto* profile = uma_proto->add_profiler_event();
127 profile->Swap(&profiler_event_cache_);
128 has_profiler_data_ = false;
129 }
130
RecordProfilerData(const tracked_objects::ProcessDataSnapshot & process_data,int process_type)131 void ProfilerMetricsProvider::RecordProfilerData(
132 const tracked_objects::ProcessDataSnapshot& process_data,
133 int process_type) {
134 if (tracked_objects::GetTimeSourceType() !=
135 tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) {
136 // We currently only support the default time source, wall clock time.
137 return;
138 }
139
140 has_profiler_data_ = true;
141 profiler_event_cache_.set_profile_type(ProfilerEventProto::STARTUP_PROFILE);
142 profiler_event_cache_.set_time_source(ProfilerEventProto::WALL_CLOCK_TIME);
143 WriteProfilerData(process_data, process_type, &profiler_event_cache_);
144 }
145
146 } // namespace metrics
147