Accurately measuring the current load on a Mac

Discussion in 'Mac Programming' started by Cromulent, Jun 26, 2009.

  1. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Messages:
    5,988
    Location:
    The Land of Hope and Glory
    #1
    I'm trying to add a little feature that takes the current load on a computer and then sends it back to a central server (same computer or different computer it does not really matter) and then the server routes network requests based on the load of the registered servers with it.

    What is the best way of getting current load on a Mac for a system such as this? It is a C only application I am afraid so no Cocoa / Objective-C.
     
  2. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Messages:
    981
    Location:
    Austin, TX
    #2
    Use the stdlib function "getloadavg()" like this:

    Code:
    double   loads[3];
    
    getloadavg(loads, 3);
    That gives the three load values like you see in:

    Code:
    TwinTurboG5:~ nerfle$ uptime
     9:44  up 23:22, 2 users, load averages: 1.45 1.31 1.12
     
  3. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Messages:
    5,988
    Location:
    The Land of Hope and Glory
    #3
    The problem with that is it is not accurate enough. The servers performance will be quite variable and as such I think the load average might not offer the kind of information I need.

    I mean obviously it does give you an idea, but it is still reasonably general.

    Edit : I guess it might actually do the job if I just call it more than once a minute and take the moving average of the average load over a one minute period.
     
  4. Moderator

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #4
    Use DTrace to read the kernel load variable? As discussed here...
     
  5. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Messages:
    5,988
    Location:
    The Land of Hope and Glory
    #5
    Heh, I didn't think DTrace handled things like that. I really need to start trying to understand it better. Thanks for the link.
     

Share This Page