vc編寫cpu使用率

2021-07-06 00:15:53 字數 3434 閱讀 8570

這幾天研究如何獲取cpu使用率,研究來研究去,根據別人的總結來寫一篇自己的理解文件吧

#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

system_basic_information;

typedef struct

system_performance_information;

typedef struct

system_time_information;

// ntdll!ntquerysysteminformation (nt specific!)

//// the function copies the system information of the

// specified type into a buffer

//// ntsysapi

// ntstatus

// ntapi

// ntquerysysteminformation(

// in uint systeminformationclass, // information type

// out pvoid systeminformation, // pointer to buffer

// in ulong systeminformationlength, // buffer size in bytes

// out pulong returnlength optional // pointer to a 32-bit

// // variable that receives

// // the number of bytes

// // written to the buffer

// );

typedef long (winapi *procntqsi)(uint,pvoid,ulong,pulong);

procntqsi ntquerysysteminformation;

void main(void)

; large_integer lioldsystemtime = ;

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;

printf("/ncpu usage (press any key to exit) /n");

while(!_kbhit())

// store new cpu's idle and system time

lioldidletime = sysperfinfo.liidletime;

lioldsystemtime = systimeinfo.likesystemtime;

// wait one second

sleep(1000);

}printf("/n");

}

搜了很多的文件基本上都會告訴你這麼寫可以得到cpu使用率,但發現在win7下(我的系統)執行出錯了,斷點後你會發現執行

status =ntquerysysteminformation(systemperformanceinformation,&sysperfinfo,sizeof(sysperfinfo),null);
出錯,  可能這些文章都是比較早的,那是流行的是xp   or 更早的系統,可能在哪些系統下能得到cpu使用率吧

後來找到了以下

#define _win32_winnt 0x0501

#include #include using namespace std;

__int64 comparefiletime ( filetime time1, filetime time2 )

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空閒率:" <

方法都離不開乙個概念就是cpu使用率的計算

cpu佔用率:在乙個時間段內消耗的cpu時間與該時間段長度的比值。

控制CPU使用率

我使用的是ubuntu 14.04版本,用的是自帶的系統監視器來觀察cpu使用率的變化。1.首先來說說怎麼控制cpu使用率,當程式執行乙個死迴圈的時候,使用率就會變成100 而當程式進入idle的時候,使用率就會很低 在別的程式不啟動的情況下 那麼控制cpu使用率就是調整它idle和busy的時間比...

cpu使用率統計

cat proc stat得到 user nice system idle iowait irq softirq stealstolen guest 的9元組 再採兩個夠短的時間點,做差計算即可 cat proc pid stat讀取到 pid 6873 程序號utime 1587 該任務在使用者態...

檢視CPU使用率

rem 如果wmi服務 服務名為winmgmt 壞掉了,需要到system32 webm目錄下執行如下注釋的命令 remfor i in dll do regsvr32 s i remregsvr32 s scrcons.exe remrem regsvr32 s wbemtest.exe remr...