CPU Usage weergeven in C ++

Pagina: 1
Acties:

  • Chieliot
  • Registratie: September 2001
  • Laatst online: 04-12-2025
Hier mij weer over C++, ik wil een proggie maken die je CPU usage en je Memory usage uitleest en weergeef. En dan zonder #include <window.h> ofzo gewoon die hard, wie helpt mij op weg? :)

AMD XP1800+, 512MB Intern, Maxtor 160Gig, Matrox G550 32 DDR Dual Head in een Chieftec DX-01-SLD


Verwijderd

Waarom zonder #windows.h? Het os regelt je processen en geheugen beheer, en jij wil zonder het aan het os te vragen achter die informatie komen? ehh zit er niet in er is geen *die hard* manier, je vraagt het maar netjes aan meneer windows of je krijgt het niet >:)

  • Lone Gunman
  • Registratie: Juni 1999
  • Niet online
idd...

onder nt/2k/xp gaat het dacht ik via ntdll (QuerySystemInformation) en onder win95/98/me moet je het uit het register plukken (alhoewel ik dat laatste niet zeker weet)

Experience has taught me that interest begets expectation, and expectation begets disappointment, so the key to avoiding disappointment is to avoid interest.


  • Chieliot
  • Registratie: September 2001
  • Laatst online: 04-12-2025
Oke, Oke, dan maar geen die hard manier, wie kan mij iets meer vertellen over die ntdll (QuerySystemInformation) manier. Ik gebruik namelijk win2k. Maar ik heb geen idee hoe dat zou moeten. :)

AMD XP1800+, 512MB Intern, Maxtor 160Gig, Matrox G550 32 DDR Dual Head in een Chieftec DX-01-SLD


  • Lone Gunman
  • Registratie: Juni 1999
  • Niet online
als je ff gezocht had met google had je dit ook wel kunnen vinden denk ik....

maargoed, this should do the trick.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// cpusagent.cpp (Windows NT/2000)
//
// Getting the CPU usage in percent on Windows NT/2000
//
// (c)2000 Ashot Oganesyan K, SmartLine, Inc
// mailto:ashot@aha.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("\b\b\b\b%3d%%",(UINT)dbIdleTime);
     }

      // store new CPU's idle and system time
      liOldIdleTime = SysPerfInfo.liIdleTime;
      liOldSystemTime = SysTimeInfo.liKeSystemTime;
        
      // wait one second
      Sleep(1000);
    }
    printf("\n");
}

Experience has taught me that interest begets expectation, and expectation begets disappointment, so the key to avoiding disappointment is to avoid interest.


  • Chieliot
  • Registratie: September 2001
  • Laatst online: 04-12-2025
Hee bedankt, ik ga meteen even testen of het werkt, nogmaals bedankt. *D

AMD XP1800+, 512MB Intern, Maxtor 160Gig, Matrox G550 32 DDR Dual Head in een Chieftec DX-01-SLD


  • Chieliot
  • Registratie: September 2001
  • Laatst online: 04-12-2025
Het werkt, op 1 dingetje na, op regel 97 staat:

while(!_kbhit()){

}

Dat moet volgens mij:

while(!kbhit()){

}

zijn, anders vind mijn compiler het niet leuk, verder doet ie het perfekt bedankt !!!!

AMD XP1800+, 512MB Intern, Maxtor 160Gig, Matrox G550 32 DDR Dual Head in een Chieftec DX-01-SLD


  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 21:46

Creepy

Tactical Espionage Splatterer

flinke lap code... ik geloof dat ik het maar bij de PDH interface hou... :)
code:
1
2
3
4
5
6
7
8
9
10
11
12
var phquery, phcounter: cardinal;
    pval: _pdh_fmt_countervalue;
begin
   PDHOpenQuery(nil,1,phquery);
   PdhAddCounter(phquery,pchar('\Processor(_Total)\%Processor Time'),1,phcounter);
   PdhCollectQueryData(phquery);
   PdhGetFormattedCounterValue(phcounter,PDH_FMT_LARGE,nil,pval);
   percstr:=inttostr(pval.largeValue); 
   //percstr bevat nu het percentage als string
end;
   // als je dit meerdere keren wilt opvragen dan 
   // hoef je alleen nog maar CollectQueryData en GetFormattedCounterValue aan te roepen

humz.. ok.. dit is de Delphi variant.. maar omzetten naar c lijkt me niet zo moeilijk :)

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


  • Chieliot
  • Registratie: September 2001
  • Laatst online: 04-12-2025
Kan je op deze wijze ook je geheugen gebruik uitlezen? In C++

AMD XP1800+, 512MB Intern, Maxtor 160Gig, Matrox G550 32 DDR Dual Head in een Chieftec DX-01-SLD


Verwijderd

Op vrijdag 26 oktober 2001 19:25 schreef Chieliot het volgende:
Kan je op deze wijze ook je geheugen gebruik uitlezen? In C++
Is het nou verdomme es klaar met dat gezeik om sleur en pleur code? Neem zelf es *2* minuten tijd om dit op te zoeken in msdn of de platformsdk.

Hier kijk in nog geen 30 seconden gevonden... volgende keer *ZELF* doen ok?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//  Sample output:
//  c:\>global
//  The MemoryStatus structure is 32 bytes long.
//  It should be 32.
//  78 percent of memory is in use.
//  There are   65076 total Kbytes of physical memory.
//  There are   13756 free Kbytes of physical memory.
//  There are  150960 total Kbytes of paging file.
//  There are   87816 free Kbytes of paging file.
//  There are  1fff80 total Kbytes of virtual memory.
//  There are  1fe770 free Kbytes of virtual memory.

#include <windows.h>

// Use to change the divisor from Kb to Mb.

#define DIV 1024
// #define DIV 1

char *divisor = "K";
// char *divisor = "";

// Handle the width of the field in which to print numbers this way to
// make changes easier. The asterisk in the print format specifier
// "%*ld" takes an int from the argument list, and uses it to pad and
// right-justify the number being formatted.
#define WIDTH 7

void main(int argc, char *argv[])
{
  MEMORYSTATUS stat;

  GlobalMemoryStatus (&stat);

  printf ("The MemoryStatus structure is %ld bytes long.\n",
        stat.dwLength);
  printf ("It should be %d.\n", sizeof (stat));
  printf ("%ld percent of memory is in use.\n",
        stat.dwMemoryLoad);
  printf ("There are %*ld total %sbytes of physical memory.\n",
        WIDTH, stat.dwTotalPhys/DIV, divisor);
  printf ("There are %*ld free %sbytes of physical memory.\n",
        WIDTH, stat.dwAvailPhys/DIV, divisor);
  printf ("There are %*ld total %sbytes of paging file.\n",
        WIDTH, stat.dwTotalPageFile/DIV, divisor);
  printf ("There are %*ld free %sbytes of paging file.\n",
        WIDTH, stat.dwAvailPageFile/DIV, divisor);
  printf ("There are %*lx total %sbytes of virtual memory.\n",
        WIDTH, stat.dwTotalVirtual/DIV, divisor);
  printf ("There are %*lx free %sbytes of virtual memory.\n",
        WIDTH, stat.dwAvailVirtual/DIV, divisor);
}

  • Chieliot
  • Registratie: September 2001
  • Laatst online: 04-12-2025
Bedankt, maar ik wil nog even reageren op je uitlatingen over dat *zelf* zoeken:

De dag voordat ik deze topic poste ben ik ongeveer 4 uur aan het zoeken geweest van MSDN tot andere ontwikkel sites, en waarschijnlijk zoek ik dan helemaal verkeerd, maar ik heb niets bruikbaars kunnen vinden. Misschien een topic:
Hoe vind ik de juiste source?

IIG bedankt

AMD XP1800+, 512MB Intern, Maxtor 160Gig, Matrox G550 32 DDR Dual Head in een Chieftec DX-01-SLD


Verwijderd

Online versie van msdn is ranzig gebruik gewoon die msdn cdtjes die je bij je visual C gehad hebt...

  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 21:46

Creepy

Tactical Espionage Splatterer

google is your friend.... use the google.. love the google... may the google be with you

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney

Pagina: 1