diff --git a/settings/qt6-app/qml/pages/PageModel.qml b/settings/qt6-app/qml/pages/PageModel.qml index 549a0d2..3e11e7f 100644 --- a/settings/qt6-app/qml/pages/PageModel.qml +++ b/settings/qt6-app/qml/pages/PageModel.qml @@ -60,14 +60,18 @@ Item { spacing: 12 Repeater { + id: statTiles model: [ { label: "Latency", value: Engine.latencyP50 + " ms" }, { label: "RAM", value: Engine.ramGb.toFixed(1) + " GB" }, { label: "CPU", value: Engine.cpuPct + "%" }, - ] + ].concat(Engine.gpuAvailable ? [ + { label: "VRAM", value: Engine.vramGb.toFixed(1) + " GB" }, + { label: "GPU", value: Engine.gpuPct + "%" }, + ] : []) delegate: Rectangle { required property var modelData - width: (parent.width - 24) / 3; height: 52 + width: (parent.width - 12 * (statTiles.count - 1)) / statTiles.count; height: 52 radius: 7 color: Theme.ink03; border.width: 1; border.color: Theme.ink06 diff --git a/settings/qt6-app/qml/pages/PagePersonalisation.qml b/settings/qt6-app/qml/pages/PagePersonalisation.qml index aa5e84f..dc30dd4 100644 --- a/settings/qt6-app/qml/pages/PagePersonalisation.qml +++ b/settings/qt6-app/qml/pages/PagePersonalisation.qml @@ -29,11 +29,14 @@ Item { width: parent.width spacing: 12 + // Accept rate and time saved come from the engine's in-memory + // counters (metrics.json), which reset when fcitx5 restarts — + // hence "this session" and no per-day bars for those two. Repeater { model: [ - { label: "Words learned", value: CorpusStats.words.toLocaleString(), bars: [20, 30, 25, 40, 38, 50, CorpusStats.words > 0 ? 100 : 0] }, - { label: "Accept rate · 7d", value: "0%", bars: [0, 0, 0, 0, 0, 0, 0] }, - { label: "Time saved · 7d", value: "0 s", bars: [0, 0, 0, 0, 0, 0, 0] }, + { label: "Words learned", value: CorpusStats.words.toLocaleString(), bars: CorpusStats.wordsPerDay }, + { label: "Accept rate · this session", value: Math.round(Engine.acceptRate * 100) + "%", bars: [] }, + { label: "Time saved · this session", value: root.formatDuration(Engine.secondsSaved), bars: [] }, ] delegate: Rectangle { required property var modelData @@ -63,6 +66,7 @@ Item { } Item { width: 6 } XBars { + opacity: modelData.bars.length > 0 ? 1 : 0 // keep the slot so labels stay aligned values: modelData.bars width: 56; height: 24 anchors.verticalCenter: parent.verticalCenter @@ -307,6 +311,15 @@ Item { } } + // Time saved is an estimate: accepted characters at an assumed typing + // speed (EngineProbe::kCharsPerSecond). + function formatDuration(secs) { + const s = Math.round(secs) + if (s < 60) return s + " s" + if (s < 3600) return Math.round(s / 60) + " min" + return (s / 3600).toFixed(1) + " h" + } + function exportProfile() { const src = Qt.resolvedUrl("file://" + Qt.platform.os) // placeholder // In a real app: QFileDialog exposed via invokable; for now show toast @@ -325,13 +338,14 @@ Item { Rectangle { anchors.centerIn: parent - width: 320; height: 160 + width: 320; height: confirmCol.implicitHeight + 40 radius: Theme.radius.card color: Qt.rgba(0.08, 0.06, 0.13, 0.97) border.color: Theme.ink12; border.width: 1 Column { - anchors { fill: parent; margins: 20 } + id: confirmCol + anchors { left: parent.left; right: parent.right; top: parent.top; margins: 20 } spacing: 16 Text { @@ -342,7 +356,7 @@ Item { } Text { width: parent.width - text: "This removes all learned text. A backup is saved automatically." + text: "This permanently deletes everything XType learned from your typing. The engine stops using it within 5 minutes." font.family: Theme.sansFamily; font.pixelSize: 12 color: Theme.ink65; wrapMode: Text.WordWrap renderType: Text.NativeRendering @@ -355,9 +369,9 @@ Item { variant: "primary" onClicked: { resetConfirm.visible = false - // xtype-corpus wipe --yes is destructive — run and refresh stats - Qt.openUrlExternally("xtype-corpus://wipe") // no-op placeholder - CorpusStats.refresh() + const removed = CorpusStats.wipe() + StyleProfile.refresh() + wipeText.text = removed < 0 ? "Could not delete some corpus files" : "Corpus wiped" wipeToast.show() } } diff --git a/settings/qt6-app/qml/primitives/XSidebar.qml b/settings/qt6-app/qml/primitives/XSidebar.qml index a2570c8..0ea393e 100644 --- a/settings/qt6-app/qml/primitives/XSidebar.qml +++ b/settings/qt6-app/qml/primitives/XSidebar.qml @@ -139,58 +139,28 @@ Item { } } - // CPU row - Row { - width: parent.width - spacing: 8 - Text { - text: "CPU" - font.family: Theme.sansFamily; font.pixelSize: 10 - font.capitalization: Font.AllUppercase - color: Theme.ink45; font.weight: Font.DemiBold - renderType: Text.NativeRendering - anchors.verticalCenter: parent.verticalCenter - } - XMiniBar { - value: Engine.cpuPct / 100.0 - anchors.verticalCenter: parent.verticalCenter - width: parent.width - cpuLabel.implicitWidth - 38 - } - Text { - id: cpuLabel - text: Engine.cpuPct + "%" - font.family: Theme.monoFamily; font.pixelSize: 11 - color: Theme.ink80 - renderType: Text.NativeRendering - anchors.verticalCenter: parent.verticalCenter - } + // Resource rows. GPU/VRAM only appear on machines with an NVIDIA GPU. + StatRow { + caption: "CPU" + fraction: Engine.cpuPct / 100.0 + valueText: Engine.cpuPct + "%" } - - // RAM row - Row { - width: parent.width - spacing: 8 - Text { - text: "RAM" - font.family: Theme.sansFamily; font.pixelSize: 10 - font.capitalization: Font.AllUppercase - color: Theme.ink45; font.weight: Font.DemiBold - renderType: Text.NativeRendering - anchors.verticalCenter: parent.verticalCenter - } - XMiniBar { - value: Math.min(1.0, Engine.ramGb / 4.0) - anchors.verticalCenter: parent.verticalCenter - width: parent.width - ramLabel.implicitWidth - 38 - } - Text { - id: ramLabel - text: Engine.ramGb.toFixed(1) + " GB" - font.family: Theme.monoFamily; font.pixelSize: 11 - color: Theme.ink80 - renderType: Text.NativeRendering - anchors.verticalCenter: parent.verticalCenter - } + StatRow { + caption: "RAM" + fraction: Math.min(1.0, Engine.ramGb / 4.0) + valueText: Engine.ramGb.toFixed(1) + " GB" + } + StatRow { + visible: Engine.gpuAvailable + caption: "GPU" + fraction: Engine.gpuPct / 100.0 + valueText: Engine.gpuPct + "%" + } + StatRow { + visible: Engine.gpuAvailable + caption: "VRAM" + fraction: Engine.vramTotalGb > 0 ? Math.min(1.0, Engine.vramGb / Engine.vramTotalGb) : 0 + valueText: Engine.vramGb.toFixed(1) + " GB" } // Engine line @@ -207,7 +177,43 @@ Item { } } - // ── Inline component ─────────────────────────────────────────────────── + // ── Inline components ────────────────────────────────────────────────── + // Caption and value columns have fixed widths so the bars line up. + TextMetrics { id: captionMetrics; font.family: Theme.sansFamily; font.pixelSize: 10; font.weight: Font.DemiBold; text: "VRAM" } + TextMetrics { id: valueMetrics; font.family: Theme.monoFamily; font.pixelSize: 11; text: "00.0 GB" } + + component StatRow: Row { + id: statRow + property string caption: "" + property real fraction: 0.0 // 0.0 – 1.0 + property string valueText: "" + width: parent ? parent.width : 0 + spacing: 8 + Text { + width: captionMetrics.advanceWidth + text: statRow.caption + font.family: Theme.sansFamily; font.pixelSize: 10 + font.capitalization: Font.AllUppercase + color: Theme.ink45; font.weight: Font.DemiBold + renderType: Text.NativeRendering + anchors.verticalCenter: parent.verticalCenter + } + XMiniBar { + value: statRow.fraction + anchors.verticalCenter: parent.verticalCenter + width: statRow.width - captionMetrics.advanceWidth - valueMetrics.advanceWidth - 2 * statRow.spacing + } + Text { + width: valueMetrics.advanceWidth + horizontalAlignment: Text.AlignRight + text: statRow.valueText + font.family: Theme.monoFamily; font.pixelSize: 11 + color: Theme.ink80 + renderType: Text.NativeRendering + anchors.verticalCenter: parent.verticalCenter + } + } + component NavItem: Item { id: navRoot property string navId: "" diff --git a/settings/qt6-app/src/corpus_stats.cpp b/settings/qt6-app/src/corpus_stats.cpp index 3b816ac..1a28a17 100644 --- a/settings/qt6-app/src/corpus_stats.cpp +++ b/settings/qt6-app/src/corpus_stats.cpp @@ -1,54 +1,158 @@ #include "corpus_stats.h" -#include -#include -#include +#include +#include +#include + +// Counts whitespace-separated tokens; byte-level so multi-MB corpora stay cheap. +static int countWords(const QByteArray &line) { + int n = 0; + bool inWord = false; + for (char c : line) { + const bool space = c == ' ' || c == '\t' || c == '\r' || c == '\n' + || c == '\v' || c == '\f'; + if (!space && !inWord) ++n; + inWord = !space; + } + return n; +} + +static QString timestampsPath(const QString &corpusPath) { + const QFileInfo fi(corpusPath); + return fi.dir().filePath(fi.completeBaseName() + "_timestamps.txt"); +} CorpusStats::CorpusStats(QObject *parent) : QObject(parent) { - _timeout.setSingleShot(true); - _timeout.setInterval(5000); - connect(&_timeout, &QTimer::timeout, this, &CorpusStats::onTimeout); - connect(&_proc, QOverload::of(&QProcess::finished), - this, &CorpusStats::onFinished); - _proc.setProcessChannelMode(QProcess::MergedChannels); + _counts.wordsPerDay = QList(kDays, 0); + _timer.setInterval(3000); + connect(&_timer, &QTimer::timeout, this, &CorpusStats::refresh); + _timer.start(); + setCorpusPath("~/.local/share/xtype/corpus.txt"); +} +QVariantList CorpusStats::wordsPerDay() const { + QVariantList out; + for (int n : _counts.wordsPerDay) out.append(n); + return out; +} + +void CorpusStats::setCorpusPath(const QString &path) { + QString p = path; + if (p.startsWith("~/")) p = QDir::homePath() + p.mid(1); + if (p == _path) return; + _path = p; + _signature.clear(); refresh(); } +QStringList CorpusStats::corpusFiles(const QString &corpusPath) { + const QFileInfo fi(corpusPath); + QStringList out; + if (fi.exists()) out << fi.absoluteFilePath(); + // Rotated siblings written by CorpusCollector::rotateIfNeeded(); skip the + // transient .tmp the engine uses for atomic pruning. + const QStringList rotated = fi.dir().entryList({fi.fileName() + ".*"}, QDir::Files, QDir::Name); + for (const QString &name : rotated) + if (!name.endsWith(".tmp")) out << fi.dir().filePath(name); + return out; +} + +QString CorpusStats::fileSignature() const { + QString sig; + QStringList inputs = corpusFiles(_path); + inputs << timestampsPath(_path); + for (const QString &f : inputs) { + const QFileInfo fi(f); + if (!fi.exists()) continue; + sig += f + ':' + QString::number(fi.size()) + ':' + + QString::number(fi.lastModified().toMSecsSinceEpoch()) + ';'; + } + return sig; +} + void CorpusStats::refresh() { - if (_running) return; - _running = true; - _proc.start("xtype-corpus", {"stats", "--json"}); - if (!_proc.waitForStarted(500)) { - _running = false; - return; // binary not found — stats stay at 0 + if (_path.isEmpty()) return; + const QDate today = QDate::currentDate(); + const QString sig = fileSignature(); + if (sig == _signature && today == _countedOn) return; + _signature = sig; + _countedOn = today; + + const CorpusCounts c = count(_path, QDateTime::currentDateTime()); + if (c.sentences != _counts.sentences || c.words != _counts.words + || c.bytes != _counts.bytes || c.wordsPerDay != _counts.wordsPerDay) { + _counts = c; + emit statsChanged(); } - _timeout.start(); } -void CorpusStats::onFinished(int code, QProcess::ExitStatus) { - _timeout.stop(); - _running = false; +CorpusCounts CorpusStats::count(const QString &corpusPath, const QDateTime &now) { + CorpusCounts c; + c.wordsPerDay = QList(kDays, 0); - if (code != 0) return; + // The engine appends one timestamp line per corpus line (same order), so + // line i of the live corpus file was harvested at timestamps[i]. Rotated + // files have no timestamps and only contribute to the totals. + QList stamps; + { + QFile tf(timestampsPath(corpusPath)); + if (tf.open(QIODevice::ReadOnly)) { + for (const QByteArray &l : tf.readAll().split('\n')) + stamps.append(l.trimmed().toLongLong()); // malformed -> 0 (undated) + } + } - const QByteArray raw = _proc.readAll(); - const QJsonObject obj = QJsonDocument::fromJson(raw).object(); - if (obj.isEmpty()) return; + const QString live = QFileInfo(corpusPath).absoluteFilePath(); + const QDate today = now.date(); - const int s = obj.value("sentences").toInt(); - const int w = obj.value("words").toInt(); - const double m = obj.value("mb").toDouble(); + for (const QString &path : corpusFiles(corpusPath)) { + QFile f(path); + if (!f.open(QIODevice::ReadOnly)) continue; + const QByteArray data = f.readAll(); + c.bytes += data.size(); - if (s != _sentences || w != _words || m != _mb) { - _sentences = s; - _words = w; - _mb = m; - emit statsChanged(); + const bool dated = (path == live); + const QList lines = data.split('\n'); + for (qsizetype i = 0; i < lines.size(); ++i) { + const int w = countWords(lines[i]); + if (w == 0) continue; + ++c.sentences; + c.words += w; + + if (!dated || i >= stamps.size() || stamps[i] <= 0) continue; + const QDate day = QDateTime::fromSecsSinceEpoch(stamps[i]).date(); + const qint64 ago = day.daysTo(today); + if (ago >= 0 && ago < kDays) + c.wordsPerDay[kDays - 1 - static_cast(ago)] += w; + } } + return c; } -void CorpusStats::onTimeout() { - _proc.kill(); - _running = false; +int CorpusStats::wipe() { + const int removed = wipeFiles(_path); + _signature.clear(); + refresh(); + return removed; +} + +int CorpusStats::wipeFiles(const QString &corpusPath) { + if (corpusPath.isEmpty()) return -1; + const QFileInfo fi(corpusPath); + // style_profile.json sits next to the corpus (XTypeEngine derives it from + // corpus_path's parent directory). + QStringList targets = corpusFiles(corpusPath); + targets << timestampsPath(corpusPath) + << fi.dir().filePath(fi.fileName() + ".tmp") // pruneOldEntries() scratch files + << timestampsPath(corpusPath) + ".tmp" + << fi.dir().filePath("style_profile.json"); + + int removed = 0; + bool failed = false; + for (const QString &t : targets) { + if (!QFileInfo::exists(t)) continue; + if (QFile::remove(t)) ++removed; + else failed = true; + } + return failed ? -1 : removed; } diff --git a/settings/qt6-app/src/corpus_stats.h b/settings/qt6-app/src/corpus_stats.h index eccb628..ed16342 100644 --- a/settings/qt6-app/src/corpus_stats.h +++ b/settings/qt6-app/src/corpus_stats.h @@ -1,36 +1,61 @@ #pragma once +#include +#include #include -#include +#include #include +#include + +struct CorpusCounts { + int sentences = 0; + int words = 0; + qint64 bytes = 0; + QList wordsPerDay; // kDays entries, oldest first; last entry is today +}; class CorpusStats : public QObject { Q_OBJECT - Q_PROPERTY(int sentences READ sentences NOTIFY statsChanged) - Q_PROPERTY(int words READ words NOTIFY statsChanged) - Q_PROPERTY(double mb READ mb NOTIFY statsChanged) + Q_PROPERTY(int sentences READ sentences NOTIFY statsChanged) + Q_PROPERTY(int words READ words NOTIFY statsChanged) + Q_PROPERTY(double mb READ mb NOTIFY statsChanged) + Q_PROPERTY(QVariantList wordsPerDay READ wordsPerDay NOTIFY statsChanged) public: + static constexpr int kDays = 7; + explicit CorpusStats(QObject *parent = nullptr); - int sentences() const { return _sentences; } - int words() const { return _words; } - double mb() const { return _mb; } + int sentences() const { return _counts.sentences; } + int words() const { return _counts.words; } + double mb() const { return _counts.bytes / (1024.0 * 1024.0); } + QVariantList wordsPerDay() const; + + // Accepts a leading "~/" (as stored in config.toml). + void setCorpusPath(const QString &path); Q_INVOKABLE void refresh(); + // Deletes everything learned from typing: the corpus, its rotated copies, + // the timestamp file and style_profile.json. Returns the number of files + // removed, or -1 if any existing file could not be removed. + Q_INVOKABLE int wipe(); + static int wipeFiles(const QString &corpusPath); + + // Reads the corpus file plus its rotated siblings (corpus.txt.1, ...) and + // the engine's per-line timestamp file. `now` anchors the 7-day window. + static CorpusCounts count(const QString &corpusPath, const QDateTime &now); + signals: void statsChanged(); -private slots: - void onFinished(int code, QProcess::ExitStatus status); - void onTimeout(); - private: - int _sentences = 0; - int _words = 0; - double _mb = 0.0; - QProcess _proc; - QTimer _timeout; - bool _running = false; + static QStringList corpusFiles(const QString &corpusPath); + QString fileSignature() const; + + QString _path; + QString _signature; // size+mtime of every input; recount only on change + QDate _countedOn; // day buckets shift at midnight even without writes + CorpusCounts _counts; + QTimer _timer; }; diff --git a/settings/qt6-app/src/engine_probe.cpp b/settings/qt6-app/src/engine_probe.cpp index 2249a44..718a255 100644 --- a/settings/qt6-app/src/engine_probe.cpp +++ b/settings/qt6-app/src/engine_probe.cpp @@ -3,8 +3,11 @@ #include #include #include +#include +#include #include -#include +#include +#include #include #include @@ -19,8 +22,28 @@ EngineProbe::EngineProbe(QObject *parent) : QObject(parent) { _clkTck = sysconf(_SC_CLK_TCK); + _numCpus = qMax(1L, sysconf(_SC_NPROCESSORS_ONLN)); _demoMetrics = qEnvironmentVariableIsSet("XTYPE_DEMO_METRICS"); + // GPU stats: only bother if there is an NVIDIA display/3D controller + // (PCI vendor 0x10de, class 0x03xxxx) and nvidia-smi is installed. + const QDir pciDir("/sys/bus/pci/devices"); + for (const QString &dev : pciDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { + const QString base = pciDir.filePath(dev); + QFile vendor(base + "/vendor"), cls(base + "/class"); + if (!vendor.open(QIODevice::ReadOnly) || !cls.open(QIODevice::ReadOnly)) continue; + if (vendor.readAll().trimmed() == "0x10de" && cls.readAll().startsWith("0x03")) + _nvidiaPciDevs.append(base); + } + _gpuDisabled = _nvidiaPciDevs.isEmpty() || QStandardPaths::findExecutable("nvidia-smi").isEmpty(); + _gpuAvailable = !_gpuDisabled; + connect(&_gpuProc, &QProcess::finished, this, &EngineProbe::onGpuQueryFinished); + connect(&_gpuProc, &QProcess::errorOccurred, this, [this](QProcess::ProcessError err) { + if (err != QProcess::FailedToStart) return; + _gpuDisabled = true; + if (_gpuAvailable) { _gpuAvailable = false; emit gpuChanged(); } + }); + _timer.setInterval(1500); _timer.setSingleShot(false); connect(&_timer, &QTimer::timeout, this, &EngineProbe::poll); @@ -28,102 +51,123 @@ EngineProbe::EngineProbe(QObject *parent) poll(); // first tick immediately } -// Find the PID of `ollama serve` (or ollama in any invocation) by scanning /proc. -// Returns -1 if not found. -int EngineProbe::findOllamaPid() { - if (_demoMetrics) return static_cast(getpid()); +// Reads /proc//stat. The comm field is wrapped in parens and may contain +// spaces, so everything after the last ')' is split as the remaining fields +// (fields[0] = state, [1] = ppid, [11] = utime, [12] = stime). +static bool readStat(int pid, QByteArray *comm, QList *fields) { + QFile f(QString("/proc/%1/stat").arg(pid)); + if (!f.open(QIODevice::ReadOnly)) return false; + const QByteArray raw = f.readAll(); + const int lparen = raw.indexOf('('); + const int rparen = raw.lastIndexOf(')'); + if (lparen < 0 || rparen < lparen || rparen + 2 >= raw.size()) return false; + if (comm) *comm = raw.mid(lparen + 1, rparen - lparen - 1); + *fields = raw.mid(rparen + 2).split(' '); + return true; +} - const QDir procDir("/proc"); - const QStringList entries = procDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); +QList EngineProbe::engineProcessTree(const QList &procs) { + QHash> children; + QList result; + for (const ProcInfo &p : procs) { + children[p.ppid].append(p.pid); + if (p.comm == "ollama") result.append(p.pid); + } + // Breadth-first over descendants; `seen` guards against a root that is + // itself a descendant of another root (e.g. `ollama runner` under serve). + QSet seen(result.begin(), result.end()); + for (int i = 0; i < result.size(); ++i) { + for (int child : children.value(result[i])) { + if (!seen.contains(child)) { seen.insert(child); result.append(child); } + } + } + return result; +} + +// PIDs of every ollama process plus their descendants (the model runner). +// Empty if ollama isn't running. +QList EngineProbe::findEnginePids() { + if (_demoMetrics) return { static_cast(getpid()) }; + + QList procs; + const QStringList entries = QDir("/proc").entryList(QDir::Dirs | QDir::NoDotAndDotDot); for (const QString &entry : entries) { bool ok = false; - int pid = entry.toInt(&ok); + const int pid = entry.toInt(&ok); if (!ok || pid <= 0) continue; - - QFile comm("/proc/" + entry + "/comm"); - if (!comm.open(QIODevice::ReadOnly)) continue; - const QByteArray name = comm.readAll().trimmed(); - if (name == "ollama") return pid; + QByteArray comm; + QList fields; + if (!readStat(pid, &comm, &fields) || fields.size() < 2) continue; + procs.append({ pid, fields[1].toInt(), comm }); } - return -1; + return engineProcessTree(procs); } -void EngineProbe::sampleCpu(int pid) { - if (pid < 0) { - if (_cpuPct != 0) { _cpuPct = 0; emit cpuPctChanged(); } - _lastPid = -1; - return; +void EngineProbe::sampleCpu(const QList &pids) { + QHash ticks; + for (int pid : pids) { + QList fields; + if (!readStat(pid, nullptr, &fields) || fields.size() < 13) continue; + bool ok1, ok2; + const uint64_t utime = fields[11].toULongLong(&ok1); + const uint64_t stime = fields[12].toULongLong(&ok2); + if (ok1 && ok2) ticks.insert(pid, utime + stime); } - const QString statPath = QString("/proc/%1/stat").arg(pid); - QFile f(statPath); - if (!f.open(QIODevice::ReadOnly)) { + const uint64_t wallNs = nowNs(); + if (ticks.isEmpty()) { if (_cpuPct != 0) { _cpuPct = 0; emit cpuPctChanged(); } - return; - } - const QByteArray raw = f.readAll(); - // Find the last ')' to skip the comm field which may contain spaces - const int rparen = raw.lastIndexOf(')'); - if (rparen < 0 || rparen + 2 >= raw.size()) return; - - const QByteArray rest = raw.mid(rparen + 2); // skip ") " - const QList fields = rest.split(' '); - // fields[11] = utime, fields[12] = stime (0-indexed from state) - if (fields.size() < 13) return; - - bool ok1, ok2; - const uint64_t utime = fields[11].toULongLong(&ok1); - const uint64_t stime = fields[12].toULongLong(&ok2); - if (!ok1 || !ok2) return; - - const uint64_t totalTicks = utime + stime; - const uint64_t wallNs = nowNs(); - - if (_lastPid == pid && _lastWallNs > 0) { - const double deltaTicks = static_cast(totalTicks - _lastCpuTicks); - const double deltaSecs = static_cast(wallNs - _lastWallNs) / 1e9; + } else if (_lastWallNs > 0) { + // Only processes present in both samples contribute; a freshly spawned + // runner gets its baseline this tick and is counted from the next. + double deltaTicks = 0.0; + for (auto it = ticks.cbegin(); it != ticks.cend(); ++it) { + const auto prev = _lastCpuTicks.constFind(it.key()); + if (prev != _lastCpuTicks.cend() && it.value() >= prev.value()) + deltaTicks += static_cast(it.value() - prev.value()); + } + const double deltaSecs = static_cast(wallNs - _lastWallNs) / 1e9; if (deltaSecs > 0.0) { + // Share of total machine CPU: the runner is multi-threaded, so + // per-core % would routinely exceed 100. const int pct = static_cast( - (deltaTicks / static_cast(_clkTck)) / deltaSecs * 100.0 + (deltaTicks / static_cast(_clkTck)) / deltaSecs + / static_cast(_numCpus) * 100.0 + 0.5 ); const int clamped = qBound(0, pct, 100); if (_cpuPct != clamped) { _cpuPct = clamped; emit cpuPctChanged(); } } } - _lastPid = pid; - _lastCpuTicks = totalTicks; - _lastWallNs = wallNs; + _lastCpuTicks = std::move(ticks); + _lastWallNs = wallNs; } -void EngineProbe::sampleRam(int pid) { - if (pid < 0) { - if (_ramGb != 0.0) { _ramGb = 0.0; emit ramGbChanged(); } - return; - } - - QFile f(QString("/proc/%1/status").arg(pid)); - if (!f.open(QIODevice::ReadOnly)) { - if (_ramGb != 0.0) { _ramGb = 0.0; emit ramGbChanged(); } - return; - } - - while (!f.atEnd()) { - const QByteArray line = f.readLine(); - if (line.startsWith("VmRSS:")) { - // Format: "VmRSS: 123456 kB" - const QList parts = line.simplified().split(' '); - if (parts.size() >= 2) { - bool ok; - const double kb = parts[1].toDouble(&ok); - if (ok) { - const double gb = kb / (1024.0 * 1024.0); - if (qAbs(_ramGb - gb) > 0.005) { _ramGb = gb; emit ramGbChanged(); } - } +void EngineProbe::sampleRam(const QList &pids) { + double totalKb = 0.0; + for (int pid : pids) { + QFile f(QString("/proc/%1/status").arg(pid)); + if (!f.open(QIODevice::ReadOnly)) continue; + // Don't loop on f.atEnd(): procfs files report size 0, so QFile + // considers them at EOF before anything is read. readAll() works. + const QList lines = f.readAll().split('\n'); + for (const QByteArray &line : lines) { + if (line.startsWith("VmRSS:")) { + // Format: "VmRSS: 123456 kB" + const QList parts = line.simplified().split(' '); + bool ok = false; + const double kb = parts.size() >= 2 ? parts[1].toDouble(&ok) : 0.0; + if (ok) totalKb += kb; + break; } - break; } } + + const double gb = totalKb / (1024.0 * 1024.0); + if (qAbs(_ramGb - gb) > 0.005 || (gb == 0.0 && _ramGb != 0.0)) { + _ramGb = gb; + emit ramGbChanged(); + } } void EngineProbe::checkFcitx5State() { @@ -163,23 +207,149 @@ void EngineProbe::checkFcitx5State() { } void EngineProbe::poll() { - const int pid = findOllamaPid(); - sampleCpu(pid); - sampleRam(pid); + const QList pids = findEnginePids(); + sampleCpu(pids); + sampleRam(pids); + _gpuQueryPids = pids; + startGpuQuery(); checkFcitx5State(); QFile mf(QDir::homePath() + "/.local/share/xtype/metrics.json"); if (mf.open(QIODevice::ReadOnly)) { - QByteArray data = mf.readAll(); + const auto m = parseMetrics(mf.readAll()); mf.close(); - QRegularExpression re(R"("latency_p50_ms"\s*:\s*(\d+))"); - auto m = re.match(QString::fromUtf8(data)); - if (m.hasMatch()) { - int p50 = m.captured(1).toInt(); - if (p50 != _latencyP50) { - _latencyP50 = p50; - emit latencyP50Changed(); - } + if (!m) return; + if (m->latencyP50 != _latencyP50) { + _latencyP50 = m->latencyP50; + emit latencyP50Changed(); + } + if (m->generated != _generated || m->accepted != _accepted + || m->charsAccepted != _charsAccepted) { + _generated = m->generated; + _accepted = m->accepted; + _charsAccepted = m->charsAccepted; + emit metricsChanged(); } } } + +// ── GPU (NVIDIA) ───────────────────────────────────────────────────────────── + +static const QStringList kGpuQueryArgs = { "--query-gpu=uuid,utilization.gpu,memory.total", + "--format=csv,noheader,nounits" }; +static const QStringList kAppsQueryArgs = { "--query-compute-apps=gpu_uuid,pid,used_memory", + "--format=csv,noheader,nounits" }; + +// False when every NVIDIA GPU is runtime-suspended (hybrid laptops). Querying +// it would wake it up and burn battery, and a suspended GPU holds no model. +bool EngineProbe::nvidiaGpuAwake() const { + for (const QString &dev : _nvidiaPciDevs) { + QFile f(dev + "/power/runtime_status"); + if (!f.open(QIODevice::ReadOnly) || f.readAll().trimmed() != "suspended") return true; + } + return false; +} + +void EngineProbe::startGpuQuery() { + if (_gpuDisabled) return; + if (_gpuProc.state() != QProcess::NotRunning) { + // Previous query still going. nvidia-smi can hang when the driver is + // wedged; give up on it after a few ticks so later polls can retry. + if (++_gpuBusyTicks >= 4) { _gpuProc.kill(); _gpuBusyTicks = 0; } + return; + } + _gpuBusyTicks = 0; + + if (!nvidiaGpuAwake()) { + GpuSample idle; + idle.valid = true; + idle.vramTotalGb = _vramTotalGb; // unknown while asleep; keep the bar scale + applyGpuSample(idle); + return; + } + _gpuCsv.clear(); + _gpuProc.start("nvidia-smi", kGpuQueryArgs); +} + +void EngineProbe::onGpuQueryFinished() { + const bool ok = _gpuProc.exitStatus() == QProcess::NormalExit && _gpuProc.exitCode() == 0; + const QByteArray out = _gpuProc.readAllStandardOutput(); + if (!ok) return; // keep last values; try again next tick + + if (_gpuProc.arguments() == kGpuQueryArgs) { + _gpuCsv = out; + _gpuProc.start("nvidia-smi", kAppsQueryArgs); + return; + } + applyGpuSample(summarizeGpu(_gpuCsv, out, _gpuQueryPids)); +} + +void EngineProbe::applyGpuSample(const GpuSample &g) { + if (!g.valid) return; + if (_gpuPct != g.pct || qAbs(_vramGb - g.vramGb) > 0.005 + || qAbs(_vramTotalGb - g.vramTotalGb) > 0.005) { + _gpuPct = g.pct; + _vramGb = g.vramGb; + _vramTotalGb = g.vramTotalGb; + emit gpuChanged(); + } +} + +// Splits one CSV line from nvidia-smi into trimmed fields. +static QList csvFields(const QByteArray &line) { + QList fields = line.split(','); + for (QByteArray &f : fields) f = f.trimmed(); + return fields; +} + +EngineProbe::GpuSample EngineProbe::summarizeGpu(const QByteArray &gpuCsv, const QByteArray &appsCsv, + const QList &enginePids) { + struct Gpu { int util = 0; double totalMib = 0.0; }; + QHash gpus; + QByteArray firstUuid; + for (const QByteArray &line : gpuCsv.split('\n')) { + const QList f = csvFields(line); + if (f.size() < 3 || f[0].isEmpty()) continue; + if (firstUuid.isEmpty()) firstUuid = f[0]; + gpus.insert(f[0], { f[1].toInt(), f[2].toDouble() }); // "[N/A]" -> 0 + } + + GpuSample s; + if (gpus.isEmpty()) return s; + s.valid = true; + + const QSet pids(enginePids.begin(), enginePids.end()); + QSet hosting; // GPUs that hold part of the model + double usedMib = 0.0; + for (const QByteArray &line : appsCsv.split('\n')) { + const QList f = csvFields(line); + if (f.size() < 3 || !pids.contains(f[1].toInt())) continue; + usedMib += f[2].toDouble(); + hosting.insert(f[0]); + } + + double totalMib = 0.0; + for (const QByteArray &uuid : hosting) { + const Gpu g = gpus.value(uuid); + s.pct = qMax(s.pct, g.util); + totalMib += g.totalMib; + } + // Model not on any GPU: report 0% load and scale the bar to the first GPU. + if (hosting.isEmpty()) totalMib = gpus.value(firstUuid).totalMib; + + s.pct = qBound(0, s.pct, 100); + s.vramGb = usedMib / 1024.0; + s.vramTotalGb = totalMib / 1024.0; + return s; +} + +std::optional EngineProbe::parseMetrics(const QByteArray &json) { + const QJsonObject obj = QJsonDocument::fromJson(json).object(); + if (obj.isEmpty()) return std::nullopt; + Metrics m; + m.latencyP50 = obj.value("latency_p50_ms").toInt(); + m.generated = obj.value("suggestions_generated").toInteger(); + m.accepted = obj.value("suggestions_accepted").toInteger(); + m.charsAccepted = obj.value("chars_accepted").toInteger(); + return m; +} diff --git a/settings/qt6-app/src/engine_probe.h b/settings/qt6-app/src/engine_probe.h index bebd3c6..82da44a 100644 --- a/settings/qt6-app/src/engine_probe.h +++ b/settings/qt6-app/src/engine_probe.h @@ -1,57 +1,129 @@ #pragma once +#include +#include #include #include #include #include #include +#include class EngineProbe : public QObject { Q_OBJECT Q_PROPERTY(int cpuPct READ cpuPct NOTIFY cpuPctChanged) Q_PROPERTY(double ramGb READ ramGb NOTIFY ramGbChanged) + // NVIDIA GPU stats via nvidia-smi. gpuAvailable stays false on machines + // without an NVIDIA GPU or without nvidia-smi; the UI hides the rows then. + Q_PROPERTY(bool gpuAvailable READ gpuAvailable NOTIFY gpuChanged) + Q_PROPERTY(int gpuPct READ gpuPct NOTIFY gpuChanged) // utilisation of the GPU(s) holding the model + Q_PROPERTY(double vramGb READ vramGb NOTIFY gpuChanged) // VRAM used by the engine processes + Q_PROPERTY(double vramTotalGb READ vramTotalGb NOTIFY gpuChanged) Q_PROPERTY(QString state READ state NOTIFY stateChanged) Q_PROPERTY(QString stateMessage READ stateMessage NOTIFY stateMessageChanged) Q_PROPERTY(int latencyP50 READ latencyP50 NOTIFY latencyP50Changed) + // Engine counters from metrics.json. They live in the fcitx5 process and + // reset whenever fcitx5 restarts. + Q_PROPERTY(qint64 suggestionsGenerated READ suggestionsGenerated NOTIFY metricsChanged) + Q_PROPERTY(qint64 suggestionsAccepted READ suggestionsAccepted NOTIFY metricsChanged) + Q_PROPERTY(qint64 charsAccepted READ charsAccepted NOTIFY metricsChanged) + Q_PROPERTY(double acceptRate READ acceptRate NOTIFY metricsChanged) // 0..1 + Q_PROPERTY(double secondsSaved READ secondsSaved NOTIFY metricsChanged) public: + // Typing speed assumed for the time-saved estimate: 40 WPM x 5 chars/word. + static constexpr double kCharsPerSecond = 40.0 * 5.0 / 60.0; explicit EngineProbe(QObject *parent = nullptr); int cpuPct() const { return _cpuPct; } double ramGb() const { return _ramGb; } + bool gpuAvailable() const { return _gpuAvailable; } + int gpuPct() const { return _gpuPct; } + double vramGb() const { return _vramGb; } + double vramTotalGb() const { return _vramTotalGb; } QString state() const { return _state; } QString stateMessage() const { return _stateMessage; } int latencyP50() const { return _latencyP50; } + qint64 suggestionsGenerated() const { return _generated; } + qint64 suggestionsAccepted() const { return _accepted; } + qint64 charsAccepted() const { return _charsAccepted; } + double acceptRate() const { return _generated > 0 ? double(_accepted) / double(_generated) : 0.0; } + double secondsSaved() const { return double(_charsAccepted) / kCharsPerSecond; } + + struct Metrics { + int latencyP50 = 0; + qint64 generated = 0; + qint64 accepted = 0; + qint64 charsAccepted = 0; + }; + // Parses metrics.json as written by XTypeEngine::writeMetrics(). + // Returns nullopt for unreadable/partial JSON so the last good values stay. + static std::optional parseMetrics(const QByteArray &json); + + // Given (pid, ppid, comm) for every process, returns the `ollama` processes + // plus all their descendants. Ollama runs the model in a child process + // (`llama-server` or `ollama runner`), so `ollama serve` alone is nearly idle. + struct ProcInfo { int pid; int ppid; QByteArray comm; }; + static QList engineProcessTree(const QList &procs); + + // Output of `nvidia-smi --query-gpu=uuid,utilization.gpu,memory.total` and + // `nvidia-smi --query-compute-apps=gpu_uuid,pid,used_memory`, both with + // `--format=csv,noheader,nounits`. "[N/A]" fields count as 0. + struct GpuSample { bool valid = false; int pct = 0; double vramGb = 0.0; double vramTotalGb = 0.0; }; + static GpuSample summarizeGpu(const QByteArray &gpuCsv, const QByteArray &appsCsv, + const QList &enginePids); + signals: void cpuPctChanged(); void ramGbChanged(); void stateChanged(); void stateMessageChanged(); void latencyP50Changed(); + void metricsChanged(); + void gpuChanged(); private slots: void poll(); private: - int findOllamaPid(); - void sampleCpu(int pid); - void sampleRam(int pid); + QList findEnginePids(); + void sampleCpu(const QList &pids); + void sampleRam(const QList &pids); void checkFcitx5State(); + void startGpuQuery(); + void onGpuQueryFinished(); + void applyGpuSample(const GpuSample &g); + bool nvidiaGpuAwake() const; QTimer _timer; QProcess _stateProc; // reused for fcitx5-remote -s + QProcess _gpuProc; // nvidia-smi, run asynchronously (two queries chained) int _cpuPct = 0; double _ramGb = 0.0; QString _state = "paused"; QString _stateMessage; int _latencyP50 = 0; + qint64 _generated = 0; + qint64 _accepted = 0; + qint64 _charsAccepted = 0; + + bool _gpuAvailable = false; + int _gpuPct = 0; + double _vramGb = 0.0; + double _vramTotalGb = 0.0; + bool _gpuDisabled = false; // no NVIDIA device, or nvidia-smi missing + QStringList _nvidiaPciDevs; // /sys/bus/pci/devices/ of NVIDIA GPUs + QList _gpuQueryPids; // engine PIDs at the time the query started + QByteArray _gpuCsv; // first query's output while the second runs + int _gpuBusyTicks = 0; // polls skipped because nvidia-smi was still running - // CPU delta tracking - int _lastPid = -1; - uint64_t _lastCpuTicks = 0; + // CPU delta tracking, per PID so processes coming and going (model load / + // unload spawns and reaps the runner) don't produce bogus spikes. + QHash _lastCpuTicks; uint64_t _lastWallNs = 0; long _clkTck = 100; // sysconf(_SC_CLK_TCK), set in ctor + long _numCpus = 1; // sysconf(_SC_NPROCESSORS_ONLN), set in ctor bool _demoMetrics = false; }; diff --git a/settings/qt6-app/src/main.cpp b/settings/qt6-app/src/main.cpp index 2b969af..d08533a 100644 --- a/settings/qt6-app/src/main.cpp +++ b/settings/qt6-app/src/main.cpp @@ -70,6 +70,9 @@ int main(int argc, char** argv) { static AppsKnown appsKnown; configStore.load(); + corpusStats.setCorpusPath(configStore.corpusPath()); + QObject::connect(&configStore, &ConfigStore::corpusPathChanged, &corpusStats, + [] { corpusStats.setCorpusPath(configStore.corpusPath()); }); qmlRegisterSingletonInstance ("XType.Settings", 1, 0, "Config", &configStore); qmlRegisterSingletonInstance ("XType.Settings", 1, 0, "Engine", &engineProbe); qmlRegisterSingletonInstance ("XType.Settings", 1, 0, "Reloader", &reloader); diff --git a/settings/qt6-app/tests/CMakeLists.txt b/settings/qt6-app/tests/CMakeLists.txt index 04d1f26..c4c96da 100644 --- a/settings/qt6-app/tests/CMakeLists.txt +++ b/settings/qt6-app/tests/CMakeLists.txt @@ -2,7 +2,10 @@ find_package(Catch2 3 REQUIRED) add_executable(xtype-settings-tests test_config_store.cpp + test_stats.cpp ${CMAKE_SOURCE_DIR}/src/config_store.cpp + ${CMAKE_SOURCE_DIR}/src/corpus_stats.cpp + ${CMAKE_SOURCE_DIR}/src/engine_probe.cpp ) target_include_directories(xtype-settings-tests PRIVATE diff --git a/settings/qt6-app/tests/test_stats.cpp b/settings/qt6-app/tests/test_stats.cpp new file mode 100644 index 0000000..6be1bb7 --- /dev/null +++ b/settings/qt6-app/tests/test_stats.cpp @@ -0,0 +1,218 @@ +#include +#include + +#include +#include +#include +#include + +#include "corpus_stats.h" +#include "engine_probe.h" + +static void writeFile(const QString &path, const QByteArray &data) { + QFile f(path); + REQUIRE(f.open(QIODevice::WriteOnly | QIODevice::Truncate)); + f.write(data); +} + +static QByteArray stamp(const QDateTime &t) { + return QByteArray::number(t.toSecsSinceEpoch()) + '\n'; +} + +// ── CorpusStats::count ─────────────────────────────────────────────────────── + +TEST_CASE("count: missing corpus yields zeros and 7 empty day buckets", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), QDateTime::currentDateTime()); + CHECK(c.sentences == 0); + CHECK(c.words == 0); + CHECK(c.bytes == 0); + CHECK(c.wordsPerDay == QList(CorpusStats::kDays, 0)); +} + +TEST_CASE("count: sentences, words and bytes; blank lines are not sentences", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QByteArray data = "hello there world\n\n \nsecond line\twith tabs\n"; + writeFile(tmp.filePath("corpus.txt"), data); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), QDateTime::currentDateTime()); + CHECK(c.sentences == 2); + CHECK(c.words == 7); + CHECK(c.bytes == data.size()); +} + +TEST_CASE("count: UTF-8 words are counted as whole words", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + writeFile(tmp.filePath("corpus.txt"), "não sei se a configuração está certa\n"); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), QDateTime::currentDateTime()); + CHECK(c.words == 7); +} + +TEST_CASE("count: words are bucketed per day from the timestamps file", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QDateTime now(QDate(2026, 10, 14), QTime(12, 0)); + + writeFile(tmp.filePath("corpus.txt"), + "one two three\n" // today + "four five\n" // today + "six\n" // yesterday + "seven eight nine ten\n" // 6 days ago (oldest bucket) + "eleven twelve\n" // 7 days ago (outside window) + "thirteen\n" // malformed timestamp + "fourteen fifteen\n"); // no timestamp line at all + writeFile(tmp.filePath("corpus_timestamps.txt"), + stamp(now) + stamp(now.addSecs(-3600)) + stamp(now.addDays(-1)) + + stamp(now.addDays(-6)) + stamp(now.addDays(-7)) + "garbage\n"); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), now); + CHECK(c.words == 15); + CHECK(c.wordsPerDay == QList{4, 0, 0, 0, 0, 1, 5}); +} + +TEST_CASE("count: rotated files add to totals but not to day buckets; .tmp is ignored", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QDateTime now = QDateTime::currentDateTime(); + + writeFile(tmp.filePath("corpus.txt"), "live line here\n"); + writeFile(tmp.filePath("corpus_timestamps.txt"), stamp(now)); + writeFile(tmp.filePath("corpus.txt.1"), "rotated one\nrotated two\n"); + writeFile(tmp.filePath("corpus.txt.tmp"), "transient prune file\n"); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), now); + CHECK(c.sentences == 3); + CHECK(c.words == 7); + CHECK(c.wordsPerDay.last() == 3); +} + +TEST_CASE("wipeFiles: removes corpus, rotations, timestamps, scratch files and profile", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QStringList learned = { + "corpus.txt", "corpus.txt.1", "corpus.txt.tmp", + "corpus_timestamps.txt", "corpus_timestamps.txt.tmp", "style_profile.json", + }; + for (const QString &f : learned) writeFile(tmp.filePath(f), "x\n"); + // Engine state that is not learned text must survive. + for (const QString &f : {QString("metrics.json"), QString("fcitx5.log")}) + writeFile(tmp.filePath(f), "keep\n"); + + CHECK(CorpusStats::wipeFiles(tmp.filePath("corpus.txt")) == learned.size()); + for (const QString &f : learned) + CHECK_FALSE(QFile::exists(tmp.filePath(f))); + CHECK(QFile::exists(tmp.filePath("metrics.json"))); + CHECK(QFile::exists(tmp.filePath("fcitx5.log"))); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), QDateTime::currentDateTime()); + CHECK(c.words == 0); +} + +TEST_CASE("wipeFiles: nothing to delete is not an error", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + CHECK(CorpusStats::wipeFiles(tmp.filePath("corpus.txt")) == 0); +} + +TEST_CASE("wipeFiles: reports failure when a file cannot be removed", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + writeFile(tmp.filePath("corpus.txt"), "x\n"); + QFile dir(tmp.path()); + REQUIRE(dir.setPermissions(QFile::ReadOwner | QFile::ExeOwner)); // read-only dir + CHECK(CorpusStats::wipeFiles(tmp.filePath("corpus.txt")) == -1); + dir.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); +} + +// ── EngineProbe::parseMetrics ──────────────────────────────────────────────── + +TEST_CASE("parseMetrics: reads the counters the engine writes", "[engine_probe]") { + // Verbatim shape of XTypeEngine::writeMetrics() output. + const QByteArray json = + R"({"latency_p50_ms":110,"latency_p95_ms":292,"suggestions_generated":147,)" + R"("suggestions_accepted":14,"chars_accepted":47,"updated_at":1792011683})" "\n"; + + const auto m = EngineProbe::parseMetrics(json); + REQUIRE(m.has_value()); + CHECK(m->latencyP50 == 110); + CHECK(m->generated == 147); + CHECK(m->accepted == 14); + CHECK(m->charsAccepted == 47); +} + +TEST_CASE("parseMetrics: empty or truncated file is rejected", "[engine_probe]") { + CHECK_FALSE(EngineProbe::parseMetrics("").has_value()); + CHECK_FALSE(EngineProbe::parseMetrics(R"({"latency_p50_ms":11)").has_value()); +} + +TEST_CASE("time-saved estimate assumes 40 WPM", "[engine_probe]") { + // 200 accepted chars at 40 WPM x 5 chars/word = one minute of typing. + CHECK(200.0 / EngineProbe::kCharsPerSecond == 60.0); +} + +// ── EngineProbe::engineProcessTree ─────────────────────────────────────────── + +TEST_CASE("engineProcessTree: includes the model runner child of ollama", "[engine_probe]") { + // Real layout: `ollama serve` (idle) spawns `llama-server`, which holds the model. + const QList procs = { + { 1, 0, "systemd" }, + { 1399, 1, "ollama" }, + { 7073, 1399, "llama-server" }, + { 7074, 7073, "llama-helper" }, + { 500, 1, "llama-server" }, // unrelated, not under ollama + { 600, 1, "bash" }, + }; + QList pids = EngineProbe::engineProcessTree(procs); + std::sort(pids.begin(), pids.end()); + CHECK(pids == QList{ 1399, 7073, 7074 }); +} + +TEST_CASE("engineProcessTree: nested ollama runner is counted once", "[engine_probe]") { + const QList procs = { + { 10, 1, "ollama" }, // ollama serve + { 11, 10, "ollama" }, // ollama runner + }; + CHECK(EngineProbe::engineProcessTree(procs).size() == 2); +} + +TEST_CASE("engineProcessTree: empty when ollama is not running", "[engine_probe]") { + CHECK(EngineProbe::engineProcessTree({ { 1, 0, "systemd" } }).isEmpty()); +} + +TEST_CASE("EngineProbe reads a non-zero RSS from procfs", "[engine_probe]") { + // Demo mode samples this test process. Regression: looping on + // QFile::atEnd() over /proc//status read nothing (procfs size is 0). + qputenv("XTYPE_DEMO_METRICS", "1"); + EngineProbe probe; // ctor polls immediately + qunsetenv("XTYPE_DEMO_METRICS"); + CHECK(probe.ramGb() > 0.0); +} + +// ── EngineProbe::summarizeGpu ──────────────────────────────────────────────── + +TEST_CASE("summarizeGpu: counts only the engine's VRAM and its GPU's load", "[engine_probe]") { + // Verbatim nvidia-smi output shape (csv,noheader,nounits). + const QByteArray gpus = + "GPU-aaa, 25, 6144\n" + "GPU-bbb, 90, 8192\n"; + const QByteArray apps = + "GPU-aaa, 17487, 3122\n" // llama-server (engine) + "GPU-bbb, 900, 4096\n"; // some game on the other GPU + const auto g = EngineProbe::summarizeGpu(gpus, apps, { 1399, 17487 }); + REQUIRE(g.valid); + CHECK(g.pct == 25); + CHECK(g.vramGb == Catch::Approx(3122.0 / 1024.0)); + CHECK(g.vramTotalGb == Catch::Approx(6.0)); +} + +TEST_CASE("summarizeGpu: model not on the GPU reports zero load", "[engine_probe]") { + const auto g = EngineProbe::summarizeGpu("GPU-aaa, 40, 6144\n", "", { 1399 }); + REQUIRE(g.valid); + CHECK(g.pct == 0); + CHECK(g.vramGb == 0.0); + CHECK(g.vramTotalGb == Catch::Approx(6.0)); // bar still has a scale +} + +TEST_CASE("summarizeGpu: tolerates [N/A] fields and empty output", "[engine_probe]") { + const auto g = EngineProbe::summarizeGpu("GPU-aaa, [N/A], 6144\n", "GPU-aaa, 17487, [N/A]\n", { 17487 }); + REQUIRE(g.valid); + CHECK(g.pct == 0); + CHECK(g.vramGb == 0.0); + CHECK_FALSE(EngineProbe::summarizeGpu("", "", { 1 }).valid); +}