Groups | Blog | Home
all groups > dotnet clr > october 2003 >

dotnet clr : Getting Performance Counter Data - (CPU Usage)



Rick Strahl [MVP]
10/28/2003 11:41:13 PM
Hi all,

Having some issues with retrieving some perf data. I'm getting a number of
stats that return static values just fine. But I can't retrieve values that
are 'timed' values that return data like requests per second or CPU
percentage.

For example: CPU Percentage returns 0 with:

PerformanceCounter C = new PerformanceCounter("Processor", "% Processor
Time", "_Total","CPU Utilization");

float f = C.NextValue();

The Rawvalue is set on this counter, but NextValue() is returning 0. I
suspect one has to convert the RawValue to the proper set, but I'm not sure
how this really is supposed to work for a value that is somehow sampled.

What am I missing? <g>

TIA,

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web


ryanbyi NO[at]SPAM online.microsoft.com
11/4/2003 7:03:41 PM
First I am not sure if you are intentionally do this but the following line
actually tries to get a performance counter on a machine called "CPU
Utilization"
PerformanceCounter C = new PerformanceCounter("Processor", "% Processor
Time", "_Total","CPU Utilization");

Also I think the first value of a performance counter will be inaccurate.
This is because the method used to calculate the value for NextValue
sometimes needs a previous and a current value. So when you call NextValue
for the first time the previous values does not exist and zero is returned.
Also most performance coutners should not be polled faster then once a
second.

The following code worked on my machine:

using System;
using System.Diagnostics;
using System.Threading;

public class PerCounterTest
{
public static void Main()
{
PerformanceCounter C = new PerformanceCounter("Processor", "% Processor
Time", "_Total");

Console.WriteLine(C.NextValue());
Thread.Sleep(1000);
Console.WriteLine(C.NextValue());
}
}


See the Remarks section in the following link for more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemDiagnosticsPerformanceCounterClassTopic.asp


Ryan Byington [MS]

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
--------------------
| From: "Rick Strahl [MVP]" <rickstrahl@hotmail.com>
| Subject: Getting Performance Counter Data - (CPU Usage)
| Date: Tue, 28 Oct 2003 23:41:13 -1000
| Lines: 35
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <OxKBODgnDHA.3504@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.clr
| NNTP-Posting-Host: dsl-gte-14318-2.linkline.com 64.30.213.14
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.clr:8474
| X-Tomcat-NG: microsoft.public.dotnet.framework.clr
|
| Hi all,
|
| Having some issues with retrieving some perf data. I'm getting a number of
| stats that return static values just fine. But I can't retrieve values
that
| are 'timed' values that return data like requests per second or CPU
| percentage.
|
| For example: CPU Percentage returns 0 with:
|
| PerformanceCounter C = new PerformanceCounter("Processor", "% Processor
| Time", "_Total","CPU Utilization");
|
| float f = C.NextValue();
|
| The Rawvalue is set on this counter, but NextValue() is returning 0. I
| suspect one has to convert the RawValue to the proper set, but I'm not
sure
| how this really is supposed to work for a value that is somehow sampled.
|
| What am I missing? <g>
|
| TIA,
|
| +++ Rick ---
|
| --
|
| Rick Strahl
| West Wind Technologies
| http://www.west-wind.com/
| http://www.west-wind.com/wwHelp
| ----------------------------------
| Making waves on the Web
|
|
|
|
AddThis Social Bookmark Button