2 Star 8 Fork 0

XMuli / lfxSpeed

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
speedinfo.cpp 8.95 KB
一键复制 编辑 原始数据 按行查看 历史
XMuli 提交于 2021-03-25 10:44 . feat: get all inter faces info
/*
* Copyright (c) 2020 xmuli
*
* Author: xmuli(偕臧) xmulitech@gmail.com
* GitHub: https://github.com/xmuli
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* */
#include "speedinfo.h"
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QStringList>
#include <QDebug>
#include <QThread>
#include <QtMath>
#include <QTimer>
#include <QTime>
#include <QDateTime>
#define PROC_PATH_UPTIME "/proc/uptime" // "系统启动" 和 "系统空闲" 的时间
#define PROC_PATH_CPU "/proc/stat" // "CPU" 使用率 的状态
#define PROC_PATH_MEM "/proc/meminfo" // "内存" 和 "交换空间" 的状态
#define PROC_PATH_NET "/proc/net/dev" // "网速" 下载和上传 的状态
#define PROC_PATH_DISK "/proc/diskstats" // "磁盘" 读取和写入 的状态
SpeedInfo::SpeedInfo(QObject *parent)
:QObject(parent)
{
}
/*!
* \brief SpeedInfo::cpuRate 获取某一次 CPU 的使用情况
* \param[out] cpuAll 总 cpu 使用量
* \param[out] cpuFree 空闲 cpu 的使用量
*/
void SpeedInfo::cpuRate(long &cpuAll, long &cpuFree)
{
cpuAll = cpuFree = 0;
bool ok = false;
QFile file(PROC_PATH_CPU);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream stream(&file);
QString line = stream.readLine();
if (!line.isNull()) {
QStringList list = line.split(QRegExp("\\s{1,}"));
for (auto v = list.begin() + 1; v != list.end(); ++v)
cpuAll += (*v).toLong(&ok);
cpuFree = list.at(4).toLong(&ok);
}
file.close();
}
/*!
* \brief SpeedInfo::memoryRate 获取 “内存” 和 “交换空间” 的某一时刻的使用情况
* \param memory 内存使用量
* \param memoryAll 内存总量
* \param swap 交换空间使用量
* \param swapAll 交换空间总量
*/
void SpeedInfo::memoryRate(long &memory, long &memoryAll, long &swap, long &swapAll)
{
memory = memoryAll = 0;
swap = swapAll = 0;
bool ok = false;
QFile file(PROC_PATH_MEM);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream stream(&file);
long buff[16] = {0};
for (int i = 0; i <= 15; ++i) {
QString line = stream.readLine();
QStringList list = line.split(QRegExp("\\s{1,}"));
buff[i] = list.at(1).toLong(&ok);
}
memoryAll = buff[0];
memory = buff[0] - buff[2];
swapAll = buff[14];
swap = buff[14] - buff[15];
file.close();
}
/*!
* \brief SpeedInfo::netRate 获取网某一时刻的网络总的数据包量
* \param[out] netUpload 网络上传数据量
* \param[out] netUpload 网络下载数据量
* \return 是否获取网络速率成功
*/
void SpeedInfo::netRate(long &netDown, long &netUpload)
{
QFile file(PROC_PATH_NET);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { // 在读取时,把行尾结束符修改为 '\n'; 在写入时,把行尾结束符修改为本地系统换行风格,比如Windows文本换行是 "\r\n"
qDebug()<<"\"/proc/net/dev\" don't open!";
return;
}
long down = 0;
long upload = 0;
QTextStream stream(&file);
QString line = stream.readLine();
line = stream.readLine();
line = stream.readLine();
while (!line.isNull()) {
line = line.trimmed();
QStringList list = line.split(QRegExp("\\s{1,}")); // 匹配任意 大于等于1个的 空白字符
if (!list.isEmpty()) {
down = list.at(1).toLong();
upload = list.at(9).toLong();
}
netDown += down;
netUpload += upload;
line = stream.readLine();
}
file.close();
}
/*!
* \brief SpeedInfo::setRateUnitSensitive 设置速率单位的大小写模式
* \param[in] unit 传入进来的网速单位
* \param sensitive 设置的速率单位大小写模式
* \return 速率单位字符串
*/
QString SpeedInfo::setRateUnitSensitive(RateUnit unit, Sensitive sensitive)
{
switch (sensitive) {
case Sensitive::Default: {
switch (unit) {
case RateUnit::RateBit:
return QString("b/s");
case RateUnit::RateByte:
return QString("B/s");
case RateUnit::RateKb:
return QString("Kb/s");
case RateUnit::RateMb:
return QString("Mb/s");
case RateUnit::RateGb:
return QString("Gb/s");
case RateUnit::RateTb:
return QString("Tb/s");
default:
qDebug()<<QString("Sensitive::Default, RateUnit is RateUnknow.");
return QString("");
}
}
case Sensitive::Upper: {
switch (unit) {
case RateUnit::RateBit:
return QString("BIT/S");
case RateUnit::RateByte:
return QString("B/S");
case RateUnit::RateKb:
return QString("KB/S");
case RateUnit::RateMb:
return QString("MB/S");
case RateUnit::RateGb:
return QString("GB/S");
case RateUnit::RateTb:
return QString("TB/S");
default:
qDebug()<<QString("Sensitive::Upper, RateUnit is RateUnknow.");
return QString("");
}
}
case Sensitive::Lower: {
switch (unit) {
case RateUnit::RateBit:
return QString("bit/s");
case RateUnit::RateByte:
return QString("b/s");
case RateUnit::RateKb:
return QString("kb/s");
case RateUnit::RateMb:
return QString("mb/s");
case RateUnit::RateGb:
return QString("gb/s");
case RateUnit::RateTb:
return QString("tb/s");
default:
qDebug()<<QString("Sensitive::Lower, RateUnit is RateUnknow.");
return QString("");
}
}
default: {
qDebug()<<QString("Sensitive is RateUnknow.");
return QString("");
}
}
}
/*!
* \brief SpeedInfo::autoRateUnits 自动显示单位
* \param[in] speed 传入的网速(无单位)
* \param[out] unit 智能调节后的网速的单位
* \param sensitive 速率单位的大小写模式
* \return 自能调节单位后的速率
*/
double SpeedInfo::autoRateUnits(long speed, SpeedInfo::RateUnit &unit)
{
/* 自动判断合适的速率单位,默认传进来的是 Byte
* bit 0 ~ 7 位 (不到 1 字节)
* Byte 1 ~ 2^10 Byte
* KB 2^10 ~ 2^20 Byte
* MB 2^20 ~ 2^30 Byte
* GB 2^30 ~ 2^40 Byte
* TB 2^40 ~ 2^50 Byte
*/
if (unit != SpeedInfo::RateByte) {
qDebug()<<"请先将单位转为字节(byte)后再传参";
return -1;
}
double sp = 0;
if (0 <= speed && speed < qPow(2, 10)) {
unit = SpeedInfo::RateByte;
sp = speed;
} else if (qPow(2, 10) <= speed && speed < qPow(2, 20)) {
unit = SpeedInfo::RateKb;
sp = static_cast<double>(speed / qPow(2, 10) * 1.0);
} else if (qPow(2, 20) <= speed && speed < qPow(2, 30)) {
unit = SpeedInfo::RateMb;
sp = static_cast<double>(speed / qPow(2, 20) * 1.0);
} else if (qPow(2, 30) <= speed && speed < qPow(2, 40)) {
unit = SpeedInfo::RateGb;
sp = static_cast<double>(speed / qPow(2, 30) * 1.0);
} else if (qPow(2, 40) <= speed && speed < qPow(2, 50)) {
unit = SpeedInfo::RateTb;
sp = static_cast<double>(speed / qPow(2, 40) * 1.0);
} else {
unit = SpeedInfo::RateUnknow;
qDebug()<<"本设备网络速率单位传输超过 TB, 或者低于 0 Byte.";
sp = -1;
}
return sp;
}
void SpeedInfo::uptime(double &run, double &idle)
{
run = 0;
idle = 0;
QFile file(PROC_PATH_UPTIME);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug()<<"\"/proc/uptime\" don't open!";
return;
}
QTextStream stream(&file);
QString line = stream.readLine();
QStringList list = line.split(QRegExp("\\s{1,}")); // 匹配任意 大于等于1个的 空白字符
if (!list.isEmpty()) {
run = list.at(0).toDouble();
idle = list.at(1).toDouble();
}
file.close();
}
QString SpeedInfo::autoTimeUnits(double s)
{
int time = qFloor(s);
int ss = time % 60;
int MM = (time % 3600) / 60;
int hh = (time % 86400) / 3600;
int dd = time / 86400;
QString runTime = QString(tr("系统已运行: %1天, %2:%3:%4"))
.arg(dd, 0, 'f', 0, QLatin1Char(' '))
.arg(hh, 2, 'f', 0, QLatin1Char('0'))
.arg(MM, 2, 'f', 0, QLatin1Char('0'))
.arg(ss, 2, 'f', 0, QLatin1Char('0'));
return runTime;
}
1
https://gitee.com/XMuli/lfxSpeed.git
git@gitee.com:XMuli/lfxSpeed.git
XMuli
lfxSpeed
lfxSpeed
master

搜索帮助