File: gpu.h

package info (click to toggle)
libksysguard 4%3A6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,596 kB
  • sloc: cpp: 13,691; xml: 297; sh: 23; makefile: 11
file content (78 lines) | stat: -rw-r--r-- 2,063 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
    SPDX-FileCopyrightText: 2022 Lenon Kitchens <lenon.kitchens@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#pragma once

#include <QProcess>

#include <chrono>
#include <filesystem>
#include <unordered_map>
#include <vector>

#include <processcore/process_attribute.h>
#include <processcore/process_data_provider.h>

namespace fs = std::filesystem;

struct GpuFd {
    uint64_t gfx{0};
    uint32_t vram{0};
    std::chrono::high_resolution_clock::time_point ts;
};

struct HistoryKey {
    pid_t pid;
    uint64_t deviceMinor;
    friend bool operator==(const HistoryKey &, const HistoryKey &) = default;
};

struct NvidiaValues {
    unsigned int usage = 0;
    unsigned int vram = 0;
};

struct GpuInfo {
    std::string pciAdress;
    unsigned int deviceMinor;
};

template<>
struct std::hash<HistoryKey> {
    std::size_t operator()(const HistoryKey &key, size_t seed = 0) const noexcept
    {
        return qHashMulti(seed, key.pid, key.deviceMinor);
    }
};

class GpuPlugin : public KSysGuard::ProcessDataProvider
{
    Q_OBJECT
public:
    GpuPlugin(QObject *parent, const QVariantList &args);
    ~GpuPlugin();
    void handleEnabledChanged(bool enabled) override;
    void update() override;
    void readNvidiaData();

private:
    KSysGuard::ProcessAttribute *m_usage = nullptr;
    KSysGuard::ProcessAttribute *m_memory = nullptr;
    KSysGuard::ProcessAttribute *m_gpuName = nullptr;

    bool m_enabled = false;
    QString m_sniExecutablePath;
    QProcess *m_nvidiaSmiProcess = nullptr;

    std::unordered_map<HistoryKey, GpuFd> m_process_history;
    std::unordered_map<HistoryKey, NvidiaValues> m_currentNvidiaValues;
    std::unordered_map<unsigned int, unsigned int> m_minorToGpuNum;
    std::unordered_map<unsigned int, unsigned int> m_nvidiaIndexToGpuNum;

    void processPidDir(const fs::path &path, KSysGuard::Process *proc, const std::unordered_map<HistoryKey, GpuFd> &previousValues);
    bool processPidEntry(const fs::path &path, GpuFd &proc);
    void setupNvidia(const std::vector<GpuInfo> &gpuInfo);
};