Obtain the C code of CPU usage

Source: Internet
Author: User
Obtain the C code for CPU usage-general Linux technology-Linux programming and kernel information. The following is a detailed description.
CODE: # include
# Include
# Include
# Define SystemBasicInformation 0
# Define SystemPerformanceInformation 2
# Define SystemTimeInformation 3
# Define Li2Double (x) (double) (x). HighPart) * 4.294967296E9 + (double) (x). LowPart ))
Typedef struct
{
DWORD dwUnknown1;
ULONG uKeMaximumIncrement;
ULONG uPageSize;
ULONG uMmNumberOfPhysicalPages;
ULONG uMmLowestPhysicalPage;
ULONG uMmHighestPhysicalPage;
ULONG uAllocationGranularity;
PVOID pLowestUserAddress;
PVOID pMmHighestUserAddress;
ULONG uKeActiveProcessors;
BYTE bKeNumberProcessors;
BYTE bUnknown2;
WORD wUnknown3;
} SYSTEM_BASIC_INFORMATION;
Typedef struct
{
LARGE_INTEGER liIdleTime;
DWORD dwSpare [76];
} SYSTEM_PERFORMANCE_INFORMATION;
Typedef struct
{
LARGE_INTEGER liKeBootTime;
LARGE_INTEGER liKeSystemTime;
LARGE_INTEGER liExpTimeZoneBias;
ULONG uCurrentTimeZoneId;
DWORD dwReserved;
} SYSTEM_TIME_INFORMATION;
Typedef LONG (WINAPI * PROCNTQSI) (UINT, PVOID, ULONG, PULONG );
PROCNTQSI NtQuerySystemInformation;
Void main ()
{
SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
SYSTEM_TIME_INFORMATION extends imeinfo;
SYSTEM_BASIC_INFORMATION SysBaseInfo;
Double dbIdleTime;
Double dbSystemTime;
LONG status;
LARGE_INTEGER liOldIdleTime = {0, 0 };
LARGE_INTEGER liOldSystemTime = {0, 0 };
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;
Int amount = 0;
// Printf ("\ n your CPU usage is :");
While (! _ Kbhit ())
{
// Set the new system time:
Amount ++;
Status = NtQuerySystemInformation (SystemTimeInformation, & policimeinfo, sizeof (policimeinfo), 0 );
If (status! = NO_ERROR) return;
Status = NtQuerySystemInformation (SystemPerformanceInformation, & SysPerfInfo, sizeof (SysPerfInfo), NULL );
If (status! = NO_ERROR) return;
If (liOldIdleTime. QuadPart! = 0)
{
DbIdleTime = Li2Double (SysPerfInfo. liIdleTime)-Li2Double (liOldIdleTime );
DbSystemTime = Li2Double (SysTimeInfo. liKeSystemTime)-Li2Double (liOldSystemTime );
DbIdleTime = dbIdleTime/dbSystemTime;
DbIdleTime = 100.0-dbIdleTime * 100.0/(double) SysBaseInfo. bKeNumberProcessors;
Int c = (int) dbIdleTime;
Printf ("\ B \ B \ B % d --- % d % ", amount, c );
}
// Change the new CPU usage and system time
LiOldIdleTime = SysPerfInfo. liIdleTime;
LiOldSystemTime = policimeinfo. liKeSystemTime;
Sleep (1000 );
}
Printf ("\ n ");
}

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.