15 Network Optimization Part1 Essential Network Optimization Knowledge for Mobile Developers

15 Network Optimization Part1 Essential Network Optimization Knowledge for Mobile Developers #

In previous columns, we have already learned about file I/O and storage optimization. I believe you have mastered the performance analysis and optimization approaches for file I/O and storage. Today, let’s continue our journey and delve into another common type of I/O in the system: network I/O.

As I was writing this article, I recalled the thick book on “Computer Networks” that I studied during my university days. At that time, I found the subject quite perplexing, as there are so many aspects to network knowledge. As mobile developers, what essential network knowledge do we need to grasp? And how does file I/O differ from network I/O?

Today, we will set aside the “classics” and focus on solving the network problems faced by mobile developers.

Basic Knowledge of Networking #

Nowadays, it is difficult to find an application that does not require an internet connection. Even standalone applications have various network requests for data reporting, advertisements, and more. Since the internet is ubiquitous, what basic knowledge do we need to master?

1. Wireless Networks

In the past decade, the rapid growth of mobile internet has relied on the popularity of wireless networks. There are various types of wireless networks, each with its own characteristics and suitable usage scenarios.

The image below shows the wireless network types supported by the iPhone XS. You can see that WiFi, cellular networks, Bluetooth, and NFC are commonly used wireless network types in our daily lives.

Wireless Networks

“Gigabit LTE” refers to cellular networks that can achieve theoretical speeds up to 1Gbps (125MB/s), which is comparable to fiber optic speeds. Although based on the 4G standard, with technologies such as MIMO (Multiple-Input Multiple-Output) and LAA (Licensed Assisted Access) that utilize carrier aggregation, cellular networks have now developed into “Gigabit LTE” (source). In 2020, we are also about to usher in the commercialization of 5G, which has a theoretical transmission rate of 20Gbps. Currently, the standards for 5G have not been fully released yet. If you are interested in the principles of 5G, I recommend you read this article.

Gigabit LTE and 802.11ac

“802.11ac wireless network” refers to WiFi, which is commonly used. WiFi is defined and standardized by IEEE (Institute of Electrical and Electronics Engineers). Like any popular technology, IEEE has been actively releasing new protocols. Currently, the most commonly used standard is 802.11ac, which can ideally reach speeds of 866.7Mbps.

From a hardware perspective, all wireless networks are supported by baseband chips. Currently, Qualcomm has a significant advantage in the baseband chip field. Previously, due to patent disputes between Apple and Qualcomm, the iPhone XS used Intel’s baseband chips, but it also resulted in many user complaints about network connection issues.

With so many wireless network standards and specifications on the market, as well as various special features such as dual SIM dual standby, baseband chips have extremely high technical requirements. With the upcoming commercial use and popularization of 5G, a new wave of device upgrades is expected in China. This is both an opportunity and a challenge for major chip manufacturers. Currently, Qualcomm, MTK, and Huawei have all released 5G baseband chips. If you are interested in the current 5G landscape, you can read “Worldwide 5G Landscape”.

2. Link Turbo

New standards like 5G can greatly increase network speeds, but they require support from new base stations and mobile devices. This process will take at least a few years.

To improve user network experience, smartphone manufacturers also make various custom optimizations. The “Link Turbo network aggregation acceleration technology” recently introduced in the Honor V20 by Huawei is a more advanced “black technology” in this regard.

Link Turbo

From a hardware perspective, WiFi and cellular networks belong to different modules of the baseband chip. We can simply understand them as similar to having dual network cards. The so-called Link Turbo allows for the simultaneous use of both WiFi and mobile network for acceleration.

Some may wonder why they need to use expensive mobile networks when they are already connected to WiFi. Those who have this question probably have not experienced the frustration of playing “King of Glory” with poor WiFi, which can result in constantly losing connection. WiFi can be quite unstable for various reasons.

WiFi Issues

In fact, the technology of dual-channel communication is not pioneered by Huawei. Similar features like iPhone’s wireless network assistant, Xiaomi’s adaptive WLAN, and OnePlus’ adaptive WLAN can automatically switch to mobile networks when WiFi is unstable. The iPhone can still maintain a connection to the mobile network while connected to WiFi.

However, Link Turbo stands out in that it can transfer data simultaneously via both channels, supporting both TCP and UDP. TCP uses the open-source MultiPath TCP (introduced in iOS 7), while UDP utilizes Huawei’s self-developed MultiPath UDP.

Currently, this feature is relatively underutilized primarily due to the limited number of supported users, with only the V20 being the sole device supporting it, and it also requires users to manually enable it. Additionally, there are also modification costs involved, as dual-channel functionality requires some changes to be made to our backend servers to support it.

Link Turbo Benefits

However, this technology still has certain value. On one hand, data traffic has become cheaper, and many users no longer care as much about data fees. On the other hand, Huawei can directly partner with cloud service providers such as Aliyun, Huawei Cloud, Tencent Cloud, and CDN service providers to shield the modification costs for application backend servers. Currently, applications such as iQiyi, Douyu, and Yingke have attempted to integrate this functionality.

To get to this point, you may wonder why I am spending so much time talking about Link Turbo technology today. It’s not because I received any advertising fees, but because I have found that after optimizing a certain extent, it is difficult for an application to make significant breakthroughs on its own. At this point, it may be necessary to consider collaborating with smartphone manufacturers, chip manufacturers, or network operators. Therefore, we need to constantly pay attention to industry trends and have a clear understanding of the essence behind these new technologies.

Network I/O #

In the previous column, I discussed the processing flow of file I/O and the use cases of different I/O methods. Today, let’s take a look at the differences between network I/O and file I/O.

1. I/O Models

“In Linux, everything is a file.” The Linux kernel treats all external devices as files for operation. In network I/O, the system also has corresponding descriptors for reading and writing a socket, known as socket file descriptors (socket fd).

Taking the example of reading data from a socket using the recvfrom function, the entire I/O process can be divided into two stages:

  • Waiting for the socket data to be ready.

  • Copying the data from the kernel to the application process.

In “Unix Network Programming,” network I/O in Unix is divided into the following five models.

In the development process, commonly used models include blocking I/O, non-blocking I/O, and multiplexing I/O. For more information about Unix network I/O models, you can refer to Chapter 6 of “Unix Network Programming,” “An Introduction to Linux I/O Multiplexing,” and “Unix Network I/O Model and Linux’s I/O Multiplexing Model.”

When searching for information, I found that there are still some issues with the descriptions of many articles online, so we need to look at them dialectically.

  • Is multiplexing I/O always better than blocking I/O? Just like file I/O, the simplest way to achieve I/O concurrency is by using multiple threads and blocking I/O. If we have a large number of network connections active at the same time, using multiplexing I/O can improve performance. However, this assumption may not hold true for the client. For multiplexing I/O, the entire process involves a large number of system calls such as select/epoll, which may not be faster than blocking I/O.

  • Is epoll always better than select/poll? If the number of connections at the same time is very small, select’s performance may not be worse than epoll and may be better in many cases.

  • Does epoll use mmap to reduce the copy from the kernel to user space? Many online articles say that epoll uses mmap technology, but I have checked the implementation of epoll for Linux and Android and have not found any relevant implementations. Moreover, I personally don’t think it would be implemented in this way, as directly sharing memory may cause significant security vulnerabilities.

2. Data Processing

In the next issue, I will analyze the I/O models of some popular network libraries with you. Now, let’s dive deeper into the underlying process of sending and receiving packets.

Similar to file I/O, network I/O also uses interrupts. However, the interrupts in network I/O are more complex and involve both soft interrupts and hard interrupts. Hard interrupts notify the CPU that data has arrived, but this processing is very lightweight. Time-consuming operations are moved to the soft interrupt handler for slow processing. You can view the system soft interrupts in the /proc/softirqs file and the hard interrupts in the /proc/interrupts file.

There is not much information available online about the packet sending and receiving process of network cards. Interested students can refer to the following articles:

Considering that this topic is quite complex, I have provided reference materials in this column. Interested students can further delve into the topic.

Network Performance Evaluation #

What aspects of network performance do we usually optimize? Some students may focus on network bandwidth and server costs, especially for live streaming and video-based companies, where these costs are very high. Although trade-offs may be made at times, the speed and user experience are the consistent pursuit of all applications.

1. Latency and Bandwidth

If speed is the key, there are two main factors that have a decisive impact on network transmission speed:

  • Latency: The time it takes for data to travel from the source to the destination.

  • Bandwidth: The maximum throughput of the logical or physical communication path.

Recalling the complexity of file I/O performance evaluation, it at least takes place entirely within the mobile system. For the network, the entire process involves even more complex links. For a data packet to reach our server, it must go through wireless networks, core networks, and external networks (the Internet) starting from the mobile device.

What factors affect latency and bandwidth? There are also many factors involved, such as signal strength, the presence of base stations nearby, and the distance. It is also related to the network technology in use, whether it is 3G, 4G, or 5G, and the network congestion, such as whether it is in a large gathering place with tens of thousands of people.

The following are general reference values for bandwidth and latency of different network technologies. You can form a rough impression in your mind.

When these factors mentioned above occur, the network’s available bandwidth will be significantly reduced, and the latency will be greatly increased. The network scenario of high latency and low bandwidth is what we usually refer to as “weak network,” which has the following main characteristics:

Regarding how to optimize “weak networks,” I have devoted a lot of effort to optimizing for weak networks in WeChat, and this will be the focus of my next article. However, I want to say that even if 5G becomes widely available in the future, there will still be various influencing factors, and the optimization of weak networks will have long-term value.

On the other hand, the emphasis on latency and bandwidth may differ for different applications. For example, for live streaming applications or games like “Honor of Kings,” latency will be more important, while for Tencent Video, iQiyi, and other on-demand video applications, bandwidth will be more important. Network optimization needs to be considered in combination with the actual situation of your application.

2. Performance Measurement

Just like file I/O measurement, what methods can help us evaluate network performance?

For the network, we are concerned about the following indicators:

  • Throughput: The number of bytes received and transmitted per second by the network interface.

  • Latency: System call send/receive delay, connection delay, first packet delay, round-trip time, etc.

  • Number of Connections: Number of connections per second.

  • Errors: Packet loss count, timeouts, etc.

Linux provides many network performance analysis tools. The following tools are supported by Android and are relatively practical. For the complete functionality of these tools, please refer to their documentation or other online resources. It will not be elaborated here.

If you are more familiar with the underlying Linux system, you can directly check /proc/net, which contains files with various network statistical information. For example, Android’s TrafficState interface uses the files /proc/net/xt_qtaguid/stats and /proc/net/xt_qtaguid/iface_stat_fmt to monitor application traffic information.

Summary #

In the development of network communication, from 2G to 4G, it took more than a decade. Behind this, there are millions of base stations, hundreds of millions of routers, and various patents supporting it. Although network standards are constantly evolving, limited by infrastructure, its speed seems fast but also slow.

What should we think about for ourselves or applications? Network technologies like HTTP 2.0, HTTP 3.0 (QUIC), are constantly evolving. We need to persistently learn and consider the impact they can have on us, which is a “fast” thinking about the network. TCP and UDP protocols, weak networks, and many other things have not changed much in the past 20 years. The basic knowledge of networks is still very important to us, which is a “slow” thinking about the network.

Homework #

When I talked about Link Turbo before, I mentioned that the wireless network assistant on iPhones, as well as Xiaomi and OnePlus’ adaptive WLAN, automatically switch to mobile networks when detecting unstable WiFi. So, please think about how they detect and differentiate whether it is a problem with the application’s backend server or with the WiFi itself. Today’s homework is to write your thoughts on this issue in the comments section. Feel free to discuss with me and other classmates.

Today, I recommend a must-read book on networks: “Web性能权威指南” (The Definitive Guide to Web Performance). The first sentence in the book is excellent, and I want to share it with you: “Qualified developers know how to do it, while excellent developers know why it is done that way.”

For students who want to further explore this topic, you can study the following books:

  • “UNIX网络编程” (UNIX Network Programming)

  • “TCP/IP详解 卷1:协议” (TCP/IP Illustrated, Volume 1: The Protocols)

Feel free to click “Invite a friend to read” and share today’s content with your friends, inviting them to study together. Lastly, don’t forget to submit today’s homework in the comments section. I have prepared a generous “study encouragement package” for students who complete their homework diligently. Looking forward to discussing and progressing together with you.