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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
AMIT SHELKE
  • 501
  • 3
  • 12
  • 34

1 Answers1

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

Community
  • 1
  • 1
Sam
  • 2,917
  • 1
  • 15
  • 28
  • 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() + "%"); } } – AMIT SHELKE Mar 20 '14 at 07:20
  • If you run above code you will get different values. Why is this like that? Please comment – AMIT SHELKE Mar 20 '14 at 07:22
  • Answer is in the MSDN documentation. http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter(v=vs.110).aspx – Sam Mar 20 '14 at 23:29
  • RawValue - Gets or sets the raw, or uncalculated, value of this counter. NextValue() - Obtains a counter sample and returns the calculated value for it. – Sam Mar 20 '14 at 23:30