OSDN Git Service

Improved Russian & Spanish translations for the installer
[lamexp/LameXP.git] / src / Thread_CPUObserver.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 //Internal
24 #include "Thread_CPUObserver.h"
25 #include "Global.h"
26
27 //MUtils
28 #include <MUtils/OSSupport.h>
29 #include <MUtils/Exception.h>
30
31 //Qt
32 #include <QDir>
33
34 //Windows includes
35 #define NOMINMAX
36 #define WIN32_LEAN_AND_MEAN
37 #include <Windows.h>
38
39 ////////////////////////////////////////////////////////////
40 // Constructor & Destructor
41 ////////////////////////////////////////////////////////////
42
43 CPUObserverThread::CPUObserverThread(void)
44 {
45 }
46
47 CPUObserverThread::~CPUObserverThread(void)
48 {
49 }
50
51 ////////////////////////////////////////////////////////////
52 // Protected functions
53 ////////////////////////////////////////////////////////////
54
55 void CPUObserverThread::run(void)
56 {
57         qDebug("CPU observer started!");
58
59         try
60         {
61                 observe();
62         }
63         catch(const std::exception &error)
64         {
65                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
66                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
67         }
68         catch(...)
69         {
70                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
71                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
72         }
73
74         while(m_semaphore.available()) m_semaphore.tryAcquire();
75 }
76
77 ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime)
78 {
79         ULARGE_INTEGER tmp; tmp.QuadPart = 0UI64;
80         const FILETIME* fileTime = reinterpret_cast<const FILETIME*>(ftime);
81         tmp.LowPart = fileTime->dwLowDateTime;
82         tmp.HighPart = fileTime->dwHighDateTime;
83         return tmp.QuadPart;
84 }
85
86 void CPUObserverThread::observe(void)
87 {
88         bool first = true;
89         double previous = -1.0;
90         FILETIME sysTime, usrTime, idlTime;
91         ULONGLONG sys[2], usr[2], idl[2];
92
93         for(size_t i = 0; i < 2; i++)
94         {
95                 sys[i] = 0; usr[i] = 0; idl[i] = 0;
96         }
97
98         forever
99         {
100                 if(GetSystemTimes(&idlTime, &sysTime, &usrTime))
101                 {
102                         sys[1] = sys[0]; sys[0] = filetime2ulonglong(&sysTime);
103                         usr[1] = usr[0]; usr[0] = filetime2ulonglong(&usrTime);
104                         idl[1] = idl[0]; idl[0] = filetime2ulonglong(&idlTime);
105
106                         if(first)
107                         {
108                                 first = false;
109                                 emit currentUsageChanged(1.0);
110                                 msleep(250);
111                                 continue;
112                         }
113
114                         ULONGLONG timeIdl = (idl[0] - idl[1]); //Idle time only
115                         ULONGLONG timeSys = (sys[0] - sys[1]); //Kernel mode time (incl. Idle time!)
116                         ULONGLONG timeUsr = (usr[0] - usr[1]); //User mode time only
117                                 
118                         ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
119                         ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
120
121                         if(timeSum > 0)
122                         {
123                                 double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
124                                 if(current != previous)
125                                 {
126                                         emit currentUsageChanged(current);
127                                         previous = current;
128                                 }
129                         }
130                 }
131
132                 if(m_semaphore.tryAcquire(1, 2000)) break;
133         }
134 }
135
136 ////////////////////////////////////////////////////////////
137 // SLOTS
138 ////////////////////////////////////////////////////////////
139
140 /*NONE*/
141
142 ////////////////////////////////////////////////////////////
143 // EVENTS
144 ////////////////////////////////////////////////////////////
145
146 /*NONE*/