A read-ahead worker: takes a predicted (layer, expert), skips it when its pages are cached or it is already staged, and otherwise reads the whole blob into a staging buffer (copy_from_files preads what is not cached).
| 1365 | // A read-ahead worker: takes a predicted (layer, expert), skips it when its pages are cached or it is already |
| 1366 | // staged, and otherwise reads the whole blob into a staging buffer (copy_from_files preads what is not cached). |
| 1367 | void FileExpertSource::io_worker() { |
| 1368 | strata::platform::name_this_thread("strata-io"); |
| 1369 | for (;;) { |
| 1370 | int32_t layer, expert; |
| 1371 | bool ahead = true; // a prediction (marked for the used / unused count), not a layer's own demand read |
| 1372 | { |
| 1373 | std::unique_lock<std::mutex> lk(io_mu_); |
| 1374 | io_cv_.wait(lk, [&] { return io_quit_ || !io_q_.empty() || !io_jobs_.empty(); }); |
| 1375 | if (io_quit_) return; |
| 1376 | if (io_sched_ && io_q_.empty() && !io_jobs_.empty()) { // (the layers' own reads in io_q_ go first) |
| 1377 | // 149-prefetch: drop the jobs whose layer the decode has reached (its demand read covers them); then the |
| 1378 | // earliest deadline, the surest guess first among equals |
| 1379 | const uint64_t now = io_epoch_.load(std::memory_order_relaxed); |
| 1380 | size_t best = io_jobs_.size(); |
| 1381 | for (size_t i = 0; i < io_jobs_.size();) { |
| 1382 | if (io_jobs_[i].deadline <= now) { |
| 1383 | io_q_bytes_ -= std::min(io_q_bytes_, io_jobs_[i].bytes); |
| 1384 | io_jobs_[i] = io_jobs_.back(); |
| 1385 | io_jobs_.pop_back(); |
| 1386 | io_pf_late_.fetch_add(1, std::memory_order_relaxed); |
| 1387 | continue; |
| 1388 | } |
| 1389 | ++i; |
| 1390 | } |
| 1391 | best = io_jobs_.size(); |
| 1392 | for (size_t i = 0; i < io_jobs_.size(); ++i) |
| 1393 | if (best == io_jobs_.size() || io_jobs_[i].deadline < io_jobs_[best].deadline || |
| 1394 | (io_jobs_[i].deadline == io_jobs_[best].deadline && io_jobs_[i].conf > io_jobs_[best].conf)) |
| 1395 | best = i; |
| 1396 | if (best == io_jobs_.size()) continue; // all were late |
| 1397 | layer = io_jobs_[best].layer; |
| 1398 | expert = io_jobs_[best].expert; |
| 1399 | io_q_bytes_ -= std::min(io_q_bytes_, io_jobs_[best].bytes); |
| 1400 | io_jobs_[best] = io_jobs_.back(); |
| 1401 | io_jobs_.pop_back(); |
| 1402 | } else { |
| 1403 | if (io_q_.empty()) continue; |
| 1404 | layer = io_q_.back().first; // the newest prediction first: it is the layer that comes next |
| 1405 | expert = io_q_.back().second & ~kIoAhead; |
| 1406 | ahead = (io_q_.back().second & kIoAhead) != 0; |
| 1407 | io_q_.pop_back(); |
| 1408 | } |
| 1409 | } |
| 1410 | io_pf_jobs_.fetch_add(1, std::memory_order_relaxed); |
| 1411 | const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert; |
| 1412 | if (complement_ready_ && resident_blob(index) != nullptr) continue; |
| 1413 | if (!override_.empty() && override_[index] != nullptr) continue; |
| 1414 | const auto tjob = std::chrono::steady_clock::now(); |
| 1415 | // 149-prefetch: one worker's read rate (bytes per microsecond), for the scheduler's window budget |
| 1416 | auto note_rate = [&](uint64_t b) { |
| 1417 | if (b == 0) return; |
| 1418 | const double us = std::max(1.0, std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - tjob).count()); |
| 1419 | io_pf_bytes_.fetch_add(b, std::memory_order_relaxed); |
| 1420 | std::lock_guard<std::mutex> lk(io_mu_); |
| 1421 | const double r = (double) b / us; |
| 1422 | io_bw_ = io_bw_ <= 0.0 ? r : 0.9 * io_bw_ + 0.1 * r; |
| 1423 | }; |
| 1424 | if (io_fill_) { // read what is not cached into a scratch buffer: the pages stay in the page cache for the kernels |
nothing calls this directly
no test coverage detected