Before looking at these three techniques, we need to find a tool that will simulate high CPU usage on a system. We will be using CentOS as our base system, and to artificially load the processor we can use the prime number generator from the Mathomatic toolkit. There isn’t a prebuilt package for CentOS so you will need to build it yourself. Download the source code from http://mathomatic.orgserve.de/mathomatic-16.0.5.tar.bz2 and then unpack the archive file. Change directory into Run the command like this: /usr/local/bin/matho-primes 0 9999999999 > /dev/null & This will generate a list of prime numbers from zero to nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine. Since we don’t really want to keep the list, the output is redirected to Now run top and you will see that the matho-primes process is using all the available CPU. Exit top (press the q key) and kill the matho-primes process (fg to bring the process to the foreground and press CTRL+C). niceThe Start two nice matho-primes 0 9999999999 > /dev/null &matho-primes 0 9999999999 > /dev/null & Now run Observe that the process started without What this means in real terms is that if you want to run a CPU intensive task you can start it using nice and the scheduler will always ensure that other tasks have priority over it. This means that the server (or desktop) will remain responsive even when under heavy load. Nice has an associated command called renice +10 1234 Where 1234 is the PID. Don’t forget to kill the cpulimitThe
To install it on CentOS type: wget -O cpulimit.zip https://github.com/opsengine/cpulimit/archive/master.zipunzip cpulimit.zipcd cpulimit-mastermakesudo cp src/cpulimit /usr/bin The commands above will download the source code from GitHub, unpack the archive file, build the binary, and copy it to
cpulimit -l 50 matho-primes 0 9999999999 > /dev/null & Note how the You can also limit a currently running process by specifying its PID using the ‘-p’ parameter. For example cpulimit -l 50 -p 1234 Where 1234 is the PID of the process. cgroupsControl groups (cgroups) are a Linux kernel feature that allows you to specify how the kernel should allocate specific resources to a group of processes. With cgroups you can specify how much CPU time, system memory, network bandwidth, or combinations of these resources can be used by the processes residing in a certain group. The advantage of control groups over By judiciously using cgroups the resources of entire subsystems of a server can be controlled. For example in CoreOS, the minimal Linux distribution designed for massive server deployments, the upgrade processes are controlled by a cgroup. This means the downloading and installing of system updates doesn’t affect system performance. To demonstrate cgroups, we will create two groups with different CPU resources allocated to each group. The groups will be called ‘cpulimited’ and ‘lesscpulimited’. The groups are created with the sudo cgcreate -g cpu:/cpulimitedsudo cgcreate -g cpu:/lesscpulimited The “-g cpu” part of the command tell cgroups that the groups can place limits on the amount of CPU resources given to the processes in the group. Other contollers include The cpu controller has a property known as cpu.shares. It is used by the kernel to determine the share of CPU resources available to each process across the cgroups. The default value is 1024. By leaving one group (lesscpulimited) at the default of 1024 and setting the other (cpulimited) to 512, we are telling the kernel to split the CPU resources using a 2:1 ratio. To set the cpu.shares to 512 in the cpulimited group, type: sudo cgset -r cpu.shares=512 cpulimited To start a task in a particular cgroup you can use the cgexec command. To test the two cgroups, start matho-primes in the cpulimited group, like this: sudo cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null & If you run This is because when a single process is running, it uses as much CPU as necessary, regardless of which cgroup it is placed in. The CPU limitation only comes into effect when two or more processes compete for CPU resources. Now start a second matho-primes process, this time in the lesscpulimited group: sudo cgexec -g cpu:lesscpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null & The top command shows us that the process in the cgroup with the greater cpu.shares value is getting more CPU time. Now start another matho-primes process in the cpulimited group: sudo cgexec -g cpu:cpulimited /usr/local/bin/matho-primes 0 9999999999 > /dev/null & Observe how the CPU is still being proportioned in a 2:1 ratio. Now the two You can read the full control groups documentation from Red Hat (which applies equally to CentOS 7). |
Home > Server config >