Updated SoX binary to v14.4.2-Git (2014-10-06), compiled with ICL 15.0 and MSVC 12.0.
[LameXP.git] / src / Thread_CPUObserver.cpp
blob724817a06ff918bbd3fd7e49b1c9ff60d6dda2ac
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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.
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"
24 #include "Global.h"
26 #include <QDir>
28 //Windows includes
29 #define NOMINMAX
30 #define WIN32_LEAN_AND_MEAN
31 #include <Windows.h>
33 ////////////////////////////////////////////////////////////
34 // Constructor & Destructor
35 ////////////////////////////////////////////////////////////
37 CPUObserverThread::CPUObserverThread(void)
41 CPUObserverThread::~CPUObserverThread(void)
45 ////////////////////////////////////////////////////////////
46 // Protected functions
47 ////////////////////////////////////////////////////////////
49 void CPUObserverThread::run(void)
51 qDebug("CPU observer started!");
53 try
55 observe();
57 catch(const std::exception &error)
59 PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
60 lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
62 catch(...)
64 PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
65 lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
68 while(m_semaphore.available()) m_semaphore.tryAcquire();
71 ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime)
73 ULARGE_INTEGER tmp; tmp.QuadPart = 0UI64;
74 const FILETIME* fileTime = reinterpret_cast<const FILETIME*>(ftime);
75 tmp.LowPart = fileTime->dwLowDateTime;
76 tmp.HighPart = fileTime->dwHighDateTime;
77 return tmp.QuadPart;
80 void CPUObserverThread::observe(void)
82 bool first = true;
83 double previous = -1.0;
84 FILETIME sysTime, usrTime, idlTime;
85 ULONGLONG sys[2], usr[2], idl[2];
87 for(size_t i = 0; i < 2; i++)
89 sys[i] = 0; usr[i] = 0; idl[i] = 0;
92 forever
94 if(GetSystemTimes(&idlTime, &sysTime, &usrTime))
96 sys[1] = sys[0]; sys[0] = filetime2ulonglong(&sysTime);
97 usr[1] = usr[0]; usr[0] = filetime2ulonglong(&usrTime);
98 idl[1] = idl[0]; idl[0] = filetime2ulonglong(&idlTime);
100 if(first)
102 first = false;
103 emit currentUsageChanged(1.0);
104 msleep(250);
105 continue;
108 ULONGLONG timeIdl = (idl[0] - idl[1]); //Idle time only
109 ULONGLONG timeSys = (sys[0] - sys[1]); //Kernel mode time (incl. Idle time!)
110 ULONGLONG timeUsr = (usr[0] - usr[1]); //User mode time only
112 ULONGLONG timeSum = timeUsr + timeSys; //Overall CPU time that has elapsed
113 ULONGLONG timeWrk = timeSum - timeIdl; //Time the CPU spent working
115 if(timeSum > 0)
117 double current = static_cast<double>(timeWrk) / static_cast<double>(timeSum);
118 if(current != previous)
120 emit currentUsageChanged(current);
121 previous = current;
126 if(m_semaphore.tryAcquire(1, 2000)) break;
130 ////////////////////////////////////////////////////////////
131 // SLOTS
132 ////////////////////////////////////////////////////////////
134 /*NONE*/
136 ////////////////////////////////////////////////////////////
137 // EVENTS
138 ////////////////////////////////////////////////////////////
140 /*NONE*/