Skip to content

Commit f829065

Browse files
hnakamuragentzh
authored andcommitted
feature: shdict: added pure C API for getting free page size and total capacity for lua-resty-core.
Signed-off-by: Hiroaki Nakamura <[email protected]> Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent 59dff5e commit f829065

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

README.markdown

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,6 +3196,8 @@ Nginx API for Lua
31963196
* [ngx.shared.DICT.flush_all](#ngxshareddictflush_all)
31973197
* [ngx.shared.DICT.flush_expired](#ngxshareddictflush_expired)
31983198
* [ngx.shared.DICT.get_keys](#ngxshareddictget_keys)
3199+
* [ngx.shared.DICT.capacity](#ngxshareddictcapacity)
3200+
* [ngx.shared.DICT.free_space](#ngxshareddictfree_space)
31993201
* [ngx.socket.udp](#ngxsocketudp)
32003202
* [udpsock:setpeername](#udpsocksetpeername)
32013203
* [udpsock:send](#udpsocksend)
@@ -6205,6 +6207,8 @@ The resulting object `dict` has the following methods:
62056207
* [flush_all](#ngxshareddictflush_all)
62066208
* [flush_expired](#ngxshareddictflush_expired)
62076209
* [get_keys](#ngxshareddictget_keys)
6210+
* [capacity](#ngxshareddictcapacity)
6211+
* [free_space](#ngxshareddictfree_space)
62086212

62096213
All these methods are *atomic* operations, that is, safe from concurrent accesses from multiple nginx worker processes for the same `lua_shared_dict` zone.
62106214

@@ -6669,6 +6673,79 @@ This feature was first introduced in the `v0.7.3` release.
66696673

66706674
[Back to TOC](#nginx-api-for-lua)
66716675

6676+
ngx.shared.DICT.capacity
6677+
------------------------
6678+
**syntax:** *capacity_bytes = ngx.shared.DICT:capacity()*
6679+
6680+
**context:** *init_by_lua&#42;, set_by_lua&#42;, rewrite_by_lua&#42;, access_by_lua&#42;, content_by_lua&#42;, header_filter_by_lua&#42;, body_filter_by_lua&#42;, log_by_lua&#42;, ngx.timer.&#42;, balancer_by_lua&#42;, ssl_certificate_by_lua&#42;, ssl_session_fetch_by_lua&#42;, ssl_session_store_by_lua&#42;*
6681+
6682+
**requires:** `resty.core.shdict` or `resty.core`
6683+
6684+
Retrieves the capacity in bytes for the shm-based dictionary [ngx.shared.DICT](#ngxshareddict) declared with
6685+
the [lua_shared_dict](#lua_shared_dict) directive.
6686+
6687+
Example:
6688+
6689+
```lua
6690+
6691+
require "resty.core.shdict"
6692+
6693+
local cats = ngx.shared.cats
6694+
local capacity_bytes = cats:capacity()
6695+
```
6696+
6697+
This feature was first introduced in the `v0.10.11` release.
6698+
6699+
**Note:** This method requires the `resty.core.shdict` or `resty.core` modules from the [lua-resty-core](https://github.com/openresty/lua-resty-core) library.
6700+
6701+
This feature requires at least nginx core version `0.7.3`.
6702+
6703+
See also [ngx.shared.DICT](#ngxshareddict).
6704+
6705+
[Back to TOC](#nginx-api-for-lua)
6706+
6707+
ngx.shared.DICT.free_space
6708+
--------------------------
6709+
**syntax:** *free_page_bytes = ngx.shared.DICT:free_space()*
6710+
6711+
**context:** *init_by_lua&#42;, set_by_lua&#42;, rewrite_by_lua&#42;, access_by_lua&#42;, content_by_lua&#42;, header_filter_by_lua&#42;, body_filter_by_lua&#42;, log_by_lua&#42;, ngx.timer.&#42;, balancer_by_lua&#42;, ssl_certificate_by_lua&#42;, ssl_session_fetch_by_lua&#42;, ssl_session_store_by_lua&#42;*
6712+
6713+
**requires:** `resty.core.shdict` or `resty.core`
6714+
6715+
Retrieves the free page size in bytes for the shm-based dictionary [ngx.shared.DICT](#ngxshareddict).
6716+
6717+
**Note:** The memory for ngx.shared.DICT is allocated via the nginx slab allocator which has each slot for
6718+
data size ranges like ~8, 9~16, 17~32, ..., 1025~2048, 2048~ bytes. And pages are assigned to a slot if there
6719+
is no room in already assigned pages for the slot.
6720+
6721+
So even if the return value of the `free_space` method is zero, there may be room in already assigned pages, so
6722+
you may successfully set a new key value pair to the shared dict without getting `true` for `forcible` or
6723+
non nil `err` from the `ngx.shared.DICT.set`.
6724+
6725+
On the other hand, if already assigned pages for a slot are full and a new key value pair is added to the
6726+
slot and there is no free page, you may get `true` for `forcible` or non nil `err` from the
6727+
`ngx.shared.DICT.set` method.
6728+
6729+
Example:
6730+
6731+
```lua
6732+
6733+
require "resty.core.shdict"
6734+
6735+
local cats = ngx.shared.cats
6736+
local free_page_bytes = cats:free_space()
6737+
```
6738+
6739+
This feature was first introduced in the `v0.10.11` release.
6740+
6741+
**Note:** This method requires the `resty.core.shdict` or `resty.core` modules from the [lua-resty-core](https://github.com/openresty/lua-resty-core) library.
6742+
6743+
This feature requires at least nginx core version `1.11.7`.
6744+
6745+
See also [ngx.shared.DICT](#ngxshareddict).
6746+
6747+
[Back to TOC](#nginx-api-for-lua)
6748+
66726749
ngx.socket.udp
66736750
--------------
66746751
**syntax:** *udpsock = ngx.socket.udp()*

doc/HttpLuaModule.wiki

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5201,6 +5201,8 @@ The resulting object <code>dict</code> has the following methods:
52015201
* [[#ngx.shared.DICT.flush_all|flush_all]]
52025202
* [[#ngx.shared.DICT.flush_expired|flush_expired]]
52035203
* [[#ngx.shared.DICT.get_keys|get_keys]]
5204+
* [[#ngx.shared.DICT.capacity|capacity]]
5205+
* [[#ngx.shared.DICT.free_space|free_space]]
52045206
52055207
All these methods are ''atomic'' operations, that is, safe from concurrent accesses from multiple nginx worker processes for the same <code>lua_shared_dict</code> zone.
52065208
@@ -5598,6 +5600,71 @@ By default, only the first 1024 keys (if any) are returned. When the <code><max_
55985600
55995601
This feature was first introduced in the <code>v0.7.3</code> release.
56005602
5603+
== ngx.shared.DICT.capacity ==
5604+
'''syntax:''' ''capacity_bytes = ngx.shared.DICT:capacity()''
5605+
5606+
'''context:''' ''init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''
5607+
5608+
'''requires:''' <code>resty.core.shdict</code> or <code>resty.core</code>
5609+
5610+
Retrieves the capacity in bytes for the shm-based dictionary [[#ngx.shared.DICT|ngx.shared.DICT]] declared with
5611+
the [[#lua_shared_dict|lua_shared_dict]] directive.
5612+
5613+
Example:
5614+
5615+
<geshi lang="lua">
5616+
require "resty.core.shdict"
5617+
5618+
local cats = ngx.shared.cats
5619+
local capacity_bytes = cats:capacity()
5620+
</geshi>
5621+
5622+
This feature was first introduced in the <code>v0.10.11</code> release.
5623+
5624+
'''Note:''' This method requires the <code>resty.core.shdict</code> or <code>resty.core</code> modules from the [https://github.com/openresty/lua-resty-core lua-resty-core] library.
5625+
5626+
This feature requires at least nginx core version <code>0.7.3</code>.
5627+
5628+
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
5629+
5630+
== ngx.shared.DICT.free_space ==
5631+
'''syntax:''' ''free_page_bytes = ngx.shared.DICT:free_space()''
5632+
5633+
'''context:''' ''init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''
5634+
5635+
'''requires:''' <code>resty.core.shdict</code> or <code>resty.core</code>
5636+
5637+
Retrieves the free page size in bytes for the shm-based dictionary [[#ngx.shared.DICT|ngx.shared.DICT]].
5638+
5639+
'''Note:''' The memory for ngx.shared.DICT is allocated via the nginx slab allocator which has each slot for
5640+
data size ranges like ~8, 9~16, 17~32, ..., 1025~2048, 2048~ bytes. And pages are assigned to a slot if there
5641+
is no room in already assigned pages for the slot.
5642+
5643+
So even if the return value of the <code>free_space</code> method is zero, there may be room in already assigned pages, so
5644+
you may successfully set a new key value pair to the shared dict without getting <code>true</code> for <code>forcible</code> or
5645+
non nil <code>err</code> from the <code>ngx.shared.DICT.set</code>.
5646+
5647+
On the other hand, if already assigned pages for a slot are full and a new key value pair is added to the
5648+
slot and there is no free page, you may get <code>true</code> for <code>forcible</code> or non nil <code>err</code> from the
5649+
<code>ngx.shared.DICT.set</code> method.
5650+
5651+
Example:
5652+
5653+
<geshi lang="lua">
5654+
require "resty.core.shdict"
5655+
5656+
local cats = ngx.shared.cats
5657+
local free_page_bytes = cats:free_space()
5658+
</geshi>
5659+
5660+
This feature was first introduced in the <code>v0.10.11</code> release.
5661+
5662+
'''Note:''' This method requires the <code>resty.core.shdict</code> or <code>resty.core</code> modules from the [https://github.com/openresty/lua-resty-core lua-resty-core] library.
5663+
5664+
This feature requires at least nginx core version <code>1.11.7</code>.
5665+
5666+
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
5667+
56015668
== ngx.socket.udp ==
56025669
'''syntax:''' ''udpsock = ngx.socket.udp()''
56035670

src/ngx_http_lua_shdict.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,6 +2983,33 @@ ngx_http_lua_ffi_shdict_set_expire(ngx_shm_zone_t *zone, u_char *key,
29832983

29842984
return NGX_OK;
29852985
}
2986+
2987+
2988+
size_t
2989+
ngx_http_lua_ffi_shdict_capacity(ngx_shm_zone_t *zone)
2990+
{
2991+
return zone->shm.size;
2992+
}
2993+
2994+
2995+
# if nginx_version >= 1011007
2996+
size_t
2997+
ngx_http_lua_ffi_shdict_free_space(ngx_shm_zone_t *zone)
2998+
{
2999+
size_t bytes;
3000+
ngx_http_lua_shdict_ctx_t *ctx;
3001+
3002+
ctx = zone->data;
3003+
3004+
ngx_shmtx_lock(&ctx->shpool->mutex);
3005+
bytes = ctx->shpool->pfree * ngx_pagesize;
3006+
ngx_shmtx_unlock(&ctx->shpool->mutex);
3007+
3008+
return bytes;
3009+
}
3010+
# endif /* nginx_version >= 1011007 */
3011+
3012+
29863013
#endif /* NGX_LUA_NO_FFI_API */
29873014

29883015

0 commit comments

Comments
 (0)