news 2026/5/11 21:42:00

流量监控前端不显示问题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
流量监控前端不显示问题

现象描述

前端界面显示上下行速率有问题,明明有流量,有时候是0有时候有数字。

通过接口定位到以下代码:

.h文件

#ifndef __TRAN_MONITOR__ #define __TRANS_MONITOR__ #include "hv/HttpServer.h" int NetSpeedMonitor_Init(const std::string &usbname); hv::Json UpDownloadGet(); #endif

Cpp文件:

#include <iostream> #include <stdio.h> #include <fstream> #include <thread> #include <chrono> #include <sys/stat.h> #include "Comport/TransMonitor.h" #include "hv/hlog.h" #include "hv/HttpServer.h" using namespace std; string upload_speed = "0 bytes/s"; // ❗❗❗❗❗❗❗❗❗❗后台线程不停写,HTTP线程不停读,没有mutex,没有atomic,没有内存屏障 string download_speed = "0 bytes/s"; // ❗❗❗❗❗❗❗❗❗❗导致:读到旧值,空字符串,读到一半的内容 static bool directoryExists(const string& path) { struct stat info; return stat(path.c_str(), &info) == 0 && S_ISDIR(info.st_mode); } static void readNetworkStatistics(const string &usbname) { const string basePath = "/sys/class/net/" + usbname + "/statistics/"; uint64_t prevRxBytes = 0, prevTxBytes = 0; while(true) { if (directoryExists(basePath)) { ifstream rxFile(basePath + "rx_bytes"); ifstream txFile(basePath + "tx_bytes"); uint64_t rxBytes = 0, txBytes = 0; char upspeed[32]; char downspeed[32]; if (rxFile >> rxBytes && txFile >> txBytes) { uint64_t rxDiff = rxBytes - prevRxBytes; // ❗❗❗❗❗❗❗计算的不是速率,只是差值❗❗❗❗❗❗❗❗❗❗ uint64_t txDiff = txBytes - prevTxBytes; //❗❗❗❗❗❗❗第一次diff极大,之后立刻归零❗❗❗❗❗❗❗ string rxUnit = "bytes/s", txUnit = "bytes/s"; double rxValue = rxDiff, txValue = txDiff; if (rxDiff > 1024) { // ❗❗❗❗❗❗❗❗❗❗1.两个 if 都会进l,前面的 KB 计算被后面的 MB 覆盖,逻辑可读性极差,不工程化 rxValue = static_cast<double>(rxDiff) / 1024; rxUnit = "KB/s"; } if (txDiff > 1024) { txValue = static_cast<double>(txDiff) / 1024; txUnit = "KB/s"; } if (rxDiff > 1024 * 1024) { // 2 rxValue = static_cast<double>(rxDiff) / (1024 * 1024); rxUnit = "MB/s"; } if (txDiff > 1024 * 1024) { txValue = static_cast<double>(txDiff) / (1024 * 1024); txUnit = "MB/s"; } if(txValue < 0) txValue = 0; if(rxValue < 0) rxValue = 0; sprintf(upspeed, "%.2f %s", txValue, txUnit.c_str()); sprintf(downspeed, "%.2f %s", rxValue, rxUnit.c_str()); upload_speed = upspeed; download_speed = downspeed; prevRxBytes = rxBytes; prevTxBytes = txBytes; } else { hloge("Read error at network transfer speed"); } rxFile.close(); txFile.close(); } else { sleep(5); } sleep(1); // ❗❗❗❗❗❗❗❗❗❗Linux sleep(1):≥1 秒 } } int NetSpeedMonitor_Init(const std::string &usbname) { thread networkThread(readNetworkStatistics, usbname); networkThread.detach(); return 0; } hv::Json UpDownloadGet() { hv::Json j; j["upstream"] = upload_speed; // ❗❗❗❗❗❗❗❗❗❗后端不建议返回字符串速率 j["downstream"] = download_speed; return j; }

看代码后问题显而易见:

只有在以下条件同时满足时,才会看到非 0:

  • 网卡此刻刚好有流量

  • 线程刚好按近似 1 秒跑

  • HTTP 线程刚好没读到写一半的 string

在demo里能跑,但是在生产环境必出问题。

解决问题

#include <iostream> #include <stdio.h> #include <fstream> #include <thread> #include <chrono> #include <sys/stat.h> #include "Comport/TransMonitor.h" #include "hv/hlog.h" #include "hv/HttpServer.h" #include <atomic> using namespace std; static std::atomic<uint64_t> g_rx_bps{0}; static std::atomic<uint64_t> g_tx_bps{0}; static void readNetworkStatistics(const string& ifname) { const string base = "/sys/class/net/" + ifname + "/statistics/"; uint64_t prevRx = 0, prevTx = 0; auto prevTime = chrono::steady_clock::now(); while (true) { ifstream rx(base + "rx_bytes"); ifstream tx(base + "tx_bytes"); uint64_t rxBytes = 0, txBytes = 0; if (!(rx >> rxBytes && tx >> txBytes)) { sleep(1); continue; } auto now = chrono::steady_clock::now(); double seconds = chrono::duration_cast<chrono::milliseconds>(now - prevTime).count() / 1000.0; if (prevRx != 0 && seconds > 0.2) { g_rx_bps = (rxBytes - prevRx) / seconds; g_tx_bps = (txBytes - prevTx) / seconds; } prevRx = rxBytes; prevTx = txBytes; prevTime = now; sleep(1); } } int NetSpeedMonitor_Init(const std::string &usbname) { thread networkThread(readNetworkStatistics, usbname); networkThread.detach(); return 0; } static std::string formatSpeed(uint64_t bps) { char buf[32]; if (bps < 1024) { snprintf(buf, sizeof(buf), "%lu B/s", bps); } else if (bps < 1024 * 1024) { snprintf(buf, sizeof(buf), "%.2f KB/s", bps / 1024.0); } else { snprintf(buf, sizeof(buf), "%.2f MB/s", bps / (1024.0 * 1024.0)); } return std::string(buf); } hv::Json UpDownloadGet() { uint64_t tx = g_tx_bps.load(); uint64_t rx = g_rx_bps.load(); hv::Json j; j["upstream"] = formatSpeed(tx); j["downstream"] = formatSpeed(rx); return j; }

1.atomic<uint64_t>存速率,避免了 string 竞争,HTTP线程读安全,后台线程安全

static std::atomic<uint64_t> g_rx_bps{0}; static std::atomic<uint64_t> g_tx_bps{0};

2.steady_clock+ 时间差算速率,不依赖sleep(1)精度,线程调度抖动不会把速率压成 0

double seconds = chrono::duration_cast<chrono::milliseconds>(now - prevTime).count() / 1000.0;

3.第一次采样不算速率,避免第一次巨大 diff,避免刚启动速率乱跳

if (prevRx != 0 && seconds > 0.2) {
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/3 18:20:35

JVET-AH0086

&#x1f4c4; 提案名称&#xff1a;EE2-2.9c: Enhanced Extrapolation-based Intra Prediction with Bias and Clipping in EIP提案来源&#xff1a; 文件编号&#xff1a;JVET-AH0086-v1作者单位&#xff1a;OPPO 等技术背景&#xff1a;基于已有 EIP&#xff08;Extrapolati…

作者头像 李华
网站建设 2026/5/11 18:51:41

Qwen3-14B-Base:148亿参数重塑开源模型效率

Qwen3-14B-Base&#xff1a;148亿参数重塑开源模型效率 在大模型的军备竞赛逐渐退潮之际&#xff0c;一个更务实的问题浮出水面&#xff1a;我们真的需要千亿参数来解决大多数企业级任务吗&#xff1f; 阿里巴巴通义千问团队的答案清晰而有力——不需要。他们推出的 Qwen3-14B-…

作者头像 李华
网站建设 2026/5/11 2:54:28

星巴克、库迪等大牌点餐如何对接api接口?

对接星巴克和库迪这样的大牌点餐API接口&#xff0c;通常涉及到一系列的步骤&#xff0c;包括选择合适的API平台、资质申请、技术开发、测试上线等。以下是基于给定搜索结果的详细对接流程&#xff1a;1. 选择合适的点餐API平台首先&#xff0c;您需要选择一个合适的点餐API平台…

作者头像 李华
网站建设 2026/5/11 6:31:50

v3学生成绩管理系统

源码可s领取!!V3 学生成绩管理系统是一款专门为教育机构、学校打造的综合性成绩管理平台。它致力于简化成绩管理流程&#xff0c;提高教学管理效率&#xff0c;为学校的教师、学生和管理人员提供便捷的成绩管理与查询服务。该系统基于先进的技术架构&#xff0c;具备丰富且实用…

作者头像 李华
网站建设 2026/5/9 13:19:26

蓝牙电话-acceptCall-调用流程

BluetoothHeadsetClient.java acceptCall 调用流程及日志分析 1. 完整的 acceptCall 调用流程 1.1 调用时序图 应用层 (App)↓ 1. 调用 acceptCall() 框架层 (BluetoothHeadsetClient.java)↓ 2. 通过Binder IPC 蓝牙服务进程 (HeadsetClientService)↓ 3. 状态机处理 HeadsetC…

作者头像 李华
网站建设 2026/5/8 5:33:58

OpenAI gpt-oss-20b支持13万token长上下文

OpenAI gpt-oss-20b支持13万token长上下文 [【免费下载链接】gpt-oss-20b gpt-oss-20b —— 基于OpenAI开源权重的轻量级高性能语言模型&#xff08;21B参数&#xff0c;3.6B活跃参数&#xff09; 项目地址: https://ai.gitcode.com/hf_mirrors/openai/gpt-oss-20b](https://…

作者头像 李华