This source file includes following definitions.
- AppendAsTraceFormat
- GetSystemProfilingStats
- weak_factory_
- OnTraceLogEnabled
- OnTraceLogDisabled
- StartProfiling
- DumpSystemStats
- StopProfiling
- IsTimerRunningForTest
- AppendSystemProfileAsTraceFormat
#include "base/debug/trace_event_system_stats_monitor.h"
#include "base/debug/leak_annotations.h"
#include "base/debug/trace_event.h"
#include "base/json/json_writer.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread_local_storage.h"
namespace base {
namespace debug {
namespace {
class SystemStatsHolder : public base::debug::ConvertableToTraceFormat {
public:
SystemStatsHolder() { }
void GetSystemProfilingStats();
virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
AppendSystemProfileAsTraceFormat(system_stats_, out);
}
private:
virtual ~SystemStatsHolder() { }
SystemMetrics system_stats_;
DISALLOW_COPY_AND_ASSIGN(SystemStatsHolder);
};
void SystemStatsHolder::GetSystemProfilingStats() {
system_stats_ = SystemMetrics::Sample();
}
}
TraceEventSystemStatsMonitor::TraceEventSystemStatsMonitor(
scoped_refptr<SingleThreadTaskRunner> task_runner)
: task_runner_(task_runner),
weak_factory_(this) {
TraceLog::GetCategoryGroupEnabled(TRACE_DISABLED_BY_DEFAULT("system_stats"));
TraceLog::GetInstance()->AddEnabledStateObserver(this);
}
TraceEventSystemStatsMonitor::~TraceEventSystemStatsMonitor() {
if (dump_timer_.IsRunning())
StopProfiling();
TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
}
void TraceEventSystemStatsMonitor::OnTraceLogEnabled() {
bool enabled;
TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT(
"system_stats"), &enabled);
if (!enabled)
return;
task_runner_->PostTask(
FROM_HERE,
base::Bind(&TraceEventSystemStatsMonitor::StartProfiling,
weak_factory_.GetWeakPtr()));
}
void TraceEventSystemStatsMonitor::OnTraceLogDisabled() {
task_runner_->PostTask(
FROM_HERE,
base::Bind(&TraceEventSystemStatsMonitor::StopProfiling,
weak_factory_.GetWeakPtr()));
}
void TraceEventSystemStatsMonitor::StartProfiling() {
if (dump_timer_.IsRunning())
return;
dump_timer_.Start(FROM_HERE,
TimeDelta::FromMilliseconds(TraceEventSystemStatsMonitor::
kSamplingIntervalMilliseconds),
base::Bind(&TraceEventSystemStatsMonitor::
DumpSystemStats,
weak_factory_.GetWeakPtr()));
}
void TraceEventSystemStatsMonitor::DumpSystemStats() {
scoped_refptr<SystemStatsHolder> dump_holder = new SystemStatsHolder();
dump_holder->GetSystemProfilingStats();
TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
TRACE_DISABLED_BY_DEFAULT("system_stats"),
"base::TraceEventSystemStatsMonitor::SystemStats",
this,
scoped_refptr<ConvertableToTraceFormat>(dump_holder));
}
void TraceEventSystemStatsMonitor::StopProfiling() {
dump_timer_.Stop();
}
bool TraceEventSystemStatsMonitor::IsTimerRunningForTest() const {
return dump_timer_.IsRunning();
}
void AppendSystemProfileAsTraceFormat(const SystemMetrics& system_metrics,
std::string* output) {
std::string tmp;
base::JSONWriter::Write(system_metrics.ToValue().get(), &tmp);
*output += tmp;
}
}
}