File indexing completed on 2024-05-12 17:00:14
0001 /* 0002 SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "usagecomputer.h" 0008 0009 #include <algorithm> 0010 0011 void UsageComputer::setTicks(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle) 0012 { 0013 // according to the documentation some counters can go backwards in some circumstances 0014 auto systemDiff = std::max(system - m_systemTicks, 0ull); 0015 auto userDiff = std::max(user - m_userTicks, 0ull); 0016 auto waitDiff = std::max(wait - m_waitTicks, 0ull); 0017 0018 unsigned long long totalTicks = system + user + wait + idle; 0019 auto totalDiff = std::max(totalTicks - m_totalTicks, 0ull); 0020 0021 auto percentage = [totalDiff] (unsigned long long tickDiff) { 0022 if (tickDiff > 0 && totalDiff > 0) { 0023 return 100.0 * tickDiff / totalDiff; 0024 } 0025 return 0.0; 0026 }; 0027 0028 systemUsage = percentage(systemDiff); 0029 userUsage = percentage(userDiff); 0030 waitUsage = percentage(waitDiff); 0031 totalUsage = percentage(systemDiff + userDiff + waitDiff); 0032 0033 m_totalTicks = totalTicks; 0034 m_systemTicks = system; 0035 m_userTicks = user; 0036 m_waitTicks = wait; 0037 }