kingluo commented on issue #7949:
URL: https://github.com/apache/apisix/issues/7949#issuecomment-1255799376

   @hahayyum 
   
   If you could reproduce the issue at ease, you could use this gdb script to 
check where's the possible leak.
   **Note that use it with caution, you should test it in your test env, but 
not production env!!!**
   
   `/tmp/leak.gdb`
   ```gdb
   set $cnt = 0
   
   source luajit21.py
   
   set logging off
   
   shell rm -f /tmp/lua_mem.log
   
   b lj_gc.c:875
   command 1
       set $cnt++
       if $cnt == 1000
           quit
       end
       pipe printf "-> alloc %p total=%d\n", p, g->gc.total | tee -a 
/tmp/lua_mem.log >/dev/null
       pipe lbt | tee -a /tmp/lua_mem.log >/dev/null
       continue
   end
   
   b lj_alloc_free
   command 2
       pipe printf "-> free %p\n", ptr | tee -a /tmp/lua_mem.log >/dev/null
       continue
   end
   
   continue
   ```
   
   Here gdb script breaks the program in 1000 times and exit. You could change 
it to fit your need.
   
   
   Here is the demo:
   
   ```bash
   cd /tmp
   git glone https://github.com/api7/openresty-gdb-utils
   cd openresty-gdb-utils
   # determine the pid of your worker process, e.g. 3338439
   # it's recommanded to set worker_processes 1
   PYTHONPATH=$PWD command gdb -iex 'set pagination off' -batch-silent -x 
/tmp/leak.gdb -p 3338439
   ```
   
   Lauch your workload.
   
   **Here is my demo workload:**
   
   For example, `apisix/leak.lua`
   
   ```lua
   local sock = require("socket")
   
   local _M = {}
   
   local t = {}
   
   local function foo()
       for i=1,500 do
           -- local tt = {}
           -- for j=1,1000 do
           --     tt[j]=j
           -- end
           table.insert(t, {i=i})
       end
   end
   
   function _M.run()
       ngx.say("start")
       for i=1,333 do
           ngx.say("loop", i)
           ngx.flush(true)
           foo()
           sock.sleep(3)
           ngx.say(#t)
       end
   end
   
   return _M
   ```
   
   ```bash
   curl http://127.0.0.1:9180/apisix/admin/routes/leak  -H 'X-API-KEY: 
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
   {
       "uri": "/leak",
       "plugins": {
           "serverless-pre-function": {
               "phase": "rewrite",
               "functions" : ["local demo = require(\"apisix.leak\"); return 
function() demo.run(); ngx.say(\"ok\"); end"]
           }
       }
   }'
   
   curl localhost:9080/leak
   ```
   
   In my demo, I could check where my demo script allocate so many memory:
   
   
   ```bash
   tail -100 /tmp/lua_mem.log
   
   -> alloc 0x7fc33fce8500 total=11437441
   @/opt/apisix.fork/apisix/leak.lua:7
   @/opt/apisix.fork/apisix/leak.lua:22
   local demo = require("apisix.leak"); return function() demo.run(); 
ngx.say("ok"); end:1
   @/opt/apisix.fork/apisix/plugins/serverless/init.lua:84
   @/opt/apisix.fork/apisix/plugin.lua:918
   @/opt/apisix.fork/apisix/init.lua:442
   =access_by_lua(nginx.conf:347):2
   -> alloc 0x7fc33fce8580 total=11437553
   @/opt/apisix.fork/apisix/leak.lua:7
   @/opt/apisix.fork/apisix/leak.lua:22
   local demo = require("apisix.leak"); return function() demo.run(); 
ngx.say("ok"); end:1
   @/opt/apisix.fork/apisix/plugins/serverless/init.lua:84
   @/opt/apisix.fork/apisix/plugin.lua:918
   @/opt/apisix.fork/apisix/init.lua:442
   =access_by_lua(nginx.conf:347):2
   -> alloc 0x7fc33fce8600 total=11437665
   @/opt/apisix.fork/apisix/leak.lua:7
   @/opt/apisix.fork/apisix/leak.lua:22
   local demo = require("apisix.leak"); return function() demo.run(); 
ngx.say("ok"); end:1
   @/opt/apisix.fork/apisix/plugins/serverless/init.lua:84
   @/opt/apisix.fork/apisix/plugin.lua:918
   @/opt/apisix.fork/apisix/init.lua:442
   
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to