Skip to content

Commit

Permalink
Switch P/Invokes to use LibraryImport in .NET 7.0 and above.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel15 committed Jan 22, 2024
1 parent 07689dc commit 4f9c254
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public void CreateMetrics(MetricFactory factory)
/// </summary>
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());
}
Expand Down
14 changes: 12 additions & 2 deletions src/Prometheus.SystemMetrics/Native/LinuxNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@ namespace Prometheus.SystemMetrics.Native
/// <summary>
/// Native API calls for Linux.
/// </summary>
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
}
}
24 changes: 18 additions & 6 deletions src/Prometheus.SystemMetrics/Native/WindowsNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@ namespace Prometheus.SystemMetrics.Native
/// <summary>
/// Native API calls for Windows.
/// </summary>
internal static class WindowsNative
internal static partial class WindowsNative
{
#if NET7_0_OR_GREATER
/// <summary>
/// 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
/// </summary>
[return: MarshalAs(UnmanagedType.Bool)]
[LibraryImport("kernel32.dll", SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
internal static partial bool GlobalMemoryStatusEx(ref MemoryStatusEx lpBuffer);
#else
/// <summary>
/// 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
/// </summary>
[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
}

/// <summary>
/// Contains information about the current state of both physical and virtual memory, including extended memory.
/// See https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MEMORYSTATUSEX
internal struct MemoryStatusEx
{
/// <summary>
/// The size of the structure, in bytes. You must set this member before calling
Expand Down Expand Up @@ -83,11 +95,11 @@ public class MEMORYSTATUSEX
public ulong ullAvailExtendedVirtual;

/// <summary>
/// Creates a new instance of <see cref="MEMORYSTATUSEX"/>.
/// Creates a new instance of <see cref="MemoryStatusEx"/>.
/// </summary>
public MEMORYSTATUSEX()
public MemoryStatusEx()
{
dwLength = (uint)Marshal.SizeOf<MEMORYSTATUSEX>();
dwLength = (uint)Marshal.SizeOf<MemoryStatusEx>();
}
}
}
2 changes: 2 additions & 0 deletions src/Prometheus.SystemMetrics/Prometheus.SystemMetrics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<Nullable>enable</Nullable>
<LangVersion>latestMajor</LangVersion>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
<!-- Required by LibraryImport -->
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>prometheus-net.SystemMetrics</PackageId>
<Product>prometheus-net.SystemMetrics</Product>
<Authors>Daniel Lo Nigro</Authors>
Expand Down

0 comments on commit 4f9c254

Please sign in to comment.