internal static int GetProcessMemoryUsageSnapshot(int pid, ref ProcessResourceUsage buffer, long bufferSize, bool includeChildProcesses) { var resourceUsage = GetResourceUsageForProcessTree(pid, includeChildProcesses); buffer.WorkingSetSize = resourceUsage.Where(u => u.HasValue).Aggregate(0UL, (acc, usage) => acc + usage.Value.WorkingSetSize); buffer.PeakWorkingSetSize = resourceUsage.Where(u => u.HasValue).Aggregate(0UL, (acc, usage) => acc + usage.Value.PeakWorkingSetSize); return(0); }
/// <summary> /// Returns the memory counters of a specific process only when running on Windows, /// otherwise counters for a whole processes tree rooted at the given process id. /// </summary> /// <param name="handle">When calling from Windows the SafeProcessHandle is required</param> /// <param name="pid">On non-windows systems a process id has to be provided</param> public static ProcessMemoryCountersSnapshot?GetMemoryCountersSnapshot(IntPtr handle, int pid) { switch (s_currentOS) { case OperatingSystem.Win: { var counters = Windows.Memory.GetMemoryUsageCounters(handle); if (counters != null) { return(ProcessMemoryCountersSnapshot.CreateFromBytes( counters.PeakWorkingSetSize, counters.WorkingSetSize, (counters.WorkingSetSize + counters.PeakWorkingSetSize) / 2, counters.PeakPagefileUsage, counters.PagefileUsage)); } return(null); } default: { var usage = new ProcessResourceUsage(); if (Unix.Process.GetProcessMemoryUsage(pid, ref usage, includeChildProcesses: true) == MACOS_INTEROP_SUCCESS) { return(ProcessMemoryCountersSnapshot.CreateFromBytes( usage.PeakWorkingSetSize, usage.WorkingSetSize, (usage.WorkingSetSize + usage.PeakWorkingSetSize) / 2, peakCommitSize: 0, lastCommitSize: 0)); } return(null); } } }
/// <summary> /// Returns process resource usage information to the caller /// </summary> /// <param name="pid">The process id to check</param> /// <param name="buffer">A ProcessResourceUsage struct to hold the process resource information</param> /// <param name="includeChildProcesses">Whether the result should include the execution times of all the child processes</param> public static int GetProcessResourceUsage(int pid, ref ProcessResourceUsage buffer, bool includeChildProcesses) => IsMacOS ? Impl_Mac.GetProcessResourceUsage(pid, ref buffer, Marshal.SizeOf(buffer), includeChildProcesses) : ERROR;
/// <summary> /// Returns process resource usage information to the caller /// </summary> /// <param name="pid">The process id to check</param> /// <param name="buffer">A ProcessResourceUsage struct to hold the process resource information</param> /// <param name="includeChildProcesses">Whether the result should include the execution times of all the child processes</param> public static int GetProcessResourceUsage(int pid, ref ProcessResourceUsage buffer, bool includeChildProcesses) => GetProcessResourceUsage(pid, ref buffer, Marshal.SizeOf(buffer), includeChildProcesses);
private static extern int GetProcessResourceUsage(int pid, ref ProcessResourceUsage buffer, long bufferSize, bool includeChildProcesses);
/// <summary> /// Populates a process resource usage information buffer with memory usage information only. /// </summary> /// <param name="pid">The process id to check</param> /// <param name="buffer">A ProcessResourceUsage struct to hold the memory usage information</param> /// <param name="includeChildProcesses">Whether the result should include the usage numbers of all the child processes</param> public static int GetProcessMemoryUsage(int pid, ref ProcessResourceUsage buffer, bool includeChildProcesses) => IsMacOS ? Impl_Mac.GetProcessResourceUsageSnapshot(pid, ref buffer, Marshal.SizeOf(buffer), includeChildProcesses) : Impl_Linux.GetProcessMemoryUsageSnapshot(pid, ref buffer, Marshal.SizeOf(buffer), includeChildProcesses);
internal static extern int GetProcessResourceUsageSnapshot(int pid, ref ProcessResourceUsage buffer, long bufferSize, bool includeChildProcesses);