hs.hostinfo
ModuleInformation about this Mac's hardware and how busy it is (CPU and memory usage, load average, swap and memory pressure), and whether it is in light or dark mode.
Every reading is a single cheap system call, so to monitor something over time, read it from
an hs.timer. The exception is CPU usage, which macOS only reports as running totals: a
percentage needs two readings some time apart, so cpuUsage() and cpuCoreUsage() return
Promises that resolve after the interval you choose.
hs.timer.every(5, async () => {
const cpu = await hs.hostinfo.cpuUsage(1)
const mem = hs.hostinfo.memoryUsage()
console.log(`CPU ${cpu.active.toFixed(0)}%, app memory ${(mem.appMemory / 2 ** 30).toFixed(1)} GiB`)
})
Object Types
Plain JavaScript objects that this module's methods and properties take or return:
HSCPUUsage
How a CPU's time was spent over a sampling interval, as percentages
| Field | Type | Description |
|---|---|---|
| user | number | Time spent running user processes |
| system | number | Time spent in the kernel |
| nice | number | Time spent running low-priority (niced) user processes |
| idle | number | Time spent idle |
| active | number | Time spent doing anything (user + system + nice, or 100 - idle) |
Event Types
The events this module's watchers emit, and when. In TypeScript, each type is a union of its event names that you can use as a type of your own:
HSHostInfoEvent
An event name for hs.hostinfo.on(), hs.hostinfo.off() and hs.hostinfo.once()
| Event | Description |
|---|---|
"memoryPressure" |
The system's memory pressure level changed |
"interfaceStyle" |
macOS switched between its light and dark appearance |
Properties
hs.hostinfo.ramAmount
The amount of physical RAM installed on this Mac, in whole gigabytes. memoryUsage().total
gives the exact amount in bytes.
Methods
hs.hostinfo.cpuUsage(seconds) -> Promise<HSCPUUsage>
Measures how busy the CPU is, across all of its cores, over an interval.
macOS only reports how much CPU time has been spent since the Mac started, so this takes
one reading now and another after seconds, and reports how the time in between was
spent. Nothing blocks while it waits. Shorter intervals react faster but are noisier.
hs.hostinfo.cpuUsage(seconds) -> Promise<HSCPUUsage>
| Name | Type | Description |
|---|---|---|
| seconds optional | number | How long to measure for, in seconds, up to an hour (3600). Defaults to 1. |
EINVALIDARG error if seconds isn't a positive number no larger than 3600const cpu = await hs.hostinfo.cpuUsage(0.5)
console.log(`CPU is ${cpu.active.toFixed(0)}% busy`)
hs.hostinfo.cpuCoreUsage(seconds) -> Promise<HSCPUUsage[]>
Measures how busy each CPU core is over an interval. See cpuUsage() for how the
measurement works.
hs.hostinfo.cpuCoreUsage(seconds) -> Promise<HSCPUUsage[]>
| Name | Type | Description |
|---|---|---|
| seconds optional | number | How long to measure for, in seconds, up to an hour (3600). Defaults to 1. |
EINVALIDARG error if seconds isn't a positive number no larger than 3600const cores = await hs.hostinfo.cpuCoreUsage()
cores.forEach((core, i) => console.log(`Core ${i}: ${core.active.toFixed(0)}%`))
hs.hostinfo.memoryUsage() -> object | null
How physical memory is being used right now, in bytes.
The fields match the kernel's accounting (what vm_stat prints, converted from pages to
bytes). Little memory is ever "free" on macOS, since the system keeps recently used files
in memory until something else needs it, so appMemory is usually the most useful figure.
Activity Monitor's "Memory Used" is appMemory + wired + compressed.
hs.hostinfo.memoryUsage() -> object | null
null if macOS wouldn't say| Field | Type | Description |
|---|---|---|
| total | number | The physical memory installed |
| free | number | Memory not being used for anything |
| active | number | Memory in use that has been used recently |
| inactive | number | Memory in use that hasn't been used recently, and can be reclaimed |
| speculative | number | Memory holding data read ahead of time, in case it's needed |
| wired | number | Memory the kernel has locked in place, which can't be compressed or swapped out |
| purgeable | number | Memory apps have marked as safe for the system to discard |
| compressed | number | Memory used to hold compressed data (Activity Monitor's "Compressed") |
| appMemory | number | Memory used by apps and processes (Activity Monitor's "App Memory") |
| cachedFiles | number | Memory holding file contents that can be dropped if needed (Activity Monitor's "Cached Files") |
const mem = hs.hostinfo.memoryUsage()
const used = mem.appMemory + mem.wired + mem.compressed
console.log(`${(used / mem.total * 100).toFixed(0)}% of memory used`)
hs.hostinfo.swapUsage() -> object | null
How much swap space is in use, in bytes. macOS grows and shrinks the swap space as it
needs to, so total changes over time and is 0 when nothing is swapped out.
hs.hostinfo.swapUsage() -> object | null
null if macOS wouldn't say| Field | Type | Description |
|---|---|---|
| total | number | The size of the swap space |
| used | number | How much of it is in use |
| available | number | How much of it is not in use |
const swap = hs.hostinfo.swapUsage()
console.log(`${(swap.used / 2 ** 20).toFixed(0)} MiB of swap in use`)
hs.hostinfo.loadAverage() -> number[] | null
The system load average: the average number of processes running or waiting to run, over the last 1, 5 and 15 minutes.
hs.hostinfo.loadAverage() -> number[] | null
null if macOS wouldn't sayconst [one, five, fifteen] = hs.hostinfo.loadAverage()
console.log(`Load: ${one.toFixed(2)} ${five.toFixed(2)} ${fifteen.toFixed(2)}`)
hs.hostinfo.memoryPressure() -> "normal" | "warning" | "critical" | null
How short of memory the system is right now, as macOS judges it. This is the same
measure as the graph in Activity Monitor's Memory tab. To be told when it changes, use
on("memoryPressure", ...).
hs.hostinfo.memoryPressure() -> "normal" | "warning" | "critical" | null
null if macOS wouldn't sayif (hs.hostinfo.memoryPressure() !== "normal") {
console.log("The system is short of memory")
}
hs.hostinfo.interfaceStyle() -> "light" | "dark"
Whether macOS is using its light or dark appearance right now. When the appearance is set
to Auto, this is the one currently in use. To be told when it changes, use
on("interfaceStyle", ...).
hs.hostinfo.interfaceStyle() -> "light" | "dark"
if (hs.hostinfo.interfaceStyle() === "dark") {
console.log("Dark mode is on")
}
hs.hostinfo.on(event, listener) -> None
Register a listener for host events.
"memoryPressure"fires when the system's memory pressure level changes (seememoryPressure()). The listener gets the new level."interfaceStyle"fires when macOS switches between its light and dark appearance (seeinterfaceStyle()), including automatic switches when the appearance is set to Auto. The listener gets the new style.
hs.hostinfo.on(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSHostInfoEvent | The event to listen for |
| listener | (value: "normal" | "warning" | "critical" | "light" | "dark") => void | Called with the new memory pressure level or interface style |
| Event | Description |
|---|---|
"memoryPressure" |
The system's memory pressure level changed |
"interfaceStyle" |
macOS switched between its light and dark appearance |
try {
hs.hostinfo.on('memoryPressure', (level) => console.log("Memory pressure is now " + level))
hs.hostinfo.on('interfaceStyle', (style) => console.log("Switched to " + style + " mode"))
} catch (err) {
console.error(err.message)
}
hs.hostinfo.off(event, listener) -> None
Remove a previously registered host event listener.
hs.hostinfo.off(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSHostInfoEvent | The event the listener was registered for |
| listener | (value: "normal" | "warning" | "critical" | "light" | "dark") => void | The function originally passed to on |
| Event | Description |
|---|---|
"memoryPressure" |
The system's memory pressure level changed |
"interfaceStyle" |
macOS switched between its light and dark appearance |
const onPressure = (level) => console.log("Memory pressure: " + level)
hs.hostinfo.on('memoryPressure', onPressure)
// later…
hs.hostinfo.off('memoryPressure', onPressure)
hs.hostinfo.once(event, listener) -> None
Register a listener that fires at most once, the next time a host event happens. See on() for the events.
hs.hostinfo.once(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | HSHostInfoEvent | The event to listen for |
| listener | (value: "normal" | "warning" | "critical" | "light" | "dark") => void | Called once, then automatically removed |
| Event | Description |
|---|---|
"memoryPressure" |
The system's memory pressure level changed |
"interfaceStyle" |
macOS switched between its light and dark appearance |
try {
hs.hostinfo.once('memoryPressure', (level) => console.log("Memory pressure changed to " + level))
} catch (err) {
console.error(err.message)
}