找回密码
 加入
搜索
查看: 3025|回复: 3

[系统综合] CPU利用率的代码,纯另一种思路的思考,希望大家来讨论

[复制链接]
发表于 2010-9-23 22:37:34 | 显示全部楼层 |阅读模式
本帖最后由 tan133 于 2012-5-14 19:19 编辑

已经知道WMI和另一种方法可以做到,只是在网上看到这一文,我只会一点AU3
有兴趣的可以参考下

转自孤独剑客的博客,最近一直研究MFC的东东,今天忽然发现大牛博客有这么个文章就转过来了……

希望不要说我侵权……

原文地址:http://www.janker.org/post/45.html


利用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;
     }
发表于 2010-9-23 23:09:50 | 显示全部楼层
至少有3种方法可以实现检测CPU占用率。
1、WMI
2、NtQuerySystemInformation
3、PDH

在性能检测与统计上,PDH是最强大的,不只是CPU占用率,单个进程的CPU占用率、内存占用率、IO操作数量、网络传输数量等,都可以检测到,管理工具中的Performance就是调用了PDH库。
NtQuerySystemInformation在其他方面很强大,枚举进程、线程、句柄、模块,处理器数量等等,一共能够获取40多种对象的信息。
发表于 2010-9-24 00:08:43 | 显示全部楼层
至少有3种方法可以实现检测CPU占用率。
1、WMI
2、NtQuerySystemInformation
3、PDH

在性能检测与统计 ...
pusofalse 发表于 2010-9-23 23:09



   
发表于 2010-9-24 00:42:58 | 显示全部楼层
3楼纯表情回复哦~~呵呵~~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-21 20:36 , Processed in 0.076990 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表