fix: 计数器回绕/重置时钳制负速度为0
网卡重启、/proc/net/dev 计数器溢出或接口重置时,字节差值为负, QML 会显示负速度。对 rxDelta/txDelta 钳制为 0 避免此问题。
This commit is contained in:
@@ -13,8 +13,9 @@
|
|||||||
~~`calculateSpeed()` 直接用 `currentRxBytes - m_lastRxBytes` 作为 bytes/sec,未除以实际流逝时间。系统负载高或定时器抖动时,速度值会失真。~~
|
~~`calculateSpeed()` 直接用 `currentRxBytes - m_lastRxBytes` 作为 bytes/sec,未除以实际流逝时间。系统负载高或定时器抖动时,速度值会失真。~~
|
||||||
已改为记录上次采样的毫秒时间戳 `m_lastTimestampMs`,按真实流逝时间 `delta_bytes / elapsed_seconds` 计算速度;速度存储由 `qint64` 改为 `double` 保留精度;`elapsedSec > 0` 守卫避免除零。
|
已改为记录上次采样的毫秒时间戳 `m_lastTimestampMs`,按真实流逝时间 `delta_bytes / elapsed_seconds` 计算速度;速度存储由 `qint64` 改为 `double` 保留精度;`elapsedSec > 0` 守卫避免除零。
|
||||||
|
|
||||||
**2. 计数器回绕/重置产生负速度**
|
**2. ~~计数器回绕/重置产生负速度~~ ✅ 已修复**
|
||||||
网卡重启、`/proc/net/dev` 计数器溢出或接口重置时,`currentRxBytes - m_lastRxBytes` 可能为负。代码无任何兜底,QML 会显示负速度。应加 `if (delta < 0) delta = 0;` 保护。
|
~~网卡重启、`/proc/net/dev` 计数器溢出或接口重置时,`currentRxBytes - m_lastRxBytes` 可能为负。代码无任何兜底,QML 会显示负速度。~~
|
||||||
|
已在 `calculateSpeed()` 中对 `rxDelta` / `txDelta` 钳制为 0,计数器回绕/接口重置/网卡重启时不再产生负速度。
|
||||||
|
|
||||||
**3. "总量统计"语义有误导**
|
**3. "总量统计"语义有误导**
|
||||||
`m_totalDownload = currentRxBytes` 只存储活动接口的当前计数器值,并非真正的累计流量。它会在以下场景跳变:切换网卡(跳到新接口计数器)、网卡重启(归零)、系统重启(归零)。README 宣称"累计上传/下载流量统计",但实际不是跨重启/跨接口的累计。
|
`m_totalDownload = currentRxBytes` 只存储活动接口的当前计数器值,并非真正的累计流量。它会在以下场景跳变:切换网卡(跳到新接口计数器)、网卡重启(归零)、系统重启(归零)。README 宣称"累计上传/下载流量统计",但实际不是跨重启/跨接口的累计。
|
||||||
|
|||||||
@@ -378,8 +378,12 @@ void NetworkMonitorApplet::calculateSpeed()
|
|||||||
|
|
||||||
// 仅当间隔合法时计算速度,避免除零;间隔为 0 时保持上次速度值
|
// 仅当间隔合法时计算速度,避免除零;间隔为 0 时保持上次速度值
|
||||||
if (elapsedSec > 0) {
|
if (elapsedSec > 0) {
|
||||||
m_downloadSpeed = (currentRxBytes - m_lastRxBytes) / elapsedSec;
|
// 计数器回绕/接口重置时差值可能为负,钳制为 0 避免显示负速度
|
||||||
m_uploadSpeed = (currentTxBytes - m_lastTxBytes) / elapsedSec;
|
// 触发场景:网卡重启、/proc/net/dev 计数器溢出、USB 网卡拔出重插
|
||||||
|
const qint64 rxDelta = currentRxBytes - m_lastRxBytes;
|
||||||
|
const qint64 txDelta = currentTxBytes - m_lastTxBytes;
|
||||||
|
m_downloadSpeed = (rxDelta > 0 ? rxDelta : 0) / elapsedSec;
|
||||||
|
m_uploadSpeed = (txDelta > 0 ? txDelta : 0) / elapsedSec;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_lastRxBytes = currentRxBytes;
|
m_lastRxBytes = currentRxBytes;
|
||||||
|
|||||||
Reference in New Issue
Block a user