fix: column index desync after reorder
This commit is contained in:
@@ -2,8 +2,13 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <qbytearray.h>
|
||||
@@ -12,6 +17,7 @@
|
||||
#include <qfile.h>
|
||||
#include <qfuturewatcher.h>
|
||||
#include <qloggingcategory.h>
|
||||
#include <qthreadpool.h>
|
||||
#include <qregularexpression.h>
|
||||
#include <qset.h>
|
||||
#include <QtConcurrent/qtconcurrentrun.h>
|
||||
@@ -53,6 +59,82 @@ template <typename T> int cmpVals(const T& a, const T& b) {
|
||||
return (a < b) - (a > b);
|
||||
}
|
||||
|
||||
// glibc hides getdents64 behind __USE_MISC; declare directly (no _GNU_SOURCE
|
||||
// in this project).
|
||||
extern "C" {
|
||||
struct ProcDirEnt64 {
|
||||
unsigned long d_ino;
|
||||
off_t d_off;
|
||||
unsigned short d_reclen;
|
||||
unsigned char d_type;
|
||||
char d_name[1];
|
||||
};
|
||||
|
||||
ssize_t getdents64(int fd, void* dirp, size_t count);
|
||||
}
|
||||
|
||||
// Raw open/read/close instead of QFile: QFSFileEngine issues an extra
|
||||
// statx per open, and the per-file object churn adds up across ~500
|
||||
// processes per refresh.
|
||||
QByteArray readProcFile(const char* path) {
|
||||
const int fd = ::open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
return {};
|
||||
}
|
||||
QByteArray data;
|
||||
char buf[4096];
|
||||
for (;;) {
|
||||
const ssize_t n = ::read(fd, buf, sizeof(buf));
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (n == 0) {
|
||||
break;
|
||||
}
|
||||
data.append(buf, static_cast<qsizetype>(n));
|
||||
}
|
||||
::close(fd);
|
||||
return data;
|
||||
}
|
||||
|
||||
void procPath(char* out, size_t size, pid_t pid, const char* what) {
|
||||
::snprintf(out, size, "/proc/%d/%s", static_cast<int>(pid), what);
|
||||
}
|
||||
|
||||
QVector<pid_t> listProcPids() {
|
||||
QVector<pid_t> pids;
|
||||
const int fd = ::openat(AT_FDCWD, "/proc", O_RDONLY | O_DIRECTORY);
|
||||
if (fd < 0) {
|
||||
return pids;
|
||||
}
|
||||
|
||||
std::array<char, 256 * 1024> buf;
|
||||
for (;;) {
|
||||
const ssize_t n = ::getdents64(
|
||||
fd, reinterpret_cast<ProcDirEnt64*>(buf.data()), buf.size());
|
||||
if (n <= 0) {
|
||||
break;
|
||||
}
|
||||
for (ssize_t off = 0; off < n;) {
|
||||
const auto* d =
|
||||
reinterpret_cast<const ProcDirEnt64*>(buf.data() + off);
|
||||
if (d->d_type == DT_DIR) {
|
||||
bool ok = false;
|
||||
const long v = QByteArray(d->d_name).toLong(&ok);
|
||||
if (ok && v > 0) {
|
||||
pids.push_back(static_cast<pid_t>(v));
|
||||
}
|
||||
}
|
||||
off += static_cast<ssize_t>(d->d_reclen);
|
||||
}
|
||||
}
|
||||
::close(fd);
|
||||
return pids;
|
||||
}
|
||||
|
||||
|
||||
struct FuzzyText {
|
||||
QString chars;
|
||||
@@ -460,19 +542,24 @@ void ProcessModel::setDesktopEntryIcons(
|
||||
}
|
||||
|
||||
QString ProcessModel::resolveProcessExe(pid_t pid) {
|
||||
const QString path = QStringLiteral("/proc/%1/exe").arg(pid);
|
||||
const QString target = QFile::symLinkTarget(path);
|
||||
|
||||
return target;
|
||||
char path[40];
|
||||
procPath(path, sizeof(path), pid, "exe");
|
||||
char buf[4096];
|
||||
const ssize_t n = ::readlink(path, buf, sizeof(buf) - 1);
|
||||
if (n < 0) {
|
||||
return {};
|
||||
}
|
||||
buf[n] = '\0';
|
||||
return QString::fromLocal8Bit(buf, static_cast<int>(n));
|
||||
}
|
||||
|
||||
std::optional<ProcessModel::RawStat> ProcessModel::readStat(pid_t pid) {
|
||||
QFile f(QStringLiteral("/proc/%1/stat").arg(pid));
|
||||
if (!f.open(QIODevice::ReadOnly)) {
|
||||
char path[40];
|
||||
procPath(path, sizeof(path), pid, "stat");
|
||||
const QByteArray data = readProcFile(path);
|
||||
if (data.isEmpty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const QByteArray data = f.readAll();
|
||||
f.close();
|
||||
|
||||
const qsizetype openParen = data.indexOf('(');
|
||||
const qsizetype closeParen = data.lastIndexOf(')');
|
||||
@@ -500,12 +587,9 @@ std::optional<ProcessModel::RawStat> ProcessModel::readStat(pid_t pid) {
|
||||
}
|
||||
|
||||
QString ProcessModel::readCommandLine(pid_t pid, const QString& fallbackComm) {
|
||||
QFile f(QStringLiteral("/proc/%1/cmdline").arg(pid));
|
||||
if (!f.open(QIODevice::ReadOnly)) {
|
||||
return QStringLiteral("[%1]").arg(fallbackComm);
|
||||
}
|
||||
QByteArray data = f.readAll();
|
||||
f.close();
|
||||
char path[40];
|
||||
procPath(path, sizeof(path), pid, "cmdline");
|
||||
QByteArray data = readProcFile(path);
|
||||
|
||||
if (data.isEmpty()) {
|
||||
return QStringLiteral("[%1]").arg(fallbackComm);
|
||||
@@ -523,12 +607,9 @@ QString ProcessModel::readCommandLine(pid_t pid, const QString& fallbackComm) {
|
||||
}
|
||||
|
||||
ProcessModel::StatusInfo ProcessModel::readStatus(pid_t pid) {
|
||||
QFile f(QStringLiteral("/proc/%1/status").arg(pid));
|
||||
if (!f.open(QIODevice::ReadOnly)) {
|
||||
return {};
|
||||
}
|
||||
const QByteArray data = f.readAll();
|
||||
f.close();
|
||||
char path[40];
|
||||
procPath(path, sizeof(path), pid, "status");
|
||||
const QByteArray data = readProcFile(path);
|
||||
|
||||
StatusInfo out;
|
||||
qsizetype pos = 0;
|
||||
@@ -554,12 +635,9 @@ ProcessModel::StatusInfo ProcessModel::readStatus(pid_t pid) {
|
||||
}
|
||||
|
||||
ProcessModel::IoInfo ProcessModel::readIo(pid_t pid) {
|
||||
QFile f(QStringLiteral("/proc/%1/io").arg(pid));
|
||||
if (!f.open(QIODevice::ReadOnly)) {
|
||||
return {};
|
||||
}
|
||||
const QByteArray data = f.readAll();
|
||||
f.close();
|
||||
char path[40];
|
||||
procPath(path, sizeof(path), pid, "io");
|
||||
const QByteArray data = readProcFile(path);
|
||||
|
||||
IoInfo out;
|
||||
qsizetype pos = 0;
|
||||
@@ -606,12 +684,10 @@ QString ProcessModel::resolveUsername(uid_t uid, QHash<uid_t, QString>& cache) {
|
||||
}
|
||||
|
||||
std::optional<double> ProcessModel::readUptimeSeconds() {
|
||||
QFile f(QStringLiteral("/proc/uptime"));
|
||||
if (!f.open(QIODevice::ReadOnly)) {
|
||||
const QByteArray data = readProcFile("/proc/uptime");
|
||||
if (data.isEmpty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const QByteArray data = f.readAll();
|
||||
f.close();
|
||||
|
||||
const auto fields = splitWhitespace(QByteArrayView(data));
|
||||
if (fields.isEmpty()) {
|
||||
@@ -623,35 +699,17 @@ std::optional<double> ProcessModel::readUptimeSeconds() {
|
||||
return ok ? std::make_optional(uptime) : std::nullopt;
|
||||
}
|
||||
|
||||
ProcessModel::ScanResult ProcessModel::scanProcesses(
|
||||
double uptime,
|
||||
double deltaSecs,
|
||||
QHash<pid_t, int> prevPidToAll,
|
||||
QVector<ProcessEntry> prevAll,
|
||||
QHash<uid_t, QString> uidCache,
|
||||
long clockTicksPerSec,
|
||||
QHash<QString, QString> execPathToIcon,
|
||||
QHash<QString, QString> execNameToIcon,
|
||||
QStringList pathDirs,
|
||||
bool desktopEntryIconsChanged,
|
||||
quint64 iconVersion) {
|
||||
Q_UNUSED(
|
||||
pathDirs); // kept for future cgroup fallback; sanitizeExec already ran on GUI thread
|
||||
|
||||
QDir procDir(QStringLiteral("/proc"));
|
||||
const auto entries =
|
||||
procDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::NoSort);
|
||||
|
||||
QVector<ProcessEntry> fresh;
|
||||
fresh.reserve(entries.size());
|
||||
|
||||
for (const QString& name : entries) {
|
||||
bool isPid = false;
|
||||
const pid_t pid = static_cast<pid_t>(name.toLong(&isPid));
|
||||
if (!isPid || pid <= 0) {
|
||||
continue;
|
||||
}
|
||||
void ProcessModel::scanChunk(
|
||||
const ScanContext& ctx,
|
||||
const QVector<pid_t>& pids,
|
||||
QVector<ProcessEntry>& out,
|
||||
QHash<uid_t, QString>& uidCache) {
|
||||
const QHash<pid_t, int>& prevPidToAll = *ctx.prevPidToAll;
|
||||
const QVector<ProcessEntry>& prevAll = *ctx.prevAll;
|
||||
const QHash<QString, QString>& execPathToIcon = *ctx.execPathToIcon;
|
||||
const QHash<QString, QString>& execNameToIcon = *ctx.execNameToIcon;
|
||||
|
||||
for (pid_t pid : pids) {
|
||||
const auto statOpt = readStat(pid);
|
||||
if (!statOpt) {
|
||||
continue;
|
||||
@@ -675,7 +733,7 @@ ProcessModel::ScanResult ProcessModel::scanProcesses(
|
||||
entry.commandLine = readCommandLine(pid, stat.comm);
|
||||
}
|
||||
|
||||
if (!samePidInstance || desktopEntryIconsChanged) {
|
||||
if (!samePidInstance || ctx.iconsChanged) {
|
||||
const QString exePath = resolveProcessExe(pid);
|
||||
|
||||
QString iconName;
|
||||
@@ -711,18 +769,23 @@ ProcessModel::ScanResult ProcessModel::scanProcesses(
|
||||
entry.uid = status.uid;
|
||||
entry.username = resolveUsername(status.uid, uidCache);
|
||||
|
||||
const IoInfo io = readIo(pid);
|
||||
// /proc/PID/io is only readable by root or the process owner; skip
|
||||
// the open when it is guaranteed to fail.
|
||||
IoInfo io;
|
||||
if (ctx.selfEuid == 0 || status.uid == ctx.selfEuid) {
|
||||
io = readIo(pid);
|
||||
}
|
||||
entry.ioReadBytes = io.readBytes;
|
||||
entry.ioWriteBytes = io.writeBytes;
|
||||
|
||||
entry.elapsedSecs = std::max(
|
||||
0.0,
|
||||
uptime - (static_cast<double>(stat.starttime) /
|
||||
static_cast<double>(clockTicksPerSec)));
|
||||
ctx.uptime - (static_cast<double>(stat.starttime) /
|
||||
static_cast<double>(ctx.clockTicksPerSec)));
|
||||
entry.lastUtime = stat.utime;
|
||||
entry.lastStime = stat.stime;
|
||||
|
||||
if (samePidInstance && deltaSecs > 0.0) {
|
||||
if (samePidInstance && ctx.deltaSecs > 0.0) {
|
||||
const ProcessEntry& prev = prevAll.at(prevIt.value());
|
||||
const quint64 prevTicks = prev.lastUtime + prev.lastStime;
|
||||
const quint64 nowTicks = stat.utime + stat.stime;
|
||||
@@ -730,13 +793,97 @@ ProcessModel::ScanResult ProcessModel::scanProcesses(
|
||||
nowTicks > prevTicks ? nowTicks - prevTicks : 0;
|
||||
entry.cpuPercent =
|
||||
(static_cast<double>(deltaTicks) /
|
||||
(deltaSecs * static_cast<double>(clockTicksPerSec))) *
|
||||
(ctx.deltaSecs *
|
||||
static_cast<double>(ctx.clockTicksPerSec))) *
|
||||
100.0;
|
||||
} else {
|
||||
entry.cpuPercent = 0.0;
|
||||
}
|
||||
|
||||
fresh.push_back(std::move(entry));
|
||||
out.push_back(std::move(entry));
|
||||
}
|
||||
}
|
||||
|
||||
ProcessModel::ScanResult ProcessModel::scanProcesses(
|
||||
double uptime,
|
||||
double deltaSecs,
|
||||
QHash<pid_t, int> prevPidToAll,
|
||||
QVector<ProcessEntry> prevAll,
|
||||
QHash<uid_t, QString> uidCache,
|
||||
long clockTicksPerSec,
|
||||
QHash<QString, QString> execPathToIcon,
|
||||
QHash<QString, QString> execNameToIcon,
|
||||
QStringList pathDirs,
|
||||
bool desktopEntryIconsChanged,
|
||||
quint64 iconVersion) {
|
||||
Q_UNUSED(
|
||||
pathDirs); // kept for future cgroup fallback; sanitizeExec already ran on GUI thread
|
||||
|
||||
const QVector<pid_t> pids = listProcPids();
|
||||
|
||||
ScanContext ctx;
|
||||
ctx.uptime = uptime;
|
||||
ctx.deltaSecs = deltaSecs;
|
||||
ctx.prevPidToAll = &prevPidToAll;
|
||||
ctx.prevAll = &prevAll;
|
||||
ctx.clockTicksPerSec = clockTicksPerSec;
|
||||
ctx.execPathToIcon = &execPathToIcon;
|
||||
ctx.execNameToIcon = &execNameToIcon;
|
||||
ctx.iconsChanged = desktopEntryIconsChanged;
|
||||
ctx.selfEuid = ::geteuid();
|
||||
|
||||
QVector<ProcessEntry> fresh;
|
||||
fresh.reserve(pids.size());
|
||||
|
||||
// Split the pid list across worker threads; a single pass over /proc is
|
||||
// syscall-bound (open/read/close per file per process) and serializes
|
||||
// badly on large systems. Keep one pool thread free for this function,
|
||||
// which itself runs inside the global pool.
|
||||
int nThreads = qMin(16, QThreadPool::globalInstance()->maxThreadCount() - 1);
|
||||
if (pids.size() < 128 || nThreads < 2) {
|
||||
nThreads = 1;
|
||||
}
|
||||
|
||||
if (nThreads <= 1) {
|
||||
scanChunk(ctx, pids, fresh, uidCache);
|
||||
} else {
|
||||
const qsizetype per = (pids.size() + nThreads - 1) / nThreads;
|
||||
QVector<QVector<pid_t>> chunks(nThreads);
|
||||
QVector<QVector<ProcessEntry>> outs(nThreads);
|
||||
QVector<QHash<uid_t, QString>> uidParts(nThreads);
|
||||
QVector<QFuture<void>> futures;
|
||||
|
||||
for (int t = 0; t < nThreads; ++t) {
|
||||
const qsizetype a = static_cast<qsizetype>(t) * per;
|
||||
const qsizetype b = qMin<qsizetype>(a + per, pids.size());
|
||||
if (a >= b) {
|
||||
continue;
|
||||
}
|
||||
chunks[t] = pids.mid(a, b - a);
|
||||
futures.append(QtConcurrent::run(
|
||||
[&ctx, &chunks, &outs, &uidParts, t]() {
|
||||
scanChunk(ctx, chunks[t], outs[t], uidParts[t]);
|
||||
}));
|
||||
}
|
||||
for (QFuture<void>& f : futures) {
|
||||
f.waitForFinished();
|
||||
}
|
||||
|
||||
for (qsizetype t = 0; t < nThreads; ++t) {
|
||||
for (ProcessEntry& e : outs[t]) {
|
||||
fresh.push_back(std::move(e));
|
||||
}
|
||||
for (auto it = uidParts[t].constBegin();
|
||||
it != uidParts[t].constEnd();
|
||||
++it) {
|
||||
uidCache.insert(it.key(), it.value());
|
||||
}
|
||||
}
|
||||
std::sort(
|
||||
fresh.begin(), fresh.end(), [](const ProcessEntry& a,
|
||||
const ProcessEntry& b) {
|
||||
return a.pid < b.pid;
|
||||
});
|
||||
}
|
||||
|
||||
ScanResult result;
|
||||
@@ -810,6 +957,13 @@ void ProcessModel::onScanFinished() {
|
||||
for (int i = 0; i < mAll.size(); ++i) {
|
||||
mPidToAll.insert(mAll.at(i).pid, i);
|
||||
}
|
||||
for (auto it = mFuzzyCache.begin(); it != mFuzzyCache.end();) {
|
||||
if (mPidToAll.contains(it.key())) {
|
||||
++it;
|
||||
} else {
|
||||
it = mFuzzyCache.erase(it);
|
||||
}
|
||||
}
|
||||
for (auto it = result.newUidNames.constBegin();
|
||||
it != result.newUidNames.constEnd();
|
||||
++it) {
|
||||
@@ -846,11 +1000,35 @@ void ProcessModel::applyView() {
|
||||
|
||||
ProcessEntry entry = src;
|
||||
if (searching) {
|
||||
int score = fuzzyQueryScore(fuzzyPrepare(src.command), terms);
|
||||
if (!src.commandLine.isEmpty() && src.commandLine != src.command) {
|
||||
score = qMax(
|
||||
score,
|
||||
fuzzyQueryScore(fuzzyPrepare(src.commandLine), terms));
|
||||
// Normalizing (NFD) + lowering per process per tick is the
|
||||
// expensive part of search; commands are stable for the life of
|
||||
// a process instance (pid + starttime), so cache the prepared
|
||||
// text. comm can still change without an exec (prctl), so
|
||||
// re-prepare when the raw command differs.
|
||||
FuzzyCacheEntry& fe = mFuzzyCache[src.pid];
|
||||
if (fe.starttime != src.starttime || fe.command != src.command) {
|
||||
fe.starttime = src.starttime;
|
||||
fe.command = src.command;
|
||||
const FuzzyText prep = fuzzyPrepare(src.command);
|
||||
fe.commandChars = std::move(prep.chars);
|
||||
fe.commandClasses = std::move(prep.classes);
|
||||
fe.hasCommandLine =
|
||||
!src.commandLine.isEmpty() && src.commandLine != src.command;
|
||||
if (fe.hasCommandLine) {
|
||||
const FuzzyText prepCl = fuzzyPrepare(src.commandLine);
|
||||
fe.commandLineChars = std::move(prepCl.chars);
|
||||
fe.commandLineClasses = std::move(prepCl.classes);
|
||||
} else {
|
||||
fe.commandLineChars = QString();
|
||||
fe.commandLineClasses = {};
|
||||
}
|
||||
}
|
||||
const FuzzyText cmdFuzzy{fe.commandChars, fe.commandClasses};
|
||||
int score = fuzzyQueryScore(cmdFuzzy, terms);
|
||||
if (fe.hasCommandLine) {
|
||||
const FuzzyText clFuzzy{
|
||||
fe.commandLineChars, fe.commandLineClasses};
|
||||
score = qMax(score, fuzzyQueryScore(clFuzzy, terms));
|
||||
}
|
||||
if (score < 0) {
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user