Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the performance counter instance name of a process change even if the process has not exited

Tags:

c#

I am using this class as a base class for a category of tests that launch a process and give it some input and wait for it to become idle before giving it more input.

public abstract class TestProcessLaunchingBase
{
    protected PerformanceCounter PerfCounter { get; set; }

    protected void WaitForProcessIdle()
    {
        while (true)
        {
            float oldValue = PerfCounter.NextValue();

            Thread.Sleep(1000);

            float nextValue = PerfCounter.NextValue();

            if (nextValue == 0)
                break;
        }
    }

    protected void FindSpawnedProcessPerfCounter(int processId)
    {
        PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
        string[] instances = cat.GetInstanceNames();
        foreach (string instance in instances)
        {
            using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true))
            {
                int val = (int)cnt.RawValue;
                if (val == processId)
                {
                    PerfCounter = new PerformanceCounter("Process", "% Processor Time", instance);
                    break;
                }
            }

        }

        Assert.IsNotNull(PerfCounter, "Failed to perf counter");
    }
}

These tests occasionally fail because PerfCounter.NextValue() throws an

System.InvalidOperationException Instance 'foobar#2' does not exist in the specified Category

It seems like the instance name of the performance counter is not persistent.

If there are three foobar processes they might have instance names

  • foobar pid 5331
  • foobar #1 pid 5332
  • foobar #2 pid 5333

It seems like if pid 5332 exits foobar #2 becomes foobar #1.

Questions:

  1. Is this a documented behavior ? Can you not persistent a performance counter ? Do you have to look it up every time ?

  2. Alternatively, is there a performance counter that can give Processor Time for all processes named foobar

like image 539
parapura rajkumar Avatar asked Aug 09 '12 14:08

parapura rajkumar


People also ask

Which of the following are exception performance counters in dotnet framework?

Exception performance counters. The Performance console . NET CLR Exceptions category includes counters that provide information about the exceptions thrown by an application.

What are the different counters in Performance monitor?

Performance counters are bits of code that monitor, count, or measure events in software, which allow us to see patterns from a high-level view. They are registered with the operating system during installation of the software, allowing anyone with the proper permissions to view them.

What is meant by hardware performance counters?

In computers, hardware performance counters (HPC), or hardware counters are a set of special-purpose registers built into modern microprocessors to store the counts of hardware-related activities within computer systems. Advanced users often rely on those counters to conduct low-level performance analysis or tuning.

What are performance counters Windows?

Windows Performance Counters provide a high-level abstraction layer that provides a consistent interface for collecting various kinds of system data such as CPU, memory, and disk usage. System administrators often use performance counters to monitor systems for performance or behavior problems.


Video Answer


1 Answers

I already faced this issue in the past. The ProcessName#InstanceNumber pattern for the instance name was clearly a poor choice from Microsoft, you know why :)

So basically you have two choices:

1) Create a new PerformanceCounter instance each time, using your FindSpawnedProcessPerfCounter method.

2) Follow the steps described in KB281884 to change the pattern from ProcessName#InstanceNumber to ProcessName_ProcessID.

The problem of the first solution is that it requires some CPU time to build a new instance each time.

The problem of the second solution is that the registry modification also impacts all programs that are also using this performance counter. And it requires to modify the registry before launching your app.

Last option you have, is to not use Performance counters at all. If you are only interested in the ProcessorTime information, there are some Kernel32 functions you could call using P/Invoke to retrieve it.

EDIT:

The Process class also provides UserProcessorTime and PrivilegedProcessorTime (kernel processor time) properties. Both return a TimeSpan instance (= amount of time), so to retrieve a percentage of processor time, you'll have to do some computation by yourself (involving the refresh period and the processor times).

like image 153
ken2k Avatar answered Oct 12 '22 01:10

ken2k