Skip to content

Commit

Permalink
Adding CPU model info
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Jul 11, 2019
1 parent e7bf222 commit 1cec5eb
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 79 deletions.
36 changes: 0 additions & 36 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The output should look somehow like this:
{
"cpu": {
"clock": 2900000000,
"physicalCpu": 4,
"cores": 4,
"logicalCpu": 8
},
"usage": {
Expand Down
9 changes: 6 additions & 3 deletions Sources/SystemManager/Model/SystemInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 28 additions & 8 deletions Sources/SystemManager/SystemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
),
Expand All @@ -99,10 +112,17 @@ public class SystemManager {

public func linuxInfo() -> EventLoopFuture<SystemInfo> {
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
)
}
Expand Down
28 changes: 0 additions & 28 deletions Sources/SystemManager/Tools/Cpuinfo.swift

This file was deleted.

42 changes: 42 additions & 0 deletions Sources/SystemManager/Tools/Lscpu.swift
Original file line number Diff line number Diff line change
@@ -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

}
4 changes: 3 additions & 1 deletion Sources/SystemManager/Tools/Sysctl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down

0 comments on commit 1cec5eb

Please sign in to comment.