Setting up user slice for CPU resource management in Debian12
by walee from LinuxQuestions.org on (#6NVG9)
Hi,
I have the following set up in my Deb12 distribution:
#cat /etc/systemd/system/user-.slice.d/50_CPUQuota.conf
[Slice]
CPUQuota=600%
MemoryMax=6G
However, when I test it why a normal user with a test program that uses multiple cores, I see:
ps -u myUser -o %cpu | awk '{sum+=$1} END {print sum}'
It reports percentages of more than the allocated 600%. The program is very simple python program where I call on multiple sub processes.
import time
import sys
from itertools import repeat
from multiprocessing import Pool, cpu_count
def f(x, runtime=1, sleeptime=0, busycycles=100000):
timeout = time.time() + runtime
cnt = 0
while True:
if time.time() > timeout:
break
if sleeptime and cnt % busycycles == 0:
time.sleep(sleeptime)
x*x
cnt += 1
if __name__ == '__main__':
runtime = 5 if len(sys.argv) <= 1 else float(sys.argv[1])
sleeptime = 0.0 if len(sys.argv) <= 2 else float(sys.argv[2])
busycycles = 100000 if len(sys.argv) <= 3 else int(sys.argv[3])
processes = cpu_count() if len(sys.argv) <= 4 else int(sys.argv[4]) if 0 < int(sys.argv[4]) <= cpu_count() else cpu_count()
print(f'Running for {runtime}s with sleep time of {sleeptime}s per {busycycles} cycles utilizing {processes} cores')
pool = Pool(processes)
pool.starmap(f, zip(range(processes), repeat(runtime), repeat(sleeptime), repeat(busycycles)))
Can anyone let me know if what I am doing correct for setting up user limitations on a shared machine or do I need to use something like cgroups2?
I have the following set up in my Deb12 distribution:
#cat /etc/systemd/system/user-.slice.d/50_CPUQuota.conf
[Slice]
CPUQuota=600%
MemoryMax=6G
However, when I test it why a normal user with a test program that uses multiple cores, I see:
ps -u myUser -o %cpu | awk '{sum+=$1} END {print sum}'
It reports percentages of more than the allocated 600%. The program is very simple python program where I call on multiple sub processes.
import time
import sys
from itertools import repeat
from multiprocessing import Pool, cpu_count
def f(x, runtime=1, sleeptime=0, busycycles=100000):
timeout = time.time() + runtime
cnt = 0
while True:
if time.time() > timeout:
break
if sleeptime and cnt % busycycles == 0:
time.sleep(sleeptime)
x*x
cnt += 1
if __name__ == '__main__':
runtime = 5 if len(sys.argv) <= 1 else float(sys.argv[1])
sleeptime = 0.0 if len(sys.argv) <= 2 else float(sys.argv[2])
busycycles = 100000 if len(sys.argv) <= 3 else int(sys.argv[3])
processes = cpu_count() if len(sys.argv) <= 4 else int(sys.argv[4]) if 0 < int(sys.argv[4]) <= cpu_count() else cpu_count()
print(f'Running for {runtime}s with sleep time of {sleeptime}s per {busycycles} cycles utilizing {processes} cores')
pool = Pool(processes)
pool.starmap(f, zip(range(processes), repeat(runtime), repeat(sleeptime), repeat(busycycles)))
Can anyone let me know if what I am doing correct for setting up user limitations on a shared machine or do I need to use something like cgroups2?