C#实现让CPU占用率曲线听你的指挥 可指定运行核心

目标: 实现在指定核心显示正选曲线。
基础原理: Windows任务管理器(Task Manager)所显示的CPU占用率指的是一段时间内cpu使用时间所占的百分比,而不是CPU有多少被用掉了。 举个例子说一下:比如一个员工一天的工作时间是8小时,他用了4小时把任务完成,于是他这一天的使用率就是50%。对于CPU而言,在一秒钟里,CPU被使用了多少毫秒,也就是CPU在这一秒钟里的使用率。
基于这个基本原理,就有了一个理论上的实践方式: 1.确定一个工作时间片 2.指定这个时间工作时间片里CPU的工作和空闲时间。 3.指定的方法应根据需求
技术准备: 首先:实现的目标是利用任务管理器中某个核心显示正选曲线,所以应注意一下两点: 1.任务管理器无法显示负值。 2.利用正选曲线函数来填充工作时间片。 其次:Cpu核心的指定:由此参看:多核处理器中,指定线程运行在指定CPU核心上

C#实现

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 ConsoleApplication1
10 {
11 public class Program
12 {
13 [DllImport("kernel32.dll")]
14 static extern uint GetTickCount();
15
16 //SetThreadAffinityMask 指定hThread 运行在 核心 dwThreadAffinityMask
17 [DllImport("kernel32.dll")]
18 static extern UIntPtr SetThreadAffinityMask(IntPtr hThread,
19 UIntPtr dwThreadAffinityMask);
20
21 //得到当前线程的handler
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 //让指定的cpu占用率成直线,保持在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 //可以手工输入1-100任意数值的暂用率,动态设定
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 //正弦曲线
80 //split*count=2;也就是正弦函数的周期2 Pi,也就是把一个周期的细分为200份
81 //double split = 0.01;
82 //int count = 200;
83
84 //double pi = 3.1415962525;
85
86 ////工作周期 300 ms
87 //int interval = 300;
88
89 ////每个工作周期里工作和空闲的毫秒数
90 //int[] busySpan = new int[count];
91 //int[] idealSpan = new int[count];
92
93 ////根据正弦函数计算并填入每个工作周期的工作和空闲毫秒数
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 //函数中的参数 dwThreadAffinityMask 为无符号长整型,用位标识那个核心
131 //比如:为简洁使用四位表示
132 //0x0001表示核心1,
133 //0x0010表示核心2,
134 //0x0100表示核心3,
135 //0x1000表示核心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 }
原文地址:https://www.cnblogs.com/tewuapple/p/2312579.html