Thread: retrieving current CPU load

  1. #1
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92

    retrieving current CPU load

    I've been searcing for hours trying to find a way to retrieve the current CPU load, but still nothing. I can find the CPU speed with HW_CPU_FREQ (sysctl), but that's all I know. Since I'm on OS X there's no /proc directory that can help me either. I need the current CPU load in a C program, but how do I find it? It has to be possible..

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The only way to find that in Windows is to gather information about the available resources and then info about how much of them are taken. Windows will not let you access any processor count registers in a C program and inline assembler does not even support them.

    As far as I'm concerned the CPU load counter in Windows is a load of crapola. Its a nice idea but very flawed as it does not represent the true load on the CPU. The memory feature however is very nice as it does show how much is taken and how much is available.


    The only true way to find the load would be to have a map of the opcodes and the clock cycles required for each. Then you would have to compute the optimum average clock cycle per instruction per the CPU - which is nearly impossible in all situations. Bad code will produce flawed results since its not the CPU's fault the code sucks.


    Unless you can gain access to the debug registers and/or count registers for your CPU you cannot accurately compute the current load on the CPU. None of my Intel manuals ever tell of an opcode that will return the current CPU load - prob since it is nearly impossible to compute accurately.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You could use popen() to open a pipe to "top", "uptime", or "ps".

    Or download the source to those apps and see how they do it (if you can).

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Upgrading my old CPU (for another old one!)
    By foxman in forum Tech Board
    Replies: 16
    Last Post: 01-11-2008, 05:41 PM
  2. Just finished assembly assignment
    By xddxogm3 in forum Tech Board
    Replies: 6
    Last Post: 10-08-2005, 04:01 PM
  3. How can I load a cpu a certain percentage?
    By Smartiepants586 in forum C Programming
    Replies: 4
    Last Post: 06-07-2005, 12:15 PM
  4. Replies: 72
    Last Post: 11-25-2002, 05:55 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM