File: linuxbackend.cpp

package info (click to toggle)
ksystemstats 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: cpp: 4,881; makefile: 6; sh: 1
file content (67 lines) | stat: -rw-r--r-- 2,212 bytes parent folder | download | duplicates (3)
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

/*
    SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include "linuxbackend.h"

#include <systemstats/SensorObject.h>
#include <systemstats/SensorProperty.h>

#include <QFile>

LinuxMemoryBackend::LinuxMemoryBackend(KSysGuard::SensorContainer *container)
    : MemoryBackend(container)
{
}

void LinuxMemoryBackend::update()
{
    if (!m_physicalObject->isSubscribed() && !m_swapObject->isSubscribed()) {
        return;
    }

    QFile meminfo(QStringLiteral("/proc/meminfo"));
    meminfo.open(QIODevice::ReadOnly);
    // The format of the file is as follows:
    // Fieldname:[whitespace]value kB
    // A description of the fields can be found at 
    // https://www.kernel.org/doc/html/latest/filesystems/proc.html#meminfo
    unsigned long long total, free, available, buffer, cache, slab, swapTotal, swapFree;
    for (QByteArray line = meminfo.readLine(); !line.isEmpty(); line = meminfo.readLine()) {
        int colonIndex = line.indexOf(':');
        const auto fields = line.simplified().split(' ');

        const QByteArray name = line.left(colonIndex);
        const unsigned long long value = std::strtoull(line.mid(colonIndex + 1), nullptr, 10) * 1024;
        if (name == "MemTotal") {
            total = value;
        } else if (name == "MemFree") {
            free = value;
        } else if (name == "MemAvailable") {
            available = value;
        } else if (name == "Buffers") {
            buffer = value;
        } else if (name == "Cached") {
            cache = value;
        } else if (name == "Slab") {
            slab = value;
        } else if (name == "SwapTotal") {
            swapTotal = value;
        } else if (name == "SwapFree") {
            swapFree = value;
        }
    }
    m_total->setValue(total);
    m_used->setValue(total - available);
    m_free->setValue(available);
    m_application->setValue(total - free - cache - buffer - slab);
    m_cache->setValue(cache + slab);
    m_buffer->setValue(buffer);
    m_swapTotal->setValue(swapTotal);
    m_swapUsed->setValue(swapTotal - swapFree);
    m_swapFree->setValue(swapFree);
}