2017-09-07 20:10:25 +05:30
|
|
|
(library
|
|
|
|
;; We can't call this (bcache bcache) because it'll clash with the C lib
|
|
|
|
(bcache block-manager)
|
|
|
|
(export with-bcache
|
|
|
|
get-nr-blocks
|
|
|
|
get-nr-locked
|
|
|
|
get-block
|
|
|
|
block-data
|
|
|
|
block-index
|
|
|
|
release-block
|
|
|
|
flush-cache
|
|
|
|
get-flags
|
|
|
|
prefetch-block
|
2017-09-14 16:11:26 +05:30
|
|
|
with-block
|
|
|
|
spine
|
|
|
|
spine-exit
|
|
|
|
spine-step
|
|
|
|
spine-current
|
|
|
|
spine-parent
|
2017-10-04 14:55:32 +05:30
|
|
|
with-spine
|
|
|
|
checksum-block)
|
2017-09-14 16:11:26 +05:30
|
|
|
|
2017-09-07 20:10:25 +05:30
|
|
|
(import (chezscheme)
|
2017-10-04 14:55:32 +05:30
|
|
|
(crc32c checksum)
|
2017-09-07 20:10:25 +05:30
|
|
|
(fmt fmt)
|
2017-09-14 16:11:26 +05:30
|
|
|
(srfi s8 receive)
|
2017-09-07 20:10:25 +05:30
|
|
|
(utils))
|
|
|
|
|
2017-10-10 14:58:56 +05:30
|
|
|
(define __ (load-shared-object "../lib/libft.so"))
|
2017-09-07 20:10:25 +05:30
|
|
|
|
2018-01-25 21:18:56 +05:30
|
|
|
(define-record-type bcache (fields ptr))
|
|
|
|
|
|
|
|
(define (wrap ptr) (make-bcache ptr))
|
|
|
|
(define (unwrap c) (bcache-ptr c))
|
|
|
|
|
2017-09-07 20:10:25 +05:30
|
|
|
(define bcache-simple
|
2018-01-25 21:18:56 +05:30
|
|
|
(let ((fn (foreign-procedure "bcache_simple" (string unsigned) ptr)))
|
|
|
|
(lambda (path mem)
|
|
|
|
(wrap (fn path mem)))))
|
2017-09-07 20:10:25 +05:30
|
|
|
|
|
|
|
(define bcache-destroy
|
2018-01-25 21:18:56 +05:30
|
|
|
(let ((fn (foreign-procedure "bcache_destroy" (ptr) void)))
|
|
|
|
(lambda (c)
|
|
|
|
(fn (unwrap c)))))
|
2017-09-07 20:10:25 +05:30
|
|
|
|
|
|
|
(define-syntax with-bcache
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ (name path nr-cache-blocks) b1 b2 ...)
|
|
|
|
(let ((name (bcache-simple path nr-cache-blocks)))
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda () #f)
|
|
|
|
(lambda () b1 b2 ...)
|
|
|
|
(lambda () (bcache-destroy name)))))))
|
|
|
|
|
|
|
|
(define get-nr-blocks
|
2018-01-25 21:18:56 +05:30
|
|
|
(let ((fn (foreign-procedure "get_nr_blocks" (ptr) unsigned-64)))
|
|
|
|
(lambda (c)
|
|
|
|
(fn (unwrap c)))))
|
2017-09-07 20:10:25 +05:30
|
|
|
|
|
|
|
(define get-nr-locked
|
2018-01-25 21:18:56 +05:30
|
|
|
(let ((fn (foreign-procedure "get_nr_locked" (ptr) unsigned-64)))
|
|
|
|
(lambda (c)
|
|
|
|
(fn (unwrap c)))))
|
2017-09-07 20:10:25 +05:30
|
|
|
|
|
|
|
(define-enumeration get-flag-element
|
|
|
|
(zero dirty barrier) get-flags)
|
|
|
|
|
|
|
|
(define (build-flags es)
|
|
|
|
(define (to-bits e)
|
|
|
|
(case e
|
|
|
|
((zero) 1)
|
|
|
|
((dirty) 2)
|
|
|
|
((barrier) 4)))
|
|
|
|
|
|
|
|
(define (combine fs e)
|
|
|
|
(fxior fs (to-bits e)))
|
|
|
|
|
|
|
|
(fold-left combine 0 (enum-set->list es)))
|
|
|
|
|
|
|
|
(define (fail msg)
|
|
|
|
(raise
|
|
|
|
(condition
|
|
|
|
(make-error)
|
|
|
|
(make-message-condition msg))))
|
|
|
|
|
|
|
|
(define-ftype Block
|
|
|
|
(struct
|
|
|
|
(data void*)
|
|
|
|
(index unsigned-64)))
|
|
|
|
|
|
|
|
(define (block-data b)
|
|
|
|
(ftype-ref Block (data) b))
|
|
|
|
|
|
|
|
(define (block-index b)
|
|
|
|
(ftype-ref Block (index) b))
|
|
|
|
|
|
|
|
(define (get-block cache index flags)
|
|
|
|
(define getb (foreign-procedure "get_block" (ptr unsigned-64 unsigned) (* Block)))
|
|
|
|
|
2018-01-25 21:18:56 +05:30
|
|
|
(let ((b (getb (unwrap cache) index (build-flags flags))))
|
2017-09-07 20:10:25 +05:30
|
|
|
(if (ftype-pointer-null? b)
|
|
|
|
(fail (fmt #f "unable to get block " index))
|
|
|
|
b)))
|
|
|
|
|
|
|
|
(define release-block
|
|
|
|
(foreign-procedure "release_block" ((* Block)) void))
|
|
|
|
|
|
|
|
(define (flush-cache cache)
|
|
|
|
(define flush (foreign-procedure "flush_cache" (ptr) int))
|
|
|
|
|
2018-01-25 21:18:56 +05:30
|
|
|
(let ((r (flush (unwrap cache))))
|
2017-09-07 20:10:25 +05:30
|
|
|
(when (< 0 r)
|
|
|
|
(fail "flush_cache failed"))))
|
|
|
|
|
|
|
|
(define prefetch-block
|
2018-01-25 21:18:56 +05:30
|
|
|
(let ((fn (foreign-procedure "prefetch_block" (ptr unsigned-64) void)))
|
|
|
|
(lambda (c b)
|
|
|
|
(fn (unwrap c) b))))
|
2017-09-07 20:10:25 +05:30
|
|
|
|
|
|
|
(define-syntax with-block
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ (b cache index flags) b1 b2 ...)
|
|
|
|
(let ((b (get-block cache index flags)))
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda () #f)
|
|
|
|
(lambda () b1 b2 ...)
|
|
|
|
(lambda () (release-block b)))))))
|
2017-09-14 16:11:26 +05:30
|
|
|
|
|
|
|
;;;--------------------------------------------------------
|
|
|
|
;;; Spine
|
|
|
|
;;;--------------------------------------------------------
|
|
|
|
(define-record-type spine
|
2017-09-14 20:39:43 +05:30
|
|
|
(fields (immutable cache) (mutable max) (mutable entries))
|
2017-09-14 16:11:26 +05:30
|
|
|
(protocol
|
|
|
|
(lambda (new)
|
2017-09-14 20:39:43 +05:30
|
|
|
(lambda (cache max)
|
|
|
|
(new cache max '())))))
|
2017-09-14 16:11:26 +05:30
|
|
|
|
|
|
|
(define (spine-exit sp)
|
|
|
|
(for-each release-block (spine-entries sp)))
|
|
|
|
|
|
|
|
(define (pop-last xs)
|
|
|
|
(let ((rs (reverse xs)))
|
|
|
|
(values (car xs) (reverse (cdr xs)))))
|
|
|
|
|
2017-09-14 20:39:43 +05:30
|
|
|
(define (spine-step% sp index flags)
|
|
|
|
(let ((b (get-block (spine-cache sp) index flags)))
|
|
|
|
(if (> (length (spine-entries sp))
|
|
|
|
(spine-max sp))
|
|
|
|
(receive (oldest-b es) (pop-last (spine-entries sp))
|
|
|
|
(release-block oldest-b)
|
|
|
|
(spine-entries-set! sp (cons b es)))
|
|
|
|
(spine-entries-set! sp (cons b (spine-entries sp))))))
|
2017-09-14 16:11:26 +05:30
|
|
|
|
|
|
|
(define (spine-current sp)
|
|
|
|
(car (spine-entries sp)))
|
|
|
|
|
|
|
|
(define (spine-parent sp)
|
|
|
|
(cadr (spine-entries sp)))
|
|
|
|
|
|
|
|
(define-syntax with-spine
|
|
|
|
(syntax-rules ()
|
2017-09-14 20:39:43 +05:30
|
|
|
((_ (sp cache max) b1 b2 ...)
|
|
|
|
(let ((sp (make-spine cache max)))
|
2017-09-14 16:11:26 +05:30
|
|
|
(dynamic-wind
|
|
|
|
(lambda () #f)
|
|
|
|
(lambda () b1 b2 ...)
|
|
|
|
(lambda () (spine-exit sp)))))))
|
|
|
|
|
|
|
|
(define-syntax spine-step
|
|
|
|
(syntax-rules ()
|
2017-09-14 20:39:43 +05:30
|
|
|
((_ sp (b index flags) b1 b2 ...)
|
2017-09-14 16:11:26 +05:30
|
|
|
(begin
|
2017-09-14 20:39:43 +05:30
|
|
|
(spine-step% sp index flags)
|
2017-09-14 16:11:26 +05:30
|
|
|
(let ((b (spine-current sp)))
|
|
|
|
b1 b2 ...)))))
|
2017-10-04 14:55:32 +05:30
|
|
|
|
|
|
|
;;;--------------------------------------------------------
|
|
|
|
;;; Checksumming
|
|
|
|
;;;--------------------------------------------------------
|
|
|
|
(define md-block-size 4096)
|
|
|
|
|
|
|
|
(define (checksum-block b offset salt)
|
|
|
|
(let ((ptr (make-ftype-pointer unsigned-8
|
|
|
|
(+ (block-data b) offset))))
|
|
|
|
(bitwise-xor salt (crc32c #xffffffff ptr (- md-block-size offset)))))
|
2017-09-07 20:10:25 +05:30
|
|
|
)
|