     }
     
     
-    private func hostCPULoadInfo() -> host_cpu_load_info {
+    private static func hostCPULoadInfo() -> host_cpu_load_info {
         var size = HOST_CPU_LOAD_INFO_COUNT
         var hi = host_info_t.alloc(Int(HOST_CPU_LOAD_INFO_COUNT))
         hi.initialize(0)
         
-        let result = host_statistics(host, HOST_CPU_LOAD_INFO, hi, &size)
-        
-        if (result != KERN_SUCCESS) {
-            println("ERROR: \(__FUNCTION__) - \(result)")
+        let result = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, hi,
+                                                                          &size)
+        
+        if (result != KERN_SUCCESS) {
+            #if DEBUG
+                println("ERROR - \(__FILE__):\(__FUNCTION__) - kern_result_t = "
+                        + "\(result)")
+            #endif
             return host_cpu_load_info(cpu_ticks: (0,0,0,0))
         }
         

import Foundation

//------------------------------------------------------------------------------
// MARK: GLOBAL PUBLIC PROPERTIES
//------------------------------------------------------------------------------


// As defined in <mach/machine.h>

public let CPU_TYPE_X86       : cpu_type_t = 7
public let CPU_TYPE_I386      : cpu_type_t = CPU_TYPE_X86
public let CPU_TYPE_X86_64    : cpu_type_t = CPU_TYPE_X86 | CPU_ARCH_ABI64
public let CPU_TYPE_ARM       : cpu_type_t = 12
public let CPU_TYPE_ARM64     : cpu_type_t = CPU_TYPE_ARM | CPU_ARCH_ABI64
public let CPU_TYPE_POWERPC   : cpu_type_t = 18
public let CPU_TYPE_POWERPC64 : cpu_type_t = CPU_TYPE_POWERPC | CPU_ARCH_ABI64


/**
System page size.

- Can check this via pagesize shell command as well
- C lib function getpagesize()
- host_page_size()

- This page size var was added starting 10.9/iOS 7, same as Swift's
aval, thus we can use it
*/
public let PAGE_SIZE = vm_kernel_page_size


//------------------------------------------------------------------------------
// MARK: GLOBAL PUBLIC STRUCTS
//------------------------------------------------------------------------------


struct libtop_i64_t {
    var accumulator : UInt64 = 0
    var last_value  : Int    = 0
}


struct libtop_i64_values_t {
    var i64               = libtop_i64_t()
    var now      : UInt64 = 0
    var began    : UInt64 = 0
    var previous : UInt64 = 0
}


/*
* Process sample information.
*
* Fields prefix meanings:
*
*   b_ : Value for first sample.
*   p_ : Value for previous sample (invalid if p_seq is 0).
*/
struct libtop_psamp_s {
    // TODO: Optionals?
    // TODO: Better init values for some metrics, -1?
    
    // TODO: These should be const right?
    var uid  : uid_t = 0 // User ID
    var pid  : pid_t = 0
    var	ppid : pid_t = 0 // Parent PID
    var pgrp : gid_t = 0 // Proc group ID
    
    /* Memory statistics. */
    var rsize      : UInt64 = 0
    var vsize      : UInt64 = 0
    var rprvt      : UInt64 = 0
    var vprvt      : UInt64 = 0
    var rshrd      : UInt64 = 0
    var fw_private : UInt64 = 0
    var empty      : UInt64 = 0
    
    var reg        : UInt32 = 0
    var p_reg      : UInt32 = 0
    
    // Previous
    var p_rsize    : UInt64 = 0
    var p_vprvt    : UInt64 = 0
    var p_vsize    : UInt64 = 0
    var p_rprvt    : UInt64 = 0
    var p_rshrd    : UInt64 = 0
    var p_empty    : UInt64 = 0
    
    /* Anonymous/purgeable memory statistics. */
    var anonymous   : UInt64 = 0
    var purgeable   : UInt64 = 0
    var p_anonymous : UInt64 = 0
    var p_purgeable : UInt64 = 0
    
    /* Compressed memory statistics. */
    var compressed   : UInt64 = 0
    var p_compressed : UInt64 = 0
    
    /* Number of threads. */
    var th   : UInt32 = 0
    var p_th : UInt32 = 0
    
    var running_th   : UInt32 = 0
    var p_running_th : UInt32 = 0
    
    
    /* Number of ports. */
    var prt   : UInt32 = 0
    var p_prt : UInt32 = 0
    
    /* CPU state/usage statistics. */
    var state : Int = 0 /* Process state. */
    
    /* Total time consumed by process. */
    var total_time   = timeval(tv_sec: 0, tv_usec: 0)
    var b_total_time = timeval(tv_sec: 0, tv_usec: 0)
    var p_total_time = timeval(tv_sec: 0, tv_usec: 0)
    
    /* Event counters. */
    var events   = task_events_info_data_t(faults: 0, pageins: 0, cow_faults: 0, messages_sent: 0, messages_received: 0, syscalls_mach: 0, syscalls_unix: 0, csw: 0)
    var b_events = task_events_info_data_t(faults: 0, pageins: 0, cow_faults: 0, messages_sent: 0, messages_received: 0, syscalls_mach: 0, syscalls_unix: 0, csw: 0)
    var p_events = task_events_info_data_t(faults: 0, pageins: 0, cow_faults: 0, messages_sent: 0, messages_received: 0, syscalls_mach: 0, syscalls_unix: 0, csw: 0)
    
    var faults        = libtop_i64_values_t()
    var pageins       = libtop_i64_values_t()
    var cow_faults    = libtop_i64_values_t()
    var messages_sent = libtop_i64_values_t()
    var messages_recv = libtop_i64_values_t()
    var syscalls_mach = libtop_i64_values_t()
    var syscalls_bsd  = libtop_i64_values_t()
    var csw           = libtop_i64_values_t()
    
    var palloc : UInt64 = 0
    var pfree  : UInt64 = 0
    var salloc : UInt64 = 0
    var sfree  : UInt64 = 0
    
    var p_palloc : UInt64 = 0
    var p_pfree  : UInt64 = 0
    var p_salloc : UInt64 = 0
    var p_sfree  : UInt64 = 0
    
    /* malloc()ed '\0'-terminated string. */
    //char			*command;
    let command = String()
    
    /* Sequence number, used to detect defunct processes. */
    var seq : UInt32 = 0
    
    /*
    * Previous sequence number, used to detect processes that have only
    * existed for the current sample (p_seq == 0).
    */ 
    var p_seq : UInt32 = 0
    
    /* time process was started */
    var started = timeval(tv_sec: 0, tv_usec: 0)
    /* process cpu type */
    var cputype : cpu_type_t = 0
    
    var wq_nthreads        : UInt32 = 0
    var wq_run_threads     : UInt32 = 0
    var wq_blocked_threads : UInt32 = 0
    
    var p_wq_nthreads        : UInt32 = 0
    var p_wq_run_threads     : UInt32 = 0
    var p_wq_blocked_threads : UInt32 = 0
    
    /* Power info. */
    var power   = task_power_info_data_t(total_user: 0, total_system: 0, task_interrupt_wakeups: 0, task_platform_idle_wakeups: 0, task_timer_wakeups_bin_1: 0, task_timer_wakeups_bin_2: 0)
    var b_power = task_power_info_data_t(total_user: 0, total_system: 0, task_interrupt_wakeups: 0, task_platform_idle_wakeups: 0, task_timer_wakeups_bin_1: 0, task_timer_wakeups_bin_2: 0)
    var p_power = task_power_info_data_t(total_user: 0, total_system: 0, task_interrupt_wakeups: 0, task_platform_idle_wakeups: 0, task_timer_wakeups_bin_1: 0, task_timer_wakeups_bin_2: 0)
}


//------------------------------------------------------------------------------
// MARK: GLOBAL PRIVATE PROPERTIES
//------------------------------------------------------------------------------



    }
    
    
    private static func processorLoadInfo() -> processor_set_load_info {
        // NOTE: Duplicate load average and mach factor here
        
        var pset: processor_set_name_t = 0
        var result = processor_set_default(mach_host_self(), &pset)
        
        if (result != KERN_SUCCESS) {
            #if DEBUG
                println("ERROR - \(__FILE__):\(__FUNCTION__) - kern_result_t = "
                        + "\(result)")
            #endif
            return processor_set_load_info(task_count   : 0,
                                           thread_count : 0,
                                           load_average : 0,
                                           mach_factor  : 0)
        }

        
        var count = PROCESSOR_SET_LOAD_INFO_COUNT
        var info_out = processor_set_info_t.alloc(
                                             Int(PROCESSOR_SET_LOAD_INFO_COUNT))
        info_out.initialize(0)
        
        
        result = processor_set_statistics(pset,
                                          PROCESSOR_SET_LOAD_INFO,
                                          info_out,
                                          &count)
        
        if (result != KERN_SUCCESS) {
            #if DEBUG
                println("ERROR - \(__FILE__):\(__FUNCTION__) - kern_result_t = "
                        + "\(result)")
            #endif
            return processor_set_load_info(task_count   : 0,
                                           thread_count : 0,
                                           load_average : 0,
                                           mach_factor  : 0)
        }
        
        
