Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"% Disk Time" Performance Counter always returns zero

367 views
Skip to first unread message

BMeyer

unread,
Nov 12, 2002, 1:27:42 PM11/12/02
to
The WMI newsgroups suggested I post my question here since they were unable
to provide an answer.

I'm trying to use the "% Disk Time" Preformance Counter under the Physical
Disk category but it always returns a value of zero. I noticed the
CounterType is blank in the Properties window so not sure if this means
anything. Is this counter available in XP? If so, do I need to do some
additional work to get a valid value?


I'm running XP and developing on C# .NET and PerfMon.exe reflects changing
values for "% Disk Time".

Any insight is appreciated.

Basil


BMeyer

unread,
Nov 12, 2002, 3:46:05 PM11/12/02
to
Here's a paraphrased version...


private System.Diagnostics.PerformanceCounter DiskIOPercent;

public MyDiskIOTestApp()
{
InitializeComponent();

// initialize the diskIO counter
float DiskIO = DiskIOPercent.NextValue();

while (true)
{
DiskIO = DiskIOPercent.NextValue();

if (DiskIO > 0.0)
break;

// sleep for 10 seconds
System.Threading.Thread.Sleep( new TimeSpan( 0, 0, 10 ) );
}
}

private void InitializeComponent()
{
this.DiskIOPercent = new System.Diagnostics.PerformanceCounter();

((System.ComponentModel.ISupportInitialize)(this.DiskIOPercent )).BeginInit(
);

//
// DiskIOPercent
//
this.DiskIOPercent.CategoryName = "PhysicalDisk";
this.DiskIOPercent.CounterName = "% Disk Time";
this.DiskIOPercent.InstanceName = "_Total";

((System.ComponentModel.ISupportInitialize)(this.DiskIOPercent )).EndInit();
}


"Willy Denoyette [MVP]" <willy.d...@pandora.be> wrote in message
news:#sQ$2MoiCHA.1784@tkmsftngp09...
> Please post your code.
>
> Willy.
>
> "BMeyer" <bme...@krollontrack.com> wrote in message
news:#7NxainiCHA.2580@tkmsftngp12...

Willy Denoyette [MVP]

unread,
Nov 12, 2002, 2:42:14 PM11/12/02
to
Please post your code.

Willy.

"BMeyer" <bme...@krollontrack.com> wrote in message news:#7NxainiCHA.2580@tkmsftngp12...

Li-Yan Zhang [MS]

unread,
Nov 13, 2002, 3:48:07 AM11/13/02
to
This is a known bug, please read the following KB article:

BUG: NextValue Method of .NET PerformanceCounter Object Returns Zero
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q324548

Thanks,
Li-Yan Zhang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com

BMeyer

unread,
Nov 13, 2002, 12:31:41 PM11/13/02
to
Thanks for the link. However, since Perfmon.exe can somehow retrieve this
value, I'm assuming I can too in my code. Any ideas how this can be done
without using the PERF_PRECISION_100NS_TIMER counter type?

"Li-Yan Zhang [MS]" <ly...@online.microsoft.com> wrote in message
news:QR#mcVviCHA.2604@cpmsftngxa06...

Willy Denoyette [MVP]

unread,
Nov 13, 2002, 12:52:03 PM11/13/02
to
Use the Management namespace classes (WMI wrappers), here's a sample console application ...
Compile and run from the command line using arg value 1 for "PercentDiskTime"


using System;
using System.Diagnostics;
using System.Threading;
using System.Timers;
using System.Management;

class Diskperf
{
ManagementObject pdisk;
decimal drpct;
decimal drpctm;
ulong prevCountBase = 0;
ulong newCountBase = 0;
ulong prevCountTime = 0;
ulong newCountTime = 0;
string counter = null;
internal decimal DiskReadPct {
get
{
return drpct;
}
}
public int Counter{
set
{
switch (value) {
case 1:
counter = "PercentDiskTime";
break;
case 2:
counter = "PercentDiskReadTime";
break;
case 3:
counter = "PercentDiskWriteTime";
break;
}
}
}
internal decimal DiskReadPctMax{
get
{
return drpctm;
}
}

public Diskperf(string instance)
{
// Using Raw Physical disk performance counters
pdisk = new ManagementObject("Win32_PerfRawData_PerfDisk_PhysicalDisk.Name='" + instance + "'");
Console.Write("Current Highest\n----------------- ");
}
public void OnTimer(Object source, ElapsedEventArgs e)
{
pdisk.Get();
newCountBase = (ulong)pdisk.Properties[counter + "_Base"].Value;
newCountTime = (ulong)pdisk.Properties[counter].Value;
decimal pdbde = Convert.ToDecimal(prevCountBase);
decimal pdtde = Convert.ToDecimal(prevCountTime);
decimal pdbd = Convert.ToDecimal(newCountBase);
decimal pdtd = Convert.ToDecimal(newCountTime);
drpct = (pdtde - pdtd) *100m / ( pdbde - pdbd) ;
// save highest value
if (drpct > drpctm)
drpctm = drpct;
Console.WriteLine("{0:f2} \t {1:f2}",DiskReadPct, DiskReadPctMax);
prevCountBase = newCountBase;
prevCountTime = newCountTime;
System.Timers.Timer theTimer = (System.Timers.Timer)source;
theTimer.Enabled = true;
}
public void Close()
{
pdisk.Dispose();
}
}

public class PCReadWMI {
static readonly int disktime = 1;
public static void Main(string[] args){
string instance = "_total";
string appName = Environment.GetCommandLineArgs()[0];
int _counter = 0;
if(args.Length > 1)
instance = args[1];
// instance = _total (default) or any valid value like "0 C:"
if (args.Length > 0) {
if(Convert.ToInt32(args[0]) > 3) {
Console.WriteLine("Usage: {0} <counter> (values : 1 = % Disk time; 2 = % Read Time; 3 = % WriteTime) [<instance>]", appName);
return;
}
_counter = Convert.ToInt32(args[0]);
}
else
_counter = disktime;

Diskperf dp = new Diskperf(instance);
dp.Counter = _counter;
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(dp.OnTimer);
aTimer.Interval = 1000;
aTimer.Enabled = true;
aTimer.AutoReset = false;

Console.WriteLine("Press \'q\' to quit the sample");
while ( Console.Read()!='q' ) {
Thread.Sleep(1000);
}
dp.Close();
}
}

Willy.

"BMeyer" <bme...@krollontrack.com> wrote in message news:OkC#wnziCHA.1884@tkmsftngp10...

0 new messages