How to Use strace to diagnose problems

Source: Internet
Author: User

How to Use strace to diagnose problems

Early in the year, if you knew there was a strace command, it would be awesome. Now everyone knows about strace. If you encounter performance problems and ask for help, we recommend that you use strace to hook up and see it. However, when you see the characters on the screen, you cannot see the characters on the screen. This article uses a simple case to show you some practices for diagnosing problems with strace.

The following real cases are inevitable if there are similarities! Let's look at the top result of a High-load Server:

Top

Tip: when running top, press 「1"Open the CPU list and press 「shift+p"Is sorted by CPU.

In this example, it is easy to find that the CPU is mainly occupied by several PHP processes, and the PHP process occupies a large amount of memory. However, the system memory balance still exists and SWAP is not serious, this is not the cause of the problem.

However, we can see in the CPU list that the CPU is mainly consumed in the kernel state "sy", rather than the user State "us", which is inconsistent with our experience. In Linux, there are many tools used to track program behavior. "strace" is used to track kernel-state function calls, and "ltrace" is used to track user-state function calls 」, so here we should use strace 」:

  1. shell>strace-p <PID>

However, if you use strace to track a process directly, it is often the character that waits for you to roll over the screen. It is not easy to see the crux of the problem from here, fortunately, the strace can be summarized by Operation Time:

  1. shell>strace-cp<PID>

Click 「cThis option is used to summarize the total time consumed by each operation. The result after running is roughly as shown in:

Strace-cp

Obviously, we can see that the CPU is mainly consumed by clone operations. You can also track the clone separately:

  1. shell>strace-T -e clone-p <PID>

You can use the "T" option to obtain the actual time consumed by the operation. You can use the "e" option to track an operation:

Strace-T-e clone-p

Obviously, a clone operation takes several hundred milliseconds. For the meaning of clone, refer to the man documentation:

clone() creates a new process, in a manner similar to fork(2). It is actually a library function layered on top of the underlying clone() system call, hereinafter referred to as sys_clone. A description of sys_clone is given towards the end of this page.

Unlike fork(2), these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. (Note that on this manual page, “calling process” normally corresponds to “parent process”. But see the description of CLONE_PARENT below.)

Simply put, it is to create a new process. So when will such system calls occur in PHP? The exec function is displayed in the query Business Code. The following command is used to verify that the exec function does cause clone system calls:

  1. shell>strace-eclone php -r 'exec("ls");'

Finally, let's take another question: if we use strace to track a process, there will be very few output results. Does it mean that the process is idle? In fact, if you try ltrace, you may find it strange. Remember that there are kernel and user States.

Linux strace commands

Linux strace trace Process Information

Introduction to the strace command in Linux

Linux strace CIDR Block troubleshooting Error

Linux Process Control-strace: Tracing signals and system calls

Strace, a process analysis tool for program execution in Linux

This article permanently updates the link address:

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.