Native VBS and CMake builds of the server and every registered suite: protocol and document sync, VBS/compdb providers, stock GCC diagnostics (including in-process default profile preparation), the stock occurrence observer and hover/definition, the bounded Scheduler, C++ module graph, scan and artifact cache with real CMI production, the context-aware symbol index with schema-2 snapshot migration, navigation and guarded rename, reflection provenance with splice-origin extraction, index-backed completion and signature help, live GCC worker control with a registered DAP suite, and the Neovim and VS Code clients. VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced from the versioned patches and verified stepping reflection/template code with projected values 17/34 and 41/82.
296 lines
10 KiB
C++
296 lines
10 KiB
C++
#include <vls/scheduler/Scheduler.hpp>
|
|
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace vls::scheduler;
|
|
using namespace std::chrono_literals;
|
|
using Void::Testing::expect;
|
|
using Void::Testing::require;
|
|
|
|
constexpr Budget small{
|
|
.foreground = 1, .background = 1, .artifact = 1, .queued = 8, .memory_mib = 64, .watchdog = 5s};
|
|
|
|
struct Gate {
|
|
std::mutex mutex;
|
|
std::condition_variable changed;
|
|
bool open{};
|
|
void wait() {
|
|
std::unique_lock lock{mutex};
|
|
changed.wait(lock, [&] { return open; });
|
|
}
|
|
void release() {
|
|
std::lock_guard lock{mutex};
|
|
open = true;
|
|
changed.notify_all();
|
|
}
|
|
};
|
|
|
|
struct Terminal {
|
|
mutable std::mutex mutex;
|
|
std::condition_variable changed;
|
|
std::vector<Result> results;
|
|
|
|
void add(Result result) {
|
|
std::lock_guard lock{mutex};
|
|
results.push_back(std::move(result));
|
|
changed.notify_all();
|
|
}
|
|
std::optional<Result> wait(std::string_view key, std::uint64_t revision,
|
|
std::chrono::milliseconds timeout = 5000ms) {
|
|
std::unique_lock lock{mutex};
|
|
changed.wait_for(lock, timeout, [&] {
|
|
for (const auto &result : results)
|
|
if (result.key == key && result.revision == revision)
|
|
return true;
|
|
return false;
|
|
});
|
|
for (const auto &result : results)
|
|
if (result.key == key && result.revision == revision)
|
|
return result;
|
|
return std::nullopt;
|
|
}
|
|
std::size_t size() const {
|
|
std::lock_guard lock{mutex};
|
|
return results.size();
|
|
}
|
|
};
|
|
|
|
// Cooperative work that returns only when the token is stopped.
|
|
Work stopped_work(std::atomic<bool> &started, std::string detail = "stopped") {
|
|
return [&started, detail](std::stop_token token) {
|
|
started = true;
|
|
std::mutex mutex;
|
|
std::condition_variable changed;
|
|
bool stopped{};
|
|
std::stop_callback callback{token, [&] {
|
|
std::lock_guard lock{mutex};
|
|
stopped = true;
|
|
changed.notify_all();
|
|
}};
|
|
std::unique_lock lock{mutex};
|
|
changed.wait(lock, [&] { return stopped; });
|
|
return detail;
|
|
};
|
|
}
|
|
|
|
template <class Predicate>
|
|
bool eventually(Predicate predicate, std::chrono::milliseconds timeout = 5000ms) {
|
|
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
|
while (std::chrono::steady_clock::now() < deadline) {
|
|
if (predicate())
|
|
return true;
|
|
std::this_thread::sleep_for(1ms);
|
|
}
|
|
return predicate();
|
|
}
|
|
} // namespace
|
|
|
|
VOID_TEST(Scheduler, runs_work_and_reports_metrics) {
|
|
Scheduler scheduler{small};
|
|
Terminal terminal;
|
|
std::atomic<int> ran{};
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "a", 1, 1},
|
|
[&](std::stop_token) {
|
|
++ran;
|
|
return std::string{"ok"};
|
|
},
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
scheduler.drain();
|
|
const auto result = terminal.wait("a", 1);
|
|
require(result.has_value());
|
|
expect(result->outcome == Outcome::completed && result->detail == "ok");
|
|
const auto metrics = scheduler.metrics();
|
|
expect(metrics.accepted == 1 && metrics.completed == 1 && metrics.rejected == 0);
|
|
expect(ran == 1);
|
|
}
|
|
|
|
VOID_TEST(Scheduler, foreground_is_reserved_while_background_is_saturated) {
|
|
Scheduler scheduler{small};
|
|
Gate gate;
|
|
Terminal terminal;
|
|
std::atomic<bool> background_started{};
|
|
require(scheduler.submit(
|
|
{Priority::background, "background", 1, 0},
|
|
[&](std::stop_token) {
|
|
background_started = true;
|
|
gate.wait();
|
|
return std::string{"background"};
|
|
},
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
require(eventually([&] { return background_started.load(); }));
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "foreground", 1, 0},
|
|
[](std::stop_token) { return std::string{"foreground"}; },
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
const auto foreground = terminal.wait("foreground", 1);
|
|
require(foreground.has_value());
|
|
expect(foreground->outcome == Outcome::completed);
|
|
expect(!terminal.wait("background", 1, 10ms).has_value());
|
|
gate.release();
|
|
scheduler.drain();
|
|
require(terminal.wait("background", 1).has_value());
|
|
}
|
|
|
|
VOID_TEST(Scheduler, coalesces_superseded_revisions) {
|
|
Scheduler scheduler{small};
|
|
Gate gate;
|
|
Terminal terminal;
|
|
std::atomic<bool> blocker_started{};
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "blocker", 1, 0},
|
|
[&](std::stop_token) {
|
|
blocker_started = true;
|
|
gate.wait();
|
|
return std::string{};
|
|
},
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
require(eventually([&] { return blocker_started.load(); }));
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "document", 1, 0},
|
|
[](std::stop_token) { return std::string{"old"}; },
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "document", 2, 0},
|
|
[](std::stop_token) { return std::string{"new"}; },
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
const auto superseded = terminal.wait("document", 1);
|
|
require(superseded.has_value());
|
|
expect(superseded->outcome == Outcome::cancelled && superseded->detail == "superseded");
|
|
gate.release();
|
|
scheduler.drain();
|
|
const auto current = terminal.wait("document", 2);
|
|
require(current.has_value());
|
|
expect(current->outcome == Outcome::completed && current->detail == "new");
|
|
}
|
|
|
|
VOID_TEST(Scheduler, running_cancellation_is_cooperative) {
|
|
Scheduler scheduler{small};
|
|
Terminal terminal;
|
|
std::atomic<bool> started{};
|
|
require(scheduler.submit({Priority::foreground, "running", 1, 0}, stopped_work(started),
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
require(eventually([&] { return started.load(); }));
|
|
scheduler.cancel("running");
|
|
const auto result = terminal.wait("running", 1);
|
|
require(result.has_value());
|
|
expect(result->outcome == Outcome::cancelled);
|
|
}
|
|
|
|
VOID_TEST(Scheduler, watchdog_times_out_a_stalled_task) {
|
|
Budget budget = small;
|
|
budget.watchdog = 50ms;
|
|
Scheduler scheduler{budget};
|
|
Terminal terminal;
|
|
std::atomic<bool> started{};
|
|
require(scheduler.submit({Priority::foreground, "stalled", 1, 0}, stopped_work(started),
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
require(eventually([&] { return started.load(); }));
|
|
const auto result = terminal.wait("stalled", 1);
|
|
require(result.has_value());
|
|
expect(result->outcome == Outcome::timed_out);
|
|
}
|
|
|
|
VOID_TEST(Scheduler, refuses_overflow_and_oversized_estimates) {
|
|
Budget budget = small;
|
|
budget.queued = 1;
|
|
budget.memory_mib = 4;
|
|
Scheduler scheduler{budget};
|
|
Gate gate;
|
|
Terminal terminal;
|
|
std::atomic<bool> blocker_started{};
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "blocker", 1, 0},
|
|
[&](std::stop_token) {
|
|
blocker_started = true;
|
|
gate.wait();
|
|
return std::string{};
|
|
},
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
require(eventually([&] { return blocker_started.load(); }));
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "queued", 1, 0}, [](std::stop_token) { return std::string{}; },
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
expect(!scheduler.submit(
|
|
{Priority::foreground, "overflow", 1, 0}, [](std::stop_token) { return std::string{}; },
|
|
[&](Result) {}));
|
|
expect(!scheduler.submit(
|
|
{Priority::foreground, "huge", 1, 8}, [](std::stop_token) { return std::string{}; },
|
|
[&](Result) {}));
|
|
expect(scheduler.metrics().rejected == 1);
|
|
gate.release();
|
|
scheduler.drain();
|
|
}
|
|
|
|
VOID_TEST(Scheduler, memory_budget_serializes_heavy_tasks) {
|
|
Budget budget = small;
|
|
budget.foreground = 2;
|
|
budget.memory_mib = 4;
|
|
Scheduler scheduler{budget};
|
|
Gate gate;
|
|
Terminal terminal;
|
|
std::atomic<bool> first_started{}, second_started{};
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "first", 1, 4},
|
|
[&](std::stop_token) {
|
|
first_started = true;
|
|
gate.wait();
|
|
return std::string{};
|
|
},
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
require(eventually([&] { return first_started.load(); }));
|
|
require(scheduler.submit(
|
|
{Priority::foreground, "second", 1, 4},
|
|
[&](std::stop_token) {
|
|
second_started = true;
|
|
return std::string{};
|
|
},
|
|
[&](Result result) { terminal.add(std::move(result)); }));
|
|
std::this_thread::sleep_for(20ms);
|
|
expect(!second_started.load());
|
|
gate.release();
|
|
scheduler.drain();
|
|
expect(second_started.load());
|
|
}
|
|
|
|
VOID_TEST(Scheduler, every_accepted_submission_has_one_terminal_result) {
|
|
Scheduler scheduler{small};
|
|
Terminal terminal;
|
|
std::size_t accepted{};
|
|
for (std::uint64_t revision = 1; revision <= 100; ++revision)
|
|
accepted +=
|
|
scheduler.submit(
|
|
{Priority::foreground, "document", revision, 0},
|
|
[](std::stop_token token) {
|
|
std::this_thread::sleep_for(1ms);
|
|
return token.stop_requested() ? std::string{"stopped"} : std::string{"done"};
|
|
},
|
|
[&](Result result) { terminal.add(std::move(result)); })
|
|
? 1
|
|
: 0;
|
|
scheduler.drain();
|
|
expect(terminal.size() == accepted);
|
|
}
|
|
|
|
VOID_TEST(Scheduler, rejects_invalid_budgets) {
|
|
bool threw{};
|
|
try {
|
|
Scheduler scheduler{Budget{.foreground = 0}};
|
|
} catch (const std::invalid_argument &) {
|
|
threw = true;
|
|
}
|
|
expect(threw);
|
|
}
|