Bump version.
[LameXP.git] / src / Thread_CPUObserver.cpp
blob8af9619c5a9427d031c7af2a797737e23993a58f
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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"
23 #include "Global.h"
25 #include <QDir>
26 #include <QLibrary>
28 ////////////////////////////////////////////////////////////
30 typedef enum { SystemProcInfo = 8 } SYSTEM_INFO_CLASS;
32 typedef struct
34 LARGE_INTEGER IdleTime;
35 LARGE_INTEGER KrnlTime;
36 LARGE_INTEGER UserTime;
37 LARGE_INTEGER Reserved[2];
38 ULONG Reserved2;
40 SYSTEM_PROC_INFO;
42 typedef BOOL (WINAPI *GetSystemTimesPtr)(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime);
43 typedef LONG (WINAPI *NtQuerySystemInformationPtr)(SYSTEM_INFO_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);
45 #define IS_OK(X) (((LONG)(X)) == ((LONG)0x00000000L))
47 ////////////////////////////////////////////////////////////
48 // Constructor & Destructor
49 ////////////////////////////////////////////////////////////
51 CPUObserverThread::CPUObserverThread(void)
55 CPUObserverThread::~CPUObserverThread(void)
59 ////////////////////////////////////////////////////////////
60 // Protected functions
61 ////////////////////////////////////////////////////////////
63 void CPUObserverThread::run(void)
65 qDebug("CPU observer started!");
67 try
69 observe();
71 catch(...)
73 fflush(stdout);
74 fflush(stderr);
75 fprintf(stderr, "\nGURU MEDITATION !!!\n");
76 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
77 TerminateProcess(GetCurrentProcess(), -1);
80 while(m_semaphore.available()) m_semaphore.tryAcquire();
83 ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime)
85 ULARGE_INTEGER tmp; tmp.QuadPart = 0UI64;
86 const FILETIME* fileTime = reinterpret_cast<const FILETIME*>(ftime);
87 tmp.LowPart = fileTime->dwLowDateTime;
88 tmp.HighPart = fileTime->dwHighDateTime;
89 return tmp.QuadPart;
92 void CPUObserverThread::observe(void)
94 QLibrary kernel32("kernel32.dll"), ntdll("ntdll.dll");
96 ULONG performanceInfoSize = 0;
97 BYTE *performanceInfoBuffer = NULL;
98 NtQuerySystemInformationPtr querySysInfo = NULL;
99 GetSystemTimesPtr getSystemTimes = NULL;
101 if(kernel32.load())
103 getSystemTimes = reinterpret_cast<GetSystemTimesPtr>(kernel32.resolve("GetSystemTimes"));
106 if(!getSystemTimes)
108 qWarning("GetSystemTimes() not found, falling back to NtQueryInformationProcess().");
109 if(ntdll.load())
111 querySysInfo = reinterpret_cast<NtQuerySystemInformationPtr>(ntdll.resolve("NtQuerySystemInformation"));
112 if(querySysInfo)
114 querySysInfo(SystemProcInfo, &performanceInfoBuffer, 0, &performanceInfoSize);
115 if(performanceInfoSize < sizeof(SYSTEM_PROC_INFO)) performanceInfoSize = sizeof(SYSTEM_PROC_INFO);
116 performanceInfoBuffer = new BYTE[performanceInfoSize];
121 if(getSystemTimes || (querySysInfo && performanceInfoBuffer))
123 bool first = true;
124 double previous = -1.0;
125 FILETIME sysTime, usrTime, idlTime;
126 ULONGLONG sys[2], usr[2], idl[2];
128 for(size_t i = 0; i < 2; i++)
130 sys[i] = 0; usr[i] = 0; idl[i] = 0;
133 forever
135 bool ok = false;
137 if(getSystemTimes)
139 if(ok = getSystemTimes(&idlTime, &sysTime, &usrTime))
141 sys[1] = sys[0]; sys[0] = filetime2ulonglong(&sysTime);
142 usr[1] = usr[0]; usr[0] = filetime2ulonglong(&usrTime);
143 idl[1] = idl[0]; idl[0] = filetime2ulonglong(&idlTime);
146 else
148 if(ok = IS_OK(querySysInfo(SystemProcInfo, performanceInfoBuffer, performanceInfoSize, NULL)))
150 sys[1] = sys[0]; sys[0] = reinterpret_cast<SYSTEM_PROC_INFO*>(performanceInfoBuffer)[0].KrnlTime.QuadPart;
151 usr[1] = usr[0]; usr[0] = reinterpret_cast<SYSTEM_PROC_INFO*>(performanceInfoBuffer)[0].UserTime.QuadPart;
152 idl[1] = idl[0]; idl[0] = reinterpret_cast<SYSTEM_PROC_INFO*>(performanceInfoBuffer)[0].IdleTime.QuadPart;
156 if(ok)
158 if(first)
160 first = false;
161 emit currentUsageChanged(1.0);
162 msleep(250);
163 continue;
166 ULONGLONG timeIdl = (idl[0] - idl[1]); //Idle time only
167 ULONGLONG timeSys = (sys[0] - sys[1]); //Kernel mode time (incl. Idle time!)
168 ULONGLONG timeUsr = (usr[0] - usr[1]); //User mode time only
170 ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
171 ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
173 if(timeSum > 0)
175 double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
176 if(current != previous)
178 emit currentUsageChanged(current);
179 previous = current;
183 if(m_semaphore.tryAcquire(1, 2000)) break;
186 else
188 qWarning("NtQueryInformationProcess() not available, giving up!");
191 LAMEXP_DELETE_ARRAY(performanceInfoBuffer);
194 ////////////////////////////////////////////////////////////
195 // SLOTS
196 ////////////////////////////////////////////////////////////
198 /*NONE*/
200 ////////////////////////////////////////////////////////////
201 // EVENTS
202 ////////////////////////////////////////////////////////////
204 /*NONE*/