进程监视器添加内存健康项目

master
zcy 2022-03-28 14:24:11 +08:00
parent 21eb616164
commit 36a2f64c17
6 changed files with 80 additions and 18 deletions

View File

@ -12,16 +12,16 @@ set_property(TARGET generallib PROPERTY
MSVC_RUNTIME_LIBRARY MultiThreadedDebug) 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 (WIN32)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
message("clang compiler \r\n") message("clang compiler \r\n")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
message("gcc compiler \r\n") message("gcc compiler \r\n")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
message("using asm") message("using asm\r\n")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
message("using msvc compiler") message("using msvc compiler\r\n")
endif() endif()
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(GLOB ALGORITHM ${PROJECT_SOURCE_DIR}/general/src/algorithm/*)
file(COPY ${ALGORITHM} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/algorithm/ file(COPY ${ALGORITHM} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/algorithm/
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ) FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)

View File

@ -92,9 +92,22 @@ float CPUusage::GetMemoryUsage(){
#endif #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 now;
FILETIME creation_time; FILETIME creation_time;
@ -145,7 +158,7 @@ float CPUusage::get_cpu_usage()
return cpu; return cpu;
} }
CPUusage::uint64_t CPUusage::FileTime2UTC(const FILETIME* ftime) CProcessMonitor::uint64_t CProcessMonitor::FileTime2UTC(const FILETIME* ftime)
{ {
LARGE_INTEGER li; LARGE_INTEGER li;
@ -154,7 +167,7 @@ CPUusage::uint64_t CPUusage::FileTime2UTC(const FILETIME* ftime)
return li.QuadPart; return li.QuadPart;
} }
int CPUusage::get_processor_number() int CProcessMonitor::get_processor_number()
{ {
SYSTEM_INFO info; SYSTEM_INFO info;
GetSystemInfo(&info); GetSystemInfo(&info);

View File

@ -1,8 +1,11 @@
#ifndef __CPU_USAGE__ #ifndef __CPU_USAGE__
#define __CPU_USAGE__ #define __CPU_USAGE__
#ifdef WINDOWS #ifdef _WIN64 || _WIN32
#include <Windows.h> #include <windows.h>
#include <psapi.h>
#include <assert.h>
#endif #endif
#ifdef linux #ifdef linux
@ -27,8 +30,8 @@ using namespace std;
//原理调用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 CProcessMonitor {
#ifdef WINDOWS #ifdef _WIN64 || _WIN32
private: private:
typedef long long int64_t; typedef long long int64_t;
typedef unsigned long long uint64_t; typedef unsigned long long uint64_t;
@ -62,13 +65,13 @@ private:
} }
public: public:
CPUusage(DWORD ProcessID) { CProcessMonitor(DWORD ProcessID) {
init(); init();
_processor = get_processor_number(); _processor = get_processor_number();
setpid(ProcessID); setpid(ProcessID);
} }
CPUusage() { init(); _processor = get_processor_number(); } CProcessMonitor() { init(); _processor = get_processor_number(); }
~CPUusage() { clear(); } ~CProcessMonitor() { clear(); }
//返回值为进程句柄可判断OpenProcess是否成功 //返回值为进程句柄可判断OpenProcess是否成功
HANDLE setpid(DWORD ProcessID) { HANDLE setpid(DWORD ProcessID) {
@ -78,12 +81,13 @@ public:
} }
//-1 即为失败或进程已退出; 如果成功,首次调用会返回-2中途用setpid更改了PID后首次调用也会返回-2 //-1 即为失败或进程已退出; 如果成功,首次调用会返回-2中途用setpid更改了PID后首次调用也会返回-2
float get_cpu_usage(); float get_cpu_usage();
int get_memory_usage(uint64_t* mem, uint64_t* vmem);
#endif #endif
#ifdef linux #ifdef linux
public: public:
CPUusage(uint16_t ProcessID) { CProcessMonitor(uint16_t ProcessID) {
m_process_id = ProcessID; m_process_id = ProcessID;
m_cpu_count = 0; m_cpu_count = 0;
m_cpu_count = sysconf( _SC_NPROCESSORS_CONF); m_cpu_count = sysconf( _SC_NPROCESSORS_CONF);
@ -94,11 +98,11 @@ public:
float GetCpuUsage(); float GetCpuUsage();
float GetMemoryUsage(); float GetMemoryUsage();
CPUusage() { CProcessMonitor() {
} }
~CPUusage() { ~CProcessMonitor() {
} }

View File

@ -19,7 +19,7 @@ TEST(testCase,test0){
} }
TEST(testCase,test1){ TEST(testCase,test1){
CPUusage ussage1(21132); CProcessMonitor ussage1(21132);
while (1) while (1)
{ {
std::cout<<ussage1.get_cpu_usage()<<std::endl; std::cout<<ussage1.get_cpu_usage()<<std::endl;

View File

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.19)
project(test_cpu_usage)
message("cmake module " $ENV{CMAKE_MODULE_PATH})
message("project dir " ${PROJECT_SOURCE_DIR})
set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_CXX_FLAGS "/OPT:NOREF /std:c++14 /EHsc /MTd")
set(CMAKE_CXX_FLAGS_RELEASE "")
string(REGEX REPLACE "(.*)/(.*)/(.*)" "\\1" PARENTPATH ${PROJECT_SOURCE_DIR})
message("last=" ${PARENTPATH})
include(${PARENTPATH}/CmakeTemplate.txt)
include_directories(./third/gtest/include)
link_directories(./third/gtest/lib)
add_executable(test_cpu_usage test_cpu_usage.cpp)
set_property(TARGET test_cpu_usage PROPERTY
MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
target_include_directories(test_cpu_usage SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../obj/inc/third/include)

View File

@ -0,0 +1,19 @@
#include "function/cpu_usage.h"
#include <Windows.h>
#include <iostream>
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() << " "<<pmem << "byte "<< vmem <<"byte" << std::endl;
Sleep(1000);
}
}