Skip to content

Commit

Permalink
feature: added support for ARM64 (Aarch64).
Browse files Browse the repository at this point in the history
now we require OpenResty's LuaJIT branch to work properly on ARM64 since
we make use of the thread.exdata Lua API.

On architectures other than ARM64, the thread.exdata API also saves the
per-request global env table and closure objects for each light thread,
which gives a nice ~10% speedup for the simplest Lua handler location
loaded by wrk over HTTP 1.1.

We now use proper userdata instead of lightuserdata for the
ngx.shared.DICT Lua objects.

we also mask off the bits higher than 47-bit of lightuserdata keys based
on C static variable addresses.

We also implemented the pure C API for the ndk.* API (to be used in
lua-resty-core).

Thanks Dejiang Zhu and Zexuan Luo for the development work of this patch.

Thanks Cloudflare for sponsoring this work.

Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
  • Loading branch information
doujiang24 authored and agentzh committed Oct 18, 2018
1 parent f64ec8c commit 7286812
Show file tree
Hide file tree
Showing 46 changed files with 1,106 additions and 215 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ install:
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
- git clone https://github.com/openresty/openresty-devel-utils.git
- git clone https://github.com/openresty/mockeagain.git
- git clone https://github.com/openresty/lua-cjson.git
- git clone https://github.com/openresty/lua-cjson.git lua-cjson
- git clone https://github.com/openresty/lua-upstream-nginx-module.git ../lua-upstream-nginx-module
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
- git clone https://github.com/openresty/nginx-eval-module.git ../nginx-eval-module
Expand All @@ -89,7 +89,7 @@ install:
- git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
- git clone https://github.com/openresty/lua-resty-mysql.git ../lua-resty-mysql
- git clone https://github.com/openresty/stream-lua-nginx-module.git ../stream-lua-nginx-module
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git luajit2

before_script:
- mysql -uroot -e 'create database ngx_test; grant all on ngx_test.* to "ngx_test"@"%" identified by "ngx_test"; flush privileges;'
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_lua_accessby.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,11 @@ ngx_http_lua_access_by_chunk(lua_State *L, ngx_http_request_t *r)
/* move code closure to new coroutine */
lua_xmove(L, co, 1);

#ifndef OPENRESTY_LUAJIT
/* set closure's env table to new coroutine's globals table */
ngx_http_lua_get_globals_table(co);
lua_setfenv(co, -2);
#endif

/* save nginx request in coroutine globals table */
ngx_http_lua_set_req(co, r);
Expand Down
3 changes: 3 additions & 0 deletions src/ngx_http_lua_balancer.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ ngx_http_lua_balancer_by_chunk(lua_State *L, ngx_http_request_t *r)

/* init nginx context in Lua VM */
ngx_http_lua_set_req(L, r);

#ifndef OPENRESTY_LUAJIT
ngx_http_lua_create_new_globals_table(L, 0 /* narr */, 1 /* nrec */);

/* {{{ make new env inheriting main thread's globals table */
Expand All @@ -372,6 +374,7 @@ ngx_http_lua_balancer_by_chunk(lua_State *L, ngx_http_request_t *r)
/* }}} */

lua_setfenv(L, -2); /* set new running env for the code closure */
#endif /* OPENRESTY_LUAJIT */

lua_pushcfunction(L, ngx_http_lua_traceback);
lua_insert(L, 1); /* put it under chunk and args */
Expand Down
50 changes: 23 additions & 27 deletions src/ngx_http_lua_bodyfilterby.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ static void ngx_http_lua_body_filter_by_lua_env(lua_State *L,
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;


/* key for the ngx_chain_t *in pointer in the Lua thread */
#define ngx_http_lua_chain_key "__ngx_cl"


/**
* Set environment table for the given code closure.
*
Expand All @@ -51,12 +47,14 @@ static void
ngx_http_lua_body_filter_by_lua_env(lua_State *L, ngx_http_request_t *r,
ngx_chain_t *in)
{
/* set nginx request pointer to current lua thread's globals table */
ngx_http_lua_main_conf_t *lmcf;

ngx_http_lua_set_req(L, r);

lua_pushlightuserdata(L, in);
lua_setglobal(L, ngx_http_lua_chain_key);
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
lmcf->body_filter_chain = in;

#ifndef OPENRESTY_LUAJIT
/**
* we want to create empty environment for current script
*
Expand All @@ -79,6 +77,7 @@ ngx_http_lua_body_filter_by_lua_env(lua_State *L, ngx_http_request_t *r,
/* }}} */

lua_setfenv(L, -2); /* set new running env for the code closure */
#endif /* OPENRESTY_LUAJIT */
}


Expand Down Expand Up @@ -236,8 +235,8 @@ ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
ngx_int_t rc;
uint16_t old_context;
ngx_http_cleanup_t *cln;
lua_State *L;
ngx_chain_t *out;
ngx_http_lua_main_conf_t *lmcf;

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua body filter for user lua code, uri \"%V\"", &r->uri);
Expand Down Expand Up @@ -299,11 +298,8 @@ ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
return NGX_ERROR;
}

L = ngx_http_lua_get_lua_vm(r, ctx);

lua_getglobal(L, ngx_http_lua_chain_key);
out = lua_touserdata(L, -1);
lua_pop(L, 1);
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
out = lmcf->body_filter_chain;

if (in == out) {
return ngx_http_next_body_filter(r, in);
Expand Down Expand Up @@ -345,7 +341,7 @@ ngx_http_lua_body_filter_init(void)


int
ngx_http_lua_body_filter_param_get(lua_State *L)
ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r)
{
u_char *data, *p;
size_t size;
Expand All @@ -354,6 +350,8 @@ ngx_http_lua_body_filter_param_get(lua_State *L)
int idx;
ngx_chain_t *in;

ngx_http_lua_main_conf_t *lmcf;

idx = luaL_checkint(L, 2);

dd("index: %d", idx);
Expand All @@ -363,8 +361,8 @@ ngx_http_lua_body_filter_param_get(lua_State *L)
return 1;
}

lua_getglobal(L, ngx_http_lua_chain_key);
in = lua_touserdata(L, -1);
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
in = lmcf->body_filter_chain;

if (idx == 2) {
/* asking for the eof argument */
Expand Down Expand Up @@ -442,6 +440,8 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
ngx_chain_t *cl;
ngx_chain_t *in;

ngx_http_lua_main_conf_t *lmcf;

idx = luaL_checkint(L, 2);

dd("index: %d", idx);
Expand All @@ -450,13 +450,13 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
return luaL_error(L, "bad index: %d", idx);
}

lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);

if (idx == 2) {
/* overwriting the eof flag */
last = lua_toboolean(L, 3);

lua_getglobal(L, ngx_http_lua_chain_key);
in = lua_touserdata(L, -1);
lua_pop(L, 1);
in = lmcf->body_filter_chain;

if (last) {
ctx->seen_last_in_filter = 1;
Expand Down Expand Up @@ -521,9 +521,7 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
case LUA_TNIL:
/* discard the buffers */

lua_getglobal(L, ngx_http_lua_chain_key); /* key val */
in = lua_touserdata(L, -1);
lua_pop(L, 1);
in = lmcf->body_filter_chain;

last = 0;

Expand Down Expand Up @@ -557,9 +555,7 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
lua_typename(L, type));
}

lua_getglobal(L, ngx_http_lua_chain_key);
in = lua_touserdata(L, -1);
lua_pop(L, 1);
in = lmcf->body_filter_chain;

last = 0;

Expand Down Expand Up @@ -625,8 +621,8 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
}
}

lua_pushlightuserdata(L, cl);
lua_setglobal(L, ngx_http_lua_chain_key);
lmcf->body_filter_chain = cl;

return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ngx_http_lua_bodyfilterby.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ngx_int_t ngx_http_lua_body_filter_inline(ngx_http_request_t *r,
ngx_chain_t *in);
ngx_int_t ngx_http_lua_body_filter_file(ngx_http_request_t *r,
ngx_chain_t *in);
int ngx_http_lua_body_filter_param_get(lua_State *L);
int ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r);
int ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx);

Expand Down
17 changes: 15 additions & 2 deletions src/ngx_http_lua_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ static ngx_int_t
ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
const char *key)
{
#ifndef OPENRESTY_LUAJIT
int rc;
u_char *err;
#endif

/* get code cache table */
lua_pushlightuserdata(L, &ngx_http_lua_code_cache_key);
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
code_cache_key));
lua_rawget(L, LUA_REGISTRYINDEX); /* sp++ */

dd("Code cache table to load: %p", lua_topointer(L, -1));
Expand All @@ -52,6 +55,10 @@ ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
lua_getfield(L, -1, key); /* sp++ */

if (lua_isfunction(L, -1)) {
#ifdef OPENRESTY_LUAJIT
lua_remove(L, -2); /* sp-- */
return NGX_OK;
#else
/* call closure factory to gen new closure */
rc = lua_pcall(L, 0, 1, 0);
if (rc == 0) {
Expand All @@ -73,6 +80,7 @@ ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
key, err);
lua_pop(L, 2);
return NGX_ERROR;
#endif /* OPENRESTY_LUAJIT */
}

dd("Value associated with given key in code cache table is not code "
Expand Down Expand Up @@ -102,10 +110,13 @@ ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
static ngx_int_t
ngx_http_lua_cache_store_code(lua_State *L, const char *key)
{
#ifndef OPENRESTY_LUAJIT
int rc;
#endif

/* get code cache table */
lua_pushlightuserdata(L, &ngx_http_lua_code_cache_key);
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
code_cache_key));
lua_rawget(L, LUA_REGISTRYINDEX);

dd("Code cache table to store: %p", lua_topointer(L, -1));
Expand All @@ -121,12 +132,14 @@ ngx_http_lua_cache_store_code(lua_State *L, const char *key)
/* remove cache table, leave closure factory at top of stack */
lua_pop(L, 1); /* closure */

#ifndef OPENRESTY_LUAJIT
/* call closure factory to generate new closure */
rc = lua_pcall(L, 0, 1, 0);
if (rc != 0) {
dd("Error: failed to call closure factory!!");
return NGX_ERROR;
}
#endif

return NGX_OK;
}
Expand Down
Loading

0 comments on commit 7286812

Please sign in to comment.