|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** DDRS4PALS, a software for the acquisition of lifetime spectra using the |
| 4 | +** DRS4 evaluation board of PSI: https://www.psi.ch/drs/evaluation-board |
| 5 | +** |
| 6 | +** Copyright (C) 2016-2020 Danny Petschke |
| 7 | +** |
| 8 | +** This program is free software: you can redistribute it and/or modify |
| 9 | +** it under the terms of the GNU General Public License as published by |
| 10 | +** the Free Software Foundation, either version 3 of the License, or |
| 11 | +** (at your option) any later version. |
| 12 | +** |
| 13 | +** This program is distributed in the hope that it will be useful, |
| 14 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +** GNU General Public License for more details. |
| 17 | +** |
| 18 | +** You should have received a copy of the GNU General Public License |
| 19 | +** along with this program. If not, see http://www.gnu.org/licenses/. |
| 20 | +** |
| 21 | +***************************************************************************** |
| 22 | +** |
| 23 | +** @author: Danny Petschke |
| 24 | + |
| 25 | +** |
| 26 | +*****************************************************************************/ |
| 27 | + |
| 28 | +#include "drs4cpuusage.h" |
| 29 | + |
| 30 | +static DRS4CPUUsageManager *__sharedInstanceDRS4CPUUsageManager = nullptr; |
| 31 | + |
| 32 | +float DRS4CPUUsageManager::releaseCPULoad() |
| 33 | +{ |
| 34 | + FILETIME ftime, fsys, fuser; |
| 35 | + ULARGE_INTEGER now, sys, user; |
| 36 | + |
| 37 | + GetSystemTimeAsFileTime(&ftime); |
| 38 | + memcpy(&now, &ftime, sizeof(FILETIME)); |
| 39 | + |
| 40 | + GetProcessTimes(GetCurrentProcess(), &ftime, &ftime, &fsys, &fuser); |
| 41 | + |
| 42 | + memcpy(&sys, &fsys, sizeof(FILETIME)); |
| 43 | + memcpy(&user, &fuser, sizeof(FILETIME)); |
| 44 | + |
| 45 | + double percent = (sys.QuadPart - m_lastSysCPU.QuadPart) + (user.QuadPart - m_lastUserCPU.QuadPart); |
| 46 | + |
| 47 | + percent /= (now.QuadPart - m_lastCPU.QuadPart); |
| 48 | + percent /= m_sysInfo.dwNumberOfProcessors; |
| 49 | + |
| 50 | + m_lastCPU = now; |
| 51 | + m_lastUserCPU = user; |
| 52 | + m_lastSysCPU = sys; |
| 53 | + |
| 54 | + return percent * 100; |
| 55 | +} |
| 56 | + |
| 57 | +void DRS4CPUUsageManager::start(int intervalInMilliseconds) |
| 58 | +{ |
| 59 | + if (intervalInMilliseconds != -1) |
| 60 | + m_interval = intervalInMilliseconds; |
| 61 | + |
| 62 | + m_timer.setInterval(int(m_interval*0.2)); // averaging over 5 cycles |
| 63 | + m_timer.setSingleShot(false); |
| 64 | + |
| 65 | + connect(&m_timer, SIGNAL(timeout()), this, SLOT(cpuLoad())); |
| 66 | + |
| 67 | + m_avgCPUUsage = 0.0; |
| 68 | + m_avgCounter = 0; |
| 69 | + |
| 70 | + FILETIME ftime, fsys, fuser; |
| 71 | + |
| 72 | + GetSystemInfo(&m_sysInfo); |
| 73 | + |
| 74 | + GetSystemTimeAsFileTime(&ftime); |
| 75 | + memcpy(&m_lastCPU, &ftime, sizeof(FILETIME)); |
| 76 | + |
| 77 | + GetProcessTimes(GetCurrentProcess(), &ftime, &ftime, &fsys, &fuser); |
| 78 | + memcpy(&m_lastSysCPU, &fsys, sizeof(FILETIME)); |
| 79 | + memcpy(&m_lastUserCPU, &fuser, sizeof(FILETIME)); |
| 80 | + |
| 81 | + m_timer.start(); |
| 82 | +} |
| 83 | + |
| 84 | +void DRS4CPUUsageManager::stop() |
| 85 | +{ |
| 86 | + m_timer.stop(); |
| 87 | + |
| 88 | + m_avgCPUUsage = 0.0; |
| 89 | + m_avgCounter = 0; |
| 90 | + |
| 91 | + disconnect(&m_timer, SIGNAL(timeout()), this, SLOT(cpuLoad())); |
| 92 | +} |
| 93 | + |
| 94 | +void DRS4CPUUsageManager::setInterval(int intervalInMilliseconds) |
| 95 | +{ |
| 96 | + m_interval = intervalInMilliseconds; |
| 97 | +} |
| 98 | + |
| 99 | +void DRS4CPUUsageManager::cpuLoad() |
| 100 | +{ |
| 101 | + m_avgCPUUsage += releaseCPULoad(); |
| 102 | + m_avgCounter ++; |
| 103 | + |
| 104 | + if (m_avgCounter == 5) { |
| 105 | + m_avgCPUUsage /= (float)m_avgCounter; |
| 106 | + |
| 107 | + emit cpu((int)m_avgCPUUsage); |
| 108 | + |
| 109 | + m_avgCounter = 0; |
| 110 | + m_avgCPUUsage = 0.0; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +int DRS4CPUUsageManager::interval() const |
| 115 | +{ |
| 116 | + return m_interval; |
| 117 | +} |
| 118 | + |
| 119 | +DRS4CPUUsageManager *DRS4CPUUsageManager::sharedInstance() |
| 120 | +{ |
| 121 | + if (!__sharedInstanceDRS4CPUUsageManager) |
| 122 | + __sharedInstanceDRS4CPUUsageManager = new DRS4CPUUsageManager; |
| 123 | + |
| 124 | + return __sharedInstanceDRS4CPUUsageManager; |
| 125 | +} |
| 126 | + |
| 127 | +DRS4CPUUsageManager::DRS4CPUUsageManager(QObject *parent) : |
| 128 | + QObject(parent) |
| 129 | +{ |
| 130 | + m_avgCPUUsage = 0.0; |
| 131 | + m_avgCounter = 0; |
| 132 | + |
| 133 | + m_interval = 1000; |
| 134 | +} |
| 135 | + |
| 136 | +DRS4CPUUsageManager::~DRS4CPUUsageManager() |
| 137 | +{ |
| 138 | + m_timer.stop(); |
| 139 | + |
| 140 | + DDELETE_SAFETY(__sharedInstanceDRS4CPUUsageManager); |
| 141 | +} |
0 commit comments