silk.system.system_info.
The SystemInfo effect: facts about the computer and the running process that the operating
system reports, such as the host name, the memory, the processors, the network interfaces, the
current user, and the resource usage of the process.
When to use#
Require &mut SystemInfo in code that reports on the host or on its own process, such as an
os module of a language runtime, a status page, or a tool that sizes its work to the memory.
A test can provide fixed values. silk.system.system_info_native has the provider for native
programs.
Details#
Each operation asks the operating system again, so two calls can give different values. The operations never wait for an event: each one makes a few system calls or reads a few files of the system, and returns.
Text that the system reports, such as a host name, a path, or a user name, is returned as
owned Bytes, because the system does not promise UTF-8. Use String.fromUtf8 when you need
text.
Environment variables are not read. A runtime that follows HOME or TMPDIR reads them with
silk.system.host_input, and uses UserInfo when HOME is not set.
Errors#
SystemInfoErrorwhen the system cannot answer: theSystemInfoOperationand the native error code.OutOfMemoryErrorwhen theAllocatorcannot supply a returned value.
Examples#
Read the memory of the computer#
import silk.core.effect { Effect }
import silk.core.result { Result }
import silk.system.system_info { MemoryInfo, SystemInfo, SystemInfoError }
import silk.system.system_info_native { OsSystemInfo }
pub effect fn main() -> i32 {
let mut provider = OsSystemInfo.make()
let result = run Effect.result(SystemInfo.memory()) |> Effect.provideMut<SystemInfo>(&mut provider)
return match move result {
Result<MemoryInfo, SystemInfoError>.Success { value } => {
// The free memory is never more than the total.
if value.total == 0 || value.free > value.total { 1 } else { 0 }
}
Result<MemoryInfo, SystemInfoError>.Failure { .. } => 1
}
}Import as SystemInfo with import silk.system.system_info { SystemInfo }.
Public declarations: 12.
SystemInfoOperation#
pub enum SystemInfoOperationThe operation of SystemInfo that failed.
HostName#
HostNameReading the host name, in SystemInfo.hostName.
Kernel#
KernelReading the kernel names, in SystemInfo.kernel.
Uptime#
UptimeReading the time since boot, in SystemInfo.uptime.
LoadAverage#
LoadAverageReading the load averages, in SystemInfo.loadAverage.
Memory#
MemoryReading the memory counts, in SystemInfo.memory.
Processors#
ProcessorsReading the processors, in SystemInfo.processors.
Parallelism#
ParallelismReading the processors that the process may use, in SystemInfo.availableParallelism.
NetworkInterfaces#
NetworkInterfacesReading the network interfaces, in SystemInfo.networkInterfaces.
User#
UserReading a user account, in SystemInfo.currentUser or SystemInfo.user.
Priority#
PriorityReading a scheduling priority, in SystemInfo.priority.
SetPriority#
SetPriorityChanging a scheduling priority, in SystemInfo.setPriority.
ResourceUsage#
ResourceUsageReading resource usage, in SystemInfo.resourceUsage.
ResidentSetSize#
ResidentSetSizeReading the resident memory of the process, in SystemInfo.residentSetSize.
ExecutablePath#
ExecutablePathReading the path of the program file, in SystemInfo.executablePath.
SystemInfoError#
pub struct SystemInfoErrorA failed SystemInfo operation: which one, and the native error code.
Details#
code is the errno value of the failed call, such as 2 (ENOENT) for a user id that has no
account, 3 (ESRCH) for a process that does not exist, or 13 (EACCES) for a priority that
the process may not set. For a failed Mach call on macOS, it is the kern_return_t value. It
is 0 when the system answered with data that is not valid.
Field operation#
pub operation: SystemInfoOperationThe operation that failed.
Field code#
pub code: i32The native error code, or 0 for data that is not valid.
Implementation Copy for SystemInfoError#
impl Copy for SystemInfoErrorKernelInfo#
pub struct KernelInfoThe names of the running kernel, as uname reports them.
Details#
On Linux, name is Linux, and on macOS it is Darwin. release is the kernel release, such
as 6.8.0-45-generic, version is the build text of the kernel, and machine is the hardware
name, such as x86_64 or arm64.
Field name#
pub name: BytesThe kernel name, such as Linux or Darwin.
Field release#
pub release: BytesThe kernel release, such as 6.8.0-45-generic.
Field version#
pub version: BytesThe build text of the kernel.
Field machine#
pub machine: BytesThe hardware name, such as x86_64 or arm64.
MemoryLimit#
pub struct MemoryLimitA memory limit of a control group, and the memory that the group uses now.
Details#
Linux reports it for a process in a cgroup with a memory.max (cgroup v2) or a
memory.limit_in_bytes (cgroup v1) limit, such as in a container.
Field bytes#
pub bytes: u64The limit, in bytes.
Field used#
pub used: u64The memory that the group uses now, in bytes.
Implementation Copy for MemoryLimit#
impl Copy for MemoryLimitMemoryInfo#
pub struct MemoryInfoThe memory of the computer, in bytes.
Details#
free is memory that nothing uses. available also counts memory that the system can take
back at once, such as file caches, so it is at least free on Linux (MemAvailable). On
macOS, available is the free page count, the same as free.
limit is the memory limit of the control group of the process, when it has one. A process
with a limit can use at most limit.bytes - limit.used more bytes, also when available is
larger.
Field total#
pub total: u64The physical memory, in bytes.
Field free#
pub free: u64The memory that nothing uses, in bytes.
Field available#
pub available: u64The memory that a new allocation can get without swapping, in bytes.
Field limit#
pub limit: Option<MemoryLimit>The memory limit of the control group of the process, if there is one.
Implementation Copy for MemoryInfo#
impl Copy for MemoryInfoProcessorTimes#
pub struct ProcessorTimesThe time that one processor spent in each state since boot, in milliseconds.
Field user#
pub user: u64Time in user mode.
Field nice#
pub nice: u64Time in user mode for processes with a lower priority.
Field system#
pub system: u64Time in kernel mode.
Field idle#
pub idle: u64Time with nothing to do.
Field interrupt#
pub interrupt: u64Time spent on hardware interrupts. It is 0 on macOS.
Implementation Copy for ProcessorTimes#
impl Copy for ProcessorTimesProcessorInfo#
pub struct ProcessorInfoOne logical processor: its model, its speed, and its times.
Details#
model is the model text of the system, such as Intel(R) Xeon(R) CPU @ 2.20GHz, or unknown
when the system has none. speed is the current clock speed in MHz, or 0 when the system does
not report it, as on Apple silicon.
Field model#
pub model: BytesThe model text, or unknown.
Field speed#
pub speed: u32The clock speed in MHz, or 0 when it is not known.
Field times#
pub times: ProcessorTimesThe time that the processor spent in each state since boot.
NetworkInterface#
pub struct NetworkInterfaceOne address of a network interface that is up.
Details#
An interface with more than one address gives one entry for each address, with the same name.
mac is the hardware address of the interface, or all zeros when it has none, such as a
loopback interface. internal is true for a loopback interface. scopeId is the scope of an
IPv6 address, such as the interface index of a link-local address, and 0 for IPv4.
Field name#
pub name: BytesThe interface name, such as eth0 or en0.
Field address#
pub address: IpAddressThe address.
Field netmask#
pub netmask: IpAddressThe network mask, of the same family as the address.
Field mac#
pub mac: [u8; 6]The hardware address, or all zeros.
Field internal#
pub internal: boolTrue for a loopback interface.
Field scopeId#
pub scopeId: u32The IPv6 scope, or 0.
UserInfo#
pub struct UserInfoA user account, from the user database of the system.
Details#
home and shell are empty when the account has none.
Field name#
pub name: BytesThe login name.
Field uid#
pub uid: u32The user id.
Field gid#
pub gid: u32The id of the primary group.
Field home#
pub home: BytesThe home directory.
Field shell#
pub shell: BytesThe login shell.
UsageScope#
pub enum UsageScopeWhich resource usage SystemInfo.resourceUsage reports.
Process#
ProcessThe whole process: all its threads.
Thread#
ThreadThe calling thread.
ResourceUsage#
pub struct ResourceUsageThe resources that a process or a thread used, as getrusage reports them.
Details#
Times are in microseconds. maxResidentBytes is the largest resident memory so far, in bytes
on every system. The counters that a system does not keep are 0: Linux keeps no
sharedMemory, unsharedData, unsharedStack, swaps, messagesSent, messagesReceived,
or signals. For UsageScope.Thread on macOS, only the two times are reported.
Field userMicroseconds#
pub userMicroseconds: u64CPU time in user mode, in microseconds.
Field systemMicroseconds#
pub systemMicroseconds: u64CPU time in kernel mode, in microseconds.
Field maxResidentBytes#
pub maxResidentBytes: u64The largest resident memory so far, in bytes.
Field sharedMemory#
pub sharedMemory: u64The integral shared memory size (ru_ixrss).
Field unsharedData#
pub unsharedData: u64The integral unshared data size (ru_idrss).
Field unsharedStack#
pub unsharedStack: u64The integral unshared stack size (ru_isrss).
Field minorFaults#
pub minorFaults: u64Page faults that needed no input or output.
Field majorFaults#
pub majorFaults: u64Page faults that needed input or output.
Field swaps#
pub swaps: u64Times the process was swapped out.
Field blockInputs#
pub blockInputs: u64Block input operations.
Field blockOutputs#
pub blockOutputs: u64Block output operations.
Field messagesSent#
pub messagesSent: u64IPC messages sent.
Field messagesReceived#
pub messagesReceived: u64IPC messages received.
Field signals#
pub signals: u64Signals received.
Field voluntaryContextSwitches#
pub voluntaryContextSwitches: u64Context switches because the process waited.
Field involuntaryContextSwitches#
pub involuntaryContextSwitches: u64Context switches because a higher priority process ran, or the time slice ended.
Implementation Copy for ResourceUsage#
impl Copy for ResourceUsageSystemInfo#
pub service SystemInfoThe effect that reads facts about the computer and the running process from the operating system.
When to use#
Require &mut SystemInfo in code that reports on the host or on its own process, such as an
os module of a language runtime. A test can provide fixed values. Use the provider in
silk.system.system_info_native in a native program.
Details#
Each operation asks the system again and returns without waiting for an event. Text that the
system reports is owned Bytes, because the system does not promise UTF-8. Environment
variables are not read: a runtime that follows HOME or TMPDIR reads them with
silk.system.host_input. Only SystemInfo.setPriority changes the system.
Operation hostName#
effect fn hostName() -> Bytes ! SystemInfoError | OutOfMemoryError ? &mut SystemInfo | &mut AllocatorReturns the host name of the computer.
Errors#
SystemInfoErrorwithHostNameif the system cannot report it.OutOfMemoryErrorif theAllocatorcannot supply the copy.
Operation kernel#
effect fn kernel() -> KernelInfo ! SystemInfoError | OutOfMemoryError ? &mut SystemInfo | &mut AllocatorReturns the names of the running kernel: name, release, version, and hardware.
Errors#
SystemInfoErrorwithKernelif the system cannot report them.OutOfMemoryErrorif theAllocatorcannot supply the copies.
Operation uptime#
effect fn uptime() -> u64 ! SystemInfoError ? &mut SystemInfoReturns the time since the computer started, in nanoseconds. It counts time asleep.
Errors#
SystemInfoErrorwithUptimeif the system cannot report it.
Operation loadAverage#
effect fn loadAverage() -> [f64; 3] ! SystemInfoError ? &mut SystemInfoReturns the 1, 5, and 15 minute load averages, in this order.
Details#
A load average is the mean number of threads that ran or waited to run.
Errors#
SystemInfoErrorwithLoadAverageif the system cannot report them.
Operation memory#
effect fn memory() -> MemoryInfo ! SystemInfoError ? &mut SystemInfoReturns the total, free, and available memory of the computer, and the memory limit of the process when it has one.
Errors#
SystemInfoErrorwithMemoryif the system cannot report them.
Operation processors#
effect fn processors() -> List<ProcessorInfo> ! SystemInfoError | OutOfMemoryError ? &mut SystemInfo | &mut AllocatorReturns one entry for each logical processor that is online, in the order of the system.
Errors#
SystemInfoErrorwithProcessorsif the system cannot report them.OutOfMemoryErrorif theAllocatorcannot supply the list.
Operation availableParallelism#
effect fn availableParallelism() -> usize ! SystemInfoError ? &mut SystemInfoReturns the number of processors that the process may run on, at least 1.
Details#
On Linux, it counts the CPU affinity mask of the process, so it can be less than the number of processors online. On macOS, it is the number of processors online.
Errors#
SystemInfoErrorwithParallelismif the system cannot report it.
Operation networkInterfaces#
effect fn networkInterfaces() -> List<NetworkInterface> ! SystemInfoError | OutOfMemoryError ? &mut SystemInfo | &mut AllocatorReturns one entry for each address of each network interface that is up and running.
Details#
Only IPv4 and IPv6 addresses are listed. An interface with no address is not listed.
Errors#
SystemInfoErrorwithNetworkInterfacesif the system cannot report them.OutOfMemoryErrorif theAllocatorcannot supply the list.
Operation currentUser#
effect fn currentUser() -> UserInfo ! SystemInfoError | OutOfMemoryError ? &mut SystemInfo | &mut AllocatorReturns the account of the effective user of the process.
Errors#
SystemInfoErrorwithUserif the account cannot be read, with code 2 (ENOENT) if the user id has no account.OutOfMemoryErrorif theAllocatorcannot supply the copies.
Operation user#
effect fn user(uid: u32) -> UserInfo ! SystemInfoError | OutOfMemoryError ? &mut SystemInfo | &mut AllocatorReturns the account of user id uid.
Errors#
SystemInfoErrorwithUserif the account cannot be read, with code 2 (ENOENT) ifuidhas no account.OutOfMemoryErrorif theAllocatorcannot supply the copies.
Operation priority#
effect fn priority(pid: i32) -> i32 ! SystemInfoError ? &mut SystemInfoReturns the scheduling priority of process pid, from -20 (first) to 19 (last). Process 0 is
the calling process.
Errors#
SystemInfoErrorwithPriority, with code 3 (ESRCH) if no process has the id.
Operation setPriority#
effect fn setPriority(pid: i32, value: i32) -> () ! SystemInfoError ? &mut SystemInfoSets the scheduling priority of process pid to value, from -20 (first) to 19 (last).
Process 0 is the calling process. The system limits a value outside the range to it.
Errors#
SystemInfoErrorwithSetPriority, with code 3 (ESRCH) if no process has the id, or 13 (EACCES) or 1 (EPERM) if the process may not set that priority.
Operation resourceUsage#
effect fn resourceUsage(scope: UsageScope) -> ResourceUsage ! SystemInfoError ? &mut SystemInfoReturns the resources that the process or the calling thread used.
Errors#
SystemInfoErrorwithResourceUsageif the system cannot report them.
Operation residentSetSize#
effect fn residentSetSize() -> u64 ! SystemInfoError ? &mut SystemInfoReturns the resident memory of the process now, in bytes.
Errors#
SystemInfoErrorwithResidentSetSizeif the system cannot report it.
Operation executablePath#
effect fn executablePath() -> Bytes ! SystemInfoError | OutOfMemoryError ? &mut SystemInfo | &mut AllocatorReturns the absolute path of the program file of the process, with links resolved.
Errors#
SystemInfoErrorwithExecutablePathif the system cannot report it.OutOfMemoryErrorif theAllocatorcannot supply the copy.
Operation processId#
effect fn processId() -> i32 ? &mut SystemInfoReturns the id of the process.
Operation parentProcessId#
effect fn parentProcessId() -> i32 ? &mut SystemInfoReturns the id of the parent process.
Implementation SystemInfo#
impl SystemInfoAssociated function failure#
pub fn failure(operation: SystemInfoOperation, code: i32) -> SystemInfoErrorReturns a SystemInfoError for operation and the native error code code. Providers use
it when an operation fails.
Category: System.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.