今天,我的生活很简单。我就是伸了伸手掌,测了测我的内存,掂了掂我的CPU使用率。今天,我的思维就钻到计算机里,给绞了。CPU型号和逻辑硬盘都在我的手指边,随手就可以碰触。
今天,我写的博客不针对搜索引擎。不去迎合它的喜好,没有罗列关键词,也没有写一个好的Title。
CPU使用率
计算CPU使用率要借用NtQuerySystemInformation函数。从SystemPerformanceInformation中得到空闲时间;从SystemTimeInformation中得到系统时间;从SystemBasicInformation中得到进程数。CPU使用率的公式就是:CpuUsageInPercent = 100 – (CpuTime[n] – CpuTime[n-1]) / (SystemTime[n] – SystemTime[n-1]) / NumberOfProcessors * 100。这种方法可在Windows NT/2000系统中使用。
另外,还有一种得到CPU使用率的方法。GetSystemTimes函数可以得到idleTime, kernelTime, userTime。由此也可以得到CPU使用率,程序如下:
#define _WIN32_WINNT 0×0501
#include
#include
using namespace std;
__int64 CompareFileTime ( FILETIME time1, FILETIME time2 )
{
__int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime ;
__int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime ;
return (b – [...]

SYSTEM_INFO Structure
Contains information about the current computer system. This includes the architecture and type of the processor, the number of processors in the system, the page size, and other such information.
Syntax
C++
typedef struct _SYSTEM_INFO {
union {
DWORD dwOemId;
struct {
WORD wProcessorArchitecture;
WORD wReserved;
} ;
} ;
DWORD dwPageSize;
LPVOID lpMinimumApplicationAddress;
LPVOID lpMaximumApplicationAddress;
DWORD_PTR dwActiveProcessorMask;
DWORD [...]

#include   <windows.h>
#include   <conio.h>
#include   <stdio.h>
#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   [...]