Apache StatCache Patch The MIT License (c)2007 Brian Shire & Facbeook Inc Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Provides a Stat Cache to save filesystem calls. To use: Apply the patch with the following commands: cd apache-1.3.x patch -p1 -i ap_StatCache.patch Enable/Disable StatCache in your Apache configuration: StatCacheEnable On Provide feedback to diff --git a/src/include/http_core.h b/src/include/http_core.h index bbe2bb6..16f656f 100644 --- a/src/include/http_core.h +++ b/src/include/http_core.h @@ -323,6 +323,19 @@ typedef struct { /* Per-server core configuration */ +/*** BEGIN Patch: StatCache ***/ +#ifndef STAT_CACHE_SIZE +#define STAT_CACHE_SIZE 256 +#endif +typedef struct _stat_cache_entry { + struct stat finfo; + char *filename; + short int rv; + int rv_errno; + struct _stat_cache_entry *next; +} stat_cache_entry; +/*** END Patch: StatCache**/ + typedef struct { #ifdef GPROF @@ -349,6 +362,12 @@ typedef struct { /* TRACE control */ int trace_enable; /* see AP_TRACE_ below */ + /*** BEGIN Patch: StatCache ***/ + stat_cache_entry *stat_cache[STAT_CACHE_SIZE]; + pool *stat_cache_pool; + short int stat_cache_enable; + /*** END Patch: StatCache ***/ + } core_server_config; /* trace_enable options */ diff --git a/src/main/http_core.c b/src/main/http_core.c index c29dc87..f5cf408 100644 --- a/src/main/http_core.c +++ b/src/main/http_core.c @@ -344,6 +344,11 @@ static void *create_core_server_config(pool *a, server_rec *s) conf->trace_enable = AP_TRACE_UNSET; + /*** BEGIN Patch: StatCache ***/ + conf->stat_cache_pool = ap_make_sub_pool(a); /* create a sub pool that we can clear as needed*/ + conf->stat_cache_enable = 0; /* off by default */ + /*** END Patch: StatCache ***/ + return (void *)conf; } @@ -3370,6 +3375,41 @@ static const char *set_trace_enable(cmd_parms *cmd, void *dummy, return NULL; } +static const char *set_stat_cache_enable(cmd_parms *cmd, void *dummy, const char *arg1) { + core_server_config *conf = ap_get_module_config(cmd->server->module_config, &core_module); + + if (strcasecmp(arg1, "on") == 0) { + conf->stat_cache_enable = 1; + } else if (strcasecmp(arg1, "off") == 0) { + conf->stat_cache_enable = 0; + } else { + return "StatCacheEnable must be set to 'on' or 'off'."; + } + + return NULL; +} + + +/*** BEGIN Patch: StatCache ***/ +int ap_clear_stat_cache(request_rec *r) { + core_server_config *sconf = ap_get_module_config(r->server->module_config, &core_module); + unsigned int hash, i, path_size; + stat_cache_entry *entry, *entry_next; + + if(sconf->stat_cache_enable) { + for(i=0; i < STAT_CACHE_SIZE; i++) { + sconf->stat_cache[hash] = NULL; + } + ap_clear_pool(sconf->stat_cache_pool); + return 0; + } + + return -1; + +} +/*** END Patch: StatCache ***/ + + static void log_backtrace(const request_rec *r) { const request_rec *top = r; @@ -3773,6 +3813,12 @@ static const command_rec core_cmds[] = { { "TraceEnable", set_trace_enable, NULL, RSRC_CONF, TAKE1, "'on' (default), 'off' or 'extended' to trace request body content"}, + +/*** BEGIN Patch: StatCache ***/ +{ "StatCacheEnable", set_stat_cache_enable, NULL, RSRC_CONF, TAKE1, + "'on', or 'off' (default) to enabled/disable the stat() system call cache."}, +/*** END Patch: StatCache ***/ + { NULL } }; diff --git a/src/main/http_request.c b/src/main/http_request.c index e69f326..e7f6bdb 100644 --- a/src/main/http_request.c +++ b/src/main/http_request.c @@ -141,6 +141,7 @@ static int get_path_info(request_rec *r) char bStripSlash=1; #endif + if (r->finfo.st_mode) { /* assume path_info already set */ return OK; @@ -204,7 +205,44 @@ static int get_path_info(request_rec *r) } else { errno = 0; - rv = stat(path, &r->finfo); + + /*** BEGIN Patch: StatCache ***/ + /* Cache the stat results so we can eliminate file system calls */ + { + core_server_config *sconf = ap_get_module_config(r->server->module_config, &core_module); + unsigned int hash, i, path_size; + stat_cache_entry *entry=NULL; + + if(sconf->stat_cache_enable) { + path_size = strlen(path)+1; + hash = 0; + for(i=0; istat_cache[hash]; + while(entry) { + if(!strncmp(entry->filename, path, path_size)) { + break; + } + entry = entry->next; + } + if(!entry) { /* add new entry to cache */ + entry = ap_palloc(sconf->stat_cache_pool, sizeof(stat_cache_entry)); + entry->filename = ap_pstrndup(sconf->stat_cache_pool, path, path_size-1); + entry->rv = stat(path, &entry->finfo); + entry->rv_errno = errno; + entry->next = sconf->stat_cache[hash]; + sconf->stat_cache[hash] = entry; + } + errno = entry->rv_errno; + rv = entry->rv; + r->finfo = entry->finfo; + } else { + rv = stat(path, &r->finfo); + } + } + /*** END Patch: StatCache ***/ #ifdef OS2 r->finfo.st_ino = 0; #endif