计算cpu使用率和判断服务器断开

来源:互联网 发布:32位单片机有哪些 编辑:程序博客网 时间:2024/04/29 01:29

 一,获得cup使用率的方法如下
  1,  取出系统中cpu个数;   
   
  2,  取出当前系统时间,减去原来保存的系统时间,得出从上次取值到目前的系统时间差;   
   
  3,  取出系统当前空闲时间,减去原来保存的空闲时间,得出这个时间段空闲时间   
   
  4,  用这个时间段空闲时间除以系统时间,得出这段时间系统的cpu空闲率   
   
  5,  用100-(系统的空闲率)/(cpu个数)所得的值就是cpu占有率   

 

老外的代码:

void CCpuUsgesCtl::GetCpuUsgesNt()
{
 SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
    SYSTEM_TIME_INFORMATION        SysTimeInfo;
    SYSTEM_BASIC_INFORMATION       SysBaseInfo;
    double                         dbIdleTime;
    double                         dbSystemTime;
    LONG                           status;
    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;
     }

     // store new CPU's idle and system time
     m_liOldIdleTime = SysPerfInfo.liIdleTime;
     m_liOldSystemTime = SysTimeInfo.liKeSystemTime;

}

二, send和recv的返回值可以判断,返回值小于等于0,则说明连接已经断开或者出现了错误。


 

代码:
BOOL ClientDlg::Socket_HasConnectDropped(int i_sd)
{
 BOOL bConnDropped = FALSE;
 INT iRet = 0;
 BOOL bOK = TRUE;
 
 if (i_sd == INVALID_SOCKET)
  return TRUE;
 
 struct timeval timeout = { 0, 0 };
 fd_set readSocketSet;
 
 FD_ZERO(&readSocketSet);
 FD_SET(i_sd, &readSocketSet);
 
 iRet = ::select(0, &readSocketSet, NULL, NULL, &timeout);
 bOK = (iRet > 0);
 
 if(bOK)
 {
  bOK = FD_ISSET(i_sd, &readSocketSet);
 }
 
 if(bOK)
 {
  CHAR szBuffer[1] = "";
  iRet = ::recv(i_sd, szBuffer, 1, MSG_PEEK);
  bOK = (iRet > 0);
  if(!bOK)
  {
   INT iError = ::WSAGetLastError();
   bConnDropped = (( iError == WSAENETRESET) ||
    (iError == WSAECONNABORTED) ||
    (iError == WSAECONNRESET) ||
    (iError == WSAEINVAL) ||
    (iRet == 0));
  }
 }
    return(bConnDropped);
 
}

 

//资源均来自互联网,我只是想让更多人知道。

原创粉丝点击