Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/tracing/child/child_trace_message_filter.h" | 5 #include "components/tracing/child/child_trace_message_filter.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| 11 #include "base/trace_event/memory_dump_manager.h" | |
| 11 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
| 12 #include "components/tracing/child/child_memory_dump_manager_delegate_impl.h" | 13 #include "components/tracing/common/process_metrics_memory_dump_provider.h" |
| 13 #include "components/tracing/common/tracing_messages.h" | 14 #include "components/tracing/common/tracing_messages.h" |
| 14 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
| 15 | 16 |
| 17 using base::trace_event::MemoryDumpManager; | |
| 16 using base::trace_event::TraceLog; | 18 using base::trace_event::TraceLog; |
| 17 | 19 |
| 18 namespace tracing { | 20 namespace tracing { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const int kMinTimeBetweenHistogramChangesInSeconds = 10; | 24 const int kMinTimeBetweenHistogramChangesInSeconds = 10; |
| 23 | 25 |
| 24 } // namespace | 26 } // namespace |
| 25 | 27 |
| 26 ChildTraceMessageFilter::ChildTraceMessageFilter( | 28 ChildTraceMessageFilter::ChildTraceMessageFilter( |
| 27 base::SingleThreadTaskRunner* ipc_task_runner) | 29 base::SingleThreadTaskRunner* ipc_task_runner) |
| 28 : sender_(NULL), | 30 : sender_(NULL), ipc_task_runner_(ipc_task_runner) {} |
| 29 ipc_task_runner_(ipc_task_runner), | |
| 30 pending_memory_dump_guid_(0) { | |
| 31 } | |
| 32 | 31 |
| 33 void ChildTraceMessageFilter::OnFilterAdded(IPC::Channel* channel) { | 32 void ChildTraceMessageFilter::OnFilterAdded(IPC::Channel* channel) { |
| 34 sender_ = channel; | 33 sender_ = channel; |
| 35 sender_->Send(new TracingHostMsg_ChildSupportsTracing()); | 34 sender_->Send(new TracingHostMsg_ChildSupportsTracing()); |
| 36 ChildMemoryDumpManagerDelegateImpl::GetInstance()->SetChildTraceMessageFilter( | 35 |
| 37 this); | 36 #if !defined(OS_LINUX) && !defined(OS_NACL) |
|
Primiano Tucci (use gerrit)
2017/02/17 19:08:44
don't we have an equivalent notification in the ne
chiniforooshan
2017/02/22 05:16:34
I was thinking it might be better to not leak conc
| |
| 37 // On linux the browser process takes care of dumping process metrics. | |
| 38 // The child process is not allowed to do so due to BPF sandbox. | |
| 39 tracing::ProcessMetricsMemoryDumpProvider::RegisterForProcess( | |
| 40 base::kNullProcessId); | |
| 41 #endif | |
| 38 } | 42 } |
| 39 | 43 |
| 40 void ChildTraceMessageFilter::SetSenderForTesting(IPC::Sender* sender) { | 44 void ChildTraceMessageFilter::SetSenderForTesting(IPC::Sender* sender) { |
| 41 sender_ = sender; | 45 sender_ = sender; |
| 42 } | 46 } |
| 43 | 47 |
| 44 void ChildTraceMessageFilter::OnFilterRemoved() { | 48 void ChildTraceMessageFilter::OnFilterRemoved() { |
| 45 ChildMemoryDumpManagerDelegateImpl::GetInstance()->SetChildTraceMessageFilter( | |
| 46 nullptr); | |
| 47 sender_ = NULL; | 49 sender_ = NULL; |
| 48 } | 50 } |
| 49 | 51 |
| 50 bool ChildTraceMessageFilter::OnMessageReceived(const IPC::Message& message) { | 52 bool ChildTraceMessageFilter::OnMessageReceived(const IPC::Message& message) { |
| 51 bool handled = true; | 53 bool handled = true; |
| 52 IPC_BEGIN_MESSAGE_MAP(ChildTraceMessageFilter, message) | 54 IPC_BEGIN_MESSAGE_MAP(ChildTraceMessageFilter, message) |
| 53 IPC_MESSAGE_HANDLER(TracingMsg_BeginTracing, OnBeginTracing) | 55 IPC_MESSAGE_HANDLER(TracingMsg_BeginTracing, OnBeginTracing) |
| 54 IPC_MESSAGE_HANDLER(TracingMsg_EndTracing, OnEndTracing) | 56 IPC_MESSAGE_HANDLER(TracingMsg_EndTracing, OnEndTracing) |
| 55 IPC_MESSAGE_HANDLER(TracingMsg_CancelTracing, OnCancelTracing) | 57 IPC_MESSAGE_HANDLER(TracingMsg_CancelTracing, OnCancelTracing) |
| 56 IPC_MESSAGE_HANDLER(TracingMsg_GetTraceLogStatus, OnGetTraceLogStatus) | 58 IPC_MESSAGE_HANDLER(TracingMsg_GetTraceLogStatus, OnGetTraceLogStatus) |
| 57 IPC_MESSAGE_HANDLER(TracingMsg_ProcessMemoryDumpRequest, | |
| 58 OnProcessMemoryDumpRequest) | |
| 59 IPC_MESSAGE_HANDLER(TracingMsg_GlobalMemoryDumpResponse, | |
| 60 OnGlobalMemoryDumpResponse) | |
| 61 IPC_MESSAGE_HANDLER(TracingMsg_SetUMACallback, OnSetUMACallback) | 59 IPC_MESSAGE_HANDLER(TracingMsg_SetUMACallback, OnSetUMACallback) |
| 62 IPC_MESSAGE_HANDLER(TracingMsg_ClearUMACallback, OnClearUMACallback) | 60 IPC_MESSAGE_HANDLER(TracingMsg_ClearUMACallback, OnClearUMACallback) |
| 63 IPC_MESSAGE_UNHANDLED(handled = false) | 61 IPC_MESSAGE_UNHANDLED(handled = false) |
| 64 IPC_END_MESSAGE_MAP() | 62 IPC_END_MESSAGE_MAP() |
| 65 return handled; | 63 return handled; |
| 66 } | 64 } |
| 67 | 65 |
| 68 ChildTraceMessageFilter::~ChildTraceMessageFilter() {} | 66 ChildTraceMessageFilter::~ChildTraceMessageFilter() {} |
| 69 | 67 |
| 70 void ChildTraceMessageFilter::OnBeginTracing( | 68 void ChildTraceMessageFilter::OnBeginTracing( |
| 71 const std::string& trace_config_str, | 69 const std::string& trace_config_str, |
| 72 base::TimeTicks browser_time, | 70 base::TimeTicks browser_time, |
| 73 uint64_t tracing_process_id) { | 71 uint64_t tracing_process_id) { |
| 74 #if defined(__native_client__) | 72 #if defined(__native_client__) |
| 75 // NaCl and system times are offset by a bit, so subtract some time from | 73 // NaCl and system times are offset by a bit, so subtract some time from |
| 76 // the captured timestamps. The value might be off by a bit due to messaging | 74 // the captured timestamps. The value might be off by a bit due to messaging |
| 77 // latency. | 75 // latency. |
| 78 base::TimeDelta time_offset = base::TimeTicks::Now() - browser_time; | 76 base::TimeDelta time_offset = base::TimeTicks::Now() - browser_time; |
| 79 TraceLog::GetInstance()->SetTimeOffset(time_offset); | 77 TraceLog::GetInstance()->SetTimeOffset(time_offset); |
| 80 #endif | 78 #endif |
| 81 ChildMemoryDumpManagerDelegateImpl::GetInstance()->set_tracing_process_id( | 79 MemoryDumpManager::GetInstance()->set_tracing_process_id(tracing_process_id); |
| 82 tracing_process_id); | |
| 83 TraceLog::GetInstance()->SetEnabled( | 80 TraceLog::GetInstance()->SetEnabled( |
| 84 base::trace_event::TraceConfig(trace_config_str), | 81 base::trace_event::TraceConfig(trace_config_str), |
| 85 base::trace_event::TraceLog::RECORDING_MODE); | 82 base::trace_event::TraceLog::RECORDING_MODE); |
| 86 } | 83 } |
| 87 | 84 |
| 88 void ChildTraceMessageFilter::OnEndTracing() { | 85 void ChildTraceMessageFilter::OnEndTracing() { |
| 89 TraceLog::GetInstance()->SetDisabled(); | 86 TraceLog::GetInstance()->SetDisabled(); |
| 90 | 87 |
| 91 // Flush will generate one or more callbacks to OnTraceDataCollected | 88 // Flush will generate one or more callbacks to OnTraceDataCollected |
| 92 // synchronously or asynchronously. EndTracingAck will be sent in the last | 89 // synchronously or asynchronously. EndTracingAck will be sent in the last |
| 93 // OnTraceDataCollected. We are already on the IO thread, so the | 90 // OnTraceDataCollected. We are already on the IO thread, so the |
| 94 // OnTraceDataCollected calls will not be deferred. | 91 // OnTraceDataCollected calls will not be deferred. |
| 95 TraceLog::GetInstance()->Flush( | 92 TraceLog::GetInstance()->Flush( |
| 96 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this)); | 93 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this)); |
| 97 | 94 |
| 98 ChildMemoryDumpManagerDelegateImpl::GetInstance()->set_tracing_process_id( | 95 MemoryDumpManager::GetInstance()->set_tracing_process_id( |
| 99 base::trace_event::MemoryDumpManager::kInvalidTracingProcessId); | 96 MemoryDumpManager::kInvalidTracingProcessId); |
| 100 } | 97 } |
| 101 | 98 |
| 102 void ChildTraceMessageFilter::OnCancelTracing() { | 99 void ChildTraceMessageFilter::OnCancelTracing() { |
| 103 TraceLog::GetInstance()->CancelTracing( | 100 TraceLog::GetInstance()->CancelTracing( |
| 104 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this)); | 101 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this)); |
| 105 } | 102 } |
| 106 | 103 |
| 107 void ChildTraceMessageFilter::OnGetTraceLogStatus() { | 104 void ChildTraceMessageFilter::OnGetTraceLogStatus() { |
| 108 sender_->Send(new TracingHostMsg_TraceLogStatusReply( | 105 sender_->Send(new TracingHostMsg_TraceLogStatusReply( |
| 109 TraceLog::GetInstance()->GetStatus())); | 106 TraceLog::GetInstance()->GetStatus())); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 122 sender_->Send(new TracingHostMsg_TraceDataCollected( | 119 sender_->Send(new TracingHostMsg_TraceDataCollected( |
| 123 events_str_ptr->data())); | 120 events_str_ptr->data())); |
| 124 } | 121 } |
| 125 if (!has_more_events) { | 122 if (!has_more_events) { |
| 126 std::vector<std::string> category_groups; | 123 std::vector<std::string> category_groups; |
| 127 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); | 124 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); |
| 128 sender_->Send(new TracingHostMsg_EndTracingAck(category_groups)); | 125 sender_->Send(new TracingHostMsg_EndTracingAck(category_groups)); |
| 129 } | 126 } |
| 130 } | 127 } |
| 131 | 128 |
| 132 // Sent by the Browser's MemoryDumpManager when coordinating a global dump. | |
| 133 void ChildTraceMessageFilter::OnProcessMemoryDumpRequest( | |
| 134 const base::trace_event::MemoryDumpRequestArgs& args) { | |
| 135 ChildMemoryDumpManagerDelegateImpl::GetInstance()->CreateProcessDump( | |
| 136 args, | |
| 137 base::Bind(&ChildTraceMessageFilter::OnProcessMemoryDumpDone, this)); | |
| 138 } | |
| 139 | |
| 140 void ChildTraceMessageFilter::OnProcessMemoryDumpDone(uint64_t dump_guid, | |
| 141 bool success) { | |
| 142 sender_->Send( | |
| 143 new TracingHostMsg_ProcessMemoryDumpResponse(dump_guid, success)); | |
| 144 } | |
| 145 | |
| 146 // Initiates a dump request, asking the Browser's MemoryDumpManager to | |
| 147 // coordinate a global memory dump. The Browser's MDM will answer back with a | |
| 148 // MemoryDumpResponse when all the child processes (including this one) have | |
| 149 // dumped, or with a NACK (|success| == false) if the dump failed (e.g., due to | |
| 150 // a collision with a concurrent request from another child process). | |
| 151 void ChildTraceMessageFilter::SendGlobalMemoryDumpRequest( | |
| 152 const base::trace_event::MemoryDumpRequestArgs& args, | |
| 153 const base::trace_event::MemoryDumpCallback& callback) { | |
| 154 // If there is already another dump request pending from this child process, | |
| 155 // there is no point bothering the Browser's MemoryDumpManager. | |
| 156 if (pending_memory_dump_guid_) { | |
| 157 if (!callback.is_null()) | |
| 158 callback.Run(args.dump_guid, false); | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 pending_memory_dump_guid_ = args.dump_guid; | |
| 163 pending_memory_dump_callback_ = callback; | |
| 164 sender_->Send(new TracingHostMsg_GlobalMemoryDumpRequest(args)); | |
| 165 } | |
| 166 | |
| 167 // Sent by the Browser's MemoryDumpManager in response of a dump request | |
| 168 // initiated by this child process. | |
| 169 void ChildTraceMessageFilter::OnGlobalMemoryDumpResponse(uint64_t dump_guid, | |
| 170 bool success) { | |
| 171 DCHECK_NE(0U, pending_memory_dump_guid_); | |
| 172 pending_memory_dump_guid_ = 0; | |
| 173 if (pending_memory_dump_callback_.is_null()) | |
| 174 return; | |
| 175 pending_memory_dump_callback_.Run(dump_guid, success); | |
| 176 } | |
| 177 | |
| 178 void ChildTraceMessageFilter::OnHistogramChanged( | 129 void ChildTraceMessageFilter::OnHistogramChanged( |
| 179 const std::string& histogram_name, | 130 const std::string& histogram_name, |
| 180 base::Histogram::Sample reference_lower_value, | 131 base::Histogram::Sample reference_lower_value, |
| 181 base::Histogram::Sample reference_upper_value, | 132 base::Histogram::Sample reference_upper_value, |
| 182 bool repeat, | 133 bool repeat, |
| 183 base::Histogram::Sample actual_value) { | 134 base::Histogram::Sample actual_value) { |
| 184 if (actual_value < reference_lower_value || | 135 if (actual_value < reference_lower_value || |
| 185 actual_value > reference_upper_value) { | 136 actual_value > reference_upper_value) { |
| 186 if (!repeat) { | 137 if (!repeat) { |
| 187 ipc_task_runner_->PostTask( | 138 ipc_task_runner_->PostTask( |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 } | 218 } |
| 268 } | 219 } |
| 269 | 220 |
| 270 void ChildTraceMessageFilter::OnClearUMACallback( | 221 void ChildTraceMessageFilter::OnClearUMACallback( |
| 271 const std::string& histogram_name) { | 222 const std::string& histogram_name) { |
| 272 histogram_last_changed_ = base::Time(); | 223 histogram_last_changed_ = base::Time(); |
| 273 base::StatisticsRecorder::ClearCallback(histogram_name); | 224 base::StatisticsRecorder::ClearCallback(histogram_name); |
| 274 } | 225 } |
| 275 | 226 |
| 276 } // namespace tracing | 227 } // namespace tracing |
| OLD | NEW |