Average CPU load on Windows?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Average CPU load on Windows?

Post by kaisellgren »

Hi,

Does anyone have an idea how to get the average CPU load on Windows?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Average CPU load on Windows?

Post by McInfo »

I'm assuming you want a command-line tool that you can call from PHP? Tasklist provides some useful information on running processes and memory usage.

Code: Select all

<?php
header('Content-Type: text/plain');
echo system('tasklist /V');
?>
The ouput is too extensive to paste here.

For something shorter, this call filters the output for processes where the image name is "System Idle Process".

Code: Select all

tasklist /FI "IMAGENAME eq System Idle Process"
The output is something like

Code: Select all

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0         24 K
If the memory usage of the System Idle Process is low, the CPU load is low as well.

I'll keep looking for the proper command-line tool.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:07 pm, edited 1 time in total.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Average CPU load on Windows?

Post by mikemike »

I posted a C app to someone earlier. Let me find it...
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Average CPU load on Windows?

Post by mikemike »

User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Average CPU load on Windows?

Post by McInfo »

Thanks, mikemike. Because of the code you posted, I was able to find information on the Performance Command Line Tools and discovered that the performance counters can be accessed using typeperf.

Command line:

Code: Select all

C:>typeperf -?
Output:

Code: Select all

Microsoft r TypePerf.exe (6.0.6000.16386)
 
Typeperf writes performance data to the command window or to a log file. To
stop Typeperf, press CTRL+C.
 
Usage:
typeperf { <counter [counter ...]>
                                | -cf <filename>
                                | -q [object]
                                | -qx [object]
                                } [options]
 
Parameters:
  <counter [counter ...]>       Performance counters to monitor.
 
Options:
  -?                            Displays context sensitive help.
  -f <CSV|TSV|BIN|SQL>          Output file format. Default is CSV.
  -cf <filename>                File containing performance counters to
                                monitor, one per line.
  -si <[[hh:]mm:]ss>            Time between samples. Default is 1 second.
  -o <filename>                 Path of output file or SQL database. Default
                                is STDOUT.
  -q [object]                   List installed counters (no instances). To
                                list counters for one object, include the
                                object name, such as Processor.
  -qx [object]                  List installed counters with instances. To
                                list counters for one object, include the
                                object name, such as Processor.
  -sc <samples>                 Number of samples to collect. Default is to
                                sample until CTRL+C.
  -config <filename>            Settings file containing command options.
  -s <computer_name>            Server to monitor if no server is specified
                                in the counter path.
  -y                            Answer yes to all questions without prompting.
 
Note:
  Counter is the full name of a performance counter in
  "\\<Computer>\<Object>(<Instance>)\<Counter>" format,
  such as "\\Server1\Processor(0)\% User Time".
 
Examples:
  typeperf "\Processor(_Total)\% Processor Time"
  typeperf -cf counters.txt -si 5 -sc 50 -f TSV -o domain2.tsv
  typeperf -qx PhysicalDisk -o counters.txt
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:09 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Average CPU load on Windows?

Post by McInfo »

This script displays the % Processor Time for all processors in CSV format and appends an update every second until you hit the stop button in your browser.

Code: Select all

<?php
header('Content-Type: text/plain');
echo system('typeperf "\Processor(*)\% Processor Time"');
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:09 pm, edited 1 time in total.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Average CPU load on Windows?

Post by kaisellgren »

Thank you. I knew this could be accomplished. :)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Average CPU load on Windows?

Post by mikemike »

:)welcome
madhuj1260
Forum Newbie
Posts: 5
Joined: Wed Jun 10, 2009 3:26 am

Re: Average CPU load on Windows?

Post by madhuj1260 »

Is there any command for web application?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Average CPU load on Windows?

Post by McInfo »

The system() function makes command-line programs accessible through Web applications. If you are looking for a different answer, you need to be more specific.

Edit: This post was recovered from search engine cache.
Post Reply