OSDN Git Service

Updated Monkey's Audio binary to v4.11 (2013-01-20), including STDERR flush fix.
[lamexp/LameXP.git] / src / Thread_CPUObserver.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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                 lamexp_fatal_exit(L"Unhandeled exception error, application will exit!");
77         }
78
79         while(m_semaphore.available()) m_semaphore.tryAcquire();
80 }
81
82 ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime)
83 {
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;
89 }
90
91 void CPUObserverThread::observe(void)
92 {
93         QLibrary kernel32("kernel32.dll"), ntdll("ntdll.dll");
94
95         ULONG performanceInfoSize = 0;
96         BYTE *performanceInfoBuffer = NULL;
97         NtQuerySystemInformationPtr querySysInfo = NULL;
98         GetSystemTimesPtr getSystemTimes = NULL;
99
100         if(kernel32.load())
101         {
102                 getSystemTimes = reinterpret_cast<GetSystemTimesPtr>(kernel32.resolve("GetSystemTimes"));
103         }
104
105         if(!getSystemTimes)
106         {
107                 qWarning("GetSystemTimes() not found, falling back to NtQueryInformationProcess().");
108                 if(ntdll.load())
109                 {
110                         querySysInfo = reinterpret_cast<NtQuerySystemInformationPtr>(ntdll.resolve("NtQuerySystemInformation"));
111                         if(querySysInfo)
112                         {
113                                 querySysInfo(SystemProcInfo, &performanceInfoBuffer, 0, &performanceInfoSize);
114                                 if(performanceInfoSize < sizeof(SYSTEM_PROC_INFO)) performanceInfoSize = sizeof(SYSTEM_PROC_INFO);
115                                 performanceInfoBuffer = new BYTE[performanceInfoSize];
116                         }
117                 }
118         }
119
120         if(getSystemTimes || (querySysInfo && performanceInfoBuffer))
121         {
122                 bool first = true;
123                 double previous = -1.0;
124                 FILETIME sysTime, usrTime, idlTime;
125                 ULONGLONG sys[2], usr[2], idl[2];
126
127                 for(size_t i = 0; i < 2; i++)
128                 {
129                         sys[i] = 0; usr[i] = 0; idl[i] = 0;
130                 }
131
132                 forever
133                 {
134                         bool ok = false;
135                         
136                         if(getSystemTimes)
137                         {
138                                 if(ok = getSystemTimes(&idlTime, &sysTime, &usrTime))
139                                 {
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);
143                                 }
144                         }
145                         else
146                         {
147                                 if(ok = IS_OK(querySysInfo(SystemProcInfo, performanceInfoBuffer, performanceInfoSize, NULL)))
148                                 {
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;
152                                 }
153                         }
154
155                         if(ok)
156                         {
157                                 if(first)
158                                 {
159                                         first = false;
160                                         emit currentUsageChanged(1.0);
161                                         msleep(250);
162                                         continue;
163                                 }
164
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
168                                 
169                                 ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
170                                 ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
171
172                                 if(timeSum > 0)
173                                 {
174                                         double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
175                                         if(current != previous)
176                                         {
177                                                 emit currentUsageChanged(current);
178                                                 previous = current;
179                                         }
180                                 }
181                         }
182                         if(m_semaphore.tryAcquire(1, 2000)) break;
183                 }
184         }
185         else
186         {
187                 qWarning("NtQueryInformationProcess() not available, giving up!");
188         }
189
190         LAMEXP_DELETE_ARRAY(performanceInfoBuffer);
191 }
192
193 ////////////////////////////////////////////////////////////
194 // SLOTS
195 ////////////////////////////////////////////////////////////
196
197 /*NONE*/
198
199 ////////////////////////////////////////////////////////////
200 // EVENTS
201 ////////////////////////////////////////////////////////////
202
203 /*NONE*/