feat(metal): share shader caches across same-user clients

Use a writable per-user cache with container fallback, keep Turnip disk caching enabled, and isolate concurrent cache-pack imports. Verify cache round trips, concurrent imports, and the iPadOS 27 plugin compile.
This commit is contained in:
rponeawa
2026-11-02 10:33:03 +08:00
Unverified
parent 831a9dcbd6
commit 69efcdcdc5
5 changed files with 79 additions and 13 deletions
+9 -3
View File
@@ -21,6 +21,7 @@
#include <zlib.h>
#include <functional>
#include <set>
#include <atomic>
#include <string>
#include <vector>
#ifndef CACHEEXPORT_FORMAT_ONLY // the format alone (host check, bridge/cache_export_host.cpp); the plugin also gets the sender
@@ -55,6 +56,9 @@ static void scan(const std::string &root, const std::string &relative, std::vect
if (!dir) return;
while (dirent *entry = readdir(dir)) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue;
// A concurrent import or cache writer has not published these entries yet.
const std::string leaf = entry->d_name;
if (leaf.find(".import.") != std::string::npos || leaf.rfind(".write-", 0) == 0) continue;
const std::string path = relative.empty() ? std::string(entry->d_name) : relative + "/" + entry->d_name;
struct stat st = {};
if (lstat((root + "/" + path).c_str(), &st) != 0) continue;
@@ -223,8 +227,10 @@ static int importPack(const char *pack, const std::string &directory) {
return importStream(r, directory);
}
static int importStream(PackReader &r, const std::string &directory) {
static std::atomic<unsigned long long> importSequence{0};
const std::string suffix=".import."+std::to_string(getpid())+"."+std::to_string(importSequence.fetch_add(1));
std::vector<std::string> written;
auto fail = [&]() {for (const std::string &f : written) unlink((f + ".import").c_str()); return -1;};
auto fail = [&]() {for (const std::string &f : written) unlink((f + suffix).c_str()); return -1;};
char magic[8];
uint32_t rootLength = 0, count = 0;
if (!r.read(magic, 8) || memcmp(magic, "M2VCACH1", 8) || !r.read(&rootLength, 4) || rootLength > 4096 || !r.skip(rootLength) ||
@@ -246,7 +252,7 @@ static int importStream(PackReader &r, const std::string &directory) {
for (size_t slash = full.find('/', directory.size() + 1); slash != std::string::npos; slash = full.find('/', slash + 1))
mkdir(full.substr(0, slash).c_str(), 0700);
written.push_back(full);
FILE *out = fopen((full + ".import").c_str(), "wb");
FILE *out = fopen((full + suffix).c_str(), "wb");
if (!out) return fail();
bool ok = true;
for (uint64_t left = size; ok && left;) {
@@ -259,7 +265,7 @@ static int importStream(PackReader &r, const std::string &directory) {
}
if (!r.finished()) return fail();
int renamed = 0;
for (const std::string &f : written) if (rename((f + ".import").c_str(), f.c_str()) == 0) ++renamed; else unlink((f + ".import").c_str());
for (const std::string &f : written) if (rename((f + suffix).c_str(), f.c_str()) == 0) ++renamed; else unlink((f + suffix).c_str());
baked().insert(seen.begin(), seen.end()); // only a pack read to its end counts
return renamed;
}
+11 -7
View File
@@ -8,6 +8,7 @@
// gets its own native device too (its own driver connection, GPU address space and shader cache), as every process does with Apple's
// own GPU plugin.
#import <Foundation/Foundation.h>
#include "SharedShaderCache.h"
#include <pthread.h>
#include <thread>
#include <dlfcn.h>
@@ -242,15 +243,17 @@ static id clientDevice(unsigned port) {
}
// IOSurfaces this GPU reads or writes must be plain rows, as in the render server.
CFPreferencesSetAppValue(CFSTR("CAEnableIOSurfaceCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication);
char candidates[2][512] = {};
char candidates[3][512] = {};
if (!confstr(_CS_DARWIN_USER_CACHE_DIR, candidates[0], sizeof candidates[0] - 16)) candidates[0][0] = 0;
if (!confstr(_CS_DARWIN_USER_TEMP_DIR, candidates[1], sizeof candidates[1] - 16)) candidates[1][0] = 0;
strlcpy(candidates[2], sharedShaderCacheDirectory().c_str(), sizeof candidates[2]);
id device = nil;
const char *lastError = "";
unsigned usable = 0;
for (unsigned i = 0; i < 2 && !device; ++i) {
for (unsigned i : {2u, 0u, 1u}) {
if (device) break;
if (!candidates[i][0]) continue;
strlcat(candidates[i], "m2v", sizeof candidates[i]);
if (i != 2) strlcat(candidates[i], "m2v", sizeof candidates[i]);
mkdir(candidates[i], 0700);
if (access(candidates[i], W_OK) != 0) continue;
++usable;
@@ -435,7 +438,7 @@ static id deviceForPort(unsigned port) {
if (getenv("SHENG_DIAG")) setenv("METAL2VULKAN_LOG_PIPELINES", "1", 0);
CFPreferencesSetAppValue(CFSTR("CAEnableFramebufferCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication);
CFPreferencesSetAppValue(CFSTR("CAEnableIOSurfaceCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication);
char candidates[5][512] = {};
char candidates[6][512] = {};
if (!confstr(_CS_DARWIN_USER_CACHE_DIR, candidates[0], sizeof candidates[0] - 16)) candidates[0][0] = 0;
if (!confstr(_CS_DARWIN_USER_TEMP_DIR, candidates[1], sizeof candidates[1] - 16)) candidates[1][0] = 0;
strlcpy(candidates[2], "/tmp/", sizeof candidates[2]);
@@ -448,17 +451,18 @@ static id deviceForPort(unsigned port) {
mkdir(candidates[4], 0700);
strlcat(candidates[4], "/", sizeof candidates[4]);
}
static const unsigned order[] = {4, 0, 1, 2, 3};
strlcpy(candidates[5], sharedShaderCacheDirectory().c_str(), sizeof candidates[5]);
static const unsigned order[] = {5, 4, 0, 1, 2, 3};
for (unsigned i : order) {
if (device) break;
if (!candidates[i][0]) continue;
strlcat(candidates[i], "m2v", sizeof candidates[i]);
if (i != 5) strlcat(candidates[i], "m2v", sizeof candidates[i]);
mkdir(candidates[i], 0700);
if (access(candidates[i], W_OK) != 0) continue;
// Files an earlier image's copying import left half written here (int 590aaee2), which hold memory of the Data disk.
{
static bool once;
if (!once) {once = true; removeHalfImported(candidates[i]);}
if (!once && i != 5) {once = true; removeHalfImported(candidates[i]);}
}
// The image's pipeline pack (an earlier boot's export), into this directory before the runtime reads it. Marker 0x6Cnn: files written
// (saturated at 0xfe), 0x6Cff the pack was refused.
+4 -3
View File
@@ -13,6 +13,7 @@
#pragma once
#define VK_NO_PROTOTYPES 1
#include <vulkan/vulkan_core.h>
#include "SharedShaderCache.h"
#include "NativeTurnipShaders.h" // generated: scripts/gen-turnip-smoke-shaders.py
#include <dlfcn.h>
#include <mach/mach.h>
@@ -429,10 +430,10 @@ static Result run(Marker &&mark, const char *libraryPath) {
return out;
};
// The Darwin backend reaches the driver the same way the plugin does: the AGX accelerator service with open type 0x4147, ABI methods through
// an admitted selector. Nothing to configure; set here only so the choice is visible. The shader disk cache is off: this runs inside a
// system process whose home directory is not ours to fill.
// an admitted selector. The shader cache uses the same writable per-user directory as the Metal runtime.
setenv("TU_DARWIN_SERVICE", "IOAcceleratorES", 0);
setenv("MESA_SHADER_CACHE_DISABLE", "true", 0);
const std::string sharedCache = sharedShaderCacheDirectory();
if (!sharedCache.empty()) configureShaderDiskCache(sharedCache);
// Concurrent binning (BV running ahead of BR) is a performance feature; its barriers make BR wait for BV timestamps. int-058's
// first fill hung with the ring stopped inside the IB (no fault), before the kernel framed IBs for both threads. Keep it off
// until a run with the framing has passed.
+42
View File
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: MIT
#pragma once
#include <string>
#include <pwd.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <vector>
// Processes of one user share immutable, version-keyed shader entries. A sandbox
// that cannot write this directory continues with its container's cache.
inline std::string sharedShaderCacheDirectory() {
static const std::string path = []() -> std::string {
std::string root;
if (const char *override = getenv("NATIVE_METAL_SHARED_CACHE_DIR")) {
if (!*override) return {};
root = override;
} else {
passwd user{}, *found=nullptr;
std::vector<char> bytes(16384);
if (getpwuid_r(getuid(), &user, bytes.data(), bytes.size(), &found) || !found || !user.pw_dir) return {};
root = std::string(user.pw_dir) + "/Library/Caches/org.unixnu.metal";
}
mkdir(root.c_str(), 0700);
struct stat st = {};
if (lstat(root.c_str(), &st) || !S_ISDIR(st.st_mode) || st.st_uid != getuid() || (st.st_mode & 0022)) return {};
std::string probe = root + "/.write-XXXXXX";
int fd = mkstemp(probe.data());
if (fd < 0) return {};
close(fd); unlink(probe.c_str());
return root;
}();
return path;
}
// Call before the first Vulkan device is opened, including the optional smoke path.
inline void configureShaderDiskCache(const std::string &root) {
if (root.empty()) return;
const std::string ir3 = root + "/ir3";
mkdir(ir3.c_str(), 0700);
if (access(ir3.c_str(), W_OK) == 0) setenv("MESA_SHADER_CACHE_DIR", ir3.c_str(), 0);
}
+13
View File
@@ -7,6 +7,7 @@
#include "../CacheExport.h"
#include <cstdio>
#include <cstdlib>
#include <sys/wait.h>
#define CHECK(x) do {if(!(x)){std::fprintf(stderr,"line %d: %s\n",__LINE__,#x);std::exit(1);}}while(0)
@@ -37,6 +38,18 @@ int main(int argc,char **argv) {
CHECK(cacheexport::importPack(blobPath.c_str(),dest)==(int)files.size());
CHECK(cacheexport::baked().size()==files.size()); /* every entry of a pack read to its end is known as baked */
for(const auto &f:files)CHECK(slurp(source+"/"+f.path)==slurp(dest+"/"+f.path));
// Independent processes may populate the same directory at the same time.
const std::string shared=dest+"-shared";mkdir(shared.c_str(),0700);
pid_t workers[2];
for (auto &pid:workers) {
pid=fork();CHECK(pid>=0);
if(!pid)_exit(cacheexport::importPack(blobPath.c_str(),shared)>=0?0:1);
}
for(auto pid:workers){int status=0;CHECK(waitpid(pid,&status,0)==pid && WIFEXITED(status) && WEXITSTATUS(status)==0);}
for(const auto &f:files)CHECK(slurp(source+"/"+f.path)==slurp(shared+"/"+f.path));
std::vector<cacheexport::File> sharedFiles;cacheexport::scan(shared,"",sharedFiles,0);CHECK(sharedFiles.size()==files.size());
spill(shared+"/in-progress.import.1.0",{1,2});
sharedFiles.clear();cacheexport::scan(shared,"",sharedFiles,0);CHECK(sharedFiles.size()==files.size());
CHECK(cacheexport::importPack(blobPath.c_str(),dest)==0); /* all present with their sizes: nothing rewritten */
/* damaged packs: truncated, bad magic, flipped compressed byte */
std::vector<uint8_t> bad=blob;bad.resize(bad.size()/2);spill(blobPath+".t",bad);