Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


相关
#2225
我在排查磁盘健康寿命的时候,发现TrafficMonitor存在频繁写入磁盘的问题,虽然是30s写入一次,但是全量写入的机制,实际上不友好的。
提交目的
解决
history_traffic.dat文件频繁写入问题,通过引入增量保存机制,在保持数据安全性的前提下,大幅减少磁盘I/O操作。细节说明
1. 数据结构重构
(目的是减少代码的复杂度,不用每次都更新数组,直接链表进行维护)
m_today_traffic: 单独存储今天的记录(频繁更新)m_history_traffics: 历史记录链表(按日期从大到小排序,较少更新)m_traffics_cache: 缓存合并后的完整列表(按需更新)2. 新增增量保存机制
(这是我加入的核心功能,可以有效减少磁盘写入频率,原先的全量保存是写一点就触发一次缓冲区同步,对磁盘IO来说是很不友好的)
SaveTodayOnly()3. 保存策略优化
(原本是30s && 100kB 触发一次保存,现在增加更大的30s&&10MB)
频繁保存场景(流量变化超过10MB时):
SaveHistoryTraffic()→SaveTodayOnly()进行增量保存程序退出/系统关机场景:
SaveHistoryTrafficFull()→Save()进行完整保存4. 缓存机制
(简化代码复杂度)
m_cache_dirty标志,实现按需更新缓存GetTraffics()方法在缓存过期时自动更新5. 数据一致性保障
(简化代码复杂度)
IsTodayRecord()方法检查今天的记录日期是否正确OnDateChanged()将今天的记录移到历史记录MormalizeData()方法确保数据排序和去重影响分析
性能显著提升
磁盘寿命延长
数据安全性保持
文件格式兼容性
Load()方法保持向后兼容,能正确解析旧格式文件并发访问
文件损坏恢复
.bak文件)数据丢失风险