public static ResourceSnapshot FromProcess(Process process) { ResourceSnapshot newSnapshot = new ResourceSnapshot() { PeakPagedMemorySize = process.PeakPagedMemorySize64, PeakWorkingSet = process.PeakWorkingSet64, PrivateMemorySize = process.PrivateMemorySize64, ThreadCount = process.Threads.Count, HandleCount = process.HandleCount, IsNotResponding = !process.Responding }; return(newSnapshot); }
public ResourceSnapshot LogResourceSnapshot(Process proc) { proc.Refresh(); ResourceSnapshot snapshot = ResourceSnapshot.FromProcess(proc); Log.Logger.Debug("Process Snapshot: {0}", snapshot); lock (_resourceSnapshots) { _resourceSnapshots.Enqueue(snapshot); while (_resourceSnapshots.Count > MaxResourceSnapshots) { _resourceSnapshots.Dequeue(); //Just abandon old resource snapshots } } return(snapshot); }
private void MonitorProcess(object state) { try { string executionString = ExecutionString; Log.Logger.Information("Launching {0}", executionString); while (!_disposed) { Process p = new Process(); p.StartInfo = new ProcessStartInfo(executionString); p.Start(); Thread.Sleep(5000); //Wait at least 5 seconds before doing anything else to prevent spiraling out of control //Look for process foreach (Process proc in Process.GetProcesses()) { if (proc.ProcessName.StartsWith(ProcessName)) { Log.Logger.Information("Found Monitor Process: {0}", proc.ProcessName); lock (_monitoringProcessLock) { _monitoringProcess = proc; } ClearSnapshotHistory(); DateTime unresponsiveTime = DateTime.Now; bool exited = proc.WaitForExit((int)(ResourceSnapshotInterval.TotalMilliseconds)); while (!exited) { //The process is still running. This is good, just take a snapshot of it lock (_monitoringProcessLock) { ResourceSnapshot snapshot = LogResourceSnapshot(_monitoringProcess); try { ResourceEvent?.Invoke(this, snapshot); } catch (Exception e) { Log.Logger.Information("No Event Handler for the snap {e}", e); } if (snapshot.IsNotResponding) { if (DateTime.Now >= unresponsiveTime + UnresponsiveTimeout) { Log.Logger.Warning("Application is not responding. Will kill."); Kill(); } } else { unresponsiveTime = DateTime.Now; } } exited = proc.WaitForExit((int)(ResourceSnapshotInterval.TotalMilliseconds)); } //Once the process exits, then dump some stats and restart it lock (_monitoringProcessLock) { RaiseProcessExited(); _monitoringProcess = null; } } } } } catch (Exception ex) { RaiseProcessEvent(string.Format("Error with program: {0}", ex.Message)); } }