Skip to content

Commit a2379f5

Browse files
committed
adjust get_avail_memory for cgroupsv2
1 parent 4d6e299 commit a2379f5

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

SitePackage.lua

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,25 +256,48 @@ end
256256

257257
local function get_avail_memory()
258258
-- If a limit is set, return the maximum allowed memory, else nil
259-
260-
-- look for the memory cgroup (if any)
261259
local cgroup = nil
260+
local memory_file = nil
261+
262+
-- look for the memory cgroup (if any):
263+
-- for cgroupv2: find the hierarchegy and the memory controller
264+
-- for cgroupv1: look for the memory controller
265+
local lines = {}
262266
for line in io.lines("/proc/self/cgroup") do
263-
cgroup = line:match("^[0-9]+:memory:(.*)$")
264-
if cgroup then
265-
break
266-
end
267+
table.insert(lines, line)
267268
end
268269

269-
if not cgroup then
270-
return nil
271-
end
270+
-- if it's one line: cgroupv2
271+
if #lines == 1 then
272+
cgroup = lines[1]:match("^[0-9]+::(.*)$")
273+
274+
if not cgroup then
275+
return nil
276+
end
272277

273-
-- Slurm tasks are only limited by the job step that launched it
274-
cgroup = cgroup:gsub("/task_[%d]+$", "")
278+
-- Slurm tasks are only limited by the job step that launched it
279+
cgroup = cgroup:gsub("/task_[%d]+$", "")
275280

276-
-- read the current maximum allowed memory usage (memory + swap)
277-
local memory_file = io.open("/sys/fs/cgroup/memory/" .. cgroup .. "/memory.memsw.limit_in_bytes")
281+
-- read the current maximum allowed memory usage (memory)
282+
memory_file = io.open("/sys/fs/cgroup/" .. cgroup .. "/memory.max")
283+
else
284+
for _, line in ipairs(lines) do
285+
cgroup = line:match("^[0-9]+:memory:(.*)$")
286+
if cgroup then
287+
break
288+
end
289+
end
290+
291+
if not cgroup then
292+
return nil
293+
end
294+
295+
-- Slurm tasks are only limited by the job step that launched it
296+
cgroup = cgroup:gsub("/task_[%d]+$", "")
297+
298+
-- read the current maximum allowed memory usage (memory + swap)
299+
memory_file = io.open("/sys/fs/cgroup/memory/" .. cgroup .. "/memory.memsw.limit_in_bytes")
300+
end
278301

279302
if not memory_file then
280303
return nil

0 commit comments

Comments
 (0)