Performance Counter by Process ID instead of name? - c#

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;

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 ...");
}

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!

Related

CPU Usage% NotifyIcon Using WMI

I've been trying to create a taskbar tray icon that displays the CPU usage (pulled from wbemtest if possible) when hovered over or clicked on using C#. I used the PercentProcessorTime Name from the ManagementClass Win32_PerfFormattedData_Counters_ProcessorInformation to pull the data. I haven't been able to find what data type the Name is even meant to return. Is there somewhere else I may be able to get the data from?
public void CPUactivitythread()
{
//Create a management object to open wbemtest
ManagementClass CPUdataclass = new ManagementClass("Win32_PerfFormattedData_Counters_ProcessorInformation");
try
{
//While Loop to pull consistent data from the CPU
while (true)
{
//Connect to the CPU Performance Instances in wbemtest
ManagementObjectCollection CPUobjectCollection = CPUdataclass.GetInstances();
foreach (ManagementObject obj in CPUobjectCollection) {
//Check that the "PercentProcessorTime" instance is there
if (obj["Name"].ToString() == "PercentProcessorTime")
{
if (Convert.ToUInt64(obj["PercentProcessorTime"]) > 0)
{
cPUUsageToolStripMenuItem.Text = (obj["PercentProcessorTime"]).ToString();
CPUoutputLabel.Text = (obj["PercentProcessorTime"]).ToString();
}
else
{
}
}
}
Thread.Sleep(1000);
}
}
The objects in the Collection correspond to the Task Manager CPU Information, one for each CPU, one for Total named "_Total". The "PercentProcessorTime" is a property of each performance object. Since you are getting the Formatted data, it has already been calculated ("cooked") according to its performance formula and can be used directly. LINQPad is a really useful tool for exploring objects if you don't like to read documentation :)
Try this:
ManagementClass CPUdataclass = new ManagementClass("Win32_PerfFormattedData_Counters_ProcessorInformation");
try {
//While Loop to pull consistent data from the CPU
while (true) {
//Connect to the CPU Performance Instances in wbemtest
ManagementObjectCollection CPUobjectCollection = CPUdataclass.GetInstances();
foreach (ManagementObject obj in CPUobjectCollection) {
//Check that the "PercentProcessorTime" instance is there
if (obj["Name"].ToString() == "_Total") {
var PPT = Convert.ToUInt64(obj.GetPropertyValue("PercentProcessorTime"));
if (PPT > 0) {
cPUUsageToolStripMenuItem.Text = PPT.ToString();
CPUoutputLabel.Text = PPT.ToString();
}
}
else {
}
}
}

Getting Performance Counters from W3wp in c#

I want to retrieve performance Counters from a W3WP (IIS 10.0) Process. I', able to read Process and Thread Performance counters, but not any other like % Time spent in GC
So I wrote a small console showing me all Categories which may have a associations with w3wp and I only see Process and Thread.
I also ran the console as administrator and as the user with which the app pool is running. But still same result.
What am I missing?
var cats = PerformanceCounterCategory.GetCategories();
foreach (var cat in cats)
{
var names = cat.GetInstanceNames();
if (names.Any(i => i.ToLowerInvariant().Contains("w3wp")))
{
Console.WriteLine(cat.CategoryName);
}
}
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;
}
}
}
}
You have to run the application in the same bitness as your website. If your website is 64 bit then you need to run the application in 64 bit mode.
To do this right click on the console application project, click properties in the Build tab untick the box that says "Prefer 32-bit".
See Reading w3wp .Net Performance Counter Instances programmatically for more info.

Performance Counter Instance Name vs. Process Name

I am connecting to various performance counters in the Process category. I am using the following c# method to determine the instance name to use when acquiring the counters:
private const string _categoryName = "Process";
private const string _processIdCounter = "ID Process";
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string[] instanceNames = processCategory.GetInstanceNames();
foreach (string name in instanceNames)
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
if (process.Id == (int)processIdCounter.RawValue)
{
instanceName = name;
return true;
}
}
}
instanceName = null;
return false;
}
Now, I have noticed that the instance name returned usually matches the value of Process.ProcessName.
How are the instance name and process name related?
I ask because I want to simplify the foreach loop in the routine so that I do not have to acquire the ID Process counter for instances that cannot match the current process. I envision a final method that might look like this:
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string[] instanceNames = processCategory.GetInstanceNames();
foreach (string name in instanceNames)
{
if (name /* more or less matches */ process.ProcessName)
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
// ...
}
}
}
instanceName = null;
return false;
}
Seeing that an answer was not forthcoming, I did some more trial and error testing and observed the following behaviour:
Regular Processes
It appears that, for the first regular process with a given name, the process name matches the instance name. For subsequent processes with the same name, the instance name is modified by appending #1, #2, ...
Alarmingly, it also appears to be possible for the instance name associated with the process to change. This appears to happen when processes earlier in the numeric sequence end. There is a race-condition between determining the instance name and acquiring the relevant counters!
Service Processes
Windows NT Services running under the Service Control Manager appear to behave in the same way that regular processes behave. The instance name also appears to change if you end a service-process earlier in the numeric sequence.
ASP.NET Applications
The same assumptions work for applications hosted under IIS except that the process name is w3wp. Different app. pools definitely get different processes and, by starting and stopping app. pools, I ascertained that the instance name changes in the same way, under the same circumstances as above.
Conclusion
My conclusion is that the instance name always starts with the process name and the method can be modified as follows:
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string[] instanceNames = processCategory.GetInstanceNames();
foreach (string name in instanceNames)
{
if (name.StartsWith(process.ProcessName))
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
if (process.Id == (int)processIdCounter.RawValue)
{
instanceName = name;
return true;
}
}
}
}
instanceName = null;
return false;
}
Additionally, it is vitally important that one acknowledges the presence of the race-condition mentioned above when using the instance name returned.
(In the absence of further input, I will accept this as an answer. Feel free to correct me.)
Also note that if you are monitoring muliple instances of the same process then the instance naming isn't consistent across different categories. So the solution given above only works if you are pulling counters from the same category that the pid was was pulled from. I did find the pid was in some other categores - but not all - and not with consistent naming.
Wouldn't this solution be a little bit faster:
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string processName = Path.GetFileNameWithoutExtension(process.ProcessName);
string[] instanceNames = processCategory.GetInstanceNames()
.Where(inst => inst.StartsWith(processName))
.ToArray();
foreach (string name in instanceNames)
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
if (process.Id == (int)processIdCounter.RawValue)
{
instanceName = name;
return true;
}
}
}
instanceName = null;
return false;
}
(Copied from Rick Strahl's blog and modified a bit).
Nevertheless, you need to take care: If there a multiple processes with the same name and one of them exits, the naming of all of them changes:
One thing to mention related to windows process instance names is that they change dynamically when one of the processes exits.
For example if chrome#8 exits, chrome#9 will become chrome#8 and chrome#10 >will become chrome#9. At this point getting the value of the counter >previously created for chrome#10 will throw an exception. This
is really annoying if you want to to monitor multiple instances of
multiple processes as it gets down to monitoring process exits and
recreating all the counters (really ugly).
One way would be to change the way process instance names are generated >(see http://support.microsoft.com/kb/281884) but this has
the potential of affecting other apps using the perfmon api.

Duplicate process (strange issue)

I am trying to prevent opening help file more than once.
This is the method I am using:
public void openHelp()
{
int count = 0;
string helpPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + #"\MyApp\Help\eHelp.chm";
System.Diagnostics.Process[] helpProcs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process proc in helpProcs)
{
if (proc.MainWindowTitle == "Sample App Help")
{
count++;
}
}
if (count == 0)
{
System.Diagnostics.Process.Start(helpPath);
}
else
{
}
}
The idea is, if you find the process with the same MainWindowTitle, then do not start a new one.
However, this is not reliable. In some cases it still starts the process, even though one is already running. Is there an issue with a logic?
Thank you.
P.S. Of course the MainWindowTitle is "Sample App Help", at least that is what I see while debugging.
Update:
Issue only occurs when user has minimised help file. So I suspect something happens in the system and I need to check something. Any suggestions?
The Remarks section in Process.MainWindowTitle contains the following note:
The main window is the window that currently has the focus; note that
this might not be the primary window for the process. You must use the
Refresh method to refresh the Process object to get the current main
window handle if it has changed.
Could this perhaps be the cause of your problem?
What about keeping the process id of a newly started help viewer and before starting another one, just check if the old one is still alive.
int id = ...
try
{
var proc = Process.GetProcessById(id);
}
catch
{
// no process running with that id
}

Exception: Instance 'Name of instance' does not exist in the specified Category

When I create and use performance counters like this:
private readonly PerformanceCounter _cpuPerformanceCounter;
public ProcessViewModel(Process process)
{
_cpuPerformanceCounter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName, true);
}
public void Update()
{
CPU = (int)_cpuPerformanceCounter.NextValue() / Environment.ProcessorCount; // Exception
}
... I get an exception Instance 'Name of instance' does not exist in the specified Category and don't understand why.
P.S. Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<settings>
<performanceCounters enabled="true"/>
</settings>
</system.net>
</configuration>
... included in App.config.
Adding on to previous posts, I have seen processes being formatted like <ProcessName>_<ProcessId> - depending on the OS you are running your app on (Win XP, Win Vista, Win 7, Win 2003 or 2008 Server). In order to have a reliable way to identify your process name for obtaining other performance counters down the road, a function could look like this:
private string ObtainProcessName()
{
string baseProcessName;
string processName = null;
int processId;
bool notFound = true;
int processOptionsChecked = 0;
int maxNrOfParallelProcesses = 3 + 1;
try
{
baseProcessName = Process.GetCurrentProcess().ProcessName;
}
catch (Exception exception)
{
return null;
}
try
{
processId = Process.GetCurrentProcess().Id;
}
catch (Exception exception)
{
return null;
}
while (notFound)
{
processName = baseProcessName;
if (processOptionsChecked > maxNrOfParallelProcesses)
{
break;
}
if (1 == processOptionsChecked)
{
processName = string.Format("{0}_{1}", baseProcessName, processId);
}
else if (processOptionsChecked > 1)
{
processName = string.Format("{0}#{1}", baseProcessName, processOptionsChecked - 1);
}
try
{
PerformanceCounter counter = new PerformanceCounter("Process", "ID Process", processName);
if (processId == (int)counter.NextValue())
{
notFound = !true;
}
}
catch (Exception)
{
}
processOptionsChecked++;
}
return processName;
}
I think your issue happens when there are more than one process with the same name. What PerfMon does then is append #1, #2, etc to the process name. So that means MyApp.exe executed twice will cause this exception when you try to read the performance monitor for "MyApp". Here's a link to one way of solving this: Read performance counters by pid
Here is my solution for all processes and multiple process instances:
var processes = Process.GetProcesses().GroupBy(g => g.ProcessName);
List<Tuple<string, PerformanceCounter>> pcList = new List<Tuple<string, PerformanceCounter>>();
foreach (var pg in processes)
{
if (pg.First().ProcessName == "Idle")
continue;
if (pg.Count() == 1)
{
var process_cpu = new PerformanceCounter(
"Process",
"% Processor Time",
pg.First().ProcessName
);
process_cpu.NextValue();
pcList.Add(new Tuple<string, PerformanceCounter>(pg.First().ProcessName, process_cpu));
}
else
{
int id = 1;
foreach(var p in pg)
{
var process_cpu = new PerformanceCounter(
"Process",
"% Processor Time",
p.ProcessName + "#" + id
);
process_cpu.NextValue();
pcList.Add(new Tuple<string, PerformanceCounter>(p.ProcessName + "#" + id, process_cpu));
id++;
}
}
}
The origninal format that used a pid suffix (registry ProcessNameFormat = 1) appears to have changed as of .NET 4.5 (msdn link) to "processame_pid_rid". Thus, the accepted answer as currently written may no longer work for that case.
This solution should still work for the newer formats:
https://weblog.west-wind.com/posts/2014/Sep/27/Capturing-Performance-Counter-Data-for-a-Process-by-Process-Id
However all these matching solutions may be prone to a race condition where the instance name changes due to a process exiting (#9 becomes #8) just after the instance name was determined but before the new PerformanceCounter() was allocated.
It would make much more sense for MS to provide a PerformanceCounter constructor that accepts a Pid (and possibly now RuntimeId?) directly since the instance names can change on the fly.
You can check this code
Use > new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
Instead of> new PerformanceCounter("Processor", "% Processor Time", "_Total");

Categories

Resources