/usr/share/racket/collects/setup/parallel-build.rkt is in racket-common 6.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | #lang racket/base
(require compiler/cm
racket/list
racket/match
racket/path
racket/fasl
racket/serialize
"private/cc-struct.rkt"
setup/parallel-do
racket/class
racket/future
compiler/find-exe
racket/place
syntax/modresolve
(for-syntax racket/base))
(provide parallel-compile
parallel-compile-files)
(define-syntax-rule (DEBUG_COMM a ...)
(void)
; (begin a ...)
)
(define lock-manager%
(class object%
(field (locks (make-hash)))
(define depends (make-hash))
(define/public (lock fn wrkr)
(let ([v (hash-ref locks fn #f)])
(hash-set! locks fn
(if v
(match v [(list w waitlst)
(hash-set! depends wrkr (cons w fn))
(check-cycles wrkr (hash) null)
(list w (append waitlst (list wrkr)))])
(begin
(wrkr/send wrkr (list 'locked))
(list wrkr null))))
(not v)))
(define/public (unlock fn)
(match (hash-ref locks fn)
[(list w waitlst)
(for ([x (second (hash-ref locks fn))])
(hash-remove! depends x)
(wrkr/send x (list 'compiled)))
(hash-remove! locks fn)]))
(define/private (check-cycles w seen fns)
(cond
[(hash-ref seen w #f)
(error 'setup "dependency cycle: ~s"
(cons (car fns) (reverse fns)))]
[(hash-ref depends w #f)
=> (lambda (d)
(check-cycles (car d) (hash-set seen w #t) (cons (cdr d) fns)))]
[else (void)]))
(super-new)))
(define/class/generics lock-manager%
(lm/lock lock fn wrkr)
(lm/unlock unlock fn))
(define (->bytes x)
(cond [(path? x) (path->bytes x)]
[(string? x) (string->bytes/locale x)]
[(equal? x 'relative) (->bytes (path->complete-path (current-directory)))]
[else (raise-argument-error '->bytes "(or/c path? string? 'relative)" x)]))
(define (dir->bytes x)
(cond [(path? x) (path->bytes (path->complete-path x))]
[else (->bytes x)]))
(define collects-queue%
(class* object% (work-queue<%>)
(init-field cclst printer append-error options)
(field (lock-mgr (new lock-manager%)))
(field (hash (make-hash)))
(inspect #f)
(define/public (work-done work wrkr msg)
(match (list work msg)
[(list (list cc file last) (list result-type out err))
(begin0
(match result-type
[(list 'ERROR msg)
(append-error cc "making" (exn msg (current-continuation-marks)) out err "error")
#t]
[(list 'LOCK fn) (lm/lock lock-mgr fn wrkr) #f]
[(list 'UNLOCK fn) (lm/unlock lock-mgr fn) #f]
['DONE
(define (string-!empty? s) (not (zero? (string-length s))))
(when (ormap string-!empty? (list out err))
(append-error cc "making" null out err "output"))
;(when last (printer (current-output-port) "made" "~a" (cc-name cc)))
#t]
[else (eprintf "Failed trying to match:\n~e\n" result-type)]))]
[(list _ (list 'ADD fn))
;; Currently ignoring queued individual files
#f]
[else
(match work
[(list-rest (list cc file last) message)
(append-error cc "making" null "" "" "error")
(eprintf "work-done match cc failed.\n")
(eprintf "trying to match:\n~e\n" (list work msg))
#t]
[else
(eprintf "work-done match cc failed.\n")
(eprintf "trying to match:\n~e\n" (list work msg))
(eprintf "FATAL\n")
(exit 1)])]))
;; assigns a collection to each worker to be compiled
;; when it runs out of collections, steals work from other workers collections
(define/public (get-job workerid)
(define (say-making id x)
(unless (null? x)
(printer (current-output-port)
(format "~a making" id)
"~a"
(cc-name (car (car x))))))
(define (find-job-in-cc cc id)
(define (retry) (get-job workerid))
(define (build-job cc file last)
(values
(list cc file last)
(list (->bytes (cc-name cc))
(dir->bytes (cc-path cc))
(->bytes file)
options)))
(match cc
[(list)
(hash-remove! hash id) (retry)]
[(list (list cc (list) (list))) ;empty collect
(hash-remove! hash id) (retry)]
[(cons (list cc (list) (list)) tail) ;empty parent collect
(say-making id tail)
(hash-set! hash id tail) (retry)]
[(cons (list cc (list) subs) tail) ;empty srcs list
(define nl (append subs tail))
(say-making id nl)
(hash-set! hash id nl) (retry)]
[(cons (list cc (list file) subs) tail)
(define nl (append subs tail))
(hash-set! hash id nl)
(say-making id nl)
(build-job cc file #t)]
[(cons (list cc (cons file ft) subs) tail)
(hash-set! hash id (cons (list cc ft subs) tail))
(build-job cc file #f)]
[else
(eprintf "get-job match cc failed.\n")
(eprintf "trying to match:\n~v\n" cc)]))
; find a cc
(cond
; lookup already assigned cc
[(hash-ref hash workerid #f) => (lambda (x)
(find-job-in-cc x workerid))]
; get next cc from cclst
[(pair? cclst)
(define workercc (list (car cclst)))
(say-making workerid workercc)
(set! cclst (cdr cclst))
(hash-set! hash workerid workercc)
(find-job-in-cc workercc workerid)]
; try to steal work from another workers cc
[(hash-iterate-first hash) => (lambda (x)
(find-job-in-cc (hash-iterate-value hash x)
(hash-iterate-key hash x)))]))
; no work left
; should never get here, get-job only called when the queue has work
(define/public (has-jobs?)
(define (hasjob? cct)
(let loop ([cct cct])
(ormap (lambda (x) (or ((length (second x)) . > . 0) (loop (third x)))) cct)))
(or (hasjob? cclst)
(for/or ([cct (in-hash-values hash)])
(hasjob? cct))))
(define/public (jobs-cnt)
(define (count-cct cct)
(let loop ([cct cct])
(apply + (map (lambda (x) (+ (length (second x)) (loop (third x)))) cct))))
(+ (count-cct cclst)
(for/fold ([cnt 0]) ([cct (in-hash-values hash)])
(+ cnt (count-cct cct)))))
(define/public (get-results) (void))
(super-new)))
(define file-list-queue%
(class* object% (work-queue<%>)
(init-field filelist handler options)
(field (lock-mgr (new lock-manager%)))
(field [results (void)])
(inspect #f)
(define seen
(for/hash ([k (in-list filelist)])
(values k #t)))
(define/public (work-done work wrkr msg)
(define id (send wrkr get-id))
(match msg
[(list result-type out err)
(match result-type
[(list 'LOCK fn) (lm/lock lock-mgr fn wrkr) #f]
[(list 'UNLOCK fn) (lm/unlock lock-mgr fn) #f]
[(list 'ERROR msg) (handler id 'error work msg out err)
(set! results #f)
#t]
['DONE
(define (string-!empty? s) (not (zero? (string-length s))))
(if (ormap string-!empty? (list out err))
(handler id 'output work "" out err)
(handler id 'done work "" "" ""))
#t])]
[(list 'ADD fn)
(unless (hash-ref seen fn #f)
(set! filelist (cons fn filelist))
(set! seen (hash-set seen fn #t)))
#f]
[else
(handler id 'fatalerror (format "Error matching work: ~a queue ~a" work filelist) "" "") #t]))
(define/public (get-job workerid)
(match filelist
[(cons hd tail)
(define-values (dir file b) (split-path hd))
(set! filelist tail)
(handler workerid 'start hd "" "" "")
(values hd (list (->bytes hd) (dir->bytes dir) (->bytes file) null))]
[(list) null]))
(define/public (has-jobs?) (not (null? filelist)))
(define/public (jobs-cnt) (length filelist))
(define/public (get-results) results)
(super-new)))
(define (parallel-build work-queue worker-count)
(parallel-do
worker-count
(lambda (workerid) (list workerid))
work-queue
(define-worker (parallel-compile-worker worker-id)
(DEBUG_COMM (eprintf "WORKER ~a\n" worker-id))
(define prev-uncaught-exception-handler (uncaught-exception-handler))
(uncaught-exception-handler
(lambda (x)
(when (exn:break? x) (exit 1))
(prev-uncaught-exception-handler x)))
(define cmc (make-caching-managed-compile-zo))
(match-message-loop
[(list name _dir _file options)
(DEBUG_COMM (eprintf "COMPILING ~a ~a ~a ~a\n" worker-id name _file _dir))
(define dir (bytes->path _dir))
(define file (bytes->path _file))
(define out-str-port (open-output-string))
(define err-str-port (open-output-string))
(define cip (current-input-port))
(define cop (current-output-port))
(define cep (current-error-port))
(define (send/recv msg) (send/msg msg) (recv/req))
(define (send/add fn) (send/msg (list 'ADD fn)))
(define (send/resp type)
(send/msg (list type (get-output-string out-str-port) (get-output-string err-str-port))))
(define (pp x) (fprintf cep "COMPILING ~a ~a ~a ~a\n" worker-id name file x))
(define (lock-client cmd fn)
(match cmd
['lock
(DEBUG_COMM (eprintf "REQUESTING LOCK ~a ~a ~a ~a\n" worker-id name _file _dir))
(match (send/recv (list (list 'LOCK (path->bytes fn)) "" ""))
[(list 'locked) #t]
[(list 'compiled) #f]
[(list 'DIE) (worker/die 1)]
[x (send/error (format "DIDNT MATCH B ~v\n" x))]
[else (send/error (format "DIDNT MATCH B\n"))])]
['unlock
(DEBUG_COMM (eprintf "UNLOCKING ~a ~a ~a ~a\n" worker-id name _file _dir))
(send/msg (list (list 'UNLOCK (path->bytes fn)) "" ""))]
[x (send/error (format "DIDNT MATCH C ~v\n" x))]
[else (send/error (format "DIDNT MATCH C\n"))]))
(with-handlers ([exn:fail? (lambda (x)
(define sp (open-output-string))
(parameterize ([current-error-port sp])
((error-display-handler) (exn-message x) x))
(send/resp (list 'ERROR (get-output-string sp))))])
(parameterize ([parallel-lock-client lock-client]
[compile-context-preservation-enabled (member 'disable-inlining options )]
[manager-trace-handler
(lambda (p)
(when (member 'very-verbose options)
(printf " ~a\n" p)))]
[current-namespace (make-base-empty-namespace)]
[current-directory dir]
[current-load-relative-directory dir]
[current-input-port (open-input-string "")]
[current-output-port out-str-port]
[current-error-port err-str-port]
;[manager-compile-notify-handler pp]
)
;; Watch for module-prefetch events, and queue jobs in response
(define t (start-prefetch-thread send/add))
(cmc (build-path dir file))
(kill-thread t))
(send/resp 'DONE))]
[x (send/error (format "DIDNT MATCH A ~v\n" x))]
[else (send/error (format "DIDNT MATCH A\n"))]))))
(define (parallel-compile-files list-of-files
#:worker-count [worker-count (processor-count)]
#:handler [handler void]
#:options [options '()])
(parallel-build (make-object file-list-queue% list-of-files handler options) worker-count))
(define (parallel-compile worker-count setup-fprintf append-error collects-tree)
(setup-fprintf (current-output-port) #f "--- parallel build using ~a jobs ---" worker-count)
(define collects-queue (make-object collects-queue% collects-tree setup-fprintf append-error '()))
(parallel-build collects-queue worker-count))
(define (start-prefetch-thread send/add)
(define pf (make-log-receiver (current-logger) 'info 'module-prefetch))
(thread
(lambda ()
(let loop ()
(let ([v (sync pf)])
(define l (vector-ref v 2))
(when (and (list? l)
(= 2 (length l))
(list? (car l))
(path? (cadr l))
(andmap module-path? (car l)))
(define dir (cadr l))
(define (quote? p) (and (pair? p) (eq? (car p) 'quote)))
(define (submod? p) (and (pair? p) (eq? (car p) 'submod)))
;; Add prefetch modules to work queue --- but skip the first one,
;; because it's going to be compiled immediately, anyway:
(for/fold ([prev #f]) ([p (in-list (reverse (car l)))])
(cond
[(or (quote? p)
(and (submod? p) (quote? (cadr p))))
;; skip `quote` module paths
prev]
[else
(when prev
(define path
(let loop ([prev prev])
(cond
[(submod? prev)
(loop (cadr prev))]
[else (resolve-module-path prev (build-path dir "dummy.rkt"))])))
(when (path? path)
(send/add path)))
p])))
(loop))))))
|