OSDN Git Service

Print the total duration when the process is completed.
[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 }
37
38 CPUObserverThread::~CPUObserverThread(void)
39 {
40 }
41
42 ////////////////////////////////////////////////////////////
43 // Protected functions
44 ////////////////////////////////////////////////////////////
45
46 void CPUObserverThread::run(void)
47 {
48         qDebug("CPU observer started!");
49
50         try
51         {
52                 observe();
53         }
54         catch(...)
55         {
56                 fflush(stdout);
57                 fflush(stderr);
58                 fprintf(stderr, "\nGURU MEDITATION !!!\n");
59                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
60                 TerminateProcess(GetCurrentProcess(), -1);
61         }
62
63         while(m_semaphore.available()) m_semaphore.tryAcquire();
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         QLibrary kernel32("kernel32.dll");
78         GetSystemTimesPtr getSystemTimes = NULL;
79
80         if(kernel32.load())
81         {
82                 getSystemTimes = reinterpret_cast<GetSystemTimesPtr>(kernel32.resolve("GetSystemTimes"));
83         }
84
85         if(getSystemTimes != NULL)
86         {
87                 bool first = true;
88                 double previous = -1.0;
89                 FILETIME sysTime, usrTime, idlTime;
90                 ULONGLONG sys[2], usr[2], idl[2];
91
92                 for(size_t i = 0; i < 2; i++)
93                 {
94                         sys[i] = 0; usr[i] = 0; idl[i] = 0;
95                 }
96
97                 forever
98                 {
99                         if(getSystemTimes(&idlTime, &sysTime, &usrTime))
100                         {
101                                 sys[1] = sys[0]; sys[0] = filetime2ulonglong(&sysTime);
102                                 usr[1] = usr[0]; usr[0] = filetime2ulonglong(&usrTime);
103                                 idl[1] = idl[0]; idl[0] = filetime2ulonglong(&idlTime);
104
105                                 if(first)
106                                 {
107                                         first = false;
108                                         emit currentUsageChanged(1.0);
109                                         msleep(250);
110                                         continue;
111                                 }
112
113                                 ULONGLONG timeIdl = (idl[0] - idl[1]); //Idle time only
114                                 ULONGLONG timeSys = (sys[0] - sys[1]); //Kernel mode time (incl. Idle time!)
115                                 ULONGLONG timeUsr = (usr[0] - usr[1]); //User mode time only
116                                 
117                                 ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
118                                 ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
119
120                                 if(timeSum > 0)
121                                 {
122                                         double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
123                                         if(current != previous)
124                                         {
125                                                 emit currentUsageChanged(current);
126                                                 previous = current;
127                                         }
128                                 }
129                         }
130                         if(m_semaphore.tryAcquire(1, 2000)) break;
131                 }
132         }
133         else
134         {
135                 qWarning("GetSystemTimes() ist not available on this system!");
136         }
137 }
138
139 ////////////////////////////////////////////////////////////
140 // SLOTS
141 ////////////////////////////////////////////////////////////
142
143 /*NONE*/
144
145 ////////////////////////////////////////////////////////////
146 // EVENTS
147 ////////////////////////////////////////////////////////////
148
149 /*NONE*/