mon.c
April 3, 2026 ยท View on GitHub
Purpose
Linux-oriented host/process monitor backed by /proc and /sys/class/net.
Platform Scope
- The whole public implementation in
mon.cis compiled only on non-Windows builds. - In practice it is Linux-specific because it depends on
/procand/sys/class/net.
API Reference
int XMon_InitStats(xmon_stats_t *pStats)
- Arguments:
pStats: monitoring state object.
- Does:
- initializes the CPU core array, network-interface array, memory counters, worker-task state and network lock.
- Returns:
XSTDOKon success.XSTDERRwhen array initialization fails.
void XMon_DestroyStats(xmon_stats_t *pStats)
- Arguments:
pStats: monitoring state.
- Does:
- destroys CPU/network arrays and the network lock.
- Returns:
- no return value.
- Note:
- this function does not stop the worker thread for you.
int XMon_StartMonitoring(xmon_stats_t *pStats, uint32_t nIntervalU, xpid_t nPID)
- Arguments:
pStats: monitoring state.nIntervalU: update interval in microseconds.nPID: specific process id, or<= 0for the current process.
- Does:
- validates
/proc/<pid>when a specific pid is requested. - stores interval and pid.
- starts a detached repeating worker via
XTask_Start().
- validates
- Returns:
- current task status stored in
pStats->monitoring.nStatus. - typically
XTASK_STAT_CREATEDon successful thread creation. XTASK_STAT_FAILon worker creation failure.XSTDERRwhen the target/proc/<pid>directory does not exist.
- current task status stored in
uint32_t XMon_StopMonitoring(xmon_stats_t *pStats, uint32_t nWaitUsecs)
- Arguments:
pStats: monitoring state.nWaitUsecs: sleep interval passed intoXTask_Stop().
- Does:
- requests worker shutdown and waits until status becomes
XTASK_STAT_STOPPED.
- requests worker shutdown and waits until status becomes
- Returns:
- approximate waited microseconds.
uint32_t XMon_WaitLoad(xmon_stats_t *pStats, uint32_t nWaitUsecs)
- Arguments:
pStats: monitoring state.nWaitUsecs: sleep interval between polls.
- Does:
- waits until
nLoadDonebecomes true.
- waits until
- Returns:
- approximate waited microseconds.
int XMon_GetMemoryInfo(xmon_stats_t *pStats, xmem_info_t *pInfo)
- Arguments:
- live stats object and destination memory snapshot.
- Does:
- copies all memory-related atomic fields into
pInfo.
- copies all memory-related atomic fields into
- Returns:
pInfo->nMemoryTotal.0when the monitor has not populated memory totals yet.
int XMon_GetCPUStats(xmon_stats_t *pStats, xcpu_stats_t *pCpuStats)
- Arguments:
- live stats object and destination CPU snapshot.
- Does:
- deep-copies aggregate CPU usage, total CPU info and every tracked core into a caller-owned
pCpuStats->coresarray.
- deep-copies aggregate CPU usage, total CPU info and every tracked core into a caller-owned
- Returns:
- positive core count on success.
0when no cores are available yet.-1when the destination array cannot be initialized.-2when copy-out produced no usable cores.
- Ownership:
- caller must destroy
pCpuStats->coresafter use.
- caller must destroy
int XMon_GetNetworkStats(xmon_stats_t *pStats, xarray_t *pIfaces)
- Arguments:
- live stats object and destination array.
- Does:
- locks
netLock. - deep-copies only active network interfaces into
pIfaces.
- locks
- Returns:
- copied interface count.
0when there are no interfaces yet or destination array init fails.
- Ownership:
- caller must destroy
pIfacesafter use.
- caller must destroy
Data Sources and Calculation Rules
- memory:
/proc/meminfo/proc/<pid>/statusor/proc/self/status
- CPU:
/proc/stat/proc/loadavg/proc/<pid>/stator/proc/self/stat
- network:
/sys/class/net/*- IP addresses are resolved through
XAddr_GetIFCIP()
CPU percentages are calculated from differences between two consecutive raw snapshots, so the first update mainly seeds baseline state.
Important Notes
- The source currently copies
KernelSpaceChildsfromUserSpaceChildsin two places. Treat per-process child CPU split fields carefully. - Network bandwidth-per-second counters depend on
nIntervalU / XMON_INTERVAL_USEC; choose a sane non-zero update interval.