Resource isolation - system D

Keywords: sudo Apache snapshot socket

introduce

systemd System and Users

position

/usr/lib/systemd/system
/usr/lib/systemd/user

unit

The basic unit of system management, called unit, is divided into the following types

Service unit: System services
 Target unit: A group of units
 Device Unit: Hardware Device
 Mount Unit: Mount Point of File System
 Automount Unit: Automatic Mount Point
 Path Unit: File or Path
 Scope Unit: An external process that is not initiated by System D
 Slice Unit: Process Group
 Snapshot Unit: System snapshot, you can cut back a snapshot
 Socket Unit: socket for Interprocess Communication
 Swap Unit: swap file
 Timer Unit: Timer

Frequently used commands

# List Unit s that are running
$ systemctl list-units

# List all Unit s, including those that did not find the configuration file or failed to start
$ systemctl list-units --all

# List all Unit s that are not running
$ systemctl list-units --all --state=inactive

# List all Unit s that failed to load
$ systemctl list-units --failed

# List all running Unit s of service type
$ systemctl list-units --type=service

# Display system status
$ systemctl status

# Display the status of a single Unit
$ sysystemctl status bluetooth.service

# Show whether a Unit is running
$ systemctl is-active application.service

# Show whether a Unit is in a failed start state
$ systemctl is-failed application.service

# Show whether a Unit service has established a startup link
$ systemctl is-enabled application.service

# Start a service immediately
$ sudo systemctl start apache.service

# Stop a service immediately
$ sudo systemctl stop apache.service

# Restart a service
$ sudo systemctl restart apache.service

# Kill all the subprocesses of a service
$ sudo systemctl kill apache.service

# Reload the configuration file for a service
$ sudo systemctl reload apache.service

# Overload all modified configuration files
$ sudo systemctl daemon-reload

# Display all the underlying parameters of a Unit
$ systemctl show httpd.service

# Displays the value of a specified attribute for a Unit
$ systemctl show -p CPUShares httpd.service

# Setting the specified properties of a Unit
$ sudo systemctl set-property httpd.service CPUShares=500

test

start-up

Start a service: toptest2.service under slice: test2.slice
As you can see, the pid of the starting process is 8929

[root@rac2 ~]# systemd-run --unit=toptest2 --slice=test2 top -b
Running as unit toptest2.service.
[root@rac2 ~]# systemctl status toptest2.service
● toptest2.service - /usr/bin/top -b
   Loaded: loaded (/run/systemd/system/toptest2.service; static; vendor preset: disabled)
  Drop-In: /run/systemd/system/toptest2.service.d
           └─50-Description.conf, 50-ExecStart.conf, 50-Slice.conf
   Active: active (running) since Fri 2017-03-10 14:46:18 CST; 6s ago
 Main PID: 8928 (top)
   CGroup: /test2.slice/toptest2.service
           └─8928 /usr/bin/top -b

Mar 10 14:46:24 rac2 top[8928]: 25230 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/6:0
Mar 10 14:46:24 rac2 top[8928]: 26164 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/3:2
Mar 10 14:46:24 rac2 top[8928]: 27118 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/8:2
Mar 10 14:46:24 rac2 top[8928]: 29597 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/9:2
Mar 10 14:46:24 rac2 top[8928]: 30021 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/2:0
Mar 10 14:46:24 rac2 top[8928]: 31018 root      20   0  116276   3076   1800 S   0.0  0.0   0:00.75 bash
Mar 10 14:46:24 rac2 top[8928]: 31112 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/19+
Mar 10 14:46:24 rac2 top[8928]: 31827 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/1:2
Mar 10 14:46:24 rac2 top[8928]: 32082 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/7:+
Mar 10 14:46:24 rac2 top[8928]: 32136 polkitd   20   0  524900  11184   4640 S   0.0  0.0   0:02.24 polkitd
[root@rac2 ~]# ps -ef | grep top
root      8928     1  0 14:46 ?        00:00:00 /usr/bin/top -b

CPU affinity

CPUAffinity
CPU affinity refers to the ability to bind one or more processes to one or more processors in a Linux system.
CPU affinity masks of a process determine which CPUs or CPUs the process will run on. In a multiprocessor system, setting CPU affinity masks may achieve better performance.

[root@rac2 ~]# ps -ef | grep top
root      8928     1  0 14:46 ?        00:00:00 /usr/bin/top -b

First look at the current cpu affinity of the process pid is 8928

[root@rac2 ~]# taskset -c -p 8928
pid 8928's current affinity list: 0-31

Set to run only on cpu2

[root@rac2 ~]# taskset -p -c 2 8928
pid 8928's current affinity list: 0-31
pid 8928's new affinity list: 2
[root@rac2 ~]# taskset -c -p 8928
pid 8928's current affinity list: 2

Set to run only on CPU 3, 5, 7-10

[root@rac2 ~]#  taskset -p -c 3,5,7-10 8928
pid 8928's current affinity list: 2
pid 8928's new affinity list: 3,5,7-10
[root@rac2 ~]# taskset -c -p 8928
pid 8928's current affinity list: 3,5,7-10

service file format

The following service is not very standard. It mainly shows the parameter CPUAffinity.

[Unit]
Description=t1

[Service]
Type=simple
ExecStart=/usr/bin/top -b
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true
CPUAffinity=3

[Install]
WantedBy=multi-user.target

Limited memory

systemd-run --unit=memorytest1 --slice=test2  /usr/bin/bash /tmp/highmem.sh
systemctl set-property memorytest1.service MemoryLimit=2G

You can see if the process is running by the following command

systemctl status memorytest1.service

From top, you can see that the process consumes no more memory than the limit.
For example, on 128G servers, set to 2G, memory consumption fluctuates around 1.5%.

Restrict disk reading speed

systemd-run --unit=ioread --slice=test2 dd if=/dev/sdk of=/dev/null
iotop You can see that the reading speed is:180M/s
systemctl set-property ioread BlockIOReadBandwidth='/dev/sdk 50M'
iotop You can see that the reading speed is:50M/s

Posted by rashmi_k28 on Mon, 15 Apr 2019 21:45:32 -0700