|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import sqlite3 |
| 6 | +import time |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from cuda.tile._cache import cache_key, cache_lookup, cache_store, evict_lru |
| 11 | + |
| 12 | + |
| 13 | +def test_cache_key_equal(): |
| 14 | + k1 = cache_key("v1", "sm_90", 3, b"data") |
| 15 | + k2 = cache_key("v1", "sm_90", 3, b"data") |
| 16 | + assert k1 == k2 |
| 17 | + |
| 18 | + |
| 19 | +def test_cache_key_differs(): |
| 20 | + base = cache_key("v1", "sm_90", 3, b"data") |
| 21 | + assert cache_key("v2", "sm_90", 3, b"data") != base |
| 22 | + assert cache_key("v1", "sm_80", 3, b"data") != base |
| 23 | + assert cache_key("v1", "sm_90", 2, b"data") != base |
| 24 | + assert cache_key("v1", "sm_90", 3, b"other") != base |
| 25 | + |
| 26 | + |
| 27 | +def make_cubin(tmp_path, name, content): |
| 28 | + src = tmp_path / name |
| 29 | + src.write_bytes(content) |
| 30 | + return src |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def cache_env(tmp_path): |
| 35 | + cache_dir = str(tmp_path / "cache") |
| 36 | + return cache_dir, tmp_path |
| 37 | + |
| 38 | + |
| 39 | +def test_store_then_lookup(cache_env): |
| 40 | + cache_dir, tmp_path = cache_env |
| 41 | + key = cache_key("v1", "sm_90", 3, b"data") |
| 42 | + content = b"\x7fELF_fake_cubin_data" |
| 43 | + |
| 44 | + cache_store(cache_dir, key, make_cubin(tmp_path, "kernel.cubin", content)) |
| 45 | + |
| 46 | + result = cache_lookup(cache_dir, key, str(tmp_path)) |
| 47 | + assert result is not None |
| 48 | + assert result.read_bytes() == content |
| 49 | + |
| 50 | + |
| 51 | +def test_lookup_updates_atime(cache_env): |
| 52 | + cache_dir, tmp_path = cache_env |
| 53 | + key = cache_key("v1", "sm_90", 3, b"data") |
| 54 | + |
| 55 | + cache_store(cache_dir, key, make_cubin(tmp_path, "kernel.cubin", b"data")) |
| 56 | + |
| 57 | + # Manually set old atime in DB |
| 58 | + import os |
| 59 | + db_path = os.path.join(cache_dir, "cache.db") |
| 60 | + old_time = time.time() - 1000 |
| 61 | + conn = sqlite3.connect(db_path) |
| 62 | + conn.execute("UPDATE cache SET atime = ? WHERE key = ?", (old_time, key)) |
| 63 | + conn.commit() |
| 64 | + conn.close() |
| 65 | + |
| 66 | + cache_lookup(cache_dir, key, str(tmp_path)) |
| 67 | + |
| 68 | + conn = sqlite3.connect(db_path) |
| 69 | + atime = conn.execute( |
| 70 | + "SELECT atime FROM cache WHERE key = ?", (key,) |
| 71 | + ).fetchone()[0] |
| 72 | + conn.close() |
| 73 | + assert atime > old_time |
| 74 | + |
| 75 | + |
| 76 | +def test_lookup_miss(cache_env): |
| 77 | + cache_dir, _ = cache_env |
| 78 | + |
| 79 | + result = cache_lookup(cache_dir, "a" * 64, str(cache_dir)) |
| 80 | + assert result is None |
| 81 | + |
| 82 | + |
| 83 | +def test_evict_lru(cache_env): |
| 84 | + cache_dir, tmp_path = cache_env |
| 85 | + import os |
| 86 | + db_path = os.path.join(cache_dir, "cache.db") |
| 87 | + |
| 88 | + # Populate 5 entries (1000 bytes each, 5000 total) |
| 89 | + keys = [] |
| 90 | + for i in range(5): |
| 91 | + key = cache_key(str(i), "sm_90", 3, b"data") |
| 92 | + keys.append(key) |
| 93 | + cache_store(cache_dir, key, |
| 94 | + make_cubin(tmp_path, f"k{i}.cubin", b"x" * 1000)) |
| 95 | + |
| 96 | + # Set controlled atimes so eviction order is deterministic |
| 97 | + conn = sqlite3.connect(db_path) |
| 98 | + for i, key in enumerate(keys): |
| 99 | + conn.execute( |
| 100 | + "UPDATE cache SET atime = ? WHERE key = ?", |
| 101 | + (float(i), key) |
| 102 | + ) |
| 103 | + conn.commit() |
| 104 | + conn.close() |
| 105 | + |
| 106 | + # Evict to keep 3000 bytes; newest 3 survive (indices 2, 3, 4) |
| 107 | + evict_lru(cache_dir, 3000) |
| 108 | + |
| 109 | + remaining = [k for k in keys |
| 110 | + if cache_lookup(cache_dir, k, str(tmp_path)) is not None] |
| 111 | + assert remaining == keys[2:] |
0 commit comments