a program for Monitoring CPU Usage

This is a class for Moniroring CPU Usage, there may be some problems,
anyone who please give me some recommendations,
I would be very grateful!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#ifndef __CMONITOR_H__
#define __CMONITOR_H__

//idle
#define WM_REACHED_IDLE		WM_USER + 101
//busy
#define WM_REACHED_BUSY		WM_USER + 102

class CCPUUsageRateMonitor
{
public:
	CCPUUsageRateMonitor();
	~CCPUUsageRateMonitor();
	
	void RunMonitor();
	void QuitMonitor();

	//Set Monitorint intervals, default 1s
	void SetMonitorInterval(int nMonitorInterval = 1);

	//Set CPU Usage Consider as Idle state & the time of keepint the Usage
	BOOL SetIdleInfo(int nRequestIdleRatio, int nIdleKeepSecond);

	//Set CPU Usage Consider as Busy state & the time of keepint the Usage
	BOOL SetBusyInfo(int nRequestBusyRatio, int nBusyKeepSecond);

	//Set window handle to receive the meeesage
	void LoadingWnd(HWND hWndRecvMsg);

private:
	HANDLE m_hThread;
	HANDLE m_hQuitEvent;

	int m_nMonitorInterval;

	int m_nRequestIdleRatio;
	int m_nRequestBusyRatio;

	int m_nIdleKeepingTime;
	int m_nBusyKeepingTime;

	HWND m_hWndRecvMsg;

private:
	void Monitoring();

	static UINT __stdcall MonitorThreadFunc(void* pArguments);

	int CalcActualUsageRate(
					FILETIME ftIdleTimeStart,
					FILETIME ftKernelTimeStart,
				        FILETIME ftUserTimeStart,
					FILETIME ftIdleTimeEnd,
					FILETIME ftKernelTimeEnd,
					FILETIME ftUserTimeEnd
					);

	//convert FILETIME to unsigned __int64
	unsigned __int64 FileTimeToQuadWord(PFILETIME pft);
};

#endif//__CMONITOR_H__

//////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CPUUsageRateMonitor.h"
#include <process.h>

CCPUUsageRateMonitor::CCPUUsageRateMonitor()
{
	m_nRequestIdleRatio = 20;
	m_nRequestBusyRatio = 80;

	m_nIdleKeepingTime = 180;
	m_nBusyKeepingTime = 60;

	m_hWndRecvMsg = NULL;

    m_nMonitorInterval = 1;

	m_hThread = INVALID_HANDLE_VALUE;
	m_hQuitEvent = INVALID_HANDLE_VALUE;
}

CCPUUsageRateMonitor::~CCPUUsageRateMonitor()
{
	QuitMonitor();
}

void CCPUUsageRateMonitor::RunMonitor()
{
	if (m_hThread != INVALID_HANDLE_VALUE)
	{
		return;
	}

	m_hQuitEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);

	unsigned uThreadID = 0;

	m_hThread = (HANDLE)::_beginthreadex(
				NULL,
				0,
				MonitorThreadFunc,
				(void*)this,
				0,
				&uThreadID
				);
}

void CCPUUsageRateMonitor::QuitMonitor()
{
	if (m_hThread == INVALID_HANDLE_VALUE)//监视线程已经退出
	{
		return;
	}

	::SetEvent(m_hQuitEvent);	

	::WaitForSingleObject(m_hThread, INFINITE);

	::CloseHandle(m_hThread);
	m_hThread = INVALID_HANDLE_VALUE;

	::CloseHandle(m_hQuitEvent);
	m_hQuitEvent = INVALID_HANDLE_VALUE;
}

void CCPUUsageRateMonitor::LoadingWnd(HWND hWndRecvMsg)
{
	ASSERT(hWndRecvMsg != NULL);

	m_hWndRecvMsg = hWndRecvMsg;
}

void CCPUUsageRateMonitor::SetMonitorInterval(int nMonitorInterval)
{
	m_nMonitorInterval = nMonitorInterval;
}

BOOL CCPUUsageRateMonitor::SetIdleInfo(int nRequestIdleRatio, int nIdleKeepSecond)
{
	if (nIdleKeepSecond < m_nMonitorInterval)
	{
		return FALSE;
	}

	m_nRequestIdleRatio = nRequestIdleRatio;
	m_nIdleKeepingTime = nIdleKeepSecond;

	return TRUE;
}

BOOL CCPUUsageRateMonitor::SetBusyInfo(int nRequestBusyRatio, int nBusyKeepSecond)
{
	if (nBusyKeepSecond < m_nMonitorInterval)
	{
		return FALSE;
	}

	m_nRequestBusyRatio = nRequestBusyRatio;
	m_nBusyKeepingTime = nBusyKeepSecond;

	return TRUE;
}

//private function
UINT __stdcall CCPUUsageRateMonitor::MonitorThreadFunc(LPVOID pParam)
{
	CCPUUsageRateMonitor* pMonitor = (CCPUUsageRateMonitor*)pParam;

	if (pMonitor != NULL)
	{
		pMonitor->Monitoring();
	}

	_endthreadex(0);
    return 0;
}

void CCPUUsageRateMonitor::Monitoring()
{
	int nIdleKeepingTimes = 0;
	int nBusyKeepingTimes = 0;

	FILETIME ftIdleTimeStart = {0};
	FILETIME ftKernelTimeStart = {0};
	FILETIME ftUserTimeStart = {0};

	FILETIME ftIdleTimeEnd = {0};
	FILETIME ftKernelTimeEnd = {0};
	FILETIME ftUserTimeEnd = {0};

	while (TRUE)
	{
		if (::WaitForSingleObject(m_hQuitEvent, 0) == WAIT_OBJECT_0)
		{
			break;
		}

		// Get starting times
		::GetSystemTimes(&ftIdleTimeStart, &ftKernelTimeStart, &ftUserTimeStart);

		Sleep(1000 * m_nMonitorInterval);		

		// Get ending times
		::GetSystemTimes(&ftIdleTimeEnd, &ftKernelTimeEnd, &ftUserTimeEnd);

		int nActualUsageRatio = CalcActualUsageRate(
							ftIdleTimeStart,
							ftKernelTimeStart,
							ftUserTimeStart,
							ftIdleTimeEnd,
							ftKernelTimeEnd,
							ftUserTimeEnd
							);

		if (nActualUsageRatio <= m_nRequestIdleRatio)
		{
			nIdleKeepingTimes += m_nMonitorInterval;
			nBusyKeepingTimes = 0;
		}
		else if (nActualUsageRatio >= m_nRequestBusyRatio)
		{
			nBusyKeepingTimes += m_nMonitorInterval;
			nIdleKeepingTimes = 0;
		}
		else
		{
			nBusyKeepingTimes = 0;
			nIdleKeepingTimes = 0;

			continue;
		}
		
		if (nIdleKeepingTimes == m_nIdleKeepingTime)
		{
			if (m_hWndRecvMsg != NULL)
			{
				::PostMessage(m_hWndRecvMsg, WM_REACHED_IDLE, 0, 0);
			}

			nIdleKeepingTimes = 0;
		}
		
		if (nBusyKeepingTimes == m_nBusyKeepingTime)
		{
			if (m_hWndRecvMsg != NULL)
			{
				::PostMessage(m_hWndRecvMsg, WM_REACHED_BUSY, 0, 0);
			}

			nBusyKeepingTimes = 0;
		}
	}
}

int CCPUUsageRateMonitor::CalcActualUsageRate(
				FILETIME ftIdleTimeStart,
				FILETIME ftKernelTimeStart,
				FILETIME ftUserTimeStart,
				FILETIME ftIdleTimeEnd,
				FILETIME ftKernelTimeEnd,
				FILETIME ftUserTimeEnd
				)
{
	int nActualUsageRatio = 0;

	// Get the elapsed idle, kernel and user times by
	// subtracting the start times from the end times.
	unsigned __int64 qwIdleTimeElapsed = FileTimeToQuadWord(&ftIdleTimeEnd)
		- FileTimeToQuadWord(&ftIdleTimeStart);

	unsigned __int64 qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd)
		- FileTimeToQuadWord(&ftKernelTimeStart);

	unsigned __int64 qwUserTimeElapsed = FileTimeToQuadWord(&ftUserTimeEnd)
		- FileTimeToQuadWord(&ftUserTimeStart);

	// Get total time duration by anding the kernel and user times
	unsigned __int64 qwTotalTimeElapsed =qwKernelTimeElapsed + qwUserTimeElapsed;

	ASSERT(qwTotalTimeElapsed != 0);

	// Get Actual Usage In Percent
	nActualUsageRatio = (qwTotalTimeElapsed - qwIdleTimeElapsed) * 100 / qwTotalTimeElapsed;
	
	return nActualUsageRatio;	
}

unsigned __int64 CCPUUsageRateMonitor::FileTimeToQuadWord(PFILETIME pft)
{ 
	return (Int64ShllMod32(pft->dwHighDateTime, 32) | pft->dwLowDateTime);
}



Last edited on
I just modified Monitoring() function below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
void CCPUUsageRateMonitor::Monitoring()
{
	int nIdleKeepingTimes = 0;
	int nBusyKeepingTimes = 0;

	FILETIME ftIdleTimeStart = {0};
	FILETIME ftKernelTimeStart = {0};
	FILETIME ftUserTimeStart = {0};

	FILETIME ftIdleTimeEnd = {0};
	FILETIME ftKernelTimeEnd = {0};
	FILETIME ftUserTimeEnd = {0};

	while (TRUE)
	{
		if (::WaitForSingleObject(m_hQuitEvent, 0) == WAIT_OBJECT_0)
		{
			break;
		}

		// Get starting times
		::GetSystemTimes(&ftIdleTimeStart, &ftKernelTimeStart, &ftUserTimeStart);

		Sleep(1000 * m_nMonitorInterval);		

		// Get ending times
		::GetSystemTimes(&ftIdleTimeEnd, &ftKernelTimeEnd, &ftUserTimeEnd);

		//计算实际的使用率
		int nActualUsageRatio = CalcActualUsageRate(
													ftIdleTimeStart,
													ftKernelTimeStart,
													ftUserTimeStart,
													ftIdleTimeEnd,
													ftKernelTimeEnd,
													ftUserTimeEnd
													);

		if (nActualUsageRatio < m_nRequestIdleRatio)//默认CPU使用率低于20%,则认为闲
		{
				nBusyKeepingTimes = 0;
				nIdleKeepingTimes += m_nMonitorInterval;
		}
		else if (nActualUsageRatio > m_nRequestBusyRatio)//默认CPU使用率超过80%,则认为忙
		{
			nIdleKeepingTimes = 0;
			nBusyKeepingTimes += m_nMonitorInterval;
		}
		else if ((nActualUsageRatio == m_nRequestIdleRatio) //同时满足空闲和忙
			&& (m_nRequestIdleRatio == m_nRequestBusyRatio))
		{
			nIdleKeepingTimes += m_nMonitorInterval;
			nBusyKeepingTimes += m_nMonitorInterval;
		}
		else//都不满足
		{
			nBusyKeepingTimes = 0;
			nIdleKeepingTimes = 0;
		}
			
		//判断是否符合要求,并且发送消息
		if (nIdleKeepingTimes == m_nIdleKeepingTime)
		{
		            if (m_hWndRecvMsg != NULL)
		            {
			  ::PostMessage(m_hWndRecvMsg, WM_REACHED_IDLE, 0, 0);
		            }

			nIdleKeepingTimes = 0;
		}
		
		if (nBusyKeepingTimes == m_nBusyKeepingTime)
		{
		         if (m_hWndRecvMsg != NULL)
		        {
		              ::PostMessage(m_hWndRecvMsg, WM_REACHED_BUSY, 0, 0);
		         }

			nBusyKeepingTimes = 0;
		}
	}
}
Japanese comments?
Chinese comments
Topic archived. No new replies allowed.