Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Compression ratio of zram
#!/usr/bin/env ruby
MIB = 1024*1024
Dir.entries(BLOCK_PATH).select{|e| e.start_with? 'zram'}.each {|dev|
orig = BLOCK_PATH, dev, 'orig_data_size').to_i
compr = BLOCK_PATH, dev, 'compr_data_size').to_i
printf "%s: %6.2f%% (%.2f MiB -> %.2f MiB)\n", dev, 100*compr/orig, orig/MIB, compr/MIB
#!/usr/bin/csi -s
(use srfi-1 srfi-13 utils posix files format)
(define BLOCK-PREFIX "/sys/block")
(define (metrics-for-dev name)
(let* ((dev-path (make-absolute-pathname BLOCK-PREFIX name))
(compr-path (make-absolute-pathname dev-path "compr_data_size"))
(orig-path (make-absolute-pathname dev-path "orig_data_size")))
(cons (string->number (string-trim-right (read-all compr-path)))
(string->number (string-trim-right (read-all orig-path))))))
(define (format-size n)
(format "~,2F MiB" (/ n (* 1024 1024))))
(define (format-dev name metrics)
(let* ((compr (car metrics)) (orig (cdr metrics))
(ratio (/ compr orig)))
(format "~A: ~6,2F% (~A -> ~A)"
name (* 100 ratio) (format-size orig) (format-size compr))))
(define (display-zram)
(let* ((devs (directory BLOCK-PREFIX))
(zdevs (filter (lambda (name) (string-prefix? "zram" name)) devs)))
(for-each (lambda (dev) (print (format-dev dev (metrics-for-dev dev)))) zdevs)))
#!/usr/bin/env zsh
for f in /sys/block/zram*; do
orig=$(< $f/orig_data_size)
compr=$(< $f/compr_data_size)
printf '%s: %6.2f%% (%.2f MiB -> %.2f MiB)\n' \
${f:t} \
$((100 * ${compr}.0 / ${orig})) \
$((${orig} / 1048576.0)) \
$((${compr} / 1048576.0))
Copy link

pcarrier commented Sep 25, 2012

bash only has integer arithmetic in 2012!?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment