3

I'm working on a server that reads the '% CPU' and 'Available memory' from PerformanceCounters. It works good on my development machine. But on actual servers, it is really slow to read from these two PerformanceCounters. at least at the first two read operations.
It can take up to 4-6 minutes performing the code below:

        Stopwatch watch = new Stopwatch();

        Log.Instance.Debug("Going to initialize Diagnostic's PerformanceCounters");

        watch.Start();

        m_CPUPerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
        m_MemoryPerformanceCounter = new PerformanceCounter("Memory", "Available MBytes", true);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        //Ensure an updated value...
        System.Threading.Thread.Sleep(1000);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        watch.Stop();

        Log.Instance.Debug("Finished initializing Diagnosticss PerformanceCounters. Time elapsed: {0}", watch.Elapsed);  

When I run this code on my development machine, it will finish in less than 2 seconds (sometimes even less). But on the actual servers that clients of our product should use, it will take a long time. See below:

Finished initializing Diagnosticss PerformanceCounters. Time elapsed: 00:03:59.6706860

It is important that these read operations (and later 'read' operation) will execute really fast. Even at the beginning.

My development machine is Windows 7, 64 Bit, 8 GB RAM.
Clients servers are Windows server 2008 R2 Enterprise, 64 Bit, 4 GB RAM.

I Googled(and Binged) it, but couldn't find any answer. Why is this happening? And how can I fix it?

m1o2
  • 1,549
  • 2
  • 17
  • 27
  • 1
    I have no idea what the problem is, but this: `//Ensure an updated value... System.Threading.Thread.Sleep(1000);` is definately not ensuring that the value gets updated. – Tarec Feb 24 '14 at 14:14
  • Why are you specifying the counters as access in read only mode? As a test try new PerformanceCounter("Processor", "% Processor Time", "_Total"); - Are you sure your not hiding exceptions here? e.g. UnauthorizedAccessException – Paul Zahra Feb 24 '14 at 14:24
  • @Tarec You are right. But it is enough time for the OS to update these PerformanceCounters if it needed to. – m1o2 Feb 24 '14 at 14:31
  • 3 seconds is rather a lot of time. The watch.Start() call is awkwardly placed, it also measures how long it takes to initialize the operating system support for counters plus the time needed to locate these counters. Something is very screwy about these machines, might have something to do with their registry. Virtualization could play a role. The kind of problem the customer's IT staff needs to tackle, it is just guessing from here. – Hans Passant Feb 24 '14 at 14:57
  • @PaulZahra I'm sure that there aren't any exceptions that are being thrown. I also did the changes you suggested and it didn't help :\ – m1o2 Feb 24 '14 at 15:12
  • @HansPassant I think that it is 3 minutes (well 4 minutes) and not seconds. Yes, the current server isn't a 'real' server. It is running under virtualization. But why should it matter? The TimeWatch is there to calculate how many seconds (or minutes) this method need to execute. – m1o2 Feb 24 '14 at 15:15
  • Oh, right. Delays that long are almost always network timeouts. Certificate revocation checks, that sort of thing. The customer needs to check this, SysInternals' tools are good for this. – Hans Passant Feb 24 '14 at 15:23
  • Does it take this long only the first time you run this code in a session, or every time in the same VM instance? – RBarryYoung Feb 24 '14 at 15:24
  • 1
    @RBarryYoung After this long initialization, every read operation will execute really fast. The slow operation will reproduce every time the service (That I built, not the OS) will restart. Doesn't matter If I restart the VM or not. – m1o2 Feb 24 '14 at 15:48
  • @HansPassant Can you please elaborate? How this could be a network issue if I'm attempting to read local PerformanceCounters? – m1o2 Feb 24 '14 at 15:49
  • 1
    Network issues could be coming from internal attempts to contact the PDC/BDC and authorize your service's access to the performance information. – RBarryYoung Feb 24 '14 at 15:55
  • @RBarryYoung Thanks! I didn't know that. Are there any suggestions about how to check that? – m1o2 Feb 24 '14 at 19:03
  • As @HansPassant said, Process Explorer and Process Browser from MS and SysInternals, are probably the best way. – RBarryYoung Feb 24 '14 at 19:13

1 Answers1

0

The problem is most likely with counters provided by additional software installed on the monitored system.

In this case, you should do a performance counter initialization scan for all performance counters registered in system registry, which can be done with ProcMon.

Jonah
  • 1,013
  • 15
  • 25