heh.pl
Kanał informacyjny Heh.pl


Sobota 21 grudnia 2024 r.

artykuły | abc komputera (archiwum) | forum dyskusyjne | redakcja


Temat

Jak z linii polecen sprawdzic obciazenie kompa?


80.55.48.* napisał:
Jak z linii polecen sprawdzic obciazenie kompa (% zajetosc procesora)? Efekt/wynik ma sie wyswietlic w trybie tekstowym albo zapisac do pliku.
Moge ew. uzyc czegos w rodzaju program.exe>wynik.txt tylko nie wiem jaki prg.

62.111.232.* napisał:
top

80.55.48.* napisał:
W windzie......

62.111.232.* napisał:
aha icon_razz.gif
malo jest programikow tekstowych dla tego sysa. przyznam sie, ze nie widzialem takiego.
jak masz pol godzikni to mozesz napisac icon_smile3.gif nie mowie tego zlosliwie icon_smile3.gif

80.55.48.* napisał:
moze i tak tylko nie wiem jakiej funcji API uzyc....

62.111.232.* napisał:
CODE
Here is a document I got from a web site:



The one of most popular question I see in the newsgroups is a question about



receiving and calculating the CPU usage, so I've decided to describe it in



this trick. At first, I should say that there is a big difference between



getting the CPU usage on Windows 95/98 and on Windows NT/2000, in this



article I'll show you a method that perfectly works on Windows 9x (for



example in my free screen saver - CPU Indicator SS ). About getting the CPU



usage on NT you'll read in my next trick.



Like NT the Windows 9x has the Performance Counters but on Windows 9x they



are more easy to use so we will use one of these counters to receive the CPU



usage.



Our first step is starting selected counter because by default all counters



are stoped. To start the counter we should read the corresponding registry



key:



HKEY hkey;



RegOpenKeyEx ( HKEY_DYN_DATA,



"PerfStats\\StartStat",



0,KEY_ALL_ACCESS,



&hkey );



DWORD dwDataSize;



DWORD dwType;



DWORD dwDummy;



dwDataSize=sizeof(dwDummy); RegQueryValueEx ( hkey,



"KERNEL\\CPUUsage",



NULL,&dwType,



(LPBYTE)&dwDummy,



&dwDataSize );



RegCloseKey(hkey);



The next step is reading the CPU usage directly from the registry:



HKEY hkey;



RegOpenKeyEx ( HKEY_DYN_DATA,



"PerfStats\\StatData",



0,KEY_ALL_ACCESS,



&hkey );



DWORD dwDataSize;



DWORD dwType;



DWORD dwCpuUsage;



dwDataSize=sizeof(dwCpuUsage); RegQueryValueEx ( hkey,



"KERNEL\\CPUUsage",



NULL,&dwType,



(LPBYTE)&dwCpuUsage,



&dwDataSize );



RegCloseKey(hkey);



Now the dwCpuUsage variable contains current CPU usage in percent. You



should call above code in the loop to see the dynamic changes.



And the final step is stoping the counter previously started:



HKEY hkey;



RegOpenKeyEx ( HKEY_DYN_DATA,



"PerfStats\\StopStat",



0,KEY_ALL_ACCESS,



&hkey );



DWORD dwDataSize;



DWORD dwType;



DWORD dwDummy;



dwDataSize=sizeof(dwDummy); RegQueryValueEx ( hkey,



"KERNEL\\CPUUsage",



NULL,&dwType,



(LPBYTE)&dwDummy,



&dwDataSize );



RegCloseKey(hkey);















-------------------



















#include <winreg.h>



int getCpuUsage(void)



{



 int trug=-1;



 DWORD rc;



 HKEY hOpen;



 LPVOID lpMsgBuf;



 unsigned char data[4];



 unsigned long size=4;



 bool error=false;



 if ((rc = RegOpenKeyEx(HKEY_DYN_DATA,"PerfStats\\StatData", 0,KEY_READ,



&hOpen)) == ERROR_SUCCESS)



 {



   if ((rc=RegQueryValueEx(hOpen,"KERNEL\\CPUUsage",NULL,NULL,(unsigned



char *)data,&size)) == ERROR_SUCCESS)



     trug=data[0];



   else error=true;



   RegCloseKey(hOpen);



 }



 else error=true;



 if (error)



 {



   FormatMessage(



     FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,



     NULL,



     rc,



     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),



     (LPSTR) &lpMsgBuf,



     0,



     NULL);



   MessageBox( NULL, (LPSTR)lpMsgBuf, "Fout", MB_OK|MB_ICONINFORMATION );



   LocalFree( lpMsgBuf );



 } return trug;



}











------------------------















#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   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;



// 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)



{



   SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;



   SYSTEM_TIME_INFORMATION        SysTimeInfo;



   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;



   



printf("nCPU Usage (press any key to exit):    ");



   while(!_kbhit())



   {



       // get new system time



    status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);



       if (status!=NO_ERROR)



           return;



       // get new CPU's idle time



       status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);



       if (status != NO_ERROR)



           return;



       // if it's a first call - skip it



      if (liOldIdleTime.QuadPart != 0)



      {



           // CurrentValue = NewValue - OldValue



           dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);



           dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);



           // CurrentCpuIdle = IdleTime / SystemTime



           dbIdleTime = dbIdleTime / dbSystemTime;



           // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors



           dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;



           printf("bbbb%3d%%",(UINT)dbIdleTime);



      }



       // store new CPU's idle and system time



       liOldIdleTime = SysPerfInfo.liIdleTime;



       liOldSystemTime = SysTimeInfo.liKeSystemTime;



 



       // wait one second



       Sleep(1000);



   } printf("n");



}











--------------------











// cpusagent.cpp (Windows NT/2000)



//



// Getting the CPU usage in percent on Windows NT/2000



//



// (c)2000 Ashot Oganesyan K, SmartLine, Inc



// mailto:ashotaha.ru, http://www.protect-me.com, http://www.codepile.com







#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   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;











// 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)



{



   SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;



   SYSTEM_TIME_INFORMATION        SysTimeInfo;



   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;



   



printf("nCPU Usage (press any key to exit):    ");



   while(!_kbhit())



   {



       // get new system time



    status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);



       if (status!=NO_ERROR)



           return;







       // get new CPU's idle time



       status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);



       if (status != NO_ERROR)



           return;







       // if it's a first call - skip it



      if (liOldIdleTime.QuadPart != 0)



      {



           // CurrentValue = NewValue - OldValue



           dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);



           dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);







           // CurrentCpuIdle = IdleTime / SystemTime



           dbIdleTime = dbIdleTime / dbSystemTime;







           // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors



           dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;







           printf("bbbb%3d%%",(UINT)dbIdleTime);



      }







       // store new CPU's idle and system time



       liOldIdleTime = SysPerfInfo.liIdleTime;



       liOldSystemTime = SysTimeInfo.liKeSystemTime;



 



       // wait one second



       Sleep(1000);



   }



   printf("n");



}




sie mi po dysku poniewieralo, moze cos wyskubiesz z tego. pewnie gdzies jeszcze mam chodzacy program icon_smile3.gif

tak, wiem ze duzo wkleilem ;]

80.55.48.* napisał:
To moze daj to juz skompilowane... icon_smile3.gif Bo ja nie mam zadnego kompilatora do C icon_razz.gif I dla takiej pie.rdołki nie chce nic nowego instalowac...

Bede "dźwięczny"....

80.55.48.* napisał:
Znalazlem odpowiednie narzedzie w Windows 2000 Resource Kit:

Dzial identycznie jak uniksowe top

http://www.dynawell.com/reskit/microsoft/win2000/pmon.zip

Podobne tematy


Działy









Copyright © 2002-2024 | Prywatność | Load: 0.76 | SQL: 1 | Uptime: 142 days, 13:38 h:m | Wszelkie uwagi prosimy zgłaszać pod adresem eddy@heh.pl