From 3ddd2df7cb55d2810cc7ce06d2fd2d1503591f28 Mon Sep 17 00:00:00 2001 From: bo-tato <122528427+bo-tato@users.noreply.github.com> Date: Sat, 13 May 2023 14:54:05 +0000 Subject: [PATCH 1/4] include os thread in thread attributes for sbcl --- slynk/backend/sbcl.lisp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/slynk/backend/sbcl.lisp b/slynk/backend/sbcl.lisp index d1a782b4b..58bbcd1ba 100644 --- a/slynk/backend/sbcl.lisp +++ b/slynk/backend/sbcl.lisp @@ -1708,6 +1708,9 @@ stack." "Running" "Stopped")) + (defimplementation thread-attributes (thread) + (list :tid (sb-thread:thread-os-tid thread))) + (defimplementation make-lock (&key name) (sb-thread:make-mutex :name name)) From 03e1c5ee270ae58e10b6a3ec42a9147fb5501a46 Mon Sep 17 00:00:00 2001 From: bo-tato <122528427+bo-tato@users.noreply.github.com> Date: Sat, 13 May 2023 23:28:44 +0000 Subject: [PATCH 2/4] show elapsed time and pcpu --- sly.el | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sly.el b/sly.el index 5dcec4d38..df373a5b9 100644 --- a/sly.el +++ b/sly.el @@ -6286,7 +6286,7 @@ was called originally." (sly-eval-async '(slynk:list-threads) #'(lambda (threads) (with-current-buffer (current-buffer) - (sly--display-threads threads)))))) + (sly--display-threads (sly--threads-add-info threads))))))) (defun sly-move-point (position) "Move point in the current buffer and in the window the buffer is displayed." @@ -6295,6 +6295,16 @@ was called originally." (when window (set-window-point window position)))) +(defun sly--threads-add-info (threads) + "When TID is present, add running time and CPU usage of THREADS." + (when-let ((columns (car threads)) + (tid-column (seq-position columns :tid))) + (setf columns (nconc columns '(:time :%%cpu))) + (dolist (thread (cdr threads)) + (let-alist (process-attributes (nth tid-column thread)) + (setf thread (nconc thread (list (format-seconds "%Y, %D, %.2h:%z%.2m:%.2s" .etime) (format "%.1f" .pcpu))))))) + threads) + (defun sly--display-threads (threads) (let* ((inhibit-read-only t) (old-thread-id (get-text-property (point) 'thread-id)) From a40bc163d0aa754892dd281310fd7425c3ceaf65 Mon Sep 17 00:00:00 2001 From: bo-tato <122528427+bo-tato@users.noreply.github.com> Date: Sun, 14 May 2023 13:06:38 +0000 Subject: [PATCH 3/4] properly compute per thread cpu usage --- sly.el | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/sly.el b/sly.el index df373a5b9..4ddd14ea8 100644 --- a/sly.el +++ b/sly.el @@ -6295,14 +6295,50 @@ was called originally." (when window (set-window-point window position)))) +(defvar sly--threads-last-update-time nil + "Time of the last update to threads cpu usage info.") + +(defvar sly--threads-last-cpu-time (make-hash-table) + "Hashtable mapping thread ID to CPU usage time.") + +;; in emacs 29 this is no longer need, we can use current-cpu-time +(defvar sly--clocks-per-sec nil + "Clock ticks per second.") + +(defun sly--thread-cpu-time (tid) + "Compute CPU usage in seconds for TID from /proc/pid/task/tid/stat" + (unless sly--clocks-per-sec + (setq sly--clocks-per-sec (string-to-number (shell-command-to-string "getconf CLK_TCK")))) + + (with-file-contents! (format "/proc/%d/task/%d/stat" (sly-pid) tid) + (skip-chars-forward "^)") + (forward-word 12) + (/ (float (+ (string-to-number (current-word)) + (progn (forward-word) + (string-to-number (current-word))))) + sly--clocks-per-sec))) + (defun sly--threads-add-info (threads) "When TID is present, add running time and CPU usage of THREADS." - (when-let ((columns (car threads)) - (tid-column (seq-position columns :tid))) - (setf columns (nconc columns '(:time :%%cpu))) - (dolist (thread (cdr threads)) - (let-alist (process-attributes (nth tid-column thread)) - (setf thread (nconc thread (list (format-seconds "%Y, %D, %.2h:%z%.2m:%.2s" .etime) (format "%.1f" .pcpu))))))) + (when (eq system-type 'gnu/linux) + (let (cpu-times) + (when-let ((columns (car threads)) + (tid-column (seq-position columns :tid))) + (setf columns (nconc columns '(:time :%%cpu))) + (dolist (thread (cdr threads)) + (let* ((tid (nth tid-column thread)) + (etime (alist-get 'etime (process-attributes tid))) + (cpu-time (sly--thread-cpu-time tid))) + (push (cons tid cpu-time) cpu-times) + (setf thread (nconc thread (list (format-seconds "%Y, %D, %.2h:%z%.2m:%.2s" etime) + (if-let (last-cpu-time (gethash tid sly--threads-last-cpu-time)) + (format "%.1f" (* 100 (/ (- cpu-time last-cpu-time) + (time-to-seconds (time-subtract (current-time) sly--threads-last-update-time))))) + "-")))))) + (setq sly--threads-last-update-time (current-time)) + (clrhash sly--threads-last-cpu-time) + (cl-loop for (tid . time) in cpu-times + do (puthash tid time sly--threads-last-cpu-time))))) threads) (defun sly--display-threads (threads) From 5f9513e9f4b8c89b740b0091126202acee2420e6 Mon Sep 17 00:00:00 2001 From: bo-tato <122528427+bo-tato@users.noreply.github.com> Date: Sun, 20 Aug 2023 07:17:49 +0000 Subject: [PATCH 4/4] remove doom specific macro --- sly.el | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sly.el b/sly.el index 4ddd14ea8..4833715f6 100644 --- a/sly.el +++ b/sly.el @@ -6306,11 +6306,12 @@ was called originally." "Clock ticks per second.") (defun sly--thread-cpu-time (tid) - "Compute CPU usage in seconds for TID from /proc/pid/task/tid/stat" + "Compute CPU usage in seconds for TID from /proc/pid/task/tid/stat." (unless sly--clocks-per-sec (setq sly--clocks-per-sec (string-to-number (shell-command-to-string "getconf CLK_TCK")))) - - (with-file-contents! (format "/proc/%d/task/%d/stat" (sly-pid) tid) + (with-temp-buffer + (insert-file-contents (format "/proc/%d/task/%d/stat" + (sly-pid) tid)) (skip-chars-forward "^)") (forward-word 12) (/ (float (+ (string-to-number (current-word))