forked from lilydjwg/swapview-rosetta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswapview.scm
68 lines (60 loc) · 2.26 KB
/
swapview.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(use srfi-1 extras data-structures utils posix)
(include "format/format.scm")
(import format)
(define-record process-info pid swap-usage command-line)
(define (filesize size)
(let lp ((units '(B KiB MiB GiB TiB))
(size size))
(if (and (> size 1100) (not (null? units)))
(lp (cdr units) (/ size 1024))
(if (eq? (car units) 'B)
(conc size "B")
(format #f "~,1f~a" size (car units))))))
(define (get-command-line pid)
(let* ((cmdline-file (sprintf "/proc/~a/cmdline" pid))
(raw-commandline (with-input-from-file cmdline-file read-all)))
(string-translate
(string-chomp raw-commandline "\x00") #\nul #\space)))
(define (get-process-swap-usage pid)
(condition-case
(let ((smaps (open-input-file (sprintf "/proc/~a/smaps" pid))))
(let lp ((size 0)
(line (read-line smaps)))
(cond ((eof-object? line)
(close-input-port smaps)
(and (not (zero? size))
(make-process-info
pid (* 1024 size) (get-command-line pid))))
(else
(lp (if (substring=? "Swap:" line)
(+ size
(string->number
(cadr (reverse (string-split line)))))
size)
(read-line smaps))))))
((exn file) #f)))
(define (get-swapped-processes)
(sort
(filter-map
(lambda (file-name)
(and (string->number file-name)
(get-process-swap-usage file-name)))
(directory "/proc"))
(lambda (a b)
(< (process-info-swap-usage a) (process-info-swap-usage b)))))
(define (main)
(let* ((results (get-swapped-processes))
(FORMATSTR "~5@a ~9@a ~@a~%")
(total-swap 0))
(format #t FORMATSTR "PID" "SWAP" "COMMAND")
(map
(lambda (swapped-process-info)
(set! total-swap
(+ total-swap (process-info-swap-usage swapped-process-info)))
(format #t FORMATSTR
(process-info-pid swapped-process-info)
(filesize (process-info-swap-usage swapped-process-info))
(process-info-command-line swapped-process-info)))
results)
(format #t "Total: ~8@a~%" (filesize total-swap))))
(main)