-
Notifications
You must be signed in to change notification settings - Fork 8
/
mls-memory.el
206 lines (178 loc) · 6.68 KB
/
mls-memory.el
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
;;; mls-memory.el --- display various memory stats in the mode-line -*- coding: mule-utf-8 -*-
;; Copyright (C) 2012 Kajetan Rzepecki
;; Author: Kajetan Rzepecki
;; Created: 1 Sep 2012
;; Keywords: hardware
;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Usage:
;; (require 'mls-memory)
;; (mls-memory-start)
;; There are a few variables to tweak:
;; `mls-memory-update-interval' - Time interval after which current memory stats are updated.
;; `mls-memory-format' - A string format used in the mode-line.
;; Supports the following escape sequences:
;; %r - Percentile main RAM usage.
;; %R - Percentile main RAM usage (no buffers or cache).
;; %t - Total RAM in MB.
;; %c - Cache in MB.
;; %b - Buffers in MB.
;; %f - Free main RAM in MB.
;; %F - Total free memory (with cache and buffers) in MB.
;; %u - Used memory in MB.
;; %U - Used memory (no cache or buffers) in MB.
;; %S - Swap usage in percent.
;; %St - Total swap in MB.
;; %Su - Used swap in MB.
;; %Sf - Free swap in MB.
;;; TODO:
;; Maybe add HDD usage.
;;; Code:
(require 'cl)
(require 'mls-common)
(defvar mls-memory-formatters nil)
(defvar mls-memory-timer nil)
(defvar mls-memory-mode-line-string "")
(defvar mls-memory-shell-command "free -o -m | tail -n2")
(defvar mls-memory-settings
'((:formats
((:primary "&R{m}")
(:secondary " MEM[%R{%%}] SWAP[%S{%%}]")
(:monitor "&R")))
(:levels
(("%R" ((90.0 "crit")
(50.0 "warn")
(0.0 "norm")))
("%S" ((90.0 "crit")
(50.0 "warn")
(0.0 "norm"))))))
"MEMORY stats settings.")
(defgroup mls-memory nil
"Display various memory stats in the mode-line."
:group 'mls-memory)
(defcustom mls-memory-update-interval 2
"Number of seconds between memory stats recalculation."
:type 'number
:group 'mls-memory)
(defcustom mls-memory-format "%R %S"
"Format string:
%r - Percentile main RAM usage.
%R - Percentile main RAM usage (no buffers or cache).
%t - Total RAM in MB.
%c - Cache in MB.
%b - Buffers in MB.
%f - Free main RAM in MB.
%F - Total free memory (with cache and buffers) in MB.
%u - Used memory in MB.
%U - Used memory (no cache or buffers) in MB.
%S - Swap usage in percent.
%St - Total swap in MB.
%Su - Used swap in MB.
%Sf - Free swap in MB."
:type 'string
:group 'mls-memory)
(defun mls-memory-update ()
"Update stats."
(setq mls-memory-mode-line-string (mls-memory-stats))
(force-mode-line-update)
(sit-for 0))
(defun mls-memory-start ()
"Start displaying memory usage stats in the mode-line."
(interactive)
(setq mls-memory-mode-line-string "")
(mls-set-timer 'mls-memory-timer
mls-memory-update-interval
'mls-memory-update))
(defun mls-memory-stop ()
"Stop displaying memory usage stats in the mode-line."
(interactive)
(setq mls-memory-mode-line-string "")
(mls-cancel-timer 'mls-memory-timer))
(defun mls-memory-stats ()
"Build stats."
(let ((stats (mls-memory-fetch)))
(mls-format-expand mls-memory-formatters mls-memory-format stats)))
(defun mls-memory-fetch ()
"Returns a bunch of memory stats in a form of an alist."
(let ((stats (mapcar #'split-string
(remove-if (lambda (str) (string= str ""))
(split-string
(shell-command-to-string mls-memory-shell-command)
"\n")))))
(mapcar (lambda (lst)
(cons (car lst)
(mapcar #'string-to-number (cdr lst))))
stats)))
(setq mls-memory-formatters
(list
; Percentile RAM usage.
(cons "r" (lambda (stats)
(let* ((ram (assoc "Mem:" stats))
(used (nth 2 ram))
(total (nth 1 ram)))
(format "%.0f" (* 100 (/ (float used)
total))))))
; Percentile RAM usage (no cache).
(cons "R" (lambda (stats)
(let* ((ram (assoc "Mem:" stats))
(total (nth 1 ram))
(used (nth 2 ram))
(buffers (nth 5 ram))
(cache (nth 6 ram)))
(format "%.0f" (* 100 (/ (- used buffers cache)
(float total)))))))
; Total RAM.
(cons "t" (lambda (stats)
(number-to-string (nth 1 (assoc "Mem:" stats)))))
; Cache.
(cons "c" (lambda (stats)
(number-to-string (nth 6 (assoc "Mem:" stats)))))
; Buffers.
(cons "b" (lambda (stats)
(number-to-string (nth 5 (assoc "Mem:" stats)))))
; Free memory.
(cons "f" (lambda (stats)
(number-to-string (nth 3 (assoc "Mem:" stats)))))
; Free memory total.
(cons "F" (lambda (stats)
(let* ((ram (assoc "Mem:" stats))
(free (nth 3 ram))
(buffers (nth 5 ram))
(cache (nth 6 ram)))
(number-to-string (+ free buffers cache)))))
; Used memory.
(cons "u" (lambda (stats)
(number-to-string (nth 2 (assoc "Mem:" stats)))))
; Used memory (no buffers or cache).
(cons "U" (lambda (stats)
(let* ((ram (assoc "Mem:" stats))
(used (nth 2 ram))
(buffers (nth 5 ram))
(cache (nth 6 ram)))
(number-to-string (- used buffers cache)))))
; Total swap.
(cons "St" (lambda (stats)
(number-to-string (nth 1 (assoc "Swap:" stats)))))
; Used swap.
(cons "Su" (lambda (stats)
(number-to-string (nth 2 (assoc "Swap:" stats)))))
; Free swap.
(cons "Sf" (lambda (stats)
(number-to-string (nth 3 (assoc "Swap:" stats)))))
; Percentile swap usage.
(cons "S" (lambda (stats)
(let* ((swap (assoc "Swap:" stats))
(total (nth 1 swap))
(used (nth 2 swap)))
(format "%.0f" (* 100 (/ (float used)
total))))))))
(provide 'mls-memory)
;;; mls-memory.el ends here