Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance Counter by Process ID instead of name?

I am tracking multiple instances of the same application and need to get the memory and cpu use of both processes. However, I cant seem to figure out a way to use the performance counter and know which result is for which process. I have seen that I can append #1 and such to the end of the name to get results for each, but that doesn't tell me which one is for which process.

How can I determine the ProcessId or pass the process ID to the counter to get the result per each process with same name?

PerformanceCounterCPU.CategoryName = "Process";
PerformanceCounterCPU.CounterName = "% Processor Time";
PerformanceCounterCPU.InstanceName = proc.ProcessHandle.ProcessName;

PerformanceCounterMemory.CategoryName = "Process";
PerformanceCounterMemory.CounterName = "Working Set - Private";
PerformanceCounterMemory.InstanceName = proc.ProcessHandle.ProcessName;
like image 362
JeremyK Avatar asked Feb 02 '12 15:02

JeremyK


People also ask

How can I see my performance counter?

You can view performance counters using the Microsoft Windows Reliability and Performance Monitor application. Click Start > Run. In the Open field, enter perfmon , and then click OK. From Monitoring Tools, select Performance Monitor.

What counters We monitor especially for a processor in performance testing?

Processor, Counter: Interrupts/sec and Performance Object; Processor, Counter: % DPC Time. You can use these Performance Monitor counters to determine how much time the processor spends on interrupts and deferred procedure calls (DPCs). Both interrupts and DPCs can be huge sources of load on your servers' CPUs.

How do I enable performance counters in Windows?

In the navigation pane, expand Monitoring Tools, and then choose Performance Monitor. In the console pane toolbar, choose the Add button. In the Add Counters window, in the Select counters from computer drop-down list, choose the computer that is running Business Central Server.

What are Windows performance counters?

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.


2 Answers

This answer to a related question might work:

private static string GetProcessInstanceName(int pid) {   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 == pid)         {            return instance;         }      }   }   throw new Exception("Could not find performance counter " +        "instance name for current process. This is truly strange ..."); } 
like image 173
M.Babcock Avatar answered Sep 19 '22 10:09

M.Babcock


If you don't mind a machine-wide registry change, you can configure Windows to use the form ProcessName_ProcessID for Perf Counter instance names, rather than appending #1, #2, etc:

Create DWORD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfProc\Performance\ProcessNameFormat and set its value to 2.

If you do stick with the #1, #2 etc form, beware that the instance name for a given process can change during the process' lifetime!

like image 26
Seb Wills Avatar answered Sep 21 '22 10:09

Seb Wills