Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,8 @@ without `-Render` is ignored with a warning). The watcher waits 2 seconds betwee
the watcher started in newest-first order ahead of the older backlog. It processes batches of up to
four keys per configured worker and rescans between batches at most once per second, so a pipeline
created during the run is translated within seconds and the runtime picks it up on a later draw
instead of on the next run. The runtime
instead of on the next run. `compile_dxil --watch` uses the same ready-marker priority, batch size
and rescan cadence. The runtime
looks for an absent manifest again 1 second after the first miss and doubles the wait with each
further miss up to 32 seconds. A translation published during the run is therefore
used on a draw or Dispatch at most 32 seconds later, and a key that never gets a translation costs one
Expand Down
29 changes: 29 additions & 0 deletions tests/native_compile_dxil_watch_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,35 @@ def main(argv: list[str]) -> int:
assert rs.stdout == rp.stdout, (rs.stdout, rp.stdout)
assert G.pending_keys(str(d_parallel), skip_failed=True) == []

# A fresh marker must jump ahead of a backlog larger than the four-key batch.
# Making every marker old reproduces the legacy path order with the same inputs.
backlog = list(range(1, 6))
watch_orders = {}
watch_outputs = {}
now = time.time()
for label, fresh_mtime in (("legacy", now - 60), ("fresh", now + 3600)):
order_dir = work / f"order_{label}"
order_dir.mkdir()
for key in backlog:
touch(order_dir / f"req_{key:016x}.c67", b"missing request sidecars", now - 60)
fresh_key = put_compute_request(order_dir, c_code, c_root, c_sa)
assert fresh_key > backlog[-1], f"fixture must put fresh key last in path order: {fresh_key:016x}"
touch(order_dir / f"req_{fresh_key:016x}.c67", mtime=fresh_mtime)
result = run_once(cli, sdk, frontend, order_dir)
assert result.returncode == 0 and not result.stderr, (result.stdout, result.stderr)
assert f"{fresh_key:016x}: compiled compute" in result.stdout, result.stdout
watch_orders[label] = [int(line.split(":", 1)[0], 16) for line in result.stdout.splitlines()]
watch_outputs[label] = {
path.name: path.read_bytes() for path in order_dir.iterdir() if not path.name.startswith("req_")
}
assert set(watch_outputs[label]) == {
*(f"{key:016x}.fail" for key in backlog), f"{fresh_key:016x}.txt", f"{fresh_key:016x}.cs.cso"
}
assert watch_orders["legacy"] == [*backlog, fresh_key], watch_orders
assert fresh_key not in watch_orders["legacy"][:4], watch_orders
assert watch_orders["fresh"] == [fresh_key, *backlog], watch_orders
assert watch_outputs["fresh"] == watch_outputs["legacy"], "watch order changed output bytes"

# RTV variants in --once: compile base + variants, publish rtv1 manifest + .psdep, and recheck on base .psin update.
d_rtv = work / "once_rtv"
d_rtv.mkdir()
Expand Down
75 changes: 53 additions & 22 deletions tools/startup/compile_dxil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ struct WatchRequest {
std::wstring key;
fs::path marker;
bool compute = false;
fs::file_time_type marker_mtime{};
};

static bool read_small_file(const fs::path& path, size_t max_bytes, std::string& out) {
Expand All @@ -726,11 +727,12 @@ static bool read_small_file(const fs::path& path, size_t max_bytes, std::string&
return !size || static_cast<bool>(file.read(out.data(), size));
}

static bool is_watch_request_pending(const fs::path& request_dir, const WatchRequest& req, bool skip_failed) {
static bool is_watch_request_pending(const fs::path& request_dir, WatchRequest& req, bool skip_failed) {
std::error_code ec;
const auto marker_mtime = fs::last_write_time(req.marker, ec);
if (ec)
return false;
req.marker_mtime = marker_mtime;
const fs::path stem = request_dir / req.key;
const fs::path manifest_path = fs::path(stem) += L".txt";
const fs::path fail_path = fs::path(stem) += L".fail";
Expand Down Expand Up @@ -806,7 +808,9 @@ static bool is_watch_request_pending(const fs::path& request_dir, const WatchReq
return true;
}

static std::vector<WatchRequest> collect_watch_requests(const fs::path& request_dir, bool skip_failed) {
static std::vector<WatchRequest> collect_watch_requests(const fs::path& request_dir, bool skip_failed,
fs::file_time_type watch_start,
const std::set<std::wstring>& attempted) {
std::vector<WatchRequest> found;
for (const auto& file : fs::directory_iterator(request_dir)) {
std::error_code ec;
Expand All @@ -821,7 +825,7 @@ static std::vector<WatchRequest> collect_watch_requests(const fs::path& request_
if (stem.size() != 20 || stem.rfind(L"req_", 0) != 0)
continue;
const auto key = stem.substr(4);
if (!is_lower_hex16(key))
if (!is_lower_hex16(key) || attempted.count(key))
continue;
found.push_back({key, fs::weakly_canonical(path), is_compute});
}
Expand All @@ -840,6 +844,12 @@ static std::vector<WatchRequest> collect_watch_requests(const fs::path& request_
if (is_watch_request_pending(request_dir, req, skip_failed))
pending.push_back(std::move(req));
}
const auto recent_end = std::stable_partition(pending.begin(), pending.end(), [watch_start](const WatchRequest& req) {
return req.marker_mtime > watch_start;
});
std::stable_sort(pending.begin(), recent_end, [](const WatchRequest& a, const WatchRequest& b) {
return a.marker_mtime > b.marker_mtime;
});
return pending;
}

Expand Down Expand Up @@ -892,6 +902,7 @@ static const char* compile_watch_item(x2w::ShaderDxilCompiler& compiler, const f
}

static int run_watch(int argc, wchar_t** argv) {
const auto watch_start = fs::file_time_type::clock::now();
fs::path sdk_dir, cache_dir, request_dir;
int positional = 0;
int jobs = 1;
Expand Down Expand Up @@ -941,21 +952,40 @@ static int run_watch(int argc, wchar_t** argv) {

x2w::ShaderDxilCompiler coordinator(sdk_dir);
bool skip_failed = false;
const auto batch_size = static_cast<size_t>(jobs) * 4;
while (true) {
std::set<std::wstring> attempted;
std::vector<WatchRequest> pending;
try {
pending = collect_watch_requests(request_dir, skip_failed);
} catch (const fs::filesystem_error& error) {
if (once)
throw;
std::cerr << "watch scan error, retrying: " << error.what() << '\n';
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
continue;
}
if (!pending.empty()) {
std::vector<std::string> outcomes(pending.size());
size_t offset = 0;
bool scan_failed = false;
std::optional<Clock::time_point> last_scan;
while (true) {
const auto now = Clock::now();
if (!last_scan || now - *last_scan >= std::chrono::seconds(1)) {
last_scan = now;
try {
pending = collect_watch_requests(request_dir, skip_failed, watch_start, attempted);
} catch (const fs::filesystem_error& error) {
if (once)
throw;
std::cerr << "watch scan error, retrying: " << error.what() << '\n';
scan_failed = true;
break;
}
offset = 0;
}
std::vector<const WatchRequest*> batch;
// The first pass can retry existing failures, including after a rescan.
while (offset < pending.size() && batch.size() < batch_size) {
const auto& req = pending[offset++];
attempted.insert(req.key);
batch.push_back(&req);
}
if (batch.empty())
break;
std::vector<std::string> outcomes(batch.size());
auto process_item = [&](x2w::ShaderDxilCompiler& compiler, size_t idx) {
const auto& req = pending[idx];
const auto& req = *batch[idx];
try {
outcomes[idx] = compile_watch_item(compiler, cache_dir, request_dir, req);
} catch (const std::exception& error) {
Expand All @@ -970,9 +1000,9 @@ static int run_watch(int argc, wchar_t** argv) {
outcomes[idx] = "refused: " + summary;
}
};
const int worker_count = (std::min)(jobs, static_cast<int>(pending.size()));
const int worker_count = (std::min)(jobs, static_cast<int>(batch.size()));
if (worker_count <= 1) {
for (size_t idx = 0; idx < pending.size(); ++idx)
for (size_t idx = 0; idx < batch.size(); ++idx)
process_item(coordinator, idx);
} else {
std::atomic<size_t> next{};
Expand All @@ -981,25 +1011,26 @@ static int run_watch(int argc, wchar_t** argv) {
for (int w = 0; w < worker_count; ++w) {
workers.emplace_back(std::async(std::launch::async, [&]() {
auto compiler = coordinator.new_worker();
for (size_t idx = next++; idx < pending.size(); idx = next++)
for (size_t idx = next++; idx < batch.size(); idx = next++)
process_item(compiler, idx);
}));
}
for (auto& w : workers)
w.get();
}
for (size_t idx = 0; idx < pending.size(); ++idx) {
for (size_t idx = 0; idx < batch.size(); ++idx) {
std::string key_ascii;
key_ascii.reserve(pending[idx].key.size());
for (wchar_t ch : pending[idx].key)
key_ascii.reserve(batch[idx]->key.size());
for (wchar_t ch : batch[idx]->key)
key_ascii.push_back(static_cast<char>(ch));
std::cout << key_ascii << ": " << outcomes[idx] << '\n';
}
std::cout.flush();
}
if (once)
return 0;
skip_failed = true;
if (!scan_failed)
skip_failed = true;
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
}
} catch (const std::exception& error) {
Expand Down
Loading