pdf
|<<
<
>
>>|
/
The Latest and Greatest from {{#x|ngx_lua}}: New Features & Tools ☺{{#author|agentzh@gmail.com}}☺ {{#author|Yichun Zhang (agentzh)}} {{img src="images/cloudflare2.gif" width="141" height="71"}} {{#date|2014.10}} ---- {{img src="images/lz-shopsummary.png" width="1059" height="723"}} ---- {{img src="images/lz-arch.png" width="1152" height="607"}} ---- {{img src="images/modules-cloud.jpg" width="1186" height="685"}} ---- {{img src="images/chaoscat-big.jpg" width="1100" height="825"}} ---- {{img src="images/nginx-lua-big.png" width="1100" height="398"}} ---- {{img src="images/lua-embed-nginx.png" width="1142" height="662"}} ---- {{#x|☺}} No \"callback hell\". {{#x|☺}} 100% nonblocking IO ---- {{img src="images/ngx-lua-cmd-cloud.jpg" width="1186" height="636"}} ---- {{img src="images/cosocket.png" width="1168" height="397"}} ---- {{img src="images/pool.png" width="1133" height="467"}} ---- {{img src="images/lua-resty-cloud.jpg" width="1186" height="715"}} ---- {{img src="images/cf-arch.png" width="961" height="772"}} ---- {{#x|☺}} Lua SSL {{#x|☺}} Lua CDN {{#x|☺}} Lua WAF ---- {{#x|☺}} Light threads ---- {{img src="images/schedule-threads.jpg" width="1100" height="589"}} ---- {{#kw|local}} thread_A, err = {{#x|ngx.thread.spawn}}(func1) {{#cm|-- thread_A keeps running asynchronously}} {{#cm|-- in the background of the current}} {{#cm|-- "light thread".}} ---- {{#kw|local}} ok, res1, res2 = {{#x|ngx.thread.wait}}(thread_A, thread_B) ---- {{#kw|local}} ok, err = {{#x|ngx.thread.kill}}(thread_A) ---- {{#x|☺}} lua-resty-websocket ---- {{#kw|local}} server = {{#kw|require}} \"resty.websocket.server\" {{#kw|local}} wb, err = server:{{#x|new}}{ timeout = 5000, {{#cm|-- in milliseconds}} max_payload_len = 65535, } {{#kw|local}} data, typ, err = wb:{{#x|recv_frame}}() {{#kw|local}} bytes, err = wb:{{#x|send_text}}(\"Hello world\") ---- {{#x|☺}} full-duplex cosockets ---- {{#x|☺}} SSL/TLS cosocket {{img src="images/stock_lock.png" width="128" height="128"}} ---- {{#kw|local}} sock = ngx.socket.tcp() {{#kw|local}} ok, err = sock:connect(\"www.cloudflare.com\", 443) ok, err = sock:{{#x|sslhandshake}}( {{#kw|false}}, {{#cm|-- disable SSL session}} \"www.cloudflare.com\", {{#cm|-- SNI name}} {{#kw|true}} {{#cm|-- verify everything}} ) ---- {{#x|☺}} ssl_certificate_by_lua {{img src="images/stock_lock.png" width="128" height="128"}} ---- {{#x|☺}} lua-resty-core ---- LuaJIT internal workflow {{img src="images/luajit-code-flow.jpg" width="1100" height="406"}} ---- LuaJIT FFI ---- {{img src="images/ngx-lua-api.jpg" width="1100" height="625"}} ---- {{img src="images/lua-resty-core.jpg" width="1100" height="661"}} ---- {{#x|☺}} Much more CPU time is spent on JIT {{#ci|compiled}} Lua code. ---- {{#v|$}} {{#x|ngx-lj-vm-states.sxx}} -x 6817 --arg time=60 Start tracing 6817 (/opt/nginx/sbin/nginx) Please wait for 60 seconds... Observed 192 Lua-running samples and ignored 7 unrelated samples. Interpreted: 46% (89 samples) Compiled: {{#c|27%}} (52 samples) C Code (by interpreted Lua): 17% (34 samples) Garbage Collector: 8% (17 samples) ---- {{#v|$}} {{#x|lj-vm-states.sxx}} -x 5521 --arg time=60 Start tracing 5521 (/usr/local/nginx-waf/sbin/nginx-waf) Please wait for 60 seconds... Observed 1203 Lua-running samples and ignored 49 unrelated samples. Compiled: {{#c|64%}} (779 samples) C Code (by interpreted Lua): 14% (172 samples) Interpreted: 13% (158 samples) Garbage Collector (compiled): 3% (48 samples) Garbage Collector (not compiled): 3% (42 samples) Trace exiting: 0% (4 samples) ---- {{#x|☺}} nginx-systemtap-toolkit ---- {{img src="images/nginx-stap-tools-cloud.jpg" width="1186" height="716"}} ---- {{#x|☺}} stapxx (stap++) ---- {{img src="images/stapxx-tools-cloud.jpg" width="1186" height="716"}} ---- {{#x|☺}} When an nginx worker's CPU is too high... ---- {{img src="images/on-cpu-sketch.jpg" width="1088" height="512"}} ---- {{#cm|# assuming one nginx worker process has the pid 19647.}} {{#v|$}} {{#x|ngx-rps.sxx -x 19647}} WARNING: Tracing process 19647. Hit Ctrl-C to end. [1376939543] 300 req/sec [1376939544] 235 req/sec [1376939545] 235 req/sec [1376939546] 166 req/sec [1376939547] 238 req/sec [1376939548] 234 req/sec ^C ---- {{#v|$}} {{#x|./sample-bt -p 19647 -t 20 -u}} > a.bt WARNING: Tracing 19647 (/opt/nginx/sbin/nginx) in user-space only... WARNING: Time's up. Quitting now...(it may take a while) ---- {{#cm|# using Brendan Gregg's flame graph tools:}} {{#v|$}} stackcollapse-stap.pl a.bt > a.cbt {{#v|$}} flamegraph.pl a.cbt > a.svg ---- {{img src="images/nginx-c-land-flamegraph.jpg" width="1100" height="538"}} ---- {{#cm|# assuming the nginx worker process pid is 19647:}} {{#v|$}} {{#x|lj-lua-stacks.sxx --arg time=20 --skip-badvars -x 19647}} > a.bt Start tracing 19647 (/opt/nginx/sbin/nginx) Please wait for 20 seconds ---- {{#cm|# using Brendan Gregg's flame graph tools:}} {{#v|$}} stackcollapse-stap.pl a.bt > a.cbt {{#v|$}} flamegraph.pl a.cbt > a.svg ---- {{img src="images/lua-land-flamegraph.jpg" width="1100" height="225"}} ---- {{#x|☺}} When an nginx worker's CPU is low and the throughput is low... ---- {{img src="images/off-cpu-sketch.jpg" width="1152" height="448"}} ---- {{#x|☺}} {{#ci|off-CPU}} flame graphs from the {{#x|sample-bt-off-cpu}} tool ---- {{img src="images/off-cpu-flame-graph-nginx-cache-v2.jpg" width="1200" height="658"}} ---- {{img src="images/off-cpu-flame-graph-nginx-cache.jpg" width="1200" height="658"}} ---- {{img src="images/how-systemtap-works.jpg" width="1152" height="576"}} ---- {{#x|☺}} nginx-gdb-utils ---- {{img src="images/nginx-gdb-utils-cloud.jpg" width="1186" height="716"}} ---- {{#v|(gdb)}} {{#x|lbt}} C:ngx_http_lua_socket_tcp_receive @.../lib/resty/mysql.lua:191 @.../lib/resty/mysql.lua:530 content_by_lua:10 ---- {{#v|(gdb)}} {{#x|lbt full}} C:ngx_http_lua_socket_tcp_receive @.../lib/resty/mysql.lua:191 local \"self\": table (0x40f181a8) local \"sock\": table (0x40f181b0) @.../lib/resty/mysql.lua:530 local \"self\": table (0x40f18148) local \"opts\": table (0x40f18150) ... ---- {{#v|(gdb)}} {{#x|lgc}} The current memory size (allocated by GC): 898960 bytes ---- {{#v|(gdb)}} {{#x|lgcstat}} 15172 str objects: max=2956, avg = 51, min=18, sum=779126 987 upval objects: max=24, avg = 24, min=24, sum=23688 104 thread objects: max=1648, avg = 1622, min=528, sum=168784 431 proto objects: max=226274, avg = 2234, min=78, sum=963196 952 func objects: max=144, avg = 30, min=20, sum=28900 446 trace objects: max=23400, avg = 1857, min=160, sum=828604 2965 cdata objects: max=4112, avg = 17, min=12, sum=51576 18961 tab objects: max=24608, avg = 207, min=32, sum=3943256 9 udata objects: max=176095, avg = 39313, min=32, sum=353822 ---- {{#x|☺}} We are hiring :) ---- ☺ {{#ci|Any questions}}? ☺