Last active
June 24, 2022 00:44
-
-
Save jaredrummler/e122a985a0faf054def871778c564fc8 to your computer and use it in GitHub Desktop.
Parse /proc/cpuinfo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* SPDX-FileCopyrightText: © 2022 Jared Rummler <jared@jaredrummler.com> | |
* SPDX-License-Identifier: MIT | |
*/ | |
import androidx.annotation.WorkerThread | |
import java.io.File | |
/** | |
* CpuInfo from /proc/cpuinfo. | |
* | |
* @property name The processor architecture. | |
* @property processors The list of processors | |
* @property hardware The SOC manufacturer | |
* | |
* @author Jared Rummler | |
* @see parse | |
*/ | |
class CpuInfo private constructor( | |
val name: String, | |
val processors: List<Processor>, | |
val hardware: String | |
) { | |
/** | |
* | |
* | |
* @property core The core id | |
* @property bogoMIPS A measurement that indicates how fast the computer processor runs | |
* @property features The features of the CPU | |
* @property implementor Indicates the implementer code. | |
* @property architecture Indicates the architecture code. | |
* @property variant Indicates the variant number of the core. | |
* @property part Indicates the primary part number. | |
* @property revision Indicates the minor revision number of the core. | |
*/ | |
data class Processor( | |
val core: Int, | |
val bogoMIPS: Double, | |
val features: List<String>, | |
val implementor: Int, | |
val architecture: Int, | |
val variant: Int, | |
val part: Int, | |
val revision: Int | |
) | |
/** | |
* The exception thrown when parsing /proc/cpuinfo fails. | |
* | |
* @param cause The exception thrown while parsing. | |
*/ | |
class ParseException(cause: Throwable?) : Exception("Error parsing $PATH", cause) | |
companion object { | |
/** | |
* Parse /proc/cpuinfo into a new [CpuInfo] object. | |
* | |
* @return The CPU Info. | |
*/ | |
@WorkerThread | |
@Throws(ParseException::class) | |
fun parse(): CpuInfo { | |
try { | |
val cpuinfo = File(PATH).readText() | |
val pattern = REGEX.toPattern() | |
val matcher = pattern.matcher(cpuinfo) | |
val processors = mutableListOf<Processor>() | |
var name = "" | |
var processor = 0 | |
var bogoMIPS = 0.0 | |
var features = emptyList<String>() | |
var implementor = 0 | |
var architecture = 0 | |
var variant = 0 | |
var part = 0 | |
var revision = 0 | |
var hardware = "" | |
fun addProcessor() = try { | |
processors.add( | |
Processor( | |
processor, bogoMIPS, features, implementor, | |
architecture, variant, part, revision | |
) | |
) | |
} finally { | |
// reset all the values | |
processor = 0; bogoMIPS = 0.0; features = emptyList() | |
implementor = 0; architecture = 0; variant = 0 | |
part = 0; revision = 0 | |
} | |
while (matcher.find()) { | |
val key = requireNotNull(matcher.group(1)) | |
val value = requireNotNull(matcher.group(2)) | |
if (key == PROCESSOR_NAME) { | |
name = value | |
} else if (key.equals(PROCESSOR, true)) { | |
val number = value.toInt() | |
if (number > 0) addProcessor() | |
processor = number | |
} else if (key.equals(BOGO_MIPS, true)) { | |
bogoMIPS = value.toDouble() | |
} else if (key.equals(FEATURES, true)) { | |
features = value.split("\\s+".toRegex()) | |
} else if (key.equals(CPU_IMPLEMENTER, true)) { | |
implementor = Integer.decode(value) | |
} else if (key.equals(CPU_ARCHITECTURE, true)) { | |
architecture = value.toInt() | |
} else if (key.equals(CPU_VARIANT, true)) { | |
variant = Integer.decode(value) | |
} else if (key.equals(CPU_PART, true)) { | |
part = Integer.decode(value) | |
} else if (key.equals(CPU_REVISION, true)) { | |
revision = value.toInt() | |
} else if (key.equals(HARDWARE, true)) { | |
hardware = value | |
} | |
} | |
addProcessor() | |
return CpuInfo(name, processors.toList(), hardware) | |
} catch (e: Exception) { | |
throw ParseException(e) | |
} | |
} | |
private const val PATH = "/proc/cpuinfo" | |
private const val REGEX = "(.*)\\b\\s?:\\s\\b(.*)" | |
private const val PROCESSOR_NAME = "Processor" | |
private const val PROCESSOR = "processor" | |
private const val BOGO_MIPS = "BogoMIPS" | |
private const val FEATURES = "Features" | |
private const val CPU_IMPLEMENTER = "CPU implementer" | |
private const val CPU_ARCHITECTURE = "CPU architecture" | |
private const val CPU_VARIANT = "CPU variant" | |
private const val CPU_PART = "CPU part" | |
private const val CPU_REVISION = "CPU revision" | |
private const val HARDWARE = "Hardware" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2022 Jared Rummler <jared@jaredrummler.com> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment