Files
Strata/engine/metal/tests/buffer_free_test.cpp
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

57 lines
2.5 KiB
C++

// metal/tests/buffer_free_test.cpp - Context::free gives the memory back: 4 rounds of 40 x 100 MB buffers allocated,
// used by one dispatch and freed, on a thread with no autorelease pool (as the engine's are). The process footprint
// (task_vm_info.phys_footprint) after each free must be back near the start, and must not grow round after round:
// Metal autoreleases a reference to every new buffer, and before alloc() drained its own pool every freed buffer
// stayed allocated (16.8 GB after the 4 rounds). Exit 0 = every check passed.
#include "mtl_rt.h"
#include <mach/mach.h>
#include <cstdio>
#include <string>
#include <vector>
static int failures = 0;
static void check(bool ok, const std::string& what) {
std::printf("%s %s\n", ok ? "ok " : "FAIL", what.c_str());
failures += !ok;
}
static double footprint_gb() {
task_vm_info_data_t info;
mach_msg_type_number_t n = TASK_VM_INFO_COUNT;
if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &info, &n) != KERN_SUCCESS) return -1;
return info.phys_footprint / 1e9;
}
int main() {
mtl::Context cx;
const int lib = cx.compile({"mm_bench.metal"}, "#define NO_GGML_COMMON 1\n");
mtl::Pipeline p = cx.pipeline(lib, "coop_layout_16_64_32");
const int nbuf = 40;
const size_t bytes = 100u << 20;
const double set_gb = nbuf * bytes / 1e9, start = footprint_gb();
std::printf("device %s; %d x %zu MB a round (%.2f GB); footprint at start %.2f GB\n", cx.device_name().c_str(), nbuf,
bytes >> 20, set_gb, start);
double after_first = 0;
for (int round = 0; round < 4; ++round) {
std::vector<mtl::Buffer> v;
for (int i = 0; i < nbuf; ++i) v.push_back(cx.alloc(bytes));
mtl::Dispatch d; // one dispatch on the first buffer (the GPU holds no reference after run returns)
d.p = &p;
d.args = {mtl::Arg::B(v[0])};
d.tg[0] = 32;
cx.run({d});
const double held = footprint_gb();
for (auto& b : v) cx.free(b);
const double freed = footprint_gb();
std::printf("round %d: %.2f GB allocated, %.2f GB after free\n", round, held, freed);
if (round == 0) after_first = freed;
check(held >= start + 0.9 * set_gb, "round " + std::to_string(round) + ": the buffers are counted while held");
check(freed < start + 0.25 * set_gb, "round " + std::to_string(round) + ": free() gives them back");
}
check(footprint_gb() < after_first + 0.25 * set_gb, "no growth over the 4 rounds");
std::printf(failures ? "FAILED\n" : "PASS\n");
return failures ? 1 : 0;
}