Skip to content

Commit 8701992

Browse files
authored
Merge pull request #1149 from herbie-fp/server-change-order
Change execution order for single-threaded server
2 parents f63971d + 8768e8b commit 8701992

File tree

4 files changed

+38
-42
lines changed

4 files changed

+38
-42
lines changed

infra/test-api.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function getFreePort() {
2020
const PORT = await getFreePort();
2121

2222
console.log("Spawning server on port " + PORT)
23-
const child = spawn('racket', ['-y', 'src/main.rkt', 'web', '--quiet', '--port', ""+PORT]);
23+
const child = spawn('racket', ['-y', 'src/main.rkt', 'web', '--threads', '2', '--quiet', '--port', ""+PORT]);
2424

2525
child.stdout.on('data', (data) => {
2626
console.log(""+data);

src/api/demo.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
(provide run-demo)
3030

31+
(define *demo?* (make-parameter false))
3132
(define *demo-prefix* (make-parameter "/"))
3233
(define *demo-log* (make-parameter false))
3334

src/api/run.rkt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@
6363
(define (merge-profile-jsons ps)
6464
(profile->json (apply profile-merge (map json->profile (dict-values ps)))))
6565

66-
(define (generate-bench-report job-id bench-name test-number dir number-of-test)
67-
(define result (wait-for-job job-id))
66+
(define (generate-bench-report result bench-name test-number dir total-tests)
6867
(define report-path (bench-folder-path bench-name test-number))
6968
(define report-directory (build-path dir report-path))
7069
(unless (directory-exists? report-directory)
@@ -79,7 +78,7 @@
7978
(make-page page out result #t #f)))))
8079

8180
(define table-data (get-table-data-from-hash result report-path))
82-
(print-test-result (+ test-number 1) number-of-test table-data)
81+
(print-test-result (+ test-number 1) total-tests table-data)
8382
table-data)
8483

8584
(define (run-tests tests #:dir dir #:threads threads)
@@ -96,7 +95,7 @@
9695
(for/list ([job-id (in-list job-ids)]
9796
[test (in-list tests)]
9897
[test-number (in-naturals)])
99-
(generate-bench-report job-id (test-name test) test-number dir (length tests))))
98+
(generate-bench-report (wait-for-job job-id) (test-name test) test-number dir (length tests))))
10099

101100
(define info (make-report-info results #:seed seed))
102101
(write-datafile (build-path dir "results.json") info)

src/api/server.rkt

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
(provide make-path
2323
get-improve-table-data
24-
make-improve-result
2524
server-check-on
2625
get-results-for
2726
get-timeline-for
@@ -31,10 +30,8 @@
3130
wait-for-job
3231
start-job-server
3332
write-results-to-disk
34-
*demo?*
3533
*demo-output*)
3634

37-
(define *demo?* (make-parameter false))
3835
(define *demo-output* (make-parameter false))
3936

4037
(define log-level #f)
@@ -118,21 +115,25 @@
118115
(place-channel-put manager (list* msg args))
119116
(match msg
120117
['start
121-
(match-define (list hash-false command job-id) args)
122-
(hash-set! completed-work job-id (herbie-do-server-job command job-id))])))
118+
(match-define (list #f command job-id) args)
119+
(hash-set! queued-jobs job-id command)])))
123120

124121
(define (manager-ask msg . args)
125122
(log "Asking manager: ~a, ~a.\n" msg args)
126123
(if manager
127124
(manager-ask-with-callback msg args)
128125
(match (list* msg args) ; public commands
129-
[(list 'wait hash-false job-id) (hash-ref completed-work job-id)]
130-
[(list 'result job-id) (hash-ref completed-work job-id #f)]
131-
[(list 'timeline job-id) (hash-ref completed-work job-id #f)]
132-
[(list 'check job-id) (and (hash-ref completed-work job-id #f) job-id)]
126+
[(list 'wait hash-false job-id)
127+
(define command (hash-ref queued-jobs job-id))
128+
(define result (herbie-do-server-job command job-id))
129+
(hash-set! completed-jobs job-id result)
130+
result]
131+
[(list 'result job-id) (hash-ref completed-jobs job-id #f)]
132+
[(list 'timeline job-id) (hash-ref completed-jobs job-id #f)]
133+
[(list 'check job-id) (and (hash-ref completed-jobs job-id #f) job-id)]
133134
[(list 'count) (list 0 0)]
134135
[(list 'improve)
135-
(for/list ([(job-id result) (in-hash completed-work)]
136+
(for/list ([(job-id result) (in-hash completed-jobs)]
136137
#:when (equal? (hash-ref result 'command) "improve"))
137138
(get-table-data-from-hash result (make-path job-id)))])))
138139

@@ -161,7 +162,8 @@
161162
'path
162163
(make-path job-id)))
163164

164-
(define completed-work (make-hash))
165+
(define queued-jobs (make-hash))
166+
(define completed-jobs (make-hash))
165167

166168
(define (manager-ask-with-callback msg args)
167169
(define-values (a b) (place-channel))
@@ -214,8 +216,6 @@
214216
(parameterize ([params fresh] ...)
215217
body ...))))]))
216218

217-
(struct work-item (command id))
218-
219219
(define (make-manager worker-count)
220220
(place/context*
221221
ch
@@ -240,39 +240,35 @@
240240
(hash-set! waiting-workers i (make-worker i)))
241241
(log "~a workers ready.\n" (hash-count waiting-workers))
242242
(define waiting (make-hash))
243-
(define job-queue (list))
244243
(log "Manager waiting to assign work.\n")
245244
(for ([i (in-naturals)])
246245
(match (place-channel-get ch)
247246
[(list 'start self command job-id)
248247
; Check if the work has been completed already if not assign the work.
249-
(if (hash-has-key? completed-work job-id)
250-
(place-channel-put self (list 'send job-id (hash-ref completed-work job-id)))
251-
(place-channel-put self (list 'queue self job-id command)))]
252-
[(list 'queue self job-id command)
253-
(set! job-queue (append job-queue (list (work-item command job-id))))
254-
(place-channel-put self (list 'assign self))]
248+
(cond
249+
[(hash-has-key? completed-jobs job-id)
250+
(place-channel-put self (list 'send job-id (hash-ref completed-jobs job-id)))]
251+
[else
252+
(hash-set! queued-jobs job-id command)
253+
(place-channel-put self (list 'assign self))])]
255254
[(list 'assign self)
256255
(define reassigned (make-hash))
257256
(for ([(wid worker) (in-hash waiting-workers)]
258-
[job (in-list job-queue)])
259-
(log "Starting worker [~a] on [~a].\n"
260-
(work-item-id job)
261-
(test-name (herbie-command-test (work-item-command job))))
257+
[(jid command) (in-hash queued-jobs)])
258+
(log "Starting worker [~a] on [~a].\n" jid (test-name (herbie-command-test command)))
262259
; Check if the job is already in progress.
263-
(unless (hash-has-key? current-jobs (work-item-id job))
264-
(hash-set! current-jobs (work-item-id job) wid)
265-
(place-channel-put worker (list 'apply self (work-item-command job) (work-item-id job)))
266-
(hash-set! reassigned wid worker)
260+
(unless (hash-has-key? current-jobs jid)
261+
(place-channel-put worker (list 'apply self command jid))
262+
(hash-set! reassigned wid jid)
267263
(hash-set! busy-workers wid worker)))
268264
; remove X many jobs from the Q and update waiting-workers
269-
(for ([(wid worker) (in-hash reassigned)])
265+
(for ([(wid jid) (in-hash reassigned)])
270266
(hash-remove! waiting-workers wid)
271-
(set! job-queue (cdr job-queue)))]
267+
(hash-remove! queued-jobs jid))]
272268
; Job is finished save work and free worker. Move work to 'send state.
273269
[(list 'finished self wid job-id result)
274270
(log "Job ~a finished, saving result.\n" job-id)
275-
(hash-set! completed-work job-id result)
271+
(hash-set! completed-jobs job-id result)
276272

277273
; move worker to waiting list
278274
(hash-remove! current-jobs job-id)
@@ -286,7 +282,7 @@
286282
(log "Waiting for job: ~a\n" job-id)
287283
; first we add the handler to the wait list.
288284
(hash-update! waiting job-id (curry append (list handler)) '())
289-
(define result (hash-ref completed-work job-id #f))
285+
(define result (hash-ref completed-jobs job-id #f))
290286
; check if the job is completed or not.
291287
(unless (false? result)
292288
(log "Done waiting for job: ~a\n" job-id)
@@ -298,7 +294,7 @@
298294
(place-channel-put handle result))
299295
(hash-remove! waiting job-id)]
300296
; Get the result for the given id, return false if no work found.
301-
[(list 'result handler job-id) (place-channel-put handler (hash-ref completed-work job-id #f))]
297+
[(list 'result handler job-id) (place-channel-put handler (hash-ref completed-jobs job-id #f))]
302298
[(list 'timeline handler job-id)
303299
(define wid (hash-ref current-jobs job-id #f))
304300
(cond
@@ -310,17 +306,17 @@
310306
(place-channel-put handler requested-timeline)]
311307
[else
312308
(log "Job complete, no timeline, send result.\n")
313-
(place-channel-put handler (hash-ref completed-work job-id #f))])]
309+
(place-channel-put handler (hash-ref completed-jobs job-id #f))])]
314310
[(list 'check handler job-id)
315-
(place-channel-put handler (and (hash-has-key? completed-work job-id) job-id))]
311+
(place-channel-put handler (and (hash-has-key? completed-jobs job-id) job-id))]
316312
; Returns the current count of working workers.
317313
[(list 'count handler)
318314
(log "Count requested\n")
319-
(place-channel-put handler (list (hash-count busy-workers) (length job-queue)))]
315+
(place-channel-put handler (list (hash-count busy-workers) (hash-count queued-jobs)))]
320316
; Retreive the improve results for results.json
321317
[(list 'improve handler)
322318
(define improved-list
323-
(for/list ([(job-id result) (in-hash completed-work)]
319+
(for/list ([(job-id result) (in-hash completed-jobs)]
324320
#:when (equal? (hash-ref result 'command) "improve"))
325321
(get-table-data-from-hash result (make-path job-id))))
326322
(place-channel-put handler improved-list)]))))

0 commit comments

Comments
 (0)