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)
38 CPUObserverThread::~CPUObserverThread(void)
42 ////////////////////////////////////////////////////////////
43 // Protected functions
44 ////////////////////////////////////////////////////////////
46 void CPUObserverThread::run(void)
48 qDebug("CPU observer started!");
58 fprintf(stderr, "\nGURU MEDITATION !!!\n");
59 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
60 TerminateProcess(GetCurrentProcess(), -1);
63 while(m_semaphore.available()) m_semaphore.tryAcquire();
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 QLibrary kernel32("kernel32.dll");
78 GetSystemTimesPtr getSystemTimes = NULL;
82 getSystemTimes = reinterpret_cast<GetSystemTimesPtr>(kernel32.resolve("GetSystemTimes"));
85 if(getSystemTimes != NULL)
88 double previous = -1.0;
89 FILETIME sysTime, usrTime, idlTime;
90 ULONGLONG sys[2], usr[2], idl[2];
92 for(size_t i = 0; i < 2; i++)
94 sys[i] = 0; usr[i] = 0; idl[i] = 0;
99 if(getSystemTimes(&idlTime, &sysTime, &usrTime))
101 sys[1] = sys[0]; sys[0] = filetime2ulonglong(&sysTime);
102 usr[1] = usr[0]; usr[0] = filetime2ulonglong(&usrTime);
103 idl[1] = idl[0]; idl[0] = filetime2ulonglong(&idlTime);
108 emit currentUsageChanged(1.0);
113 ULONGLONG timeIdl = (idl[0] - idl[1]); //Idle time only
114 ULONGLONG timeSys = (sys[0] - sys[1]); //Kernel mode time (incl. Idle time!)
115 ULONGLONG timeUsr = (usr[0] - usr[1]); //User mode time only
117 ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
118 ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
122 double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
123 if(current != previous)
125 emit currentUsageChanged(current);
130 if(m_semaphore.tryAcquire(1, 2000)) break;
135 qWarning("GetSystemTimes() ist not available on this system!");
139 ////////////////////////////////////////////////////////////
141 ////////////////////////////////////////////////////////////
145 ////////////////////////////////////////////////////////////
147 ////////////////////////////////////////////////////////////