From 9b38cdda0e7d28d91a9ef1534df38c3310a70d4c Mon Sep 17 00:00:00 2001 From: Timo <38291523+lovetodream@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:12:52 +0200 Subject: [PATCH] add support for compiling against musl --- Sources/SystemMetrics/SystemMetrics.swift | 13 ++++++++++--- Tests/SystemMetricsTests/SystemMetricsTests.swift | 4 +++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Sources/SystemMetrics/SystemMetrics.swift b/Sources/SystemMetrics/SystemMetrics.swift index 7887ca9..d7f4c73 100644 --- a/Sources/SystemMetrics/SystemMetrics.swift +++ b/Sources/SystemMetrics/SystemMetrics.swift @@ -14,10 +14,13 @@ import CoreMetrics import Dispatch -#if os(Linux) +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif + extension MetricsSystem { fileprivate static var systemMetricsProvider: SystemMetricsProvider? @@ -329,7 +332,7 @@ public enum SystemMetrics { static let stimeTicks = 12 } - let ticks = _SC_CLK_TCK + let ticks = Int(_SC_CLK_TCK) let statFile = CFile("/proc/self/stat") statFile.open() @@ -362,7 +365,7 @@ public enum SystemMetrics { let utimeTicks = Int(stats[safe: StatIndices.utimeTicks]), let stimeTicks = Int(stats[safe: StatIndices.stimeTicks]) else { return nil } - let residentMemoryBytes = rss * _SC_PAGESIZE + let residentMemoryBytes = rss * Int(_SC_PAGESIZE) let processStartTimeInSeconds = startTimeTicks / ticks let cpuTicks = utimeTicks + stimeTicks let cpuSeconds = cpuTicks / ticks @@ -384,7 +387,11 @@ public enum SystemMetrics { var _rlim = rlimit() guard withUnsafeMutablePointer(to: &_rlim, { ptr in + #if canImport(Musl) + getrlimit(RLIMIT_NOFILE, ptr) == 0 + #else getrlimit(__rlimit_resource_t(RLIMIT_NOFILE.rawValue), ptr) == 0 + #endif }) else { return nil } let maxFileDescriptors = Int(_rlim.rlim_max) diff --git a/Tests/SystemMetricsTests/SystemMetricsTests.swift b/Tests/SystemMetricsTests/SystemMetricsTests.swift index 62f15f1..d0e1e7c 100644 --- a/Tests/SystemMetricsTests/SystemMetricsTests.swift +++ b/Tests/SystemMetricsTests/SystemMetricsTests.swift @@ -13,8 +13,10 @@ //===----------------------------------------------------------------------===// @testable import SystemMetrics import XCTest -#if os(Linux) +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif class SystemMetricsTest: XCTestCase {