1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Thread_CPUObserver.h"
31 #define WIN32_LEAN_AND_MEAN
34 ////////////////////////////////////////////////////////////
36 typedef enum { SystemProcInfo
= 8 } SYSTEM_INFO_CLASS
;
40 LARGE_INTEGER IdleTime
;
41 LARGE_INTEGER KrnlTime
;
42 LARGE_INTEGER UserTime
;
43 LARGE_INTEGER Reserved
[2];
48 typedef BOOL (WINAPI
*GetSystemTimesPtr
)(LPFILETIME lpIdleTime
, LPFILETIME lpKernelTime
, LPFILETIME lpUserTime
);
49 typedef LONG (WINAPI
*NtQuerySystemInformationPtr
)(SYSTEM_INFO_CLASS SystemInformationClass
, PVOID SystemInformation
, ULONG SystemInformationLength
, PULONG ReturnLength
);
51 #define IS_OK(X) (((LONG)(X)) == ((LONG)0x00000000L))
53 ////////////////////////////////////////////////////////////
54 // Constructor & Destructor
55 ////////////////////////////////////////////////////////////
57 CPUObserverThread::CPUObserverThread(void)
61 CPUObserverThread::~CPUObserverThread(void)
65 ////////////////////////////////////////////////////////////
66 // Protected functions
67 ////////////////////////////////////////////////////////////
69 void CPUObserverThread::run(void)
71 qDebug("CPU observer started!");
77 catch(const std::exception
&error
)
79 fflush(stdout
); fflush(stderr
);
80 fprintf(stderr
, "\nGURU MEDITATION !!!\n\nException error:\n%s\n", error
.what());
81 lamexp_fatal_exit(L
"Unhandeled C++ exception error, application will exit!");
85 fflush(stdout
); fflush(stderr
);
86 fprintf(stderr
, "\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
87 lamexp_fatal_exit(L
"Unhandeled C++ exception error, application will exit!");
90 while(m_semaphore
.available()) m_semaphore
.tryAcquire();
93 ULONGLONG
CPUObserverThread::filetime2ulonglong(const void *ftime
)
95 ULARGE_INTEGER tmp
; tmp
.QuadPart
= 0UI
64;
96 const FILETIME
* fileTime
= reinterpret_cast<const FILETIME
*>(ftime
);
97 tmp
.LowPart
= fileTime
->dwLowDateTime
;
98 tmp
.HighPart
= fileTime
->dwHighDateTime
;
102 void CPUObserverThread::observe(void)
104 QLibrary
kernel32("kernel32.dll"), ntdll("ntdll.dll");
106 ULONG performanceInfoSize
= 0;
107 BYTE
*performanceInfoBuffer
= NULL
;
108 NtQuerySystemInformationPtr querySysInfo
= NULL
;
109 GetSystemTimesPtr getSystemTimes
= NULL
;
113 getSystemTimes
= reinterpret_cast<GetSystemTimesPtr
>(kernel32
.resolve("GetSystemTimes"));
118 qWarning("GetSystemTimes() not found, falling back to NtQueryInformationProcess().");
121 querySysInfo
= reinterpret_cast<NtQuerySystemInformationPtr
>(ntdll
.resolve("NtQuerySystemInformation"));
124 querySysInfo(SystemProcInfo
, &performanceInfoBuffer
, 0, &performanceInfoSize
);
125 if(performanceInfoSize
< sizeof(SYSTEM_PROC_INFO
)) performanceInfoSize
= sizeof(SYSTEM_PROC_INFO
);
126 performanceInfoBuffer
= new BYTE
[performanceInfoSize
];
131 if(getSystemTimes
|| (querySysInfo
&& performanceInfoBuffer
))
134 double previous
= -1.0;
135 FILETIME sysTime
, usrTime
, idlTime
;
136 ULONGLONG sys
[2], usr
[2], idl
[2];
138 for(size_t i
= 0; i
< 2; i
++)
140 sys
[i
] = 0; usr
[i
] = 0; idl
[i
] = 0;
149 if(ok
= getSystemTimes(&idlTime
, &sysTime
, &usrTime
))
151 sys
[1] = sys
[0]; sys
[0] = filetime2ulonglong(&sysTime
);
152 usr
[1] = usr
[0]; usr
[0] = filetime2ulonglong(&usrTime
);
153 idl
[1] = idl
[0]; idl
[0] = filetime2ulonglong(&idlTime
);
158 if(ok
= IS_OK(querySysInfo(SystemProcInfo
, performanceInfoBuffer
, performanceInfoSize
, NULL
)))
160 sys
[1] = sys
[0]; sys
[0] = reinterpret_cast<SYSTEM_PROC_INFO
*>(performanceInfoBuffer
)[0].KrnlTime
.QuadPart
;
161 usr
[1] = usr
[0]; usr
[0] = reinterpret_cast<SYSTEM_PROC_INFO
*>(performanceInfoBuffer
)[0].UserTime
.QuadPart
;
162 idl
[1] = idl
[0]; idl
[0] = reinterpret_cast<SYSTEM_PROC_INFO
*>(performanceInfoBuffer
)[0].IdleTime
.QuadPart
;
171 emit
currentUsageChanged(1.0);
176 ULONGLONG timeIdl
= (idl
[0] - idl
[1]); //Idle time only
177 ULONGLONG timeSys
= (sys
[0] - sys
[1]); //Kernel mode time (incl. Idle time!)
178 ULONGLONG timeUsr
= (usr
[0] - usr
[1]); //User mode time only
180 ULONGLONG timeSum
= timeUsr
+ timeSys
; //Overall CPU time that has elapsed
181 ULONGLONG timeWrk
= timeSum
- timeIdl
; //Time the CPU spent working
185 double current
= static_cast<double>(timeWrk
) / static_cast<double>(timeSum
);
186 if(current
!= previous
)
188 emit
currentUsageChanged(current
);
193 if(m_semaphore
.tryAcquire(1, 2000)) break;
198 qWarning("NtQueryInformationProcess() not available, giving up!");
201 LAMEXP_DELETE_ARRAY(performanceInfoBuffer
);
204 ////////////////////////////////////////////////////////////
206 ////////////////////////////////////////////////////////////
210 ////////////////////////////////////////////////////////////
212 ////////////////////////////////////////////////////////////