Another method for obtaining CPU and memory usage information of the Operating System

Source: Internet
Author: User

We can use two methods to obtain CPU and memory information: use the top tool provided by Linux, or directly read the directory/proc/{process ID}/stat in the file system. Here, I will introduce another method to obtain this information, which is applicable to both global systems and specific processes. It is easier to obtain this method. We will use the libgtop library for implementation. Next we will introduce libgtop and use it to compile a simple example tool.

First, install the libgtop library in the system. If it is not installed, search for and download the library online. It is worth noting that the libgtop-2.0 depends on the glib-2.0 library, so make sure that the glib-2.0 library is correctly installed. After installing the libgtop-2.0, you can use its header file to program. Here is an example of monitoring CPU and memory usage:


# Include <stdio. h>
# Include <glibtop. h>
# Include <glibtop/cpu. h>
# Include <glibtop/mem. h>
# Include <glibtop/proctime. h>
# Include <glibtop/procmem. h>
# Include <unistd. h>
Int main (int argc, char * argv [])
{
Glibtop_cpu cpu_begin, cpu_end ;/////////////////////////////
Glibtop_proc_time proctime_begin, proctime_end; // Declare the CPU info and
Glibtop_mem memory; // memory info struct
Glibtop_proc_mem procmem ;///////////////////////////////

Int du, dn, ds, di;
Int dpu, dps;
Float cpurate, memrate;
Int pid = fork (); // create a process to run the specified program
If (pid = 0) // the child process
{
Execvp (argv [1], & argv [1]);
}
Else // the parent process
{
While (1)
{
Glibtop_get_cpu (& cpu_begin );
Glibtop_get_proc_time (& proctime_begin, pid );
Sleep (1); // the interval time is 1 second
Glibtop_get_cpu (& cpu_end );
Glibtop_get_proc_time (& proctime_end, pid );
Du = cpu_end.user-cpu_begin.user;
Dn = cpu_end.nice-cpu_begin.nice;
Ds = cpu_end.sys-cpu_begin.sys;
Di = cpu_end.idle-cpu_begin.idle;
Dpu = proctime_end.utime-proctime_begin.utime;
Dps = proctime_end.stime-proctime_begin.stime;
Cpurate = 100.0 * (dpu + dps)/(du + dn + ds + di) * 1.0); // calculate the CPU usage
Glibtop_get_mem (& memory );
Glibtop_get_proc_mem (& procmem, pid );
Memrate = 100 * (procmem. resident)/(memory. total) * 1.0); // calculate the memory usage

Fprintf (stderr, "du: % d, dn: % d, ds: % d, di: % d,", du, dn, ds, di );
Fprintf (stderr, "dpu: % d, dps: % d", dpu, dps );
Fprintf (stderr, "cpu rate is: % 0.3f %", cpurate );
Fprintf (stderr, "mem rate is: % 0.3f % \ n", memrate );
}
}
Return 0;
}

 

Run the following command to compile the program:


Gcc procmonitor. c-o procmonitor-I/usr/include/glib-2.0-I/usr/lib/glib-2.0/include-I/usr/include/libgtop-2.0-lgtop-2.0-lglib-2.0
The executable program procmonitor is obtained, and the program is started by passing the command line parameter, for example:


./Procmonitor mplayer movie.mkv
The initiator mplayerwill start and upload the movie.mkv file, and the CPU and memory information will be displayed in the command line. You can also use the redirection '>' symbol to print the information to the file, as shown below:


./Procmonitor mplayer movie.mkv 2> infofile.txt
Note:

1. Specify the glib-2.0 and libgtop-2.0 Library at the same time when building the program.

2. All involved struct and function prototype can be found in/usr/include/libgtop-2.0.

3. The formula for calculating memory usage is as follows:


(Memory of resident) memory (memory of total ).
Formula for Calculating CPU usage:


(User_mode CPU time + kernel_mode CPU time) hour (total CPU time ).
Compare with the result of top, the mem % is basic equal, but the CUP % is totally different. it is because that the machine we test with has more than one CPU. so the result calculated by the top is the usage of the CPU which only used by the process. but the result we get is the average usage of all the CPU. certainly, we can get the same result using the same calculate mothod with other member of structure, such as xcpu_utime [GLIBTOP_NCPU] and xcpu_stime [GLIBTOP_NCPU] in structure glibtop_proc_time.

Compared with the result of the top command, the memory usage is basically the same, but the CPU usage percentage is completely different. This is because the target machine we tested has multiple CPUs. Therefore, the CPU usage calculated by the top command is the CPU usage used by the process, and the result of our program is the average usage of all CPUs. Of course, we can also get the same result as top, which requires other struct members (such as xcpu_utime [GLIBTOP_NCPU] and xcpu_stime [GLIBTOP_NCPU] In glibtop_proc_time) to use the same calculation method.

Therefore, using the libgtop library, we can more easily and flexibly obtain CPU and memory information.

 

CPU % (top)

Mem % (top)

Cpuusage % (procmonitor)

Memusage (procmonitor)

Process

16.9

0.7

4.26

0.7

2: 32. 41 mplayer

17.6

0.7

4.354

0.7

2: 32. 94 mplayer

16.9

0.7

4.341

0.7

2: 33. 45 mplayer

17.0

0.7

4.218

0.7

2: 33. 96 mplayer

17.9

0.7

4.281

0.7

2: 34. 50 mplayer

17.3

0.7

4.401

0.7

2: 35. 02 mplayer

7.3

0.7

4.233

0.7

2: 35. 54 mplayer

16.9

0.7

4.285

0.7

2: 36. 05 mplayer

17.6

0.7

4.38

0.7

2:36. 58 mplayer

17.3

0.7

4.324

0.7

2: 37. 10 mplayer

Related Article

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.