function _NODE.get_memory_stats()

in kong/kong/pdk/node.lua [149:231]


  function _NODE.get_memory_stats(unit, scale)
    

    do
      unit = unit or "b"
      scale = scale or 2

      local pok, perr = pcall(utils.bytes_to_str, 0, unit, scale)
      if not pok then
        error(perr, 2)
      end
    end

    local res = {
      workers_lua_vms = self.table.new(n_workers, 0),
      lua_shared_dicts = self.table.new(0, #shms),
    }

    

    do
      if not shared.kong then
        goto lua_shared_dicts
      end

      local keys, err = shared.kong:get_keys()
      if not keys then
        res.workers_lua_vms.err = "could not get kong shm keys: " .. err
        goto lua_shared_dicts
      end

      if #keys == 1024 then
        
        
        ngx.log(ngx.WARN, "ngx.shared.kong:get_keys() returned 1024 keys, ",
                          "but it may have more")
      end

      for i = 1, #keys do
        local pid = match(keys[i], "kong:mem:(%d+)")
        if not pid then
          goto continue
        end

        local w = self.table.new(0, 2)
        w.pid = tonumber(pid)

        local count, err = shared.kong:get("kong:mem:" .. pid)
        if err then
          w.err = "could not get worker's HTTP Lua VM memory (pid: " ..
                  pid .. "): " .. err

        elseif type(count) ~= "number" then
          w.err = "could not get worker's HTTP Lua VM memory (pid: " ..
                  pid .. "): reported value is corrupted"

        else
          count = count * 1024 
          w.http_allocated_gc = convert_bytes(count, unit, scale)
        end

        insert(res.workers_lua_vms, w)

        ::continue::
      end

      sort(res.workers_lua_vms, sort_pid_asc)
    end

    
    ::lua_shared_dicts::

    for _, shm in ipairs(shms) do
      local allocated = shm.capacity - shm.zone:free_space()

      res.lua_shared_dicts[shm.name] = {
        capacity = convert_bytes(shm.capacity, unit, scale),
        allocated_slabs = convert_bytes(allocated, unit, scale),
      }
    end

    return res
  end