• 3867阅读
  • 2回复

[提问]Qt调用windows ntd dll函数获取cpu各个核的使用率遇到数据错误的问题 [复制链接]

上一主题 下一主题
离线wangqi
 

只看楼主 倒序阅读 楼主  发表于: 2016-06-18
//获得CPU使用率void CpuUsage::CalProcessorsUses(){    PROCNTQSI    NtQuerySystemInformation;    __int64    nIdleTime=0,nKernelTime=0,nUserTime=0,nDpcTime=0,nInterruptTime=0,nTotalTime=0;    NtQuerySystemInformation=(PROCNTQSI)GetProcAddress(GetModuleHandleA("ntdll"),"NtQuerySystemInformation");  //利用ntdll内置的API获取CPU    int status=NtQuerySystemInformation(SystemProcessorPerformanceInformation, m_ProcessInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION_2) * m_nNumberProcessors, 0);    for (int i = 0; i < m_nNumberProcessors; i++)     {          nIdleTime = m_ProcessInfo.i64IdleTime - m_nOidleTime;          nKernelTime = m_ProcessInfo.i64KernelTime - m_nOkerenelTime;          nUserTime = m_ProcessInfo.i64UserTime - m_nOuserTime;          nDpcTime = m_ProcessInfo.i64DpcTime -m_nOdpctTime;          nInterruptTime = m_ProcessInfo.i64InterruptTime - m_nOinterruptTime;          nTotalTime = nKernelTime + nUserTime + nDpcTime;// + nInterruptTime;          m_nUses =100*(nTotalTime-nIdleTime)/nTotalTime;    //计算CPU使用率
          m_nOidleTime = m_ProcessInfo.i64IdleTime;          m_nOkerenelTime = m_ProcessInfo.i64KernelTime;          m_nOuserTime = m_ProcessInfo.i64UserTime;          m_nOdpctTime = m_ProcessInfo.i64DpcTime;          m_nOinterruptTime = m_ProcessInfo.i64InterruptTime;     }}

设置了一个定时器:
    this->timer = new QTimer;    QObject::connect(this->timer,SIGNAL(timeout()),this,SLOT(get_system_info()));    this->timer->start(1000); //1秒


我发现定时器对我调用去获得cpu各个核的kerneltime,interrupttime等时间有很大的影响,很多时间都不准。用Visual Stduio2010只实现的获取没任何问题移植到Qt里面  时间就不对了。。。。请问是怎么回事啊,或者怎解决啊?求大神,求助!感谢啊!
血染琴弦,知音一生难求;泪洒青冢,黄土一抔尽掩。
离线wangqi

只看该作者 1楼 发表于: 2016-06-18
上面乱码了,重新发下:有兴趣的可以找我要代码哈,求 解决这个问题啊    


this->timer = new QTimer;
    QObject::connect(this->timer,SIGNAL(timeout()),this,SLOT(get_system_info()));
    this->timer->start(1000); //1秒


//获得CPU使用率
void CpuUsage::CalProcessorsUses()
{
    PROCNTQSI    NtQuerySystemInformation;
    __int64    nIdleTime=0,nKernelTime=0,nUserTime=0,nDpcTime=0,nInterruptTime=0,nTotalTime=0;

    NtQuerySystemInformation=(PROCNTQSI)GetProcAddress(GetModuleHandleA("ntdll"),"NtQuerySystemInformation");  //利用ntdll内置的API获取CPU

    int status=NtQuerySystemInformation(SystemProcessorPerformanceInformation, m_ProcessInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION_2) * m_nNumberProcessors, 0);
    for (int i = 0; i < m_nNumberProcessors; i++)
     {
          nIdleTime = m_ProcessInfo.i64IdleTime - m_nOidleTime;
          nKernelTime = m_ProcessInfo.i64KernelTime - m_nOkerenelTime;
          nUserTime = m_ProcessInfo.i64UserTime - m_nOuserTime;
          nDpcTime = m_ProcessInfo.i64DpcTime -m_nOdpctTime;
          nInterruptTime = m_ProcessInfo.i64InterruptTime - m_nOinterruptTime;

          nTotalTime = nKernelTime + nUserTime + nDpcTime;// + nInterruptTime;
          m_nUses =100*(nTotalTime-nIdleTime)/nTotalTime;    //计算CPU使用率

          m_nOidleTime = m_ProcessInfo.i64IdleTime;
          m_nOkerenelTime = m_ProcessInfo.i64KernelTime;
          m_nOuserTime = m_ProcessInfo.i64UserTime;
          m_nOdpctTime = m_ProcessInfo.i64DpcTime;
          m_nOinterruptTime = m_ProcessInfo.i64InterruptTime;
     }
}
血染琴弦,知音一生难求;泪洒青冢,黄土一抔尽掩。
离线wangqi

只看该作者 2楼 发表于: 2016-06-18
调试信息:interrupttime会出现很大的负值???为啥啊? 比对了下 vs2010的调试,不会出现这种情况啊。    
局部变量        
        NtQuerySystemInformation    0x778d621c <ntdll!ZwQueueApcThread>    PROCNTQSI
        i    0    int
        nDpcTime    4748514439    long long
        nIdleTime    800110720882    long long
        nInterruptTime    -7753833852956925205    long long
        nKernelTime    836241800490    long long
        nTotalTime    0    long long
        nUserTime    29940647926    long long
        status    0    int
        this     @0x22cc40    CpuUsage
    
血染琴弦,知音一生难求;泪洒青冢,黄土一抔尽掩。
快速回复
限100 字节
 
上一个 下一个