Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,18 @@ if(TARGET Vulkan::Headers)
add_test(NAME vk_host_at_aliasing COMMAND vk_host_at_aliasing_test)
endif()
# CPU-only: ScratchBuffers chunk alignment, retire/collect/forget, and spare cap (runtime/src/vulkan_scratch.h).
if(WIN32 AND BUILD_TESTING)
# LEONIDA_MEM_PROBE (src/mem_probe.h) on a stand-in function table: unset (nothing installed, nothing written)
# and set (--on: the entries wrapped, the counts and the lines). No GPU.
add_executable(mem_probe_test test/mem_probe_test.cpp)
target_link_libraries(mem_probe_test PRIVATE Vulkan::Headers)
target_compile_definitions(mem_probe_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
if(MSVC)
target_compile_options(mem_probe_test PRIVATE /W4 /permissive- /utf-8)
endif()
add_test(NAME mem_probe_off COMMAND mem_probe_test)
add_test(NAME mem_probe_on COMMAND mem_probe_test --on)
endif()
add_executable(vulkan_scratch_test test/vulkan_scratch_test.cpp)
target_link_libraries(vulkan_scratch_test PRIVATE Vulkan::Headers)
if(MSVC)
Expand Down
8 changes: 5 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9677,7 +9677,7 @@ class Device final
for (auto queue : info.compute_queues) state_.context->add_diagnostic_queue(queue);
for (auto queue : info.copy_queues) state_.context->add_diagnostic_queue(queue);
}
leonida::mem_probe::install(state_.context->vk); // LOCAL DIAGNOSTIC: before anything allocates
const bool mem_probe_installed = leonida::mem_probe::install(state_.context->vk);
if (info.device_fault_diagnostics && rt::compute_probe::enabled())
state_.context->set_input_probe([this](auto lists, auto sequence, auto queue) {
rt::compute_probe::inspect(state_, lists, sequence, queue);
Expand Down Expand Up @@ -9760,7 +9760,7 @@ class Device final
}
if (info.buffer_device_address && state_.context->loaded() && !create_arena())
std::fprintf(stderr, "[rt] d3d12: the GPU address arena could not be created\n");
{ // LOCAL DIAGNOSTIC: mem probe (LEONIDA_MEM_PROBE=<seconds>)
if (mem_probe_installed) {
leonida::mem_probe::DeviceFacts facts;
facts.memory = state_.memory;
facts.title_ranges = [] { return leonida::title_va_aliases().title_memory_ranges(); };
Expand Down Expand Up @@ -9806,7 +9806,7 @@ class Device final
(unsigned long long)shadows, leonida::fsdraw::pipeline_count());
return std::string(text);
};
leonida::mem_probe::start(std::move(facts));
mem_probe_ = leonida::mem_probe::start(std::move(facts));
}
if (info.conditional_rendering) {
const auto properties = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
Expand Down Expand Up @@ -10257,6 +10257,7 @@ class Device final
}

~Device() override {
mem_probe_.reset(); // The sampler reads state_.
raw_images_.clear();
leonida::title_va_aliases().clear_hooks(&state_);
{
Expand Down Expand Up @@ -15148,6 +15149,7 @@ class Device final
std::mutex native_pipeline_mutex_;
std::unordered_map<std::string, std::weak_ptr<PipelineState>> native_pipelines_;
DeviceState state_;
std::unique_ptr<leonida::mem_probe::Sampler> mem_probe_;
struct RawImage {
std::unique_ptr<Resource> resource;
std::vector<std::string> fingerprints;
Expand Down
63 changes: 45 additions & 18 deletions runtime/src/mem_probe.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
#include <atomic>
#include <cctype>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
Expand Down Expand Up @@ -60,7 +62,7 @@ struct State {
std::uint64_t module_bytes = 0, modules_made = 0, module_bytes_made = 0;
std::uint64_t graphics_live = 0, compute_live = 0, graphics_made = 0, compute_made = 0;
std::uint64_t graphics_ns = 0, compute_ns = 0; // time inside vkCreate*Pipelines
std::uint64_t installed = 0;
bool installed = false;
ULONGLONG started = 0;
};

Expand Down Expand Up @@ -239,15 +241,16 @@ inline VKAPI_ATTR void VKAPI_CALL destroy_pipeline(VkDevice device, VkPipeline p

// Wraps the runtime's function table (leonida::VulkanDeviceFunctions). Once per process: the first device wins.
template<typename Functions>
void install(Functions& vk) {
if (!interval_seconds()) return;
bool install(Functions& vk) {
if (!interval_seconds()) return false;
State& s = state();
std::lock_guard lock(s.mutex);
if (s.installed++) return;
if (s.installed) return false;
if (!vk.vkAllocateMemory || !vk.vkFreeMemory || !vk.vkMapMemory || !vk.vkUnmapMemory ||
!vk.vkCreateShaderModule || !vk.vkDestroyShaderModule || !vk.vkCreateGraphicsPipelines ||
!vk.vkCreateComputePipelines || !vk.vkDestroyPipeline)
return;
return false;
s.installed = true;
s.started = GetTickCount64();
s.allocate = vk.vkAllocateMemory, vk.vkAllocateMemory = allocate_memory;
s.free = vk.vkFreeMemory, vk.vkFreeMemory = free_memory;
Expand All @@ -259,6 +262,7 @@ void install(Functions& vk) {
s.create_compute = vk.vkCreateComputePipelines, vk.vkCreateComputePipelines = create_compute_pipelines;
s.destroy_pipeline = vk.vkDestroyPipeline, vk.vkDestroyPipeline = destroy_pipeline;
std::fprintf(stderr, "[rt] mem-probe: installed, every %u s\n", interval_seconds());
return true;
}

// ---- /proc/self/smaps, classified ----
Expand Down Expand Up @@ -552,19 +556,42 @@ inline void sample(const DeviceFacts& facts) {
read_rollup().c_str());
}

// Starts the sampling thread (once). The thread runs until the process ends.
inline void start(DeviceFacts facts) {
const unsigned seconds = interval_seconds();
if (!seconds) return;
static std::once_flag once;
std::call_once(once, [&] {
std::thread([facts = std::move(facts), seconds] {
for (;;) {
Sleep(seconds * 1000);
sample(facts);
}
}).detach();
});
// Own the sampler on the device whose state its callbacks read.
class Sampler {
public:
Sampler(DeviceFacts facts, unsigned seconds)
: thread_([this, facts = std::move(facts), seconds] {
std::unique_lock lock(mutex_);
while (!wake_.wait_for(lock, std::chrono::seconds(seconds), [this] { return stopping_; })) {
lock.unlock();
sample(facts);
lock.lock();
}
lock.unlock();
sample(facts);
}) {}
~Sampler() {
{
std::lock_guard lock(mutex_);
stopping_ = true;
}
wake_.notify_one();
thread_.join();
}
Sampler(const Sampler&) = delete;
Sampler& operator=(const Sampler&) = delete;

private:
std::mutex mutex_;
std::condition_variable wake_;
bool stopping_ = false;
std::thread thread_;
};

inline std::unique_ptr<Sampler> start(DeviceFacts facts) {
if (const unsigned seconds = interval_seconds())
return std::make_unique<Sampler>(std::move(facts), seconds);
return nullptr;
}

} // namespace leonida::mem_probe
Loading
Loading