From 0d8c4700c034ffc967216808fc9dece758315218 Mon Sep 17 00:00:00 2001 From: anemofilia Date: Tue, 5 Sep 2023 00:51:14 -0300 Subject: [PATCH] feat: add a module (radix scripts memory) to gather memory usage information --- modules/radix/utils/memory.scm | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 modules/radix/utils/memory.scm diff --git a/modules/radix/utils/memory.scm b/modules/radix/utils/memory.scm new file mode 100755 index 0000000..c0c5361 --- /dev/null +++ b/modules/radix/utils/memory.scm @@ -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"))))