From: Pavel Charvat Date: Tue, 29 May 2007 12:09:18 +0000 (+0200) Subject: implemented most of parametrized fastbufs X-Git-Tag: holmes-import~506^2~13^2~118 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=bdcc7c3ea4bbf343c877271501ba25b39df515ce;p=libucw.git implemented most of parametrized fastbufs --- diff --git a/lib/fastbuf.h b/lib/fastbuf.h index 14b403c7..e5bba535 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -73,9 +73,34 @@ struct fastbuf { int can_overwrite_buffer; /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */ }; +/* FastIO on files with run-time parametrization */ + +enum fb_type { + FB_STD, + FB_DIRECT, + FB_MMAP +}; + +struct fb_params { + enum fb_type type; + uns buffer_size; + uns read_ahead; + uns write_back; + struct asio_queue *asio; +}; + +struct cf_section; +extern struct cf_section fbpar_cf; +extern struct fb_params fbpar_def; + +struct fastbuf *bopen_file(byte *name, int mode, struct fb_params *params); +struct fastbuf *bopen_file_try(byte *name, int mode, struct fb_params *params); +struct fastbuf *bopen_tmp_file(struct fb_params *params); +struct fastbuf *bopen_fd(int fd, struct fb_params *params); + /* FastIO on standard files (specify buffer size 0 to enable mmaping) */ -struct fastbuf *bfdopen_internal(int fd, uns buflen, byte *name); +struct fastbuf *bfdopen_internal(int fd, byte *name, uns buflen); struct fastbuf *bopen(byte *name, uns mode, uns buflen); struct fastbuf *bopen_try(byte *name, uns mode, uns buflen); struct fastbuf *bopen_tmp(uns buflen); @@ -93,6 +118,7 @@ struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */ /* FastIO on memory mapped files */ +struct fastbuf *bfmmopen_internal(int fd, byte *name, uns mode); struct fastbuf *bopen_mm(byte *name, uns mode); /* FastIO on files opened with O_DIRECT (see fb-direct.c for description) */ @@ -100,35 +126,12 @@ struct fastbuf *bopen_mm(byte *name, uns mode); extern uns fbdir_cheat; struct asio_queue; -struct fastbuf *fbdir_open_fd_internal(int fd, struct asio_queue *io_queue, byte *name); +struct fastbuf *fbdir_open_fd_internal(int fd, byte *name, struct asio_queue *io_queue, uns buffer_size, uns read_ahead, uns write_back); struct fastbuf *fbdir_open(byte *name, uns mode, struct asio_queue *io_queue); struct fastbuf *fbdir_open_try(byte *name, uns mode, struct asio_queue *io_queue); struct fastbuf *fbdir_open_fd(int fd, struct asio_queue *io_queue); struct fastbuf *fbdir_open_tmp(struct asio_queue *io_queue); -/* FastIO on files with run-time parametrization */ - -enum fb_type { - FB_STD, - FB_DIRECT, - FB_MMAP -}; - -struct fb_params { - enum fb_type type; - uns buffer_size; - struct asio_queue *asio; -}; - -struct cf_section; -extern struct cf_section fbpar_cf; -extern struct fb_params fbpar_def; - -struct fastbuf *bopen_file(byte *name, int mode, struct fb_params *params); -struct fastbuf *bopen_file_try(byte *name, int mode, struct fb_params *params); -struct fastbuf *bopen_tmp_file(struct fb_params *params); -struct fastbuf *bopen_fd(int fd, struct fb_params *params); - /* FastI on file descriptors with limit */ struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit); diff --git a/lib/fb-direct.c b/lib/fb-direct.c index e56d9840..31b12e15 100644 --- a/lib/fb-direct.c +++ b/lib/fb-direct.c @@ -38,16 +38,10 @@ #include uns fbdir_cheat; -static uns fbdir_buffer_size = 65536; -static uns fbdir_read_ahead = 1; -static uns fbdir_write_back = 1; static struct cf_section fbdir_cf = { CF_ITEMS { CF_UNS("Cheat", &fbdir_cheat), - CF_UNS("BufferSize", &fbdir_buffer_size), - CF_UNS("ReadAhead", &fbdir_read_ahead), - CF_UNS("WriteBack", &fbdir_write_back), CF_END } }; @@ -227,15 +221,15 @@ fbdir_seek(struct fastbuf *f, sh_off_t pos, int whence) } static struct asio_queue * -fbdir_get_io_queue(void) +fbdir_get_io_queue(uns buffer_size, uns write_back) { struct ucwlib_context *ctx = ucwlib_thread_context(); struct asio_queue *q = ctx->io_queue; if (!q) { q = xmalloc_zero(sizeof(struct asio_queue)); - q->buffer_size = fbdir_buffer_size; - q->max_writebacks = fbdir_write_back; + q->buffer_size = buffer_size; + q->max_writebacks = write_back; asio_init_queue(q); ctx->io_queue = q; } @@ -296,7 +290,7 @@ fbdir_config(struct fastbuf *f, uns item, int value) } struct fastbuf * -fbdir_open_fd_internal(int fd, struct asio_queue *q, byte *name) +fbdir_open_fd_internal(int fd, byte *name, struct asio_queue *q, uns buffer_size, uns read_ahead UNUSED, uns write_back) { int namelen = strlen(name) + 1; struct fb_direct *F = xmalloc(sizeof(struct fb_direct) + namelen); @@ -310,7 +304,7 @@ fbdir_open_fd_internal(int fd, struct asio_queue *q, byte *name) if (q) F->io_queue = F->user_queue = q; else - F->io_queue = fbdir_get_io_queue(); + F->io_queue = fbdir_get_io_queue(buffer_size, write_back); f->refill = fbdir_refill; f->spout = fbdir_spout; f->seek = fbdir_seek; diff --git a/lib/fb-file.c b/lib/fb-file.c index cb479f3b..1f6dcb2e 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -92,13 +92,13 @@ bfd_config(struct fastbuf *f, uns item, int value) } struct fastbuf * -bfdopen_internal(int fd, uns buflen, byte *name) +bfdopen_internal(int fd, byte *name, uns buflen) { + ASSERT(buflen); int namelen = strlen(name) + 1; struct fb_file *F = xmalloc(sizeof(struct fb_file) + buflen + namelen); struct fastbuf *f = &F->fb; - ASSERT(buflen); bzero(F, sizeof(*F)); f->buffer = (byte *)(F+1); f->bptr = f->bstop = f->buffer; diff --git a/lib/fb-mmap.c b/lib/fb-mmap.c index 41e65832..13087d6e 100644 --- a/lib/fb-mmap.c +++ b/lib/fb-mmap.c @@ -165,7 +165,7 @@ bfmm_config(struct fastbuf *f, uns item, int value) } } -static struct fastbuf * +struct fastbuf * bfmmopen_internal(int fd, byte *name, uns mode) { int namelen = strlen(name) + 1; @@ -192,15 +192,9 @@ bfmmopen_internal(int fd, byte *name, uns mode) struct fastbuf * bopen_mm(byte *name, uns mode) { - int fd; - if ((mode & O_ACCMODE) == O_WRONLY) mode = (mode & ~O_ACCMODE) | O_RDWR; - fd = sh_open(name, mode, 0666); - if (fd < 0) - die("Unable to %s file %s: %m", - (mode & O_CREAT) ? "create" : "open", name); - return bfmmopen_internal(fd, name, mode); + return bopen_file(name, mode, &(struct fb_params){ .type = FB_MMAP }); } #ifdef TEST diff --git a/lib/fb-param.c b/lib/fb-param.c index 714ef647..e97e2293 100644 --- a/lib/fb-param.c +++ b/lib/fb-param.c @@ -17,15 +17,18 @@ struct fb_params fbpar_def = { .buffer_size = 65536, + .read_ahead = 1, + .write_back = 1, }; struct cf_section fbpar_cf = { # define F(x) PTR_TO(struct fb_params, x) CF_TYPE(struct fb_params), CF_ITEMS { - // FIXME CF_LOOKUP("Type", (int *)F(type), ((byte *[]){"std", "direct", "mmap", NULL})), CF_UNS("BufSize", F(buffer_size)), + CF_UNS("ReadAhead", F(read_ahead)), + CF_UNS("WriteBack", F(write_back)), CF_END } # undef F @@ -45,21 +48,29 @@ fbpar_global_init(void) } static struct fastbuf * -bopen_fd_internal(int fd, struct fb_params *params, byte *name) +bopen_fd_internal(int fd, struct fb_params *params, uns mode, byte *name) { + byte buf[32]; + if (!name) + sprintf(name = buf, "fd%d", fd); struct fastbuf *fb; switch (params->type) { case FB_STD: - return bfdopen_internal(fd, params->buffer_size, name); + return bfdopen_internal(fd, name, + params->buffer_size ? : fbpar_def.buffer_size); case FB_DIRECT: - fb = fbdir_open_fd_internal(fd, params->asio, name); - if (!fbdir_cheat && fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_DIRECT) < 0) + fb = fbdir_open_fd_internal(fd, name, params->asio, + params->buffer_size ? : fbpar_def.buffer_size, + params->read_ahead ? : fbpar_def.read_ahead, + params->write_back ? : fbpar_def.write_back); + if (!~mode && !fbdir_cheat && ((int)(mode = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, mode | O_DIRECT)) < 0) log(L_WARN, "Cannot set O_DIRECT on fd %d: %m", fd); return fb; case FB_MMAP: - // FIXME - ASSERT(0); + if (!~mode && (int)(mode = fcntl(fd, F_GETFL)) < 0) + die("Cannot get flags of fd %d: %m", fd); + return bfmmopen_internal(fd, name, mode); } ASSERT(0); } @@ -75,7 +86,7 @@ bopen_file_internal(byte *name, int mode, struct fb_params *params, int try) return NULL; else die("Unable to %s file %s: %m", (mode & O_CREAT) ? "create" : "open", name); - struct fastbuf *fb = bopen_fd_internal(fd, params, name); + struct fastbuf *fb = bopen_fd_internal(fd, params, mode, name); ASSERT(fb); if (mode & O_APPEND) bseek(fb, 0, SEEK_END); @@ -97,9 +108,7 @@ bopen_file_try(byte *name, int mode, struct fb_params *params) struct fastbuf * bopen_fd(int fd, struct fb_params *params) { - byte x[32]; - sprintf(x, "fd%d", fd); - return bopen_fd_internal(fd, params ? : &fbpar_def, x); + return bopen_fd_internal(fd, params ? : &fbpar_def, ~0U, NULL); } struct fastbuf *