mode-line-cpu.lisp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ;;; mode-line-cpu.lisp --- CPU info for the mode line
  2. ;; Copyright © 2007 Anonymous Coward, Jonathan Moore Liles
  3. ;; Copyright © 2019 Alex Kost <alezost@gmail.com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file originates from
  18. ;; <https://github.com/stumpwm/stumpwm-contrib/blob/master/modeline/cpu>.
  19. ;; I wanted to display a more sophisticated CPU info, so I adjusted that
  20. ;; file for my needs.
  21. ;;
  22. ;; Info on CPU usage ("/proc/stat" file) is taken from "man proc".
  23. ;;; Code:
  24. (defpackage #:al/stumpwm-cpu
  25. (:use :common-lisp
  26. :stumpwm)
  27. (:export #:cpu-mode-line-string))
  28. (in-package #:al/stumpwm-cpu)
  29. (defvar *last-user-time* 0)
  30. (defvar *last-system-time* 0)
  31. (defvar *last-idle-time* 0)
  32. (defvar *last-iowait-time* 0)
  33. (defvar *last-irq-time* 0)
  34. (defun current-cpu-usage ()
  35. "Return the average CPU usage since the last call.
  36. 1st value is a total percent of CPU usage.
  37. 2nd value is percent of CPU time spent in user mode.
  38. 3rd value is percent of CPU time spent in system mode.
  39. 4th value is percent of CPU time spent waiting for IO to complete.
  40. 5th value is percent of CPU time spent servicing interrupts."
  41. (let ((cpu% 0)
  42. (user% 0)
  43. (system% 0)
  44. (io% 0)
  45. (irq% 0))
  46. (with-open-file (stat #P"/proc/stat" :direction :input)
  47. (read stat) ; read the first "cpu" word
  48. (let* ((cur-user-time (+ (read stat) (read stat)))
  49. (cur-system-time (read stat))
  50. (cur-idle-time (read stat))
  51. (cur-iowait-time (read stat))
  52. (cur-irq-time (+ (read stat) (read stat)))
  53. (user-time (- cur-user-time *last-user-time*))
  54. (system-time (- cur-system-time *last-system-time*))
  55. (idle-time (- cur-idle-time *last-idle-time*))
  56. (iowait-time (- cur-iowait-time *last-iowait-time*))
  57. (irq-time (- cur-irq-time *last-irq-time*))
  58. (cpu-time (+ user-time system-time iowait-time irq-time))
  59. (total-time (+ cpu-time idle-time)))
  60. (unless (zerop total-time)
  61. (setf cpu% (/ cpu-time total-time)
  62. user% (/ user-time total-time)
  63. system% (/ system-time total-time)
  64. io% (/ iowait-time total-time)
  65. irq% (/ irq-time total-time)
  66. *last-user-time* cur-user-time
  67. *last-system-time* cur-system-time
  68. *last-idle-time* cur-idle-time
  69. *last-iowait-time* cur-iowait-time
  70. *last-irq-time* cur-irq-time))))
  71. (values cpu% user% system% io% irq%)))
  72. (defun cpu-mode-line-string ()
  73. "Return a string with CPU info suitable for the mode-line."
  74. (defun d (float)
  75. (round (* 100 float)))
  76. (multiple-value-bind (cpu user system io irq)
  77. (current-cpu-usage)
  78. (let ((cpu (d cpu))
  79. (user (d user))
  80. (system (d system))
  81. (io (d io))
  82. (irq (d irq)))
  83. (format nil "^[^b^7*CPU:^[~A~3D^]%% (~2,'0D ~2,'0D ~D ~D)^]"
  84. (bar-zone-color cpu) cpu
  85. user system io irq))))
  86. ;;; mode-line-cpu.lisp ends here