news 2026/6/12 9:43:45

流量监控前端不显示问题

作者头像

张小明

前端开发工程师

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/6/10 20:49:21

ACE-Step:开源音乐生成模型快速部署指南

ACE-Step&#xff1a;开源音乐生成模型快速部署指南 在 AI 创作工具不断进化的今天&#xff0c;我们正见证一个激动人心的转折点 —— 音乐创作不再是少数专业人士的专属领域。随着 ACE-Step 的横空出世&#xff0c;哪怕你不会五线谱、不懂和弦进行&#xff0c;也能通过一段文…

作者头像 李华
网站建设 2026/6/10 17:08:18

Java集合操作(List、Set、Map)

List元素有序//.add增List<Integer> intlist new ArrayList<>();intlist.add(12);intlist.add(99);intlist.add(88);intlist.add(77);intlist.add(55);//.remove 删intlist.remove(1);//删除对应索引的值如果List中是整形&#xff0c;在remove特定整形时用.remove…

作者头像 李华
网站建设 2026/6/11 7:08:39

Mybatis基础使用教程

什么是MyBatis?• MyBatis是⼀款优秀的 持久层 框架&#xff0c;⽤于简化JDBC的开发。• MyBatis本是 Apache的⼀个开源项⽬iBatis&#xff0c;2010年这个项⽬由apache迁移到了google code&#xff0c;并 且改名为MyBatis 。2013年11⽉迁移到Github.• 官⽹&#xff1a;MyBati…

作者头像 李华
网站建设 2026/6/10 18:01:36

弹论:为投资者打造稳定投资之路

在金融投资的世界里&#xff0c;投资者都渴望拥有一条稳定的投资之路&#xff0c;能够在市场的风浪中稳健前行。而弹论以其判断趋势、分区操作和避免频繁换手的优势&#xff0c;为投资者打造了这样一条稳定投资之路。弹论优势的全面阐述弹论是一种基于均线理论的创新交易方法&a…

作者头像 李华
网站建设 2026/6/10 3:06:36

小程序管理后台项目

GET https://cloud1-7g5siu5u6bae09ea.636c-cloud1-7g5siu5u6bae09ea-1333007326.cos.ap-shanghai.myqcloud.com/assets/images/1765853236705_318_%E5%90%8E%E7%AB%AF.png net::ERR_CERT_COMMON_NAME_INVALID各位大佬&#xff0c;使用云服务开发&#xff0c;使用云数据库&…

作者头像 李华
网站建设 2026/6/11 8:16:10

0.5B参数超越大模型:KaLM-Embedding-V2.5重塑轻量级标准

PyTorch-CUDA 基础镜像 v2.5&#xff1a;让开发者专注模型&#xff0c;让环境自己跑起来 你有没有经历过这样的场景&#xff1f;凌晨两点&#xff0c;实验马上要跑通&#xff0c;结果 pip install torch 卡在编译 cuDNN 的环节&#xff1b;或者刚在服务器上配置好环境&#xf…

作者头像 李华