feat: add a module (radix scripts memory) to gather memory usage information
parent
a228f0fe70
commit
0d8c4700c0
|
@ -0,0 +1,35 @@
|
|||
(define-module (radix utils memory)
|
||||
#:use-module (srfi srfi-26)
|
||||
#:use-module (ice-9 rdelim)
|
||||
#:export (current-memory-info
|
||||
current-ram-usage
|
||||
current-swap-usage))
|
||||
|
||||
(define (current-memory-info)
|
||||
(let* ((port (open "/proc/meminfo" O_RDONLY))
|
||||
(lines (let loop ((line (read-line port))
|
||||
(acc '()))
|
||||
(if (eof-object? line) acc
|
||||
(loop (read-line port)
|
||||
(cons line acc))))))
|
||||
(close-input-port port)
|
||||
(map (compose
|
||||
(lambda (line)
|
||||
(cons (car line)
|
||||
((compose (cut round/ <> 1024)
|
||||
string->number
|
||||
car
|
||||
(cut string-split <> #\ )
|
||||
string-trim
|
||||
cadr)
|
||||
line)))
|
||||
(cut string-split <> #\:))
|
||||
lines)))
|
||||
|
||||
(define (current-ram-usage)
|
||||
(apply - (map (cut assoc-ref (current-memory-info) <>)
|
||||
'("MemTotal" "MemFree" "Buffers" "Cached" "SReclaimable"))))
|
||||
|
||||
(define (current-swap-usage)
|
||||
(apply - (map (cut assoc-ref (current-memory-info) <>)
|
||||
'("SwapTotal" "SwapFree"))))
|
Loading…
Reference in New Issue