1 /*
2     SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
3 
4     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6 
7 #include "backend.h"
8 
9 #include <systemstats/AggregateSensor.h>
10 #include <systemstats/SensorObject.h>
11 #include <systemstats/SensorProperty.h>
12 
13 #include <KLocalizedString>
14 
MemoryBackend(KSysGuard::SensorContainer * container)15 MemoryBackend::MemoryBackend(KSysGuard::SensorContainer *container)
16 {
17     m_physicalObject = new KSysGuard::SensorObject(QStringLiteral("physical"), i18nc("@title", "Physical Memory"), container);
18     m_swapObject = new KSysGuard::SensorObject(QStringLiteral("swap"), i18nc("@title", "Swap Memory"), container);
19 }
20 
makeSensors()21 void MemoryBackend::makeSensors()
22 {
23     m_total = new KSysGuard::SensorProperty(QStringLiteral("total"), m_physicalObject);
24     m_used = new KSysGuard::SensorProperty(QStringLiteral("used"), m_physicalObject);
25     m_free = new KSysGuard::SensorProperty(QStringLiteral("free"), m_physicalObject);
26     m_application = new KSysGuard::SensorProperty(QStringLiteral("application"), m_physicalObject);
27     m_cache = new KSysGuard::SensorProperty(QStringLiteral("cache"), m_physicalObject);
28     m_buffer = new KSysGuard::SensorProperty(QStringLiteral("buffer"), m_physicalObject);
29 
30     m_swapTotal = new KSysGuard::SensorProperty(QStringLiteral("total"), m_swapObject);
31     m_swapUsed = new KSysGuard::SensorProperty(QStringLiteral("used"), m_swapObject);
32     m_swapFree = new KSysGuard::SensorProperty(QStringLiteral("free"), m_swapObject);
33 }
34 
initSensors()35 void MemoryBackend::initSensors()
36 {
37     makeSensors();
38 
39     m_total->setName(i18nc("@title", "Total Physical Memory"));
40     m_total->setShortName(i18nc("@title, Short for 'Total Physical Memory'", "Total"));
41     m_total->setUnit(KSysGuard::UnitByte);
42     m_total->setVariantType(QVariant::ULongLong);
43 
44     m_used->setName(i18nc("@title", "Used Physical Memory"));
45     m_used->setShortName(i18nc("@title, Short for 'Used Physical Memory'", "Used"));
46     m_used->setUnit(KSysGuard::UnitByte);
47     m_used->setVariantType(QVariant::ULongLong);
48     m_used->setMax(m_total);
49     auto usedPercentage = new KSysGuard::PercentageSensor(m_physicalObject, QStringLiteral("usedPercent"), i18nc("@title", "Used Physical Memory Percentage"));
50     usedPercentage->setShortName(m_used->info().shortName);
51     usedPercentage->setBaseSensor(m_used);
52 
53     m_free->setName(i18nc("@title", "Free Physical Memory"));
54     m_free->setShortName(i18nc("@title, Short for 'Free Physical Memory'", "Free"));
55     m_free->setUnit(KSysGuard::UnitByte);
56     m_free->setVariantType(QVariant::ULongLong);
57     m_free->setMax(m_total);
58     auto freePercentage = new KSysGuard::PercentageSensor(m_physicalObject, QStringLiteral("freePercent"), i18nc("@title", "Free Physical Memory Percentage"));
59     freePercentage->setShortName(m_free->info().shortName);
60     freePercentage->setBaseSensor(m_free);
61 
62     m_application->setName(i18nc("@title", "Application Memory"));
63     m_application->setShortName(i18nc("@title, Short for 'Application Memory'", "Application"));
64     m_application->setUnit(KSysGuard::UnitByte);
65     m_application->setVariantType(QVariant::ULongLong);
66     m_application->setMax(m_total);
67     auto applicationPercentage = new KSysGuard::PercentageSensor(m_physicalObject, QStringLiteral("applicationPercent"), i18nc("@title", "Application Memory Percentage"));
68     applicationPercentage->setShortName(m_application->info().shortName);
69     applicationPercentage->setBaseSensor(m_application);
70 
71     m_cache->setName(i18nc("@title", "Cache Memory"));
72     m_cache->setShortName(i18nc("@title, Short for 'Cache Memory'", "Cache"));
73     m_cache->setUnit(KSysGuard::UnitByte);
74     m_cache->setVariantType(QVariant::ULongLong);
75     m_cache->setMax(m_total);
76     auto cachePercentage = new KSysGuard::PercentageSensor(m_physicalObject, QStringLiteral("cachePercent"), i18nc("@title", "Cache Memory Percentage"));
77     cachePercentage->setShortName(m_cache->info().shortName);
78     cachePercentage->setBaseSensor(m_cache);
79 
80     m_buffer->setName(i18nc("@title", "Buffer Memory"));
81     m_buffer->setShortName(i18nc("@title, Short for 'Buffer Memory'", "Buffer"));
82     m_buffer->setDescription(i18n("Amount of memory used for caching disk blocks"));
83     m_buffer->setUnit(KSysGuard::UnitByte);
84     m_buffer->setVariantType(QVariant::ULongLong);
85     m_buffer->setMax(m_total);
86     auto bufferPercentage = new KSysGuard::PercentageSensor(m_physicalObject, QStringLiteral("bufferPercent"), i18nc("@title", "Buffer Memory Percentage"));
87     bufferPercentage->setShortName(m_buffer->info().shortName);
88     bufferPercentage->setBaseSensor(m_buffer);
89 
90     m_swapTotal->setName(i18nc("@title", "Total Swap Memory"));
91     m_swapTotal->setShortName(i18nc("@title, Short for 'Total Swap Memory'", "Total"));
92     m_swapTotal->setUnit(KSysGuard::UnitByte);
93     m_swapTotal->setVariantType(QVariant::ULongLong);
94 
95     m_swapUsed->setName(i18nc("@title", "Used Swap Memory"));
96     m_swapUsed->setShortName(i18nc("@title, Short for 'Used Swap Memory'", "Used"));
97     m_swapUsed->setUnit(KSysGuard::UnitByte);
98     m_swapUsed->setVariantType(QVariant::ULongLong);
99     m_swapUsed->setMax(m_swapTotal);
100     auto usedSwapPercentage = new KSysGuard::PercentageSensor(m_swapObject, QStringLiteral("usedPercent"), i18nc("@title", "Used Swap Memory Percentage"));
101     usedSwapPercentage->setShortName(m_swapUsed->info().shortName);
102     usedSwapPercentage->setBaseSensor(m_swapUsed);
103 
104     m_swapFree->setName(i18nc("@title", "Free Swap Memory"));
105     m_swapFree->setShortName(i18nc("@title, Short for 'Free Swap Memory'", "Free"));
106     m_swapFree->setUnit(KSysGuard::UnitByte);
107     m_swapFree->setVariantType(QVariant::ULongLong);
108     m_swapFree->setMax(m_swapTotal);
109     auto freeSwapPercentage = new KSysGuard::PercentageSensor(m_swapObject, QStringLiteral("freePercent"), i18nc("@title", "Free Swap Memory Percentage"));
110     freeSwapPercentage->setShortName(m_swapFree->info().shortName);
111     freeSwapPercentage->setBaseSensor(m_swapFree);
112 }
113