Martin

Martin

  • NA
  • 1
  • 0

Performance Counters

May 19 2007 9:08 AM
Hi,
  I've been experimenting a lot lately with the PerformanceCounter class created
by Microsoft and am having a lot of trouble understanding the results. I've read
all of the MSDN documents on the subject but unfortunately, they don't detail
specific metrics. I've been working mostly with the % Procesor Time metric within 
the Processor Category and Thread Category. Here's my questions.


1. Within the Processor Category, what is the value returned by a query for %
Processor Time? Here is the code I tested:


if(counterinfo._intervalMetric)
{
  List threadSteps = new
List();
  List threadStepVals = new List();
  ArrayList pcToRemove = new ArrayList();
  foreach (PerformanceCounter pc in PerformCounters)
  {
     try
     {
        Console.WriteLine(((int)pc.NextValue()).ToString());
        threadSteps.Add(pc.NextSample());
     }
     catch (Exception)
     {
        pcToRemove.Add(pc);
        threadsToUse.Remove(pc.InstanceName);
     }
   }
   foreach (PerformanceCounter pc in pcToRemove)
   {
     PerformCounters.Remove(pc);
   }
   System.Threading.Thread.Sleep(1000);
   int i = 0;
   foreach (PerformanceCounter pc in PerformCounters)
   {
     try
     {
       Console.WriteLine(((int)pc.NextValue()).ToString());
       threadStepVals.Add(CounterSample.Calculate(threadSteps[i], pc.NextSample()));
       i++;
     }
     catch (Exception)
     {
        threadSteps.RemoveAt(i);
     }
  }



  The code is generic, but in this case, it has been initialized to deal with one
counter by the following line:
PerformCounters.Add(new PerformanceCounter(counterinfo._categoryName,
counterinfo._counterName, "_Total", _config.ServerName));
Where categoryName is Processor, counterName is % ProcessorTime and serverName is
the machine I've been polling for the information.


Now everything works all spiffy, but what confuses me is the result. The results
for the query are always somewhere between 399 and 386. What exactly does this
value mean? Everywhere I look says that % Processor Time should return a value
between 0 and 100 (since well... it's a %), but this value definitely isn't.


Does anyone have any ideas?


2. Querying in bulk. My second question has to do with the same code, except now,
it is dealing with individual threads instead of a single processor. When trying
to track threads, I am looking at the processor time of over 100 threads running
on my server. However, because with each one, it must make an individual call to
nextResult, the entire process requires over 200 queries to a server, making it
take an awful long time (2 minutes per execution of the queries). I'm hoping to
cut the interval between checks to 10 seconds but, when it takes 2 minutes to
query, this is pretty much useless.


Is there a way in which I can run all of these queries to the server at the same
time? In essence, can I get a batch process where it will contact the server once,
obtain all of these values, and then return them together?


Thanks for your help!