) Linux tool strace

Source: Internet
Author: User
Tags add time htons

Introduction: This is a detailed page of strace, a tool for Linux. It introduces the knowledge, skills, and experience related to strace, a tool for PHP, nginx, CGI, PHP, and Linux, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 347704 'rolling = 'no'>
Original: http://www.perfgeeks.com /? P = 501

Today, strace is a good thing.

Strace is often used to track system calls and received signals during process execution. In the Linux World, processes cannot directly access hardware devices. When a process needs to access hardware devices (such as reading disk files and receiving network data, you must switch from user mode to kernel mode to access hardware devices through system calls. Strace can trace the system calls generated by a process, including parameters, return values, and execution time.

Strace usage parameters

-P: tracks the specified process

-F trace is called by the Fork sub-Process System

-F tries to track the system call of the vfork sub-process, and when the system appears at the same time as-F, vfork is not tracked

-O filename: by default, strace outputs the result to stdout. You can use-O to write the output to the filename file.

-FF is often used with the-O option. system calls generated by different processes (sub-processes) are output to the filename. PID file.

-R: print the relative time of each system call.

-T add time information before each row in the output. -The tt time is determined in microseconds. You can also use-TTT to print relative time

-V outputs all system calls. By default, system calls that are frequently called are not output.

-S specifies the length of the output string for each row. The default value is 32. All file names are output

-C: count the time, number of calls, and number of errors of each system call.

-E expr output filter. You can use expressions to filter out the output that you do not want.

Application scenarios

#1. Track your web server system calls

System Call optimization is also an important direction for Web performance optimization, especially for I/O-Intensive Web applications. The test environment here is centos5.4 + nginx + FastCGI.

<? PHP

// File: Hello. php

Define ('document _ root', dirname (_ file __));

Include ("Hello. Inc ");

Include ("./Hello. Inc ");

Include (document_root. "/Hello. Inc ");

?>

# Strace-f-o strace_nginx strace/wwwchroot/nginx/sbin/nginx-C/wwwchroot/nginx. conf

... (Some unimportant data affects typographical layout, which should be replaced)

// -- Accept HTTP requests from the client

4165 Recv (16, "Get/Hello. php HTTP/1.1 \ r \ nhost: F"..., 32768, 0) = 391

4165 epoll_ctl (9, epoll_ctl_mod, 16, {epollin | epollout | epollet, {u32 = 3081162952, u64 = 698098541354471624}) = 0

// -- Perform DNS search

4165 getsockname (16, {sa_family = af_inet, sin_port = htons (80), sin_addr = inet_addr ("222.73.211.214")}, [16]) = 0

// -- Create a new socket and connect to fast-CGI. The port number is 9000.

4165 socket (pf_inet, sock_stream, ipproto_ip) = 17

4165 IOCTL (17, fionbio, [1]) = 0

4165 epoll_ctl (9, epoll_ctl_add, 17, {epollin | epollout | epollet, {u32 = 3081163048, u64 = 697886249710965032}) = 0

4165 connect (17, {sa_family = af_inet, sin_port = htons (9000), sin_addr = inet_addr ("127.0.0.1")}, 16) =-1)

4165 epoll_wait (9, {epollout, {u32 = 3081163048, u64 = 697886249710965032 }}, {...}, 5 \

12,300 000) = 2

4165 gettimeofday ({1295420285,130 967}, null) = 0

4165 Recv (16, 0xbfdd7d8b, 1, msg_peek) =-1 eagain (resource temporarily unavailable)

4165 getsockopt (17, sol_socket, so_error, [0], [4]) = 0

// -- Send the HTTP request to fast-CGI

4165 writev (17, [{"\ 1 \ 1 \ 0 \ 1 \ 0 \ 10 \ 0 \ 0 \ 0 \ 1 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 1 \ 4 \ 0 \ 1 \ 3 \ 30 \ 0 \ 0 \ 21 \ 7 running wa "..., 832}], 1) = 832

4165 epoll_wait (9, {epollin | epollout, {u32 = 3081163048, u64 = 697886249710965032 }}, 512,300 000) = 1

4165 gettimeofday ({1295420285,131 559}, null) = 0

// -- Receives the fast-CGI response

4165 Recv (17, "\ 1 \ 6 \ 0 \ 1 \ 0v \ 2 \ 0x-powered-by: PHP/5.2.10"..., 65536, 0) = 112

4165 readv (17, [{"\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 "..., 65424}], 1) = 0

4165 mmap2 (null, 274432, prot_read | prot_write, map_private | map_anonymous,-1, 0) = 0xb7514000

4165 close (17) = 0

4165 munmap (0xb7514000, 274432) = 0

// -- Response to the client's HTTP request, that is, the HTTP Response

4165 writev (16, [{"HTTP/1.1 200 OK \ r \ nserver: nginx/0 "..., 228 },{ "22 \ r \ n", 4 },..., 5) = 273

4165 write (5, "116.66.34.82--[19/Jan/201"..., 191) = 191

4165 setsockopt (16, sol_tcp, tcp_nodelay, [1], 4) = 0

4165 Recv (16, 0x9b024e8, 32768, 0) =-1 eagain (resource temporarily unavailable)

...

Through this, we can only get a general idea that epoll is enabled in nginx. You can also learn how nginx and fast-CGI work at the underlying layer. Strange: There are three inclue In the hello. php file, that is, three files are loaded. The corresponding I/O logic operations are not shown here. Why? This is because nginx did not parse and process the PHP script, but handed it to fast-CGI to do this.

# Strace-f-o PHP-CGI-strace/wwwchroot/PHP/bin/PHP-CGI -- FPM-config/wwwchroot/PHP/etc/php-fpm.conf

// -- Receives requests from nginx

4510 <... accept resumed> {sa_family = af_inet, sin_port = htons (35983), sin_addr = inet_addr ("127.0.0.1")}, [16]) = 3

4510 clock_gettime (clock_monotonic, {22638545,869 965681}) = 0

4510 poll ([{FD = 3, events = Pollin}], 1, 5000) = 1 ([{FD = 3, revents = Pollin}])

4510 read (3, "\ 1 \ 1 \ 0 \ 1 \ 0 \ 10 \ 0 \ 0 ",
= 8

4510 read (3, "\ 0 \ 1 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 ",
= 8

4510 read (3, "\ 1 \ 4 \ 0 \ 1 \ 0035 \ 3 \ 0 ",
= 8

4510 read (3, "\ 21 \ 7gateway_interfacecgi/1.1 \ 17 \ 5 serv"..., 824) = 824

4510 read (3, "\ 1 \ 4 \ 0 \ 1 \ 0 \ 0 \ 0 \ 0 ",
= 8

4510 time (null) = 1295425149

// -- Load the request resource file hello. php

4510 lstat64 ("/Var", {st_mode = s_ifdir | 0755, st_size = 4096,...}) = 0

4510 lstat64 ("/var/www", {st_mode = s_ifdir | 0755, st_size = 4096,...}) = 0

4510 lstat64 ("/var/www/EP", {st_mode = s_ifdir | 0755, st_size = 4096,...}) = 0

4510 lstat64 ("/var/www/EP/Hello. php", {st_mode = s_ifreg | 0644, st_size = 119,...}) = 0

4510 clock_gettime (clock_monotonic, {22638545,870 893872}) = 0

4510 setitimer (itimer_prof, {it_interval = {0, 0}, it_value = {60, 0}, null) = 0

4510 rt_sigaction (sigprof, {0x835c120, [Prof], sa_restart}, {sig_dfl, [], 0 },
= 0

4510 rt_sigprocmask (sig_unblock, [Prof], null,
= 0

4510 time (null) = 1295425149

4510 open ("/var/www/EP/Hello. php", o_rdonly) = 4

4510 fstat64 (4, {st_mode = s_ifreg | 0644, st_size = 119,...}) = 0

4510 time (null) = 1295425149

4510 chdir ("/var/www/EP") = 0

4510 fstat64 (4, {st_mode = s_ifreg | 0644, st_size = 119,...}) = 0

4510 mmap2 (null, 4096, prot_read | prot_write, map_private | map_anonymous,-1, 0) = 0xb7fe7000

4510 read (4, "\ n", 8192) = 29

4510 read (4, "", 8192) = 0

4510 read (4, "", 8192) = 0

4510 close (4) = 0

// -- Load hello. Inc, corresponding to the PHP Code include './Hello. inc'

4510 getcwd ("/var/www/EP"..., 4096) = 12

4510 time (null) = 1295425149

4510 open ("/var/www/EP/Hello. Inc", o_rdonly) = 4

4510 fstat64 (4, {st_mode = s_ifreg | 0644, st_size = 29,...}) = 0

4510 read (4, "\ n", 8192) = 29

4510 read (4, "", 8192) = 0

4510 read (4, "", 8192) = 0

4510 close (4) = 0

4510 time (null) = 1295425149

// -- Load hello. Inc, corresponding to the PHP Code include document_root. '/Hello. inc'

4510 lstat64 ("/Var", {st_mode = s_ifdir | 0755, st_size = 4096,...}) = 0

4510 lstat64 ("/var/www", {st_mode = s_ifdir | 0755, st_size = 4096,...}) = 0

4510 lstat64 ("/var/www/EP", {st_mode = s_ifdir | 0755, st_size = 4096,...}) = 0

4510 lstat64 ("/var/www/EP/Hello. Inc", {st_mode = s_ifreg | 0644, st_size = 29,...}) = 0

4510 open ("/var/www/EP/Hello. Inc", o_rdonly) = 4

4510 fstat64 (4, {st_mode = s_ifreg | 0644, st_size = 29,...}) = 0

4510 read (4, "\ n", 8192) = 29

4510 read (4, "", 8192) = 0

4510 read (4, "", 8192) = 0

4510 close (4) = 0

// -- Output the response to nginx and disable the connection

4510 write (3, "\ 1 \ 6 \ 0 \ 1 \ 0v \ 2 \ 0x-powered-by: PHP/5.2.10"..., 96) = 96

4510 setitimer (itimer_prof, {it_interval = {0, 0}, it_value = {0, 0}, null) = 0

4510 write (3, "\ 1 \ 3 \ 0 \ 1 \ 0 \ 10 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0ere", 16) = 16

4510 Shutdown (3, 1/* Send */) = 0

4510 Recv (3, "\ 1 \ 5 \ 0 \ 1 \ 0 \ 0 \ 0", 8, 0) = 8

4510 Recv (3, "", 8, 0) = 0

4510 close (3) = 0

By tracking PHP-CGI, we can know that, compared with the other two methods, include './Hello. inc' has the highest performance. The strace output is truncated. If you need to see more output, you can use the-s option to let strace output more content.

When you find that an HTTP request causes a sudden increase in CPU usage, you can track the root cause of the problem through strace. You can also use strace-C statistics to monitor whether your optimization has taken effect.

#2. MySQL execution Statement List

When an HTTP request occurs, you often want to obtain the number of database select operations performed on the HTTP request and whether the select operation is completed in the same MySQL connection. Here, we take access to this page as an example and use strace to track these MySQL SELECT query statements.

//-9514 is the process Number of mysqld. To view the entire SQL statement, we want to output more content through-s 1024.

# Strace-f-FF-O strace-mysqld-s 1024-P 9514

# Find.-Name "strace-mysqld *"-type F-print | xargs grep-n "Select. * from WP _"

./Strace-mysqld.19203: 64:

Read (19, "\ 3 select option_name, option_value from wp_options where autoload = 'Yes'", 72) = 72

./Strace-mysqld.19203: 165:

Read (19, "\ 3 select * From wp_users where user_login = 'admin'", 50) = 50

./Strace-mysqld.19203: 184:

Read (19, "\ 3 select meta_key, meta_value from wp_usermeta where user_id = 1", 63) = 63

./Strace-mysqld.19203: 295:

Read (19, "\ 3 select option_value from wp_options where option_name = 'rewrite _ rules' limit 1", 80) = 80

./Strace-mysqld.19203: 311:

Read (19, "\ 3 select wp_posts. * From wp_posts where 1 = 1 and wp_posts.id = 501

And wp_posts.post_type = 'post' order by wp_posts.post_date DESC ", 136) = 136

... (This saves some time)

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/347704.html pageno: 3

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.