1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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 ////////////////////////////////////////////////////////////
30 typedef enum { SystemProcInfo
= 8 } SYSTEM_INFO_CLASS
;
34 LARGE_INTEGER IdleTime
;
35 LARGE_INTEGER KrnlTime
;
36 LARGE_INTEGER UserTime
;
37 LARGE_INTEGER Reserved
[2];
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!");
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
= 0UI
64;
86 const FILETIME
* fileTime
= reinterpret_cast<const FILETIME
*>(ftime
);
87 tmp
.LowPart
= fileTime
->dwLowDateTime
;
88 tmp
.HighPart
= fileTime
->dwHighDateTime
;
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
;
103 getSystemTimes
= reinterpret_cast<GetSystemTimesPtr
>(kernel32
.resolve("GetSystemTimes"));
108 qWarning("GetSystemTimes() not found, falling back to NtQueryInformationProcess().");
111 querySysInfo
= reinterpret_cast<NtQuerySystemInformationPtr
>(ntdll
.resolve("NtQuerySystemInformation"));
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
))
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;
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
);
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
;
161 emit
currentUsageChanged(1.0);
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
175 double current
= static_cast<double>(timeWrk
) / static_cast<double>(timeSum
);
176 if(current
!= previous
)
178 emit
currentUsageChanged(current
);
183 if(m_semaphore
.tryAcquire(1, 2000)) break;
188 qWarning("NtQueryInformationProcess() not available, giving up!");
191 LAMEXP_DELETE_ARRAY(performanceInfoBuffer
);
194 ////////////////////////////////////////////////////////////
196 ////////////////////////////////////////////////////////////
200 ////////////////////////////////////////////////////////////
202 ////////////////////////////////////////////////////////////