diff --git a/Package.resolved b/Package.resolved index 419a372..cdd6272 100644 --- a/Package.resolved +++ b/Package.resolved @@ -19,15 +19,6 @@ "version": "0.12.94" } }, - { - "package": "Cdns_sd", - "repositoryURL": "https://github.com/Bouke/Cdns_sd.git", - "state": { - "branch": null, - "revision": "b6dce342895400d0cd86726ad0fcdcad7abc6137", - "version": "2.0.0" - } - }, { "package": "console-kit", "repositoryURL": "https://github.com/vapor/console-kit.git", @@ -55,15 +46,6 @@ "version": "1.0.3" } }, - { - "package": "NetService", - "repositoryURL": "https://github.com/Bouke/NetService.git", - "state": { - "branch": null, - "revision": "9eb5b6a3e638222d3841574d2ecbca4cf3bd6374", - "version": "0.6.0" - } - }, { "package": "nio-websocket-client", "repositoryURL": "https://github.com/vapor/nio-websocket-client.git", @@ -100,15 +82,6 @@ "version": "0.5.0" } }, - { - "package": "llbuild", - "repositoryURL": "https://github.com/apple/swift-llbuild.git", - "state": { - "branch": null, - "revision": "f1c9ad9a253cdf1aa89a7f5c99c30b4513b06ddb", - "version": "0.1.1" - } - }, { "package": "swift-log", "repositoryURL": "https://github.com/apple/swift-log.git", @@ -163,15 +136,6 @@ "version": "2.1.1" } }, - { - "package": "SwiftPM", - "repositoryURL": "https://github.com/apple/swift-package-manager.git", - "state": { - "branch": null, - "revision": "8656a25cb906c1896339f950ac960ee1b4fe8034", - "version": "0.4.0" - } - }, { "package": "SwiftShell", "repositoryURL": "https://github.com/kareman/SwiftShell.git", diff --git a/Package.swift b/Package.swift index d0aae68..6b4cd0b 100644 --- a/Package.swift +++ b/Package.swift @@ -13,8 +13,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-alpha.1.5"), - .package(url: "https://github.com/Einstore/ShellKit.git", from: "1.0.0"), - .package(url: "https://github.com/Bouke/NetService.git", from: "0.6.0") + .package(url: "https://github.com/Einstore/ShellKit.git", from: "1.0.0") ], targets: [ .target( diff --git a/README.md b/README.md index 439a9cd..34a69e5 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ The output should look somehow like this: { "cpu": { "clock": 2900000000, - "physicalCpu": 4, + "cores": 4, "logicalCpu": 8 }, "usage": { diff --git a/Sources/SystemManager/Model/SystemInfo.swift b/Sources/SystemManager/Model/SystemInfo.swift index 031e3c7..0437719 100644 --- a/Sources/SystemManager/Model/SystemInfo.swift +++ b/Sources/SystemManager/Model/SystemInfo.swift @@ -11,9 +11,12 @@ import Foundation public struct SystemInfo: Codable { public struct CPU: Codable { - let physicalCpu: Int - let logicalCpu: Int? - let clock: Double + public let model: String + public let vendor: String + public let hypervisor: String? + public let cores: Int + public let logicalCpu: Int? + public let clock: Double } /// Number of cores diff --git a/Sources/SystemManager/SystemManager.swift b/Sources/SystemManager/SystemManager.swift index 9429e4e..22263ed 100644 --- a/Sources/SystemManager/SystemManager.swift +++ b/Sources/SystemManager/SystemManager.swift @@ -36,15 +36,25 @@ public class SystemManager { return self.shell.run(bash: Sysctl.command).map { output in let info = Sysctl.parse(output) return SystemInfo.CPU( - physicalCpu: info.int(for: .hw_physicalcpu), + model: info.string(for: .machdep_cpu_brand_string), + vendor: info.string(for: .machdep_cpu_vendor), + hypervisor: nil, + cores: info.int(for: .hw_physicalcpu), logicalCpu: info.int(for: .hw_logicalcpu), clock: info.double(for: .hw_cpufrequency) ) } case .linux: - return self.shell.run(bash: Cpuinfo.command).map { output in - let cpu = Cpuinfo.parse(output) - return cpu + return self.shell.run(bash: Lscpu.command).map { output in + let cpu = Lscpu.parse(output) + return SystemInfo.CPU( + model: cpu.model, + vendor: cpu.vendor, + hypervisor: cpu.hypervisor, + cores: cpu.cores, + logicalCpu: nil, + clock: cpu.clock + ) } default: return eventLoop.makeFailedFuture(Error.unableToProvideInformation) @@ -87,7 +97,10 @@ public class SystemManager { let info = Sysctl.parse(output) return SystemInfo( cpu: SystemInfo.CPU( - physicalCpu: info.int(for: .hw_physicalcpu), + model: info.string(for: .machdep_cpu_brand_string), + vendor: info.string(for: .machdep_cpu_vendor), + hypervisor: nil, + cores: info.int(for: .hw_physicalcpu), logicalCpu: info.int(for: .hw_logicalcpu), clock: info.double(for: .hw_cpufrequency) ), @@ -99,10 +112,17 @@ public class SystemManager { public func linuxInfo() -> EventLoopFuture { return stats(os: .linux).flatMap { stats in - return self.shell.run(bash: Cpuinfo.command).map { output in - let cpu = Cpuinfo.parse(output) + return self.shell.run(bash: Lscpu.command).map { output in + let cpu = Lscpu.parse(output) return SystemInfo( - cpu: cpu, + cpu: SystemInfo.CPU( + model: cpu.model, + vendor: cpu.vendor, + hypervisor: cpu.hypervisor, + cores: cpu.cores, + logicalCpu: nil, + clock: cpu.clock + ), usage: stats ) } diff --git a/Sources/SystemManager/Tools/Cpuinfo.swift b/Sources/SystemManager/Tools/Cpuinfo.swift deleted file mode 100644 index 56053c5..0000000 --- a/Sources/SystemManager/Tools/Cpuinfo.swift +++ /dev/null @@ -1,28 +0,0 @@ -// -// Cpuinfo.swift -// -// -// Created by Ondrej Rafaj on 10/07/2019. -// - -import Foundation - - -public struct Cpuinfo: Command { - - public static var command: String { - return "lscpu" - } - - public static func parse(_ string: String) -> SystemInfo.CPU { - let cores = string.line(with: "CPU(s):")?.intOnly() ?? 0 - let clock = string.line(with: "CPU MHz:")?.doubleOnly() ?? 0.0 - - return SystemInfo.CPU( - physicalCpu: cores, - logicalCpu: nil, - clock: (clock * 1_000_000) - ) - } - -} diff --git a/Sources/SystemManager/Tools/Lscpu.swift b/Sources/SystemManager/Tools/Lscpu.swift new file mode 100644 index 0000000..ef470ed --- /dev/null +++ b/Sources/SystemManager/Tools/Lscpu.swift @@ -0,0 +1,42 @@ +// +// Lscpu.swift +// +// +// Created by Ondrej Rafaj on 10/07/2019. +// + +import Foundation + + +public struct Lscpu: Command { + + public static var command: String { + return "lscpu" + } + + public static func parse(_ string: String) -> Lscpu { + let vendor = string.line(with: "Vendor ID:")?.dropTillFirstColon() ?? "n/a" + let model = string.line(with: "Model name:")?.dropTillFirstColon() ?? "n/a" + let hypervisor = string.line(with: "Hypervisor vendor:")?.dropTillFirstColon() ?? "n/a" + let sockets = string.line(with: "Socket(s):")?.intOnly() ?? 0 + let cores = string.line(with: "CPU(s):")?.intOnly() ?? 0 + let clock = string.line(with: "CPU MHz:")?.doubleOnly() ?? 0.0 + + return Lscpu( + vendor: vendor, + model: model, + hypervisor: hypervisor, + sockets: sockets, + cores: cores, + clock: (clock * 1_000_000) + ) + } + + public let vendor: String + public let model: String + public let hypervisor: String? + public let sockets: Int? + public let cores: Int + public let clock: Double + +} diff --git a/Sources/SystemManager/Tools/Sysctl.swift b/Sources/SystemManager/Tools/Sysctl.swift index 2b0a879..83a6919 100644 --- a/Sources/SystemManager/Tools/Sysctl.swift +++ b/Sources/SystemManager/Tools/Sysctl.swift @@ -73,10 +73,12 @@ public class Sysctl: Command { case hw_optional_avx512vbmi = "hw.optional.avx512vbmi" // 0 case hw_targettype = "hw.targettype" // Mac case hw_cputhreadtype = "hw.cputhreadtype" // 1 + case machdep_cpu_brand_string = "machdep.cpu.brand_string" // Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz + case machdep_cpu_vendor = "machdep.cpu.vendor" // GenuineIntel } public static var command: String { - return "sysctl hw" + return "sysctl -a" } public static func parse(_ string: String) -> [Value: String] {