初始化上传
This commit is contained in:
39
常用工具集/Utility/CZGL.SystemInfo/CPU/CPUHelper.cs
Normal file
39
常用工具集/Utility/CZGL.SystemInfo/CPU/CPUHelper.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class CPUHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前系统消耗的 CPU 时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static CPUTime GetCPUTime()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return WindowsCPU.GetCPUTime();
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return LinuxCPU.GetCPUTime();
|
||||
return new CPUTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算 CPU 使用率
|
||||
/// </summary>
|
||||
/// <param name="oldTime"></param>
|
||||
/// <param name="newTime"></param>
|
||||
/// <returns></returns>
|
||||
public static double CalculateCPULoad(CPUTime oldTime, CPUTime newTime)
|
||||
{
|
||||
ulong totalTicksSinceLastTime = newTime.SystemTime - oldTime.SystemTime;
|
||||
ulong idleTicksSinceLastTime = newTime.IdleTime - oldTime.IdleTime;
|
||||
|
||||
double ret = 1.0f - ((totalTicksSinceLastTime > 0) ? ((double)idleTicksSinceLastTime) / totalTicksSinceLastTime : 0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
29
常用工具集/Utility/CZGL.SystemInfo/CPU/CPUTime.cs
Normal file
29
常用工具集/Utility/CZGL.SystemInfo/CPU/CPUTime.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct CPUTime
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="idleTime"></param>
|
||||
/// <param name="systemTime"></param>
|
||||
public CPUTime(ulong idleTime, ulong systemTime)
|
||||
{
|
||||
IdleTime = idleTime;
|
||||
SystemTime = systemTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// CPU 空闲时间
|
||||
/// </summary>
|
||||
public ulong IdleTime { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// CPU 工作时间
|
||||
/// </summary>
|
||||
public ulong SystemTime { get; private set; }
|
||||
}
|
||||
}
|
||||
21
常用工具集/Utility/CZGL.SystemInfo/CPU/FILETIME.cs
Normal file
21
常用工具集/Utility/CZGL.SystemInfo/CPU/FILETIME.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct FILETIME
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间的低位部分
|
||||
/// </summary>
|
||||
public uint DateTimeLow;
|
||||
|
||||
/// <summary>
|
||||
/// 时间的高位部分
|
||||
/// </summary>
|
||||
public uint DateTimeHigh;
|
||||
}
|
||||
}
|
||||
55
常用工具集/Utility/CZGL.SystemInfo/CPU/LinuxCPU.cs
Normal file
55
常用工具集/Utility/CZGL.SystemInfo/CPU/LinuxCPU.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Linux
|
||||
/// </summary>
|
||||
public static class LinuxCPU
|
||||
{
|
||||
const string Path = "/proc/stat";
|
||||
|
||||
/// <summary>
|
||||
/// 获取 CPU 时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static CPUTime GetCPUTime()
|
||||
{
|
||||
ulong IdleTime = 0;
|
||||
ulong SystemTime = 0;
|
||||
try
|
||||
{
|
||||
var text = File.ReadAllLines(Path);
|
||||
|
||||
foreach (var item in text)
|
||||
{
|
||||
if (!item.StartsWith("cpu")) continue;
|
||||
#if NET6_0_OR_GREATER
|
||||
var values = item.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
|
||||
SystemTime += (ulong)(values[1..].Select(x => decimal.Parse(x)).Sum());
|
||||
#else
|
||||
var values = item.Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries).ToArray();
|
||||
SystemTime += (ulong)(values.ToList().GetRange(1, values.Length).Select(x => decimal.Parse(x)).Sum());
|
||||
#endif
|
||||
|
||||
IdleTime += ulong.Parse(values[4]);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex.ToString());
|
||||
Debug.Assert(false, ex.Message);
|
||||
throw new PlatformNotSupportedException($"{RuntimeInformation.OSArchitecture.ToString()} {Environment.OSVersion.Platform.ToString()} {Environment.OSVersion.ToString()}");
|
||||
}
|
||||
|
||||
return new CPUTime(IdleTime, SystemTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
71
常用工具集/Utility/CZGL.SystemInfo/CPU/WindowsCPU.cs
Normal file
71
常用工具集/Utility/CZGL.SystemInfo/CPU/WindowsCPU.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Windows
|
||||
/// </summary>
|
||||
public partial class WindowsCPU
|
||||
{
|
||||
/*
|
||||
IdleTime 空闲时间
|
||||
KernelTime 内核时间
|
||||
UserTime 用户时间
|
||||
|
||||
系统时间 = 内核时间 + 用户时间
|
||||
SystemTime = KernelTime + UserTime
|
||||
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// 在多处理器系统上,返回的值是所有处理器指定时间的总和
|
||||
/// </summary>
|
||||
/// <remarks><see href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getsystemtimes"/></remarks>
|
||||
/// <param name="lpIdleTime">指向 FILETIME 结构的指针,该结构接收系统空闲的时间量</param>
|
||||
/// <param name="lpKernelTime">指向 FILETIME 结构的指针,该结构接收系统在内核模式下执行的时间量(包括所有进程中的所有线程以及所有处理器上的所有线程)。此时间值还包括系统空闲的时间</param>
|
||||
/// <param name="lpUserTime">指向 FILETIME 结构的指针,该结构接收系统在 User 模式下执行的时间量(包括所有进程中的所有线程以及所有处理器上的所有线程)</param>
|
||||
/// <returns></returns>
|
||||
#if NET7_0_OR_GREATER
|
||||
[LibraryImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool GetSystemTimes(out FILETIME lpIdleTime, out FILETIME lpKernelTime, out FILETIME lpUserTime);
|
||||
#else
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern bool GetSystemTimes(out FILETIME lpIdleTime, out FILETIME lpKernelTime, out FILETIME lpUserTime);
|
||||
#endif
|
||||
/// <summary>
|
||||
/// 获取 CPU 工作时间
|
||||
/// </summary>
|
||||
/// <param name="lpIdleTime"></param>
|
||||
/// <param name="lpKernelTime"></param>
|
||||
/// <param name="lpUserTime"></param>
|
||||
/// <returns></returns>
|
||||
public static CPUTime GetCPUTime(FILETIME lpIdleTime, FILETIME lpKernelTime, FILETIME lpUserTime)
|
||||
{
|
||||
var IdleTime = ((ulong)lpIdleTime.DateTimeHigh << 32) | lpIdleTime.DateTimeLow;
|
||||
var KernelTime = ((ulong)lpKernelTime.DateTimeHigh << 32) | lpKernelTime.DateTimeLow;
|
||||
var UserTime = ((ulong)lpUserTime.DateTimeHigh << 32) | lpUserTime.DateTimeLow;
|
||||
|
||||
var SystemTime = KernelTime + UserTime;
|
||||
|
||||
return new CPUTime(IdleTime, SystemTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 CPU 工作时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static CPUTime GetCPUTime()
|
||||
{
|
||||
FILETIME lpIdleTime = default;
|
||||
FILETIME lpKernelTime = default;
|
||||
FILETIME lpUserTime = default;
|
||||
if (!GetSystemTimes(out lpIdleTime, out lpKernelTime, out lpUserTime))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return GetCPUTime(lpIdleTime, lpKernelTime, lpUserTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user