3

In my application, I am gathering data regarding the performance of system, where I need to find

  • % Free Space
  • % Disk Time
  • % Disk Read Time
  • % Disk Write Time
  • % Idle Time
  • % Usage
  • % Usage Peak

using below function;

private void CollectnPopulatePerfCounters() { try { foreach (var pc in System.Diagnostics.PerformanceCounterCategory.GetCategories()) { if (pc.CategoryName == "LogicalDisk" || pc.CategoryName == "Paging File" || pc.CategoryName == "ProcessorPerformance") { try { foreach (var insta in pc.GetInstanceNames()) { try { foreach (PerformanceCounter cntr in pc.GetCounters(insta)) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\amit.txt", true)) { sw.WriteLine("--------------------------------------------------------------------"); sw.WriteLine("Category Name : " + pc.CategoryName); sw.WriteLine("Counter Name : " + cntr.CounterName); sw.WriteLine("Explain Text : " + cntr.CounterHelp); sw.WriteLine("Instance Name: " + cntr.InstanceName); sw.WriteLine("Value : " + Convert.ToString(cntr.RawValue)); //TODO: sw.WriteLine("Counter Type : " + cntr.CounterType); sw.WriteLine("--------------------------------------------------------------------"); } } } catch (Exception) { } } } catch (Exception) { } } } } catch (Exception) { } } 

When the code is executed the data is generated. While observing I found that the value against the above mentioned list [i.e. % free space, % disk time etc.] is not in correct form.

On my machine the value for

  • % Disk Read Time = 44553438000 for C Drive
  • % Usage Peak = 48386 for \??\C:\pagefile.sys

actually the value should be in the percent form [i.e within the range of 0 to 100 %]

Is there any way to get the exact value for all these except [% free Space for which I have calculated].

Or

Does anyone know how to calculate for rest of all headers.

0

1 Answer 1

2

Use following

sw.WriteLine("Value : " + Convert.ToString(Math.Round(cntr.NextValue(),2)) + "%"); 

More info at:

Why the cpu performance counter kept reporting 0% cpu usage?

All the best!

Don't forget to vote :-D

Sign up to request clarification or add additional context in comments.

4 Comments

using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\New1.txt", true)) { sw.WriteLine("% Disk Time"); var cpuload = new PerformanceCounter("LogicalDisk", "% Disk Time", "C:"); for (int i = 0; i < 20; i++) { Thread.Sleep(500); sw.WriteLine(cpuload.NextValue() + "%"); } }
If you run above code you will get different values. Why is this like that? Please comment
Answer is in the MSDN documentation. msdn.microsoft.com/en-us/library/…
RawValue - Gets or sets the raw, or uncalculated, value of this counter. NextValue() - Obtains a counter sample and returns the calculated value for it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.