-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspeedinfo.cpp
296 lines (266 loc) · 8.95 KB
/
speedinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (c) 2020 xmuli
*
* Author: xmuli(偕臧) [email protected]
* 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;
}