Changeset 11953 in project


Ignore:
Timestamp:
09/15/08 20:29:27 (13 years ago)
Author:
certainty
Message:

parameterize the chunksize.
prepare c-impl of sendfile so that we can distinguish errors from
waits for io so that we can yield control to other threads.

Location:
release/3/sendfile/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/3/sendfile/trunk/sendfile-implementation.c

    r10723 r11953  
    3535  res = sendfile(dst,src,&curoffset,to_send);
    3636 
    37   if(res == -1 && (errno != EAGAIN &&  errno != EINTR))
     37        if(res < 0){
     38                if(errno == EAGAIN || errno == EINTR)
     39      return -2;
    3840    return -1;
    39  
     41        } 
    4042 
    4143#ifdef HAVE_POSIX_FADVISE
     
    6163
    6264  if(sendfile(src,dst,offset,to_send,NULL,&res,0) < 0){
    63     if(errno != EAGAIN)
    64       return -1;
     65    if(errno == EAGAIN)
     66      return -2;
     67    return -1;
    6568  }
    6669
  • release/3/sendfile/trunk/sendfile.scm

    r11825 r11953  
    109109
    110110;;biggest unsigned integer
    111 (define *sendfile:chunk-size* (inexact->exact (- (expt 2 (- %sendfile:word-size 3)) 1)))
    112 
     111;(define *sendfile:chunk-size* (inexact->exact (- (expt 2 (- %sendfile:word-size 3)) 1)))
     112
     113(define (kilobytes what) (* what 1024))
     114(define (megabytes what) (* (kilobytes what) 1024))
     115
     116
     117(define sendfile:chunk-size (make-parameter 512))
    113118
    114119(define (sendfile:madvise buff len behav)
     
    122127;;otherwise we send the difference
    123128(define (sendfile:next-chunk-size len offset)
    124     (if (> (- len offset) *sendfile:chunk-size*)
    125         *sendfile:chunk-size*
     129    (if (> (- len offset) (sendfile:chunk-size))
     130        (sendfile:chunk-size)
    126131        (- len offset)))
    127132
     
    182187  (define sys-write (foreign-lambda integer "write" integer c-pointer unsigned-integer))
    183188
     189  ;;TODO adjust this to send BUFSIZE blocks at once
     190  ;;to minimize the waits
    184191  (define (send-chunk size ptr write-timeout)
    185     (sendfile:madvise ptr size %sendfile:madvise-sequential)
     192    (when (> size (kilobytes 64))
     193        (sendfile:madvise ptr size %sendfile:madvise-willneed))
    186194   
    187195    (let loop ((left size) (work-ptr (pointer-offset ptr 0)))
    188196      (cond
    189        ((= left 0) #t)
     197       ((zero? left) #t)
    190198       (else
    191199        (let ((result (sys-write dst work-ptr left)))
     
    205213            (loop (- left result) (pointer-offset work-ptr result)))))))))
    206214
    207   (let loop ((offset 0.0))
    208     (cond
    209      ((= offset len) len)
    210      (else
    211       (let* ((next-chunk (sendfile:next-chunk-size len offset))
    212              (mem-file (map-file-to-memory #f next-chunk prot/read (bitwise-ior map/shared map/file) src offset))
    213              (write-timeout (sendfile:write-timeout)))
    214         (unless (send-chunk next-chunk (memory-mapped-file-pointer mem-file) write-timeout)
     215 
     216  (parameterize ((sendfile:chunk-size (kilobytes 512)))
     217    (let loop ((offset 0.0))
     218      (cond
     219       ((= offset len) len)
     220       (else
     221        (let* ((next-chunk (sendfile:next-chunk-size len offset))
     222               (mem-file (map-file-to-memory #f next-chunk prot/read map/shared src offset))
     223               (write-timeout (sendfile:write-timeout)))
     224          (unless (send-chunk next-chunk (memory-mapped-file-pointer mem-file) write-timeout)
     225            (unmap-file-from-memory mem-file)
     226            (##sys#update-errno)
     227            (sendfile:error "write failed"))
    215228          (unmap-file-from-memory mem-file)
    216           (##sys#update-errno)
    217           (sendfile:error "write failed"))
    218         (unmap-file-from-memory mem-file)
    219         (loop (+ offset next-chunk)))))))
     229          (loop (+ offset next-chunk))))))))
    220230             
    221231                       
     
    230240  (unless sendfile:os-dep:sendfile-available?
    231241    (sendfile:error "sendfile is not available on this system"))
    232  
    233   (let loop ((offset 0.0))
    234     (cond
    235      ((= offset len)  len)
    236      (else
    237       (let* ((next-chunk (sendfile:next-chunk-size len offset))
    238              (new-offset (%sendfile:sendfile-implementation src dst offset next-chunk)))
    239         (cond
    240          ((negative? new-offset)
    241           (##sys#update-errno)
    242           (sendfile:error "sendfile failed"))
    243          (else
    244           (loop new-offset))))))))
     242  (parameterize ((sendfile:chunk-size (inexact->exact (- (expt 2 (- %sendfile:word-size 3)) 1))))
     243    (let loop ((offset 0.0))
     244      (cond
     245       ((= offset len)  len)
     246       (else
     247        (let* ((next-chunk (sendfile:next-chunk-size len offset))
     248               (new-offset (%sendfile:sendfile-implementation src dst offset next-chunk)))
     249          (cond
     250           ((negative? new-offset)
     251            (##sys#update-errno)
     252            (sendfile:error "sendfile failed"))
     253           (else
     254            (loop new-offset)))))))))
    245255       
    246256
     
    307317(define (sendfile:default-selector len)
    308318  (cond
    309    ((< len 1024) sendfile:read-write-loop/fd)
     319   ((< (megabytes 1)) sendfile:read-write-loop/fd)
    310320   (sendfile:os-dep:sendfile-available? sendfile:sendfile)
    311321   (sendfile:os-dep:mmap-available? sendfile:mmapped)
Note: See TracChangeset for help on using the changeset viewer.