Skip to content

Instantly share code, notes, and snippets.

@pcarrier
Created September 25, 2012 17:53
Show Gist options
  • Select an option

  • Save pcarrier/3783415 to your computer and use it in GitHub Desktop.

Select an option

Save pcarrier/3783415 to your computer and use it in GitHub Desktop.
Compression ratio of zram
#!/usr/bin/env ruby
BLOCK_PATH='/sys/block'
MIB = 1024*1024
Dir.entries(BLOCK_PATH).select{|e| e.start_with? 'zram'}.each {|dev|
orig = File.read(File.join BLOCK_PATH, dev, 'orig_data_size').to_i
compr = File.read(File.join 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)))
(display-zram)
#!/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))
done
@pcarrier
Copy link
Copy Markdown
Author

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