private List <KeyValuePair <int, ulong> > ObtainPerProcessUsage() { List <KeyValuePair <int, ulong> > IOBytes; SortedDictionary <int, IO_COUNTERS> Counts = new SortedDictionary <int, IO_COUNTERS>(); HashSet <int> ForegroundPids = Lagfree.GetForegroundPids(); Process[] procs = Process.GetProcesses(); DateTime CountsTime = DateTime.UtcNow; if ((LastCountsTime - CountsTime).TotalMilliseconds >= CheckInterval * 2) { LastCounts.Clear(); } IOBytes = new List <KeyValuePair <int, ulong> >(procs.Length); ulong SafeSub(ulong a, ulong b) => a >= b ? a - b : 0; foreach (var proc in procs) { try { int pid = proc.Id; if (pid == 0 || pid == 4 || pid == Lagfree.MyPid || pid == Lagfree.AgentPid || ForegroundPids.Contains(pid) || Lagfree.IgnoredProcessNames.Contains(proc.ProcessName)) { continue; } using (SafeProcessHandle hProcess = proc.SafeHandle) { IO_COUNTERS curr = GetIOCounters(hProcess); Counts.Add(proc.Id, curr); if (LastCounts.ContainsKey(proc.Id)) { IO_COUNTERS last = LastCounts[proc.Id]; IOBytes.Add(new KeyValuePair <int, ulong>(proc.Id, SafeSub(curr.ReadTransferCount, last.ReadTransferCount) + SafeSub(curr.WriteTransferCount, last.WriteTransferCount) + SafeSub(curr.OtherTransferCount, last.OtherTransferCount) + ( SafeSub(curr.ReadOperationCount, last.ReadOperationCount) + SafeSub(curr.WriteOperationCount, last.WriteOperationCount) + SafeSub(curr.OtherOperationCount, last.OtherOperationCount) ) )); } } } catch (Win32Exception) { } catch (InvalidOperationException) { } catch (Exception ex) { WriteLogEntry(3000, ex.GetType().Name + ":" + ex.Message + "\n" + ex.StackTrace, true); } } IOBytes.Sort(new Comparison <KeyValuePair <int, ulong> >((x, y) => (y.Value > x.Value) ? 1 : ((x.Value > y.Value) ? -1 : 0))); LastCounts = Counts; LastCountsTime = CountsTime; return(IOBytes); }
internal IOCounters(IO_COUNTERS nativeCounters) { ReadCounters.OperationCount = nativeCounters.ReadOperationCount; ReadCounters.TransferCount = nativeCounters.ReadTransferCount; WriteCounters.OperationCount = nativeCounters.WriteOperationCount; WriteCounters.TransferCount = nativeCounters.WriteTransferCount; OtherCounters.OperationCount = nativeCounters.OtherOperationCount; OtherCounters.TransferCount = nativeCounters.OtherTransferCount; }
static extern bool GetProcessIoCounters(IntPtr ProcessHandle, out IO_COUNTERS IoCounters);
public static extern bool GetProcessIoCounters([In] IntPtr hProcess, out IO_COUNTERS lpIoCounters);
/// <summary> /// Creates an instance of <see cref="IOCounters"/> from <see cref="IO_COUNTERS"/>. /// </summary> public IOCounters(IO_COUNTERS nativeCounters) { ReadCounters = new IOTypeCounters(nativeCounters.ReadOperationCount, nativeCounters.ReadTransferCount); WriteCounters = new IOTypeCounters(nativeCounters.WriteOperationCount, nativeCounters.WriteTransferCount); OtherCounters = new IOTypeCounters(nativeCounters.OtherOperationCount, nativeCounters.OtherTransferCount); }
public static extern bool GetProcessIoCounters(SafeProcessHandle hProcess, ref IO_COUNTERS lpIoCounters);
static extern bool GetProcessIoCounters(IntPtr hProcess, out IO_COUNTERS counters);
public static extern bool GetProcessIoCounters(int ProcessHandle, out IO_COUNTERS IoCounters);
public JOBOBJECT_EXTENDED_LIMIT_INFORMATION(JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation = default, IO_COUNTERS IoInfo = default, System.UIntPtr ProcessMemoryLimit = default, System.UIntPtr JobMemoryLimit = default, System.UIntPtr PeakProcessMemoryUsed = default, System.UIntPtr PeakJobMemoryUsed = default) { this.BasicLimitInformation = BasicLimitInformation; this.IoInfo = IoInfo; this.ProcessMemoryLimit = ProcessMemoryLimit; this.JobMemoryLimit = JobMemoryLimit; this.PeakProcessMemoryUsed = PeakProcessMemoryUsed; this.PeakJobMemoryUsed = PeakJobMemoryUsed; }
public static extern bool GetProcessIoCounters([In] HPROCESS hProcess, out IO_COUNTERS lpIoCounters);
private static unsafe extern bool GetProcessIoCounters(System.IntPtr ProcessHandle, out IO_COUNTERS IoCounters);
private unsafe static extern bool GetProcessIoCounters(System.IntPtr ProcessHandle, out IO_COUNTERS IoCounters);