/*  Program to print out current load averages and mach numbers from the Darwin kernel.
 *  
 *  Author: Eric Carlson, carl0240@tc.umn.edu
 *  
 *  This software is supplied 'as is', with no warranty. 
 *  You are free to use and/or redistribute this source, modified or unmodified,
 *  in any form.
 */

#include <mach/mach.h>
#include <mach/mach_error.h>
#include <mach/host_info.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
  host_t host;
  host_load_info_data_t load_info;
  kern_return_t ret;
  int count;

  count = HOST_LOAD_INFO_COUNT;
  
  host = mach_host_self();

  ret = host_statistics(host, HOST_LOAD_INFO, (host_info_t) &load_info, &count);
  if(ret != KERN_SUCCESS) {
    mach_error(argv[0], ret);
    exit(EXIT_FAILURE);
  }

  printf("load averages: %d.%02d %d.%02d %d.%02d\n", 
    load_info.avenrun[0]/LOAD_SCALE,
    (load_info.avenrun[0]%LOAD_SCALE)/10,
    load_info.avenrun[1]/LOAD_SCALE,
    (load_info.avenrun[1]%LOAD_SCALE)/10,
    load_info.avenrun[2]/LOAD_SCALE,
    (load_info.avenrun[2]%LOAD_SCALE)/10);

  printf("mach factors: %d.%02d %d.%02d %d.%02d\n",
    load_info.mach_factor[0]/LOAD_SCALE,
    (load_info.mach_factor[0]%LOAD_SCALE)/10,
    load_info.mach_factor[1]/LOAD_SCALE,
    (load_info.mach_factor[1]%LOAD_SCALE)/10,
    load_info.mach_factor[2]/LOAD_SCALE,
    (load_info.mach_factor[2]%LOAD_SCALE)/10);
  
  exit(EXIT_SUCCESS);
}
