linux port

master
zcy 2022-03-27 20:14:02 -07:00
parent fa6e8763bb
commit 450a27d200
4 changed files with 74 additions and 10 deletions

View File

@ -14,9 +14,9 @@
using namespace std; using namespace std;
// 字节序选择 // 字节序选择
typedef enum{ typedef enum{
BIG_ENDIAN, // 大端 BIG_ENDIAN_TYPE, // 大端
LITTLE_ENDIAN // 小端 LITTLE_ENDIAN_TYPE // 小端
}BYTE_ORDER; }BYTE_ORDER_TYPE;
typedef enum { typedef enum {
ENV_WINDOWS = 1, ENV_WINDOWS = 1,
@ -45,7 +45,7 @@ inline ENV_COMPILER CurrentEnvCompiler();
string itos(int x); string itos(int x);
BYTE_ORDER HostByteOrder(); BYTE_ORDER_TYPE HostByteOrder();
// 限制float精确度 // 限制float精确度
float LimitFloat(float in,int size); float LimitFloat(float in,int size);

View File

@ -1,6 +1,9 @@
#include "cpu_usage.h" #include "cpu_usage.h"
#include <iostream> #include <iostream>
#ifdef WINDOWS
float CPUusage::get_cpu_usage() float CPUusage::get_cpu_usage()
{ {
FILETIME now; FILETIME now;
@ -67,3 +70,5 @@ int CPUusage::get_processor_number()
GetSystemInfo(&info); GetSystemInfo(&info);
return info.dwNumberOfProcessors; return info.dwNumberOfProcessors;
} }
#endif

View File

@ -1,7 +1,34 @@
#include <Windows.h> #ifndef __CPU_USAGE__
#define __CPU_USAGE__
#ifdef WINDOWS
#include <Windows.h>
#endif
#ifdef linux
#include<iostream>
#include<stdio.h>
#include "unistd.h"
#include<stdlib.h>
#include<string.h>
#include <string>
#include<unistd.h>
#include<fcntl.h>
#include<ctype.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <assert.h>
using namespace std;
#endif
//原理调用GetProcessTimes()并与上次调用得到的结果相减即得到某段时间内CPU的使用时间 //原理调用GetProcessTimes()并与上次调用得到的结果相减即得到某段时间内CPU的使用时间
//C++ 获取特定进程规定CPU使用率 原文http://blog.csdn.net/liuqx97bb/article/details/52058657 //C++ 获取特定进程规定CPU使用率 原文http://blog.csdn.net/liuqx97bb/article/details/52058657
class CPUusage { class CPUusage {
#ifdef WINDOWS
private: private:
typedef long long int64_t; typedef long long int64_t;
typedef unsigned long long uint64_t; typedef unsigned long long uint64_t;
@ -49,8 +76,37 @@ public:
init(); init();
return _hProcess= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, ProcessID); return _hProcess= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, ProcessID);
} }
//-1 即为失败或进程已退出; 如果成功,首次调用会返回-2中途用setpid更改了PID后首次调用也会返回-2 //-1 即为失败或进程已退出; 如果成功,首次调用会返回-2中途用setpid更改了PID后首次调用也会返回-2
float get_cpu_usage(); 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

View File

@ -4,6 +4,9 @@
#include "utils.h" #include "utils.h"
#ifdef linux #ifdef linux
using namespace std;
int itoa(int n ,char * const s,int radix){ int itoa(int n ,char * const s,int radix){
if(nullptr == s){ if(nullptr == s){
return -1; return -1;
@ -87,12 +90,12 @@ std::string FormatString(const char *format,...)
} }
BYTE_ORDER HostByteOrder(){ BYTE_ORDER_TYPE HostByteOrder(){
short x = 0x1234; short x = 0x1234;
uint8_t y = *((uint8_t*)(&x)); uint8_t y = *((uint8_t*)(&x));
if(y == 0x34){ if(y == 0x34){
return BYTE_ORDER::LITTLE_ENDIAN; return BYTE_ORDER_TYPE::LITTLE_ENDIAN_TYPE;
}else{ }else{
return BYTE_ORDER::BIG_ENDIAN; return BYTE_ORDER_TYPE::BIG_ENDIAN_TYPE;
} }
} }