今天就跟大家聊聊有关利用C++怎么获取进程CPU的占用率,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
核心代码
// 时间转换
static __int64 file_time_2_utc(const FILETIME* ftime)
{
LARGE_INTEGER li;
li.LowPart = ftime->dwLowDateTime;
li.HighPart = ftime->dwHighDateTime;
return li.QuadPart;
}
// 获得CPU的核数
static int get_processor_number()
{
SYSTEM_INFO info;
GetSystemInfo(&info);
return (int)info.dwNumberOfProcessors;
}
// 获取进程CPU占用
int get_cpu_usage(int pid)
{
//cpu数量
static int processor_count_ = -1;
//上一次的时间
static __int64 last_time_ = 0;
static __int64 last_system_time_ = 0;
FILETIME now;
FILETIME creation_time;
FILETIME exit_time;
FILETIME kernel_time;
FILETIME user_time;
__int64 system_time;
__int64 time;
__int64 system_time_delta;
__int64 time_delta;
int cpu = -1;
if(processor_count_ == -1)
{
processor_count_ = get_processor_number();
}
GetSystemTimeAsFileTime(&now);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))
{
return -1;
}
system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) / processor_count_;
time = file_time_2_utc(&now);
if ((last_system_time_ == 0) || (last_time_ == 0))
{
last_system_time_ = system_time;
last_time_ = time;
return -1;
}
system_time_delta = system_time - last_system_time_;
time_delta = time - last_time_;
if (time_delta == 0)
return -1;
cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta);
last_system_time_ = system_time;
last_time_ = time;
return cpu;
}
以下是其它网友的补充
C++ 获取进程内存占用和CPU利用率等信息
1.获取内存占用信息
获取步骤:
(1)获取当前进程句柄 使用GetCurrentProcess(),返回一个当前进程的句柄
(2)定义一个保存内存信息的结构体 PROCESS_MEMORY_COUNTERS pmc;
结构体定义如下:
typedef struct _PROCESS_MEMORY_COUNTERS {
DWORD cb; Size of the structure, in bytes.//结构体大小
DWORD PageFaultCount; Number of page faults. // 缺页中断次数
SIZE_T PeakWorkingSetSize; Peak working set size, in bytes. // 使用内存高峰
SIZE_T WorkingSetSize; Current working set size, in bytes. // 当前使用的内存
SIZE_T QuotaPeakPagedPoolUsage; Peak paged pool usage, in bytes. // 使用页面缓存池高峰
SIZE_T QuotaPagedPoolUsage; Current paged pool usage, in bytes.// 使用页面缓存池
SIZE_T QuotaPeakNonPagedPoolUsage; Peak nonpaged pool usage, in bytes.// 使用非分页缓存池高峰
SIZE_T QuotaNonPagedPoolUsage; Current nonpaged pool usage, in bytes.// 使用非分页缓存池
SIZE_T PagefileUsage; Current space allocated for the pagefile, in bytes.Those pages may or may not be in memory.// 使用分页文件
SIZE_T PeakPagefileUsage; Peak space allocated for the pagefile, in bytes.// 使用分页文件高峰
} PROCESS_MEMORY_COUNTERS, *PPROCESS_MEMORY_COUNTERS;
(3)获取当前进程的内存信息,保存到结构体pmc中(第二个参数) 使用GetProcessMemoryInfo()
API定义如下:
GetProcessMemoryInfo(
HANDLE Process, 获取内存使用情况的进程句柄。
PPROCESS_MEMORY_COUNTERS ppsmemCounters, 返回内存使用情况的结构
DWORD cb 结构的大小
);DE
2.获取CPU利用率
获取步骤:
(1)获取当前进程句柄 通过OpenProcess(),返回一个进程句柄
函数原型如下:
HANDLE OpenProcess(
DWORD dwDesiredAccess, //渴望得到的访问权限(标志)
BOOL bInheritHandle, // 是否继承句柄
DWORD dwProcessId// 进程标示符,可以通过getpid()获取当前进程ID
);
(2)获取CPU使用时间 通过调用GetProcessTimes()
函数原型如下:
BOOL
WINAPI
GetProcessTimes(
__in HANDLE hProcess, 需要获取相关时间的进程句柄
__out LPFILETIME lpCreationTime, 进程的创建时间
__out LPFILETIME lpExitTime, 进程的退出时间
__out LPFILETIME lpKernelTime, 进程在内核模式下的所有时间
__out LPFILETIME lpUserTime 进程在用户模式下的所有时间
);
CPU使用时间=(lpKernelTime+lpUserTime)/GetProcessNumber()(内核数)
内核数获取方法如下:
int GetProcessNumber()
{
SYSTEM_INFO info;
GetSystemInfo(&info);
return (int)info.dwNumberOfProcessors;
}
(3)计算CPU利用率
CPU利用率=(现在的CPU占用时间-过去的CPU占用时间)/系统时间差
注:系统时间差可以通过GetSystemTimeAsFileTime()获取,然后在转换为int64类型即可,自定义转换方法如下:
__int64 FileTimeToInt64(const FILETIME& time)
{
ULARGE_INTEGER tt; //64位无符号整型值
tt.LowPart = time.dwLowDateTime;
tt.HighPart = time.dwHighDateTime;
return(tt.QuadPart); //返回整型值
}
看完上述内容,你们对利用C++怎么获取进程CPU的占用率有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。