C#实现让CPU占用率曲线听你的指挥 可指定运行核心
2012-01-04 23:19
573 查看
目标: 实现在指定核心显示正选曲线。
基础原理: Windows任务管理器(Task Manager)所显示的CPU占用率指的是一段时间内cpu使用时间所占的百分比,而不是CPU有多少被用掉了。 举个例子说一下:比如一个员工一天的工作时间是8小时,他用了4小时把任务完成,于是他这一天的使用率就是50%。对于CPU而言,在一秒钟里,CPU被使用了多少毫秒,也就是CPU在这一秒钟里的使用率。
基于这个基本原理,就有了一个理论上的实践方式: 1.确定一个工作时间片 2.指定这个时间工作时间片里CPU的工作和空闲时间。 3.指定的方法应根据需求
技术准备: 首先:实现的目标是利用任务管理器中某个核心显示正选曲线,所以应注意一下两点: 1.任务管理器无法显示负值。 2.利用正选曲线函数来填充工作时间片。 其次:Cpu核心的指定:由此参看:多核处理器中,指定线程运行在指定CPU核心上
C#实现
View Code
基础原理: Windows任务管理器(Task Manager)所显示的CPU占用率指的是一段时间内cpu使用时间所占的百分比,而不是CPU有多少被用掉了。 举个例子说一下:比如一个员工一天的工作时间是8小时,他用了4小时把任务完成,于是他这一天的使用率就是50%。对于CPU而言,在一秒钟里,CPU被使用了多少毫秒,也就是CPU在这一秒钟里的使用率。
基于这个基本原理,就有了一个理论上的实践方式: 1.确定一个工作时间片 2.指定这个时间工作时间片里CPU的工作和空闲时间。 3.指定的方法应根据需求
技术准备: 首先:实现的目标是利用任务管理器中某个核心显示正选曲线,所以应注意一下两点: 1.任务管理器无法显示负值。 2.利用正选曲线函数来填充工作时间片。 其次:Cpu核心的指定:由此参看:多核处理器中,指定线程运行在指定CPU核心上
C#实现
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Threading; using System.Diagnostics; namespace ConsoleApplication1 { public class Program { [DllImport("kernel32.dll")] static extern uint GetTickCount(); //SetThreadAffinityMask 指定hThread 运行在 核心 dwThreadAffinityMask [DllImport("kernel32.dll")] static extern UIntPtr SetThreadAffinityMask(IntPtr hThread, UIntPtr dwThreadAffinityMask); //得到当前线程的handler [DllImport("kernel32.dll")] static extern IntPtr GetCurrentThread(); static void Main(string[] args) { Thread t1 = new Thread(new ParameterizedThreadStart(sinaG)); Console.Write("Which core you will to use (Start from 0):"); string core = Console.ReadLine(); int coreNumber = 0; try { coreNumber = Int32.Parse(core); } catch { coreNumber = 0; } t1.Start(coreNumber); } static void sinaG(object coreNumber) { int core = 0; try { core = (int)coreNumber; } catch { core = 0; } SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(core))); //让指定的cpu占用率成直线,保持在50%左右 //int busyTime = 10; //int idleTime = busyTime; //Int64 startTime = 0; //while(true) //{ // startTime = System.Environment.TickCount; // while ((System.Environment.TickCount - startTime) <= busyTime) // ; // System.Threading.Thread.Sleep(idleTime); //} //可以手工输入1-100任意数值的暂用率,动态设定 //Console.Write("Which percent you will to use (Range(1-100),Start from 1):"); //string percent = Console.ReadLine(); //float n = 1; //if (float.TryParse(percent, out n)) //{ // MakeUsage(n); //} //else //{ // MakeUsage(1); //} //正弦曲线 //split*count=2;也就是正弦函数的周期2 Pi,也就是把一个周期的细分为200份 //double split = 0.01; //int count = 200; //double pi = 3.1415962525; ////工作周期 300 ms //int interval = 300; ////每个工作周期里工作和空闲的毫秒数 //int[] busySpan = new int[count]; //int[] idealSpan = new int[count]; ////根据正弦函数计算并填入每个工作周期的工作和空闲毫秒数 //int half = interval / 2; //double radian = 0.0; //for (int i = 0; i < count; i++) //{ // busySpan[i] = (int)(half + Math.Sin(pi * radian) * half); // idealSpan[i] = interval - busySpan[i]; // radian += split; //} //uint startTime = 0; //int j = 0; //while (true) //{ // j = j % count; // startTime = GetTickCount(); // while ((GetTickCount() - startTime) <= busySpan[j]) // { // ; // } // Thread.Sleep(idealSpan[j]); // j++; //} } static void MakeUsage(float level) { PerformanceCounter p=new PerformanceCounter("Processor Information","% Processor Time","_Total"); if(p==null) return; while(true) { if(p.NextValue()>=level) System.Threading.Thread.Sleep(10); } } //函数中的参数 dwThreadAffinityMask 为无符号长整型,用位标识那个核心 //比如:为简洁使用四位表示 //0x0001表示核心1, //0x0010表示核心2, //0x0100表示核心3, //0x1000表示核心4 static ulong SetCpuID(int id) { ulong cpuid = 0; if (id < 0 || id >= System.Environment.ProcessorCount) { id = 0; } cpuid |= 1UL << id; return cpuid; } } }
相关文章推荐
- [.NET][编程之美][1.1]C# 实现让CPU占用率曲线听你的指挥 – 可指定运行核心
- 让多核CPU占用率曲线听你指挥(Windows实现)——《编程之美》1.1学习笔记
- 让多核CPU占用率曲线听你指挥(Windows实现)——《编程之美》1.1学习笔记
- 让多核CPU占用率曲线听你指挥(Linux实现)——《编程之美》1.1继续学习
- 让多核CPU占用率曲线听你指挥(Linux实现)——《编程之美》1.1继续学习
- 让多核CPU占用率曲线听你指挥(Linux实现)——《编程之美》1.1继续学习
- 使用C#指挥CPU占用率曲线
- 写程序 控制cpu占用率-4 绘制CPU使用率的正弦曲线2 通过C#指定线程运行的 CPU
- 怎样让Windows任务管理器CPU占用率呈现正玄曲线(解释+C#实现)
- 编程之美:让CPU占用率曲线听你指挥
- 让cpu占用率曲线听你的指挥
- 让CPU占用率曲线听你指挥
- [趣味编程]CPU占用率曲线听我指挥
- C#实现测量程序运行时间及cpu使用时间
- 编程之美读书笔记1.1——让CPU占用率曲线听你的指挥
- 让CPU占用率曲线听你指挥—微软技术面
- 编程之美 1.1 让cpu占用率曲线听你指挥(多核处理器)
- 让CPU占用率曲线听你指挥---linux
- 编程之美 1.1 让CPU占用率曲线听你的指挥
- C#实现测量程序运行时间及cpu使用时间