Updated Ukrainian translation.
[LameXP.git] / src / Thread_CPUObserver.cpp
blob6d3460b76eb7cfc1d41845e7fa3c0492db0c8864
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 lamexp_fatal_exit(L"Unhandeled exception error, application will exit!");
79 while(m_semaphore.available()) m_semaphore.tryAcquire();
82 ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime)
84 ULARGE_INTEGER tmp; tmp.QuadPart = 0UI64;
85 const FILETIME* fileTime = reinterpret_cast<const FILETIME*>(ftime);
86 tmp.LowPart = fileTime->dwLowDateTime;
87 tmp.HighPart = fileTime->dwHighDateTime;
88 return tmp.QuadPart;
91 void CPUObserverThread::observe(void)
93 QLibrary kernel32("kernel32.dll"), ntdll("ntdll.dll");
95 ULONG performanceInfoSize = 0;
96 BYTE *performanceInfoBuffer = NULL;
97 NtQuerySystemInformationPtr querySysInfo = NULL;
98 GetSystemTimesPtr getSystemTimes = NULL;
100 if(kernel32.load())
102 getSystemTimes = reinterpret_cast<GetSystemTimesPtr>(kernel32.resolve("GetSystemTimes"));
105 if(!getSystemTimes)
107 qWarning("GetSystemTimes() not found, falling back to NtQueryInformationProcess().");
108 if(ntdll.load())
110 querySysInfo = reinterpret_cast<NtQuerySystemInformationPtr>(ntdll.resolve("NtQuerySystemInformation"));
111 if(querySysInfo)
113 querySysInfo(SystemProcInfo, &performanceInfoBuffer, 0, &performanceInfoSize);
114 if(performanceInfoSize < sizeof(SYSTEM_PROC_INFO)) performanceInfoSize = sizeof(SYSTEM_PROC_INFO);
115 performanceInfoBuffer = new BYTE[performanceInfoSize];
120 if(getSystemTimes || (querySysInfo && performanceInfoBuffer))
122 bool first = true;
123 double previous = -1.0;
124 FILETIME sysTime, usrTime, idlTime;
125 ULONGLONG sys[2], usr[2], idl[2];
127 for(size_t i = 0; i < 2; i++)
129 sys[i] = 0; usr[i] = 0; idl[i] = 0;
132 forever
134 bool ok = false;
136 if(getSystemTimes)
138 if(ok = getSystemTimes(&idlTime, &sysTime, &usrTime))
140 sys[1] = sys[0]; sys[0] = filetime2ulonglong(&sysTime);
141 usr[1] = usr[0]; usr[0] = filetime2ulonglong(&usrTime);
142 idl[1] = idl[0]; idl[0] = filetime2ulonglong(&idlTime);
145 else
147 if(ok = IS_OK(querySysInfo(SystemProcInfo, performanceInfoBuffer, performanceInfoSize, NULL)))
149 sys[1] = sys[0]; sys[0] = reinterpret_cast<SYSTEM_PROC_INFO*>(performanceInfoBuffer)[0].KrnlTime.QuadPart;
150 usr[1] = usr[0]; usr[0] = reinterpret_cast<SYSTEM_PROC_INFO*>(performanceInfoBuffer)[0].UserTime.QuadPart;
151 idl[1] = idl[0]; idl[0] = reinterpret_cast<SYSTEM_PROC_INFO*>(performanceInfoBuffer)[0].IdleTime.QuadPart;
155 if(ok)
157 if(first)
159 first = false;
160 emit currentUsageChanged(1.0);
161 msleep(250);
162 continue;
165 ULONGLONG timeIdl = (idl[0] - idl[1]); //Idle time only
166 ULONGLONG timeSys = (sys[0] - sys[1]); //Kernel mode time (incl. Idle time!)
167 ULONGLONG timeUsr = (usr[0] - usr[1]); //User mode time only
169 ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
170 ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
172 if(timeSum > 0)
174 double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
175 if(current != previous)
177 emit currentUsageChanged(current);
178 previous = current;
182 if(m_semaphore.tryAcquire(1, 2000)) break;
185 else
187 qWarning("NtQueryInformationProcess() not available, giving up!");
190 LAMEXP_DELETE_ARRAY(performanceInfoBuffer);
193 ////////////////////////////////////////////////////////////
194 // SLOTS
195 ////////////////////////////////////////////////////////////
197 /*NONE*/
199 ////////////////////////////////////////////////////////////
200 // EVENTS
201 ////////////////////////////////////////////////////////////
203 /*NONE*/