Obtain CPU utilization and memory usage in Linux

Source: Internet
Author: User
Obtain the CPU utilization and memory usage in Linux-Linux general technology-Linux programming and kernel information. The following is a detailed description. In Linux, if you want to monitor the running status of a process, such as viewing its CPU usage efficiency and memory usage, you need to read some system information from the/proc directory of the system. Then the analysis results are obtained, especially for embedded applications. The code in this article is obtained from the source code analysis of the top Command, and some modifications are made. The code is successfully debugged in FC6 + GCC4.1. I also gained some insights from this project.

1. Linux is very elegant. If you do this function in Windows, you need to call ActiveX controls. In Linux, you only need to read the text.
2. if you do not know how to implement any functions, you can check the source code and customize the functions required by other software.
3. I want to see more and learn more about technology.

Top Command source code download: http://www.groupsys.com/top/download.shtml
Project download: http://www.cppblog.com/Files/dyj057/mytop.zip
The following code obtains the CPU and memory of the system:

Void
Get_system_info (info)
Struct system_info * info;
{
Char buffer [4096 + 1];
Int fd, len;
Char * p;
Int I;

/* Get load averages */
{
Fd = open ("loadavg", O_RDONLY );
Len = read (fd, buffer, sizeof (buffer)-1 );
Close (fd );
Buffer [len] = '\ 0 ';

Info-> load_avg [0] = strtodd (buffer, & p );
Info-> load_avg [1] = strtodd (p, & p );
Info-> load_avg [2] = strtodd (p, & p );
P = skip_token (p);/* skip running/tasks */
P = skip_ws (p );
If (* p)
Info-> last_pid = atoi (p );
Else
Info-> last_pid =-1;
}

/* Get the cpu time info */
{
Fd = open ("stat", O_RDONLY );
Len = read (fd, buffer, sizeof (buffer)-1 );
Close (fd );
Buffer [len] = '\ 0 ';

P = skip_token (buffer);/* "cpu "*/
Cp_time [0] = strtoul (p, & p, 0 );

Cp_time [1] = strtoul (p, & p, 0 );
Cp_time [2] = strtoul (p, & p, 0 );
Cp_time [3] = strtoul (p, & p, 0 );

/* Convert cp_time counts to percentages */
Percentages (4, cpu_states, cp_time, cp_old, cp_diff );
}

/* Get system wide memory usage */
{
Char * p;

Fd = open ("meminfo", O_RDONLY );
Len = read (fd, buffer, sizeof (buffer)-1 );
Close (fd );
Buffer [len] = '\ 0 ';

/* Be prepared for extra columns to appear be seeking
To ends of lines */

P = buffer;
P = skip_token (p );
Memory_stats [0] = strtoul (p, & p, 10);/* total memory */

P = strchr (p, '\ n ');
P = skip_token (p );
Memory_stats [1] = strtoul (p, & p, 10);/* free memory */


P = strchr (p, '\ n ');
P = skip_token (p );
Memory_stats [2] = strtoul (p, & p, 10);/* buffer memory */

P = strchr (p, '\ n ');
P = skip_token (p );
Memory_stats [3] = strtoul (p, & p, 10);/* cached memory */

For (I = 0; I <8; I ++ ){
P ++;
P = strchr (p, '\ n ');
}

P = skip_token (p );
Memory_stats [4] = strtoul (p, & p, 10);/* total swap */

P = strchr (p, '\ n ');
P = skip_token (p );
Memory_stats [5] = strtoul (p, & p, 10);/* free swap */

}

/* Set arrays and strings */
Info-> cpustates = cpu_states;
Info-> memory = memory_stats;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.