Improve Linux system performance and accelerate network applications

Source: Internet
Author: User
Tags telnet program
Article Title: Improving Linux system performance to accelerate network applications. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

When developing a socket application, the first task is to ensure reliability and meet specific requirements. With the four tips given in this article, you can design and develop a socket program for optimal performance from the beginning. This article includes the use of Sockets APIs, two socket options that can improve performance, and GNU/Linux optimization.

Follow these skills to develop applications with superior performance:

Minimize the delay of message transmission.

Minimize the load of system calls.

Adjusts the TCP window for the Bandwidth Delay Product.

Dynamic Optimization of the GNU/Linux TCP/IP stack.

  Tip 1. Minimize packet transmission latency

When communication is performed through TCP socket, data is split into data blocks so that they can be encapsulated into the TCP payload (the payload in the TCP packet) of the given connection. The size of TCP payload depends on several factors (such as the maximum message length and Path), but these factors are known when the connection is initiated. To achieve the best performance, we aim to fill each packet with as much available data as possible. When there is not enough data to fill in payload (also known as the maximum segment length (maximum segment size) or MSS ), TCP uses the Nagle algorithm to automatically connect some small buffers to a packet segment. In this way, the application efficiency can be improved by minimizing the number of sent packets, and the overall network congestion problem can be reduced.

Although John Nagle's algorithm can connect the data to a larger packet to minimize the number of sent packets, sometimes you may want to send only a smaller packet. A simple example is the telnet program, which allows users to interact with a remote system. This is usually done through a shell. If a user is required to fill a segment with characters entered before sending the packet, this method cannot meet our needs.

Another example is the HTTP protocol. Generally, the client browser generates a small request (an HTTP request message), and the Web server returns a larger response (Web page ).

Solution

The first thing you should consider is that the Nagle algorithm meets a requirement. Because this algorithm combines data and tries to form a complete TCP packet segment, it introduces some latency. However, this algorithm can minimize the number of packets sent online and thus minimize network congestion.

However, the Sockets API provides a solution to minimize the transmission latency. To disable the Nagle algorithm, you can set the TCP_NODELAY socket option, as shown in Listing 1.

Int sock, flag, ret;/* Create new stream socket */sock = socket (AF_INET, SOCK_STREAM, 0);/* Disable the Nagle (TCP No Delay) algorithm */flag = 1; ret = setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, (char *) & flag, sizeof (flag); if (ret =-1) {printf ("Couldn't setsockopt (TCP_NODELAY) \ n"); exit (-1 );}

Listing 1. Disable the Nagle Algorithm for TCP socket

Tip: the Samba experiment indicates that®Windows®When reading data on the Samba drive on the server, disabling the Nagle algorithm can almost double the read performance.

  Tip 2. Minimize the load of system calls

When you use a socket to read and write data, you are using a system call ). This call (such as read or write) spans the boundaries between the user space application and the kernel. In addition, before entering the kernel, your call will use the C library to enter a general function (system_call () in the kernel ()). From system_call (), this call enters the file system layer, where the kernel determines the type of device being processed. Finally, the call enters the socket layer, where data is read or queued for transmission through the socket (this involves data copies ).

This process indicates that the system call is not only performed in the application and kernel, but also through many layers in the application and kernel. This process consumes a lot of resources, so the more calls, the longer the time required to work through this call chain, the lower the performance of the application.

Since we cannot avoid these system calls, the only choice is to minimize the number of times these calls are used. Fortunately, we can control this process.

Solution

When writing data to a socket, try to write all the data at a time, instead of performing multiple write operations. For read operations, it is best to pass in the maximum buffer that can be supported, because if there is not enough data, the kernel will also try to fill the entire buffer (and also need to keep the TCP Notification window open ). In this way, you can minimize the number of calls and achieve better overall performance.

 

[1] [2] [3] Next page

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.