const short CGPUUsage::GetUsage() { short nGPUCopy = m_nGPUUsage; if (m_GPUType != UNKNOWN_GPU && ::InterlockedIncrement(&m_lRunCount) == 1) { if (!EnoughTimePassed()) { ::InterlockedDecrement(&m_lRunCount); return nGPUCopy; } if (m_GPUType == ATI_GPU) { if (ATIData.iOverdriveVersion == 5) { ADLPMActivity activity = {0}; activity.iSize = sizeof(ADLPMActivity); if (ADL_OK == ATIData.ADL_Overdrive5_CurrentActivity_Get(ATIData.iAdapterId, &activity)) { m_nGPUUsage = activity.iActivityPercent; } } else if (ATIData.iOverdriveVersion == 6) { ADLOD6CurrentStatus currentStatus = {0}; if (ADL_OK == ATIData.ADL_Overdrive6_CurrentStatus_Get(ATIData.iAdapterId, ¤tStatus)) { m_nGPUUsage = currentStatus.iActivityPercent; } } } else if (m_GPUType == NVIDIA_GPU) { NVData.NvAPI_GPU_GetUsages(NVData.gpuHandles[0], NVData.gpuUsages); m_nGPUUsage = NVData.gpuUsages[3]; } m_dwLastRun = GetTickCount(); nGPUCopy = m_nGPUUsage; } ::InterlockedDecrement(&m_lRunCount); return nGPUCopy; }
/********************************************** * CpuUsage::GetUsage * returns the percent of the CPU that this process * has used since the last time the method was called. * If there is not enough information, -1 is returned. * If the method is recalled to quickly, the previous value * is returned. ***********************************************/ double CpuUsage::GetUsage() { //create a local copy to protect against race conditions in setting the //member variable double nCpuCopy = m_nCpuUsage; if (::InterlockedIncrement(&m_lRunCount) == 1) { /* If this is called too often, the measurement itself will greatly affect the results. */ if (!EnoughTimePassed()) { ::InterlockedDecrement(&m_lRunCount); return nCpuCopy; } FILETIME ftSysIdle, ftSysKernel, ftSysUser; FILETIME ftProcCreation, ftProcExit, ftProcKernel, ftProcUser; if (!GetSystemTimes(&ftSysIdle, &ftSysKernel, &ftSysUser) || !GetProcessTimes(GetCurrentProcess(), &ftProcCreation, &ftProcExit, &ftProcKernel, &ftProcUser)) { ::InterlockedDecrement(&m_lRunCount); return nCpuCopy; } if (!IsFirstRun()) { /* CPU usage is calculated by getting the total amount of time the system has operated since the last measurement (made up of kernel + user) and the total amount of time the process has run (kernel + user). */ ULONGLONG ftSysKernelDiff = SubtractTimes(ftSysKernel, m_ftPrevSysKernel); ULONGLONG ftSysUserDiff = SubtractTimes(ftSysUser, m_ftPrevSysUser); ULONGLONG ftProcKernelDiff = SubtractTimes(ftProcKernel, m_ftPrevProcKernel); ULONGLONG ftProcUserDiff = SubtractTimes(ftProcUser, m_ftPrevProcUser); ULONGLONG nTotalSys = ftSysKernelDiff + ftSysUserDiff; ULONGLONG nTotalProc = ftProcKernelDiff + ftProcUserDiff; if (nTotalSys > 0) { //m_nCpuUsage = (short)((100.0 * nTotalProc) / nTotalSys); m_nCpuUsage = (100.0 * nTotalProc) / nTotalSys; } } m_ftPrevSysKernel = ftSysKernel; m_ftPrevSysUser = ftSysUser; m_ftPrevProcKernel = ftProcKernel; m_ftPrevProcUser = ftProcUser; //m_dwLastRun = GetTickCount64(); DWORD dwLastRun = GetTickCount(); m_dwLastRun = (ULONGLONG)dwLastRun; nCpuCopy = m_nCpuUsage; } ::InterlockedDecrement(&m_lRunCount); return nCpuCopy; }