private double?GetMachineCpuMacOS() { double?machineCpu = null; var buffer = new CpuLoadInfo(); // Initialize the CPU load info if (m_lastCpuLoadInfo.SystemTime == 0 && m_lastCpuLoadInfo.UserTime == 0 && m_lastCpuLoadInfo.IdleTime == 0) { GetCpuLoadInfo(ref buffer); m_lastCpuLoadInfo = buffer; } buffer = new CpuLoadInfo(); if (GetCpuLoadInfo(ref buffer) == MACOS_INTEROP_SUCCESS) { double systemTicks = buffer.SystemTime - m_lastCpuLoadInfo.SystemTime; double userTicks = buffer.UserTime - m_lastCpuLoadInfo.UserTime; double idleTicks = buffer.IdleTime - m_lastCpuLoadInfo.IdleTime; double totalTicks = systemTicks + userTicks + idleTicks; machineCpu = 100.0 * ((systemTicks + userTicks) / totalTicks); } m_lastCpuLoadInfo = buffer; return(machineCpu); }
/// <summary>Linux specific implementation of <see cref="Processor.GetCpuLoadInfo"/> </summary> internal static int GetCpuLoadInfo(ref CpuLoadInfo buffer, long bufferSize) { if (!File.Exists(ProcStatPath)) { return(ERROR); } var firstLine = File.ReadAllLines(ProcStatPath).FirstOrDefault(); if (string.IsNullOrEmpty(firstLine)) { return(ERROR); } var splits = firstLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); return(ulong.TryParse(splits[1], out buffer.UserTime) && ulong.TryParse(splits[3], out buffer.SystemTime) && ulong.TryParse(splits[4], out buffer.IdleTime) ? 0 : ERROR); }
/// <summary> /// Linux specific implementation of <see cref="Processor.GetCpuLoadInfo"/> /// </summary> internal static int GetCpuLoadInfo(ref CpuLoadInfo buffer, long bufferSize) { try { var firstLine = File.ReadAllLines($"{ProcPath}{ProcStatPath}").FirstOrDefault(); if (string.IsNullOrEmpty(firstLine)) { return(ERROR); } var splits = firstLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); return(ulong.TryParse(splits[1], out buffer.UserTime) && ulong.TryParse(splits[3], out buffer.SystemTime) && ulong.TryParse(splits[4], out buffer.IdleTime) ? 0 : ERROR); } #pragma warning disable catch (Exception) { return(ERROR); } #pragma warning restore }
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member /// <summary> /// Returns the current CPU load info accross all CPU cores to the caller /// </summary> /// <param name="buffer">A CpuLoadInfo struct to hold the timing inforamtion of the current host CPU</param> public static int GetCpuLoadInfo(ref CpuLoadInfo buffer) => IsMacOS ? Impl_Mac.GetCpuLoadInfo(ref buffer, Marshal.SizeOf(buffer)) : Impl_Linux.GetCpuLoadInfo(ref buffer, Marshal.SizeOf(buffer));
internal static extern int GetCpuLoadInfo(ref CpuLoadInfo buffer, long bufferSize);
/// <summary> /// Returns the current CPU load info accross all CPU cores to the caller /// </summary> /// <param name="buffer">A CpuLoadInfo struct to hold the timing inforamtion of the current host CPU</param> public static int GetCpuLoadInfo(ref CpuLoadInfo buffer) => GetCpuLoadInfo(ref buffer, Marshal.SizeOf(buffer));