Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Calculate processor usage (revisited)


  • Please log in to reply
2 replies to this topic

#1 Inquisitor

Inquisitor

    Advanced Member

  • Members
  • PipPipPip
  • 91 posts
  • LocationAtlanta, Georgia, USA

Posted 05 September 2011 - 10:17 PM

I wasn’t sure what the proper etiquette was for re-opening a thread that’s been stagnant for nearly a year. So, I opened a new one of the same name. I have a similar issue… needing to know how much I’m using (or abusing) the Netduino. I plan to do quite a bit of number crunching along with accessing and controlling lots of sensors and driving servos and/or stepper motors and/or DC motors. So I created a class to monitor the CPU. It’s rather brute force, but it seems to stay out of the way... the more you’re using the CPU for your own needs.

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!
  • aps likes this
Doing my best to keep the smoke in the little black boxes.
If my message helped you... how 'bout giving me a Posted Image
www.MessingWithReality.com

#2 aps

aps

    New Member

  • Members
  • Pip
  • 2 posts

Posted 22 November 2012 - 10:16 AM

Nice! Thanks for this, works like a charm! Jeroen

#3 Grumpy Old Fool

Grumpy Old Fool

    New Member

  • Members
  • Pip
  • 2 posts

Posted 31 March 2015 - 08:46 PM

Thank you for this awesome class.

 

I added a threshold for auto-displaying, so that it only reports CPU% if it goes above X%, and a Usage property, that returns the current usage, or if the auto-displaying is active, it returns the last result from AutoDisplay().






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.