Create systemd service with slices

in systemd •  7 years ago 

Introduction

This page describes how to write simple systemd services that can be used to gain practical knowledge about how to
use slices to manage system resources.

Prerequisites

Install the following packages:

  • daemonize
  • stress

Tasks

Create a simple systemd service that will put a high load on the system CPU

cat >  /etc/systemd/system/stress1.service <<EOF
[Unit]
Description=Stress test server
 
[Service]
Type=forking
ExecStart=/sbin/daemonize -c /tmp -p /tmp/stress.pid /bin/stress -c 4
PIDFile=/tmp/stress.pid

[Install]
WantedBy=multi-user.target"
EOF

Test if it works
systemctl daemon-reload
systemctl start stress1
top

Get systemctl information about this process
systemctl status stress1 -l

You should see four process using a lot of CPU.

    ● stress1.service - Stress test server
         Loaded: loaded (/etc/systemd/system/stress1.service; disabled; vendor preset: disabled)
         Active: active (running) since Fri 2018-02-23 08:01:00 CET; 9min ago
        Process: 12414 ExecStart=/sbin/daemonize -c /tmp -p /tmp/stress.pid /bin/stress -c 4 (code=exited, status=0/SUCCESS)
       Main PID: 12415 (stress)
         CGroup: /system.slice/stress1.service
               ├─12415 /bin/stress -c 4
               ├─12416 /bin/stress -c 4
               ├─12417 /bin/stress -c 4
               ├─12418 /bin/stress -c 4
               └─12419 /bin/stress -c 4
    
     Feb 23 08:01:00 localhost systemd[1]: Starting Stress test server...
     Feb 23 08:01:00 localhost systemd[1]: Started Stress test server.

As you can see this process is automatically assigned a 'CGroup' and it runs in 'system.slice'
Make two duplicate and name them stress2 and stress3
cp /etc/systemd/system/stress1.service /etc/systemd/system/stress2.service /etc/systemd/system/stress3.service

Change the service to run in the user slice by added the following line under the [service] section
Slice=user.slice

Stop, reload and start the service again.
systemctl stop stress
systemctl daemon-reload
systemctl start stress1
systemctl status stress1 -l

When you now check the status, the process is running under "user.slice"
If you where to start both services, you will see that they all use an equal amount of resources.
Next step is to limit the user slice CPU resources. By default each slice gets 1024 shares.
So let's limit the user slice to 256 shares and run
systemctl set-property user.slice CPUShares=512
systemctl status user.slice
systemctl stop stress2
systemctl start stress2

Now you will see that the resources are no longer equaly shared amongst both services.
Set the user.slice to it's regular shares: 1024
systemctl set-property user.slice CPUShares=1024

Now let's create sub-slices where we run our stress tests and un-equally device CPUshares.
Edit each stressX.service file, change CPUShares to different values and include a section like:
Slice=user-stressX.slice

Start restart all stress test services:
systemctl daemon-reload
systemctl stop stress1
systemctl stop stress2
systemctl start stress1
systemctl start stress2
systemctl start stress3

Check the resource usage. Is it what you expected?
Each unique sub-slice gets as much resource from it's parent.
Only one service is running in each sub-slice so the resources are equally shared.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @piet! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @piet! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!