/*
Copyright (C) 2004  Melissa K. Schrumpf

This software may be distributed freely, provided that it is
distributed in its entirety, without modifications, and with
the original copyright notice and license included.

You may distribute your own separate patches together with
this software package or a modified package if you always
include a pointer where to get the original unmodified package.
In this case you must inform the author about the modified package.

The software may not be sold for profit or as "hidden" part of 
another software, but it may be included with collections 
of other free software, such as CD-ROM images of FTP servers
and similar, provided that this software is not a significant part 
of that collection.  In this case you must inform the author of the
method of distribution.

Pre-compiled binaries of this software may be distributed in the
same way, provided that this copyright notice and license is
included without modification.

This software may be used freely, for personal and non-profit
use, provided that the original author is always credited.  If you
intend to use this software as a significant part of business
(for-profit) activities, the author's permission is required.  Also,
any usage that is not covered by this license requires the explicit
permission of the author.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
*/



#include <pdh.h>
#include <stdio.h>
#include <conio.h>

int main(int argc, char *argv[]) {
	HQUERY hQuery;
	LPCTSTR szICounterPath="\\Process(Idle)\\% Processor Time";
	LPCTSTR szPCounterPath="\\Memory\\Pages/sec";
	LPCTSTR szFCounterPath="\\Memory\\Page Faults/sec";
	HCOUNTER hICounter, hPCounter, hFCounter;
	PDH_FMT_COUNTERVALUE fmtIValue, fmtPValue, fmtFValue;
	int i;

	PdhOpenQuery(NULL,1,&hQuery);
	PdhAddCounter(hQuery,szICounterPath,0,&hICounter);
	PdhAddCounter(hQuery,szPCounterPath,0,&hPCounter);
	PdhAddCounter(hQuery,szFCounterPath,0,&hFCounter);

	PdhCollectQueryData(hQuery);
	while(!_kbhit()) {
		if (ferror(stdout)) {
			break;
		}
		Sleep(1000);
		PdhCollectQueryData(hQuery);
		PdhGetFormattedCounterValue(hICounter,PDH_FMT_LONG,0,&fmtIValue);
		PdhGetFormattedCounterValue(hPCounter,PDH_FMT_LONG,0,&fmtPValue);
		PdhGetFormattedCounterValue(hFCounter,PDH_FMT_LONG,0,&fmtFValue);
		i= fmtIValue.longValue;
		if (i>100) {i=100;}
		printf("Idle:       %8u\n",i);
		printf("Pages:      %8u\n",fmtPValue.longValue);
		printf("PageFaults: %8u\n",fmtFValue.longValue);
		fflush(stdout);
	}
	PdhCloseQuery(hQuery);
	return 0;
}

