radiance 1.7.0

- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves
  every shipped container
- Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime
- /v1/messages works with Claude Code as it ships (gearwave00001)
- a long agent conversation keeps its cached history when the client rewrites its last turn
  (manueloverride)
- a stream's first event goes out with the first token, so llama-benchy measures prefill right
- opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42)
- four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b)
- NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA
  adapters (baKon)
- many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool
  arguments and the CPU's vision and audio encoders
- plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
Deadcode committed 2026-10-16 18:25:18 +02:00
1 parent b068190326
commit 1e968f6e1d
739 files changed
+128447 -84632

No files matched your search

+37 -44
View File
@@ -1,15 +1,12 @@
/* checks.cpp -- what in the configuration or on the machine costs speed or correctness, and what to
* do about it.
*
* EACH CHECK IS A FUNCTION OF THE FACTS AND NOTHING ELSE: no file is read here and no device is
* asked, so every one of them is tested by handing it a machine described in a struct. A check
* says what it saw with the numbers, why that matters, and the change that addresses it -- a
* warning without a fix is a worry handed to the reader.
* Each check is a pure function of the facts (no file or device access), so tests pass a machine
* described in a struct. A finding states what was seen with numbers, why it matters, and a fix.
*
* WARN is a cost or a risk the operator should act on; NOTE is a setting that is a trade-off or
* worth knowing when reading a benchmark. A finding that depends on load (a parked link) is only
* raised when the card was busy when the facts were read: at startup the cards are idle and a
* PCIe link at rest parks at its lowest speed, which is the power management doing its job.
* Warn: a cost or risk to act on. Note: a trade-off, or worth knowing when reading a benchmark.
* Load-dependent findings (a parked link) are raised only when the card was busy, since an idle
* PCIe link parks at its lowest speed by design.
*/
#include "diag/diag.h"
#include "rad_internal.h"
@@ -26,9 +23,6 @@ namespace {
constexpr int64_t kGiB = (int64_t)1 << 30;
constexpr int64_t kMiB = (int64_t)1 << 20;
/* A card this busy when its link was read is under load, so a link below its maximum is not the
* idle parking every PCIe link does. */
constexpr int kBusyPercent = 30;
void add(std::vector<Finding>* out, Finding::Level lv, const char* id, std::string what,
std::string why, std::string fix) {
@@ -50,8 +44,8 @@ std::vector<const CardFacts*> served(const Facts& f) {
return out;
}
/* WHETHER THE SERVING CARDS' DMA IS TRANSLATED, from their own IOMMU groups where the kernel
* says, and from the machine's mode where it does not. */
/* Whether the serving cards' DMA is translated: from their IOMMU groups where known, else the
* machine's mode. */
bool translated(const Facts& f) {
bool any = false, dma = false;
for (const CardFacts* c : served(f)) {
@@ -62,8 +56,8 @@ bool translated(const Facts& f) {
return any ? dma : f.host.iommu == "translated";
}
/* A GPU the engine could serve on: an amdgpu device with VRAM of its own worth the name. An
* integrated GPU's carve-out of a few hundred MiB is not one; an APU set up with tens of GiB is. */
/* An amdgpu device with at least 4 GiB of VRAM. Excludes small iGPU carve-outs, includes an APU
* configured with tens of GiB. */
bool servable_gpu(const CardFacts& c) {
return c.vendor == "0x1002" && c.driver == "amdgpu" && c.vram_total >= ((int64_t)4 << 30);
}
@@ -73,19 +67,14 @@ std::string card_name(const CardFacts& c) {
}
std::string hop_link(const PciHop& h, bool cur) {
const std::string& s = cur ? h.cur_speed : h.max_speed;
const int w = cur ? h.cur_width : h.max_width;
std::string sp = s;
const size_t p = sp.find(" PCIe");
if (p != std::string::npos) sp.erase(p);
return w > 0 ? fmt("%s x%d", sp.c_str(), w) : sp;
return cur ? link_text(h.cur_speed, h.cur_width) : link_text(h.max_speed, h.max_width);
}
/* ---- the link ------------------------------------------------------------------------------ */
/* A HOP BELOW ITS OWN MAXIMUM WHILE THE CARD IS BUSY: the SMU's link power management stuck a
* level down, or a link that retrained narrower. The card's own endpoint can read full speed
* while the root port above it runs at half (a card with a switch on it), so every hop is read. */
/* A hop below its own maximum while the card is busy: link power management stuck a level down,
* or a link retrained narrower. Every hop is checked, because the card's endpoint can read full
* speed while the root port above an on-card switch runs at half. */
void check_link_under_load(const Facts& f, std::vector<Finding>* out) {
for (const CardFacts* c : served(f)) {
if (c->busy_percent < kBusyPercent) continue;
@@ -120,9 +109,8 @@ void check_link_under_load(const Facts& f, std::vector<Finding>* out) {
}
}
/* A LINK THAT CANNOT REACH THE CARD'S SPEED, whatever the load: a hop whose own maximum is below
* the card's -- a slot one generation older, a riser, a lane split. Not a fault, and often the
* board's design; worth knowing when two machines measure differently. */
/* A hop whose maximum is below the card's (older slot, riser, lane split). Often the board's
* design, not a fault; a Note. */
void check_link_capped(const Facts& f, std::vector<Finding>* out) {
for (const CardFacts* c : served(f)) {
if (c->chain.size() < 2) continue;
@@ -149,9 +137,8 @@ void check_link_capped(const Facts& f, std::vector<Finding>* out) {
/* ---- the cards against each other ----------------------------------------------------------- */
/* A TENSOR-PARALLEL STEP ENDS WHEN ITS SLOWEST RANK DOES, so cards set up differently serve at
* the slower one's pace, and a difference in a setting nobody remembers making is the usual
* reason one card is the slow one. */
/* A tensor-parallel step ends when its slowest rank does, so differently configured cards serve
* at the slower one's pace. */
void check_cards_differ(const Facts& f, std::vector<Finding>* out) {
const auto cs = served(f);
if (cs.size() < 2) return;
@@ -173,7 +160,7 @@ void check_cards_differ(const Facts& f, std::vector<Finding>* out) {
field("root port link", [](const CardFacts& c) {
return c.chain.empty() ? std::string() : hop_link(c.chain.front(), false); }, str);
field("power cap", [](const CardFacts& c) { return c.power_cap_uw; },
[](int64_t uw) { return uw < 0 ? std::string("unknown") : fmt("%lld W", (long long)(uw / 1000000)); });
watts_text);
field("voltage offset", [](const CardFacts& c) { return c.vddgfx_offset; }, str);
field("top PCIe power level", [](const CardFacts& c) {
return c.dpm_pcie.empty() ? std::string() : c.dpm_pcie.back(); }, str);
@@ -189,10 +176,8 @@ void check_cards_differ(const Facts& f, std::vector<Finding>* out) {
"/sys/class/drm/cardN/device; set them with the server stopped)");
}
/* A CAP LOWERED BELOW THE CARD'S OWN DEFAULT. The default, not the maximum, is the yardstick: a
* stock card ships below the most its board allows (300 W of 330 W on an R9700), and a finding on
* every stock card is one nobody reads. A cap someone lowered -- or one a reset left at a value a
* tool set before it -- is the one that changes what the card does under load. */
/* A power cap below the card's default. Compared to the default, not the maximum: stock cards
* ship below the board maximum (300 W of 330 W on an R9700). */
void check_power_cap(const Facts& f, std::vector<Finding>* out) {
for (const CardFacts* c : served(f)) {
if (c->power_cap_uw <= 0 || c->power_cap_default_uw <= 0) continue;
@@ -224,12 +209,9 @@ void check_small_bar(const Facts& f, std::vector<Finding>* out) {
}
}
/* AN OVERDRIVE SETTING AND RUNTIME SUSPEND. The driver holds the offset and uploads it again
* whenever the card resumes from runtime suspend; on RDNA4 that upload has been seen to fail in
* the kernel log ("Failed to upload customized OD settings") at every resume. Whether the
* firmware still holds the offset afterwards is not readable here, and a card that lost it runs a
* lower clock at the same power cap. Keeping the card out of runtime suspend keeps the question
* from arising. */
/* Overdrive offset with runtime suspend enabled. The driver re-uploads the offset on every resume,
* and on RDNA4 that upload can fail ("Failed to upload customized OD settings"). Whether the
* firmware kept it is not readable here; a card that lost it clocks lower at the same cap. */
void check_od_runtime_pm(const Facts& f, std::vector<Finding>* out) {
for (const CardFacts* c : served(f)) {
if (c->vddgfx_offset.empty() || c->vddgfx_offset == "0 mV") continue;
@@ -382,8 +364,7 @@ void check_engine(const Facts& f, const RunFacts& r, std::vector<Finding>* out)
"faster prefill, but the model does not compute the exact function; two runs on "
"different world sizes are not comparable token for token",
"--tp-wire exact when exactness matters more than prefill speed");
/* Only when the operator turned it off: where the engine did (the KL mode, an embedding
* model) there is no cache to have, and the configuration line says why. */
/* Only when the operator turned it off; an engine-side reason is shown in the configuration. */
if (!r.prefix_cache && r.prefix_cache_off_why.empty())
add(out, Finding::Note, "prefix-cache-off", "prefix caching is off (--no-prefix-cache)",
"every request prefills its whole prompt, including the conversation it shares with "
@@ -432,12 +413,24 @@ std::vector<Finding> check(const Facts& f, const RunFacts& r) {
check_iommu(f, r, &out);
check_memlock(f, r, &out);
check_cpu(f, &out);
/* Warnings first, each group in the order above: what to act on before what to know. */
/* Warnings first; stable, so each group keeps the order above. */
std::stable_sort(out.begin(), out.end(),
[](const Finding& a, const Finding& b) { return a.level > b.level; });
return out;
}
std::string link_text(const std::string& speed, int width) {
std::string s = speed;
const size_t p = s.find(" PCIe");
if (p != std::string::npos) s.erase(p);
if (s.empty()) s = "?";
return width > 0 ? fmt("%s x%d", s.c_str(), width) : s;
}
std::string watts_text(int64_t uw) {
return uw < 0 ? std::string("unknown") : fmt("%lld W", (long long)(uw / 1000000));
}
double link_gts(const std::string& speed) {
return speed.empty() ? 0.0 : std::strtod(speed.c_str(), nullptr);
}
+44 -52
View File
@@ -1,27 +1,17 @@
/* diag.h -- the startup report: what was configured, what it runs on, and what looks wrong.
*
* WHY ONE REPORT AND NOT THE LINES ALREADY IN THE LOG. Every decision startup makes is said
* somewhere on the way up, in the order it was made, between a hundred other lines. A log a user
* pastes into an issue is read by someone who has to find the line about the embedding, the one
* about the VRAM split and the one about the card's link, and cannot find the line that was never
* printed because the value was a default. The report puts all of it in one block at the end of
* startup: every option the command line takes with the value in force and where it came from,
* the machine as the kernel describes it, and a list of the things on it that cost speed or
* correctness with what to do about each.
* One block at the end of startup, instead of decisions scattered through the log (defaults are
* never logged at all): every option with its value and source, the machine as the kernel
* describes it, and findings that cost speed or correctness, each with a fix.
*
* THREE PARTS, each built from data rather than from a list kept beside the code:
* Each part is built from data, not a list kept beside the code:
* Configuration every row of the option table (core/config.cpp); a value startup resolved
* carries its reason (Decision).
* Hardware read from /proc and /sys under a root, so tests can supply a tree and a
* FROM-scratch container (no shell, lspci or rocm-smi) reads the same files.
* Findings small functions over the two above (checks.cpp), testable without a card.
*
* CONFIGURATION every row of the option table (core/config.cpp), so an option added there
* appears here without anyone remembering to; a value startup resolved carries
* the reason it was resolved that way (Decision).
* HARDWARE read from /proc and /sys under a ROOT, so a test can hand the collector a
* directory tree of its own and a container built FROM scratch -- no shell, no
* lspci, no rocm-smi -- reads the same files the host does.
* FINDINGS each a small function over the two above (checks.cpp), unit-testable without
* a card.
*
* Unknown is a value. A field that could not be read is reported as unknown rather than left out:
* a report that silently drops a line reads as a machine that does not have the thing.
* A field that could not be read is reported as unknown, never omitted.
*/
#pragma once
@@ -34,11 +24,9 @@ struct Config;
namespace diag {
/* ------------------------------------------------------------------ decisions */
/* ONE VALUE STARTUP RESOLVED, and why. `key` is the option it resolves (`--placement`) when there
* is one, so the configuration table can show it on that option's row, or a topic of its own
* (`vram split`) for what no single option states. Recording the same key again replaces the
* earlier answer: a later stage that revises a choice (the n-gram copy that could not be made)
* is the answer in force. */
/* One value startup resolved, and why. `key` is the option it resolves (`--placement`), shown on
* that option's row, or a topic of its own (`vram split`). Recording a key again replaces the
* earlier answer, so a later stage can revise a choice. */
struct Decision {
std::string key;
std::string value;
@@ -49,9 +37,8 @@ void decide(std::vector<Decision>* list, const std::string& key, const std::stri
const std::string& why);
/* ------------------------------------------------------------------ the configuration table */
/* One option as the report shows it: the value in force, where it came from (`default`, `command
* line`, an environment variable, or `auto` for a value startup derived), and the reason when
* startup derived it. */
/* One option: the value in force, its source (`default`, `command line`, an environment
* variable, or `auto` when startup derived it), and the reason when derived. */
struct ConfigRow {
std::string flag;
std::string value;
@@ -59,18 +46,17 @@ struct ConfigRow {
std::string why;
};
/* Every option in the command line's table (core/config.cpp), in its order, with the value `c`
* holds and its source from Config::given. Secrets are not values: --api-key is "set" or "not
* set". */
/* Every option in core/config.cpp's table, in order, with the value `c` holds and its source
* from Config::given. Secrets are not shown: --api-key is "set" or "not set". */
std::vector<ConfigRow> config_rows(const Config& c);
/* The options the table holds, for a test that every one reaches the report. */
std::vector<std::string> config_option_names();
/* ------------------------------------------------------------------ hardware */
/* One PCIe link on the way from the root port to a card. Speeds are the kernel's strings
* ("32.0 GT/s PCIe"), widths lanes; -1 / "" where the file is absent, as it is on a bridge
* internal to a card's own switch on some boards. */
/* One PCIe link between the root port and a card. Speeds are the kernel's strings
* ("32.0 GT/s PCIe"), widths in lanes; -1 / "" where the file is absent (some bridges inside a
* card's own switch). */
struct PciHop {
std::string bdf;
std::string cur_speed, max_speed;
@@ -98,7 +84,7 @@ struct CardFacts {
std::string vddgfx_offset; /* "-75 mV" from pp_od_clk_voltage, "" when unknown */
bool integrated = false; /* the KFD topology's node has no local memory of its own */
/* What the engine knows and the kernel does not: filled by the engine for its own cards. */
/* Filled by the engine for the cards it uses. */
std::string name; /* the runtime's product name */
int rank = -1; /* the tensor-parallel rank it serves, -1 none */
int device = -1; /* the backend's device index, -1 none */
@@ -130,24 +116,22 @@ struct Facts {
std::vector<CardFacts> cards; /* every display-class PCI device, in address order */
};
/* Reads /proc and /sys under `root` ("" is the live system). Never fails: what cannot be read is
/* Reads /proc and /sys under `root` ("" is the live system). Never fails: unreadable fields are
* left unknown.
*
* `managed`, when given, names the cards (by PCI address) whose power-management files are read
* -- the DPM tables, the overdrive block, the busy percentage and the hwmon power limits; every
* other card is described from its PCI and memory files alone. The driver answers those files
* from the card's SMU under its power-management lock, and wakes a card that was runtime-
* suspended to do it, so a server reading the machine again while it serves reads them for the
* cards it serves on and no others. Null reads them for every card. */
* `managed`, when given, lists the cards (by PCI address) whose power-management files are read:
* DPM tables, overdrive, busy percentage, hwmon power limits. The driver answers those from the
* SMU under its power-management lock and wakes a runtime-suspended card to do so, so a serving
* process reads them only for its own cards. Null reads them for every card. */
Facts collect(const std::string& root, const std::vector<std::string>* managed = nullptr);
/* What only a system call answers, filled for the live process: the memlock limit and whether
* io_uring is allowed here (a container's seccomp profile refuses it). */
/* Fills what only a system call answers for the live process: the memlock limit and whether
* io_uring is allowed (a container's seccomp profile may refuse it). */
void probe_process(HostFacts* h);
/* The names of the variables in `env` (an environ-shaped, null-terminated array) that start with
* `prefix`, sorted. Names only: a value can be a path, a host name or a token, and the report is
* written to be pasted into an issue. */
/* Sorted names of the variables in `env` (environ-shaped, null-terminated) starting with
* `prefix`. Names only: values may be paths, host names or tokens, and the report gets pasted
* into issues. */
std::vector<std::string> env_names(char* const* env, const char* prefix);
/* ------------------------------------------------------------------ what the engine did */
@@ -164,8 +148,8 @@ struct RunFacts {
bool tp_wire_lossy = false;
int64_t tp_wire_min_kb = 0;
bool prefix_cache = true;
/* Why the engine turned prefix caching off itself (the KL mode, an embedding model); "" when
* it is on or the operator turned it off. */
/* Why the engine disabled prefix caching itself (KL mode, an embedding model); "" when it is
* on or the operator turned it off. */
std::string prefix_cache_off_why;
int64_t host_pool_mib = 0; /* a rank, as configured */
int64_t pinned_bytes = -1; /* every pinned host allocation the process made */
@@ -176,9 +160,8 @@ struct RunFacts {
bool reference_kernels = false;
bool profile_ops = false;
bool deterministic = false;
/* The RADIANCE_* variables set in the environment, by name (env_names). The option table
* covers what the command line takes; these change behaviour beside it -- the log level, a
* pinned tuning, an instrument left on -- and a report of every setting in force names them. */
/* Names of the RADIANCE_* variables set (env_names): settings outside the option table,
* such as the log level, a pinned tuning or an instrument left on. */
std::vector<std::string> environment;
};
@@ -211,5 +194,14 @@ void merge(std::vector<ConfigRow>* rows, const std::vector<Decision>& decisions,
/* "32.0 GT/s PCIe" -> 32.0; 0 when it does not parse. */
double link_gts(const std::string& speed);
/* "32.0 GT/s x16" from a sysfs speed and width; "?" for an unknown speed. */
std::string link_text(const std::string& speed, int width);
/* "300 W" from microwatts; "unknown" when negative. */
std::string watts_text(int64_t uw);
/* Busy percentage above which a link below its maximum is not idle parking. */
constexpr int kBusyPercent = 30;
} /* namespace diag */
} /* namespace rad */
+11 -21
View File
@@ -1,10 +1,8 @@
/* report.cpp -- the startup report, as text for the log and as JSON for /diagnostics.
*
* ONE LAYOUT FOR A READER WHO DID NOT RUN THE SERVER: every option on its own line with the value
* in force and where it came from, the reason under any value startup derived, the machine in a
* few dense lines and a block a card, and the findings last with what to do about each. The text
* and the JSON are rendered from the same structs, so neither can say something the other does
* not.
* Text layout: one line per option with value and source (the reason below any derived value),
* the host in a few lines, a block per card, then findings with fixes. Text and JSON render from
* the same structs.
*/
#include "diag/diag.h"
#include "rad_internal.h"
@@ -57,15 +55,6 @@ std::string short_bdf(const std::string& b) {
return b.rfind("0000:", 0) == 0 ? b.substr(5) : b;
}
std::string link(const std::string& speed, int width) {
std::string s = speed;
const size_t p = s.find(" PCIe");
if (p != std::string::npos) s.erase(p);
if (s.empty()) s = "?";
return width > 0 ? fmt("%s x%d", s.c_str(), width) : s;
}
std::string watts(int64_t uw) { return uw < 0 ? std::string("unknown") : fmt("%lld W", (long long)(uw / 1000000)); }
std::string host_lines(const HostFacts& h, const RunFacts& r) {
std::string o;
@@ -118,10 +107,12 @@ std::string card_lines(const CardFacts& c) {
std::string chain;
for (const PciHop& h : c.chain)
chain += fmt("%s%s %s (max %s)", chain.empty() ? "" : " -> ", short_bdf(h.bdf).c_str(),
link(h.cur_speed, h.cur_width).c_str(), link(h.max_speed, h.max_width).c_str());
link_text(h.cur_speed, h.cur_width).c_str(),
link_text(h.max_speed, h.max_width).c_str());
o += fmt(" link %s [read at %s%% busy%s]\n", chain.empty() ? "unknown" : chain.c_str(),
c.busy_percent < 0 ? "?" : std::to_string(c.busy_percent).c_str(),
c.busy_percent >= 0 && c.busy_percent < 30 ? ": a link at rest parks below its maximum" : "");
c.busy_percent >= 0 && c.busy_percent < kBusyPercent
? ": a link at rest parks below its maximum" : "");
if (!c.dpm_pcie.empty()) {
std::string t;
for (size_t i = 0; i < c.dpm_pcie.size(); ++i)
@@ -131,8 +122,8 @@ std::string card_lines(const CardFacts& c) {
if (!c.dma.empty()) o += " dma " + c.dma + "\n";
o += fmt(" power cap %s (default %s, max %s) | voltage offset %s | sclk %s | mclk %s | "
"runtime pm %s (control %s) | iommu group %s\n",
watts(c.power_cap_uw).c_str(), watts(c.power_cap_default_uw).c_str(),
watts(c.power_cap_max_uw).c_str(), or_unknown(c.vddgfx_offset).c_str(),
watts_text(c.power_cap_uw).c_str(), watts_text(c.power_cap_default_uw).c_str(),
watts_text(c.power_cap_max_uw).c_str(), or_unknown(c.vddgfx_offset).c_str(),
or_unknown(c.sclk).c_str(), or_unknown(c.mclk).c_str(),
or_unknown(c.runtime_status).c_str(), or_unknown(c.runtime_control).c_str(),
c.iommu_type.empty() ? "none" : c.iommu_type.c_str());
@@ -286,9 +277,8 @@ std::string to_json(const std::vector<ConfigRow>& config, const std::vector<Deci
fs.push_back({ { "level", x.level == Finding::Warn ? "warn" : "note" }, { "id", x.id },
{ "what", x.what }, { "why", x.why }, { "fix", x.fix } });
o["findings"] = std::move(fs);
/* REPLACE, NOT THROW, on bytes that are not UTF-8. Values here come from the operator and the
* filesystem -- a --model path, a boot parameter, a product name -- and none of them is
* promised to be UTF-8; the strict default would make the whole answer an exception. */
/* Replace invalid UTF-8 instead of throwing: paths, boot parameters and product names are
* not guaranteed to be UTF-8. */
return o.dump(-1, ' ', false, J::error_handler_t::replace);
}
+54 -68
View File
@@ -1,10 +1,8 @@
/* sysinfo.cpp -- the machine, as /proc and /sys describe it.
*
* EVERY READ IS A FILE UNDER A ROOT. No tool is run and no library beyond the C++ runtime is
* asked: the production image is built FROM scratch and has no shell, no lspci and no rocm-smi,
* and a test hands the collector a directory tree of its own to stand for a machine with two cards
* behind a switch, a parked link, or an IOMMU that translates. What a file does not say is left
* unknown, never guessed.
* Every read is a file under a root; no tool or library is used. The production image is built
* FROM scratch (no shell, lspci or rocm-smi), and tests pass a directory tree describing the
* machine. What a file does not say is left unknown, never guessed.
*/
#include "diag/diag.h"
#include "rad_internal.h"
@@ -130,34 +128,27 @@ void read_card(const std::string& root, const std::string& bdf, bool pm, CardFac
for (const std::string& d : list(dev + "/drm"))
if (d.rfind("card", 0) == 0 && d.size() > 4 && std::isdigit((unsigned char)d[4])) c->drm = d;
/* THE WHOLE PATH TO THE ROOT PORT, not the card's own link alone. A card with a switch on it
* reports its own endpoint link at full speed whatever the slot does upstream: a root port
* parked one speed down shows only at the root port (and in the card's DPM table). */
/* Every hop up to the root port, not just the card's link: a card with an on-board switch
* reports its endpoint at full speed regardless of the slot, so a parked root port shows only
* at the root port (and in the card's DPM table). */
auto hop = [&](const std::string& at, const std::string& hop_bdf) {
PciHop h;
h.bdf = hop_bdf;
h.cur_speed = line(at + "/current_link_speed");
h.max_speed = line(at + "/max_link_speed");
h.cur_width = (int)num(at + "/current_link_width", -1);
h.max_width = (int)num(at + "/max_link_width", -1);
c->chain.push_back(h);
};
std::error_code ec;
const fs::path real = fs::canonical(dev, ec);
if (!ec) {
for (const fs::path& part : real) {
const std::string p = part.string();
if (!is_bdf(p)) continue;
const std::string hd = root + "/sys/bus/pci/devices/" + p;
PciHop h;
h.bdf = p;
h.cur_speed = line(hd + "/current_link_speed");
h.max_speed = line(hd + "/max_link_speed");
h.cur_width = (int)num(hd + "/current_link_width", -1);
h.max_width = (int)num(hd + "/max_link_width", -1);
c->chain.push_back(h);
if (is_bdf(p)) hop(root + "/sys/bus/pci/devices/" + p, p);
}
}
if (c->chain.empty()) {
PciHop h;
h.bdf = bdf;
h.cur_speed = line(dev + "/current_link_speed");
h.max_speed = line(dev + "/max_link_speed");
h.cur_width = (int)num(dev + "/current_link_width", -1);
h.max_width = (int)num(dev + "/max_link_width", -1);
c->chain.push_back(h);
}
if (c->chain.empty()) hop(dev, bdf);
c->vram_total = num(dev + "/mem_info_vram_total", -1);
c->vram_visible = num(dev + "/mem_info_vis_vram_total", -1);
@@ -185,8 +176,8 @@ void read_card(const std::string& root, const std::string& bdf, bool pm, CardFac
break;
}
{
/* "OD_VDDGFX_OFFSET:\n-75mV": the offset the driver holds for the card, which is the one
* it uploads; whether the firmware took it is not in this file. */
/* "OD_VDDGFX_OFFSET:\n-75mV": the offset the driver holds and uploads; whether the
* firmware applied it is not in this file. */
const std::string od = slurp(dev + "/pp_od_clk_voltage");
const size_t at = od.find("OD_VDDGFX_OFFSET:");
if (at != std::string::npos) {
@@ -198,14 +189,13 @@ void read_card(const std::string& root, const std::string& bdf, bool pm, CardFac
}
}
/* The KFD topology knows what the PCI files do not: each GPU node's target and SIMD count, keyed
* by its PCI location -- `location_id` is (bus << 8) | (device << 3) | function. */
/* Each KFD topology GPU node's target and SIMD count, keyed by PCI location:
* `location_id` is (bus << 8) | (device << 3) | function. */
void read_kfd(const std::string& root, std::vector<CardFacts>* cards) {
const std::string nodes = root + "/sys/class/kfd/kfd/topology/nodes";
for (const std::string& n : list(nodes)) {
/* A LINE AT A TIME, and only the four keys this needs parsed: the file holds values no
* signed 64-bit integer takes (unique_id is a full 64-bit hash), and a stream extraction
* that meets one fails and stops reading every key after it. */
/* Line by line, parsing only the needed keys: some values (unique_id, a 64-bit hash)
* overflow int64, and a failed stream extraction would stop reading all later keys. */
std::istringstream in(slurp(nodes + "/" + n + "/properties"));
std::string l;
long long loc = -1, dom = 0, gfx = 0, simd = -1;
@@ -236,19 +226,18 @@ void read_kfd(const std::string& root, std::vector<CardFacts>* cards) {
}
}
/* Of two memory limits in HostFacts' encoding (bytes, -2 unlimited, -1 unknown), the one that
* binds: the smaller number, else unlimited over unknown. */
/* The binding one of two limits in HostFacts' encoding (bytes, -2 unlimited, -1 unknown): the
* smaller number, else unlimited over unknown. */
int64_t tighter(int64_t a, int64_t b) {
if (a >= 0 && b >= 0) return std::min(a, b);
if (a >= 0 || b >= 0) return std::max(a, b);
return a == -2 || b == -2 ? -2 : -1;
}
/* THE TIGHTEST MEMORY LIMIT FROM A CGROUP UP TO THE MOUNT'S ROOT. A limit binds every cgroup
* below it, so the process's own group can say "max" while the systemd slice or the container's
* group above it holds the limit that counts. `rel` is the path /proc/self/cgroup names, under
* `mount`; every level on the way up is read, the mount's root included -- inside a container's
* cgroup namespace that root is the container's own group. */
/* The tightest memory limit from a cgroup up to the mount's root. A limit binds every cgroup
* below it, so the process's group may say "max" while a parent slice holds the real limit.
* `rel` is the /proc/self/cgroup path under `mount`; every level is read, the root included
* (inside a cgroup namespace that is the container's own group). */
int64_t cgroup_limit(const std::string& mount, std::string rel, const char* file) {
int64_t lim = -1;
while (!rel.empty() && rel.back() == '/') rel.pop_back();
@@ -323,9 +312,9 @@ void read_host(const std::string& root, HostFacts* h) {
else if (cg.find("containerd") != std::string::npos) h->container = "containerd";
}
{
/* THE PROCESS'S OWN CGROUP, which inside a container is "/" under its namespace, and
* every one above it: cgroup_limit says why. Version 2 is the "0::" line; version 1's
* memory controller is the ":memory:" one, where "unlimited" is a number near 2^63. */
/* The process's cgroup ("/" inside a container's namespace) and its ancestors. v2 is
* the "0::" line; v1's memory controller is ":memory:", where unlimited is a number
* near 2^63. */
std::istringstream in(slurp(root + "/proc/self/cgroup"));
std::string l;
int64_t lim = -1;
@@ -355,9 +344,9 @@ void read_host(const std::string& root, HostFacts* h) {
if (t.rfind(k, 0) == 0) { h->cmdline += (h->cmdline.empty() ? "" : " ") + t; break; }
}
{
/* PASSTHROUGH OR TRANSLATED, from what decides it for the cards: their IOMMU groups' type
* (identity is passthrough) is the kernel's own answer; the boot parameters and the
* presence of an IOMMU are what is left when a card has no group. */
/* Passthrough or translated. The cards' IOMMU group type (identity = passthrough) is the
* kernel's own answer; IOMMU presence and boot parameters are the fallback for a card
* with no group. */
const bool any_iommu = !list(root + "/sys/class/iommu").empty();
const bool groups = !list(root + "/sys/kernel/iommu_groups").empty();
const std::string cl = " " + h->cmdline + " ";
@@ -379,7 +368,7 @@ void read_host(const std::string& root, HostFacts* h) {
}
{
int64_t lim = num(root + "/sys/module/ttm/parameters/pages_limit", -1);
/* 0 IS THE DRIVER'S DEFAULT, which is half of RAM. */
/* 0 means the driver default, half of RAM. */
if (lim == 0 && h->mem_total > 0) lim = h->mem_total / 2 / h->page_size;
h->ttm_pages_limit = lim;
}
@@ -404,8 +393,7 @@ Facts collect(const std::string& root, const std::vector<std::string>* managed)
if (ps > 0) f.host.page_size = ps;
}
read_host(root, &f.host);
/* EVERY DISPLAY-CLASS DEVICE, the engine's or not: an integrated GPU beside the cards, or a
* card the engine was told not to use, is part of the answer to "what is in this machine". */
/* Every display-class device, used by the engine or not (an iGPU, an excluded card). */
for (const std::string& d : list(root + "/sys/bus/pci/devices")) {
const std::string cls = line(root + "/sys/bus/pci/devices/" + d + "/class");
if (cls.rfind("0x03", 0) != 0) continue;
@@ -416,23 +404,21 @@ Facts collect(const std::string& root, const std::vector<std::string>* managed)
f.cards.push_back(std::move(c));
}
read_kfd(root, &f.cards);
/* THE IOMMU'S MODE AS THE CARDS SEE IT, which beats the command line where both exist: each
* card's group says identity (passthrough) or DMA / DMA-FQ (translated), and a machine can
* hold both -- an integrated GPU left identity-mapped beside a translated discrete card. */
if (f.host.iommu == "unknown") {
std::map<std::string, std::string> by;
for (const CardFacts& c : f.cards)
if (!c.iommu_type.empty()) by[c.iommu_type] += (by[c.iommu_type].empty() ? "" : ", ") + c.bdf;
if (by.size() == 1) {
const bool id = by.begin()->first == "identity";
f.host.iommu = id ? "passthrough" : "translated";
f.host.iommu_why = "the display devices' IOMMU groups are of type " + by.begin()->first;
} else if (by.size() > 1) {
f.host.iommu = "mixed";
f.host.iommu_why.clear();
for (const auto& [t, bdfs] : by)
f.host.iommu_why += (f.host.iommu_why.empty() ? "" : "; ") + t + " for " + bdfs;
}
/* The IOMMU mode as the cards see it, which beats the command line where both exist:
* identity is passthrough, DMA / DMA-FQ translated. A machine can mix both (an
* identity-mapped iGPU beside a translated discrete card). */
std::map<std::string, std::string> by;
for (const CardFacts& c : f.cards)
if (!c.iommu_type.empty()) by[c.iommu_type] += (by[c.iommu_type].empty() ? "" : ", ") + c.bdf;
if (by.size() == 1) {
const bool id = by.begin()->first == "identity";
f.host.iommu = id ? "passthrough" : "translated";
f.host.iommu_why = "the display devices' IOMMU groups are of type " + by.begin()->first;
} else if (by.size() > 1) {
f.host.iommu = "mixed";
f.host.iommu_why.clear();
for (const auto& [t, bdfs] : by)
f.host.iommu_why += (f.host.iommu_why.empty() ? "" : "; ") + t + " for " + bdfs;
}
return f;
}
@@ -442,8 +428,8 @@ void probe_process(HostFacts* h) {
if (getrlimit(RLIMIT_MEMLOCK, &rl) == 0)
h->memlock_soft = rl.rlim_cur == RLIM_INFINITY ? -2 : (int64_t)rl.rlim_cur;
#ifdef SYS_io_uring_setup
/* ASKED, NOT INFERRED: a seccomp profile (Docker's default) refuses the call whatever the
* sysctl says, and the answer is the error the call returns. One entry, closed at once. */
/* Try the call: a seccomp profile (Docker's default) refuses it whatever the sysctl says.
* One entry, closed at once. */
struct { uint32_t sq_entries, cq_entries, flags, sq_thread_cpu, sq_thread_idle, features,
wq_fd, resv[3]; uint64_t sq_off[5]; uint64_t cq_off[5]; } p{};
static_assert(sizeof(p) == 120, "io_uring_params is 120 bytes");