/// <summary> /// Gets the process times of a specific process /// </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> /// <param name="creation">The out variable to report process start time</param> /// <param name="exit">The out variable to report process exit time</param> /// <param name="kernel">The out variable to report kernel time of the process</param> /// <param name="user">The out variable to report user time of the process</param> /// <returns></returns> public static bool GetProcessTimes(IntPtr handle, int pid, out long creation, out long exit, out long kernel, out long user) { switch (s_currentOS) { case OperatingSystem.MacOS: { creation = 0; exit = 0; kernel = 0; user = 0; MacOS.Process.ProcessTimesInfo processTimes = new MacOS.Process.ProcessTimesInfo(); if (MacOS.Process.GetProcessTimes(pid, &processTimes) == MACOS_INTEROP_SUCCESS) { var now = DateTime.UtcNow; // The reported units will be negative seconds creation = now.AddSeconds(processTimes.StartTime).ToLocalTime().Ticks; exit = processTimes.ExitTime != 0 ? now.AddSeconds(processTimes.ExitTime).ToLocalTime().Ticks : now.Ticks; kernel = (long)processTimes.SystemTime; user = (long)processTimes.UserTime; return(true); } return(false); } default: return(Process.ExternGetProcessTimes(handle, out creation, out exit, out kernel, out user)); } }
/// <summary> /// Returns total processor time for a given process. The process must be running or else an exception is thrown. /// </summary> public static TimeSpan TotalProcessorTime(System.Diagnostics.Process proc) { switch (s_currentOS) { case OperatingSystem.MacOS: { var buffer = new MacOS.Process.ProcessTimesInfo(); MacOS.Process.GetProcessTimes(proc.Id, ref buffer, includeChildProcesses: false); long ticks = (long)(buffer.SystemTimeNs + buffer.UserTimeNs) / 100; return(new TimeSpan(ticks)); } default: { return(proc.TotalProcessorTime); } } }