Download code

Jump to: navigation, search

Back to CPU_usage_using_performance_counters_(C,_Windows_2000)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

cpuusageperfcnt.c

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/CPU_usage_using_performance_counters_(C,_Windows_2000)?oldid=19028
 14 */
 15 
 16 #include <windows.h>
 17 #include <pdh.h>
 18 #include <pdhmsg.h>
 19 #include <stdio.h>
 20 
 21 //------------------------------------------------------------------------------------------------------------------
 22 // Prototype(s)...
 23 //------------------------------------------------------------------------------------------------------------------
 24 int cpuusage(void);
 25 
 26 //------------------------------------------------------------------------------------------------------------------
 27 // getcpuload()
 28 //   directly prints the CPU usage on screen. This function need to be called twice with a minimum of 1 seconds
 29 //   delay (msdn guideline) to display something usefull.
 30 //   Also returns the usage in percent 0-100 where 100 means the system is working at maximum capacity.
 31 //   Note for multiprocessor systems:
 32 //   If one CPU is working at max capacity the result will (if using (_total) for PdhAddCounter() ) show a maximum
 33 //   workload of 50% unless the other CPU(s) is also working at max load. 
 34 //------------------------------------------------------------------------------------------------------------------
 35 INT getcpuload()
 36 {
 37   static PDH_STATUS            status;
 38   static PDH_FMT_COUNTERVALUE  value;
 39   static HQUERY                query;
 40   static HCOUNTER              counter;
 41   static DWORD                 ret;
 42   static char                  runonce=1;
 43   char                         cput=0;
 44 
 45   if(runonce)
 46   {
 47     status = PdhOpenQuery(NULL, 0, &query);
 48     if(status != ERROR_SUCCESS)
 49     {
 50       printf("PdhOpenQuery() ***Error: 0x%X\n",status);
 51       return 0;
 52     }
 53 
 54     PdhAddCounter(query, TEXT("\\Processor(_Total)\\% Processor Time"),0,&counter); // A total of ALL CPU's in the system
 55     //PdhAddCounter(query, TEXT("\\Processor(0)\\% Processor Time"),0,&counter);    // For systems with more than one CPU (Cpu0)
 56     //PdhAddCounter(query, TEXT("\\Processor(1)\\% Processor Time"),0,&counter);    // For systems with more than one CPU (Cpu1)
 57     runonce=0;
 58     PdhCollectQueryData(query); // No error checking here
 59     return 0;
 60   }
 61 
 62   status = PdhCollectQueryData(query);
 63   if(status != ERROR_SUCCESS)
 64   {
 65     printf("PhdCollectQueryData() ***Error: 0x%X\n",status);
 66     if(status==PDH_INVALID_HANDLE) 
 67       printf("PDH_INVALID_HANDLE\n");
 68     else if(status==PDH_NO_DATA)
 69       printf("PDH_NO_DATA\n");
 70     else
 71       printf("Unknown error\n");
 72     return 0;
 73   }
 74 
 75   status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100 ,&ret, &value);
 76   if(status != ERROR_SUCCESS)
 77   {
 78     printf("PdhGetFormattedCounterValue() ***Error: 0x%X\n",status);
 79     return 0;
 80   }
 81   cput = value.doubleValue;
 82 
 83   printf("\n\n"
 84          "CPU Total usage: %3d%%\n",cput);
 85   
 86   return cput;
 87 }
 88 
 89 //------------------------------------------------------------------------------------------------------------------
 90 // Entry point
 91 //------------------------------------------------------------------------------------------------------------------
 92 int main(void)
 93 {
 94   while(1)
 95   {
 96     getcpuload();
 97     sleep(1000);
 98   }
 99   return 0;
100 }
101 


hijacker
hijacker
hijacker
hijacker

build.log

1 /tmp/litprog4442931/cpuusageperfcnt.c:16:21: fatal error: windows.h: No such file or directory
2 compilation terminated.