示例#1
0
        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);
        }
示例#2
0
        /// <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);
        }
示例#3
0
        /// <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
        }
示例#4
0
        #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));
示例#5
0
 internal static extern int GetCpuLoadInfo(ref CpuLoadInfo buffer, long bufferSize);
示例#6
0
        /// <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));