File indexing completed on 2024-05-19 05:30:18
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(long long system, long long user, long long wait, 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, 0ll); 0015 auto userDiff = std::max(user - m_userTicks, 0ll); 0016 auto waitDiff = std::max(wait - m_waitTicks, 0ll); 0017 0018 long long totalTicks = system + user + wait + idle; 0019 auto totalDiff = std::max(totalTicks - m_totalTicks, 0ll); 0020 0021 auto percentage = [totalDiff] (long long tickDiff) { 0022 if (tickDiff >= totalDiff) { 0023 return 100.0; 0024 } 0025 if (tickDiff > 0 && totalDiff > 0) { 0026 return 100.0 * tickDiff / totalDiff; 0027 } 0028 return 0.0; 0029 }; 0030 0031 systemUsage = percentage(systemDiff); 0032 userUsage = percentage(userDiff); 0033 waitUsage = percentage(waitDiff); 0034 totalUsage = percentage(systemDiff + userDiff + waitDiff); 0035 0036 m_totalTicks = totalTicks; 0037 m_systemTicks = system; 0038 m_userTicks = user; 0039 m_waitTicks = wait; 0040 }