modules/vfs_cacheprime.c

説明を見る。
00001 /*
00002  * Copyright (c) James Peach 2005-2006
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #include "includes.h"
00020 
00021 /* Cache priming module.
00022  *
00023  * The purpose of this module is to do RAID stripe width reads to prime the
00024  * buffer cache to do zero-copy I/O for subsequent sendfile calls. The idea is
00025  * to do a single large read at the start of the file to make sure that most or
00026  * all of the file is pulled into the buffer cache. Subsequent I/Os have
00027  * reduced latency.
00028  *
00029  * Tunables.
00030  *
00031  *      cacheprime:rsize    Amount of readahead in bytes. This should be a
00032  *                          multiple of the RAID stripe width.
00033  *      cacheprime:debug    Debug level at which to emit messages.
00034  */
00035 
00036 #define READAHEAD_MIN       (128 * 1024)        /* min is 128 KiB */
00037 #define READAHEAD_MAX       (100 * 1024 * 1024) /* max is 100 MiB */
00038 
00039 #define MODULE "cacheprime"
00040 
00041 static int module_debug;
00042 static ssize_t g_readsz = 0;
00043 static void * g_readbuf = NULL;
00044 
00045 /* Prime the kernel buffer cache with data from the specified file. We use
00046  * per-fsp data to make sure we only ever do this once. If pread is being
00047  * emulated by seek/read/seek, when this will suck quite a lot.
00048  */
00049 static BOOL prime_cache(
00050             struct vfs_handle_struct *  handle,
00051                         files_struct *                  fsp,
00052                         int                                     fd,
00053                         SMB_OFF_T                           offset,
00054                         size_t                              count)
00055 {
00056         SMB_OFF_T * last;
00057         ssize_t nread;
00058 
00059         last = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T);
00060         if (!last) {
00061                 return False;
00062         }
00063 
00064         if (*last == -1) {
00065             /* Readahead disabled. */
00066             return False;
00067         }
00068 
00069         if ((*last + g_readsz) > (offset + count)) {
00070             /* Skip readahead ... we've already been here. */
00071             return False;
00072         }
00073 
00074         DEBUG(module_debug,
00075             ("%s: doing readahead of %lld bytes at %lld for %s\n",
00076             MODULE, (long long)g_readsz, (long long)*last,
00077             fsp->fsp_name));
00078 
00079         nread = sys_pread(fd, g_readbuf, g_readsz, *last);
00080         if (nread < 0) {
00081             *last = -1;
00082             return False;
00083         }
00084 
00085         *last += nread;
00086         return True;
00087 }
00088 
00089 static int cprime_connect(
00090                 struct vfs_handle_struct *  handle,
00091                 const char *                service,
00092                 const char *                user)
00093 {
00094         module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100);
00095         if (g_readbuf) {
00096                 /* Only allocate g_readbuf once. If the config changes and
00097                  * another client multiplexes onto this smbd, we don't want
00098                  * to risk memory corruption.
00099                  */
00100                 return SMB_VFS_NEXT_CONNECT(handle, service, user);
00101         }
00102 
00103         g_readsz = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
00104                                         MODULE, "rsize", NULL));
00105 
00106         if (g_readsz < READAHEAD_MIN) {
00107                 DEBUG(module_debug, ("%s: %ld bytes of readahead "
00108                             "requested, using minimum of %u\n",
00109                             MODULE, (long)g_readsz, READAHEAD_MIN));
00110                 g_readsz = READAHEAD_MIN;
00111         } else if (g_readsz > READAHEAD_MAX) {
00112                 DEBUG(module_debug, ("%s: %ld bytes of readahead "
00113                             "requested, using maximum of %u\n",
00114                             MODULE, (long)g_readsz, READAHEAD_MAX));
00115                 g_readsz = READAHEAD_MAX;
00116         }
00117 
00118         if ((g_readbuf = SMB_MALLOC(g_readsz)) == NULL) {
00119                 /* Turn off readahead if we can't get a buffer. */
00120                 g_readsz = 0;
00121         }
00122 
00123         return SMB_VFS_NEXT_CONNECT(handle, service, user);
00124 }
00125 
00126 static ssize_t cprime_sendfile(
00127                 struct vfs_handle_struct *  handle,
00128                 int                         tofd,
00129                 files_struct *              fsp,
00130                 int                         fromfd,
00131                 const DATA_BLOB *           header,
00132                 SMB_OFF_T                   offset,
00133                 size_t                      count)
00134 {
00135         if (g_readbuf && offset == 0) {
00136                 prime_cache(handle, fsp, fromfd, offset, count);
00137         }
00138 
00139         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, fromfd,
00140                                      header, offset, count);
00141 }
00142 
00143 static ssize_t cprime_read(
00144                 vfs_handle_struct * handle,
00145                 files_struct *      fsp,
00146                 int                 fd,
00147                 void *              data,
00148                 size_t              count)
00149 {
00150         SMB_OFF_T offset;
00151 
00152         offset = SMB_VFS_LSEEK(fsp, fd, 0, SEEK_CUR);
00153         if (offset >= 0 && g_readbuf)  {
00154                 prime_cache(handle, fsp, fd, offset, count);
00155                 SMB_VFS_LSEEK(fsp, fd, offset, SEEK_SET);
00156         }
00157 
00158         return SMB_VFS_NEXT_READ(handle, fsp, fd, data, count);
00159 }
00160 
00161 static ssize_t cprime_pread(
00162                 vfs_handle_struct * handle,
00163                 files_struct *      fsp,
00164                 int                 fd,
00165                 void *              data,
00166                         size_t              count,
00167                 SMB_OFF_T           offset)
00168 {
00169         if (g_readbuf) {
00170                 prime_cache(handle, fsp, fd, offset, count);
00171         }
00172 
00173         return SMB_VFS_NEXT_PREAD(handle, fsp, fd, data, count, offset);
00174 }
00175 
00176 static vfs_op_tuple cprime_ops [] =
00177 {
00178         {SMB_VFS_OP(cprime_sendfile),
00179                 SMB_VFS_OP_SENDFILE, SMB_VFS_LAYER_TRANSPARENT},
00180         {SMB_VFS_OP(cprime_pread),
00181                 SMB_VFS_OP_PREAD, SMB_VFS_LAYER_TRANSPARENT},
00182         {SMB_VFS_OP(cprime_read),
00183                 SMB_VFS_OP_READ, SMB_VFS_LAYER_TRANSPARENT},
00184         {SMB_VFS_OP(cprime_connect),
00185                 SMB_VFS_OP_CONNECT,  SMB_VFS_LAYER_TRANSPARENT},
00186 
00187         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
00188 };
00189 
00190 /* -------------------------------------------------------------------------
00191  * Samba module initialisation entry point.
00192  * -------------------------------------------------------------------------
00193  */
00194 
00195 NTSTATUS vfs_cacheprime_init(void);
00196 NTSTATUS vfs_cacheprime_init(void)
00197 {
00198     return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE, cprime_ops);
00199 }
00200 
00201 /* vim: set sw=4 ts=4 tw=79 et: */

Sambaに対してSat Aug 29 21:23:07 2009に生成されました。  doxygen 1.4.7