Monday, 5 November 2018

New V2 CGroups inside Systemd using systemctl. Share user CPU and memory usage fairly

CPU & memory usage, can be hard limited or soft limited with CGroups.  Why is this great? It stops rogue users crashing the OS and ruining every other user's day.  There are lots of guides, but they are confusing because CGroups has V1 and V2,  with significant changes.  There doesn't seem to be many easy tutorials - this blog worked in November 2018 and uses V2 CGroups.

I recently had a problem on a multi user computer.  One user was demanding so much resource that the computer couldn't keep up.  *nix skills to the rescue!

Long Story about Multi-User Computer:

TL:DR FML.  Now skip to 'the Fix'.

Oh... you really care?  OK, imagine the scenario as follows, machine has 16 cores:

User 1(myself):  Running code on a single processor for long term test that take days to weeks of continuous computation
User 2  Running OMP tasks using 8 cores
User 3: Running OMP tasks using 13 cores

The computer (which is a linux box that is used for grunt processing) only has 16 cores, so obviously something gives.  Sadly  the computer  doesn't know that non of these processes are interactive, so it tries to carry out everything at once.   This means that each process gets a slices of 100ms in the ratio 1:8:13.  Worse still my code running on 1 core is now geting 72.7% of a core.  This wouldn't be the end of the world, but my code is quite RAM intensive, so instead of running for 100ms every 2.2s  it actually spends most of that time loading data in and out of the cache.  A run that I know used to take 40 hours went to over 8 days.  This affects everyone equally, but if you are using OMP tasks then the cache is shared and there is no need to load data in or out of it.  This means that you won't lose so much time when passing from one of your slices to another of your slices. My process only get one slice, so I'm always handing off to another user.

User 2 is under a lot of pressure and I have chosen not to push it.

Politely asking to User 3 to curtail their OMP task number to the number of cores available did not work.  Apparently the user with 13 cores has a 'bug' in their code that they can't explain or show me.  They claim not to understand it, but it magically requires 13 cores, they won't explain it to me or try to fix it.  They have also convinced our boss that this is reasonable behaviour.

User 3 routinely crashes the system when they decide to run code that leaks memory.  This obviously makes me lose weeks of work.  Yes, I should save state and be able to reload the code.  Its on my list, but it will take weeks to shoe horn in.

But wait... linux has an existing system for just my situation!  CGroups? Great! They create soft limits on CPU time, if there is no contention then processes are not curtailed, if there is, then time processor time is proportional to your CPUShare.  I talk to the sysadmin, he's sympathetic!  He asks me to find out about CGroups so that we share the load of putting them onto the system.

The Fix


Looking online it seems simple.  It turns out that it is, but most of the online documents are for V1 not V2!  Unless you are working with a legacy system, then the solution for giving users CPUShares and setting a memory limit are as follows.

Obligatory note: This is a fairly technical blog, it is your responsibility to know what you are doing. Take backups of all files that you are changing (e.g. sudo cp .safe) before you cut and paste a script from this blog, understand what it does, stay inside the magic circle.  Polite feedback welcome,  mistakes in this blog have happened.  It is up to you to decide if the commands you are executing are a good idea - I take no responsibility for lost files, forgotten passwords, accidental demon summoning and other problems encountered while reciting incantations described here.  You are now warned.

Optional: install cgroup-tools, but you need it if you want to run 'lscgroup' or network limits (which I haven't tried).
# apt install cgroup-tools
1. sudo nano /etc/default/grub, add the line:
GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
$ sudo update-grub   (note the sysadmin also found update-grub2 on his system)
2. Close all programs and run:  (this will reboot your computer)
$ sudo reboot

3. set DefaultCPUAccounting=yes  and DefaultMemoryAccounting=yes in /etc/systemd/system.conf

4.  Each user is referred to in control groups by a file called 'user-.slice'.  Do you know your uids?  Find them out by running:
$ cat /etc/passwd |grep  
which outputs something like:
andrew:x:1000:1000:andrew,,,:/home/andrew:/bin/bash
My uid is 1000 (actually the first uid defaults to 1000 and counts upwards sequentially in most systems).
Alternatively, run lscgroup and look for the names that start 'user'  and all the other cgroups.

5. To give a user 1000 CPUShares, and limit them to 2G of memory.  Run the commands: 
# systemctl set-property user-1001.slice MemoryLimit=2G 
# sudo systemctl set-property user-1001.slice CPUShares=500
6. and check that they are set correctly with:
# systemctl show -p CPUShares user-1001.slice
# systemctl show -p MemoryLimit user-1001.slice
Repeat step 3 for each user.  That's it!
Now if you want to check what this does,  try installing a program like stress.  If you have two users with different CPUShares, they'll get core access in a ratio proportional to the number of shares.
$stress --cpu 2 --timeout 60
and to check if the memory limit works, this script should get killed if the limit is less than 3Gb:
$dd bs=3G if=/dev/zero of=/dev/null
In the picture below, I am running the above line of code twice as two different users.  One is getting two thirds of the two cpus on this laptop the other is getting 1 third,  the shares are 1000 & 500 respectively. :-)

Tuesday, 3 November 2009

I thought I'd post my response to a question that I got on a user forum at the IET


> Thanks Andrew, useful info.! I'll have a look at
> gEDA
website and see how it works. One question
> though:
If you use gEDA and a client wants a
> design done in Orcad,
how will you get the
> software?


> Do you have a selection of different packages
> (I would have thought this would be really
> expensive tdo that)?


[background] My design house Paramita Electronics uses gEDA for all of it's in-house electronics development, and for around 85% (based on turnover) of my client work aswell.


Essentially, I have two kinds of clients - those that already have a toolchain and those that don't.


Those that don't, largely are only concerned that the work is up to a suitable standard. The package that I use isn't much interest to them unless it affects the design (in terms of vendor lock-in gEDA is arguably better than proprietary solutions) so we use gEDA and they pay nothing for the license.


Clients that already have a toolchain, already have licenses. So I usually offer to work onsite at their establishment, using their licenses, or I ask that my hourly rate includes an expense fee to cover the cost of the EDA tool.


In my experience, these clients don't mind hiring me onsite - I then use one of their licenses. My last large client had no problem with me using one of their altium licenses (they had 70 seats!!). They know that a consultant will cover the cost of EDA somehow - so the fact that I let them solve the problem whichever way is most economical to them is a bonus, rather than plumping for an expensive suite and limiting my clients to that, I can be more flexible.

Thursday, 29 October 2009

Paramita Electronics Product Development Blog

Andrew Whyte here. This blog is a record of progress of paramita ltd, a UK product development consultancy.

Since I set up shop in May 2008, I've been rushed off my feet! Working to design the electronics for exciting products like: motor control for a training device for surgeons, to smart utility metering devices...

In case you are wondering why that last link is so cryptic, I have to explain - the client has requested that details be kept confidential. In fact, the market for smart metering devices is very competitive, so it's really important that paramita ltd take our secrecy seriously. Luckily, we keep our data encrypted with industry standard encryption and backed up regularly.

paramita ltd has an advantage over our rivals, in that we preferentially use the gEDA toolchain for our development. gEDA is an open source electronic design toolchain. This means that we don't pay thousands of pounds for software that locks you, our clients, into using a particular design flow. With the gEDA toolchain, we are able to deliver our designs in an open format. If any client isn't happy with our service, they can take the design, to someone else to develop.

paramita ltd delivers real world, working electronic designs, not vendor lock in.