;;; bench-gc-adaptive.lsp — driver that runs ONE workload phase in
;;; this process. The outer wrapper (tests/bench-gc-adaptive.sh)
;;; launches asm-gc six times, one per (workload × mode) pair, each
;;; with a clean interpreter. One-phase-per-process isolates state
;;; across runs; cross-phase heap residue on a 1 MB chunk has caused
;;; spurious escape reports in the meta-GC that we haven't nailed
;;; yet, and we'd rather report clean numbers than fight a latent
;;; interaction mid-bench. The MODE and WORKLOAD are injected by
;;; the wrapper via pre-bindings of *mode* and *workload*.

(define K 200)
(define N 1000)

(define (build-list k acc)
  (if (= k 0) acc (build-list (- k 1) (cons k acc))))
(define (sum-list lst acc)
  (if (null? lst) acc (sum-list (cdr lst) (+ acc (car lst)))))

(define (friendly-thunk) (sum-list (build-list K '()) 0))
(define escaped-sink '())
(define (hostile-step)
  (set! escaped-sink (with-arena (lambda () (build-list K '())))))

(define (friendly-go n)
  (if (= n 0) 'done
      (begin (with-arena friendly-thunk) (friendly-go (- n 1)))))
(define (hostile-go n)
  (if (= n 0) 'done
      (begin (hostile-step) (hostile-go (- n 1)))))
(define (mixed-go n)
  (if (= n 0) 'done
      (begin
        (with-arena friendly-thunk)
        (hostile-step)
        (mixed-go (- n 1)))))

(arena-set-mode *mode*)

(define t0 (current-time-ms))
(cond
  ((= *workload* 0) (friendly-go N))
  ((= *workload* 1) (hostile-go N))
  (else             (mixed-go N)))
(define elapsed (- (current-time-ms) t0))

(display "workload=") (display *workload*)
(display " mode=") (display *mode*)
(display " N=") (display N) (newline)
(display "time_ms=") (display elapsed) (newline)
(display "arena-stats=") (display (arena-stats)) (newline)
(display "gc-stats=") (display (gc-stats)) (newline)
