Repository navigation
Expand file tree
/
Copy pathCacheExport.h
More file actions
313 lines (298 loc) · 15.8 KB
/
Copy pathCacheExport.h
File metadata and controls
313 lines (298 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// SPDX-License-Identifier: MIT
// Pipeline-cache export (int-147). The render server's runtime keeps its translated shaders and the Vulkan driver's pipeline records in its
// cache directory, but that directory lives on the RAM Data disk: every boot compiled the whole UI again (int-146: 98 pipelines, 63 s,
// up to 1.6 s each, also while the user opened App Library). This thread sends the directory to the kernel's UFS log (method 0x7100 on a
// hand-off connection, kits/sheng/BootLog.c export region) whenever it changed and then stayed unchanged for one period; the host reads it
// back with the log and builds the next image's Data with it, so those pipelines start as cache hits.
// A boot whose image carried a pack exports only the files the pack did not have (see baked()).
// Stream: "M2VCACH1", u32 directory length, directory, u32 file count, then per file u32 path length, u32 mode, u32 uid, u32 gid, u64 size,
// path (relative to the directory), contents. Export blob: "SHEXR001", u32 stream bytes, u32 0, the stream as it is: it goes out in pieces
// while the files are read, so the render server never holds more than a piece (int-149: one boot's new variants were about 100 MB).
// Image pack: "SHEXP001", u32 stream bytes, u32 compressed bytes, the stream zlib-compressed (scripts/add-gpu-plugin-data.py merges exports
// into one). The export only reads.
#pragma once
#include <dirent.h>
#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <zlib.h>
#include <set>
#include <string>
#include <vector>
#ifndef CACHEEXPORT_FORMAT_ONLY // the format alone (host check, bridge/cache_export_host.cpp); the plugin also gets the sender
#include "NativeAdrenoUserTest.h"
#endif
namespace cacheexport {
// kLimit: the largest export blob, one half of the kernel's 256 MiB export region.
constexpr uint32_t kMethod = 0x7100, kPiece = 3968, kLimit = 128u << 20;
struct File {std::string path; struct stat st;};
// A pack in the image is the union of several boots' exports (scripts/add-gpu-plugin-data.py); it is read as a stream, so only its own
// file and the stream bounds limit it.
constexpr uint64_t kPackLimit = 1ull << 30;
// Files the image's pack carried (path and size). The export leaves them out: the host has them already and merges each boot's export
// with the earlier ones, so a boot sends only what it compiled itself (int-148: the whole directory outgrew the export area once the UI's
// own variants were added, and the export refused the build, marker 0x69f2).
static std::set<std::string> &baked() {static std::set<std::string> files; return files;}
static std::string bakedKey(const std::string &path, uint64_t size) {return path + '\n' + std::to_string(size);}
static void dropBaked(std::vector<File> &files) {
if (baked().empty()) return;
std::vector<File> fresh;
for (File &f : files) if (!baked().count(bakedKey(f.path, (uint64_t)f.st.st_size))) fresh.push_back(std::move(f));
files.swap(fresh);
}
static void scan(const std::string &root, const std::string &relative, std::vector<File> &out, unsigned depth) {
if (depth > 8) return;
DIR *dir = opendir((root + "/" + relative).c_str());
if (!dir) return;
while (dirent *entry = readdir(dir)) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) 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;
if (S_ISDIR(st.st_mode)) scan(root, path, out, depth + 1);
else if (S_ISREG(st.st_mode)) out.push_back({path, st});
}
closedir(dir);
}
// Count, bytes and newest modification of the directory: equal on two scans = nothing is being written.
static uint64_t signature(const std::vector<File> &files) {
uint64_t s = files.size() * 0x9e3779b97f4a7c15ull;
for (const File &f : files) s = s * 31 + (uint64_t)f.st.st_size * 1000003 + (uint64_t)f.st.st_mtimespec.tv_sec * 7 + (uint64_t)f.st.st_mtimespec.tv_nsec;
return s;
}
template <typename T> static void put(std::vector<uint8_t> &b, T v) {const uint8_t *p = (const uint8_t *)&v; b.insert(b.end(), p, p + sizeof v);}
// The stream's length for these files, from their sizes as scanned.
static uint64_t streamBytes(const std::string &root, const std::vector<File> &files) {
uint64_t n = 8 + 4 + root.size() + 4;
for (const File &f : files) n += 24 + f.path.size() + (uint64_t)f.st.st_size;
return n;
}
// The stream, handed to emit(bytes, count) in order (emit returns false to stop); false when a file vanished or no longer has the size
// it was scanned with, or emit stopped.
template <typename Emit> static bool stream(const std::string &root, const std::vector<File> &files, Emit &&emit) {
std::vector<uint8_t> head;
{static const char magic[] = "M2VCACH1"; head.insert(head.end(), magic, magic + 8);}
put<uint32_t>(head, (uint32_t)root.size());
head.insert(head.end(), root.begin(), root.end());
put<uint32_t>(head, (uint32_t)files.size());
if (!emit(head.data(), head.size())) return false;
std::vector<uint8_t> chunk(65536);
for (const File &f : files) {
FILE *in = fopen((root + "/" + f.path).c_str(), "rb");
if (!in) return false;
head.clear();
put<uint32_t>(head, (uint32_t)f.path.size());
put<uint32_t>(head, (uint32_t)f.st.st_mode);
put<uint32_t>(head, (uint32_t)f.st.st_uid);
put<uint32_t>(head, (uint32_t)f.st.st_gid);
put<uint64_t>(head, (uint64_t)f.st.st_size);
head.insert(head.end(), f.path.begin(), f.path.end());
bool ok = emit(head.data(), head.size());
uint64_t left = (uint64_t)f.st.st_size;
while (ok && left) {
const size_t want = left < chunk.size() ? (size_t)left : chunk.size();
ok = fread(chunk.data(), 1, want, in) == want && emit(chunk.data(), want);
left -= want;
}
ok = ok && fgetc(in) == EOF; // grown since the scan: refuse rather than send a short copy
fclose(in);
if (!ok) return false;
}
return true;
}
#ifdef CACHEEXPORT_FORMAT_ONLY
// The image pack format (SHEXP001, compressed), for the host check; scripts/add-gpu-plugin-data.py builds the same. False when a file
// vanished or zlib is missing.
static bool build(const std::string &root, const std::vector<File> &files, std::vector<uint8_t> &out) {
using BoundFn = unsigned long (*)(unsigned long);
using CompressFn = int (*)(uint8_t *, unsigned long *, const uint8_t *, unsigned long, int);
static void *z = dlopen("/usr/lib/libz.1.dylib", RTLD_NOW | RTLD_LOCAL);
if (!z) return false;
auto bound = (BoundFn)dlsym(z, "compressBound");
auto compress = (CompressFn)dlsym(z, "compress2");
if (!bound || !compress) return false;
std::vector<uint8_t> raw;
if (!stream(root, files, [&](const uint8_t *p, size_t n) {raw.insert(raw.end(), p, p + n); return true;})) return false;
unsigned long zbytes = bound(raw.size());
std::vector<uint8_t> zdata(zbytes);
if (compress(zdata.data(), &zbytes, raw.data(), raw.size(), 6) != 0) return false;
out.clear();
{static const char magic[] = "SHEXP001"; out.insert(out.end(), magic, magic + 8);}
put<uint32_t>(out, (uint32_t)raw.size());
put<uint32_t>(out, (uint32_t)zbytes);
out.insert(out.end(), zdata.begin(), zdata.begin() + (long)zbytes);
return true;
}
#endif
// Reads a pack's stream: inflated as it is consumed, so neither the pack nor the stream is held in memory.
struct PackReader {
using InitFn = int (*)(z_streamp, const char *, int);
using InflateFn = int (*)(z_streamp, int);
using EndFn = int (*)(z_streamp);
FILE *in = nullptr;
z_stream z = {};
InflateFn inflateFn = nullptr;
EndFn endFn = nullptr;
bool open = false, ended = false;
uint64_t left = 0, produced = 0, expect = 0; // compressed bytes not yet read from the file, stream bytes produced, stream bytes announced
uint8_t input[65536];
~PackReader() {if (open) endFn(&z); if (in) fclose(in);}
bool start(const char *path) {
static void *lib = dlopen("/usr/lib/libz.1.dylib", RTLD_NOW | RTLD_LOCAL);
auto init = lib ? (InitFn)dlsym(lib, "inflateInit_") : nullptr;
inflateFn = lib ? (InflateFn)dlsym(lib, "inflate") : nullptr;
endFn = lib ? (EndFn)dlsym(lib, "inflateEnd") : nullptr;
if (!init || !inflateFn || !endFn || !(in = fopen(path, "rb"))) return false;
struct stat st = {};
uint8_t head[16];
uint32_t raw = 0, zbytes = 0;
if (fstat(fileno(in), &st) != 0 || (uint64_t)st.st_size > kPackLimit || fread(head, 1, 16, in) != 16 || memcmp(head, "SHEXP001", 8)) return false;
memcpy(&raw, head + 8, 4);
memcpy(&zbytes, head + 12, 4);
if ((uint64_t)zbytes != (uint64_t)st.st_size - 16) return false;
left = zbytes; expect = raw;
open = init(&z, ZLIB_VERSION, (int)sizeof z) == Z_OK;
return open;
}
bool read(void *out, size_t n) {
z.next_out = static_cast<Bytef *>(out);
z.avail_out = (uInt)n;
while (z.avail_out) {
if (ended) return false;
if (!z.avail_in && left) {
const size_t want = left < sizeof input ? (size_t)left : sizeof input;
if (fread(input, 1, want, in) != want) return false;
left -= want;
z.next_in = input;
z.avail_in = (uInt)want;
}
const uInt before = z.avail_out;
const int r = inflateFn(&z, Z_NO_FLUSH);
produced += before - z.avail_out;
if (r == Z_STREAM_END) ended = true;
else if (r != Z_OK) return false;
if (produced > expect) return false;
}
return true;
}
bool skip(uint64_t n) {
uint8_t scratch[16384];
while (n) {const size_t k = n < sizeof scratch ? (size_t)n : sizeof scratch; if (!read(scratch, k)) return false; n -= k;}
return true;
}
// The whole stream was consumed: zlib's check passed at its end and nothing follows it.
bool finished() {
if (!ended) {uint8_t b; if (read(&b, 1)) return false;}
return ended && produced == expect && !left && !z.avail_in;
}
};
// Import, before the device opens: the image may carry a pack beside the bundle (NativeAdrenoMetal.m2vpack, put there by
// scripts/add-gpu-plugin-data.py --pipeline-pack). Its files go into this process's own cache directory, so they carry this process's owner
// and mode; a file that is already there with the same size is kept. Files are written beside their place and renamed only once the
// whole pack has been read and checked: a damaged pack imports nothing. Returns the number of files written, or -1.
static int importPack(const char *pack, const std::string &directory) {
if (access(pack, R_OK) != 0) return 0;
PackReader r;
if (!r.start(pack)) return -1;
std::vector<std::string> written;
auto fail = [&]() {for (const std::string &f : written) unlink((f + ".import").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) ||
!r.read(&count, 4) || count > 1u << 20) return fail();
std::set<std::string> seen;
std::vector<uint8_t> chunk(65536);
for (uint32_t i = 0; i < count; ++i) {
uint32_t pathLength = 0, mode = 0, uid = 0, gid = 0;
uint64_t size = 0;
if (!r.read(&pathLength, 4) || !r.read(&mode, 4) || !r.read(&uid, 4) || !r.read(&gid, 4) || !r.read(&size, 8)) return fail();
if (pathLength == 0 || pathLength > 1024 || size > r.expect) return fail();
std::string path(pathLength, '\0');
if (!r.read(&path[0], pathLength)) return fail();
if (path[0] == '/' || path.find("..") != std::string::npos || path.find('\0') != std::string::npos) return fail();
seen.insert(bakedKey(path, size));
const std::string full = directory + "/" + path;
struct stat st = {};
if (stat(full.c_str(), &st) == 0 && (uint64_t)st.st_size == size) {if (!r.skip(size)) return fail(); continue;}
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");
if (!out) return fail();
bool ok = true;
for (uint64_t left = size; ok && left;) {
const size_t k = left < chunk.size() ? (size_t)left : chunk.size();
ok = r.read(chunk.data(), k) && fwrite(chunk.data(), 1, k, out) == k;
left -= k;
}
ok = fclose(out) == 0 && ok;
if (!ok) return fail();
}
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());
baked().insert(seen.begin(), seen.end()); // only a pack read to its end counts
return renamed;
}
#ifndef CACHEEXPORT_FORMAT_ONLY
// Markers 0x69xx: 00 started, 01 connection, 02 an export went out (then 0x6Bnn: its size in MiB, saturated), f0 no connection,
// f1 no selector, f2 a file changed or vanished while it was read, f3 a piece refused, f4 the new files exceed one export.
static void run(Gpu &marker, std::string root) {
while (root.size() > 1 && root.back() == '/') root.pop_back();
marker.mark(0x6900);
Gpu link;
link.call = marker.call; link.openFn = marker.openFn; link.closeFn = marker.closeFn; link.port = marker.port;
if (!link.openFn || link.openFn(link.port, mach_task_self(), 0x4148, &link.conn) != 0 || !link.conn) {marker.mark(0x69f0); return;}
if (!link.probe()) {marker.mark(0x69f1); return;}
marker.mark(0x6901);
uint64_t previous = 0, exported = 0;
uint32_t generation = 0;
std::vector<uint8_t> buffer(24 + kPiece);
for (;;) {
sleep(15);
std::vector<File> files;
scan(root, "", files, 0);
dropBaked(files);
const uint64_t now = signature(files);
const bool stable = now == previous;
previous = now;
if (!stable || now == exported || files.empty()) continue;
const uint64_t total = 16 + streamBytes(root, files);
if (total > kLimit) {marker.mark(0x69f4); exported = now; continue;}
++generation;
uint32_t offset = 0, filled = 0;
bool sent = true;
auto flush = [&]() {
const uint32_t header[6] = {kTunnelMagic, kMethod, generation, (uint32_t)total, offset, filled};
memcpy(buffer.data(), header, sizeof header);
sent = link.call(link.conn, link.selector, buffer.data(), sizeof header + filled, nullptr, nullptr) == 0;
offset += filled;
filled = 0;
return sent;
};
auto emit = [&](const uint8_t *p, size_t n) {
while (n) {
const size_t k = n < kPiece - filled ? n : kPiece - filled;
memcpy(buffer.data() + 24 + filled, p, k);
filled += (uint32_t)k; p += k; n -= k;
if (filled == kPiece && !flush()) return false;
}
return true;
};
uint8_t head[16] = {'S', 'H', 'E', 'X', 'R', '0', '0', '1'};
const uint32_t streamed = (uint32_t)(total - 16);
memcpy(head + 8, &streamed, 4);
const bool read = emit(head, sizeof head) && stream(root, files, emit);
if (read && filled) flush();
if (!sent) {marker.mark(0x69f3); continue;} // retried after the next change or period
if (!read || offset != total) {marker.mark(0x69f2); continue;}
exported = now;
marker.mark(0x6902);
marker.mark(0x6B00 | (uint32_t)(total >> 20 > 0xff ? 0xff : total >> 20));
}
}
#endif
} // namespace cacheexport