Here is a Program.cs file to demonstrate its use. Basically, it’s a one-liner to add the CPUUsage object to your program and you can add it wherever you want (it doesn’t have to be at the beginning). Once running, the demo program will explicitly use to 10% of the CPU. It shows CPU=11% on my Netduino. There seems to be a little overhead of other things going on, but I’m not sure if it’s because of the CPUUsage object or not. Clicking the on-board switch causes the CPU to up the CPU usage by 10% until 100% and then re-cycles back down to 10%.
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.IO.Ports;
using System.Text;
using MWR;
namespace Serial_Port
{
public class Program
{
const int WORK = 100; // ms to work... use something >= 1
private static InterruptPort _Switch;
private static double _Use;
public static void Main()
{
// Create our CPU Usage Object
new CPUUsage();
// Hook up to the switch so we demonstrate changing the CPU usage
_Switch = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled,
Port.InterruptMode.InterruptEdgeBoth);
_Switch.OnInterrupt += new NativeEventHandler(Switch_OnInterrupt);
// Percentage of work to start with.
_Use = 0.1; // 10%
// Now do something to tie up the CPU
double W = System.Math.PI;
while (true)
{
long f = DateTime.Now.Ticks + 10000 * WORK;
while (DateTime.Now.Ticks < f)
{
W *= System.Math.PI;
W = ((int)W) - W;
}
int delay = 0;
lock (_Switch)
{
if (_Use < 100)
delay = (int)(WORK / _Use - WORK);
}
if (delay > 0)
Thread.Sleep(delay);
}
}
private static void Switch_OnInterrupt(uint data1, uint state, DateTime time)
{
if (state == 0)
return;
lock (_Switch)
{
_Use += 0.1;
if (_Use > 1.0)
_Use = 0.1;
}
}
}
}
Here is the CPUUsage class.
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;
namespace MWR
{
public class CPUUsage
{
private int _msAutoDisplay;
private long _idle;
private long _last;
/// <summary>
/// Create this object to monitor the CPU usage.
/// Diagnostics go to the VS Output window.
/// </summary>
/// <param name="msAutoDisplay">Amount of time (ms) between updates</param>
public CPUUsage(int msAutoDisplay = 1000)
{
_msAutoDisplay = msAutoDisplay;
if (_msAutoDisplay > 0)
{
Thread t = new Thread(new ThreadStart(AutoDisplay));
t.Priority = ThreadPriority.Lowest;
t.Start();
}
new Thread(Idler).Start();
}
private void AutoDisplay()
{
lock (this)
{
_idle = 0;
_last = DateTime.Now.Ticks;
}
while (true)
{
Thread.Sleep(_msAutoDisplay);
long per;
lock (this)
{
long now = DateTime.Now.Ticks;
per = 100 - _idle * 100 / (now - _last);
_last = now;
_idle = 0;
}
Debug.Print("CPU=" + per.ToString() + "%");
}
}
private void Idler()
{
long last = DateTime.Now.Ticks;
while (true)
{
PowerState.WaitForIdleCPU(1, int.MaxValue);
long now = DateTime.Now.Ticks;
long delta = now - last;
if (delta < 10000) // 1 ms
lock (this)
_idle += delta;
last = now;
}
}
}
}
Hope this is useful to someone. And, of course, let me know if there is a better way. Thanks.
Good Luck!