Wenn ich bei XP STRG+ALT+ENTF drücke, kann ich ja die CPU Auslastung, in Prozent anschauen. Ich möchte gern ein Programm schreiben was einem diesen Wert anzeigt. Wie geht das? Was ist die CPU Auslastung überhaupt? Ist es auch möglich die CPU Auslastung einzelner Prozesse anzuzeigen?
Ist eigentlich nicht allzu kompliziert:
Einfach GetProccesTimes mehrmals aufrufen, dann teilst du die Zeitdifferenz der beiden Aufrufe durch die dazwischen vergangene Zeit - und schon hast du die Auslastung
_________________ ( Moderator im Webzeugs- und WinAPI-Forum )
Zuletzt bearbeitet von flenders am 22:55:04 21.09.2004, insgesamt 2-mal bearbeitet
Wo ist dein Problem mit den Parametern? Der erste ist das Prozess-Handle, und dann eben 4 Pointer auf FILETIME-Strukturen.
Zur Messung der Zeitdifferenz: Wieso sollten Millisekunden nicht genau genug sein?! Du misst einfach die Zeit im Aktualisierungsintervall - z.B. 1 Sekunde o.ä.
_________________ ( Moderator im Webzeugs- und WinAPI-Forum )
..... Ich möchte gern ein Programm schreiben was einem .....
joomoo schrieb:
Das versteh ich alles nicht. Wenn es so einfach ist, könntest du mir nicht dann den fertigen Code geben? bitte!
Was nun schreiben" oder "basteln"? (Copy´n Paste)
Ich denke mal du kommst mit der 64Bit Struktur (2 x 32Bit) nicht klar, oder weißt du nicht wie man eine Differenz in Prozent rechnet?
Du kannst auch mit "FileTimeToSystemTime(const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime);"
das in SystemTime umwandeln, vieleicht kommst du damit besser klar.
_________________ Fragt Floppy den HDD, warum bist du größer? Da sagt HDD: weil ich Hard bin und Du nur nen Flop
Zur Initialisierung musst du die Funktion erst einmal so aufrufen :)
#
Er sagt
14 untitled1.cpp
`union _ULARGE_INTEGER' has no member named `HighPart'
Aber sonst keine Fehler. Wie kann ich dieses Problem umgehen? (ich hab dev-c++)
..... Ich möchte gern ein Programm schreiben was einem .....
joomoo schrieb:
Das versteh ich alles nicht. Wenn es so einfach ist, könntest du mir nicht dann den fertigen Code geben? bitte!
Was nun schreiben" oder "basteln"? (Copy´n Paste)
Ich denke mal du kommst mit der 64Bit Struktur (2 x 32Bit) nicht klar, oder weißt du nicht wie man eine Differenz in Prozent rechnet?
Du kannst auch mit "FileTimeToSystemTime(const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime);"
das in SystemTime umwandeln, vieleicht kommst du damit besser klar.
Wenn du etwas nicht KANNST, dann kannst du es nicht einfach schreiben. Wenn dir jemand zeigt wies geht und du dirs kopierst, wirstes nicht checken, aber später sagste vielleicht: AHHH! Genau! Jetzt check ich det.
64Bit Struktur? Hä? Mit Prozent kann ich, hatten wir in der Schule. Mit dem FileTime komm ich nicht klar. Was soll ich mit der SystemTime??? Ich check gar nichts.
Also ich mach erst ein Führerschein bevor ich Auto fahre und guck mir das nicht vom Kumpel ab, denn dann bau ich garantiert ein Unfall!!!
Ich will damit sagen das du dich vieleicht erst mal kurz (wirklich kurz) mit
dieser Datenstruktur auseinandersetzt, sie ist im Prinzip einfach, auch wenn
es in der MSDN besch...eiden beschrieben ist und wenn du das dann mit Hi und Low Word und DWORD so wie die Struct von Filetime einigermassen weißt, ließt
sich der Code von flenders wie was Selbstgeschriebenes.
Nun kopierst du den Code und schon hast du einen Unfall (Fehlermeldung) und
weißt nicht warum.
ULARGE_INTEGER ist in winnt.h deklardiert und includen muß du windows.h (wirst du ja haben), da du devcpp verwendest kann es sein das du noch ne lib zu linken
muß mit -l***, aber er hat in jedem Fall nen Member namens DWORD HighPart.
_________________ Fragt Floppy den HDD, warum bist du größer? Da sagt HDD: weil ich Hard bin und Du nur nen Flop
Nein ich habe es gerade beim devcpp getestet, er akzeptiert die obere Struct von der UNION, also liegt der Fehler woanders aber auch nur wenn er den gleichen Compiler und Linker verwendet wie ich.
Dev-C++: Mingw32 gcc
_________________ Fragt Floppy den HDD, warum bist du größer? Da sagt HDD: weil ich Hard bin und Du nur nen Flop
Hallo,
ich habe jetzt nicht alles gelesen und weis nicht wie weit ihr siet aber ich habe vor einer wiele ein programm geschieben was cpu-auslastung für global und für einzelne prozesse anzeigen kann.
HKEY_DYNAMIC_DATA benutze ich bei mir (w98se), ich hatte mal vor ewigkeiten ein tut in VB drüber gelesen und es in C++ umgesetzt, nur bin ich mir sicher das es auch anders geht, den Windows muß diesen Wert auch ermitteln und in die Reg schreiben.
_________________ Fragt Floppy den HDD, warum bist du größer? Da sagt HDD: weil ich Hard bin und Du nur nen Flop
Klar muss auch Windows diesen Wert irgendwie ermitteln, aber das heißt deswegen noch lange nicht, dass du noch anders an diesen Wert kommst. Es kann natürlich sein, dass es noch eine Möglichkeit gibt, aber Windows kümmert sich eben und die Zuteilung der Rechenzeit, und weiß von daher, wieviel Rechenzeit der Windows-Leerlauf-Prozess bekommt (denke mal, dass das irgendwie so ermittelt wird)
_________________ ( Moderator im Webzeugs- und WinAPI-Forum )
Wegen der Unterstützung für 98, Me ... Systeme: Hat Ms den Task Manager zu NT Systemen nicht gründlich geändert. Wenn ich mich richtig erinnere, gab es damals solche Systeminfos Systemauslastung nicht im Taskmanager. (Da gabs auch noch bugs das nicht alle Programme aufgeführt wurden, spielte bei Viren mal ne Rolle).
-> Wenn Ms es damals nicht den Taskmanager eingebaut hat, war es entweder zu blöd dazu (besser hat es vergessen) oder es war zu schwirig diese Werte zu ermitteln.
Was wohl möglich ist ist die Ramauslastung rauszukriegen (IMHO), aber das ist ja nicht die Systemauslastung.
Zuletzt bearbeitet von flammenvogel am 21:38:03 27.09.2004, insgesamt 1-mal bearbeitet
Ich habe mich ein wenig Schlau gemacht, aber noch nicht Schlau genug um Endergebnisse zu liefern, aber so wie es Aussieht muß man das ziemlich hardware nah machen, sprich Assembler (Eingebettet) aber auch da giebt es noch Hürden.
Also für mich selbst reicht meine alte variante, ich brauch nicht von jeden die Auslastung.
_________________ Fragt Floppy den HDD, warum bist du größer? Da sagt HDD: weil ich Hard bin und Du nur nen Flop
//
// The performance data is accessed through the registry key
// HKEY_PEFORMANCE_DATA.
// However, although we use the registry to collect performance data,
// the data is not stored in the registry database.
// Instead, calling the registry functions with the HKEY_PEFORMANCE_DATA key
// causes the system to collect the data from the appropriate system
// object managers.
//
// QueryPerformanceData allocates memory block for getting the
// performance data.
//
// void QueryPerformanceData(PERF_DATA_BLOCK **pPerfData)
{
//
// Since i want to use the same allocated area for each query,
// i declare CBuffer as static.
// The allocated is changed only when RegQueryValueEx return ERROR_MORE_DATA
// static CBuffer Buffer(TOTALBYTES);
DWORD BufferSize = Buffer.GetSize();
LONG lRes;
Buffer.Reset();
while( (lRes = RegQueryValueEx( HKEY_PERFORMANCE_DATA,
"Global",
NULL,
NULL,
Buffer,
&BufferSize )) == ERROR_MORE_DATA )
{
// Get a buffer that is big enough.
//
// GetCounterValue gets performance object structure
// and returns the value of given counter index .
// This functions iterates through the counters of the input object
// structure and looks for the given counter index.
//
// For objects that have instances, this function returns the counter value
// of the instance pInstanceName.
//
T GetCounterValue(PPERF_OBJECT_TYPE pPerfObj, DWORD dwCounterIndex, LPCTSTR pInstanceName)
{
PPERF_COUNTER_DEFINITION pPerfCntr = NULL;
PPERF_INSTANCE_DEFINITION pPerfInst = NULL;
PPERF_COUNTER_BLOCK pCounterBlock = NULL;
// Look for instance pInstanceName
_bstr_t bstrInstance;
_bstr_t bstrInputInstance = pInstanceName;
for( int k=0; k < pPerfObj->NumInstances; k++ )
{
bstrInstance = (wchar_t *)((PBYTE)pPerfInst + pPerfInst->NameOffset);
if (!stricmp((LPCTSTR)bstrInstance, (LPCTSTR)bstrInputInstance))
{
pCounterBlock = (PPERF_COUNTER_BLOCK) ((LPBYTE) pPerfInst + pPerfInst->ByteLength);
break;
}
// Get the next instance.
pPerfInst = NextInstance( pPerfInst );
}
}
if (pCounterBlock)
{
T *lnValue = NULL;
lnValue = (T*)((LPBYTE) pCounterBlock + pPerfCntr->CounterOffset);
return *lnValue;
}
return -1;
}
/*****************************************************************
* *
* Functions used to navigate through the performance data. *
* *
*****************************************************************/
//
// The performance data is accessed through the registry key
// HKEY_PEFORMANCE_DATA.
// However, although we use the registry to collect performance data,
// the data is not stored in the registry database.
// Instead, calling the registry functions with the HKEY_PEFORMANCE_DATA key
// causes the system to collect the data from the appropriate system
// object managers.
//
// QueryPerformanceData allocates memory block for getting the
// performance data.
//
// void QueryPerformanceData(PERF_DATA_BLOCK **pPerfData)
{
//
// Since i want to use the same allocated area for each query,
// i declare CBuffer as static.
// The allocated is changed only when RegQueryValueEx return ERROR_MORE_DATA
// static CBuffer Buffer(TOTALBYTES);
DWORD BufferSize = Buffer.GetSize();
LONG lRes;
Buffer.Reset();
while( (lRes = RegQueryValueEx( HKEY_PERFORMANCE_DATA,
"Global",
NULL,
NULL,
Buffer,
&BufferSize )) == ERROR_MORE_DATA )
{
// Get a buffer that is big enough.
//
// GetCounterValue gets performance object structure
// and returns the value of given counter index .
// This functions iterates through the counters of the input object
// structure and looks for the given counter index.
//
// For objects that have instances, this function returns the counter value
// of the instance pInstanceName.
//
T GetCounterValue(PPERF_OBJECT_TYPE pPerfObj, DWORD dwCounterIndex, LPCTSTR pInstanceName)
{
PPERF_COUNTER_DEFINITION pPerfCntr = NULL;
PPERF_INSTANCE_DEFINITION pPerfInst = NULL;
PPERF_COUNTER_BLOCK pCounterBlock = NULL;
// Look for instance pInstanceName
_bstr_t bstrInstance;
_bstr_t bstrInputInstance = pInstanceName;
for( int k=0; k < pPerfObj->NumInstances; k++ )
{
bstrInstance = (wchar_t *)((PBYTE)pPerfInst + pPerfInst->NameOffset);
if (!stricmp((LPCTSTR)bstrInstance, (LPCTSTR)bstrInputInstance))
{
pCounterBlock = (PPERF_COUNTER_BLOCK) ((LPBYTE) pPerfInst + pPerfInst->ByteLength);
break;
}
// Get the next instance.
pPerfInst = NextInstance( pPerfInst );
}
}
if (pCounterBlock)
{
T *lnValue = NULL;
lnValue = (T*)((LPBYTE) pCounterBlock + pPerfCntr->CounterOffset);
return *lnValue;
}
return -1;
}
/*****************************************************************
* *
* Functions used to navigate through the performance data. *
* *
*****************************************************************/
//
// The performance data is accessed through the registry key
// HKEY_PEFORMANCE_DATA.
// However, although we use the registry to collect performance data,
// the data is not stored in the registry database.
// Instead, calling the registry functions with the HKEY_PEFORMANCE_DATA key
// causes the system to collect the data from the appropriate system
// object managers.
//
// QueryPerformanceData allocates memory block for getting the
// performance data.
//
// void QueryPerformanceData(PERF_DATA_BLOCK **pPerfData)
{
//
// Since i want to use the same allocated area for each query,
// i declare CBuffer as static.
// The allocated is changed only when RegQueryValueEx return ERROR_MORE_DATA
// static CBuffer Buffer(TOTALBYTES);
DWORD BufferSize = Buffer.GetSize();
LONG lRes;
Buffer.Reset();
while( (lRes = RegQueryValueEx( HKEY_PERFORMANCE_DATA,
"Global",
NULL,
NULL,
Buffer,
&BufferSize )) == ERROR_MORE_DATA )
{
// Get a buffer that is big enough.
//
// GetCounterValue gets performance object structure
// and returns the value of given counter index .
// This functions iterates through the counters of the input object
// structure and looks for the given counter index.
//
// For objects that have instances, this function returns the counter value
// of the instance pInstanceName.
//
T GetCounterValue(PPERF_OBJECT_TYPE pPerfObj, DWORD dwCounterIndex, LPCTSTR pInstanceName)
{
PPERF_COUNTER_DEFINITION pPerfCntr = NULL;
PPERF_INSTANCE_DEFINITION pPerfInst = NULL;
PPERF_COUNTER_BLOCK pCounterBlock = NULL;
// Look for instance pInstanceName
_bstr_t bstrInstance;
_bstr_t bstrInputInstance = pInstanceName;
for( int k=0; k < pPerfObj->NumInstances; k++ )
{
bstrInstance = (wchar_t *)((PBYTE)pPerfInst + pPerfInst->NameOffset);
if (!stricmp((LPCTSTR)bstrInstance, (LPCTSTR)bstrInputInstance))
{
pCounterBlock = (PPERF_COUNTER_BLOCK) ((LPBYTE) pPerfInst + pPerfInst->ByteLength);
break;
}
// Get the next instance.
pPerfInst = NextInstance( pPerfInst );
}
}
if (pCounterBlock)
{
T *lnValue = NULL;
lnValue = (T*)((LPBYTE) pCounterBlock + pPerfCntr->CounterOffset);
return *lnValue;
}
return -1;
}
/*****************************************************************
* *
* Functions used to navigate through the performance data. *
* *
*****************************************************************/
///////////////////////////////////////////////////////////////////
//
// GetCpuUsage uses the performance counters to retrieve the
// system cpu usage.
// The cpu usage counter is of type PERF_100NSEC_TIMER_INV
// which as the following calculation:
//
// Element Value
// ======= ===========
// X CounterData
// Y 100NsTime
// Data Size 8 Bytes
// Time base 100Ns
// Calculation 100*(1-(X1-X0)/(Y1-Y0))
//
// where the denominator (Y) represents the total elapsed time of the
// sample interval and the numerator (X) represents the time during
// the interval when the monitored components were inactive.
//
//
// Note:
// ====
// On windows NT, cpu usage counter is '% Total processor time'
// under 'System' object. However, in Win2K/XP Microsoft moved
// that counter to '% processor time' under '_Total' instance
// of 'Processor' object.
// Read 'INFO: Percent Total Performance Counter Changes on Windows 2000'
// Q259390 in MSDN.
//
///////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <atlbase.h> // for CRegKey use
#pragma pack(push,8)
#include "PerfCounters.h"
#pragma pack(pop)
#define SYSTEM_OBJECT_INDEX 2 // 'System' object
#define PROCESS_OBJECT_INDEX 230 // 'Process' object
#define PROCESSOR_OBJECT_INDEX 238 // 'Processor' object
#define TOTAL_PROCESSOR_TIME_COUNTER_INDEX 240 // '% Total processor time' counter (valid in WinNT under 'System' object)
#define PROCESSOR_TIME_COUNTER_INDEX 6 // '% processor time' counter (for Win2K/XP)
//
// GetCpuUsage returns the cpu usage.
// Since we calculate the cpu usage by two samplings, the first
// call to GetCpuUsage() returns 0 and keeps the values for the next
// sampling.
// Read the comment at the beginning of this file for the formula.
// int GetCpuUsage()
{
static bool bFirstTime = true;
static LONGLONG lnOldValue = 0;
static LARGE_INTEGER OldPerfTime100nSec = {0};
static PLATFORM Platform = GetPlatform();
if (bFirstTime)
EnablePerformaceCounters();
// Cpu usage counter is 8 byte length.
CPerfCounters<LONGLONG> PerfCounters;
char szInstance[256] = {0};
// Note:
// ====
// On windows NT, cpu usage counter is '% Total processor time'
// under 'System' object. However, in Win2K/XP Microsoft moved
// that counter to '% processor time' under '_Total' instance
// of 'Processor' object.
// Read 'INFO: Percent Total Performance Counter Changes on Windows 2000'
// Q259390 in MSDN.
///////////////////////////////////////////////////////////////////
//
// GetCpuUsage uses the performance counters to retrieve the
// system cpu usage.
// The cpu usage counter is of type PERF_100NSEC_TIMER_INV
// which as the following calculation:
//
// Element Value
// ======= ===========
// X CounterData
// Y 100NsTime
// Data Size 8 Bytes
// Time base 100Ns
// Calculation 100*(1-(X1-X0)/(Y1-Y0))
//
// where the denominator (Y) represents the total elapsed time of the
// sample interval and the numerator (X) represents the time during
// the interval when the monitored components were inactive.
//
//
// Note:
// ====
// On windows NT, cpu usage counter is '% Total processor time'
// under 'System' object. However, in Win2K/XP Microsoft moved
// that counter to '% processor time' under '_Total' instance
// of 'Processor' object.
// Read 'INFO: Percent Total Performance Counter Changes on Windows 2000'
// Q259390 in MSDN.
//
///////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <atlbase.h> // for CRegKey use
#pragma pack(push,8)
#include "PerfCounters.h"
#pragma pack(pop)
#define SYSTEM_OBJECT_INDEX 2 // 'System' object
#define PROCESS_OBJECT_INDEX 230 // 'Process' object
#define PROCESSOR_OBJECT_INDEX 238 // 'Processor' object
#define TOTAL_PROCESSOR_TIME_COUNTER_INDEX 240 // '% Total processor time' counter (valid in WinNT under 'System' object)
#define PROCESSOR_TIME_COUNTER_INDEX 6 // '% processor time' counter (for Win2K/XP)
//
// GetCpuUsage returns the cpu usage.
// Since we calculate the cpu usage by two samplings, the first
// call to GetCpuUsage() returns 0 and keeps the values for the next
// sampling.
// Read the comment at the beginning of this file for the formula.
// int GetCpuUsage()
{
static bool bFirstTime = true;
static LONGLONG lnOldValue = 0;
static LARGE_INTEGER OldPerfTime100nSec = {0};
static PLATFORM Platform = GetPlatform();
if (bFirstTime)
EnablePerformaceCounters();
// Cpu usage counter is 8 byte length.
CPerfCounters<LONGLONG> PerfCounters;
char szInstance[256] = {0};
// Note:
// ====
// On windows NT, cpu usage counter is '% Total processor time'
// under 'System' object. However, in Win2K/XP Microsoft moved
// that counter to '% processor time' under '_Total' instance
// of 'Processor' object.
// Read 'INFO: Percent Total Performance Counter Changes on Windows 2000'
// Q259390 in MSDN.
///////////////////////////////////////////////////////////////////
//
// GetCpuUsage uses the performance counters to retrieve the
// system cpu usage.
// The cpu usage counter is of type PERF_100NSEC_TIMER_INV
// which as the following calculation:
//
// Element Value
// ======= ===========
// X CounterData
// Y 100NsTime
// Data Size 8 Bytes
// Time base 100Ns
// Calculation 100*(1-(X1-X0)/(Y1-Y0))
//
// where the denominator (Y) represents the total elapsed time of the
// sample interval and the numerator (X) represents the time during
// the interval when the monitored components were inactive.
//
//
// Note:
// ====
// On windows NT, cpu usage counter is '% Total processor time'
// under 'System' object. However, in Win2K/XP Microsoft moved
// that counter to '% processor time' under '_Total' instance
// of 'Processor' object.
// Read 'INFO: Percent Total Performance Counter Changes on Windows 2000'
// Q259390 in MSDN.
//
///////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <atlbase.h> // for CRegKey use
#pragma pack(push,8)
#include "PerfCounters.h"
#pragma pack(pop)
#define SYSTEM_OBJECT_INDEX 2 // 'System' object
#define PROCESS_OBJECT_INDEX 230 // 'Process' object
#define PROCESSOR_OBJECT_INDEX 238 // 'Processor' object
#define TOTAL_PROCESSOR_TIME_COUNTER_INDEX 240 // '% Total processor time' counter (valid in WinNT under 'System' object)
#define PROCESSOR_TIME_COUNTER_INDEX 6 // '% processor time' counter (for Win2K/XP)
//
// GetCpuUsage returns the cpu usage.
// Since we calculate the cpu usage by two samplings, the first
// call to GetCpuUsage() returns 0 and keeps the values for the next
// sampling.
// Read the comment at the beginning of this file for the formula.
// int GetCpuUsage()
{
static bool bFirstTime = true;
static LONGLONG lnOldValue = 0;
static LARGE_INTEGER OldPerfTime100nSec = {0};
static PLATFORM Platform = GetPlatform();
if (bFirstTime)
EnablePerformaceCounters();
// Cpu usage counter is 8 byte length.
CPerfCounters<LONGLONG> PerfCounters;
char szInstance[256] = {0};
// Note:
// ====
// On windows NT, cpu usage counter is '% Total processor time'
// under 'System' object. However, in Win2K/XP Microsoft moved
// that counter to '% processor time' under '_Total' instance
// of 'Processor' object.
// Read 'INFO: Percent Total Performance Counter Changes on Windows 2000'
// Q259390 in MSDN.
Nächstes Thema anzeigen Vorheriges Thema anzeigen
Sie können keine Beiträge in dieses Forum schreiben. Sie können auf Beiträge in diesem Forum nicht antworten. Sie können Ihre Beiträge in diesem Forum nicht bearbeiten. Sie können Ihre Beiträge in diesem Forum nicht löschen. Sie können an Umfragen in diesem Forum nicht mitmachen.
c++.de ist Teilnehmer des Partnerprogramms von Amazon Europe S.à.r.l. und Partner des Werbeprogramms, das zur Bereitstellung eines Mediums
für Websites konzipiert wurde, mittels dessen durch die Platzierung von Werbeanzeigen und Links zu amazon.de
Werbekostenerstattung verdient werden kann.
Die Vervielfältigung der auf den Seiten www.c-plusplus.de, www.c-plusplus.info, www.c-sar.de, www.c-plusplus.net und www.baeckmann.de
enthaltenen Informationen ohne eine schriftliche Genehmigung des Seitenbetreibers ist untersagt
(vgl. §4 Urheberrechtsgesetz). Die Nutzung und Änderung der vorgestellten Strukturen und Verfahren in
privaten und kommerziellen Softwareanwendungen ist ausdrücklich erlaubt, soweit keine Rechte Dritter verletzt werden.
Der Seitenbetreiber übernimmt keine Gewähr für die Funktion einzelner Beiträge oder Programmfragmente, insbesondere
übernimmt er keine Haftung für eventuelle aus dem Gebrauch entstehenden Folgeschäden.