SwiftCPUDetect

Swift 库,适用于所有 Apple 平台,用于检测当前 CPU 架构以及当前进程是否在 Rosetta 下运行。

特性

用法

用法应该非常简单,只需查看源代码。 这里还有一个非常有用的示例用法。

另请查看项目中包含的 DEMO playground。

import SwiftCPUDetect
import SwiftSystemValues

//Disabled debug prints from the library
SwiftCPUDetect.GeneralPrinter.enabled = false

var str = " "

//those prints gets various info about the cpu
#if os(macOS) || targetEnvironment(macCatalyst)

print("This cpu has \"\(HWInfo.CPU.coresPerPackage() ?? 255)\" cores for each package")
print("This cpu has \"\(HWInfo.CPU.threadsPerPackage() ?? 255)\" threads for each package")

#if arch(x86_64) || arch(i386)
for info in HWInfo.CPU.featuresList() ?? []{ //intel only
    str += " \(info),"
}

print("Features for CPU: \(str.dropLast())")

print("This system has \"\(HWInfo.CPU.packagesCount() ?? 255)\" cpu packages")
#endif

//Prints the current execution mode
print("Is my app running with Rosetta? \((AppExecutionMode.current() == .emulated) ? "Yes" : "No")")

#endif

print("The name of the CPU is \"\(HWInfo.CPU.name() ?? "[Not detected]")\"")

print("This cpu has \"\(HWInfo.CPU.coresCount() ?? 255)\" cores")
print("This cpu has \"\(HWInfo.CPU.threadsCount() ?? 255)\" threads")

print("This cpu has \"\(HWInfo.CPU.EfficiencyCores.coresCount() ?? 0)\" E-cores")
print("This cpu has \"\(HWInfo.CPU.PerformanceCores.coresCount() ?? 0)\" P-cores")

print("This cpu is \(HWInfo.CPU.is64Bit() ? "64" : "32" ) bits")

//Prints the ammount of RAM in bytes
print("This computer has \((HWInfo.ramAmmount() ?? 0) / (1024 * 1024 * 1024)) GB of RAM")

//Prints the architecture of the current process
print("My app is running using the \(CpuArchitecture.current()?.rawValue ?? "[Can't detect architecture]") architecture")

//Prints the architecture of the current computer
print("My computer is running using the \(CpuArchitecture.machineCurrent()?.rawValue ?? "[Can't detect architecture]") architecture")

//Prints the architectures supported by the current executable
str = " "

for arch in CpuArchitecture.AppArchitectures.current() ?? []{
    str += " " + arch.rawValue + ","
}

print("My app supports those architectures: " + str.dropLast())

//Testing the uname fetching
print("Device's `uname -a`: \(UnameReimplemented.uname(withCommandLineArgs: [.a]) ?? "[Failed to get the uname string]")")

此库适用于哪些应用程序/程序?

此库应被需要了解系统信息的 Swift 应用程序/程序使用,例如当前 CPU 架构、当前应用程序/程序是否使用 Rosetta 运行,或者只需要一些基本的系统信息。

此代码应适用于基于 XNU 内核的多个平台,并提供使用 Foundation 模块的必要函数调用,但仅在 macOS/OS X 和 iOS 上进行了测试。

关于项目

此代码是作为我的 TINU 项目的一部分创建的,并已分离并制作成其自己的库,以使主项目的源代码不那么复杂,并且更加专注于其目标。

此外,将其作为自己的库可以单独更新代码,因此主 TINU 应用程序的各种版本都将能够使用此库的最新版本进行编译。

使用的库

鸣谢

联系方式

法律信息

SwiftCPUDetect 是一个用于收集系统和当前进程信息的 Swift 库。 版权所有 (C) 2022 Pietro Caruso

此库是免费软件;您可以根据自由软件基金会发布的 GNU Lesser General Public License 的条款重新分发和/或修改它;无论是许可证的 2.1 版本,还是(由您选择)任何更高版本。

发布此库是为了希望能对您有所帮助,但**不提供任何担保**;甚至没有对适销性或特定用途适用性的暗示保证。 有关更多详细信息,请参见 GNU Lesser General Public License。

您应该已收到与此库一起发布的 GNU Lesser General Public License 的副本; 如果没有,请写信给 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA