-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmode-line-cpu.lisp
97 lines (86 loc) · 3.68 KB
/
mode-line-cpu.lisp
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
;;; mode-line-cpu.lisp --- CPU info for the mode line
;; Copyright © 2007 Anonymous Coward, Jonathan Moore Liles
;; Copyright © 2019 Alex Kost <[email protected]>
;; This program 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 program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file originates from
;; <https://github.com/stumpwm/stumpwm-contrib/blob/master/modeline/cpu>.
;; I wanted to display a more sophisticated CPU info, so I adjusted that
;; file for my needs.
;;
;; Info on CPU usage ("/proc/stat" file) is taken from "man proc".
;;; Code:
(defpackage #:al/stumpwm-cpu
(:use :common-lisp
:stumpwm)
(:export #:cpu-mode-line-string))
(in-package #:al/stumpwm-cpu)
(defvar last-user-time 0)
(defvar last-system-time 0)
(defvar last-idle-time 0)
(defvar last-iowait-time 0)
(defvar last-irq-time 0)
(defun current-cpu-usage ()
"Return the average CPU usage since the last call.
1st value is a total percent of CPU usage.
2nd value is percent of CPU time spent in user mode.
3rd value is percent of CPU time spent in system mode.
4th value is percent of CPU time spent waiting for IO to complete.
5th value is percent of CPU time spent servicing interrupts."
(let ((cpu% 0)
(user% 0)
(system% 0)
(io% 0)
(irq% 0))
(with-open-file (stat #P"/proc/stat" :direction :input)
(read stat) ; read the first "cpu" word
(let* ((cur-user-time (+ (read stat) (read stat)))
(cur-system-time (read stat))
(cur-idle-time (read stat))
(cur-iowait-time (read stat))
(cur-irq-time (+ (read stat) (read stat)))
(user-time (- cur-user-time last-user-time))
(system-time (- cur-system-time last-system-time))
(idle-time (- cur-idle-time last-idle-time))
(iowait-time (- cur-iowait-time last-iowait-time))
(irq-time (- cur-irq-time last-irq-time))
(cpu-time (+ user-time system-time iowait-time irq-time))
(total-time (+ cpu-time idle-time)))
(unless (zerop total-time)
(setf cpu% (/ cpu-time total-time)
user% (/ user-time total-time)
system% (/ system-time total-time)
io% (/ iowait-time total-time)
irq% (/ irq-time total-time)
last-user-time cur-user-time
last-system-time cur-system-time
last-idle-time cur-idle-time
last-iowait-time cur-iowait-time
last-irq-time cur-irq-time))))
(values cpu% user% system% io% irq%)))
(defun cpu-mode-line-string ()
"Return a string with CPU info suitable for the mode-line."
(defun d (float)
(round (* 100 float)))
(multiple-value-bind (cpu user system io irq)
(current-cpu-usage)
(let ((cpu (d cpu))
(user (d user))
(system (d system))
(io (d io))
(irq (d irq)))
(format nil "^[^b^7*^[~A~3D^]%% (~2,'0D ~2,'0D ~D ~D)^]"
(bar-zone-color cpu) cpu
user system io irq))))
;;; mode-line-cpu.lisp ends here