From 450a27d200f00bf4633dcf214ec67eeb4b38d7b6 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sun, 27 Mar 2022 20:14:02 -0700 Subject: [PATCH] linux port --- general/inc/utils.h | 8 ++-- general/src/function/cpu_usage.cpp | 7 +++- general/src/function/cpu_usage.h | 60 +++++++++++++++++++++++++++++- general/src/utils.cpp | 9 +++-- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/general/inc/utils.h b/general/inc/utils.h index 20e7f3b..3a876a6 100644 --- a/general/inc/utils.h +++ b/general/inc/utils.h @@ -14,9 +14,9 @@ using namespace std; // 字节序选择 typedef enum{ - BIG_ENDIAN, // 大端 - LITTLE_ENDIAN // 小端 -}BYTE_ORDER; + BIG_ENDIAN_TYPE, // 大端 + LITTLE_ENDIAN_TYPE // 小端 +}BYTE_ORDER_TYPE; typedef enum { ENV_WINDOWS = 1, @@ -45,7 +45,7 @@ inline ENV_COMPILER CurrentEnvCompiler(); string itos(int x); -BYTE_ORDER HostByteOrder(); +BYTE_ORDER_TYPE HostByteOrder(); // 限制float精确度 float LimitFloat(float in,int size); diff --git a/general/src/function/cpu_usage.cpp b/general/src/function/cpu_usage.cpp index 76311b3..3125597 100644 --- a/general/src/function/cpu_usage.cpp +++ b/general/src/function/cpu_usage.cpp @@ -1,6 +1,9 @@ #include "cpu_usage.h" #include + +#ifdef WINDOWS + float CPUusage::get_cpu_usage() { FILETIME now; @@ -66,4 +69,6 @@ int CPUusage::get_processor_number() SYSTEM_INFO info; GetSystemInfo(&info); return info.dwNumberOfProcessors; -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/general/src/function/cpu_usage.h b/general/src/function/cpu_usage.h index c28c7cb..6366a21 100644 --- a/general/src/function/cpu_usage.h +++ b/general/src/function/cpu_usage.h @@ -1,7 +1,34 @@ -#include +#ifndef __CPU_USAGE__ +#define __CPU_USAGE__ + +#ifdef WINDOWS + #include +#endif + +#ifdef linux +#include +#include +#include "unistd.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +#endif + //原理:调用GetProcessTimes(),并与上次调用得到的结果相减,即得到某段时间内CPU的使用时间 //C++ 获取特定进程规定CPU使用率 原文:http://blog.csdn.net/liuqx97bb/article/details/52058657 class CPUusage { +#ifdef WINDOWS private: typedef long long int64_t; typedef unsigned long long uint64_t; @@ -49,8 +76,37 @@ public: init(); return _hProcess= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, ProcessID); } - //-1 即为失败或进程已退出; 如果成功,首次调用会返回-2(中途用setpid更改了PID后首次调用也会返回-2) float get_cpu_usage(); +#endif + +#ifdef linux + +public: + CPUusage(uint16_t ProcessID) { + m_process_id = ProcessID; + m_cpu_count = 0; + m_cpu_count = sysconf( _SC_NPROCESSORS_CONF); + m_enable_cpu_count = sysconf(_SC_NPROCESSORS_ONLN); + printf("system cpu num is %d\n", sysconf( _SC_NPROCESSORS_CONF)); + printf("system enable cpu num is %d\n", sysconf(_SC_NPROCESSORS_ONLN)); + } + + CPUusage() { + + } + + ~CPUusage() { + + } + +private: + uint16_t m_cpu_count; + uint16_t m_process_id; + uint16_t m_enable_cpu_count; + +#endif + }; +#endif \ No newline at end of file diff --git a/general/src/utils.cpp b/general/src/utils.cpp index 108e515..58b0e91 100644 --- a/general/src/utils.cpp +++ b/general/src/utils.cpp @@ -4,6 +4,9 @@ #include "utils.h" #ifdef linux + +using namespace std; + int itoa(int n ,char * const s,int radix){ if(nullptr == s){ return -1; @@ -87,12 +90,12 @@ std::string FormatString(const char *format,...) } -BYTE_ORDER HostByteOrder(){ +BYTE_ORDER_TYPE HostByteOrder(){ short x = 0x1234; uint8_t y = *((uint8_t*)(&x)); if(y == 0x34){ - return BYTE_ORDER::LITTLE_ENDIAN; + return BYTE_ORDER_TYPE::LITTLE_ENDIAN_TYPE; }else{ - return BYTE_ORDER::BIG_ENDIAN; + return BYTE_ORDER_TYPE::BIG_ENDIAN_TYPE; } }