Sei sulla pagina 1di 9

NORTH CAROLINA A&T STATE UNIVERSITY

CPU-Based Timer
Pervasive Computing: Spring 2014
Take-Home Quiz 5

Monique Kirkman-Bey
2/24/2014





Kirkman-Bey Take-Home Quiz 5 1

CPUBasedStopwatch Overview

The stopwatch that follows has the ability to count to 99 minutes and 59.99 seconds. It
operates like a typical stopwatch with the ability to start, pause, and reset. Additionally, the
stopwatch utilizes context awareness and updates its display precision based on the current CPU
usage. In this design, the threshold for altering the displayed precision is set to 30%. When the
CPU usage is below the threshold value, the stopwatch will display the timer with 0.01 second
precision. If the CPU usage is above the threshold, the timer will continue to maintain 0.01
second precision, but will only display 0.1 second precision at the user interface.
Additionally, the current CPU usage is displayed in two fashions - continuously and
instantaneously. The continuously monitored value is displayed and updated in real-time.
However, the dynamic changes in the CPU usage lead to rapidly changing values that are
difficult to read. In the event the user needs to know the CPU usage, the user simply needs to
click the Get Instantaneous CPU Usage button. This action will capture the current usage
value, and display that value until the button is pressed again. It allows the user an opportunity to
see the usage long enough to be read by the human eye. It is important to note that this value is
only updated when the Get Instantaneous CPU Usage button is pressed.



Kirkman-Bey Take-Home Quiz 5 2

CPUBasedTimer Operation and GUI Screenshots

When the CPUBasedTimer program is loaded, the user is presented with the GUI shown in
Figure 1. The timer display format is Minutes:Seconds:Centiseconds. You can see that there
are 2 buttons to control the timer: Start and Reset. To the right of the timer, the user can monitor
the usage in two ways as explained above. It can also be seen that there is no value displayed for
instantaneous usage because the Get Instantaneous CPU Usage button has not been pressed


Figure 1: CPUBasedTimer GUI

The timer can also be paused once started. After the Start button is pressed, the text updates and
it becomes the Stop button, as shown below in Figure 2. Pressing the Stop button will cause the
timer to pause.


Figure 2: Pausing the CPUBasedTimer Program

After pressing the Get Instantaneous CPU Usage button, additional text is visible below the
button which displays the instantaneous usage of the CPU. This can be seen below in Figure 3.
Please note that the instantaneous usage was, in-fact, 0% at this point even though the counter is
currently running. This is confirmed by the windows task manager as shown next to the
CPUBasedTimer window.

Kirkman-Bey Take-Home Quiz 5 3


Figure 3: Displaying Instantaneous CPU Usage

To increase the usage, a 30-second loop was implemented and published. After implementing the
loop, CPU usage for my virtual machine jumped to 100%. It is important to note here that the
Visual Studio software is implemented in a virtual machine with expanding resources. So, the
task manager monitors the CPU usage of the virtual machine, and not necessarily the CPU usage
of the physical mac on which the virtual machine is run. The 100% CPU usage value caused the
display precision to decrease due to its being above the 30% threshold. Hence, in Figure 4 below
you will see that both an updated user message and updated timer precision are displayed.
Additionally, the task manager usage is displayed alongside the timer to confirm accurate CPU
usage monitoring.


Figure 4: Display Precision Decrease Due to CPU Usage Above 30%



Kirkman-Bey Take-Home Quiz 5 4

CPUBasedStopwatch UML Class Diagram

In Figure 5 below, the UML Class Diagram for the CPUBasedStopwatch program is
displayed. It can be seen that the program contains single class, called Form1. This class has the
following attributes: TenthSecond, HundredthSecond, Sec1, Sec10, Min1, Min10, usage,
HighUsage, digits, Start_Stop. The first six attributes correspond to the respective labels that
display the values of the counter as described by the attribute names. The usage variable is used
to store the value of the continuously monitored CPU usage. HighUsage is a flag that is either
true or false if the threshold value has been exceeded or not, respectively. Digits is a string array
that holds the values 1-10 for quicker conversion between int and string when the values need to
be displayed on the labels. Lastly, Start_Stop is a boolean flag that allows the program to
determine if the start button or stop button has been clicked.
This class has 4 methods and a constructor. Form1 is simply the constructor of the class.
Timer1_tick handles the behavior of the timer, the timer labels, and the continuously monitored
CPU usage variable. Start_Stop_Button_Click responds to the Start or Stop button being clicked
and sets the Start_Stop flag accordingly. Button1_click allows the instantaneous CPU usage to
be obtained when desired. Lastly, the ResetButton_Click method stops and resets the stopwatch
when the Reset button is clicked.
Please note that hidden classes, such as the Program class which implements the Main()
function, and the resources and setting class are not displayed in this diagram because they are
computer generated classes.
+Form1()
+timer1_Tick(in sender : object, in e :EventArgs)
-Start_Stop_Button_Click(in sender : object, in e : EventArgs)
-button1_Click(in sender : object, in e :EventArgs)
-ResetButton_Click(in sender : object, in e : EventArgs)
-TenthSecond : int
-HundredthSecond : int
-Sec1 : int
-Sec10 : int
-Min1 : int
-Min10 : int
-usage : int
-HighUsage : bool
-digits : string
-Start_Stop : bool
Form1

Figure 5: Simple UML Class Diagram
A more complete UML Class Diagram, including hidden objects and attributes, was
generated in Visual Studio 2013 and is displayed in Figure 6.
Kirkman-Bey Take-Home Quiz 5 5




Figure 6: Visual Studio 2013 Generated UML Class Diagram



Kirkman-Bey Take-Home Quiz 5 6

CPUBasedStopwatch C# code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace CPUBasedStopwatch
{
public partial class Form1 : Form //Class
{
//Below, each digit is controlled individually within the CPUBased timer
int TenthSecond;
int HundredthSecond;
int Sec1;
int Sec10;
int Min1;
int Min10;
int usage;
bool HighUsage = false; //True when CPU Usage > 30%

/*you will notice that the number 10 is included within the string array, although it
is never displayed. This was done to overcome the error that was thrown when testing
the condition "if (integer == 10)"*/
string[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
bool Start_Stop = false; //True when Start is pressed, False when stop is pressed

public Form1() //Constructor
{
InitializeComponent();
}

public void timer1_Tick(object sender, EventArgs e) //Method
{
/*this first sequence of code allows me to convert the string currently being
displayed in the respective label to integer values so that they can be
incremented as they would in a numerical system*/
HundredthSecond = Convert.ToInt16(HundredthSecondLabel.Text);
TenthSecond = Convert.ToInt16(TenthSecondLabel.Text);
Sec1 = Convert.ToInt16(SecondsOnesLabel.Text);
Sec10 = Convert.ToInt16(SecondsTensLabel.Text);
Min1 = Convert.ToInt16(MinutesOnesLabel.Text);
Min10 = Convert.ToInt16(MinutesTensLabel.Text);

//The value of the Performance counter is assigned to usage
usage = (int)PerformanceCounter1.NextValue();

//These conditions allow the precision to be updated based on the 30% threshold
if (usage > 30)
{
HighUsage = true;
HundredthSecondLabel.Visible = false;
label1.Text = "CPU Usage above 30%. Timer precision reduced to 0.1 second.";
}
Kirkman-Bey Take-Home Quiz 5 7

else if (usage < 30)
{
HighUsage = false;
HundredthSecondLabel.Visible = true;
label1.Text = "CPU Usage below 30%. 0.01 second timer precision available.";
}

//Displays the CPU Usage in label3
label3.Text = "Continuously Monitored: " + usage + "% CPU Usage";

/*if the start button is pressed, increment the counters as follows. When the
stop button is pressed, do not increment the counters*/
if (Start_Stop == true)
{
if (++HundredthSecond == 10)
{
HundredthSecond = 0;
TenthSecond++;

if (TenthSecond == 10)
{
TenthSecond = 0;
Sec1++;

if (Sec1 == 10)
{
Sec1 = 0;
Sec10++;

if (Sec10 == 6)
{
Sec10 = 0;
Min1++;

if (Min1 == 10)
{
Min1 = 0;
Min10++;

if (Min10 == 10)
{
Start_Stop = false;
Start_Stop_Button.Text = "Start";
HundredthSecond = 0;
TenthSecond = 0;
Sec1 = 0;
Sec10 = 0;
Min1 = 0;
Min10 = 0;
}

}
}
}
}
}
}

/*the following code allows the integers to quickly be converted to strings
available in the string array. This method is faster than the simple
convert.string method because there is a much smaller string array when
Kirkman-Bey Take-Home Quiz 5 8

selecting the appropriate string value*/
HundredthSecondLabel.Text = digits[HundredthSecond];
TenthSecondLabel.Text = digits[TenthSecond];
SecondsOnesLabel.Text = digits[Sec1];
SecondsTensLabel.Text = digits[Sec10];
MinutesOnesLabel.Text = digits[Min1];
MinutesTensLabel.Text = digits[Min10];


}

private void Start_Stop_Button_Click(object sender, EventArgs e) //Method
{
//if start/stop button pressed, negate the value of the start_stop "flag"
Start_Stop = !Start_Stop;

/*update the text in the start_stop_button to reflect the action that will be
executed if the button is clicked again*/
if (Start_Stop_Button.Text == "Start")
Start_Stop_Button.Text = "Stop";
else if (Start_Stop_Button.Text == "Stop")
Start_Stop_Button.Text = "Start";
}

//This handler responds to the "Get Instantaneous CPU Usage" button click
public void button1_Click(object sender, EventArgs e) //Method
{
/*a second Performance Counter, with the same parameters as that of the first
is implemented */
int usage2;
usage2 = (int)PerformanceCounter2.NextValue();
label2.Text = "Instantaneuous Usage: " + usage2 + " % CPU Usage";
}

/*This handler responds to the reset button click. It stops the stopwatch and
resets the stopwatch to its inital state */
private void ResetButton_Click(object sender, EventArgs e) //Method
{
Start_Stop = false;
Start_Stop_Button.Text = "Start";
HundredthSecond = 0;
TenthSecond = 0;
Sec1 = 0;
Sec10 = 0;
Min1 = 0;
Min10 = 0;

HundredthSecondLabel.Text = digits[HundredthSecond];
TenthSecondLabel.Text = digits[TenthSecond];
SecondsOnesLabel.Text = digits[Sec1];
SecondsTensLabel.Text = digits[Sec10];
MinutesOnesLabel.Text = digits[Min1];
MinutesTensLabel.Text = digits[Min10];
}
}
}

Potrebbero piacerti anche