Top command causes system load increase

Source: Internet
Author: User

Top command causes system load increase

Linux: CentOS 6.5 x86_64

I. Problem Performance

As the system load increases, the cpu resources of a core are exhausted by the top process, as shown in:

Ii. analysis process

1. Run the strace command to print the top process information. A large number of repeated system calls occur, as shown below:

View the fd information of the top process as follows:

It is inferred from the above information that the input and output of the top process is abnormal and the select statement is triggered to return the file descriptor. However, if the file descriptor is not processed or improperly processed, the select statement is triggered infinitely and falls into an endless loop, cpu resources that occupy one core.

2. Download The procps-3.2.8 to view the top source code, found the relevant call in line 3,398th. As shown in the Code, the standard input is placed in the select call. Due to a standard input error, select is triggered to return fd without waiting for TV duration.

3.

Long file_flags;
Int rc;
Char c;
Fd_set fs;
FD_ZERO (& fs );
FD_SET (STDIN_FILENO, & fs );
File_flags = fcntl (STDIN_FILENO, F_GETFL );
If (file_flags =-1) file_flags = 0;
Fcntl (STDIN_FILENO, F_SETFL, O_NONBLOCK | file_flags );

// Check 1st, in case TV zeroed (by sig handler) before it got set
Rc = chin (0, & c, 1 );
If (rc <= 0 ){
// EOF is pretty much a "can't happen" handle t for a kernel bug.
// We shoshould quickly die via SIGHUP, and thus not spin here.
// If (rc = 0) end_pgm (0);/* EOF from terminal */
Fcntl (STDIN_FILENO, F_SETFL, file_flags );
Select (1, & fs, NULL, NULL, & TV );
Fcntl (STDIN_FILENO, F_SETFL, O_NONBLOCK | file_flags );
}
If (chin (0, & c, 1)> 0 ){
Fcntl (STDIN_FILENO, F_SETFL, file_flags );
Do_key (unsigned) c );
} Else {
Fcntl (STDIN_FILENO, F_SETFL, file_flags );
}

4. The standard input processing code is as follows:

Static int chin (int ech, char * buf, unsigned cnt)
{
Int rc;

Fflush (stdout );
If (! Ech)
Rc = read (STDIN_FILENO, buf, cnt );
Else {
Tcsetattr (STDIN_FILENO, TCSAFLUSH, & Savedtty );
Rc = read (STDIN_FILENO, buf, cnt );
Tcsetattr (STDIN_FILENO, TCSAFLUSH, & Rawtty );
}
// May be the beginning of a lengthy escape sequence
Tcflush (STDIN_FILENO, TCIFLUSH );
Return rc; // note: we do NOT produce a vaid 'string'
}

5. rc is the read return value. Three types of values are returned: Positive bytes, 0 (reaching the end of the file), and negative value-1 (indicating an error ). If rc = 0 is injected in the source code (for example), the standard input fd is not processed, and the select timeout is not effective.

6. A standard input exception occurs during the running of top. This is usually difficult. The top process is printed through ps-fe as follows:

The pid of The sudo process is changed to 1. The following information is captured through ssh-t 'strace-o sudo. strace sudo-I:

After the terminal is shut down abnormally (disconnected from the Network), the parent process is set to 1 to continue running.

7. after testing the login in ssh-t 'sudo-I 'mode, the parent process of sudo is sshd. When the terminal closes abnormally, sudo changes its parent process to 1 to continue running. After normal login, sudo runs. The parent process of sudo is usually bash (or other shells), and similar situations will not occur.

Iii. Summary

Many operations in the online environment need to be switched to the root. To facilitate ssh Login, the-t 'sudo-I 'parameter is added, resulting in a tragedy. A shortcut is always good, but you must be careful with the trap. If you are not careful, it may lead to a big mistake.

Note:

1. setsid top or other daemon processes call top. When the terminal exits abnormally and causes a standard input error, the top exception will occur.

2. This problem does not exist in Ubuntu, and debian has fixed this problem.

Nicai @ bugaosuni :~ /Procps-3.2.8/debian/patches $ cat top_stdin_eof.patch
Description: Check for stdin eof if term
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Bug-Debian: http://bugs.debian.org/458986
Reviewed-by: Craig Small <csmall@debian.org>
Index: B/top. c
========================================================== ======================================
--- A/top. c 2009-11-24 21:00:34. 000000000 + 1100
++ B/top. c 2009-11-24 21:00:35. 000000000 + 1100
@-3408, 9 + 3408 @@
// Check 1st, in case TV zeroed (by sig handler) before it got set
Rc = chin (0, & c, 1 );
If (rc <= 0 ){
-// EOF is pretty much a "can't happen" handle t for a kernel bug.
-// We shoshould quickly die via SIGHUP, and thus not spin here.
-// If (rc = 0) end_pgm (0);/* EOF from terminal */
+ If (rc = 0) end_pgm (0);/* EOF from terminal, may happen if top
+ * Erroneously gets detached from it .*/
Fcntl (STDIN_FILENO, F_SETFL, file_flags );
Select (1, & fs, NULL, NULL, & TV );
Fcntl (STDIN_FILENO, F_SETFL, O_NONBLOCK | file_flags );
Nicai @ bugaosuni :~ /Procps-3.2.8/debian/patches $

Practical top Command

Why does Linux's htop command win the top command?

Use the most powerful process monitor htop in Ubuntu

Install htop process management and monitoring tools in CentOS 6.5

This article permanently updates the link address:

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.