SwiftCPUDetect

2.0.0-beta.1

Swift Library to detect the current CPU Architecture for the current machine/process (inculing detecting if the current process is running using Rosetta) and additional system info.
ITzTravelInTime/SwiftCPUDetect

What's New

2.0 Beta 1

2022-07-10T10:33:16Z

WARNING: This release breaks code compatibility with previous library versions (before 2.0.0)

  • Complete library rework, you will need to change your code to sue this library version.
  • The library is now split into 2 targets for better organisation.
  • Partial linux support (still work in progress).
  • Added a swift-friendly uname reimplementation.
  • Rewritten the sysctlrarelated code and all the detecion code in general.
  • Removed lots of code redundancies and repetitions for better maintainability.

SwiftCPUDetect

Swift Library for all Apple Platforms to detect the current CPU Architecture and if the current process is running under Rosetta.

Features

  • Get if the current process is running using Rosetta (aka as emulated) using the AppExecutionMode enum
  • Get the Cpu architecture used by the current process, the computer or the ones supported by the current executable by using the CpuArchitecture enum
  • Get CPU and system info using the HWInfo class
  • Get information from the sysctl api easily with the Sysctl namespace class (From version 2.0 onwards)
  • Swift-friendly re-implementation of uname, called UnameReimplemented.uname provviding also an API-equivalent for the uname command line program (From version 2.0 onwards)

Usage

Usage should be pretty simple, just take a look at the source code. Here is also a very useful example usage:

Also check out the DEMO playground included inside the project.

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]")")

What apps/programs is this Library intended for?

This library should be used by Swift apps/programs, that needs to know system information like the current cpu architecture, if the current app/program is running using Rosetta or just needs some basic system info.

This code should work across multiple platforms based on the XNU kernel and that provvides the necessary function calls using the Foundation module, but it has been tested to work only on macOS/OS X and iOS.

About the project:

This code was created as part of my TINU project and it has been separated and made into it's own library to make the main project's source less complex and more focused on it's aim.

Also having this as it's own library allows for code to be updated separately and so various versions of the main TINU app will be able to be compiled all with the latest version of this library.

Libraries used:

Credits:

Contacts

Legal info

SwiftCPUDetect a Swift library to collect system and current process info. Copyright (C) 2022 Pietro Caruso

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

Last updated: Fri Oct 25 2024 23:42:25 GMT-0900 (Hawaii-Aleutian Daylight Time)