"The proc file system is a pseudo file system, which only exists in the memory and does not occupy the external storage space. It provides interfaces for accessing system kernel data in the form of a file system. Users and applications can obtain system information through proc and change certain kernel parameters. "
This section describes how to obtain some firewall-related performance parameters from the/proc file system and how to modify the Kernel configuration through the/proc file system.
1. Obtain relevant performance parameters from the/proc file system
CPU usage:/proc/STAT
Memory usage:/proc/meminfo
Network load information:/proc/NET/dev
Calculation method: (from: proc file system, see references)
(1) processor usage
(2) memory usage
(3) inbound and outbound data packets
(4) overall network load
These data must be extracted from the/proc/STAT,/proc/NET/dev, And/proc/meminfo files respectively. If you have any problems or are not clear about the data to be extracted, you can use man proc to view the online manual of the proc file system.
(1) processor usage
Here, we need to extract four pieces of data from/proc/STAT: user mode, nice mode, and system mode) and idle processor time (idle ). They are located in the first line of the/proc/STAT file. The CPU usage is calculated using the following formula.
CPU usage = 100 * (User + nice + System)/(User + nice + system + idle)
(2) memory usage
Here we need to extract two data from the/proc/meminfo file, the current memory usage (cmem) and the total memory (amem ).
Memory usage percentage = 100 * (cmem/umem)
(3) Network Utilization
To obtain data about network utilization, You need to obtain two pieces of data from the/proc/NET/dev file: the number of data packets output from the local machine and the number of data packets flowing into the local machine. Both are located in the fourth row of the file.
The performance collection program starts to record the initial values of the two data values. After each value is obtained, the initial value is subtracted from the data packets passed through the current node starting from the cluster.
Calculate the average network load by using the above data:
Average network load = (output packet + incoming packet)/2
2. Adjust the related Kernel configuration through the/proc file system
Allow IP Forwarding/proc/sys/NET/IPv4/ip_forward
Disable Ping/proc/sys/NET/IPv4/icmp_echo_ignore_all
You can directly write "1" to the above two "Files" under the command line to implement related configuration. If "0" is written, the related configuration will be canceled. However, after the system is restarted, these configurations will be restored to the default settings. If you want these modifications to take effect, you can directly write the following configuration into the/etc/profile file, or other program files executed as the system starts.
Echo 1>/proc/sys/NET/IPv4/ip_forward
Echo 1>/proc/sys/NET/IPv4/icmp_echo_ignore_all
If you need to obtain other performance parameters or perform more kernel configurations, you can refer to the proc file system introduction in the following link, or you can directly view related information through man Proc.
References and links
[1] What is a proc file system?
Http://blog.chinaunix.net/u1/37836/showart_304248.html
[2] How to obtain CPU usage in Linux
Http://topic.csdn.net/t/20060701/23/4855045.html
[3] indexing the/proc/NET/directory
Http://www.linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html? Page = 1
[4]/proc/NET Introduction
Http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/en-US/Reference_Guide/s2-proc-dir-net.html
[5] Using ulimit and proc to adjust system parameters
Http://www.linuxfly.org/post/73.htm
Note: The file contains an increment. The CPU usage for each item corresponding to a time interval increases. to calculate the percentage, you must use the last status value.
Here, there is a problem in calculating the CPU usage. You need to use the value in the previous state to calculate the CPU usage. |
Return |
Elf published on |
#4 |
The correct calculation method is to wait for the previous time: 1. Record CPU usage at a specific time point 2. Wait for the previous time period 3. Record the current CPU usage 4. Calculate the total time slice Sum all CPU usage for the first time and obtain J1 Sum up all CPU usage for the second time and obtain J2 J2-j1 gets all time slices for this time period That is, total = j2-j1 = sum of all columns for the second time-sum of all columns for the first time 5. Calculate the idle time Idle corresponds to the data in the Fifth Column. Use the second value minus the first value. Idle = fifth column of the second operation-fifth column of the first operation 6. Calculate CPU usage Rate = (total-idle)/Total |
See http://oss.lzu.edu.cn/blog/article.php? Tid_1379.html
When we conduct performance tests, it is a common method to monitor the CPU usage of backend servers. If the CPU usage of the server is high, the server is busy. If the front-end response time is getting bigger and bigger, and the back-end CPU utilization is always not going up, it means there is a bottleneck somewhere and the system needs to be tuned. This is something that is easily understood by people who do not know the technology.
Is that correct? I personally think it is not very accurate. This depends on the type of processes you test in the background. If it is a computing-intensive process, when the front-end pressure increases, it is easy to put the CPU utilization rate up. However, if it is an I/O network-intensive process, even if there are more and more client requests, but the server CPU may not be able to go up, this is determined by the natural attributes of the process you want to test.
What is CPU utilization? In Linux/Unix, CPU utilization is divided into user-state, system-state, and idle state, indicating the time when the CPU is in user-state, and the time when the system kernel is executed, and the execution time of idle system processes. Since the computer power-on, the CPU has been busy, so the CPU utilization is always 100%. When no user process needs to be executed, the CPU executes the default idle process of the system. The CPU usage refers to the time when the CPU executes non-system idle processes/The total execution time of the CPU.
In the Linux kernel, there is a global variable: jiffies. Jiffies indicates the time. Its unit varies with the hardware platform. A constant Hz is defined in the system, representing the number of minimum time intervals per second. In this way, the unit of jiffies is 1/Hz. Jiffies of Intel Platform is measured in 1/100 seconds, which is the minimum time interval that the system can distinguish. For each CPU time slice, jiffies must be added with 1. CPU utilization is expressed by dividing the execution user State + system state jiffies by the total jifffies.
In Linux, the CPU utilization is calculated from the/proc/STAT file. the first few lines of this file record the user State and system state of each CPU, for different jiffies in idle state, common monitoring software uses the data in/proc/STAT to calculate the CPU utilization.
Contains information about all CPU activities. All values in this file are accumulated from the start of the system to the current time. /Proc/STAT/
[Work @ builder ~] $ CAT/proc/STAT
CPU 432661 13295 86656 422145968 171474 233
Cpu0 123075 2462 23494 105543694 0 16586
Cpu1 111917 4124 23858 105503820 69697 123
Cpu2 103164 3554 21530 105521167 64032 106 334
Cpu3 94504 3153 17772 105577285 4 24
Intr 1065711094 1057275779 92 0 6 0 4 0 0 0 0 0 0 70 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7376958 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Ctxt 1, 19067887
(Btime 1139187531)
Processes 270014
Procs_running 1
Procs_blocked 0
Output description
The meanings of each parameter in each line of CPU and cpu0, cpu1, cpu2, and cpu3 (in the first behavior example) are:
Parameter description
User (432661) indicates the CPU time (unit: jiffies) of the user State from the start of the system to the current time. The nice value is not a negative process. 1 jiffies = 0.01 seconds
Nice (13295) indicates the CPU time occupied by processes whose nice value is negative (unit: jiffies) from the time the system starts to the current time)
System (86656) accumulates from system startup to current time, core time (unit: jiffies)
Idle (422145968) accumulates from system startup to current time, except hard disk Io wait time (unit: jiffies)
Iowait (171474) accumulates from system startup to current time, hard disk Io wait time (unit: jiffies ),
IRQ (233) indicates the hard interrupt time (unit: jiffies) from the start of the system to the current time)
Softirq (5346) indicates the Soft Interrupt time (unit: jiffies) from the start of the system to the current time)
CPU time = user + system + nice + idle + iowait + IRQ + softirq
The "intr" line shows the number of interruptions. The first line is the number of all interruptions that have occurred since the system was started; then each number corresponds to the number of times a specific interruption has occurred since the system was started.
"Ctxt" indicates the number of times the CPU context has been exchanged since the system was started.
"Btime" indicates the time (in seconds) that has elapsed since the system was started.
The number of tasks created since the system started "processes (total_forks.
"Procs_running": number of tasks in the current queue.
"Procs_blocked": Number of blocked tasks. So the CPU usage calculation method: You can use two sampling points to calculate the difference. (Idle2-idle1)/cpu2-cpu1 with Improved shell code
1 #! /Bin/sh
2
3 # function: Calculate CPU utilization and select sampling points
4 # formula:
5 # Method 1: CPU usage = (idle2-idle1)/(cpu2-cpu1) * 100
6 # Method 2: CPU usage = [(user_2 + sys_2 + nice_2)-(user_1 + sys_1 + nice_1)]/(total_2-total_1) * 100
7 # method 3: (used in this script)
8 # total_0 = user [0] + nice [0] + system [0] + idle [0] + iowait [0] + IRQ [0] + softirq [0]
9 # total_1 = user [1] + nice [1] + system [1] + idle [1] + iowait [1] + IRQ [1] + softirq [1]
10 # CPU usage = (idle [0]-idle [1])/(total_0-total_1) * 100
11
12 ## echo user nice system idle iowait IRQ softirq
13 cpulog_1 = $ (awk '/\ <CPU \>/{print $2 "" $3 "" $4 "" $5 "" $6 "" $7 "" $8} '/proc/STAT)
14 sys_idle_1 = $ (echo $ cpulog_1 | awk '{print $4 }')
15 total_1 = $ (echo $ cpulog_1 | awk '{print $1 + $2 + $3 + $4 + $5 + $6 + $7 }')
16
17 sleep 5
18
19 cpulog_2 =$ (awk '/\ <CPU \>/{print $2 "" $3 "" $4 "" $5 "" $6 "" $7 "" $8} '/proc/STAT)
20 sys_idle_2 =$ (echo $ cpulog_2 | awk '{print $4 }')
21 total_2 =$ (echo $ cpulog_2 | awk '{print $1 + $2 + $3 + $4 + $5 + $6 + $7 }')
22
23 sys_idle = 'expr $ sys_idle_2-$ sys_idle_1'
24
25 Total = 'expr $ total_2-$ total_1'
26
27 # method 1
28 # sys_usage = 'expr $ sys_idle/$ total * 100 | BC-l'
29 # sys_rate = 'expr 100-$ sys_usage | BC-l'
30
31 # method2
32 tmp_rate = 'expr 1-$ sys_idle/$ Total | BC-l'
33 sys_rate = 'expr $ tmp_rate * 100 | BC-l'
34
35 # display
36 disp_sys_rate = 'expr "scale = 3; $ sys_rate/1" | bc'
37 echo $ disp_sys_rate %