Skip to content

Commit

Permalink
[tracing] Add SkResourceCache statistics to chrome://tracing (chrome …
Browse files Browse the repository at this point in the history
…side)

This CL adds the statistics of SkResourceCache to tracing.
See crrev.com/1313793004 for the skia side changes.

BUG=503168

Review URL: https://codereview.chromium.org/1319473003

Cr-Commit-Position: refs/heads/master@{#347207}
  • Loading branch information
ssiddhartha authored and Commit bot committed Sep 3, 2015
1 parent cbf0dc0 commit 7040503
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 13 deletions.
1 change: 1 addition & 0 deletions skia/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ component("skia") {
# Chrome sources.
"ext/SkDiscardableMemory_chrome.cc",
"ext/SkMemory_new_handler.cpp",
"ext/SkTraceMemoryDump_chrome.cc",
"ext/analysis_canvas.cc",
"ext/benchmarking_canvas.cc",
"ext/convolver.cc",
Expand Down
7 changes: 7 additions & 0 deletions skia/ext/SkDiscardableMemory_chrome.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ SkDiscardableMemoryChrome::SkDiscardableMemoryChrome(
: discardable_(memory.Pass()) {
}

base::trace_event::MemoryAllocatorDump*
SkDiscardableMemoryChrome::CreateMemoryAllocatorDump(
const char* name,
base::trace_event::ProcessMemoryDump* pmd) const {
return discardable_->CreateMemoryAllocatorDump(name, pmd);
}

SkDiscardableMemory* SkDiscardableMemory::Create(size_t bytes) {
return new SkDiscardableMemoryChrome(
base::DiscardableMemoryAllocator::GetInstance()
Expand Down
10 changes: 10 additions & 0 deletions skia/ext/SkDiscardableMemory_chrome.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@

namespace base {
class DiscardableMemory;

namespace trace_event {
class MemoryAllocatorDump;
class ProcessMemoryDump;
}

} // namespace base

// This class implements the SkDiscardableMemory interface using
// base::DiscardableMemory.
class SK_API SkDiscardableMemoryChrome : public SkDiscardableMemory {
Expand All @@ -23,6 +29,10 @@ class SK_API SkDiscardableMemoryChrome : public SkDiscardableMemory {
void* data() override;
void unlock() override;

base::trace_event::MemoryAllocatorDump* CreateMemoryAllocatorDump(
const char* name,
base::trace_event::ProcessMemoryDump* pmd) const;

private:
friend class SkDiscardableMemory;

Expand Down
76 changes: 76 additions & 0 deletions skia/ext/SkTraceMemoryDump_chrome.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 "skia/ext/SkTraceMemoryDump_chrome.h"

#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/process_memory_dump.h"
#include "skia/ext/SkDiscardableMemory_chrome.h"

namespace skia {

namespace {
const char kMallocBackingType[] = "malloc";
}

SkTraceMemoryDump_Chrome::SkTraceMemoryDump_Chrome(
base::trace_event::ProcessMemoryDump* process_memory_dump)
: SkTraceMemoryDump_Chrome("", process_memory_dump) {}

SkTraceMemoryDump_Chrome::SkTraceMemoryDump_Chrome(
const char* dump_name_prefix,
base::trace_event::ProcessMemoryDump* process_memory_dump)
: dump_name_prefix_(dump_name_prefix),
process_memory_dump_(process_memory_dump) {}

SkTraceMemoryDump_Chrome::~SkTraceMemoryDump_Chrome() {}

void SkTraceMemoryDump_Chrome::dumpNumericValue(const char* dumpName,
const char* valueName,
const char* units,
uint64_t value) {
auto dump = GetOrCreateAllocatorDump(dumpName);
dump->AddScalar(valueName, units, value);
}

void SkTraceMemoryDump_Chrome::setMemoryBacking(const char* dumpName,
const char* backingType,
const char* backingObjectId) {
if (strcmp(backingType, kMallocBackingType) == 0) {
auto dump = GetOrCreateAllocatorDump(dumpName);
const char* system_allocator_name =
base::trace_event::MemoryDumpManager::GetInstance()
->system_allocator_pool_name();
if (system_allocator_name) {
process_memory_dump_->AddSuballocation(dump->guid(),
system_allocator_name);
}
} else {
NOTREACHED();
}
}

void SkTraceMemoryDump_Chrome::setDiscardableMemoryBacking(
const char* dumpName,
const SkDiscardableMemory& discardableMemoryObject) {
std::string name = dump_name_prefix_ + dumpName;
DCHECK(!process_memory_dump_->GetAllocatorDump(name));
const SkDiscardableMemoryChrome& discardable_memory_obj =
static_cast<const SkDiscardableMemoryChrome&>(discardableMemoryObject);
auto dump = discardable_memory_obj.CreateMemoryAllocatorDump(
name.c_str(), process_memory_dump_);
DCHECK(dump);
}

base::trace_event::MemoryAllocatorDump*
SkTraceMemoryDump_Chrome::GetOrCreateAllocatorDump(const char* dumpName) {
std::string name = dump_name_prefix_ + dumpName;
auto dump = process_memory_dump_->GetAllocatorDump(name);
if (!dump)
dump = process_memory_dump_->CreateAllocatorDump(name);
return dump;
}

} // namespace skia
68 changes: 68 additions & 0 deletions skia/ext/SkTraceMemoryDump_chrome.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 SKIA_EXT_SK_TRACE_MEMORY_DUMP_CHROME_H_
#define SKIA_EXT_SK_TRACE_MEMORY_DUMP_CHROME_H_

#include <string>

#include "base/macros.h"
#include "third_party/skia/include/core/SkTraceMemoryDump.h"

namespace base {
namespace trace_event {
class MemoryAllocatorDump;
class ProcessMemoryDump;
}
}

namespace skia {

class SkTraceMemoryDump_Chrome : public SkTraceMemoryDump {
public:
// This should never outlive the OnMemoryDump call since the
// ProcessMemoryDump is valid only in that timeframe. Optional
// |dump_name_prefix| argument specifies the prefix appended to the dump
// name skia provides. By defualt it is taken as empty string.
SkTraceMemoryDump_Chrome(
base::trace_event::ProcessMemoryDump* process_memory_dump);

SkTraceMemoryDump_Chrome(
const char* dump_name_prefix,
base::trace_event::ProcessMemoryDump* process_memory_dump);

~SkTraceMemoryDump_Chrome() override;

// SkTraceMemoryDump implementation:
void dumpNumericValue(const char* dumpName,
const char* valueName,
const char* units,
uint64_t value) override;
void setMemoryBacking(const char* dumpName,
const char* backingType,
const char* backingObjectId) override;
void setDiscardableMemoryBacking(
const char* dumpName,
const SkDiscardableMemory& discardableMemoryObject) override;

protected:
base::trace_event::ProcessMemoryDump* process_memory_dump() {
return process_memory_dump_;
}

private:
base::trace_event::ProcessMemoryDump* process_memory_dump_;

std::string dump_name_prefix_;

// Helper to create allocator dumps.
base::trace_event::MemoryAllocatorDump* GetOrCreateAllocatorDump(
const char* dumpName);

DISALLOW_COPY_AND_ASSIGN(SkTraceMemoryDump_Chrome);
};

} // namespace skia

#endif // SKIA_EXT_SK_TRACE_MEMORY_DUMP_CHROME_H_
27 changes: 16 additions & 11 deletions skia/ext/skia_memory_dump_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/process_memory_dump.h"
#include "skia/ext/SkTraceMemoryDump_chrome.h"
#include "third_party/skia/include/core/SkGraphics.h"
#include "third_party/skia/src/core/SkResourceCache.h"

Expand All @@ -25,17 +26,21 @@ SkiaMemoryDumpProvider::~SkiaMemoryDumpProvider() {}
bool SkiaMemoryDumpProvider::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* process_memory_dump) {
auto font_mad =
process_memory_dump->CreateAllocatorDump("skia/sk_font_cache");
font_mad->AddScalar("size", "bytes", SkGraphics::GetFontCacheUsed());
font_mad->AddScalar("count", "objects", SkGraphics::GetFontCacheCountUsed());

auto resource_mad =
process_memory_dump->CreateAllocatorDump("skia/sk_resource_cache");
resource_mad->AddScalar("size", "bytes",
SkResourceCache::GetTotalBytesUsed());
// TODO(ssid): crbug.com/503168. Add sub-allocation edges from discardable or
// malloc memory dumps to avoid double counting.
base::trace_event::MemoryAllocatorDump* font_mad =
process_memory_dump->CreateAllocatorDump("skia/sk_glyph_cache");
font_mad->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
base::trace_event::MemoryAllocatorDump::kUnitsBytes,
SkGraphics::GetFontCacheUsed());
font_mad->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectsCount,
base::trace_event::MemoryAllocatorDump::kUnitsObjects,
SkGraphics::GetFontCacheCountUsed());

if (args.level_of_detail ==
base::trace_event::MemoryDumpArgs::LevelOfDetail::LOW)
return true;

SkTraceMemoryDump_Chrome skia_dumper(process_memory_dump);
SkGraphics::DumpMemoryStatistics(&skia_dumper);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions skia/ext/skia_memory_dump_provider_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace skia {

// Tests if the skia dump provider dumps without crashing.
TEST(SkiaMemoryDumpProviderTest, OnMemoryDump) {
scoped_ptr<base::trace_event::ProcessMemoryDump> process_memory_dump(
new base::trace_event::ProcessMemoryDump(nullptr));
Expand All @@ -16,8 +17,7 @@ TEST(SkiaMemoryDumpProviderTest, OnMemoryDump) {
SkiaMemoryDumpProvider::GetInstance()->OnMemoryDump(
dump_args, process_memory_dump.get());

ASSERT_TRUE(process_memory_dump->GetAllocatorDump("skia/sk_font_cache"));
ASSERT_TRUE(process_memory_dump->GetAllocatorDump("skia/sk_resource_cache"));
ASSERT_TRUE(process_memory_dump->GetAllocatorDump("skia/sk_glyph_cache"));
}

} // namespace skia
1 change: 1 addition & 0 deletions skia/skia_chrome.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'ext/recursive_gaussian_convolution.cc',
'ext/SkDiscardableMemory_chrome.cc',
'ext/SkMemory_new_handler.cpp',
'ext/SkTraceMemoryDump_chrome.cc',
'ext/skia_memory_dump_provider.cc',
'ext/skia_utils_base.cc',
'ext/skia_utils_ios.mm',
Expand Down

0 comments on commit 7040503

Please sign in to comment.