( Index )
Month

Brief Information about the Oct '08 CSIG Meeting

Visual Studio C++ Express 2008 Introduction

Windows Performance Counters
B. Arnold

Cpu History          Cpu History

Welcome to the CSIG, a Special Interest Group of the ACGNJ. This is an exciting time for the C Language programming since Microsoft now has 4 different language compilers: C++, C++ Express, C-Sharp, and C-Sharp Express. These are all capable of creating Windows (tm) programs.

The newest versions are designated Visual Studio 2008. There are "Express" versions otherwise know as "Free" as well as the commercial versions. The Express versions are fully functional for creating Managed Code. Unless you work in a large program development company, you probably won't need the broader features of the commercial versions. For other companies check last month's CSIG page.

Checkout the Development Tools

Click this image... Microsoft Link

In order to demonstrate the C++ Express Compiler I have expanded the program from last season called CPU Usage History. This program uses 2 of the Windows Performance Counters which are provided inside the operating system. Yes, they're in there right now. Windows 2000, WinXP, and of course Windows Vista. A utility will be created using this concept for displaying the current activity of the CPU (Central Processing Unit) of the computer.

What's New in Visual Studio 2008

Click this image... Microsoft Link

What's New in Visual C++ 2008

Click this image... Microsoft Link

There are a number of ways to refer to this compiler and code. Here's what Wikipedia says: The Common Language Infrastructure (CLI) is an open specification developed by Microsoft that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework. The specification defines an environment that allows multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.

Microsoft .Net Framework 2.0
C++ 7.0
.Net 2.0
CLI
Common Language Infrastructure
Managed

The .net programming enviornment provides a special class within the System.Diagnostics namespace. This PerformanceCounter class allows access to the hundreds of performance counters that are contained in the operating system. (It also provides the ability to create new counters.) At the meeting, we will explore this counter mechanism and demonstrate a program utility for displaying some of the counters. The screen shot above shows a display similar to part of the Task Manager.

And here is a list taken from the IDE: Representative Performance Counters List

Sample Code

//  CPU History by B.Arnold
//
//  The object of this program is to display the CPU Usage History for
//  the last several minutes.  It uses the Performance Counters that
//  are built into the operating system.  The display is a graph like
//  a partial view of the Windows Task Manager but with the display
//  filtered with a 10 second low pass filter.

//      Version 1.10    Oct 2008    Notify Icon replaces taskbar when minimized
//                                  and Taskbar panel is now hidden.
//                                  2nd red line added for USER percent usage.
//      Version 1.00    May 2008    Basic version with CPU history.

......

#define GRIDPOINTS  150
#define GRIDSCALE   2
#define GRIDWIDTH   (GRIDPOINTS * GRIDSCALE)
#define GRIDHEIGHT  100

......

    // Land here every second.
    private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
                 // Move all data points down 1 index position.
                 int i;
                 for (i=0; i<GRIDPOINTS-1; i++)
                 {
                     PCprocTime[i] = PCprocTime[i+1];
                     PCuserTime[i] = PCuserTime[i+1];
                 }
                 // Now put the latest data at the end.
                 PCprocTime[i] = filter1(performanceCounter1->NextValue());
                 PCuserTime[i] = filter2(performanceCounter2->NextValue());
                 // Display textual data in real time.
                 this->Text = String::Format("{0} % All, {1} % User", PCprocTime[i], PCuserTime[i]);
                 this->notifyIcon1->Text = this->Text;
                 pictureBox1->Invalidate();
             }

    // Paint the form and the data onto the screen.  Draw the graph on the screen.
    private: System::Void pictureBox1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
                 Pen ^mPen1 = gcnew Pen(System::Drawing::Color::LawnGreen, 4.0F);
                 Pen ^mPen2 = gcnew Pen(System::Drawing::Color::Red, 4.0F);

                 array<Point>^ mLine1 = gcnew array<Point>(GRIDPOINTS); 
                 array<Point>^ mLine2 = gcnew array<Point>(GRIDPOINTS); 

                 for (int i=0; i<GRIDPOINTS; i++)   // Build arrays to define graph lines.
                 {
                     mLine1[i].X = i*GRIDSCALE;
                     mLine1[i].Y = GRIDHEIGHT - PCprocTime[i];
                     mLine2[i].X = i*GRIDSCALE;
                     mLine2[i].Y = GRIDHEIGHT - PCuserTime[i];
                 }
                 e->Graphics->DrawLines(mPen2, mLine2);     // Draw red line
                 e->Graphics->DrawLines(mPen1, mLine1);     // Draw green line

                 if (PCprocTime[GRIDPOINTS-1] >= 99)
                     this->notifyIcon1->ShowBalloonTip(3000, "Cpu very busy.", 
                            "Usage is over 99 percent", ToolTipIcon::Warning);
             }

    // Show the application version information
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             MessageBox::Show("CPU History version 1.10, Oct 2008, by B. Arnold");
         }

......

Note: typically you will need the MS C++ Redistribution Package to run programs on any computer.


"Random Access" questions start at 7:30 Tuesday night.

SOURCE CODE

Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page