利用Linux文件系统内存cache来提高性能

利用Linux文件系统内存cache来提高性能

本地磁盘文件->socket发送,4步骤数据流向:

  • hard driver -> kernel space ---- [DMA copy]
  • kernel space -> user space ---- [CPU copy]
  • user space -> kernel space ---- [CPU copy]
  • kernel space -> protocol engine ---- [DMA copy] 

左右图的上部分为user和kernel的上下文切换。下部分为数据流向图,其中左图为正常流程中Linux从文件系统读取文件然后通过socket发送的流程,其中共经历了4次内存拷贝;右图为改进版本,通过mmap内存映射将文件映射进内存,绕开了user space和kernel space的二次拷贝,实现zero copy(针对user space)。

//正常读写方式
read(file, tmp_buf, len);
write(socket, tmp_buf, len);   

//mmap读写方式
tmp_buf = mmap(file, len);
write(socket, tmp_buf, len);

猜你喜欢

转载自www.cnblogs.com/diegodu/p/9230830.html