| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/trace_event/trace_event_memory.h" | 5 #include "base/trace_event/trace_event_memory.h" |
| 6 | 6 |
| 7 #include "base/debug/leak_annotations.h" | 7 #include "base/debug/leak_annotations.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/single_thread_task_runner.h" |
| 12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/threading/thread_local_storage.h" | 15 #include "base/threading/thread_local_storage.h" |
| 15 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 namespace trace_event { | 19 namespace trace_event { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // Each item in the trace event stack contains both name and category so tell | 138 // Each item in the trace event stack contains both name and category so tell |
| 138 // tcmalloc that we have returned |count| * 2 stack frames. | 139 // tcmalloc that we have returned |count| * 2 stack frames. |
| 139 return static_cast<int>(count * 2); | 140 return static_cast<int>(count * 2); |
| 140 } | 141 } |
| 141 | 142 |
| 142 } // namespace | 143 } // namespace |
| 143 | 144 |
| 144 ////////////////////////////////////////////////////////////////////////////// | 145 ////////////////////////////////////////////////////////////////////////////// |
| 145 | 146 |
| 146 TraceMemoryController::TraceMemoryController( | 147 TraceMemoryController::TraceMemoryController( |
| 147 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 148 scoped_refptr<SingleThreadTaskRunner> task_runner, |
| 148 HeapProfilerStartFunction heap_profiler_start_function, | 149 HeapProfilerStartFunction heap_profiler_start_function, |
| 149 HeapProfilerStopFunction heap_profiler_stop_function, | 150 HeapProfilerStopFunction heap_profiler_stop_function, |
| 150 GetHeapProfileFunction get_heap_profile_function) | 151 GetHeapProfileFunction get_heap_profile_function) |
| 151 : message_loop_proxy_(message_loop_proxy), | 152 : task_runner_(task_runner.Pass()), |
| 152 heap_profiler_start_function_(heap_profiler_start_function), | 153 heap_profiler_start_function_(heap_profiler_start_function), |
| 153 heap_profiler_stop_function_(heap_profiler_stop_function), | 154 heap_profiler_stop_function_(heap_profiler_stop_function), |
| 154 get_heap_profile_function_(get_heap_profile_function), | 155 get_heap_profile_function_(get_heap_profile_function), |
| 155 weak_factory_(this) { | 156 weak_factory_(this) { |
| 156 // Force the "memory" category to show up in the trace viewer. | 157 // Force the "memory" category to show up in the trace viewer. |
| 157 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("memory"), "init"); | 158 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("memory"), "init"); |
| 158 // Watch for the tracing system being enabled. | 159 // Watch for the tracing system being enabled. |
| 159 TraceLog::GetInstance()->AddEnabledStateObserver(this); | 160 TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| 160 } | 161 } |
| 161 | 162 |
| 162 TraceMemoryController::~TraceMemoryController() { | 163 TraceMemoryController::~TraceMemoryController() { |
| 163 if (dump_timer_.IsRunning()) | 164 if (dump_timer_.IsRunning()) |
| 164 StopProfiling(); | 165 StopProfiling(); |
| 165 TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | 166 TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| 166 } | 167 } |
| 167 | 168 |
| 168 // base::trace_event::TraceLog::EnabledStateChangedObserver overrides: | 169 // base::trace_event::TraceLog::EnabledStateChangedObserver overrides: |
| 169 void TraceMemoryController::OnTraceLogEnabled() { | 170 void TraceMemoryController::OnTraceLogEnabled() { |
| 170 // Check to see if tracing is enabled for the memory category. | 171 // Check to see if tracing is enabled for the memory category. |
| 171 bool enabled; | 172 bool enabled; |
| 172 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("memory"), | 173 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("memory"), |
| 173 &enabled); | 174 &enabled); |
| 174 if (!enabled) | 175 if (!enabled) |
| 175 return; | 176 return; |
| 176 DVLOG(1) << "OnTraceLogEnabled"; | 177 DVLOG(1) << "OnTraceLogEnabled"; |
| 177 message_loop_proxy_->PostTask( | 178 task_runner_->PostTask(FROM_HERE, |
| 178 FROM_HERE, | 179 base::Bind(&TraceMemoryController::StartProfiling, |
| 179 base::Bind(&TraceMemoryController::StartProfiling, | 180 weak_factory_.GetWeakPtr())); |
| 180 weak_factory_.GetWeakPtr())); | |
| 181 } | 181 } |
| 182 | 182 |
| 183 void TraceMemoryController::OnTraceLogDisabled() { | 183 void TraceMemoryController::OnTraceLogDisabled() { |
| 184 // The memory category is always disabled before OnTraceLogDisabled() is | 184 // The memory category is always disabled before OnTraceLogDisabled() is |
| 185 // called, so we cannot tell if it was enabled before. Always try to turn | 185 // called, so we cannot tell if it was enabled before. Always try to turn |
| 186 // off profiling. | 186 // off profiling. |
| 187 DVLOG(1) << "OnTraceLogDisabled"; | 187 DVLOG(1) << "OnTraceLogDisabled"; |
| 188 message_loop_proxy_->PostTask( | 188 task_runner_->PostTask(FROM_HERE, |
| 189 FROM_HERE, | 189 base::Bind(&TraceMemoryController::StopProfiling, |
| 190 base::Bind(&TraceMemoryController::StopProfiling, | 190 weak_factory_.GetWeakPtr())); |
| 191 weak_factory_.GetWeakPtr())); | |
| 192 } | 191 } |
| 193 | 192 |
| 194 void TraceMemoryController::StartProfiling() { | 193 void TraceMemoryController::StartProfiling() { |
| 195 // Watch for the tracing framework sending enabling more than once. | 194 // Watch for the tracing framework sending enabling more than once. |
| 196 if (dump_timer_.IsRunning()) | 195 if (dump_timer_.IsRunning()) |
| 197 return; | 196 return; |
| 198 DVLOG(1) << "Starting trace memory"; | 197 DVLOG(1) << "Starting trace memory"; |
| 199 if (!InitThreadLocalStorage()) | 198 if (!InitThreadLocalStorage()) |
| 200 return; | 199 return; |
| 201 ScopedTraceMemory::set_enabled(true); | 200 ScopedTraceMemory::set_enabled(true); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 if (!base::HexStringToUInt64(hex_address, &address)) | 430 if (!base::HexStringToUInt64(hex_address, &address)) |
| 432 return "error"; | 431 return "error"; |
| 433 if (!address) | 432 if (!address) |
| 434 return "null"; | 433 return "null"; |
| 435 // Note that this cast handles 64-bit to 32-bit conversion if necessary. | 434 // Note that this cast handles 64-bit to 32-bit conversion if necessary. |
| 436 return reinterpret_cast<const char*>(address); | 435 return reinterpret_cast<const char*>(address); |
| 437 } | 436 } |
| 438 | 437 |
| 439 } // namespace trace_event | 438 } // namespace trace_event |
| 440 } // namespace base | 439 } // namespace base |
| OLD | NEW |