The top command in CentOS causes the system load to rise.

Source: Internet
Author: User

The top command in CentOS causes the system load to rise.
System Environment: CentOS 6.5x86 _ 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. longfile_flags;intrc;charc;fd_setfs;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);//check1st,incasetvzeroed(bysighandler)beforeitgotsetrc=chin(0,&c,1);if(rc<=0){//EOFisprettymucha"can'thappen"exceptforakernelbug.//WeshouldquicklydieviaSIGHUP,andthusnotspinhere.//if(rc==0)end_pgm(0);/*EOFfromterminal*/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:

  5. staticintchin(intech,char*buf,unsignedcnt){intrc;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);}//maybethebeginningofalengthyescapesequencetcflush(STDIN_FILENO,TCIFLUSH);returnrc;//note:wedoNOTproduceavaid'string'}
  6. 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.

  7. The standard input exception occurs during the running of top, which 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.

  8. 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. Top is called by setsid top or other daemon. When the terminal exits abnormally and causes a standard input error, top is abnormal.

  2. This problem does not exist in ubuntu, and debian fixes this problem.

nicai@bugaosuni:~/procps-3.2.8/debian/patches$cattop_stdin_eof.patch Description:Check for stdineof if term Author:SamuelThibault<samuel.thibault@ens-lyon.org> Bug-Debian:http: //bugs.debian.org/458986 Reviewed-by:CraigSmall<csmall@debian.org> Index:b/top.c =================================================================== ---a/top.c 2009-11-2421:00:34.000000000+1100 +++b/top.c 2009-11-2421:00:35.000000000+1100 @@-3408,9+3408,8@@ //check1st,incasetvzeroed(bysighandler)beforeitgotset rc=chin(0,&c,1); if (rc<=0){ - //EOFisprettymucha"can'thappen"exceptforakernelbug. - //WeshouldquicklydieviaSIGHUP,andthusnotspinhere. - //if(rc==0)end_pgm(0);/*EOFfromterminal*/ + if (rc==0)end_pgm(0); /*EOFfromterminal,mayhappeniftop +*erroneouslygetsdetachedfromit.*/ 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$

This article is from the "left_left" blog, please be sure to keep this source http://linux521.blog.51cto.com/4099846/1579890

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.