Files
swift-nio/Tests/NIOCoreTests/LinuxTest.swift
T
e20271f4a3 Make System.coreCount aware of cgroup2 (#2394)
Motivation:

The count already appears to be aware of cgroup1
but doesn't have logic to support cgroup2.

Modifications:

Add logic to parse /sys/fs/cgroup/cpu.max
For details see - https://stackoverflow.com/questions/65551215/get-docker-cpu-memory-limit-inside-container/65554131#65554131

Result:

Core count will adapt if constrained by a cgroup on more modern kernels.

---------

Co-authored-by: Max Desiatov <m_desiatov@apple.com>
Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
2023-07-31 05:50:24 -07:00

75 lines
2.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
@testable import NIOCore
class LinuxTest: XCTestCase {
func testCoreCountQuota() throws {
#if os(Linux) || os(Android)
try [
("50000", "100000", 1),
("100000", "100000", 1),
("100000\n", "100000", 1),
("100000", "100000\n", 1),
("150000", "100000", 2),
("200000", "100000", 2),
("-1", "100000", nil),
("100000", "-1", nil),
("", "100000", nil),
("100000", "", nil),
("100000", "0", nil)
].forEach { quota, period, count in
try withTemporaryFile(content: quota) { (_, quotaPath) -> Void in
try withTemporaryFile(content: period) { (_, periodPath) -> Void in
XCTAssertEqual(Linux.coreCountCgroup1Restriction(quota: quotaPath, period: periodPath), count)
}
}
}
#endif
}
func testCoreCountCpuset() throws {
#if os(Linux) || os(Android)
try [
("0", 1),
("0,3", 2),
("0-3", 4),
("0-3,7", 5),
("0-3,7\n", 5),
("0,2-4,6,7,9-11", 9),
("", nil)
].forEach { cpuset, count in
try withTemporaryFile(content: cpuset) { (_, path) -> Void in
XCTAssertEqual(Linux.coreCount(cpuset: path), count)
}
}
#endif
}
func testCoreCountCgoup2() throws {
#if os(Linux) || os(Android)
try [
("max 100000", nil),
("75000 100000", 1),
("200000 100000", 2)
].forEach { (content, count) in
try withTemporaryFile(content: content) { (_, path) in
XCTAssertEqual(Linux.coreCountCgroup2Restriction(cpuMaxPath: path), count)
}
}
#endif
}
}