diff --git a/src/Prometheus.SystemMetrics/Collectors/WindowsMemoryCollector.cs b/src/Prometheus.SystemMetrics/Collectors/WindowsMemoryCollector.cs index 2ed15eb..310698b 100644 --- a/src/Prometheus.SystemMetrics/Collectors/WindowsMemoryCollector.cs +++ b/src/Prometheus.SystemMetrics/Collectors/WindowsMemoryCollector.cs @@ -105,9 +105,9 @@ public void CreateMetrics(MetricFactory factory) /// public void UpdateMetrics() { - var memStatus = new MEMORYSTATUSEX(); + var memStatus = new MemoryStatusEx(); - if (!WindowsNative.GlobalMemoryStatusEx(memStatus)) + if (!WindowsNative.GlobalMemoryStatusEx(ref memStatus)) { throw new Exception(Marshal.GetLastWin32Error().ToString()); } diff --git a/src/Prometheus.SystemMetrics/Native/LinuxNative.cs b/src/Prometheus.SystemMetrics/Native/LinuxNative.cs index 0591440..6250889 100644 --- a/src/Prometheus.SystemMetrics/Native/LinuxNative.cs +++ b/src/Prometheus.SystemMetrics/Native/LinuxNative.cs @@ -5,16 +5,26 @@ namespace Prometheus.SystemMetrics.Native /// /// Native API calls for Linux. /// - internal static class LinuxNative + internal static partial class LinuxNative { public const int SC_CLK_TCK = 0x2; +#if NET7_0_OR_GREATER + [LibraryImport("libc", SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] + internal static partial int getloadavg(double[] loadavg, int nelem); + + [LibraryImport("libc", SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] + internal static partial long sysconf(int name); +#else [DllImport("libc", SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] internal static extern int getloadavg(double[] loadavg, int nelem); [DllImport("libc", SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] - public static extern long sysconf(int name); + internal static extern long sysconf(int name); +#endif } } diff --git a/src/Prometheus.SystemMetrics/Native/WindowsNative.cs b/src/Prometheus.SystemMetrics/Native/WindowsNative.cs index 9b2a920..c42dbf6 100644 --- a/src/Prometheus.SystemMetrics/Native/WindowsNative.cs +++ b/src/Prometheus.SystemMetrics/Native/WindowsNative.cs @@ -5,15 +5,27 @@ namespace Prometheus.SystemMetrics.Native /// /// Native API calls for Windows. /// - internal static class WindowsNative + internal static partial class WindowsNative { +#if NET7_0_OR_GREATER + /// + /// Retrieves information about the system's current usage of both physical and virtual memory. + /// See https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex + /// + [return: MarshalAs(UnmanagedType.Bool)] + [LibraryImport("kernel32.dll", SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] + internal static partial bool GlobalMemoryStatusEx(ref MemoryStatusEx lpBuffer); +#else /// /// Retrieves information about the system's current usage of both physical and virtual memory. /// See https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex /// [return: MarshalAs(UnmanagedType.Bool)] [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - internal static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer); + [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] + internal static extern bool GlobalMemoryStatusEx(ref MemoryStatusEx lpBuffer); +#endif } /// @@ -21,7 +33,7 @@ internal static class WindowsNative /// See https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public class MEMORYSTATUSEX + internal struct MemoryStatusEx { /// /// The size of the structure, in bytes. You must set this member before calling @@ -83,11 +95,11 @@ public class MEMORYSTATUSEX public ulong ullAvailExtendedVirtual; /// - /// Creates a new instance of . + /// Creates a new instance of . /// - public MEMORYSTATUSEX() + public MemoryStatusEx() { - dwLength = (uint)Marshal.SizeOf(); + dwLength = (uint)Marshal.SizeOf(); } } } diff --git a/src/Prometheus.SystemMetrics/Prometheus.SystemMetrics.csproj b/src/Prometheus.SystemMetrics/Prometheus.SystemMetrics.csproj index 0bb65c8..3bc288d 100644 --- a/src/Prometheus.SystemMetrics/Prometheus.SystemMetrics.csproj +++ b/src/Prometheus.SystemMetrics/Prometheus.SystemMetrics.csproj @@ -5,6 +5,8 @@ enable latestMajor true + + true prometheus-net.SystemMetrics prometheus-net.SystemMetrics Daniel Lo Nigro