初始化上传

This commit is contained in:
2025-08-26 08:37:44 +08:00
commit 31d81b91b6
448 changed files with 80981 additions and 0 deletions

View 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;
}
}
}

View 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; }
}
}

View 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;
}
}

View 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);
}
}
}

View 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);
}
}
}