scripts: Set thread names.

This allows 'guix publish' threads as well as 'guix substitute' and
'guix offload' processes to be properly labeled in 'top', 'pstree', etc.

* guix/workers.scm (worker-thunk): Add #:thread-name parameter and honor it.
(make-pool): Likewise.
* guix/scripts/publish.scm (http-write): Add calls to 'set-thread-name'
in bodies of 'call-with-new-thread'.
(guix-publish): Call 'set-thread-name'.   Pass #:thread-name to 'make-pool'.
* guix/scripts/offload.scm (guix-offload): Call 'set-thread-name'.
* guix/scripts/substitute.scm (guix-substitute): Likewise.
master
Ludovic Courtès 2017-05-28 16:09:32 +02:00
parent aa401f9ba6
commit 8902d0f267
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5
4 changed files with 31 additions and 6 deletions

View File

@ -34,7 +34,8 @@
#:select (nar-error? nar-error-file)) #:select (nar-error? nar-error-file))
#:use-module (guix nar) #:use-module (guix nar)
#:use-module (guix utils) #:use-module (guix utils)
#:use-module ((guix build syscalls) #:select (fcntl-flock)) #:use-module ((guix build syscalls)
#:select (fcntl-flock set-thread-name))
#:use-module ((guix build utils) #:select (which mkdir-p)) #:use-module ((guix build utils) #:select (which mkdir-p))
#:use-module (guix ui) #:use-module (guix ui)
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
@ -641,6 +642,7 @@ machine."
(let ((max-silent-time (string->number max-silent-time)) (let ((max-silent-time (string->number max-silent-time))
(build-timeout (string->number build-timeout)) (build-timeout (string->number build-timeout))
(print-build-trace? (string=? print-build-trace? "1"))) (print-build-trace? (string=? print-build-trace? "1")))
(set-thread-name "guix offload")
(parameterize ((%current-system system)) (parameterize ((%current-system system))
(let loop ((line (read-line))) (let loop ((line (read-line)))
(unless (eof-object? line) (unless (eof-object? line)

View File

@ -58,6 +58,7 @@
#:select (with-atomic-file-output compressed-file?)) #:select (with-atomic-file-output compressed-file?))
#:use-module ((guix build utils) #:use-module ((guix build utils)
#:select (dump-port mkdir-p find-files)) #:select (dump-port mkdir-p find-files))
#:use-module ((guix build syscalls) #:select (set-thread-name))
#:export (%public-key #:export (%public-key
%private-key %private-key
@ -649,6 +650,7 @@ blocking."
;; thread so that the main thread can keep working in the meantime. ;; thread so that the main thread can keep working in the meantime.
(call-with-new-thread (call-with-new-thread
(lambda () (lambda ()
(set-thread-name "publish nar")
(let* ((response (write-response (sans-content-length response) (let* ((response (write-response (sans-content-length response)
client)) client))
(port (begin (port (begin
@ -670,6 +672,7 @@ blocking."
;; Send a raw file in a separate thread. ;; Send a raw file in a separate thread.
(call-with-new-thread (call-with-new-thread
(lambda () (lambda ()
(set-thread-name "publish file")
(catch 'system-error (catch 'system-error
(lambda () (lambda ()
(call-with-input-file (utf8->string body) (call-with-input-file (utf8->string body)
@ -858,10 +861,16 @@ consider using the '--user' option!~%")))
(sockaddr:port address)) (sockaddr:port address))
(when repl-port (when repl-port
(repl:spawn-server (repl:make-tcp-server-socket #:port repl-port))) (repl:spawn-server (repl:make-tcp-server-socket #:port repl-port)))
;; Set the name of the main thread.
(set-thread-name "guix publish")
(with-store store (with-store store
(run-publish-server socket store (run-publish-server socket store
#:cache cache #:cache cache
#:pool (and cache (make-pool workers)) #:pool (and cache (make-pool workers
#:thread-name
"publish worker"))
#:nar-path nar-path #:nar-path nar-path
#:compression compression #:compression compression
#:narinfo-ttl ttl)))))) #:narinfo-ttl ttl))))))

View File

@ -39,6 +39,8 @@
. guix:open-connection-for-uri) . guix:open-connection-for-uri)
close-connection close-connection
store-path-abbreviation byte-count->string)) store-path-abbreviation byte-count->string))
#:use-module ((guix build syscalls)
#:select (set-thread-name))
#:use-module (ice-9 rdelim) #:use-module (ice-9 rdelim)
#:use-module (ice-9 regex) #:use-module (ice-9 regex)
#:use-module (ice-9 match) #:use-module (ice-9 match)
@ -1015,6 +1017,8 @@ default value."
(#f #f) (#f #f)
(locale (false-if-exception (setlocale LC_ALL locale)))) (locale (false-if-exception (setlocale LC_ALL locale))))
(set-thread-name "guix substitute")
(with-networking (with-networking
(with-error-handling ; for signature errors (with-error-handling ; for signature errors
(match args (match args

View File

@ -23,6 +23,7 @@
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
#:use-module (srfi srfi-9) #:use-module (srfi srfi-9)
#:use-module (srfi srfi-26) #:use-module (srfi srfi-26)
#:use-module ((guix build syscalls) #:select (set-thread-name))
#:export (pool? #:export (pool?
make-pool make-pool
pool-enqueue! pool-enqueue!
@ -60,7 +61,8 @@
(lambda () (lambda ()
(lock-mutex mutex)))) (lock-mutex mutex))))
(define (worker-thunk mutex condvar pop-queue) (define* (worker-thunk mutex condvar pop-queue
#:key (thread-name "guix worker"))
"Return the thunk executed by worker threads." "Return the thunk executed by worker threads."
(define (loop) (define (loop)
(match (pop-queue) (match (pop-queue)
@ -80,11 +82,18 @@
(loop)) (loop))
(lambda () (lambda ()
(catch 'system-error
(lambda ()
(set-thread-name thread-name))
(const #f))
(with-mutex mutex (with-mutex mutex
(loop)))) (loop))))
(define* (make-pool #:optional (count (current-processor-count))) (define* (make-pool #:optional (count (current-processor-count))
"Return a pool of COUNT workers." #:key (thread-name "guix worker"))
"Return a pool of COUNT workers. Use THREAD-NAME as the name of these
threads as reported by the operating system."
(let* ((mutex (make-mutex)) (let* ((mutex (make-mutex))
(condvar (make-condition-variable)) (condvar (make-condition-variable))
(queue (make-q)) (queue (make-q))
@ -93,7 +102,8 @@
(worker-thunk mutex condvar (worker-thunk mutex condvar
(lambda () (lambda ()
(and (not (q-empty? queue)) (and (not (q-empty? queue))
(q-pop! queue))))) (q-pop! queue)))
#:thread-name thread-name))
1+ 1+
0)) 0))
(threads (map (lambda (proc) (threads (map (lambda (proc)