diff --git a/CMakeLists.txt b/CMakeLists.txt index 02c1f76..217d4a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,16 +12,16 @@ set_property(TARGET generallib PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebug) -message("current path is" ${CMAKE_CURRENT_SOURCE_DIR}) +message("current path is" ${CMAKE_CURRENT_SOURCE_DIR} "\r\n") IF (WIN32) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") message("clang compiler \r\n") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") message("gcc compiler \r\n") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") - message("using asm") + message("using asm\r\n") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - message("using msvc compiler") + message("using msvc compiler\r\n") endif() endif() @@ -69,3 +69,5 @@ file(COPY ${THREADSAFECODE} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/threadsafe/ file(GLOB ALGORITHM ${PROJECT_SOURCE_DIR}/general/src/algorithm/*) file(COPY ${ALGORITHM} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/algorithm/ FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ) + + diff --git a/general/src/function/cpu_usage.cpp b/general/src/function/cpu_usage.cpp index 01351c5..4b28730 100644 --- a/general/src/function/cpu_usage.cpp +++ b/general/src/function/cpu_usage.cpp @@ -92,9 +92,22 @@ float CPUusage::GetMemoryUsage(){ #endif -#ifdef WINDOWS +#ifdef _WIN64 || _WIN32 -float CPUusage::get_cpu_usage() +int CProcessMonitor::get_memory_usage(uint64_t* mem, uint64_t* vmem) +{ + PROCESS_MEMORY_COUNTERS pmc; + if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) + { + if(mem) *mem = pmc.WorkingSetSize; + if(vmem) *vmem = pmc.PagefileUsage; + return 0; + } + return -1; + +} + +float CProcessMonitor::get_cpu_usage() { FILETIME now; FILETIME creation_time; @@ -145,7 +158,7 @@ float CPUusage::get_cpu_usage() return cpu; } -CPUusage::uint64_t CPUusage::FileTime2UTC(const FILETIME* ftime) +CProcessMonitor::uint64_t CProcessMonitor::FileTime2UTC(const FILETIME* ftime) { LARGE_INTEGER li; @@ -154,7 +167,7 @@ CPUusage::uint64_t CPUusage::FileTime2UTC(const FILETIME* ftime) return li.QuadPart; } -int CPUusage::get_processor_number() +int CProcessMonitor::get_processor_number() { SYSTEM_INFO info; GetSystemInfo(&info); diff --git a/general/src/function/cpu_usage.h b/general/src/function/cpu_usage.h index 15e5006..2ab9d6b 100644 --- a/general/src/function/cpu_usage.h +++ b/general/src/function/cpu_usage.h @@ -1,8 +1,11 @@ #ifndef __CPU_USAGE__ #define __CPU_USAGE__ -#ifdef WINDOWS - #include +#ifdef _WIN64 || _WIN32 +#include +#include +#include + #endif #ifdef linux @@ -27,8 +30,8 @@ using namespace std; //原理:调用GetProcessTimes(),并与上次调用得到的结果相减,即得到某段时间内CPU的使用时间 //C++ 获取特定进程规定CPU使用率 原文:http://blog.csdn.net/liuqx97bb/article/details/52058657 -class CPUusage { -#ifdef WINDOWS +class CProcessMonitor { +#ifdef _WIN64 || _WIN32 private: typedef long long int64_t; typedef unsigned long long uint64_t; @@ -62,13 +65,13 @@ private: } public: - CPUusage(DWORD ProcessID) { + CProcessMonitor(DWORD ProcessID) { init(); _processor = get_processor_number(); setpid(ProcessID); } - CPUusage() { init(); _processor = get_processor_number(); } - ~CPUusage() { clear(); } + CProcessMonitor() { init(); _processor = get_processor_number(); } + ~CProcessMonitor() { clear(); } //返回值为进程句柄,可判断OpenProcess是否成功 HANDLE setpid(DWORD ProcessID) { @@ -78,12 +81,13 @@ public: } //-1 即为失败或进程已退出; 如果成功,首次调用会返回-2(中途用setpid更改了PID后首次调用也会返回-2) float get_cpu_usage(); + int get_memory_usage(uint64_t* mem, uint64_t* vmem); #endif #ifdef linux public: - CPUusage(uint16_t ProcessID) { + CProcessMonitor(uint16_t ProcessID) { m_process_id = ProcessID; m_cpu_count = 0; m_cpu_count = sysconf( _SC_NPROCESSORS_CONF); @@ -94,11 +98,11 @@ public: float GetCpuUsage(); float GetMemoryUsage(); - CPUusage() { + CProcessMonitor() { } - ~CPUusage() { + ~CProcessMonitor() { } diff --git a/test/src/gtest_demo/gtest_demo.cpp b/test/src/gtest_demo/gtest_demo.cpp index b144e0b..8222cbf 100644 --- a/test/src/gtest_demo/gtest_demo.cpp +++ b/test/src/gtest_demo/gtest_demo.cpp @@ -19,7 +19,7 @@ TEST(testCase,test0){ } TEST(testCase,test1){ - CPUusage ussage1(21132); + CProcessMonitor ussage1(21132); while (1) { std::cout< +#include + +using namespace std; + +int main(int argc,char **argv){ + CProcessMonitor usage1 = CProcessMonitor(14316); + while(true){ + uint64_t vmem,pmem; + usage1.get_memory_usage(&pmem,&vmem); + std::cout<<"cpu usage is "<< usage1.get_cpu_usage() << " "<