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