]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/processor.c
xnu-2050.22.13.tar.gz
[apple/xnu.git] / osfmk / kern / processor.c
index 9436505b341d4e763cfa214f7a56735c9ddf3322..23a5496119f7ea63cce73147b87f3c0cd5b8bdc6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
@@ -89,10 +89,11 @@ struct pset_node            pset_node0;
 decl_simple_lock_data(static,pset_node_lock)
 
 queue_head_t                   tasks;
+queue_head_t                   terminated_tasks;       /* To be used ONLY for stackshot. */
 int                                            tasks_count;
 queue_head_t                   threads;
 int                                            threads_count;
-decl_mutex_data(,tasks_threads_lock)
+decl_lck_mtx_data(,tasks_threads_lock)
 
 processor_t                            processor_list;
 unsigned int                   processor_count;
@@ -101,8 +102,9 @@ decl_simple_lock_data(,processor_list_lock)
 
 uint32_t                               processor_avail_count;
 
-processor_t    master_processor;
-int            master_cpu = 0;
+processor_t            master_processor;
+int                    master_cpu = 0;
+boolean_t              sched_stats_active = FALSE;
 
 /* Forwards */
 kern_return_t  processor_set_things(
@@ -119,8 +121,8 @@ processor_bootstrap(void)
 
        simple_lock_init(&pset_node_lock, 0);
 
-       mutex_init(&tasks_threads_lock, 0);
        queue_init(&tasks);
+       queue_init(&terminated_tasks);
        queue_init(&threads);
 
        simple_lock_init(&processor_list_lock, 0);
@@ -132,30 +134,43 @@ processor_bootstrap(void)
 
 /*
  *     Initialize the given processor for the cpu
- *     indicated by cpu_num, and assign to the
+ *     indicated by cpu_id, and assign to the
  *     specified processor set.
  */
 void
 processor_init(
        processor_t                     processor,
-       int                                     cpu_num,
+       int                                     cpu_id,
        processor_set_t         pset)
 {
-       run_queue_init(&processor->runq);
+       if (processor != master_processor) {
+               /* Scheduler state deferred until sched_init() */
+               SCHED(processor_init)(processor);
+       }
 
        processor->state = PROCESSOR_OFF_LINE;
        processor->active_thread = processor->next_thread = processor->idle_thread = THREAD_NULL;
        processor->processor_set = pset;
        processor->current_pri = MINPRI;
-       processor->cpu_num = cpu_num;
+       processor->current_thmode = TH_MODE_NONE;
+       processor->cpu_id = cpu_id;
        timer_call_setup(&processor->quantum_timer, thread_quantum_expire, processor);
        processor->deadline = UINT64_MAX;
        processor->timeslice = 0;
+       processor->processor_meta = PROCESSOR_META_NULL;
        processor->processor_self = IP_NULL;
-       simple_lock_init(&processor->lock, 0);
        processor_data_init(processor);
        processor->processor_list = NULL;
 
+       pset_lock(pset);
+       if (pset->cpu_set_count++ == 0)
+               pset->cpu_set_low = pset->cpu_set_hi = cpu_id;
+       else {
+               pset->cpu_set_low = (cpu_id < pset->cpu_set_low)? cpu_id: pset->cpu_set_low;
+               pset->cpu_set_hi = (cpu_id > pset->cpu_set_hi)? cpu_id: pset->cpu_set_hi;
+       }
+       pset_unlock(pset);
+
        simple_lock(&processor_list_lock);
        if (processor_list == NULL)
                processor_list = processor;
@@ -166,6 +181,24 @@ processor_init(
        simple_unlock(&processor_list_lock);
 }
 
+void
+processor_meta_init(
+       processor_t             processor,
+       processor_t             primary)
+{
+       processor_meta_t        pmeta = primary->processor_meta;
+
+       if (pmeta == PROCESSOR_META_NULL) {
+               pmeta = kalloc(sizeof (*pmeta));
+
+               queue_init(&pmeta->idle_queue);
+
+               pmeta->primary = primary;
+       }
+
+       processor->processor_meta = pmeta;
+}
+
 processor_set_t
 processor_pset(
        processor_t     processor)
@@ -210,10 +243,18 @@ pset_init(
        processor_set_t         pset,
        pset_node_t                     node)
 {
+       if (pset != &pset0) {
+               /* Scheduler state deferred until sched_init() */
+               SCHED(pset_init)(pset);
+       }
+
        queue_init(&pset->active_queue);
        queue_init(&pset->idle_queue);
-       pset->processor_count = 0;
-       pset->low_pri = pset->low_count = PROCESSOR_NULL;
+       pset->online_processor_count = 0;
+       pset_pri_init_hint(pset, PROCESSOR_NULL);
+       pset_count_init_hint(pset, PROCESSOR_NULL);
+       pset->cpu_set_low = pset->cpu_set_hi = 0;
+       pset->cpu_set_count = 0;
        pset_lock_init(pset);
        pset->pset_self = IP_NULL;
        pset->pset_name_self = IP_NULL;
@@ -252,13 +293,13 @@ processor_info(
        processor_info_t                info,
        mach_msg_type_number_t  *count)
 {
-       register int    cpu_num, state;
+       register int    cpu_id, state;
        kern_return_t   result;
 
        if (processor == PROCESSOR_NULL)
                return (KERN_INVALID_ARGUMENT);
 
-       cpu_num = processor->cpu_num;
+       cpu_id = processor->cpu_id;
 
        switch (flavor) {
 
@@ -270,14 +311,14 @@ processor_info(
                        return (KERN_FAILURE);
 
                basic_info = (processor_basic_info_t) info;
-               basic_info->cpu_type = slot_type(cpu_num);
-               basic_info->cpu_subtype = slot_subtype(cpu_num);
+               basic_info->cpu_type = slot_type(cpu_id);
+               basic_info->cpu_subtype = slot_subtype(cpu_id);
                state = processor->state;
                if (state == PROCESSOR_OFF_LINE)
                        basic_info->running = FALSE;
                else
                        basic_info->running = TRUE;
-               basic_info->slot_num = cpu_num;
+               basic_info->slot_num = cpu_id;
                if (processor == master_processor) 
                        basic_info->is_master = TRUE;
                else
@@ -291,18 +332,41 @@ processor_info(
 
        case PROCESSOR_CPU_LOAD_INFO:
        {
-               register processor_cpu_load_info_t      cpu_load_info;
+               processor_cpu_load_info_t       cpu_load_info;
+               timer_data_t    idle_temp;
+               timer_t         idle_state;
 
-           if (*count < PROCESSOR_CPU_LOAD_INFO_COUNT)
+               if (*count < PROCESSOR_CPU_LOAD_INFO_COUNT)
                        return (KERN_FAILURE);
 
-           cpu_load_info = (processor_cpu_load_info_t) info;
-               cpu_load_info->cpu_ticks[CPU_STATE_USER] =
-                                                       timer_grab(&PROCESSOR_DATA(processor, user_state)) / hz_tick_interval;
-               cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] =
-                                                       timer_grab(&PROCESSOR_DATA(processor, system_state)) / hz_tick_interval;
-               cpu_load_info->cpu_ticks[CPU_STATE_IDLE] =
-                                                       timer_grab(&PROCESSOR_DATA(processor, idle_state)) / hz_tick_interval;
+               cpu_load_info = (processor_cpu_load_info_t) info;
+               if (precise_user_kernel_time) {
+                       cpu_load_info->cpu_ticks[CPU_STATE_USER] =
+                                                       (uint32_t)(timer_grab(&PROCESSOR_DATA(processor, user_state)) / hz_tick_interval);
+                       cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] =
+                                                       (uint32_t)(timer_grab(&PROCESSOR_DATA(processor, system_state)) / hz_tick_interval);
+               } else {
+                       uint64_t tval = timer_grab(&PROCESSOR_DATA(processor, user_state)) +
+                               timer_grab(&PROCESSOR_DATA(processor, system_state));
+
+                       cpu_load_info->cpu_ticks[CPU_STATE_USER] = (uint32_t)(tval / hz_tick_interval);
+                       cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] = 0;
+               }
+
+               idle_state = &PROCESSOR_DATA(processor, idle_state);
+               idle_temp = *idle_state;
+
+               if (PROCESSOR_DATA(processor, current_state) != idle_state ||
+                   timer_grab(&idle_temp) != timer_grab(idle_state)) {
+                       cpu_load_info->cpu_ticks[CPU_STATE_IDLE] =
+                                                       (uint32_t)(timer_grab(&PROCESSOR_DATA(processor, idle_state)) / hz_tick_interval);
+               } else {
+                       timer_advance(&idle_temp, mach_absolute_time() - idle_temp.tstamp);
+                               
+                       cpu_load_info->cpu_ticks[CPU_STATE_IDLE] =
+                               (uint32_t)(timer_grab(&idle_temp) / hz_tick_interval);
+               }
+
                cpu_load_info->cpu_ticks[CPU_STATE_NICE] = 0;
 
            *count = PROCESSOR_CPU_LOAD_INFO_COUNT;
@@ -312,7 +376,7 @@ processor_info(
        }
 
        default:
-           result = cpu_info(flavor, cpu_num, info, count);
+           result = cpu_info(flavor, cpu_id, info, count);
            if (result == KERN_SUCCESS)
                        *host = &realhost;                 
 
@@ -338,7 +402,7 @@ processor_start(
                prev = thread_bind(processor);
                thread_block(THREAD_CONTINUE_NULL);
 
-               result = cpu_start(processor->cpu_num);
+               result = cpu_start(processor->cpu_id);
 
                thread_bind(prev);
 
@@ -407,7 +471,7 @@ processor_start(
        if (processor->processor_self == IP_NULL)
                ipc_processor_init(processor);
 
-       result = cpu_start(processor->cpu_num);
+       result = cpu_start(processor->cpu_id);
        if (result != KERN_SUCCESS) {
                s = splsched();
                pset_lock(pset);
@@ -442,7 +506,7 @@ processor_control(
        if (processor == PROCESSOR_NULL)
                return(KERN_INVALID_ARGUMENT);
 
-       return(cpu_control(processor->cpu_num, info, count));
+       return(cpu_control(processor->cpu_id, info, count));
 }
            
 kern_return_t
@@ -468,6 +532,9 @@ processor_get_assignment(
 {
        int state;
 
+       if (processor == PROCESSOR_NULL)
+               return(KERN_INVALID_ARGUMENT);
+
        state = processor->state;
        if (state == PROCESSOR_SHUTDOWN || state == PROCESSOR_OFF_LINE)
                return(KERN_FAILURE);
@@ -710,7 +777,7 @@ processor_set_things(
        addr = NULL;
 
        for (;;) {
-               mutex_lock(&tasks_threads_lock);
+               lck_mtx_lock(&tasks_threads_lock);
 
                if (type == THING_TASK)
                        maxthings = tasks_count;
@@ -724,7 +791,7 @@ processor_set_things(
                        break;
 
                /* unlock and allocate more memory */
-               mutex_unlock(&tasks_threads_lock);
+               lck_mtx_unlock(&tasks_threads_lock);
 
                if (size != 0)
                        kfree(addr, size);
@@ -776,7 +843,7 @@ processor_set_things(
 
        }
                
-       mutex_unlock(&tasks_threads_lock);
+       lck_mtx_unlock(&tasks_threads_lock);
 
        if (actual < maxthings)
                size_needed = actual * sizeof (mach_port_t);