Let’s take a look at some very familiar knowledge that Linux users should know.
This article revolves around 3 topics: CPU, RAM, DISK
CPU
If the computer is a body, the CPU must be the controlling brain, helping the “body” actions to be smooth. Here are some ways to check whether the brain is normal or not
Check CPU information
1 2 | $ cat /proc/cpuinfo |
This command will show all the information of the CPU (name, manufacturer, …) in a fairly easy to understand way
We can combine it with the grep
command to get some information we want.
- Check physical processor number – sockets number
1 2 3 4 | $ cat /proc/cpuinfo | grep 'physical id' | uniq | wc -l 1 |
- Check the number of CPU cores on the machine:
1 2 3 4 | $ cat /proc/cpuinfo | grep -c 'core id' | uniq | wc -l 4 |
=> My computer has 1 CPU, 4 core
- Check the total number of threads
1 2 3 4 | $ cat /proc/cpuinfo | grep processor | uniq | wc -l 4 |
=> 4 core, 1 thread per core
From there, we have 1 sockets * 4 cores per socket * 1 thread per core = 4 CPU (s)
Check CPU performance
There are many ways to check CPU performance. I usually use the top
command
1 2 3 4 5 6 7 8 9 10 | $ top top - 19:58:52 up 2 days, 21:53, 1 user, load average: 1,38, 1,15, 0,87 Tasks: 293 total, 2 running, 228 sleeping, 0 stopped, 1 zombie %Cpu(s): 16,5 us, 8,3 sy, 0,0 ni, 74,3 id, 0,1 wa, 0,0 hi, 0,8 si, 0,0 st KiB Mem : 8015932 total, 242548 free, 6213208 used, 1560176 buff/cache KiB Swap: 999420 total, 2896 free, 996524 used. 729080 avail Mem |
The first line says:
- The time now is 19:58
- Server has been turned on for 2 days
- Number of users currently logged in: 1
- Average server load in 5/10/15 minutes is 1.38, 1.15, 0.87 respectively, this is quite important and I will talk more specifically below.
Line 2 shows the number of active processes
Line 3: indicates CPU usage status. 16.5 us, 8.3 sy means that the user uses 16.5% and the system uses 8.3%
Next is the information about RAM and SWAP I will talk more specifically in the RAM below
Load avg CPU
There is a remarkable parameter that load avg of CPU temporarily translated as average load or average load of CPU
In the example above, we have an load average: 1,38, 1,15, 0,87
. So understand this figure like?
Load avg can be interpreted as TOTAL average number of processes in the CPU queue. Load avg is usually expressed as a 3-digit form, averaging over 5-10-15 minutes. Here I have 4 meaningful CPUs and load avg in 5 minutes is 1.38. Mean Load avg / 1 CPU is
1 2 | 1.38/4 =0.345 |
The higher this number, the more processes the process must wait for the CPU to process. It must be understood that, at one time, 1 cpu can only execute 1 process. Other processes will have to wait until the CPU is idle to be executed. This figure should be distinguished from% of CPU usage.
- % CPU usage indicates whether a process is using more or less CPU
- Load avg shows how many processes have to wait for the CPU
Knowing these two numbers will help us evaluate the performance of the CPU and have appropriate solutions. For example, with a shared image storage server, or an LB server, there is no need to process lots of logic, we don’t need a CPU that is too strong but we need a lot of threads to handle many concurrent requests.
For monitoring Load avg of CPU, administrators usually use 0.7 / 1CPU as a landmark. If this threshold is exceeded, it is necessary to monitor and take appropriate measures (increase cpu, increase threading, ..).
RAM
Memory and cache
Linux tends to “cache the world” to increase processor speed, RAM is often freed and temporarily stored on disk to use when needed.
This means that when we open a process and turn it off, the system will cache this memory so that when we call the process again, it will immediately use this memory instead of allocating it again. The more processes you open, then turn off, the more cache.
When opening a new process, if the system lacks RAM, Linux will move the cache into SWAP to reserve all the RAM for the process being executed.
So if you want to know if your system is running low on RAM, look at the SWAP index. If the SWAP is high, the device is running low on RAM. A common command to view RAM information is:
1 2 3 4 5 6 | $ free -m total used free shared buff/cache available Mem: 7828 3231 1179 595 3417 3705 Swap: 975 0 975 |
Clear the cache server
About memory organization, cache mechanism and some concepts you can read more in this article https://kipalog.com/posts/Tim-hieu-to-chuc-memory-va-slab-trong-linux -kernel
- Clear the Cache
1 2 | sync; echo 1 > /proc/sys/vm/drop_caches |
- Delete dentries and inodes (slab)
1 2 | sync; echo 2 > /proc/sys/vm/drop_caches |
- Delete PageCache, dentries and inodes.
1 2 | sync; echo 3 > /proc/sys/vm/drop_caches |
DISK
- Check Disk status
1 2 3 4 5 6 7 | $ df -h Filesystem Size Used Avail Use% Mounted on udev 3,8G 0 3,8G 0% /dev tmpfs 783M 2,3M 781M 1% /run /dev/mapper/ubuntu--vg-root 227G 49G 168G 23% / |
- Check the disk partition
1 2 3 4 5 6 7 8 9 | $ lsblk sda 8:0 0 232,9G 0 disk ├─sda1 8:1 0 512M 0 part /boot/efi └─sda2 8:2 0 232,4G 0 part ├─ubuntu--vg-root 253:0 0 231,4G 0 lvm / └─ubuntu--vg-swap_1 253:1 0 976M 0 lvm [SWAP] sr0 11:0 1 1024M 0 rom |