diff options
author | primiano <primiano@chromium.org> | 2015-02-20 08:41:14 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-20 16:41:56 +0000 |
commit | c96eca7cc1b3a9c6de89b4928637dfa9412e74b5 (patch) | |
tree | e3515f43f16fe06f552c215bace02f85adf35a01 /base/trace_event | |
parent | fcaf87dba8e3ae20df648d169eb379d38697a552 (diff) | |
download | chromium_src-c96eca7cc1b3a9c6de89b4928637dfa9412e74b5.zip chromium_src-c96eca7cc1b3a9c6de89b4928637dfa9412e74b5.tar.gz chromium_src-c96eca7cc1b3a9c6de89b4928637dfa9412e74b5.tar.bz2 |
[tracing] Add a dump provider to dump process memory totals.
This CL introduces a dump provider which is able to capture
the process Resident Set Size and the corresponding unittest.
BUG=442731,458295
Review URL: https://codereview.chromium.org/934323002
Cr-Commit-Position: refs/heads/master@{#317336}
Diffstat (limited to 'base/trace_event')
-rw-r--r-- | base/trace_event/memory_dump_manager.cc | 10 | ||||
-rw-r--r-- | base/trace_event/process_memory_dump.cc | 20 | ||||
-rw-r--r-- | base/trace_event/process_memory_dump.h | 31 | ||||
-rw-r--r-- | base/trace_event/process_memory_totals.cc | 17 | ||||
-rw-r--r-- | base/trace_event/process_memory_totals.h | 36 | ||||
-rw-r--r-- | base/trace_event/process_memory_totals_dump_provider.cc | 53 | ||||
-rw-r--r-- | base/trace_event/process_memory_totals_dump_provider.h | 44 | ||||
-rw-r--r-- | base/trace_event/process_memory_totals_dump_provider_unittest.cc | 43 |
8 files changed, 227 insertions, 27 deletions
diff --git a/base/trace_event/memory_dump_manager.cc b/base/trace_event/memory_dump_manager.cc index c1a9685..c8be8f8 100644 --- a/base/trace_event/memory_dump_manager.cc +++ b/base/trace_event/memory_dump_manager.cc @@ -9,6 +9,7 @@ #include "base/compiler_specific.h" #include "base/trace_event/memory_dump_provider.h" #include "base/trace_event/process_memory_dump.h" +#include "base/trace_event/trace_event_argument.h" namespace base { namespace trace_event { @@ -91,15 +92,14 @@ void MemoryDumpManager::BroadcastDumpRequest() { // Creates a dump point for the current process and appends it to the trace. void MemoryDumpManager::CreateLocalDumpPoint() { AutoLock lock(lock_); - // TRACE_EVENT_* macros don't induce scoped_refptr type inference, hence we - // need the base ConvertableToTraceFormat and the upcast below. The - // alternative would be unnecessarily expensive (double Acquire/Release). - scoped_refptr<ConvertableToTraceFormat> pmd(new ProcessMemoryDump()); + scoped_ptr<ProcessMemoryDump> pmd(new ProcessMemoryDump()); for (MemoryDumpProvider* dump_provider : dump_providers_enabled_) { - dump_provider->DumpInto(static_cast<ProcessMemoryDump*>(pmd.get())); + dump_provider->DumpInto(pmd.get()); } + scoped_refptr<TracedValue> value(new TracedValue()); + pmd->AsValueInto(value.get()); // TODO(primiano): add the dump point to the trace at this point. } diff --git a/base/trace_event/process_memory_dump.cc b/base/trace_event/process_memory_dump.cc index 0a3e096..6da9132 100644 --- a/base/trace_event/process_memory_dump.cc +++ b/base/trace_event/process_memory_dump.cc @@ -4,25 +4,25 @@ #include "base/trace_event/process_memory_dump.h" -#include "base/json/json_writer.h" -#include "base/values.h" +#include "base/trace_event/process_memory_totals.h" +#include "base/trace_event/trace_event_argument.h" namespace base { namespace trace_event { -ProcessMemoryDump::ProcessMemoryDump() { +ProcessMemoryDump::ProcessMemoryDump() : has_process_totals_(false) { } ProcessMemoryDump::~ProcessMemoryDump() { } -void ProcessMemoryDump::AppendAsTraceFormat(std::string* out) const { - // Build up the [dumper name] -> [serialized snapshot] JSON dictionary. - DictionaryValue dict; - std::string json_dict; - // TODO(primiano): this will append here the actual dumps from the dumpers. - base::JSONWriter::Write(&dict, &json_dict); - *out += json_dict; +void ProcessMemoryDump::AsValueInto(TracedValue* value) const { + // Build up the [dumper name] -> [value] dictionary. + if (has_process_totals_) { + value->BeginDictionary("process_totals"); + process_totals_.AsValueInto(value); + value->EndDictionary(); + } } } // namespace trace_event diff --git a/base/trace_event/process_memory_dump.h b/base/trace_event/process_memory_dump.h index ae42987..f70537b 100644 --- a/base/trace_event/process_memory_dump.h +++ b/base/trace_event/process_memory_dump.h @@ -6,27 +6,34 @@ #define BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ #include "base/base_export.h" -#include "base/basictypes.h" -#include "base/trace_event/trace_event_impl.h" +#include "base/trace_event/process_memory_totals.h" namespace base { namespace trace_event { -// A container which holds the dumps produced by the MemoryDumpProvider(s) -// for a specific process. ProcessMemoryDump is as a strongly typed container -// which enforces the data model for each memory dump point. -// At trace generation time (i.e. when AppendAsTraceFormat is called) the -// ProcessMemoryDump will compose a key-value dictionary of the various dumps -// obtained during at trace dump point time. -class BASE_EXPORT ProcessMemoryDump : public ConvertableToTraceFormat { +class ConvertableToTraceFormat; + +// ProcessMemoryDump is as a strongly typed container which enforces the data +// model for each memory dump point and holds the dumps produced by the +// MemoryDumpProvider(s) for a specific process. +// At trace generation time (i.e. when AsValue() is called), ProcessMemoryDump +// will compose a key-value dictionary of the various dumps obtained at trace +// dump point time. +class BASE_EXPORT ProcessMemoryDump { public: ProcessMemoryDump(); + ~ProcessMemoryDump(); + + // Called at trace generation time to populate the TracedValue. + void AsValueInto(TracedValue* value) const; - // ConvertableToTraceFormat implementation. - void AppendAsTraceFormat(std::string* out) const override; + ProcessMemoryTotals* process_totals() { return &process_totals_; } + bool has_process_totals() const { return has_process_totals_; } + void set_has_process_totals() { has_process_totals_ = true; } private: - ~ProcessMemoryDump() override; + ProcessMemoryTotals process_totals_; + bool has_process_totals_; DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDump); }; diff --git a/base/trace_event/process_memory_totals.cc b/base/trace_event/process_memory_totals.cc new file mode 100644 index 0000000..41ad788 --- /dev/null +++ b/base/trace_event/process_memory_totals.cc @@ -0,0 +1,17 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/trace_event/process_memory_totals.h" + +#include "base/trace_event/trace_event_argument.h" + +namespace base { +namespace trace_event { + +void ProcessMemoryTotals::AsValueInto(TracedValue* value) const { + value->SetDouble("resident_set_bytes", resident_set_bytes_); +} + +} // namespace trace_event +} // namespace base diff --git a/base/trace_event/process_memory_totals.h b/base/trace_event/process_memory_totals.h new file mode 100644 index 0000000..1c99152 --- /dev/null +++ b/base/trace_event/process_memory_totals.h @@ -0,0 +1,36 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_TRACE_EVENT_PROCESS_MEMORY_TOTALS_H_ +#define BASE_TRACE_EVENT_PROCESS_MEMORY_TOTALS_H_ + +#include "base/base_export.h" +#include "base/basictypes.h" + +namespace base { +namespace trace_event { + +class TracedValue; + +// Dump provider which collects process-wide memory stats. +class BASE_EXPORT ProcessMemoryTotals { + public: + ProcessMemoryTotals() {} + + // Called at trace generation time to populate the TracedValue. + void AsValueInto(TracedValue* value) const; + + uint64 resident_set_bytes() const { return resident_set_bytes_; } + void set_resident_set_bytes(uint64 value) { resident_set_bytes_ = value; } + + private: + uint64 resident_set_bytes_; + + DISALLOW_COPY_AND_ASSIGN(ProcessMemoryTotals); +}; + +} // namespace trace_event +} // namespace base + +#endif // BASE_TRACE_EVENT_PROCESS_MEMORY_TOTALS_H_ diff --git a/base/trace_event/process_memory_totals_dump_provider.cc b/base/trace_event/process_memory_totals_dump_provider.cc new file mode 100644 index 0000000..cda0ff1 --- /dev/null +++ b/base/trace_event/process_memory_totals_dump_provider.cc @@ -0,0 +1,53 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/trace_event/process_memory_totals_dump_provider.h" + +#include "base/process/process_metrics.h" +#include "base/trace_event/process_memory_dump.h" +#include "base/trace_event/process_memory_totals.h" + +namespace base { +namespace trace_event { + +// static +uint64 ProcessMemoryTotalsDumpProvider::rss_bytes_for_testing = 0; + +namespace { +ProcessMetrics* CreateProcessMetricsForCurrentProcess() { +#if !defined(OS_MACOSX) || defined(OS_IOS) + return ProcessMetrics::CreateProcessMetrics(GetCurrentProcessHandle()); +#else + return ProcessMetrics::CreateProcessMetrics(GetCurrentProcessHandle(), NULL); +#endif +} +} // namespace + +// static +ProcessMemoryTotalsDumpProvider* +ProcessMemoryTotalsDumpProvider::GetInstance() { + return Singleton< + ProcessMemoryTotalsDumpProvider, + LeakySingletonTraits<ProcessMemoryTotalsDumpProvider>>::get(); +} + +ProcessMemoryTotalsDumpProvider::ProcessMemoryTotalsDumpProvider() + : process_metrics_(CreateProcessMetricsForCurrentProcess()) { +} + +ProcessMemoryTotalsDumpProvider::~ProcessMemoryTotalsDumpProvider() { +} + +// Called at trace dump point time. Creates a snapshot the memory counters for +// the current process. +void ProcessMemoryTotalsDumpProvider::DumpInto(ProcessMemoryDump* pmd) { + const uint64 rss_bytes = rss_bytes_for_testing + ? rss_bytes_for_testing + : process_metrics_->GetWorkingSetSize(); + pmd->process_totals()->set_resident_set_bytes(rss_bytes); + pmd->set_has_process_totals(); +} + +} // namespace trace_event +} // namespace base diff --git a/base/trace_event/process_memory_totals_dump_provider.h b/base/trace_event/process_memory_totals_dump_provider.h new file mode 100644 index 0000000..45917a8 --- /dev/null +++ b/base/trace_event/process_memory_totals_dump_provider.h @@ -0,0 +1,44 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_TRACE_EVENT_PROCESS_MEMORY_TOTALS_DUMP_PROVIDER_H_ +#define BASE_TRACE_EVENT_PROCESS_MEMORY_TOTALS_DUMP_PROVIDER_H_ + +#include "base/gtest_prod_util.h" +#include "base/memory/scoped_ptr.h" +#include "base/memory/singleton.h" +#include "base/trace_event/memory_dump_provider.h" + +namespace base { + +class ProcessMetrics; + +namespace trace_event { + +// Dump provider which collects process-wide memory stats. +class BASE_EXPORT ProcessMemoryTotalsDumpProvider : public MemoryDumpProvider { + public: + static ProcessMemoryTotalsDumpProvider* GetInstance(); + + // MemoryDumpProvider implementation. + void DumpInto(ProcessMemoryDump* pmd) override; + + private: + friend struct DefaultSingletonTraits<ProcessMemoryTotalsDumpProvider>; + FRIEND_TEST_ALL_PREFIXES(ProcessMemoryTotalsDumpProviderTest, DumpRSS); + + static uint64 rss_bytes_for_testing; + + ProcessMemoryTotalsDumpProvider(); + ~ProcessMemoryTotalsDumpProvider() override; + + scoped_ptr<ProcessMetrics> process_metrics_; + + DISALLOW_COPY_AND_ASSIGN(ProcessMemoryTotalsDumpProvider); +}; + +} // namespace trace_event +} // namespace base + +#endif // BASE_TRACE_EVENT_PROCESS_MEMORY_TOTALS_DUMP_PROVIDER_H_ diff --git a/base/trace_event/process_memory_totals_dump_provider_unittest.cc b/base/trace_event/process_memory_totals_dump_provider_unittest.cc new file mode 100644 index 0000000..4a60036 --- /dev/null +++ b/base/trace_event/process_memory_totals_dump_provider_unittest.cc @@ -0,0 +1,43 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/trace_event/process_memory_totals_dump_provider.h" + +#include "base/trace_event/process_memory_dump.h" +#include "base/trace_event/process_memory_totals.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace base { +namespace trace_event { + +TEST(ProcessMemoryTotalsDumpProviderTest, DumpRSS) { + auto mdptp = ProcessMemoryTotalsDumpProvider::GetInstance(); + scoped_ptr<ProcessMemoryDump> pmd_before(new ProcessMemoryDump()); + scoped_ptr<ProcessMemoryDump> pmd_after(new ProcessMemoryDump()); + + ProcessMemoryTotalsDumpProvider::rss_bytes_for_testing = 1024; + mdptp->DumpInto(pmd_before.get()); + + // Pretend that the RSS of the process increased of +1M. + const size_t kAllocSize = 1048576; + ProcessMemoryTotalsDumpProvider::rss_bytes_for_testing += kAllocSize; + + mdptp->DumpInto(pmd_after.get()); + + ProcessMemoryTotalsDumpProvider::rss_bytes_for_testing = 0; + + ASSERT_TRUE(pmd_before->has_process_totals()); + ASSERT_TRUE(pmd_after->has_process_totals()); + + const uint64 rss_before = pmd_before->process_totals()->resident_set_bytes(); + const uint64 rss_after = pmd_after->process_totals()->resident_set_bytes(); + + EXPECT_NE(0U, rss_before); + EXPECT_NE(0U, rss_after); + + EXPECT_EQ(rss_after - rss_before, kAllocSize); +} + +} // namespace trace_Event +} // namespace base |