feat: add a module (radix scripts memory) to gather memory usage information

pull/1/head
anemofilia 2023-09-05 00:51:14 -03:00
parent a228f0fe70
commit 0d8c4700c0
No known key found for this signature in database
GPG Key ID: 5A8F3D62C87A2B33
1 changed files with 35 additions and 0 deletions

35
modules/radix/utils/memory.scm Executable file
View File

@ -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"))))