2

Python, via prctl (https://pypi.python.org/pypi/python-prctl) allows one to set the name of a thread such that you can see it in the program 'htop'. How would one do this in SBCL? BT:MAKE-THREAD does not expose an interface, and SBCL doesn't seem to either. The function SB-THREAD::MAKE-THREAD creates a defstruct defined in sbcl/src/code/thread.lisp, which has no code related to this afaict).

See also: Python thread name doesn't show up on ps or htop

Community
  • 1
  • 1
Gabriel Laddel
  • 147
  • 3
  • 9

2 Answers2

1

Do it with an old version of OSICAT-POSIX. Try

(setf (osicat:process-name) "phuctor")

This will set the current thread name.

Gabriel Laddel
  • 147
  • 3
  • 9
1

SBCL bug "thread name visible in htop" about this with proposed solution from Tomas Hlavaty that does not require SBCL patches:

(defun set-native-thread-name (thread name)
  #+(and sb-thread linux)
  (when (and (stringp name) (not (equal "" name)))
    (let ((n (with-output-to-string (s)
               (dotimes (i (min (length name) 15))
                 (let ((c (char name i)))
                   (if (<= 32 (char-code c) 126)
                       (write-char c s)
                       (return-from set-native-thread-name)))))))
      (with-alien ((fn (function integer unsigned c-string)
                       :extern "pthread_setname_np"))
        (values (alien-funcall fn
                               (sb-thread::thread-os-thread thread)
                               n))))))

(defun update-native-thread-names ()
  #+sb-thread
  (dolist (x (sb-thread:list-all-threads))
    (set-native-thread-name x (sb-thread:thread-name x))))

(defun set-thread-name (name &optional thread)
  #+sb-thread
  (let ((thread (or thread sb-thread:*current-thread*)))
    (setf (sb-thread:thread-name thread) name)
    (set-native-thread-name thread name)))

;; example
(sb-thread:make-thread
 (lambda ()
   (set-thread-name "ahoj")
   (sleep 10)
   ;; see the thread name in htop
   (print :done)
   (finish-output)))
Traut
  • 339
  • 2
  • 8