Hello everyone, I am the Information Guy~
In the pursuit of extreme performance in server development, how to reduce the number of data copies and lower CPU usage is an eternal topic. Today, I will introduce the combination of <span><span>mmap</span></span>
and <span><span>socket</span></span>
, which is a powerful tool to address this pain point, and has been effectively utilized in recent projects.
1. Basics of mmap
What is mmap?<span><span>mmap</span></span>
(Memory Mapped Files) is a technique that maps files directly into the process’s memory space. With it, we can read and write files as if we were manipulating memory, avoiding multiple data copies associated with traditional <span><span>read/write</span></span>
operations.
Core advantages of mmap• Zero-copy: Data is shared directly between the kernel buffer and user space without copying• Efficient random access: Directly manipulate any position in the file via pointers• Lazy loading: Load file content on demand, saving memory
C Language Example Code
int fd = open("data.txt", O_RDONLY);
char *mapped = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
// Now you can access the file content directly through the mapped pointer
printf("First byte: %c\n", *mapped);
munmap(mapped, file_size);
2. Bottlenecks of Traditional Socket Transmission
First, let’s look at a typical data sending process:
- Disk file → Kernel buffer (read system call)
- Kernel buffer → User buffer (data copy)
- User buffer → Socket kernel buffer (write system call)
- Network card sends data
This process involves 2 data copies and 4 context switches, which is particularly detrimental to performance when handling large files.
3. The mmap + socket Combination
Optimization Idea: Eliminate user space data transfer through memory mapping
Optimized Process:
- Disk file → Memory mapped area (mmap)
- Memory mapped area → Socket buffer (send system call)
- Network card sends data
Although there is still 1 kernel data copy, compared to the traditional method:✅ Reduces 1 data copy✅ Reduces 2 context switches✅ Lowers memory usage
C Language Implementation Example
// Establish memory mapping
int fd = open("large_file.dat", O_RDONLY);
struct stat sb;
fstat(fd, &sb);
char *mapped = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
// Send data
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
// ... Connection establishment code ...
send(sockfd, mapped, sb.st_size, 0);
// Clean up resources
munmap(mapped, sb.st_size);
close(fd);
4. Performance Comparison Test
We conducted transmission tests using a 1GB file:
Method | Time (ms) | CPU Usage |
---|---|---|
Traditional Method | 1250 | 35% |
mmap + socket | 850 | 22% |
Although sendfile has the best performance, the mmap solution strikes a better balance between flexibility and compatibility.Thus, this method is more suitable for large file transfer services (videos, image files), high-frequency log collection systems, or real-time data acquisition applications.However, there are a few points to note:
- The size of the mapped area must be a multiple of the page size
- Call
<span><span>msync</span></span>
in a timely manner to ensure data persistence - Error handling must consider the SIGSEGV signal
Through the clever combination of <span><span>mmap</span></span>
and <span><span>socket</span></span>
, we can enhance the performance of the transmission process by over 30%. This method has been widely applied in many systems, being simple and efficient~
Finally
I have collected some embedded learning materials, reply with 【1024】 in the public account to find the download link!
Recommended Articles Click the blue text to jump
☞ Collection | Comprehensive Programming of Linux Applications
☞ Collection | Learn Some Networking Knowledge
☞ Collection | Handwritten C Language
☞ Collection | Handwritten C++ Language
☞ Collection | Experience Sharing
☞ Collection | From Microcontrollers to Linux
☞ Collection | Power Control Technology
☞ Collection | Essential Mathematics for Embedded Systems
☞ Collection | MCU Advanced Collection
☞ Collection | Advanced Embedded C Language
☞ Collection | Experience Sharing