fabric/utils/resourceinfo.h

fabric/utils/resourceinfo.h

fabric/utils/resourceinfo.h

Namespaces

Name
Syntalos

Classes

Name
structSyntalos::MemInfo
structSyntalos::MemPressure
Memory pressure stall information as reported by the kernel (PSI).
structSyntalos::DiskSpaceInfo
Information about the storage a path is located on.
structSyntalos::ThreadUsageStats
Resource usage of a single OS thread (or a whole process).
structSyntalos::ProcessSchedInfo
Strongest scheduling priority found among all threads of a process.
classSyntalos::ResourceTrend
Estimate how fast a resource is being used up.

Source code

/*
 * Copyright (C) 2019-2026 Matthias Klumpp <matthias@tenstral.net>
 *
 * Licensed under the GNU Lesser General Public License Version 3
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the license, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <QString>
#include <QElapsedTimer>
#include <QtGlobal>
#include <expected>
#include <optional>

namespace Syntalos
{

typedef struct {
    long long memTotalKiB;
    long long memAvailableKiB;
    long long memAvailableMiB;
    double memAvailablePercent;
    long long swapTotalKiB;
    long long swapFreeKiB;
} MemInfo;

typedef struct {
    bool available; 
    double someAvg10;
    double someAvg60;
    double fullAvg10;
    double fullAvg60;
} MemPressure;

MemInfo readMemInfo();
MemPressure readMemPressure();

struct DiskSpaceInfo {
    bool valid = false;         
    quint64 deviceId = 0;       
    QString mountPoint;         
    qint64 bytesAvailable = -1; 
    qint64 bytesTotal = -1;
};

DiskSpaceInfo diskSpaceInfo(const QString &path);

qint64 directoryTotalSize(const QString &path);

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpadded"
struct ThreadUsageStats {
    int64_t tid = 0;
    double userTimeSec = 0.0;
    double systemTimeSec = 0.0;
    uint64_t voluntaryCtxSwitches = 0;
    uint64_t involuntaryCtxSwitches = 0;
    uint64_t minorPageFaults = 0;
    uint64_t majorPageFaults = 0;
    int64_t peakRssKiB = -1; 

    double cpuTimeSec() const
    {
        return userTimeSec + systemTimeSec;
    }

    [[nodiscard]] ThreadUsageStats diff(const ThreadUsageStats &baseline) const;
};

std::optional<ThreadUsageStats> captureCurrentThreadUsage();

std::optional<ThreadUsageStats> captureProcessUsage();

std::optional<ThreadUsageStats> readProcessUsage(qint64 pid);

qint64 readProcessTreeRssKiB(qint64 pid);

auto raiseFdLimit() -> std::expected<std::pair<uint64_t, uint64_t>, std::string>;

qint64 readProcessTreePssKiB(qint64 pid);

struct ProcessSchedInfo {
    bool realtime = false; 
    int minNiceness = 0;   
};

std::optional<ProcessSchedInfo> readProcessSchedInfo(qint64 pid);

#pragma GCC diagnostic pop

class ResourceTrend
{
public:
    explicit ResourceTrend(double minRelevantRate = 0.0, double smoothing = 0.5);

    void reset();

    void addSample(qint64 remaining);

    void addSample(qint64 remaining, double elapsedSec);

    bool hasRate() const;

    double consumptionRate() const;

    double secondsUntilDepleted() const;

    qint64 lastRemaining() const;

private:
    double m_minRelevantRate;
    double m_smoothing;
    qint64 m_lastRemaining;
    double m_rate;
    QElapsedTimer m_timer;
};

} // namespace Syntalos

Updated on 2026-11-26 at 18:54:14 +0000