]> mj.ucw.cz Git - libucw.git/commitdiff
implemented most of parametrized fastbufs
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Tue, 29 May 2007 12:09:18 +0000 (14:09 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Tue, 29 May 2007 12:09:18 +0000 (14:09 +0200)
lib/fastbuf.h
lib/fb-direct.c
lib/fb-file.c
lib/fb-mmap.c
lib/fb-param.c

index 14b403c742828cc17d8ef60cb29d75405210ae60..e5bba535281434d8a188981258948c549a0d74de 100644 (file)
@@ -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);
index e56d98400332f037dbe08dd042402e1cf0eb2db0..31b12e15598a6d8df10913947517c918dc3b85c0 100644 (file)
 #include <stdio.h>
 
 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;
index cb479f3b55e01d3365adf7607d2e41b23c847da1..1f6dcb2eeab3632f709c04659cbc6dc6a8062af6 100644 (file)
@@ -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;
index 41e65832653c8c16180920d78172bc6221428e63..13087d6e72aba57313c40466d654dff0e09320d8 100644 (file)
@@ -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
index 714ef6471402aa121a1cde4edc8ed6c02a11bf7f..e97e22930910ab880a8645434061d61fca16dbb7 100644 (file)
 
 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 *