OSDN Git Service

dacc247bb37a2b8f6497a13d8e37b5291dc5b6c9
[lamexp/LameXP.git] / src / Thread_CPUObserver.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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 typedef BOOL (WINAPI *GetSystemTimesPtr)(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime);
29
30 ////////////////////////////////////////////////////////////
31 // Constructor & Destructor
32 ////////////////////////////////////////////////////////////
33
34 CPUObserverThread::CPUObserverThread(void)
35 {
36         m_terminated = false;
37 }
38
39 CPUObserverThread::~CPUObserverThread(void)
40 {
41 }
42
43 ////////////////////////////////////////////////////////////
44 // Protected functions
45 ////////////////////////////////////////////////////////////
46
47 void CPUObserverThread::run(void)
48 {
49         qDebug("CPU observer started!");
50         m_terminated = false;
51
52         try
53         {
54                 observe();
55         }
56         catch(...)
57         {
58                 fflush(stdout);
59                 fflush(stderr);
60                 fprintf(stderr, "\nGURU MEDITATION !!!\n");
61                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
62                 TerminateProcess(GetCurrentProcess(), -1);
63         }
64 }
65
66 ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime)
67 {
68         ULARGE_INTEGER tmp; tmp.QuadPart = 0UI64;
69         const FILETIME* fileTime = reinterpret_cast<const FILETIME*>(ftime);
70         tmp.LowPart = fileTime->dwLowDateTime;
71         tmp.HighPart = fileTime->dwHighDateTime;
72         return tmp.QuadPart;
73 }
74
75 void CPUObserverThread::observe(void)
76 {
77         ULONGLONG sys[2], usr[2], idl[2];
78         FILETIME sysTime, usrTime, idlTime;
79         QLibrary kernel32("kernel32.dll");
80         GetSystemTimesPtr getSystemTimes = NULL;
81         bool first = true;
82         double previous = -1.0;
83
84         if(kernel32.load())
85         {
86                 getSystemTimes = reinterpret_cast<GetSystemTimesPtr>(kernel32.resolve("GetSystemTimes"));
87         }
88
89         if(getSystemTimes == NULL)
90         {
91                 qWarning("GetSystemTimes() ist not available on this system!");
92                 return;
93         }
94         
95         for(size_t i = 0; i < 2; i++)
96         {
97                 sys[i] = 0; usr[i] = 0; idl[i] = 0;
98         }
99
100         while(!m_terminated)
101         {
102                 if(getSystemTimes(&idlTime, &sysTime, &usrTime))
103                 {
104                         sys[1] = sys[0]; sys[0] = filetime2ulonglong(&sysTime);
105                         usr[1] = usr[0]; usr[0] = filetime2ulonglong(&usrTime);
106                         idl[1] = idl[0]; idl[0] = filetime2ulonglong(&idlTime);
107
108                         if(first)
109                         {
110                                 first = false;
111                                 emit currentUsageChanged(1.0);
112                                 msleep(250);
113                                 continue;
114                         }
115
116                         ULONGLONG timeIdl = (idl[0] - idl[1]); //Idle time only
117                         ULONGLONG timeSys = (sys[0] - sys[1]); //Kernel mode time (incl. Idle time!)
118                         ULONGLONG timeUsr = (usr[0] - usr[1]); //User mode time only
119                                 
120                         ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
121                         ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
122
123                         if((timeSum > 0) || (timeWrk > 0))
124                         {
125                                 double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
126                                 if(current != previous)
127                                 {
128                                         emit currentUsageChanged(current);
129                                         previous = current;
130                                 }
131                         }
132                 }
133                 msleep(1000);
134         }
135 }
136
137 ////////////////////////////////////////////////////////////
138 // SLOTS
139 ////////////////////////////////////////////////////////////
140
141 /*NONE*/
142
143 ////////////////////////////////////////////////////////////
144 // EVENTS
145 ////////////////////////////////////////////////////////////
146
147 /*NONE*/