I’ve included the my main class that does the work, you can wrap it around the windows service. Call the start method when the service starts and call the dispose method when the service stops.
The code below binds to the httpd.exe PID and monitor the CPU load, once it hits 99% or greater than I KILL that PID. CollabNet will restart it automatically.
public class Manager : IDisposable
{
readonly ILog _log;
#region Constructor / Deconstructor
/// <summary>
/// Initializes a new instance of the <see cref="Manager" /> class.
/// </summary>
public Manager()
{
_log = LogManager.GetLogger(typeof(Manager));
}
/// <summary>
/// Finalizes an instance of the <see cref="Manager" /> class.
/// </summary>
~Manager()
{
Dispose(false);
}
#endregion
/// <summary>
/// Starts this instance.
/// </summary>
public void Start()
{
try
{
// now monitor these
WorkingThread = new Thread(() =>
{
while (!IsExit)
{
var httpdProcesses = Process.GetProcessesByName("httpd");
foreach (var httpdProcess in httpdProcesses)
{
if (GetCpuPercentById(httpdProcess.Id) >= 99)
{
try
{
_log.InfoFormat("Killing PID {0}", httpdProcess.Id);
httpdProcess.Kill();
_log.InfoFormat("Killed PID {0}", httpdProcess.Id);
}
catch (Exception ex)
{
_log.Warn(ex.Message, ex);
}
}
}
Thread.Sleep(100);
}
});
WorkingThread.Start();
}
catch (Exception ex)
{
_log.WarnFormat("Failed to process because {0}", ex.Message);
}
}
private int GetCpuPercentById(int pid)
{
PerformanceCounter pc = new PerformanceCounter("Process", "% Processor Time",
GetPerformanceCounterProcessName(pid), true);
pc.NextValue();
Thread.Sleep(1000);
int cpuPercent = (int) pc.NextValue() / Environment.ProcessorCount;
return cpuPercent;
}
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;
}
public bool IsExit { get; set; }
private Thread WorkingThread;
#region IDisposable
private bool _disposed;
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// Dispose managed resources.
IsExit = true;
if (WorkingThread != null)
{
WorkingThread.Join();
WorkingThread = null;
}
}
// There are no unmanaged resources to release, but
// if we add them, they need to be released here.
}
_disposed = true;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
From: valentijnscholten_at_gmail.com [mailto:valentijnscholten_at_gmail.com]
Sent: 20 August 2013 09:21
To: subversion_users_at_googlegroups.com
Cc: valentijnscholten_at_gmail.com; Pavel Lyalyakin; users_at_subversion.apache.org; Dinesh Hirani
Subject: Re: Subversion 1.8 httpd.exe taking 100% CPU
On Monday, August 19, 2013 5:41:55 PM UTC+2, Dinesh Hirani wrote:
I did not find a solution however I wrote an monitor application that checks if the httpd.exe process hits 100%, if so I KILL the process and CollabNet then restarts another instance.
Is it something you'd like to share?
I used ProcDump to create a dump when the process went to 100%. Would it be usefull to post it here?
The information in this e-mail is confidential and may be legally privileged. It is intended solely for the addressee(s). Access by any other person to this e-mail is not authorized. If you are not the intended recipient, please delete this e-mail. Any disclosure of this e-mail or of the parties to it and any copying or distribution of it is prohibited (and may be unlawful). The information expressed herein may be changed at any time without notice or obligation to update. We do not represent that this message is virus-free, complete or accurate and it should not be relied upon as such. Electronic communications may be monitored for operational and business purposes to the extent permitted by applicable law. This email does not constitute legal or tax advice, and the information contained in this communication should not be regarded as such.
Decura IM LLP is authorised and regulated by the Financial Conduct Authority. Registered office address: 11-12 St James’s Square, London SW1Y 4LB. Registered in England and Wales: OC375344
Decura LLP is authorised and regulated by the Financial Conduct Authority. Registered office address: 11-12 St James’s Square, London SW1Y 4LB. Registered in England and Wales: OC377231
Received on 2013-08-20 10:30:48 CEST