如何得到CPU的利用率

Benny_ywb 2007-06-19 10:58:08
已知进程,如何得到该进程的CPU利用率呢?从网上搜了下可以得到系统的CPU,但是没有找到某个进程的方法,想问问相关函数和方法。WINDOWS平台下。
...全文
228 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
todototry 2007-06-19
  • 打赏
  • 举报
回复
利用ntdll.dll中没有公开的API函数: NtQuerySystemInformation

typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);
PROCNTQSI NtQuerySystemInformation;

NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
GetModuleHandle("ntdll"),
"NtQuerySystemInformation"
);
if (!NtQuerySystemInformation)
{
return;
}

// get number of processors in the system
status = NtQuerySystemInformation(SystemBasicInformation,
&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR)
{
return;
}
status = NtQuerySystemInformation(SystemTimeInformation,
&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR)
{
return;
}
// get new CPU''s idle time
status = NtQuerySystemInformation(SystemPerformanceInformation,
&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
{
return;
}
// if it''s a first call - skip it
if (m_liOldIdleTime.QuadPart != 0)
{
// CurrentValue = NewValue - OldValue
dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(m_liOldIdleTime);
dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(m_liOldSystemTime);

// CurrentCpuIdle = IdleTime / SystemTime
dbIdleTime = dbIdleTime / dbSystemTime;

// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;

m_fNewUsges = (UINT)dbIdleTime;
}
我啃 2007-06-19
  • 打赏
  • 举报
回复
此API是ntdll.dll中没有公开的API
估计98和ME不行
Benny_ywb 2007-06-19
  • 打赏
  • 举报
回复
谢谢啊,我回去试一试。不过博客那个应该是NT平台的吧,WINDOWS也可以调用NtQuerySystemInformation()吗?
我啃 2007-06-19
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20011120/22/380241.html
http://blog.csdn.net/wanfustudio/archive/2007/04/07/1555682.aspx
yc_8301 2007-06-19
  • 打赏
  • 举报
回复
上面的函数能否在xp或2000系统下面用啊。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧