Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get cpu utilization in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to obtain the utilization rate of cpu in Linux. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

1. Obtain relevant performance parameters from the / proc file system

Cpu usage: / proc/stat

Memory usage: / proc/meminfo

Network load Information: / proc/net/dev

Corresponding calculation method: (excerpt from: what is the proc file system, see Resources)

(1) processor utilization

(2) memory utilization

(3) inflow and outflow packets

(4) overall network load

These data are extracted from / proc/stat, / proc/net/dev, and / proc/meminfo files, respectively. If there is a problem or is 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 utilization

Here we extract four pieces of data from / proc/stat: user mode (user), low priority user mode (nice), kernel mode (system), and idle processor time (idle). They are all located on the * * line of the / proc/stat file. The utilization of CPU is calculated using the following formula.

CPU utilization = 100 * (user + nice + system) / (user + nice + system + idle)

(2) memory utilization

Here you need to extract two pieces of data from the / proc/meminfo file, the current memory usage (cmem) and the total memory (amem).

% memory usage = 100 * (cmem / umem)

(3) Network utilization

In order to get data about network utilization, you need to get two pieces of data from the / proc/net/dev file: the number of packets output from the machine and the number of packets flowing into the machine. They are all on the fourth line of the file.

The performance collector begins to record the initial values of these two data, and each time it gets this value, it subtracts the initial value, that is, the packets passing from this node since the start of the cluster.

Using the above data, the average load of the network is calculated as follows:

Average network load = (outgoing packets + incoming packets) / 2

two。 Adjust the relevant kernel configuration through the / proc file system

Allow ip forwarding / proc/sys/net/ipv4/ip_forward

Prohibit ping / proc/sys/net/ipv4/icmp_echo_ignore_all

You can write "1" directly into the above two "files" under the command line to achieve the relevant configuration, if you write "0" will cancel the relevant configuration. However, after the system restarts, these configurations will revert to the default settings, so if you want these changes to take effect, you can write the following configuration directly to the / etc/profile file, or other program files that are executed with the system boot.

Echo 1 > / proc/sys/net/ipv4/ip_forward

Echo 1 > / proc/sys/net/ipv4/icmp_echo_ignore_all

There is a problem in calculating cpu utilization here, and you need to use the value of the previous state to calculate it.

The correct method of calculation is to wait for a period of time:

1. Record the use of cpu at a certain time

2. Wait for a period of time

3. Record the use of cpu at the moment

4. Calculate the total time slice

Sum all the cpu usage of * * times and get J1.

Sum all the cpu usage of the second time, and get j2

J2-j1 gets all the time slices for this period

That is, the sum of all columns of the second time of total=j2-j1= and the sum of all columns of-times.

5. Calculate idle time

Idle corresponds to the data in the fifth column, which can be subtracted from the second column by the second one.

The fifth column of the second time of idle=-the fifth column of the second time

6. Calculate cpu utilization

Rate= (total-idle) / total

Under Linux/Unix, CPU utilization is divided into user mode, system mode and idle state, which represents the execution time of CPU in user mode, the execution time of system kernel, and the execution time of idle system processes. The CPU utilization rate usually refers to the time that CPU executes non-system idle processes / the total execution time of CPU.

Linux c program acquires cpu utilization and memory usage

2009-05-17 23:10

If you want to get the hardware usage of the linux system when the target machine is running, write these Mini Program posts and use them directly later.

The method is to read the file under proc to get it. Cpu usage: / proc/stat, memory usage: / proc/meminfo

Look at the program:

/ * @ file: statusinfo.c * * @ brief: obtain cpu and memory usage from linux system * * @ version 1.0 * * * / typedef struct PACKED / / define a cpu occupy structure {char name [20] / / define an array name of char type name has 20 elements unsigned int user; / / define an unsigned int type user unsigned int nice; / / define an unsigned int type nice unsigned int system;// define an unsigned int type system unsigned int idle; / / define an unsigned int type idle} CPU_OCCUPY Typedef struct PACKED / / defines a structure of memoccupy {char name [20]; / / defines an array name of type char name with 20 elements unsigned long total; char name2 [20]; unsigned long free;} MEM_OCCUPY; get_memoccupy (MEM_OCCUPY * mem) / / A pair of typeless get functions contains a pointer O {FILE * fd of a parameter structure class Int n; char buff [256]; MEM_OCCUPY * m; masked mems; fd = fopen ("/ proc/meminfo", "r") Fgets (buff, sizeof (buff), fd); sscanf (buff, "% s% u% s", m-> name, & m-> total, m-> name2); fgets (buff, sizeof (buff), fd) / / read the string of length buff from the fd file and save it to the space with the starting address buff (buff, "% s% u", m-> name2, & m-> free, m-> name2); fclose (fd); / / close the file fd} int cal_cpuoccupy (CPU_OCCUPY * o, CPU_OCCUPY * n) {unsigned long od, nd; unsigned long id, sd Int cpu_use = 0; od = (unsigned long) (o-> user + o-> nice + o-> system + o-> idle); / / * time (user + priority + system + idle) is assigned to od nd = (unsigned long) (n-> user + n-> nice + n-> system + n-> idle) / / the second time (user + priority + system + idle) is re-assigned to od id = (unsigned long) (n-> user-o-> user); / / the time difference between the user * and the second time is assigned to id sd = (unsigned long) (n-> system-o-> system) / / the time difference between the system * times and the second time is assigned to sd if ((nd-od)! = 0) cpu_use = (int) ((sd+id) * 10000) / (nd-od); / / (user + system) divides (the time difference between * times and the second time) and then assigns g_cpu_used else cpu_use = 0; / / printf ("cpu:% uAssociation", cpu_use) Return cpu_use;} get_cpuoccupy (CPU_OCCUPY * cpust) / / A pair of untyped get functions contains a pointer O {FILE * fd; int n; char buff of a formal parameter structure class; CPU_OCCUPY * cpu_occupy; cpu_occupy=cpust Fd = fopen ("/ proc/stat", "r"); fgets (buff, sizeof (buff), fd) Sscanf (buff, "% s% u% u", cpu_occupy- > name, & cpu_occupy- > user, & cpu_occupy- > nice,&cpu_occupy- > system, & cpu_occupy- > idle); fclose (fd);} int main () {CPU_OCCUPY cpu_stat1; CPU_OCCUPY cpu_stat2; MEM_OCCUPY mem_stat; int cpu / / get memory get_memoccupy ((MEM_OCCUPY *) & mem_stat); / / * get cpu usage get_cpuoccupy ((CPU_OCCUPY *) & cpu_stat1); sleep (10); / / get cpu usage get_cpuoccupy ((CPU_OCCUPY *) & cpu_stat2) for the second time / / calculate cpu utilization cpu = cal_cpuoccupy ((CPU_OCCUPY *) & cpu_stat1, (CPU_OCCUPY *) & cpu_stat2); return 0;} after reading the above, do you have any further understanding of how to obtain cpu utilization in Linux? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report