Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show CPU usage and time since thread started to sly-threads view #598

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion sly.el
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -6295,6 +6295,53 @@ 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-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))
(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 (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)
(let* ((inhibit-read-only t)
(old-thread-id (get-text-property (point) 'thread-id))
Expand Down
3 changes: 3 additions & 0 deletions slynk/backend/sbcl.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down