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
|
/*
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 "usagecomputer.h"
#include <algorithm>
void UsageComputer::setTicks(long long system, long long user, long long wait, long long idle)
{
// according to the documentation some counters can go backwards in some circumstances
auto systemDiff = std::max(system - m_systemTicks, 0ll);
auto userDiff = std::max(user - m_userTicks, 0ll);
auto waitDiff = std::max(wait - m_waitTicks, 0ll);
long long totalTicks = system + user + wait + idle;
auto totalDiff = std::max(totalTicks - m_totalTicks, 0ll);
auto percentage = [totalDiff] (long long tickDiff) {
if (tickDiff >= totalDiff) {
return 100.0;
}
if (tickDiff > 0 && totalDiff > 0) {
return 100.0 * tickDiff / totalDiff;
}
return 0.0;
};
systemUsage = percentage(systemDiff);
userUsage = percentage(userDiff);
waitUsage = percentage(waitDiff);
totalUsage = percentage(systemDiff + userDiff);
m_totalTicks = totalTicks;
m_systemTicks = system;
m_userTicks = user;
m_waitTicks = wait;
}
|