@@ -256,25 +256,48 @@ end
256
256
257
257
local function get_avail_memory ()
258
258
-- If a limit is set, return the maximum allowed memory, else nil
259
-
260
- -- look for the memory cgroup (if any)
261
259
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 = {}
262
266
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 )
267
268
end
268
269
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
272
277
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]+$" , " " )
275
280
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
278
301
279
302
if not memory_file then
280
303
return nil
0 commit comments