1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Thread_CPUObserver.h"
28 typedef BOOL (WINAPI *GetSystemTimesPtr)(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime);
30 ////////////////////////////////////////////////////////////
31 // Constructor & Destructor
32 ////////////////////////////////////////////////////////////
34 CPUObserverThread::CPUObserverThread(void)
39 CPUObserverThread::~CPUObserverThread(void)
43 ////////////////////////////////////////////////////////////
44 // Protected functions
45 ////////////////////////////////////////////////////////////
47 void CPUObserverThread::run(void)
49 qDebug("CPU observer started!");
60 fprintf(stderr, "\nGURU MEDITATION !!!\n");
61 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
62 TerminateProcess(GetCurrentProcess(), -1);
66 ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime)
68 ULARGE_INTEGER tmp; tmp.QuadPart = 0UI64;
69 const FILETIME* fileTime = reinterpret_cast<const FILETIME*>(ftime);
70 tmp.LowPart = fileTime->dwLowDateTime;
71 tmp.HighPart = fileTime->dwHighDateTime;
75 void CPUObserverThread::observe(void)
77 ULONGLONG sys[2], usr[2], idl[2];
78 FILETIME sysTime, usrTime, idlTime;
79 QLibrary kernel32("kernel32.dll");
80 GetSystemTimesPtr getSystemTimes = NULL;
82 double previous = -1.0;
86 getSystemTimes = reinterpret_cast<GetSystemTimesPtr>(kernel32.resolve("GetSystemTimes"));
89 if(getSystemTimes == NULL)
91 qWarning("GetSystemTimes() ist not available on this system!");
95 for(size_t i = 0; i < 2; i++)
97 sys[i] = 0; usr[i] = 0; idl[i] = 0;
102 if(getSystemTimes(&idlTime, &sysTime, &usrTime))
104 sys[1] = sys[0]; sys[0] = filetime2ulonglong(&sysTime);
105 usr[1] = usr[0]; usr[0] = filetime2ulonglong(&usrTime);
106 idl[1] = idl[0]; idl[0] = filetime2ulonglong(&idlTime);
111 emit currentUsageChanged(1.0);
116 ULONGLONG timeIdl = (idl[0] - idl[1]); //Idle time only
117 ULONGLONG timeSys = (sys[0] - sys[1]); //Kernel mode time (incl. Idle time!)
118 ULONGLONG timeUsr = (usr[0] - usr[1]); //User mode time only
120 ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
121 ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
123 if((timeSum > 0) || (timeWrk > 0))
125 double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
126 if(current != previous)
128 emit currentUsageChanged(current);
137 ////////////////////////////////////////////////////////////
139 ////////////////////////////////////////////////////////////
143 ////////////////////////////////////////////////////////////
145 ////////////////////////////////////////////////////////////