Skip to content

Commit 652a7dc

Browse files
authored
Merge pull request #1361 from herbie-fp/codex/output-chrome-/tracing-json-trace
Emit chrome tracing for server jobs
2 parents 55fd399 + e2fc25d commit 652a7dc

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/api/server.rkt

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
(require openssl/sha1)
44
(require (only-in xml write-xexpr))
5+
(require json)
56

67
(require "../syntax/read.rkt"
78
"../syntax/sugar.rkt"
@@ -18,6 +19,7 @@
1819
"../reports/history.rkt"
1920
"../reports/pages.rkt"
2021
"../reports/plot.rkt"
22+
"../config.rkt"
2123
"datafile.rkt"
2224
"sandbox.rkt"
2325
(submod "../utils/timeline.rkt" debug))
@@ -37,6 +39,53 @@
3739
(when false
3840
(apply eprintf msg args)))
3941

42+
;; Tracing support
43+
44+
(define (current-thread-id)
45+
(equal-hash-code (current-thread)))
46+
47+
(define (current-timestamp)
48+
(exact-floor (* 1000 (current-inexact-milliseconds))))
49+
50+
(define (trace-start)
51+
(when (flag-set? 'dump 'trace)
52+
(call-with-output-file
53+
"dump-trace.json"
54+
#:exists 'truncate
55+
(λ (out)
56+
(fprintf out "{\"traceEvents\":[")
57+
(write-json (hash 'name "process_name" 'ph "M" 'ts 0 'pid 0 'tid 0 'args (hash 'name "herbie"))
58+
out)))))
59+
60+
(define (trace name phase [args (hash)])
61+
(when (flag-set? 'dump 'trace)
62+
(call-with-output-file "dump-trace.json"
63+
#:exists 'append
64+
(λ (out)
65+
(fprintf out ",")
66+
(write-json (hash 'name
67+
(~a name)
68+
'ph
69+
(~a phase)
70+
'ts
71+
(current-timestamp)
72+
'pid
73+
0
74+
'tid
75+
(current-thread-id)
76+
'args
77+
args)
78+
out)))))
79+
80+
(define (trace-end)
81+
(when (flag-set? 'dump 'trace)
82+
(call-with-output-file "dump-trace.json" #:exists 'append (λ (out) (fprintf out "]}\n")))))
83+
84+
(define old-exit (exit-handler))
85+
(exit-handler (λ (v)
86+
(trace-end)
87+
(old-exit v)))
88+
4089
;; Job-specific public API
4190
(define (job-path id)
4291
(format "~a.~a" id *herbie-commit*))
@@ -71,6 +120,7 @@
71120
;; Whole-server public methods
72121

73122
(define (server-start threads)
123+
(trace-start)
74124
(cond
75125
[threads
76126
(eprintf "Starting Herbie ~a with ~a workers and seed ~a...\n"
@@ -321,17 +371,19 @@
321371

322372
(define (herbie-do-server-job h-command job-id)
323373
(match-define (herbie-command command test seed pcontext profile? timeline?) h-command)
324-
(log "Started ~a job (~a): ~a\n" command job-id (test-name test))
374+
(define metadata (hash 'job-id job-id 'command (~a command) 'name (test-name test)))
375+
(trace 'herbie 'B metadata)
325376
(define herbie-result
326377
(run-herbie command
327378
test
328379
#:seed seed
329380
#:pcontext pcontext
330381
#:profile? profile?
331382
#:timeline? timeline?))
332-
(log "Completed ~a job (~a), starting reporting\n" command job-id)
383+
(trace 'herbie 'E metadata)
384+
(trace 'to-json 'B metadata)
333385
(define basic-output ((get-json-converter command) herbie-result job-id))
334-
(log "Completed reporting ~a job (~a)\n" command job-id)
386+
(trace 'to-json 'E metadata)
335387
;; Add default fields that all commands have
336388
(hash-set* basic-output
337389
'job

src/config.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
[reduce . (avg-error simplify)]
2424
[rules . (numerics special bools branches)]))
2525

26-
(define debug-flags #hash([generate . (egglog)] [dump . (egg rival egglog)]))
26+
(define debug-flags #hash([generate . (egglog)] [dump . (egg rival egglog trace)]))
2727

2828
(define all-flags (hash-union default-flags deprecated-flags debug-flags #:combine set-union))
2929

0 commit comments

Comments
 (0)