does anyone know how i can detect the cpu architec...
# random
c
does anyone know how i can detect the cpu architecture in kotlin/jvm? i don鈥檛 want the jvm architecture, i want to detect arm even when i run on an intel jvm via rosetta.
e
Copy code
val p = ProcessBuilder("uname", "-p").redirectOutput(PIPE).start()
val arch = p.inputStream().use { it.bufferedReader().readText() }
check(p.waitFor() == 0)
鈽濓笍 1
c
perfect, thanks!
i can just default to intel when it fails
e
on a different note, it is possible to change the output of
uname
on Linux, https://manpages.debian.org/setarch.8. I'm not sure there's any reasonable way to detect that from within the JVM
k
on my machine it's printing out
unknown
馃檪
e
maybe
uname -m
? the meaning of the different fields does vary a bit between UNIXen
f
-m
is usually better and gives a more meaningful result also on BSD (Mac).
c
btw this does not work, it prints i386 on a rosetta jvm
because the shell inherits the architecture
I could read sysctl:
Copy code
~ 位 /bin/zsh -c 'sysctl -n machdep.cpu.brand_string'
Apple M1 Pro

~ 位 arch -x86_64 /bin/zsh -c 'sysctl -n machdep.cpu.brand_string'
Apple M1 Pro
or parse
uname -v
Copy code
arch -x86_64 uname -v
Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000
this works, and prints true on both rosetta and native jvms:
Copy code
fun main() {
    val p = ProcessBuilder("uname", "-v").redirectOutput(PIPE).start()
    val uname = p.inputStream.use { it.bufferedReader().readText() }
    check(p.waitFor() == 0)
    println(uname.contains("ARM64"))
}