mmapBuffer是linux环境下高性能缓存异步写入库,适用于大量、频繁数据写入的场景。其基于mmap和双向环形链表。线程安全且支持创建多个实例。有如下特性:
-
内存资源占用低,其采用mmap映射的内存作为缓冲区,对写入文件使用direct io从而避开系统的页缓存策略,内存占用大小完全可控。
-
缓存内容安全性高,mmap映射的缓存文件在遇到进程意外退出时,内容不会丢失。
-
可拓展,buffer由多个内存映射的缓冲区块组成,可动态添加缓存区块。
-
可以在程序中创建多个实例,通过缓存区名称唯一标识。
此项目受ringLog启发,用相关策略减少锁的竞争开销,大幅优化了性能。
五线程同时写入一千万条100字节数据,用时11.9044s。(同时使用了优化过后的时间戳获取函数),单缓存区块大小为40960000byte,全程最大使用了4个缓冲区块,内存仅占用了约160MB。