今天,伸手丈量我的电脑

今天,我的生活很简单。我就是伸了伸手掌,测了测我的内存,掂了掂我的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 0x0501
#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 - a);
}
void main()
{
HANDLE hEvent;
BOOL res ;
FILETIME preidleTime;
FILETIME prekernelTime;
FILETIME preuserTime;
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
res = GetSystemTimes( &idleTime, &kernelTime, &userTime );
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime ;
hEvent = CreateEvent (NULL,FALSE,FALSE,NULL); // 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled
while (1){
WaitForSingleObject( hEvent,1000 ); //等待500毫秒
res = GetSystemTimes( &idleTime, &kernelTime, &userTime );
int idle = CompareFileTime( preidleTime,idleTime);
int kernel = CompareFileTime( prekernelTime, kernelTime);
int user = CompareFileTime(preuserTime, userTime);
int cpu = (kernel +user - idle) *100/(kernel+user);
int cpuidle = ( idle) *100/(kernel+user);
cout << "CPU利用率:" << cpu << "%" << " CPU空闲率:" <<< "%" <
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime ;
}
}

系统内存状态

GlobalMemoryStatus可以得到系统内存状态。函数原型是:
void WINAPI GlobalMemoryStatus(
__out LPMEMORYSTATUS lpBuffer
);
lpBuffer参数是一个MEMORYSTATUS结构体变量的指针。GlobalMemoryStatus把信息存储在这个结构体里。这个函数没有返回值。

MEMORYSTATUS结构体的定义如下:
typedef struct _MEMORYSTATUS {
DWORD dwLength;
DWORD dwMemoryLoad;
SIZE_T dwTotalPhys;
SIZE_T dwAvailPhys;
SIZE_T dwTotalPageFile;
SIZE_T dwAvailPageFile;
SIZE_T dwTotalVirtual;
SIZE_T dwAvailVirtual;
}MEMORYSTATUS, *LPMEMORYSTATUS;
它的各个成员是:
dwLength 结构体变量的大小。
dwMemoryLoad 内存负载,从0到100,物理内存使用的大致百分比。
dwTotalPhys 真实物理内存
dwAvailPhys 可用物理内存
dwTotalPageFile
The current size of the committed memory limit, in bytes. This is physical memory plus the size of the page file, minus a small overhead.
dwAvailPageFile
The maximum amount of memory the current process can commit, in bytes. This value should be smaller than the system-wide available commit. To calculate this value, call GetPerformanceInfo and subtract the value of CommitTotal from CommitLimit.
dwTotalVirtual 总虚拟内存
dwAvailVirtual 可用虚拟内存

获得所有逻辑存储设备

DWORD WINAPI GetLogicalDrives(void);
这个函数没有参数。返回一个双字节值,由每一位代表存储设备是否存在。例如:
drives = GetLogicalDrives();
if((drives&1)!=0) printf(“a:\\”);
当drives第1位为1时,代表A盘存在。

太专业了 :? 不过对于很喜欢技术的人来说,你这里是个很去处,我也来淘宝!

@麦田, 越研究会越简单 :)

你写的东西我真是看不懂

@bolo, :roll: :x 做什么写什么

不好意思,这真看不懂……

Leave a Reply