You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by hu...@apache.org on 2012/07/29 21:07:38 UTC

svn commit: r1366890 - in /httpd/httpd/trunk/modules/lua: lua_vmprep.c lua_vmprep.h mod_lua.c mod_lua.h

Author: humbedooh
Date: Sun Jul 29 19:07:38 2012
New Revision: 1366890

URL: http://svn.apache.org/viewvc?rev=1366890&view=rev
Log:
Add LuaCodeCache directive for controlling in-memory caching.
This might need some tweaking on the hash key generation for the mtime lookups, ideas are welcome.

Modified:
    httpd/httpd/trunk/modules/lua/lua_vmprep.c
    httpd/httpd/trunk/modules/lua/lua_vmprep.h
    httpd/httpd/trunk/modules/lua/mod_lua.c
    httpd/httpd/trunk/modules/lua/mod_lua.h

Modified: httpd/httpd/trunk/modules/lua/lua_vmprep.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/lua_vmprep.c?rev=1366890&r1=1366889&r2=1366890&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/lua/lua_vmprep.c (original)
+++ httpd/httpd/trunk/modules/lua/lua_vmprep.c Sun Jul 29 19:07:38 2012
@@ -341,7 +341,7 @@ AP_LUA_DECLARE(lua_State*)ap_lua_get_lua
                                                ap_lua_vm_spec *spec)
 {
     lua_State *L = NULL;
-
+    int tryCache = 0;
     if (apr_pool_userdata_get((void **)&L, spec->file,
                               lifecycle_pool) == APR_SUCCESS) {
       
@@ -360,6 +360,43 @@ AP_LUA_DECLARE(lua_State*)ap_lua_get_lua
       }
     }
         /*}*/
-
+    if (spec->codecache == AP_LUA_CACHE_FOREVER || (spec->bytecode && spec->bytecode_len > 0)) {
+        tryCache = 1;
+    }
+    else if (spec->codecache == AP_LUA_CACHE_STAT) {
+        apr_time_t modified;
+        char* mkey = apr_psprintf(lifecycle_pool, "ap_lua_modified:%s", spec->file);
+        if (apr_pool_userdata_get((void **)&modified, mkey,
+                              lifecycle_pool) == APR_SUCCESS) {
+            apr_finfo_t lua_finfo;
+            apr_stat(&lua_finfo, spec->file, APR_FINFO_MTIME, lifecycle_pool);
+            
+            /* On first visit, modified will be zero, but that's fine - The file is 
+             loaded in the vm_construct function.
+             */
+            if (modified == lua_finfo.mtime || modified == 0) tryCache = 1;
+            modified = lua_finfo.mtime;
+        }
+        else {
+            tryCache = 1;
+        }
+        apr_pool_userdata_set((void*) modified, mkey, NULL, lifecycle_pool);
+    }
+    if (tryCache == 0) {
+        int rc;
+        ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01481)
+            "(re)loading lua file %s", spec->file);
+        rc = luaL_loadfile(L, spec->file);
+        if (rc != 0) {
+            ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(01482)
+                          "Error loading %s: %s", spec->file,
+                          rc == LUA_ERRMEM ? "memory allocation error"
+                                           : lua_tostring(L, 0));
+            return 0;
+        }
+        lua_pcall(L, 0, LUA_MULTRET, 0);
+    }
+    
+    
     return L;
 }

Modified: httpd/httpd/trunk/modules/lua/lua_vmprep.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/lua_vmprep.h?rev=1366890&r1=1366889&r2=1366890&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/lua/lua_vmprep.h (original)
+++ httpd/httpd/trunk/modules/lua/lua_vmprep.h Sun Jul 29 19:07:38 2012
@@ -40,6 +40,11 @@
 #define AP_LUA_SCOPE_CONN          3
 #define AP_LUA_SCOPE_THREAD        4
 
+#define AP_LUA_CACHE_UNSET         0
+#define AP_LUA_CACHE_NEVER         1
+#define AP_LUA_CACHE_STAT          2
+#define AP_LUA_CACHE_FOREVER       3
+
 
 typedef void (*ap_lua_state_open_callback) (lua_State *L, apr_pool_t *p,
                                              void *ctx);
@@ -71,6 +76,8 @@ typedef struct
      */
     const char *bytecode;
     apr_size_t bytecode_len;
+    
+    int codecache;
 } ap_lua_vm_spec;
 
 typedef struct
@@ -81,6 +88,7 @@ typedef struct
     ap_regex_t *uri_pattern;
     const char *bytecode;
     apr_size_t bytecode_len;
+    int codecache;
 } ap_lua_mapped_handler_spec;
 
 /* remove and make static once out of mod_wombat.c */

Modified: httpd/httpd/trunk/modules/lua/mod_lua.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/mod_lua.c?rev=1366890&r1=1366889&r2=1366890&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/lua/mod_lua.c (original)
+++ httpd/httpd/trunk/modules/lua/mod_lua.c Sun Jul 29 19:07:38 2012
@@ -130,6 +130,7 @@ static ap_lua_vm_spec *create_vm_spec(ap
     spec->cb_arg = NULL;
     spec->bytecode = bytecode;
     spec->bytecode_len = bytecode_len;
+    spec->codecache = (cfg->codecache == AP_LUA_CACHE_UNSET) ? AP_LUA_CACHE_STAT : cfg->codecache;
 
     if (filename) {
         char *file;
@@ -910,6 +911,29 @@ static const char *register_lua_inherit(
     }
     return NULL;
 }
+static const char *register_lua_codecache(cmd_parms *cmd, 
+                                      void *_cfg,
+                                      const char *arg)
+{
+    ap_lua_dir_cfg *cfg = (ap_lua_dir_cfg *) _cfg;
+    
+    if (strcasecmp("never", arg) == 0) {
+        cfg->codecache = AP_LUA_CACHE_NEVER;
+    }
+    else if (strcasecmp("stat", arg) == 0) {
+        cfg->codecache = AP_LUA_CACHE_STAT;
+    }
+    else if (strcasecmp("forever", arg) == 0) {
+        cfg->codecache = AP_LUA_CACHE_FOREVER;
+    }
+    else { 
+        return apr_psprintf(cmd->pool,
+                            "LuaCodeCache type of '%s' not recognized, valid "
+                            "options are 'never', 'stat', and 'forever'", 
+                            arg);
+    }
+    return NULL;
+}
 static const char *register_lua_scope(cmd_parms *cmd, 
                                       void *_cfg,
                                       const char *scope, 
@@ -1184,6 +1208,10 @@ command_rec lua_commands[] = {
     AP_INIT_TAKE1("LuaInherit", register_lua_inherit, NULL, OR_ALL,
      "Controls how Lua scripts in parent contexts are merged with the current " 
      " context: none|parent-last|parent-first (default: parent-first) "),
+    
+    AP_INIT_TAKE1("LuaCodeCache", register_lua_codecache, NULL, OR_ALL,
+     "Controls the behavior of the in-memory code cache " 
+     " context: stat|forever|never (default: stat) "),
 
     AP_INIT_TAKE2("LuaQuickHandler", register_quick_hook, NULL, OR_ALL,
                   "Provide a hook for the quick handler of request processing"),
@@ -1270,6 +1298,7 @@ static void *merge_dir_config(apr_pool_t
 
     a->vm_scope = (overrides->vm_scope == AP_LUA_SCOPE_UNSET) ? base->vm_scope: overrides->vm_scope;
     a->inherit = (overrides->inherit== AP_LUA_INHERIT_UNSET) ? base->inherit : overrides->inherit;
+    a->codecache = (overrides->codecache== AP_LUA_CACHE_UNSET) ? base->codecache : overrides->codecache;
 
     if (a->inherit == AP_LUA_INHERIT_UNSET || a->inherit == AP_LUA_INHERIT_PARENT_FIRST) { 
         a->package_paths = apr_array_append(p, base->package_paths, overrides->package_paths);

Modified: httpd/httpd/trunk/modules/lua/mod_lua.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/mod_lua.h?rev=1366890&r1=1366889&r2=1366890&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/lua/mod_lua.h (original)
+++ httpd/httpd/trunk/modules/lua/mod_lua.h Sun Jul 29 19:07:38 2012
@@ -120,6 +120,11 @@ typedef struct
   
     /* Whether Lua scripts in a sub-dir are run before parents */
     ap_lua_inherit_t inherit;
+    
+    /**
+     * AP_LUA_CACHE_NEVER | AP_LUA_CACHE_STAT | AP_LUA_CACHE_FOREVER
+     */
+    unsigned int codecache;
 
 } ap_lua_dir_cfg;