Skip to content

Commit

Permalink
Log excess buffer allocations for Metal
Browse files Browse the repository at this point in the history
  • Loading branch information
bejado committed May 7, 2024
1 parent 1b9d2c6 commit c0ee1e2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions filament/backend/src/metal/MetalBuffer.h
Expand Up @@ -18,6 +18,7 @@
#define TNT_FILAMENT_DRIVER_METALBUFFER_H

#include "MetalContext.h"
#include "MetalPlatform.h"

#include <backend/DriverEnums.h>

Expand All @@ -34,6 +35,8 @@ namespace filament::backend {
class TrackedMetalBuffer {
public:

static constexpr size_t EXCESS_BUFFER_COUNT = 30000;

enum class Type {
NONE = 0,
GENERIC = 1,
Expand Down Expand Up @@ -62,6 +65,12 @@ class TrackedMetalBuffer {
if (buffer) {
aliveBuffers[toIndex(type)]++;
mType = type;
if (getAliveBuffers() >= EXCESS_BUFFER_COUNT) {
if (platform && platform->hasDebugUpdateStatFunc()) {
platform->debugUpdateStat("filament.metal.excess_buffers_allocated",
TrackedMetalBuffer::getAliveBuffers());
}
}
}
}

Expand Down Expand Up @@ -96,6 +105,7 @@ class TrackedMetalBuffer {
assert_invariant(type != Type::NONE);
return aliveBuffers[toIndex(type)];
}
static void setPlatform(MetalPlatform* p) { platform = p; }

private:
void swap(TrackedMetalBuffer& other) noexcept {
Expand All @@ -106,6 +116,7 @@ class TrackedMetalBuffer {
id<MTLBuffer> mBuffer;
Type mType = Type::NONE;

static MetalPlatform* platform;
static std::array<uint64_t, TypeCount> aliveBuffers;
};

Expand Down
1 change: 1 addition & 0 deletions filament/backend/src/metal/MetalBuffer.mm
Expand Up @@ -23,6 +23,7 @@
namespace backend {

std::array<uint64_t, TrackedMetalBuffer::TypeCount> TrackedMetalBuffer::aliveBuffers = { 0 };
MetalPlatform* TrackedMetalBuffer::platform = nullptr;

MetalBuffer::MetalBuffer(MetalContext& context, BufferObjectBinding bindingType, BufferUsage usage,
size_t size, bool forceGpuBuffer) : mBufferSize(size), mContext(context) {
Expand Down
3 changes: 3 additions & 0 deletions filament/backend/src/metal/MetalDriver.mm
Expand Up @@ -105,6 +105,8 @@
driverConfig.disableHandleUseAfterFreeCheck) {
mContext->driver = this;

TrackedMetalBuffer::setPlatform(platform);

mContext->device = mPlatform.createDevice();
assert_invariant(mContext->device);

Expand Down Expand Up @@ -198,6 +200,7 @@
}

MetalDriver::~MetalDriver() noexcept {
TrackedMetalBuffer::setPlatform(nullptr);
mContext->device = nil;
mContext->emptyTexture = nil;
CFRelease(mContext->textureCache);
Expand Down

0 comments on commit c0ee1e2

Please sign in to comment.