| 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_system_stats_monitor.h" | 5 #include "base/trace_event/trace_event_system_stats_monitor.h" |
| 6 | 6 |
| 7 #include "base/debug/leak_annotations.h" | 7 #include "base/debug/leak_annotations.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "base/threading/thread_local_storage.h" | 16 #include "base/threading/thread_local_storage.h" |
| 17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 namespace trace_event { | 20 namespace trace_event { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 ///////////////////////////////////////////////////////////////////////////// | 24 ///////////////////////////////////////////////////////////////////////////// |
| 25 // Holds profiled system stats until the tracing system needs to serialize it. | 25 // Holds profiled system stats until the tracing system needs to serialize it. |
| 26 class SystemStatsHolder : public base::trace_event::ConvertableToTraceFormat { | 26 class SystemStatsHolder : public base::trace_event::ConvertableToTraceFormat { |
| 27 public: | 27 public: |
| 28 SystemStatsHolder() { } | 28 SystemStatsHolder() { } |
| 29 ~SystemStatsHolder() override {} |
| 29 | 30 |
| 30 // Fills system_metrics_ with profiled system memory and disk stats. | 31 // Fills system_metrics_ with profiled system memory and disk stats. |
| 31 // Uses the previous stats to compute rates if this is not the first profile. | 32 // Uses the previous stats to compute rates if this is not the first profile. |
| 32 void GetSystemProfilingStats(); | 33 void GetSystemProfilingStats(); |
| 33 | 34 |
| 34 // base::trace_event::ConvertableToTraceFormat overrides: | 35 // base::trace_event::ConvertableToTraceFormat overrides: |
| 35 void AppendAsTraceFormat(std::string* out) const override { | 36 void AppendAsTraceFormat(std::string* out) const override { |
| 36 AppendSystemProfileAsTraceFormat(system_stats_, out); | 37 AppendSystemProfileAsTraceFormat(system_stats_, out); |
| 37 } | 38 } |
| 38 | 39 |
| 39 private: | 40 private: |
| 40 ~SystemStatsHolder() override {} | |
| 41 | |
| 42 SystemMetrics system_stats_; | 41 SystemMetrics system_stats_; |
| 43 | 42 |
| 44 DISALLOW_COPY_AND_ASSIGN(SystemStatsHolder); | 43 DISALLOW_COPY_AND_ASSIGN(SystemStatsHolder); |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 void SystemStatsHolder::GetSystemProfilingStats() { | 46 void SystemStatsHolder::GetSystemProfilingStats() { |
| 48 system_stats_ = SystemMetrics::Sample(); | 47 system_stats_ = SystemMetrics::Sample(); |
| 49 } | 48 } |
| 50 | 49 |
| 51 } // namespace | 50 } // namespace |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 dump_timer_.Start(FROM_HERE, | 97 dump_timer_.Start(FROM_HERE, |
| 99 TimeDelta::FromMilliseconds(TraceEventSystemStatsMonitor:: | 98 TimeDelta::FromMilliseconds(TraceEventSystemStatsMonitor:: |
| 100 kSamplingIntervalMilliseconds), | 99 kSamplingIntervalMilliseconds), |
| 101 base::Bind(&TraceEventSystemStatsMonitor:: | 100 base::Bind(&TraceEventSystemStatsMonitor:: |
| 102 DumpSystemStats, | 101 DumpSystemStats, |
| 103 weak_factory_.GetWeakPtr())); | 102 weak_factory_.GetWeakPtr())); |
| 104 } | 103 } |
| 105 | 104 |
| 106 // If system tracing is enabled, dumps a profile to the tracing system. | 105 // If system tracing is enabled, dumps a profile to the tracing system. |
| 107 void TraceEventSystemStatsMonitor::DumpSystemStats() { | 106 void TraceEventSystemStatsMonitor::DumpSystemStats() { |
| 108 scoped_refptr<SystemStatsHolder> dump_holder = new SystemStatsHolder(); | 107 scoped_ptr<SystemStatsHolder> dump_holder(new SystemStatsHolder()); |
| 109 dump_holder->GetSystemProfilingStats(); | 108 dump_holder->GetSystemProfilingStats(); |
| 110 | 109 |
| 111 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 110 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
| 112 TRACE_DISABLED_BY_DEFAULT("system_stats"), | 111 TRACE_DISABLED_BY_DEFAULT("system_stats"), |
| 113 "base::TraceEventSystemStatsMonitor::SystemStats", | 112 "base::TraceEventSystemStatsMonitor::SystemStats", this, |
| 114 this, | 113 std::move(dump_holder)); |
| 115 scoped_refptr<ConvertableToTraceFormat>(dump_holder)); | |
| 116 } | 114 } |
| 117 | 115 |
| 118 void TraceEventSystemStatsMonitor::StopProfiling() { | 116 void TraceEventSystemStatsMonitor::StopProfiling() { |
| 119 dump_timer_.Stop(); | 117 dump_timer_.Stop(); |
| 120 } | 118 } |
| 121 | 119 |
| 122 bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const { | 120 bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const { |
| 123 return dump_timer_.IsRunning(); | 121 return dump_timer_.IsRunning(); |
| 124 } | 122 } |
| 125 | 123 |
| 126 void AppendSystemProfileAsTraceFormat(const SystemMetrics& system_metrics, | 124 void AppendSystemProfileAsTraceFormat(const SystemMetrics& system_metrics, |
| 127 std::string* output) { | 125 std::string* output) { |
| 128 std::string tmp; | 126 std::string tmp; |
| 129 base::JSONWriter::Write(*system_metrics.ToValue(), &tmp); | 127 base::JSONWriter::Write(*system_metrics.ToValue(), &tmp); |
| 130 *output += tmp; | 128 *output += tmp; |
| 131 } | 129 } |
| 132 | 130 |
| 133 } // namespace trace_event | 131 } // namespace trace_event |
| 134 } // namespace base | 132 } // namespace base |
| OLD | NEW |