OSDN Git Service

Updated Russian translation. Thanks to Иван Митин <bardak@inbox.ru>.
[lamexp/LameXP.git] / src / Thread_CPUObserver.cpp
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Thread_CPUObserver.h"
23 #include "Global.h"
24
25 #include <QDir>
26 #include <QLibrary>
27
28 ////////////////////////////////////////////////////////////
29
30 typedef enum { SystemProcInfo = 8 } SYSTEM_INFO_CLASS;
31
32 typedef struct
33 {
34         LARGE_INTEGER IdleTime;
35         LARGE_INTEGER KrnlTime;
36         LARGE_INTEGER UserTime;
37         LARGE_INTEGER Reserved[2];
38         ULONG Reserved2;
39 }
40 SYSTEM_PROC_INFO;
41
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);
44
45 #define IS_OK(X) (((LONG)(X)) == ((LONG)0x00000000L))
46
47 ////////////////////////////////////////////////////////////
48 // Constructor & Destructor
49 ////////////////////////////////////////////////////////////
50
51 CPUObserverThread::CPUObserverThread(void)
52 {
53 }
54
55 CPUObserverThread::~CPUObserverThread(void)
56 {
57 }
58
59 ////////////////////////////////////////////////////////////
60 // Protected functions
61 ////////////////////////////////////////////////////////////
62
63 void CPUObserverThread::run(void)
64 {
65         qDebug("CPU observer started!");
66
67         try
68         {
69                 observe();
70         }
71         catch(...)
72         {
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);
78         }
79
80         while(m_semaphore.available()) m_semaphore.tryAcquire();
81 }
82
83 ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime)
84 {
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;
90 }
91
92 void CPUObserverThread::observe(void)
93 {
94         QLibrary kernel32("kernel32.dll"), ntdll("ntdll.dll");
95
96         ULONG performanceInfoSize = 0;
97         BYTE *performanceInfoBuffer = NULL;
98         NtQuerySystemInformationPtr querySysInfo = NULL;
99         GetSystemTimesPtr getSystemTimes = NULL;
100
101         if(kernel32.load())
102         {
103                 getSystemTimes = reinterpret_cast<GetSystemTimesPtr>(kernel32.resolve("GetSystemTimes"));
104         }
105
106         if(!getSystemTimes)
107         {
108                 qWarning("GetSystemTimes() not found, falling back to NtQueryInformationProcess().");
109                 if(ntdll.load())
110                 {
111                         querySysInfo = reinterpret_cast<NtQuerySystemInformationPtr>(ntdll.resolve("NtQuerySystemInformation"));
112                         if(querySysInfo)
113                         {
114                                 querySysInfo(SystemProcInfo, &performanceInfoBuffer, 0, &performanceInfoSize);
115                                 if(performanceInfoSize < sizeof(SYSTEM_PROC_INFO)) performanceInfoSize = sizeof(SYSTEM_PROC_INFO);
116                                 performanceInfoBuffer = new BYTE[performanceInfoSize];
117                         }
118                 }
119         }
120
121         if(getSystemTimes || (querySysInfo && performanceInfoBuffer))
122         {
123                 bool first = true;
124                 double previous = -1.0;
125                 FILETIME sysTime, usrTime, idlTime;
126                 ULONGLONG sys[2], usr[2], idl[2];
127
128                 for(size_t i = 0; i < 2; i++)
129                 {
130                         sys[i] = 0; usr[i] = 0; idl[i] = 0;
131                 }
132
133                 forever
134                 {
135                         bool ok = false;
136                         
137                         if(getSystemTimes)
138                         {
139                                 if(ok = getSystemTimes(&idlTime, &sysTime, &usrTime))
140                                 {
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);
144                                 }
145                         }
146                         else
147                         {
148                                 if(ok = IS_OK(querySysInfo(SystemProcInfo, performanceInfoBuffer, performanceInfoSize, NULL)))
149                                 {
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;
153                                 }
154                         }
155
156                         if(ok)
157                         {
158                                 if(first)
159                                 {
160                                         first = false;
161                                         emit currentUsageChanged(1.0);
162                                         msleep(250);
163                                         continue;
164                                 }
165
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
169                                 
170                                 ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
171                                 ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
172
173                                 if(timeSum > 0)
174                                 {
175                                         double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
176                                         if(current != previous)
177                                         {
178                                                 emit currentUsageChanged(current);
179                                                 previous = current;
180                                         }
181                                 }
182                         }
183                         if(m_semaphore.tryAcquire(1, 2000)) break;
184                 }
185         }
186         else
187         {
188                 qWarning("NtQueryInformationProcess() not available, giving up!");
189         }
190
191         LAMEXP_DELETE_ARRAY(performanceInfoBuffer);
192 }
193
194 ////////////////////////////////////////////////////////////
195 // SLOTS
196 ////////////////////////////////////////////////////////////
197
198 /*NONE*/
199
200 ////////////////////////////////////////////////////////////
201 // EVENTS
202 ////////////////////////////////////////////////////////////
203
204 /*NONE*/