Use tcprstat to investigate service response time and source code analysis

Source: Internet
Author: User
Tags percona

1. Introduction to tcprstat

Tcprstat is a tool developed by percona to investigate mysql response time. It can also be used to investigate typical request-response services. The time consumed by gameserver of yujian was recorded using this tool some time ago.

 

Details:

Http://www.percona.com/docs/wiki/tcprstat:start

Download:

Binary:

Http://github.com/downloads/Lowercases/tcprstat/tcprstat-static.v0.3.1.x86_64

 

Source code download

Http://bazaar.launchpad.net /~ Ignacio-nin/tcprstat/trunk/tarball/73? Start_revid = 73

 

Ii. Use

You can run the program directly to obtain the result (suse10sp2 kernel version is not enough, tlinux can), or you can analyze the result by reading the tcpdump cap file.

1. Direct use (Official example ):

# Tcprstat-p 3306-t 1-n 5
Timestamp count max min avg med stddev 95_max 95_avg 95_std 99_max 99_avg 99_std
1283261499 1870 559009 39 883 153 13306 1267 201 150 6792 323
1283261500 1865 25704 578 142 2755 889 175 107 23630 333 1331
1283261501 1887 26908 33 583 148 2761 714 176 23391 339 1340
1283261502 2015 304965 35 624 151 7204 564 171 8615 237 507
1283261503 1650 289087 35 462 146 7133 834 184 120 3565 244

2. Read the tcpdump File

Tcprstat-l IP-p port-r tcpdump. pcap

 

Iii. Principles

Like tcpdump, tcprstat also uses the libpcap library to capture packets. If tcprstat is not available, you can use a program to analyze the tcpdump packet capture files, which is troublesome. The general principle is as follows.

 

1. Capture packets through tcpdump

/Usr/sbin/tcpdumphost IP and tcp port-I eth1-s 0-w tcpdump. pcap

2. Determine a tcp Stream through ip: port pairs (socket is essentially an ip address port quad-tuples). The same work can be achieved through wireshark's Follow Tcp Stream function.

3. Extract the tcp packet with data (data> 0), distinguish it based on the socket stream, and calculate the time difference between each socket outbound package and inbound package.

4. Calculate a large amount of time consumption, such as average value, maximum value, and minimum value...

 

The general logic can also be seen from the tcprstat source code.

Rough process of source code (tcprstat analyzes the offline package)

1. tcprstat. c

Main function (if the parameter is r, read the tcpdump package for analysis)

Call offline_capture to analyze offline packages

2. capture. c

The offline_capture function calls pcap_loop to capture packets for analysis. process_packet is a callback function used to process packets.

3. process-packet.c

The process_ip function distinguishes the incoming and outgoing packets based on the source ip address and destination ip address of the ip package.

Only data> 0 packets are taken. The incoming and outgoing packets are processed separately. Note that the outgoing packets are exchanged between the source ip address and the destination ip address, to make the package correspond to the outgoing package.

4. stat. c

The inbound function puts the incoming packet into the hash table by using the socket (local ip, local port, destination ip, and destination port) as the key.

The outbound function uses the socket of the outbound packet (note that the source address of the outbound packet has been changed to the destination address) as the key, check whether the corresponding package exists in the hash table (the searched package is an inbound package), calculate the time difference between the package and the inbound package (Server Response Time), and record it.

5. Make statistics on the obtained records according to certain methods and print the results.

Some column functions in Stat. c (stats_avg, stats_sum, stats_max ...)

 

4. Online documents

Http://rdc.taobao.com/blog/cs? P = 728

 

 

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.