cpu占用打印
This commit is contained in:
parent
93281511bd
commit
fea0ba6a69
Binary file not shown.
@ -121,6 +121,7 @@ struct MemoryStorage
|
|||||||
struct CpuOccupyInfo
|
struct CpuOccupyInfo
|
||||||
{
|
{
|
||||||
double idle = 0.0;
|
double idle = 0.0;
|
||||||
|
double iowait = 0.0;
|
||||||
double user = 0.0;
|
double user = 0.0;
|
||||||
double sys = 0.0;
|
double sys = 0.0;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -74,22 +74,26 @@
|
|||||||
user.LowPart = userTimes.dwLowDateTime;
|
user.LowPart = userTimes.dwLowDateTime;
|
||||||
user.HighPart = userTimes.dwHighDateTime;
|
user.HighPart = userTimes.dwHighDateTime;
|
||||||
|
|
||||||
unsigned long long _total = kernel.QuadPart + user.QuadPart;
|
unsigned long long _total = kernel.QuadPart + user.QuadPart;
|
||||||
unsigned long long _idle = idle.QuadPart;
|
unsigned long long _idle = idle.QuadPart;
|
||||||
unsigned long long _user = user.QuadPart;
|
unsigned long long _iowait = 0;
|
||||||
|
unsigned long long _user = user.QuadPart;
|
||||||
|
|
||||||
static unsigned long long prevTotal = 0, prevIdle = 0, prevUser = 0;
|
static unsigned long long prevTotal = 0, prevIdle = 0, prevIowait = 0, prevUser = 0;
|
||||||
unsigned long long __total = _total - prevTotal;
|
unsigned long long __total = _total - prevTotal;
|
||||||
unsigned long long __idle = _idle - prevIdle;
|
unsigned long long __idle = _idle - prevIdle;
|
||||||
unsigned long long __user = _user - prevUser;
|
unsigned long long __iowait = _iowait - prevIowait;
|
||||||
prevTotal = _total;
|
unsigned long long __user = _user - prevUser;
|
||||||
prevIdle = _idle;
|
prevTotal = _total;
|
||||||
prevUser = _user;
|
prevIdle = _idle;
|
||||||
|
prevIowait = _iowait;
|
||||||
|
prevUser = _user;
|
||||||
if(__total > 0)
|
if(__total > 0)
|
||||||
{
|
{
|
||||||
occupy->idle = (__idle) * 100.0 / __total;
|
occupy->idle = (__idle) * 100.0 / __total;
|
||||||
occupy->user = (__user) * 100.0 / __total;
|
occupy->iowait = (__iowait) * 100.0 / __total;
|
||||||
occupy->sys = (__total - __idle - __user) * 100.0 / __total;
|
occupy->user = (__user) * 100.0 / __total;
|
||||||
|
occupy->sys = (__total - __idle - __user) * 100.0 / __total;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -187,36 +191,76 @@
|
|||||||
|
|
||||||
bool __sdk_cpu_occupy__(struct CpuOccupyInfo* occupy)
|
bool __sdk_cpu_occupy__(struct CpuOccupyInfo* occupy)
|
||||||
{
|
{
|
||||||
std::ifstream procStat("/proc/stat");
|
|
||||||
//procStat.ignore(5, ' '); //跳过'cpu'前缀
|
|
||||||
//std::stringstream buffer;
|
|
||||||
//buffer << procStat.rdbuf();
|
|
||||||
//std::string str = buffer.str();
|
|
||||||
std::string line;
|
std::string line;
|
||||||
std::getline(procStat, line); // 读取第一行,通常是CPU统计信息
|
bool root = false; //root用户权限
|
||||||
std::istringstream buffer(line); // 将字符串流转换为输入流
|
if(root)
|
||||||
procStat.close();
|
{
|
||||||
|
std::ifstream procStat("/proc/stat");
|
||||||
|
if(!procStat.is_open())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//procStat.ignore(5, ' '); //跳过'cpu'前缀
|
||||||
|
//std::stringstream buffer;
|
||||||
|
//buffer << procStat.rdbuf();
|
||||||
|
//std::string str = buffer.str();
|
||||||
|
std::getline(procStat, line); // 读取第一行,通常是CPU统计信息
|
||||||
|
procStat.close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
FILE* pipe = ::popen("cat /proc/stat", "r"); //执行命令并打开管道
|
||||||
|
if(!pipe)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//逐行读取输出
|
||||||
|
char buf[1024] = {0};
|
||||||
|
if(::fgets(buf, sizeof(buf), pipe) == NULL)
|
||||||
|
{
|
||||||
|
::pclose(pipe); //关闭管道
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
::pclose(pipe); //关闭管道
|
||||||
|
line = buf;
|
||||||
|
|
||||||
|
// std::list<std::string> data;
|
||||||
|
// while(::fgets(buf, sizeof(buf), pipe) != nullptr)
|
||||||
|
// {
|
||||||
|
// line = buf;
|
||||||
|
// memset(buf,0,sizeof(buf));
|
||||||
|
// if(line.substr(0,3) != "cpu") { break; }
|
||||||
|
// if(line.back() == '\n') { line.pop_back(); }
|
||||||
|
// data.push_back(line);
|
||||||
|
// }
|
||||||
|
// ::pclose(pipe); // 关闭管道
|
||||||
|
}
|
||||||
|
|
||||||
|
std::istringstream sis(line); // 将字符串流转换为输入流
|
||||||
std::string key;
|
std::string key;
|
||||||
unsigned long long user=0, nice=0, sys=0, idle=0, iowait=0, irq=0, softirq=0, steal=0, guest=0, guest_nice=0;
|
unsigned long long user=0, nice=0, sys=0, idle=0, iowait=0, irq=0, softirq=0, steal=0, guest=0, guest_nice=0;
|
||||||
buffer >> key >> user >> nice >> sys >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice;
|
sis >> key >> user >> nice >> sys >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice;
|
||||||
|
|
||||||
unsigned long long _total = user + nice + sys + idle + iowait + irq + softirq + steal + guest + guest_nice;
|
unsigned long long _total = user + nice + sys + idle + iowait + irq + softirq + steal + guest + guest_nice;
|
||||||
unsigned long long _idle = idle + iowait;
|
unsigned long long _idle = idle;
|
||||||
unsigned long long _user = user;
|
unsigned long long _iowait = iowait;
|
||||||
|
unsigned long long _user = user;
|
||||||
|
|
||||||
static unsigned long long prevTotal = 0, prevIdle = 0, prevUser = 0;
|
static unsigned long long prevTotal = 0, prevIdle = 0, prevIowait = 0, prevUser = 0;
|
||||||
unsigned long long __total = _total - prevTotal;
|
unsigned long long __total = _total - prevTotal;
|
||||||
unsigned long long __idle = _idle - prevIdle;
|
unsigned long long __idle = _idle - prevIdle;
|
||||||
unsigned long long __user = _user - prevUser;
|
unsigned long long __iowait = _iowait - prevIowait;
|
||||||
prevTotal = _total;
|
unsigned long long __user = _user - prevUser;
|
||||||
prevIdle = _idle;
|
prevTotal = _total;
|
||||||
prevUser = _user;
|
prevIdle = _idle;
|
||||||
|
prevIowait = _iowait;
|
||||||
|
prevUser = _user;
|
||||||
if(__total > 0)
|
if(__total > 0)
|
||||||
{
|
{
|
||||||
occupy->idle = (__idle) * 100.0 / __total;
|
occupy->idle = (__idle) * 100.0 / __total;
|
||||||
occupy->user = (__user) * 100.0 / __total;
|
occupy->iowait = (__iowait) * 100.0 / __total;
|
||||||
occupy->sys = (__total - __idle - __user) * 100.0 / __total;
|
occupy->user = (__user) * 100.0 / __total;
|
||||||
|
occupy->sys = (__total - __idle - __user) * 100.0 / __total;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -266,18 +266,19 @@ const char* ExamService::examPeerOccupy()
|
|||||||
logdebug("call examPeerOccupy.");
|
logdebug("call examPeerOccupy.");
|
||||||
//if(!m_init) return QE(errorInitNot);
|
//if(!m_init) return QE(errorInitNot);
|
||||||
|
|
||||||
TOccupyInfo occ;
|
|
||||||
|
|
||||||
CpuOccupyInfo cpu;
|
CpuOccupyInfo cpu;
|
||||||
MemoryStorage memo;
|
|
||||||
DiskStorage disk;
|
|
||||||
|
|
||||||
Tools::peerOccupyCpu(cpu);
|
Tools::peerOccupyCpu(cpu);
|
||||||
|
|
||||||
|
MemoryStorage memo;
|
||||||
Tools::peerOccupyMemory(memo, MB);
|
Tools::peerOccupyMemory(memo, MB);
|
||||||
|
|
||||||
|
DiskStorage disk;
|
||||||
Tools::peerOccupyDisk("./", disk, MB);
|
Tools::peerOccupyDisk("./", disk, MB);
|
||||||
|
|
||||||
|
TOccupyInfo occ;
|
||||||
occ.cpu.cores = Tools::getCpuProcessors();
|
occ.cpu.cores = Tools::getCpuProcessors();
|
||||||
occ.cpu.idle = std::round(cpu.idle);
|
occ.cpu.idle = std::round(cpu.idle);
|
||||||
|
occ.cpu.iowait = std::round(cpu.iowait);
|
||||||
occ.cpu.user = std::round(cpu.user);
|
occ.cpu.user = std::round(cpu.user);
|
||||||
occ.cpu.sys = std::round(cpu.sys);
|
occ.cpu.sys = std::round(cpu.sys);
|
||||||
|
|
||||||
|
|||||||
@ -31,7 +31,6 @@ public:
|
|||||||
virtual const char* examSecretDecrypt(const char* data, int size) = 0;
|
virtual const char* examSecretDecrypt(const char* data, int size) = 0;
|
||||||
virtual int examFreeMemory(const char* buf) = 0;
|
virtual int examFreeMemory(const char* buf) = 0;
|
||||||
|
|
||||||
|
|
||||||
virtual void examJudgeCallbackLogToCaller(int level, const char* info, int len) = 0;
|
virtual void examJudgeCallbackLogToCaller(int level, const char* info, int len) = 0;
|
||||||
virtual void examJudgeCallbackRealExamToCaller(const char* data, int len) = 0;
|
virtual void examJudgeCallbackRealExamToCaller(const char* data, int len) = 0;
|
||||||
virtual void examJudgeCallbackPerformToCaller(const char* data, int len) = 0;
|
virtual void examJudgeCallbackPerformToCaller(const char* data, int len) = 0;
|
||||||
|
|||||||
@ -1936,12 +1936,13 @@ struct TRadarInfo
|
|||||||
|
|
||||||
struct TCpuOccupy
|
struct TCpuOccupy
|
||||||
{
|
{
|
||||||
int32 cores = 0;
|
int32 cores = 0;
|
||||||
int32 idle = 0;
|
int32 idle = 0;
|
||||||
int32 user = 0;
|
int32 iowait = 0;
|
||||||
int32 sys = 0;
|
int32 user = 0;
|
||||||
|
int32 sys = 0;
|
||||||
|
|
||||||
JUDGE_JSON_DEFINE(TCpuOccupy,cores,idle,user,sys);
|
JUDGE_JSON_DEFINE(TCpuOccupy,cores,idle,iowait,user,sys);
|
||||||
|
|
||||||
TCpuOccupy() noexcept { clean(); }
|
TCpuOccupy() noexcept { clean(); }
|
||||||
virtual ~TCpuOccupy() noexcept { clean(); }
|
virtual ~TCpuOccupy() noexcept { clean(); }
|
||||||
@ -1949,6 +1950,7 @@ struct TCpuOccupy
|
|||||||
{
|
{
|
||||||
cores = 0;
|
cores = 0;
|
||||||
idle = 0;
|
idle = 0;
|
||||||
|
iowait = 0;
|
||||||
user = 0;
|
user = 0;
|
||||||
sys = 0;
|
sys = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@ import {
|
|||||||
import { GlobalConfig } from '../../config/index'
|
import { GlobalConfig } from '../../config/index'
|
||||||
import testNapi from '@ohos.hiserialsdk'
|
import testNapi from '@ohos.hiserialsdk'
|
||||||
import fs from '@ohos.file.fs';
|
import fs from '@ohos.file.fs';
|
||||||
|
import util from '@ohos.util'
|
||||||
|
|
||||||
let num = 0
|
let num = 0
|
||||||
|
|
||||||
@ -300,45 +301,54 @@ function openChuankouFn(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getChuankouFnMsg() {
|
function getChuankouFnMsg() {
|
||||||
let timeout = 50000; // 2秒超时
|
testNapi.SerialRecvAsync(globalThis.fd, 5000, (revTestInfo) => {
|
||||||
let databuff = [0x61, 0xAA, 0x0A, 0X15, 0X00]; // send ABCDE
|
const message = revTestInfo?.recevedBuf?.toString()
|
||||||
testNapi.SerialSendAsync(globalThis.fd, databuff, (ret) => {
|
let msg = util.TextDecoder.create().decodeWithStream(new Uint8Array(revTestInfo?.recevedBuf))
|
||||||
testNapi.SerialRecvAsync(globalThis.fd, timeout, (revTestInfo) => {
|
let dang = Number(msg?.split(",")?.[1] || 0)
|
||||||
|
globalThis.chuankoMsg = dang
|
||||||
const message = revTestInfo?.recevedBuf?.toString()
|
console.log("lixiao, chuankou", msg)
|
||||||
if (message == '') {
|
})
|
||||||
globalThis.num = 1
|
setTimeout(() => {
|
||||||
// clearInterval(chuankou)
|
getChuankouFnMsg()
|
||||||
testNapi.SerialClose(globalThis.fd);
|
}, 200)
|
||||||
globalThis.fd = null
|
// let timeout = 50000; // 2秒超时
|
||||||
setTimeout(() => {
|
// let databuff = [0x61, 0xAA, 0x0A, 0X15, 0X00]; // send ABCDE
|
||||||
getChuankouFn()
|
// testNapi.SerialSendAsync(globalThis.fd, databuff, (ret) => {
|
||||||
}, 2000)
|
// testNapi.SerialRecvAsync(globalThis.fd, timeout, (revTestInfo) => {
|
||||||
return
|
//
|
||||||
}
|
// const message = revTestInfo?.recevedBuf?.toString()
|
||||||
const msg = message?.split(',')
|
// if (message == '') {
|
||||||
if (!msg?.length) {
|
// globalThis.num = 1
|
||||||
|
// // clearInterval(chuankou)
|
||||||
} else if (msg[0] != '98' || msg[1] != '85' || msg.length < 9) {
|
// testNapi.SerialClose(globalThis.fd);
|
||||||
|
// globalThis.fd = null
|
||||||
} else if (msg.length < 12) {
|
// setTimeout(() => {
|
||||||
|
// getChuankouFn()
|
||||||
} else {
|
// }, 2000)
|
||||||
globalThis.chuankoMsg = msg[9]
|
// return
|
||||||
}
|
// }
|
||||||
|
// const msg = message?.split(',')
|
||||||
setTimeout(() => {
|
// if (!msg?.length) {
|
||||||
getChuankouFnMsg()
|
//
|
||||||
}, 500)
|
// } else if (msg[0] != '98' || msg[1] != '85' || msg.length < 9) {
|
||||||
|
//
|
||||||
// hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync callback in');
|
// } else if (msg.length < 12) {
|
||||||
// hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedLen = %{public}d', revTestInfo.recevedLen);
|
//
|
||||||
// hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedBuf = %{public}s', revTestInfo.recevedBuf.toString());
|
// } else {
|
||||||
});
|
// globalThis.chuankoMsg = msg[9]
|
||||||
|
// }
|
||||||
});
|
//
|
||||||
|
// setTimeout(() => {
|
||||||
|
// getChuankouFnMsg()
|
||||||
|
// }, 500)
|
||||||
|
//
|
||||||
|
// // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync callback in');
|
||||||
|
// // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedLen = %{public}d', revTestInfo.recevedLen);
|
||||||
|
// // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedBuf = %{public}s', revTestInfo.recevedBuf.toString());
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// });
|
||||||
// let revTestInfo = testNapi?.SerialRecv(globalThis.fd, timeout);
|
// let revTestInfo = testNapi?.SerialRecv(globalThis.fd, timeout);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let chuankou
|
let chuankou
|
||||||
|
|||||||
@ -314,7 +314,7 @@ export default class UdpClientByCenter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
strachArr[28] = globalThis.chuankoMsg || strachArr[28]
|
strachArr[28] = globalThis.chuankoMsg || strachArr[28]
|
||||||
|
// console.log("lixiao", globalThis.chuankoMsg, strachArr[28])
|
||||||
// this.stashFn(str)
|
// this.stashFn(str)
|
||||||
const newArr = JSON.parse(JSON.stringify(strachArr))
|
const newArr = JSON.parse(JSON.stringify(strachArr))
|
||||||
// this.writeLog({
|
// this.writeLog({
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user