forked from hw-native-sys/simpler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_test_cache.py
More file actions
466 lines (401 loc) · 17.4 KB
/
Copy pathscene_test_cache.py
File metadata and controls
466 lines (401 loc) · 17.4 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# Copyright (c) PyPTO Contributors.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------
"""Persistent cache for compiled scene-test callable and incore artifacts."""
from __future__ import annotations
import contextlib
import ctypes
import fcntl
import hashlib
import json
import logging
import os
import re
import shutil
import time
import uuid
from collections.abc import Callable, Iterable, Mapping
from functools import cache
from pathlib import Path
from typing import Any
from .compile_paths import compiler_visible_path
from .environment import PROJECT_ROOT
logger = logging.getLogger(__name__)
KERNEL_CACHE_DIR = PROJECT_ROOT / "build" / "cache" / "kernels"
_CACHE_VERSION = 1
_INCORE_CACHE_VERSION = 1
_BINARY_FILE = "callable.bin"
_INCORE_DIR = "incore"
_INCORE_BINARY_FILE = "artifact.bin"
_MANIFEST_FILE = "manifest.json"
_LOCK_DIR = ".locks"
_INCLUDE_RE = re.compile(rb"^\s*#\s*include\s*([<\"])([^>\"]+)[>\"]", re.MULTILINE)
# Entries are content-addressed, so a source change strands the old entry
# forever. Every hit refreshes its mtime, so this window is time-since-last-use
# and an entry still in service is never reclaimed regardless of its age.
_ENTRY_RETENTION_S = 14 * 24 * 3600
@cache
def _chip_callable_abi_token() -> str:
"""Return a fingerprint of the binding's serialized callable layout."""
from simpler.task_interface import ArgDirection, ChipCallable, CoreCallable # noqa: PLC0415
child = CoreCallable.build(
signature=[ArgDirection.IN, ArgDirection.OUT],
binary=b"scene-test-cache-core-abi",
)
callable_obj = ChipCallable.build(
signature=[ArgDirection.INOUT],
func_name="scene_test_cache_abi",
binary=b"scene-test-cache-chip-abi",
children=[(17, child)],
config_name="scene_test_cache_config",
)
raw = ctypes.string_at(int(callable_obj.buffer_ptr()), int(callable_obj.buffer_size()))
return hashlib.sha256(raw).hexdigest()
def _stable_value(value: Any) -> Any:
if value is None or isinstance(value, (bool, int, float, str)):
return value
if isinstance(value, Path):
return str(value)
if isinstance(value, Mapping):
return {str(key): _stable_value(item) for key, item in sorted(value.items(), key=lambda pair: str(pair[0]))}
if isinstance(value, (list, tuple)):
return [_stable_value(item) for item in value]
enum_value = getattr(value, "value", None)
if isinstance(enum_value, (bool, int, float, str)):
return enum_value
raise TypeError(f"unsupported compile-cache key value: {type(value).__name__}")
def _resolve_include(name: str, quoted: bool, including_file: Path, include_dirs: tuple[Path, ...]) -> Path | None:
candidates = ((including_file.parent,) if quoted else ()) + include_dirs
for directory in candidates:
candidate = directory / name
if candidate.is_file():
return Path(os.path.abspath(candidate))
return None
_scan_cache: dict[tuple[Path, int, int], tuple[bytes, tuple[tuple[str, bool], ...]]] = {}
def _scan_source(path: Path) -> tuple[bytes, tuple[tuple[str, bool], ...]]:
"""Return one file's digest and its ``#include`` directives as ``(name, quoted)``.
Memoized on ``(path, mtime_ns, size)`` because the compilation units of one
scene test share most of their include closure — the pto-isa headers alone
are reached from every incore — and both the read and the digest are
otherwise repeated once per unit.
"""
try:
stat = path.stat()
token = (path, stat.st_mtime_ns, stat.st_size)
except OSError:
token = None
if token is not None:
memoized = _scan_cache.get(token)
if memoized is not None:
return memoized
data = path.read_bytes()
directives = []
for match in _INCLUDE_RE.finditer(data):
delimiter, raw_name = match.groups()
try:
name = raw_name.decode()
except UnicodeDecodeError:
continue
directives.append((name, delimiter == b'"'))
scanned = (hashlib.sha256(data).digest(), tuple(directives))
if token is not None:
_scan_cache[token] = scanned
return scanned
def _source_closure(
source: str | Path, include_dirs: Iterable[str | Path]
) -> tuple[Path, dict[Path, bytes], list[tuple[Path, str, Path]]]:
"""Return the root path, the digest of every file in its include closure, and the closure's edges.
Paths use the same absolute, non-realpath normalization as the compiler
invocation, so path-sensitive macros such as ``__FILE__`` and symlink
spellings are part of the key. The closure holds only includes that resolve
within ``include_dirs`` — or, for a quoted include, beside the including
file — mirroring the search order the compiler applies to the same ``-I``
list. Headers the toolchain supplies from its own search path (ccec builtins,
CANN headers under ``ASCEND_HOME_PATH``) resolve outside that list and are
absent from the key; the compiler ``--version`` identity carried in the key
metadata is what covers those. ``#if`` conditions are not evaluated, so the
closure is an over-approximation of what any single build compiles.
"""
roots = tuple(Path(os.path.abspath(directory)) for directory in include_dirs if Path(directory).is_dir())
source_path = Path(os.path.abspath(source))
pending = [source_path]
files: dict[Path, bytes] = {}
edges = []
while pending:
path = pending.pop()
if path in files:
continue
file_digest, directives = _scan_source(path)
files[path] = file_digest
for name, quoted in directives:
dependency = _resolve_include(name, quoted, path, roots)
if dependency is not None:
edges.append((path, name, dependency))
if dependency not in files:
pending.append(dependency)
return source_path, files, edges
def _compile_source_key(
metadata: Mapping[str, Any],
compilation_units: Iterable[tuple[str | Path, Iterable[str | Path]]],
*,
abi_token: str | None,
) -> str:
digest = hashlib.sha256()
if abi_token is not None:
encoded_abi_token = abi_token.encode()
digest.update(len(encoded_abi_token).to_bytes(8, "little"))
digest.update(encoded_abi_token)
encoded_metadata = json.dumps(_stable_value(metadata), sort_keys=True, separators=(",", ":")).encode()
digest.update(len(encoded_metadata).to_bytes(8, "little"))
digest.update(encoded_metadata)
for source, include_dirs in compilation_units:
normalized_include_dirs = tuple(Path(os.path.abspath(directory)) for directory in include_dirs)
source_path, file_digests, edges = _source_closure(source, normalized_include_dirs)
digest.update(b"unit")
# The complete, ordered -I list affects preprocessing even when no
# matching #include appears in the source closure (for example,
# __has_include can select different source branches).
for include_dir in normalized_include_dirs:
encoded_include_dir = os.fsencode(compiler_visible_path(include_dir))
digest.update(b"include-dir")
digest.update(len(encoded_include_dir).to_bytes(8, "little"))
digest.update(encoded_include_dir)
encoded_source_path = os.fsencode(compiler_visible_path(source_path))
digest.update(len(encoded_source_path).to_bytes(8, "little"))
digest.update(encoded_source_path)
digest.update(file_digests[source_path])
file_records = sorted(
(os.fsencode(compiler_visible_path(path)), file_digest) for path, file_digest in file_digests.items()
)
for encoded_path, file_digest in file_records:
digest.update(b"file")
digest.update(len(encoded_path).to_bytes(8, "little"))
digest.update(encoded_path)
digest.update(file_digest)
edge_records = sorted(
(
os.fsencode(compiler_visible_path(parent)),
file_digests[parent],
name.encode(),
os.fsencode(compiler_visible_path(dependency)),
file_digests[dependency],
)
for parent, name, dependency in edges
)
for parent_path, parent_digest, name, dependency_path, dependency_digest in edge_records:
digest.update(b"edge")
digest.update(len(parent_path).to_bytes(8, "little"))
digest.update(parent_path)
digest.update(parent_digest)
digest.update(len(name).to_bytes(8, "little"))
digest.update(name)
digest.update(len(dependency_path).to_bytes(8, "little"))
digest.update(dependency_path)
digest.update(dependency_digest)
return digest.hexdigest()
def compile_artifact_key(
metadata: Mapping[str, Any], compilation_units: Iterable[tuple[str | Path, Iterable[str | Path]]]
) -> str:
"""Return a content key for callable metadata and source include closures."""
return _compile_source_key(metadata, compilation_units, abi_token=_chip_callable_abi_token())
def compile_incore_artifact_key(
metadata: Mapping[str, Any], source: str | Path, include_dirs: Iterable[str | Path]
) -> str:
"""Return a content key for one independently compiled incore artifact."""
return _compile_source_key(metadata, [(source, include_dirs)], abi_token=None)
def _entry_paths(key: str) -> tuple[Path, Path, Path]:
entry = KERNEL_CACHE_DIR / key
return entry, entry / _BINARY_FILE, entry / _MANIFEST_FILE
def _incore_entry_paths(key: str) -> tuple[Path, Path, Path]:
entry = KERNEL_CACHE_DIR / _INCORE_DIR / key
return entry, entry / _INCORE_BINARY_FILE, entry / _MANIFEST_FILE
def _touch_entry(entry: Path) -> None:
with contextlib.suppress(OSError):
os.utime(entry)
def _touch(key: str) -> None:
entry, _binary_path, _manifest_path = _entry_paths(key)
_touch_entry(entry)
def _load_bytes(key: str, binary_path: Path, manifest_path: Path, version: int) -> bytes | None:
try:
manifest = json.loads(manifest_path.read_text())
raw = binary_path.read_bytes()
except (OSError, json.JSONDecodeError):
return None
if manifest != {
"version": version,
"key": key,
"size": len(raw),
"sha256": hashlib.sha256(raw).hexdigest(),
}:
return None
return raw
def _load(key: str):
_entry, binary_path, manifest_path = _entry_paths(key)
raw = _load_bytes(key, binary_path, manifest_path, _CACHE_VERSION)
if raw is None:
return None
from simpler.task_interface import ChipCallable # noqa: PLC0415
try:
callable_obj = ChipCallable.from_bytes(raw)
callable_obj.buffer_size()
except (RuntimeError, ValueError):
return None
return callable_obj
def _publish_bytes(key: str, raw: bytes, entry: Path, binary_path: Path, manifest_path: Path, version: int) -> None:
entry.mkdir(parents=True, exist_ok=True)
manifest = {
"version": version,
"key": key,
"size": len(raw),
"sha256": hashlib.sha256(raw).hexdigest(),
}
suffix = f".{os.getpid()}.{uuid.uuid4().hex}.tmp"
binary_tmp = entry / f"{binary_path.name}{suffix}"
manifest_tmp = entry / f"{_MANIFEST_FILE}{suffix}"
try:
binary_tmp.write_bytes(raw)
manifest_tmp.write_text(json.dumps(manifest, sort_keys=True) + "\n")
os.replace(binary_tmp, binary_path)
os.replace(manifest_tmp, manifest_path)
finally:
binary_tmp.unlink(missing_ok=True)
manifest_tmp.unlink(missing_ok=True)
def _publish(key: str, callable_obj) -> None:
entry, binary_path, manifest_path = _entry_paths(key)
raw = ctypes.string_at(int(callable_obj.buffer_ptr()), int(callable_obj.buffer_size()))
_publish_bytes(key, raw, entry, binary_path, manifest_path, _CACHE_VERSION)
_pruned_dirs: set[Path] = set()
_disabled_dirs: set[Path] = set()
def prune_stale_entries() -> int:
"""Delete cache entries and lock files untouched for ``_ENTRY_RETENTION_S``.
Runs at most once per cache directory per process, after a publish has
already proved the directory writable. Returns the number of entries removed.
"""
if KERNEL_CACHE_DIR in _pruned_dirs:
return 0
_pruned_dirs.add(KERNEL_CACHE_DIR)
cutoff = time.time() - _ENTRY_RETENTION_S
removed = 0
try:
root_entries = list(KERNEL_CACHE_DIR.iterdir())
except OSError:
return 0
entries = [entry for entry in root_entries if entry.name not in {_LOCK_DIR, _INCORE_DIR}]
with contextlib.suppress(OSError):
entries.extend((KERNEL_CACHE_DIR / _INCORE_DIR).iterdir())
for entry in entries:
try:
if entry.stat().st_mtime >= cutoff:
continue
except OSError:
continue
if entry.is_dir():
shutil.rmtree(entry, ignore_errors=True)
else:
with contextlib.suppress(OSError):
entry.unlink()
removed += 1
for lock_path in (KERNEL_CACHE_DIR / _LOCK_DIR).glob("*.lock"):
with contextlib.suppress(OSError):
if lock_path.stat().st_mtime < cutoff:
lock_path.unlink()
if removed:
logger.info("[SceneTestCache] pruned %d stale entr(ies) from %s", removed, KERNEL_CACHE_DIR)
return removed
@contextlib.contextmanager
def _entry_lock(key: str):
"""Hold an exclusive lock for ``key``, or yield ``None`` when the cache directory is unusable.
A read-only install (a wheel whose ``PROJECT_ROOT`` is inside
``site-packages``) or a filesystem without ``flock`` yields ``None``, which
degrades the caller to plain compilation instead of failing a scene test
that used to pass.
"""
if KERNEL_CACHE_DIR in _disabled_dirs:
yield None
return
lock_file = None
try:
lock_dir = KERNEL_CACHE_DIR / _LOCK_DIR
lock_dir.mkdir(parents=True, exist_ok=True)
lock_file = (lock_dir / f"{key}.lock").open("w")
fcntl.flock(lock_file, fcntl.LOCK_EX)
except OSError as error:
if lock_file is not None:
lock_file.close()
lock_file = None
_disabled_dirs.add(KERNEL_CACHE_DIR)
logger.warning(
"[SceneTestCache] disabled: %s is not usable (%s: %s); compiling without a cache",
KERNEL_CACHE_DIR,
type(error).__name__,
error,
)
try:
yield lock_file
finally:
if lock_file is not None:
lock_file.close()
def get_or_compile(key: str, compile_fn: Callable[[], Any]):
"""Load one ``ChipCallable`` or compile and atomically publish it."""
cached = _load(key)
if cached is not None:
_touch(key)
logger.info("[SceneTestCache] hit: %s", key[:12])
return cached
with _entry_lock(key) as lock_file:
if lock_file is None:
return compile_fn()
cached = _load(key)
if cached is not None:
_touch(key)
logger.info("[SceneTestCache] hit after wait: %s", key[:12])
return cached
logger.info("[SceneTestCache] miss: %s", key[:12])
callable_obj = compile_fn()
try:
_publish(key, callable_obj)
except OSError as error:
logger.warning(
"[SceneTestCache] publish failed for %s (%s: %s); artifact not cached",
key[:12],
type(error).__name__,
error,
)
else:
prune_stale_entries()
return callable_obj
def get_or_compile_incore(key: str, compile_fn: Callable[[], bytes]) -> bytes:
"""Load one incore binary or compile and atomically publish it."""
entry, binary_path, manifest_path = _incore_entry_paths(key)
cached = _load_bytes(key, binary_path, manifest_path, _INCORE_CACHE_VERSION)
if cached is not None:
_touch_entry(entry)
logger.info("[SceneTestCache] incore hit: %s", key[:12])
return cached
with _entry_lock(f"incore-{key}") as lock_file:
if lock_file is None:
return compile_fn()
cached = _load_bytes(key, binary_path, manifest_path, _INCORE_CACHE_VERSION)
if cached is not None:
_touch_entry(entry)
logger.info("[SceneTestCache] incore hit after wait: %s", key[:12])
return cached
logger.info("[SceneTestCache] incore miss: %s", key[:12])
binary = compile_fn()
try:
_publish_bytes(key, binary, entry, binary_path, manifest_path, _INCORE_CACHE_VERSION)
except OSError as error:
logger.warning(
"[SceneTestCache] incore publish failed for %s (%s: %s); artifact not cached",
key[:12],
type(error).__name__,
error,
)
else:
prune_stale_entries()
return binary