Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Sources/SystemMetrics/SystemMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion Tests/SystemMetricsTests/SystemMetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down