//From Snooper Thread.cpp in MPI Library
while (true)
{
PDH_STATUS status;
DWORD dwValue = 6448;
HQUERY Query = 0;
HCOUNTER ProcCounter;
HCOUNTER MemCounter;
//HCOUNTER IPCounter;
// WSA doesn't have a 'wait for single event' command so wait on an array with one element
result = WSAWaitForMultipleEvents(1, &sock_event, TRUE, INFINITE, FALSE);
WSAEnumNetworkEvents(sock, sock_event, &nevents);
if (nevents.lNetworkEvents & FD_CLOSE)
{
if (Query)
PdhCloseQuery( Query );
closesocket(sock);
delete arg;
return 0;
}
else
{
if (nevents.lNetworkEvents)
{
DWORD dwCommand = 0;
LONG lProcessorValue = 0;
int iError = 0;
int iNameLength;
PDH_STATISTICS statistics;
PDH_STATISTICS statisticsmemory;
//PDH_STATISTICS statisticsIP;
PDH_RAW_COUNTER RawValue[TOTAL_PROCESSOR_LOOPS];
PDH_RAW_COUNTER RawValueMemory[TOTAL_PROCESSOR_LOOPS];
//PDH_RAW_COUNTER RawValueIP[TOTAL_PROCESSOR_LOOPS];
memset(RawValue, 0, sizeof(PDH_RAW_COUNTER) * TOTAL_PROCESSOR_LOOPS);
// read from the socket and do whatever
iError = recv(sock, (char *)&dwCommand, sizeof(DWORD), 0);
iNameLength = strlen(arg->mpi->mpi_name);
iNameLength +=1;
iError = send(sock, (char*)&iNameLength, sizeof(LONG), 0);
iError = send(sock, (char*)&arg->mpi->mpi_name, iNameLength, 0);
if (!Query)
{
status = PdhOpenQuery(NULL,// reserved
dwValue, // a value associated with this query
&Query // pointer to a buffer that will receive the
// query handle
);
status = PdhAddCounter( Query, // handle to the query
"\\System\\% Total Processor Time",
//"\\Processor(0)\\% Processor Time", // path of the counter
6445 + arg->mpi->iproc, // user-defined value
&ProcCounter // counter handle buffer
);
status = PdhAddCounter( Query, // handle to the query
"\\Memory\\% Committed Bytes In Use",
6441 + arg->mpi->iproc, // user-defined value
&MemCounter // counter handle buffer
);
/* status = PdhAddCounter( Query, // handle to the query
"\\IP\\Datagrams/sec",
//Bytes Received/sec
6450 + arg->mpi->iproc, // user-defined value
&IPCounter // counter handle buffer
);
status = PDH_CSTATUS_NO_OBJECT ;*/
}
for (int iRaw= 0; iRaw < TOTAL_PROCESSOR_LOOPS; ++iRaw)
{
status = PdhCollectQueryData(
Query // handle of the query
);
status = PdhGetRawCounterValue(
ProcCounter, // handle of the counter
NULL, // counter type
&RawValue[iRaw] // counter value
);
status = PdhGetRawCounterValue(
MemCounter, // handle of the counter
NULL, // counter type
&RawValueMemory[iRaw] // counter value
);
/* status = PdhGetRawCounterValue(
IPCounter, // handle of the counter
NULL, // counter type
&RawValueIP[iRaw] // counter value
);*/
Sleep(100);
}
status = PdhComputeCounterStatistics(
ProcCounter, // handle of the counter
PDH_FMT_LONG, // format code
0, // first entry in the array
TOTAL_PROCESSOR_LOOPS, // number of entries in the array
&RawValue[0], // array of raw counter values
&statistics // buffer for counter statistics
);
status = PdhComputeCounterStatistics(
MemCounter, // handle of the counter
PDH_FMT_LONG, // format code
0, // first entry in the array
TOTAL_PROCESSOR_LOOPS, // number of entries in the array
&RawValueMemory[0], // array of raw counter values
&statisticsmemory // buffer for counter statistics
);
/*
status = PdhComputeCounterStatistics(
IPCounter, // handle of the counter
PDH_FMT_LONG, // format code
0, // first entry in the array
TOTAL_PROCESSOR_LOOPS, // number of entries in the array
&RawValueIP[0], // array of raw counter values
&statisticsIP // buffer for counter statistics
);*/
status = PdhCollectQueryData(
Query // handle of the query
);
iError = send(sock, (char*)&statistics.mean.longValue, sizeof(LONG), 0);
iError = send(sock, (char*)&statisticsmemory.mean.longValue, sizeof(LONG), 0);
if (!arg->mpi->iproc)
{
//CString strTemp;
//sprintf(strTemp.GetBuffer(256), "Sent the Processor Data %d, Error : %d", FormatValue.longValue, iError);
//strTemp.ReleaseBuffer();
//AfxMessageBox(strTemp);
}
//sprintf(buffer, "Processor utilization is %d", FormatValue.longValue);
/*status = PdhCollectQueryData(
Query // handle of the query
);*/
}
}
}