Determines whether the system CPU is idle

Source: Internet
Author: User

It is used to determine whether the system is idle during code compilation. It must be used in both Linux and Windows. Initially, the getsystemtimes function was used for determination. Later, it was found that it could not be used in Win2000. Later, we switched to the performance API, which provides four interfaces: WMI, ODBC, registery, and PDH. Registry seems to be the best choice, but in fact there are too few materials available and it is useless. WMI is based on the COM interface and is complex to call using C ++. Finally, I studied it for half a day and used the PDH interface.

Bool systemidle ()
{
# Ifdef Win32
DWORD countertype;
Hquery;
Pdh_status pdhstatus;
Hcounter;
Static pdh_raw_counter lastrawdata;
Static bool first = true;

Tchar szcounterpath [] =
Text ("// processor (_ total) // % processor time ");

/* We can use "% idle time" to retrieve IDL time directly, but this counter is available
* On WindowsXP only. fortunatelly, We can get IDL = 100-processer
*/
// Tchar szcounterpath [] =
// Text ("// processor (0) // % idle time ");

// Open a query object.
Pdhstatus = pdhopenquery (null, 0, & hquery );
If (pdhstatus! = Error_success)
{
Return true;
}

// Add one counter that will provide the data.
Pdhstatus = pdhaddcounter (hquery,
Szcounterpath,
0,
& Hcounter );
If (pdhstatus! = Error_success)
{
Pdhclosequery (hquery );
Return true;
}

Pdh_raw_counter rawdata2;

Pdhstatus = pdhcollectquerydata (hquery );
If (pdhstatus! = Error_success)
{
Pdhclosequery (hquery );
Return true;
}
Pdhgetrawcountervalue (hcounter, & countertype, & rawdata2 );
If (first)
{
Lastrawdata = rawdata2;
First = false;
Pdhclosequery (hquery );
Return true;
}

Pdhstatus = pdhcollectquerydata (hquery );
If (pdhstatus! = Error_success)
{
Pdhclosequery (hquery );
Return true;
}
Pdhgetrawcountervalue (hcounter, & countertype, & rawdata2 );

Pdh_fmt_countervalue fmtvalue;
Pdhstatus = pdhcalculatecounterfromrawvalue (hcounter,
Pdh_fmt_double,
& Rawdata2,
& Lastrawdata,
& Fmtvalue );
If (pdhstatus! = Error_success)
{
Pdhclosequery (hquery );
Return true;
}
Lastrawdata = rawdata2;

Pdhstatus = pdhclosequery (hquery );
Return fmtvalue. doublevalue <= 20;
/*
// Method 2, Use getsystemtimes, this is more simple,
// Getsystemtimes is not available on window2000 OS
Static _ int64 last_idle = 0, last_kernel = 0, last_user = 0;
_ Int64 idle, kernel, user;
If (! Getsystemtimes (lpfiletime) & idle, (lpfiletime) & kernel, (lpfiletime) & User ))
{
Ptrtrace ("fail to call getsystemtimes/R/N ");
Return false;
}
// Pdhopenquery
Double rate = (double) (idle-last_idle)/(user-last_user + kernel-last_kernel );
Last_idle = idle;
Last_kernel = kernel;
Last_user = user;
If (rate> = 0.8) // more than 80% of system is in idle
Return true;
Return false;
*/
# Else // Linux & Unix
# Define maxload 20 // That's to say, if the system overload is no more than 20%, it's idle
Char Buf [4];
Int F = open ("/proc/loadavg", o_rdonly );
If (F <0)
{
Ptrtrace ("fail to open file/proc/loadavg ");
Return false;
}
If (read (F, Buf, 4 )! = 4)
{
Ptrtrace ("fail to Read File/proc/loadavg ");
Close (f );
Return false;
}
Close (f );
Return (BUF [0] = '0') & (atoi (BUF + 2) <maxload );
# Endif
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.