Thursday, February 24, 2011

IIS7 AppPool Controller

We have multiple Application Pools at my work that we sometimes have to go through and recycle all by hand. I'm a lazy programmer, so I made an app that does it for me. It also displays the PID and CPU utilization for each app pool, and allows you to completely start and stop all application pools, or one at a time. Enjoy!




It comes with the standard "Works On My Machine" disclaimer from Scott Hansleman:

Works On My Machine Disclaimer: This is released with exactly zero warranty or support. If it deletes files or kills your family pet, you have been warned. It might work great, and it might not.

Download Source Code Here

Download EXE Here

Tuesday, February 22, 2011

Getting % Processor Time for Process By PID

I had to search and search on how to do this, and now hopefully no one else will. It’s fairly easy to get the % CPU Usage for a process using the System.Diagnostics.PerformanceCounter (as long as you remember to sleep a second so you can get the a correct value and divide by the number of CPUs). That looks like this:



PerformanceCounter pc = new PerformanceCounter("Process", "% Processor Time", processName, true);
pc.NextValue();
Thread.Sleep(1000);
int cpuPercent = (int)ppc.PerformanceCounter.NextValue() / Environment.ProcessorCount;


The PerformanceCounter constructor accepts the processName, but it doesn’t accept a process id, so if you had multiple processes with the same name (w3wp, svchost, etc.) you can’t get the value for a specific process easily. If there are multiple processes with the same name running, they’ll have names like “w3wp#1” and “w3wp#2”, where the 1 and the 2 are completely unrelated to ProcessId. I ended up creating this method to get the performance counter process name for a given process. You could be extra fancy and make it an extension method on the Process Class, but this works for now:



private string GetPerformanceCounterProcessName(int pid)
{
return GetPerformanceCounterProcessName(pid, System.Diagnostics.Process.GetProcessById(pid).ProcessName);
}

private string GetPerformanceCounterProcessName(int pid, string processName)
{
int nameIndex = 1;
string value = processName;
string counterName = processName + "#" + nameIndex;
PerformanceCounter pc = new PerformanceCounter("Process", "ID Process", counterName, true);

while (true)
{
try
{
if (pid == (int)pc.NextValue())
{
value = counterName;
break;
}
else
{
nameIndex++;
counterName = processName +
"#" + nameIndex;
pc =
new PerformanceCounter("Process", "ID Process", counterName, true);
}
}
catch (SystemException ex)
{
if (ex.Message == "Instance '" + counterName + "' does not exist in the specified Category.")
{
break;
}
else
{
throw;
}
}
}

return value;
}


Then calling it would look like this:


PerformanceCounter pc = new PerformanceCounter("Process", "% Processor Time", GetPerformanceCounterProcessName(PID), true);
pc.NextValue();
Thread.Sleep(1000);
int cpuPercent = (int)ppc.PerformanceCounter.NextValue() / Environment.ProcessorCount;