OS
import "std/sys" (included in import "std") · namespace std.sys
Questions about the operating system and the environment the program runs in.
grav
import "std"
fn main() {
print(OS.platform() != "")
print(OS.cpuCount() >= 1)
print(OS.getEnvOr("SURELY_NOT_SET_GRAV", "fallback"))
OS.setEnv("GRAV_DEMO", "1")
print(OS.hasEnv("GRAV_DEMO"))
print(env("GRAV_DEMO"))
OS.unsetEnv("GRAV_DEMO")
print(OS.hasEnv("GRAV_DEMO"))
}true
true
fallback
true
1
false| Function | Gives |
|---|---|
platform(), arch(), kernel() | e.g. "linux", "x86_64", the kernel version |
hostname(), user(), home() | the machine name, the user name, the home directory |
cwd(), chdir(path) | the working directory, and changing it |
pid(), ppid() | this process's id and its parent's |
cpuCount(), pageSize() | online CPU cores, the memory page size |
getEnv, getEnvOr, hasEnv, setEnv, unsetEnv | environment variables (getEnv gives "" when unset) |
isTerminal() | whether standard output is a terminal |
Load, memory and uptime
OS.loadAverage() gives the 1, 5 and 15 minute load averages (compare them with cpuCount()), memoryTotal() and memoryAvailable() the machine's memory in bytes (available counts caches the system can reclaim, as Linux's MemAvailable does), and uptime() the seconds since the machine started. Each is -1 where the system cannot tell. They read /proc on Linux and sysctl on macOS, and cost a few microseconds.
grav
import "std"
fn main() {
let load = OS.loadAverage()
print("${load.size()} averages, busy: ${load[0] > OS.cpuCount() as double}")
print(OS.memoryAvailable() <= OS.memoryTotal())
}API
class OS
Static members
static fn platform() -> string— "linux", "macos", "windows", ... (the system this program was built for).static fn arch() -> string— The machine architecture ("x86_64", "aarch64", ...).static fn hostname() -> string— The computer's host name.static fn kernel() -> string— The kernel's name and release.static fn user() -> string— The user name ($USER).static fn home() -> string— The home directory ($HOME).static fn cwd() -> string— The working directory.static fn chdir(path: string) -> bool— Changes the working directory; whether it worked.static fn pid() -> int— This process's id.static fn ppid() -> int— The parent process's id.static fn cpuCount() -> int— The number of CPU cores online.static fn loadAverage() -> Array<double>— The load averages over 1, 5 and 15 minutes (runnable processes, to compare with cpuCount()); -1 each where the system cannot tell.static fn memoryTotal() -> long— The machine's memory in bytes (-1 where the system cannot tell).static fn memoryAvailable() -> long— Memory available to new work in bytes: free plus caches the system can reclaim (Linux's MemAvailable); -1 where it cannot tell.static fn uptime() -> long— Seconds since the machine started (-1 where the system cannot tell).static fn pageSize() -> int— The memory page size in bytes.static fn getEnv(name: string) -> string— An environment variable ("" when unset).static fn getEnvOr(name: string, fallback: string) -> string— An environment variable, orfallbackwhen unset.static fn hasEnv(name: string) -> bool— Whether an environment variable is set.static fn setEnv(name: string, value: string) -> bool— Sets an environment variable (for this process and its children); whether it worked.static fn unsetEnv(name: string) -> bool— Removes an environment variable; whether it worked.static fn isTerminal() -> bool— Is stdout attached to a terminal (vs. a pipe or file)?