1.1 let the CPU usage curve listen to your command [cpu manager]

Source: Internet
Author: User

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/cpu-manager.html

[Question]

Write a program to determine the CPU usage of the Windows Task Manager. The simpler the program, the better the computer language. For example, you can implement the following three situations:

1. The CPU usage is fixed at 50%, which is a straight line;

2. the CPU usage is a straight line, but the specific usage is determined by the command line parameters (parameter range: 1 ~ 100 );

3. The CPU usage status is a sine curve.

[Analysis]

If you do not consider the CPU usage of other programs, you can open a thread on each core and run the specified function to achieve the same CPU usage for each core.

To enable CPU usage, the function y = calc (t) (0 <= y <= 1, t is time, unit: ms) is distributed, as long as a series of points with short intervals are obtained, the y value remains the same in a certain interval.

When the interval value is set to GAP, it is obvious that the GAP is near the specified value,

CPU usage time: busy = GAP * calc (t ),

CPU idle time: idle = GAP-busy

(1) The CPU usage is fixed at 50%, and the cacl (t) returns a constant value of 0.5;

(2) The CPU usage is fixed at p %, and a common value p/100 is returned for cacl (t;

(3) If the CPU usage status is a sine curve, then y = 0.5*(1 + sin (a * t + B ))

Cycle T = 2 * PI/a (PI = 3.1415927). If the tvalue is 60 s, that is, 60000 ms, the value can be determined to be 2 * PI/T, if we calculate 200 times (c = 200) within ms, the GAP value is T/c = Ms. that is to say, as long as the cycle and the number of computing times are determined, other parameters are also determined.

You can create a thread and run it on the specified processor. You can use the CreateThread, SetThreadAffinityMask, and WaitForSingleObject functions.

[Code]

C ++ Code
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
  // 01_CPUManager.cpp: Defines the entry point for the console application.
//
/*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/24
*/
# Include "stdafx. h"
# Include <cmath>
# Include <iostream>
# Include <Windows. h>
Using namespace std;

# Define GAP_LINEAR 100
# Define RATIO 0.5

Typedef double Func (double );

Double cacl_linear (double ratio)
{
Return ratio;
}

Void Solve_Linear (Func * cacl)
{
Unsigned BUSY_TIME = GAP_LINEAR * cacl (RATIO); // MS
Unsigned IDLE_TIME = GAP_LINEAR-BUSY_TIME; // MS
INT64 startTime = 0;
While (true)
{
// Busy loop
StartTime = GetTickCount ();
While (GetTickCount ()-startTime <BUSY_TIME)
;
// Idle loop
Sleep (IDLE_TIME );
}
}

Void Run_Linear ()
{
// Run on processor 1
HANDLE handle;
DWORD thread_id;
Handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) Solve_Linear,
(VOID *) cacl_linear, 0, & thread_id );
If (handle! = NULL)
SetThreadAffinityMask (handle, 1); // run on process #1
WaitForSingleObject (handle, INFINITE );
}

/*
Y = calc (t) (0 <= y <= 1)
GAP
Busy: GAP * calc (t)
Idle: GAP-busy

Y = 0.5*(1 + sin (a * t + B ))
*/
Const int PERIOD = 60*1000; // MS
Const int COUNT = 200;
Const double GAP = (double) PERIOD/COUNT;
Const double PI = 3.1415926;
Const double A = (2 * PI)/PERIOD;
Double cacl_sin (double t)
{
// T = 200 * gap, 2 * gap,..., * gap
Return (1 + sin (A * t)/2;
}

Void Solve_Sin (Func * cacl)
{
Double BUSY_TIME [COUNT]; // MS
Double t = 0.0;
For (int I = 0; I <COUNT; ++ I)
{
T = I * GAP;
BUSY_TIME [I] = GAP * cacl (t );
}
Int I = 0;
INT64 startTime = 0;
Unsigned busyTime, idleTime;
While (true)
{
If (I> = COUNT)
I = 0;
BusyTime = BUSY_TIME [I];
IdleTime = GAP-busyTime;
// Busy loop
StartTime = GetTickCount ();
While (GetTickCount ()-startTime <busyTime)
;
// Idle loop
Sleep (idleTime );
}
}

Void Run_Sin ()
{
// Run on processor 2
HANDLE handle;
DWORD thread_id;
Handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) Solve_Sin,
(VOID *) cacl_sin, 0, & thread_id );
If (handle! = NULL)
SetThreadAffinityMask (handle, 2); // run on process #2
WaitForSingleObject (handle, INFINITE );
}

Void test_main ()
{
Run_Linear ();
Run_Sin ();
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
Test_main ();
Return 0;
}

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/cpu-manager.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.