初始化上传
This commit is contained in:
54
常用工具集/Utility/CZGL.SystemInfo/Memory/LinuxMemory.cs
Normal file
54
常用工具集/Utility/CZGL.SystemInfo/Memory/LinuxMemory.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using CZGL.SystemInfo.Memory;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public partial class LinuxMemory
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static MemoryValue GetMemory()
|
||||
{
|
||||
Sysinfo info = new Sysinfo();
|
||||
if (sysinfo(ref info) != 0)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
var usedPercentage = (((double)info.totalram - info.freeram) / (double)info.totalram) * 100;
|
||||
MemoryValue value = new MemoryValue(info.totalram, info.freeram, (ulong)usedPercentage, info.totalswap, info.freeswap);
|
||||
return value;
|
||||
}
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
|
||||
/// <summary>
|
||||
/// 返回整个系统统计信息,<see href="https://linux.die.net/man/2/sysinfo"/>
|
||||
/// </summary>
|
||||
/// <remarks>int sysinfo(struct sysinfo *info);</remarks>
|
||||
/// <param name="info"></param>
|
||||
/// <returns></returns>
|
||||
[LibraryImport("libc.so.6", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I4)]
|
||||
public static partial System.Int32 sysinfo(ref Sysinfo info);
|
||||
|
||||
#else
|
||||
|
||||
/// <summary>
|
||||
/// 返回整个系统统计信息,<see href="https://linux.die.net/man/2/sysinfo"/>
|
||||
/// </summary>
|
||||
/// <remarks>int sysinfo(struct sysinfo *info);</remarks>
|
||||
/// <param name="info"></param>
|
||||
/// <returns></returns>
|
||||
[DllImport("libc.so.6", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I4)]
|
||||
public static extern System.Int32 sysinfo(ref Sysinfo info);
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
17
常用工具集/Utility/CZGL.SystemInfo/Memory/MEMORYSTATUS.cs
Normal file
17
常用工具集/Utility/CZGL.SystemInfo/Memory/MEMORYSTATUS.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct MEMORYSTATUS
|
||||
{
|
||||
public uint dwLength;
|
||||
public uint dwMemoryLoad;
|
||||
public uint dwTotalPhys;
|
||||
public uint dwAvailPhys;
|
||||
public uint dwTotalPageFile;
|
||||
public uint dwAvailPageFile;
|
||||
public uint dwTotalVirtual;
|
||||
public uint dwAvailVirtual;
|
||||
}
|
||||
}
|
||||
24
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryHelper.cs
Normal file
24
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryHelper.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class MemoryHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前系统的内存信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static MemoryValue GetMemoryValue()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return WindowsMemory.GetMemory();
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return LinuxMemory.GetMemory();
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
68
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryStatusExE.cs
Normal file
68
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryStatusExE.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo.Memory
|
||||
{
|
||||
/// <summary>
|
||||
/// 包含有关物理内存和虚拟内存(包括扩展内存)的当前状态的信息。该 GlobalMemoryStatusEx在这个构造函数存储信息。
|
||||
/// <see ref="https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex"/>
|
||||
/// </summary>
|
||||
public struct MemoryStatusExE
|
||||
{
|
||||
/// <summary>
|
||||
/// 结构的大小,以字节为单位,必须在调用 GlobalMemoryStatusEx 之前设置此成员,可以用 Init 方法提前处理
|
||||
/// </summary>
|
||||
/// <remarks>应当使用本对象提供的 Init ,而不是使用构造函数!</remarks>
|
||||
public uint dwLength;
|
||||
|
||||
/// <summary>
|
||||
/// 一个介于 0 和 100 之间的数字,用于指定正在使用的物理内存的大致百分比(0 表示没有内存使用,100 表示内存已满)。
|
||||
/// </summary>
|
||||
public uint dwMemoryLoad;
|
||||
|
||||
/// <summary>
|
||||
/// 实际物理内存量,以字节为单位
|
||||
/// </summary>
|
||||
public ulong ullTotalPhys;
|
||||
|
||||
/// <summary>
|
||||
/// 当前可用的物理内存量,以字节为单位。这是可以立即重用而无需先将其内容写入磁盘的物理内存量。它是备用列表、空闲列表和零列表的大小之和
|
||||
/// </summary>
|
||||
public ulong ullAvailPhys;
|
||||
|
||||
/// <summary>
|
||||
/// 系统或当前进程的当前已提交内存限制,以字节为单位,以较小者为准。要获得系统范围的承诺内存限制,请调用GetPerformanceInfo
|
||||
/// </summary>
|
||||
public ulong ullTotalPageFile;
|
||||
|
||||
/// <summary>
|
||||
/// 当前进程可以提交的最大内存量,以字节为单位。该值等于或小于系统范围的可用提交值。要计算整个系统的可承诺值,调用GetPerformanceInfo核减价值CommitTotal从价值CommitLimit
|
||||
/// </summary>
|
||||
|
||||
public ulong ullAvailPageFile;
|
||||
|
||||
/// <summary>
|
||||
/// 调用进程的虚拟地址空间的用户模式部分的大小,以字节为单位。该值取决于进程类型、处理器类型和操作系统的配置。例如,对于 x86 处理器上的大多数 32 位进程,此值约为 2 GB,对于在启用4 GB 调整的系统上运行的具有大地址感知能力的 32 位进程约为 3 GB 。
|
||||
/// </summary>
|
||||
|
||||
public ulong ullTotalVirtual;
|
||||
|
||||
/// <summary>
|
||||
/// 当前在调用进程的虚拟地址空间的用户模式部分中未保留和未提交的内存量,以字节为单位
|
||||
/// </summary>
|
||||
public ulong ullAvailVirtual;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 预订的。该值始终为 0
|
||||
/// </summary>
|
||||
public ulong ullAvailExtendedVirtual;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
dwLength = checked((uint)Marshal.SizeOf(typeof(MemoryStatusExE)));
|
||||
}
|
||||
}
|
||||
}
|
||||
66
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryValue.cs
Normal file
66
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryValue.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 内存值表示
|
||||
/// </summary>
|
||||
public struct MemoryValue
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="totalPhysicalMemory">物理内存字节数</param>
|
||||
/// <param name="availablePhysicalMemory">可用的物理内存字节数</param>
|
||||
/// <param name="usedPercentage">已用物理内存百分比</param>
|
||||
/// <param name="totalVirtualMemory">虚拟内存字节数</param>
|
||||
/// <param name="availableVirtualMemory">可用虚拟内存字节数</param>
|
||||
public MemoryValue(
|
||||
ulong totalPhysicalMemory,
|
||||
ulong availablePhysicalMemory,
|
||||
double usedPercentage,
|
||||
ulong totalVirtualMemory,
|
||||
ulong availableVirtualMemory)
|
||||
{
|
||||
TotalPhysicalMemory = totalPhysicalMemory;
|
||||
AvailablePhysicalMemory = availablePhysicalMemory;
|
||||
UsedPercentage = usedPercentage;
|
||||
TotalVirtualMemory = totalVirtualMemory;
|
||||
AvailableVirtualMemory = availableVirtualMemory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 物理内存字节数
|
||||
/// </summary>
|
||||
public ulong TotalPhysicalMemory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 可用的物理内存字节数
|
||||
/// </summary>
|
||||
public ulong AvailablePhysicalMemory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已用物理内存字节数
|
||||
/// </summary>
|
||||
public ulong UsedPhysicalMemory => TotalPhysicalMemory - AvailablePhysicalMemory;
|
||||
|
||||
/// <summary>
|
||||
/// 已用物理内存百分比,0~100,100表示内存已用尽
|
||||
/// </summary>
|
||||
public double UsedPercentage { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 虚拟内存字节数
|
||||
/// </summary>
|
||||
public ulong TotalVirtualMemory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 可用虚拟内存字节数
|
||||
/// </summary>
|
||||
public ulong AvailableVirtualMemory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已用虚拟内存字节数
|
||||
/// </summary>
|
||||
public ulong UsedVirtualMemory => TotalVirtualMemory - AvailableVirtualMemory;
|
||||
}
|
||||
|
||||
}
|
||||
74
常用工具集/Utility/CZGL.SystemInfo/Memory/Sysinfo.cs
Normal file
74
常用工具集/Utility/CZGL.SystemInfo/Memory/Sysinfo.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct Sysinfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Seconds since boot
|
||||
/// </summary>
|
||||
public long uptime;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 1,5,15 分钟内存的平均使用量,数组大小为 3
|
||||
/// </summary>
|
||||
unsafe public fixed ulong loads[3];
|
||||
/// <summary>
|
||||
/// 总物理内存
|
||||
/// </summary>
|
||||
public ulong totalram;
|
||||
|
||||
/// <summary>
|
||||
/// 可用内存
|
||||
/// </summary>
|
||||
public ulong freeram;
|
||||
|
||||
/// <summary>
|
||||
/// 共享内存
|
||||
/// </summary>
|
||||
public ulong sharedram;
|
||||
|
||||
/// <summary>
|
||||
/// Memory used by buffers
|
||||
/// </summary>
|
||||
public ulong bufferram;
|
||||
|
||||
/// <summary>
|
||||
/// Total swap space size
|
||||
/// </summary>
|
||||
public ulong totalswap;
|
||||
|
||||
/// <summary>
|
||||
/// swap space still available
|
||||
/// </summary>
|
||||
public ulong freeswap;
|
||||
|
||||
/// <summary>
|
||||
/// Number of current processes
|
||||
/// </summary>
|
||||
public ushort procs;
|
||||
|
||||
/// <summary>
|
||||
/// Total high memory size
|
||||
/// </summary>
|
||||
public ulong totalhigh;
|
||||
|
||||
/// <summary>
|
||||
/// Available high memory size
|
||||
/// </summary>
|
||||
public ulong freehigh;
|
||||
|
||||
/// <summary>
|
||||
/// Memory unit size in bytes
|
||||
/// </summary>
|
||||
public uint mem_unit;
|
||||
|
||||
/// <summary>
|
||||
/// Padding to 64 bytes
|
||||
/// </summary>
|
||||
unsafe public fixed byte _f[64];
|
||||
}
|
||||
}
|
||||
86
常用工具集/Utility/CZGL.SystemInfo/Memory/WindowsMemory.cs
Normal file
86
常用工具集/Utility/CZGL.SystemInfo/Memory/WindowsMemory.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using CZGL.SystemInfo.Memory;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public partial class WindowsMemory
|
||||
{
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
|
||||
/// <summary>
|
||||
/// 在内存超过 4 GB 的计算机上, GlobalMemoryStatus函数可能返回不正确的信息,报告值 –1 表示溢出。因此,应用程序应改用 GlobalMemoryStatusEx函数。
|
||||
/// </summary>
|
||||
/// <remarks>Windows XP [仅限桌面应用程序];最低支持服务器 Windows Server 2003 [仅限桌面应用程序]</remarks>
|
||||
/// <param name="lpBuffer"></param>
|
||||
[LibraryImport("Kernel32.dll", SetLastError = true)]
|
||||
public static partial void GlobalMemoryStatus(ref MEMORYSTATUS lpBuffer);
|
||||
|
||||
/// <summary>
|
||||
/// 检索有关系统当前使用物理和虚拟内存的信息
|
||||
/// </summary>
|
||||
/// <remarks><see href="https://docs.microsoft.com/zh-cn/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex"/></remarks>
|
||||
/// <param name="lpBuffer"></param>
|
||||
/// <returns></returns>
|
||||
[LibraryImport("Kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial Boolean GlobalMemoryStatusEx(ref MemoryStatusExE lpBuffer);
|
||||
|
||||
#else
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 在内存超过 4 GB 的计算机上, GlobalMemoryStatus函数可能返回不正确的信息,报告值 –1 表示溢出。因此,应用程序应改用 GlobalMemoryStatusEx函数。
|
||||
/// </summary>
|
||||
/// <remarks>Windows XP [仅限桌面应用程序];最低支持服务器 Windows Server 2003 [仅限桌面应用程序]</remarks>
|
||||
/// <param name="lpBuffer"></param>
|
||||
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern void GlobalMemoryStatus(ref MEMORYSTATUS lpBuffer);
|
||||
|
||||
/// <summary>
|
||||
/// 检索有关系统当前使用物理和虚拟内存的信息
|
||||
/// </summary>
|
||||
/// <remarks><see href="https://docs.microsoft.com/zh-cn/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex"/></remarks>
|
||||
/// <param name="lpBuffer"></param>
|
||||
/// <returns></returns>
|
||||
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern Boolean GlobalMemoryStatusEx(ref MemoryStatusExE lpBuffer);
|
||||
#endif
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static MemoryValue GetMemory()
|
||||
{
|
||||
// 检查 Windows 内核版本,是否为旧系统
|
||||
if (Environment.OSVersion.Version.Major < 5)
|
||||
{
|
||||
// https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions");
|
||||
return default;
|
||||
}
|
||||
|
||||
MemoryStatusExE memoryStatusEx = new MemoryStatusExE();
|
||||
// 初始化结构的大小
|
||||
memoryStatusEx.Init();
|
||||
// 刷新值
|
||||
if (!GlobalMemoryStatusEx(ref memoryStatusEx)) return default;
|
||||
|
||||
var TotalPhysicalMemory = memoryStatusEx.ullTotalPhys;
|
||||
var AvailablePhysicalMemory = memoryStatusEx.ullAvailPhys;
|
||||
var TotalVirtualMemory = memoryStatusEx.ullTotalVirtual;
|
||||
var AvailableVirtualMemory = memoryStatusEx.ullAvailVirtual;
|
||||
var UsedPercentage = memoryStatusEx.dwMemoryLoad;
|
||||
return new MemoryValue(
|
||||
TotalPhysicalMemory,
|
||||
AvailablePhysicalMemory,
|
||||
UsedPercentage,
|
||||
TotalVirtualMemory,
|
||||
AvailableVirtualMemory);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user