天天看点

用tmpfs平滑磁盘io需求

在解决某个服务端程序问题时,有些数据需要做些即时的保存。当时采取了直接写文件的方式。经过大量的实际使用后发现,直接在磁盘写文件在用户量大的时候,很容易碰到磁盘io繁忙的问题。考虑通过在内存中建立一个tmpfs的方式来平滑磁盘io需求。

流程简述:

memory -> tmpfs -> disk

其中 tmpfs也是在内存里面的,所以由memory->tmpfs其实就是一个类似memory的copy过程,但实际比memory copy需要更多的时间。

tmpfs->disk采取定期同步机制。这样就可以平滑掉很大比例的写文件操作了。

实际测试:

条件:10000次循环,每次分别写入256,512,1024,4096,102400这5种大小的数据内容。

memory->tmpfs 耗时:45秒

memory->disk   耗时:143秒

采用memory -> tmpfs -> disk机制

在每隔10次memory->tmpfs写操作之后,写一次tmpfs->disk机制,耗时130秒

在每隔20次memory->tmpfs写操作之后,写一次tmpfs->disk机制,耗时89秒

主要的同步语句:

1. remove null file

find src_location -type f -empty -print -exec rm {} /;

2. sync tmpfs to disk

find src_location -type f -newer flag_file -print -exec cp {} dest_location /;