Identifying the system the program is running on: the operating system, the processor, the core count and the memory.
import os.platform
The os.platform package says at run time what system the program is on: the operating system, the processor family, the number of cores and the amount of memory. Use it for reports and logs, for automatic settings such as a worker count or a cache size, and for small per-system differences such as a file extension. For code that only compiles on one system, use the constants such as SALAM_OS_WINDOWS and conditional compilation instead. For the user and their home folder see os.user; for environment variables, env.
The Is... functions, OS and Arch are fixed when the program is compiled, so they cost nothing to call. The hardware questions are asked of the running system, and how they are answered differs:
Function
Linux
macOS and BSD
Windows
CpuCount
sysconf, instant
runs sysctl
the NUMBER_OF_PROCESSORS variable
TotalMemory
/proc/meminfo
runs sysctl
runs PowerShell, often a second or more
AvailableMemory
/proc/meminfo
not known, -1
runs PowerShell
What people get wrong: calling the memory functions in a loop (on Windows each call starts PowerShell, so ask once and keep the number), forgetting that every hardware function returns -1 when it cannot find out, and treating CpuCount as physical cores. It counts the logical processors online, hyper-threads included, and inside a container it counts the whole machine.
The programs below print whether a rule held rather than the raw numbers, so their output is the same on any machine.
Choose how many workers to run
Work that splits into independent pieces, such as resizing images, goes faster with one thread or process per core. CpuCount gives the number of cores; keep the result between one and a limit of your own, because a machine with 128 cores does not mean your job wants 128 threads.
os-platform-guide-workers.salam
import os.platform
funcworker_count(limit: int): int:
cores := platform.CpuCount()
if cores < 1:
ret1endif cores > limit:
ret limit
endret cores
endfuncmain:
workers := worker_count(8)
println"workers between 1 and 8:", workers >= 1and workers <= 8println"never more than the cores:", workers <= platform.CpuCount()
one := worker_count(1)
println"with a limit of one:", one
end
Output
workers between 1 and 8: true
never more than the cores: true
with a limit of one: 1
Inside a container the count is the whole machine's, not the share the container may use. If the program runs under Docker or Kubernetes, let a flag or an environment variable override it.
Size a cache from the system memory
TotalMemory and AvailableMemory return bytes as an i64. A cache can take a fraction of the total memory, kept between a floor and a ceiling, and never more than half of what is free right now. AvailableMemory returns minus one on systems where it is not known, so check for that before using it.
cache is at most 1 GB: true
cache is positive: true
cache fits in free memory: true
Small differences between systems
Most differences between systems are small: Windows programs end in .exe, and each desktop opens a file with a different command. A function that asks IsWindows or IsMac and returns the right piece keeps the rest of the program free of such checks.
os-platform-guide-dispatch.salam
import os.platform
funcexecutable(name: str): str:
if platform.IsWindows():
ret name + ".exe"endret name
endfuncopen_command(): str:
if platform.IsWindows(): ret"start"endif platform.IsMac(): ret"open"endret"xdg-open"endfuncmain:
mut matched := 0if platform.IsLinux(): matched++ endif platform.IsWindows(): matched++ endif platform.IsMac(): matched++ endif platform.IsFreeBSD(): matched++ endif platform.IsWasm(): matched++ endprintln"at most one system matched:", matched <= 1println"binary name built:", executable("salam").StartsWith("salam")
println"open command chosen:", open_command().Len() > 0end
Output
at most one system matched: true
binary name built: true
open command chosen: true
Run time or compile time
The functions of this package are answered when the program runs, so both branches of an if are compiled on every system. That fails when one branch calls a function that only exists on one system, such as a Windows API. Then use the constant itself: a plain if SALAM_OS_WINDOWS: is decided while compiling and the other branch is left out. See compile-time code for the full list of constants.
os-platform-guide-compile.salam
import os.platform
funclinux_when_compiled(): bool:
if SALAM_OS_LINUX:
rettrueelse:
retfalseendendfuncmain:
println"compile time and run time agree:", linux_when_compiled() == platform.IsLinux()
if SALAM_OS_WINDOWS:
println"this line is only compiled on Windows"else:
println"this line is compiled everywhere else"endend
Output
compile time and run time agree: true
this line is compiled everywhere else
NeedUseShow the system in a report or a logOS, ArchPick a value that differs by systemIsWindows, IsMac, IsLinuxCall code that only compiles on one systemif SALAM_OS_WINDOWS: and the other constantsDecide how much work to do at onceCpuCountDecide how much memory to useTotalMemory, AvailableMemory
import os.platform
funcmain:
known := platform.IsLinux() or platform.IsWindows() or platform.IsMac() or
platform.IsFreeBSD() or platform.IsBSD() or platform.IsAndroid() or platform.IsWasm()
println known, platform.OS().Len() > 0
arch := platform.IsX64() or platform.IsArm64() or platform.IsX86() or platform.IsArm()
println arch, platform.Arch().Len() > 0println platform.CpuCount() >= 1println platform.TotalMemory() > 0, platform.AvailableMemory() <= platform.TotalMemory()
end
The number of logical processors online (hyper-threads count), for deciding how many things to do at once; -1 when it cannot be found. In a container it is the whole machine's count.
The memory available to new programs in bytes (MemAvailable on Linux). Supported on Linux and Windows only; elsewhere it returns minus one. On Windows each call runs PowerShell.