Purpose: display the positive curve at the specified core.
Basic principle: the CPU usage displayed by Windows Task Manager refers to the percentage of CPU usage time in a period of time, rather than the CPU usage. For example, if an employee spends 8 hours a day and 4 hours completing the task, the daily usage rate is 50%. For the CPU, the number of milliseconds the CPU is used in one second, that is, the CPU usage in this second.
Based on this basic principle, there is a theoretical practice: 1. Determine a work time slice 2. Specify the CPU work and idle time in the work time slice. 3. The specified method should be as required
Technical preparation: first, the goal is to use a core in the task manager to display the positive curve. Therefore, pay attention to two points: 1. the Task Manager cannot display negative values. 2. Use the positive curve function to fill the work time slice. Second, specify the CPU core. For details, see: In a multi-core processor, the specified thread runs on the specified CPU core.
C # implementation
View code
1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5 Using System. runtime. interopservices;
6 Using System. Threading;
7 Using System. diagnostics;
8
9 Namespace Leleapplication1
10 {
11 Public Class Program
12 {
13 [Dllimport ( " Kernel32.dll " )]
14 Static Extern Uint Gettickcount ();
15
16 // Setthreadaffinitymask specifies that hthread runs on the core dwthreadaffinitymask
17 [Dllimport ( " Kernel32.dll " )]
18 Static Extern Uintptr setthreadaffinitymask (intptr hthread,
19 Uintptr dwthreadaffinitymask );
20
21 // Get the handler of the current thread
22 [Dllimport ( " Kernel32.dll " )]
23 Static Extern Intptr getcurrentthread ();
24
25 Static Void Main ( String [] ARGs)
26 {
27 Thread T1 = New Thread ( New Parameterizedthreadstart (sinag ));
28 Console. Write ( " Which core you will to use (start from 0 ): " );
29 String Core = console. Readline ();
30 Int Corenumber = 0 ;
31 Try
32 {
33 Corenumber = int32.parse (CORE );
34 }
35 Catch
36 {
37 Corenumber = 0 ;
38 }
39 T1.start (corenumber );
40 }
41 Static Void Sinag ( Object Corenumber)
42 {
43 Int Core = 0 ;
44 Try
45 {
46 Core = ( Int ) Corenumber;
47 }
48 Catch
49 {
50 Core = 0 ;
51 }
52 Setthreadaffinitymask (getcurrentthread (), New Uintptr (setcpuid (CORE )));
53
54 // Keep the specified CPU usage in a straight line at around 50%
55 // Int busytime = 10;
56 // Int idletime = busytime;
57 // Int64 starttime = 0;
58 // While (true)
59 // {
60 // Starttime = system. environment. tickcount;
61 // While (system. environment. tickcount-starttime) <= busytime)
62 // ;
63 // System. Threading. thread. Sleep (idletime );
64 // }
65
66 // You can manually enter the temporary usage rate of any number ranging from 1 to and set it dynamically.
67 // Console. Write ("which percent you will to use (range (1-100), start from 1 ):");
68 // String percent = console. Readline ();
69 // Float n = 1;
70 // If (float. tryparse (percent, out n ))
71 // {
72 // Makeusage (N );
73 // }
74 // Else
75 // {
76 // Makeusage (1 );
77 // }
78
79 // Sine Curve
80 // Split * COUNT = 2; that is, the period 2 pi of the sine function, that is, dividing a period into 200 parts.
81 // Double Split = 0.01;
82 // Int COUNT = 200;
83
84 // Double Pi = 3.1415962525;
85
86 /// /Work cycle 300 MS
87 // Int interval = 300;
88
89 /// /Number of idle and idle milliseconds in each work cycle
90 // Int [] busyspan = new int [count];
91 // Int [] idealspan = new int [count];
92
93 /// /Calculate and enter the number of working and idle milliseconds in each work cycle based on the sine function.
94 // Int half = interval/2;
95 // Double radian = 0.0;
96 // For (INT I = 0; I <count; I ++)
97 // {
98 // Busyspan [I] = (INT) (half + math. Sin (pI * radian) * Half );
99 // Idealspan [I] = interval-busyspan [I];
100 // Radian + = split;
101 // }
102
103 // Uint starttime = 0;
104 // Int J = 0;
105 // While (true)
106 // {
107 // J = J % count;
108 // Starttime = gettickcount ();
109 // While (gettickcount ()-starttime) <= busyspan [J])
110 // {
111 // ;
112 // }
113 // Thread. Sleep (idealspan [J]);
114 // J ++;
115 // }
116
117 }
118
119 Static Void Makeusage ( Float Level)
120 {
121 Performancecounter P = New Performancecounter ( " Processor Information " , " % Processor time " , " _ Total " );
122 If (P = Null )
123 Return ;
124 While ( True )
125 {
126 If (P. nextvalue ()> = level)
127 System. Threading. thread. Sleep ( 10 );
128 }
129 }
130 // The dwthreadaffinitymask parameter in the function is an unsigned long integer and identifies the core with bits.
131 // For example, to use a concise four-digit representation
132 // 0x0001 indicates core 1,
133 // 0x0010 indicates Core 2,
134 // 0x0100 indicates core 3,
135 // 0x1000 indicates Core 4
136 Static Ulong Setcpuid ( Int ID)
137 {
138 Ulong Cpuid = 0 ;
139 If (ID < 0 | ID> = system. environment. processorcount)
140 {
141 Id = 0 ;
142 }
143 Cpuid | = 1ul <ID;
144 Return Cpuid;
145 }
146 }
147 }