Bug 1870585 - Add webgl::dmd_unordered_map for WebGLTexture memory reporting of cache map. r=gfx-reviewers,lsalzman,bradwerth

Differential Revision: https://phabricator.services.mozilla.com/D203869
This commit is contained in:
Kelsey Gilbert 2024-03-11 17:37:24 +00:00
parent 95630ed986
commit 9c87b856f6
7 changed files with 142 additions and 10 deletions

View File

@ -9,8 +9,7 @@
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"
#include <unordered_map>
#include <unordered_set>
#include "DmdStdContainers.h"
#include <vector>
// -
@ -25,7 +24,7 @@ class CacheInvalidator {
friend class AbstractCache;
private:
mutable std::unordered_set<AbstractCache*> mCaches;
mutable webgl::dmd_unordered_set<AbstractCache*> mCaches;
public:
virtual ~CacheInvalidator() {
@ -38,6 +37,12 @@ class CacheInvalidator {
}
void InvalidateCaches() const;
// -
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
return mCaches.SizeOfExcludingThis(mso);
}
};
// -
@ -122,11 +127,15 @@ class CacheWeakMap final {
}
};
using MapT =
std::unordered_map<const KeyT*, UniquePtr<Entry>, DerefHash, DerefEqual>;
using MapT = webgl::dmd_unordered_map<const KeyT*, UniquePtr<Entry>,
DerefHash, DerefEqual>;
MapT mMap;
public:
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
return mMap.SizeOfExcludingThis(mso);
}
UniquePtr<Entry> MakeEntry(const KeyT& key, ValueT&& value) {
return UniquePtr<Entry>(new Entry(*this, key, std::move(value)));
}

View File

@ -0,0 +1,100 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef dom_canvas_DmdStdContainers_h
#define dom_canvas_DmdStdContainers_h
#include "mozilla/MemoryReporting.h"
#include "mozilla/layers/BuildConstants.h"
#include <unordered_map>
#include <unordered_set>
namespace mozilla::webgl {
// -
template <class T>
class dmd_allocator {
public:
using value_type = T;
private:
size_t mMallocSize = 0;
public:
dmd_allocator() = default;
// -
template <class U>
friend class dmd_allocator;
template <class U>
explicit dmd_allocator(const dmd_allocator<U>& rhs) {
if constexpr (kIsDmd) {
mMallocSize = rhs.mMallocSize;
}
}
// -
value_type* allocate(const size_t n) {
const auto p = std::allocator<value_type>{}.allocate(n);
if constexpr (kIsDmd) {
mMallocSize += moz_malloc_size_of(p);
}
return p;
}
void deallocate(value_type* const p, const size_t n) {
if constexpr (kIsDmd) {
mMallocSize -= moz_malloc_size_of(p);
}
std::allocator<value_type>{}.deallocate(p, n);
}
// -
size_t SizeOfExcludingThis(mozilla::MallocSizeOf) const {
return mMallocSize;
}
};
// -
template <class Key, class T, class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = dmd_allocator<std::pair<const Key, T>>,
class _StdT = std::unordered_map<Key, T, Hash, KeyEqual, Allocator>>
class dmd_unordered_map : public _StdT {
public:
using StdT = _StdT;
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
const auto& a = StdT::get_allocator();
return a.SizeOfExcludingThis(mso);
}
};
// -
template <class Key, class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = dmd_allocator<Key>,
class _StdT = std::unordered_set<Key, Hash, KeyEqual, Allocator>>
class dmd_unordered_set : public _StdT {
public:
using StdT = _StdT;
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
const auto& a = StdT::get_allocator();
return a.SizeOfExcludingThis(mso);
}
};
// -
} // namespace mozilla::webgl
#endif // dom_canvas_DmdStdContainers_h

View File

@ -12,8 +12,9 @@
#include "WebGLTexture.h"
namespace mozilla {
MOZ_DEFINE_MALLOC_SIZE_OF(WebGLShaderMallocSizeOf)
namespace webgl {
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
} // namespace webgl
void WebGLMemoryTracker::EnsureRegistered() {
static bool sIsRegistered = []() {
@ -42,6 +43,7 @@ WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
int64_t shaderCpuSize = 0;
size_t texCount = 0;
size_t texHeapOverhead = 0;
int64_t texGpuSize = 0;
for (const auto& context : contexts) {
@ -70,7 +72,7 @@ WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
shaderCount += context->mShaderMap.size();
for (const auto& pair : context->mShaderMap) {
const auto& shader = pair.second;
shaderCpuSize += shader->SizeOfIncludingThis(WebGLShaderMallocSizeOf);
shaderCpuSize += shader->SizeOfIncludingThis(webgl::MallocSizeOf);
}
// -
@ -79,6 +81,7 @@ WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
for (const auto& pair : context->mTextureMap) {
const auto& texture = pair.second;
texGpuSize += texture->MemoryUsage();
texHeapOverhead += texture->SizeOfIncludingThis(webgl::MallocSizeOf);
}
}
@ -126,10 +129,14 @@ WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
"Number of WebGL renderbuffers.");
MOZ_COLLECT_REPORT(
"explicit/webgl/shader", KIND_HEAP, UNITS_BYTES, shaderCpuSize,
"explicit/webgl/shaders", KIND_HEAP, UNITS_BYTES, shaderCpuSize,
"Combined size of WebGL shader ASCII sources and translation logs "
"cached on the heap.");
MOZ_COLLECT_REPORT("explicit/webgl/textures", KIND_HEAP, UNITS_BYTES,
texHeapOverhead,
"WebGLTexture implementation detail overhead.");
MOZ_COLLECT_REPORT("webgl-shader-count", KIND_OTHER, UNITS_COUNT,
static_cast<int64_t>(shaderCount),
"Number of WebGL shaders.");

View File

@ -39,7 +39,7 @@ class ShaderValidatorResults final {
bool CanLinkTo(const ShaderValidatorResults& vert,
nsCString* const out_log) const;
size_t SizeOfIncludingThis(MallocSizeOf) const;
size_t SizeOfIncludingThis(mozilla::MallocSizeOf) const;
};
class ShaderValidator final {

View File

@ -203,6 +203,14 @@ class WebGLTexture final : public WebGLContextBoundObject,
~WebGLTexture() override;
public:
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
return CacheInvalidator::SizeOfExcludingThis(mso) +
mSamplingCache.SizeOfExcludingThis(mso);
}
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mso) const {
return mso(this) + SizeOfExcludingThis(mso);
}
////////////////////////////////////
// GL calls
bool BindTexture(TexTarget texTarget);

View File

@ -52,6 +52,7 @@ EXPORTS.mozilla.dom += [
"CanvasRenderingContext2D.h",
"CanvasRenderingContextHelper.h",
"CanvasUtils.h",
"DmdStdContainers.h",
"GeneratePlaceholderCanvasData.h",
"ImageBitmap.h",
"ImageBitmapRenderingContext.h",

View File

@ -52,6 +52,13 @@ constexpr bool kIsAndroid =
false;
#endif
constexpr bool kIsDmd =
#ifdef MOZ_DMD
true;
#else
false;
#endif
} // namespace mozilla
#endif // BUILD_CONSTANTS_H_