Skip to content

Commit

Permalink
[cli] detect number of available CPU shares from cgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Feb 15, 2018
1 parent 36d9c6f commit 5f2f6e9
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gateway/src/apicast/cli/environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ local pairs = pairs
local ipairs = ipairs
local tostring = tostring
local tonumber = tonumber
local open = io.open
local ceil = math.ceil
local insert = table.insert
local concat = table.concat
local re = require('ngx.re')
Expand All @@ -40,7 +42,23 @@ local function parse_nameservers()
end
end

local function cpu_shares()
local shares
local file = open('/sys/fs/cgroup/cpu/cpu.shares')

if file then
shares = file:read('*n')

file:close()
end

return shares
end

local function cpus()
local shares = cpu_shares()
if shares then return ceil(shares / 1024) end

-- TODO: support /sys/fs/cgroup/cpuset/cpuset.cpus
-- see https://github.com/sclorg/rhscl-dockerfiles/blob/ff912d8764af9a41096e63064bbc325395afa608/rhel7.sti-base/bin/cgroup-limits#L55-L75
local nproc = util.system('nproc')
Expand Down

0 comments on commit 5f2f6e9

Please sign in to comment.