#
In theGetIdletime, the calculation method of the CPU usage recommended by MSDN, the usage rate obtained during some platforms is abnormal: non-0-100% value.
first look at the approximate implementation of the algorithm recommended algorithm MSDN:
static void callcountcpuidLethread (CCOUNTCPUDLG *PCCOUNTCPUDLG)
{{
Dword dwstarttick;
Dword dwidlestart;
Dword dwstoptick;
Dword dwidleend;
int NPRESENTIDLE;
int ncpuuage [10] = {0};
int NCOUNT = 0;
int anve = 0;
DWord Sum = 0;
int i;
While (1)
{{
dwstarttick = gettickCount (); // Here is the key code leo.zheng
dwidleStart = GetIdletime ();
Sleep (100);
dwstoptick = gettickCount ();
dwidleend = getIdletime ();
npressidle = (100*(dwidleend-dwidlestart)/((DWSTOPTICK-DWSTATTICK));
// One average of the algorithm multiple times leo.zheng
if (ncount <10)
{{
ncpuuage [ncount] = nprencentidle;
ncount ++;
if (ncount == 10)
{{
for (i = 0; i <10; i ++)
SUM+= ncpuuage [i];
Ave = SUM/10;
// G_NCPUIDLE only read in the main thread
g_ncpuidle = AVE;
ncount = 0;
sum = 0;
// Retailmsg (1, Text ("CPU LAODING IS %D/R/N");
}
}
}
}
Do not use
The method of calculating the CPU usage and calling examples of the calculation of CPU are as follows:GetIdleTime
/*
// Call for example
Dword dwthreadid = 0;
Handle hthread = null;
hthread = createThread (null, 0, Threadprintcpuload, this, 0, & dwthreadid);
if (null! = hthread)
{{
Closehandle (hthread);
}
*/
Static Dward GetThreadtick (Filetime *PFTKERNEL, Filetime *PFTUSER)
{{
__int64 iKernel64 = 0;
__int64 IUSER64 = 0;
ieernel64 = pterkernel-> dwhighDatetime;
ieernel64 << = 32;
IKERnel64 += PFTKERnel-> DWlowDatetime;
IUSER64 = PFTUSER-> DWHIGHDATETIME;
IUSER64 << = 32;
IKERnel64 += PFTUSER-> DWlowDatetime;
IKERnel64 += IUSER64;
// Nano Seco Milli SEC
ieernel64 /= 10000;
Return (dword) kernel64;
}
Static Void Idlethread (LPVOID PVPARAMS)
{{
While (1)
{{
}
}
Static dword ThreadPrintcpuload (LPVOID LPParameter)
{{
Dword dwthreadid = 0;
Handle hidden = null;
Setthreadpriority (getCurrenThread (), Thread_priority_highes); // Set high priority to ensure that the CPU can also print Leo when you are busy
Hiddenread = createThread (null, 0, (lprpthread_start_routine) Idlethread, NULL, Create_SUSPENDED, & DWthreadid);
SetThreadpriority (HIDLETHREAD, Thread_priority_idle);
Resumethread (Hidden);
While (1)
{{
Sleep (1000); // 1s Print once once leo.zheng
Static dword dwlastthreadtime = 0;
Static dword dwlastticktime = 0;
DWORD DWCURRENTTTHREADTIME = 0;
DWORD DWCURRENTTICKTIME = 0;
Dword dwcpupower = 0;
Filetime ftcreationTime;
Filetime ftexittime;
Filetime ftkerneltime;
Filetime ftusertime;
SUSPENDTHREAD (HIDLETHREAD);
dwcurrentticktime = GettickCount ();
GetThreadtimes (HIDLETHREAD, & FTCreationTime, & Ftexittime, & Ftkerneltime, & Ftusertime);
dwcurrenThreadtime = getthreadtick (& ftkerneltime, & ftusertime);
// Calculate the CPU occupation rate
if (DWCURRENTTICKTIME! = Dwlastticktime || 0! = Dwlastthreadtime || 0! = Dwlastticktime)
{{
dwcpupower = 100- ((DWCURRENTTTHREADTIME -DWlastthreadtime) * 100) / (DWCURRENTTICKKTIME -DWlastticktime));););
}
else
{{
dwcpupower = 0;
}
// Save Status
dwlastticktime = gettickCount ();
dwlastthreadtime = dwcurrentthreadtime;
Printf ("CPU loaded:%d %% \ r \ n", dwcpupower);
Resumethread (Hidden);
}
}