]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/kpc_thread.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / osfmk / kern / kpc_thread.c
index 692aa02fe1bb21eebda81983abeff8304856e672..a2f1fe6a0d759369d124312ec04387da636c1ab0 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2012 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
- * 
+ *
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -22,7 +22,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
 #include <kern/locks.h>
 #include <sys/errno.h>
 
+#include <kperf/kperf.h>
+#include <kperf/buffer.h>
+#include <kperf/context.h>
+#include <kperf/sample.h>
+#include <kperf/action.h>
+#include <kperf/kperf_kpc.h>
 #include <kern/kpc.h>
 
+#if defined (__arm64__)
+#include <arm/cpu_data_internal.h>
+#elif defined (__arm__)
+#include <arm/cpu_data_internal.h>
+#endif
 
 /* global for whether to read PMCs on context switch */
-int kpc_threads_counting;
+int kpc_threads_counting = 0;
+
+/* whether to call into KPC when a thread goes off CPU */
+boolean_t kpc_off_cpu_active = FALSE;
 
 /* current config and number of counters in that config */
 static uint32_t kpc_thread_classes = 0;
@@ -47,8 +61,6 @@ static lck_grp_attr_t *kpc_thread_lckgrp_attr = NULL;
 static lck_grp_t      *kpc_thread_lckgrp = NULL;
 static lck_mtx_t       kpc_thread_lock;
 
-void kpc_thread_init(void);
-
 void
 kpc_thread_init(void)
 {
@@ -71,10 +83,11 @@ kpc_get_thread_counting(void)
 
        lck_mtx_unlock(&kpc_thread_lock);
 
-       if( kpc_threads_counting_tmp )
+       if (kpc_threads_counting_tmp) {
                return kpc_thread_classes_tmp;
-       else
+       } else {
                return 0;
+       }
 }
 
 int
@@ -86,14 +99,11 @@ kpc_set_thread_counting(uint32_t classes)
 
        count = kpc_get_counter_count(classes);
 
-       if( (classes == 0)
-           || (count == 0) )
-       {
+       if ((classes == 0)
+           || (count == 0)) {
                /* shut down */
                kpc_threads_counting = FALSE;
-       }
-       else
-       {
+       } else {
                /* stash the config */
                kpc_thread_classes = classes;
 
@@ -105,13 +115,13 @@ kpc_set_thread_counting(uint32_t classes)
                kpc_threads_counting = TRUE;
 
                /* and schedule an AST for this thread... */
-               if( !current_thread()->kpc_buf )
-               {
-                       current_thread()->t_chud |= T_KPC_ALLOC;
+               if (!current_thread()->kpc_buf) {
+                       current_thread()->kperf_ast |= T_KPC_ALLOC;
                        act_set_kperf(current_thread());
-               }       
+               }
        }
 
+       kpc_off_cpu_update();
        lck_mtx_unlock(&kpc_thread_lock);
 
        return 0;
@@ -125,29 +135,24 @@ kpc_update_thread_counters( thread_t thread )
        uint64_t *tmp = NULL;
        cpu_data_t *cpu = NULL;
 
-/* TODO: Fix this...*/
-#if defined (__x86_64__)
        cpu = current_cpu_datap();
-#else
-#error architecture not yet supported
-#endif
 
        /* 1. stash current PMCs into latest CPU block */
-       kpc_get_cpu_counters( FALSE, kpc_thread_classes, 
-                             NULL, cpu->cpu_kpc_buf[1] );
+       kpc_get_cpu_counters( FALSE, kpc_thread_classes,
+           NULL, cpu->cpu_kpc_buf[1] );
 
        /* 2. apply delta to old thread */
-       if( thread->kpc_buf )
-               for( i = 0; i < kpc_thread_classes_count; i++ )
+       if (thread->kpc_buf) {
+               for (i = 0; i < kpc_thread_classes_count; i++) {
                        thread->kpc_buf[i] += cpu->cpu_kpc_buf[1][i] - cpu->cpu_kpc_buf[0][i];
+               }
+       }
 
-       
        /* schedule any necessary allocations */
-       if( !current_thread()->kpc_buf )
-       {
-               current_thread()->t_chud |= T_KPC_ALLOC;
+       if (!current_thread()->kpc_buf) {
+               current_thread()->kperf_ast |= T_KPC_ALLOC;
                act_set_kperf(current_thread());
-       }       
+       }
 
        /* 3. switch the PMC block pointers */
        tmp = cpu->cpu_kpc_buf[1];
@@ -155,12 +160,6 @@ kpc_update_thread_counters( thread_t thread )
        cpu->cpu_kpc_buf[0] = tmp;
 }
 
-void
-kpc_switch_context( thread_t old, thread_t new __unused )
-{
-       kpc_update_thread_counters( old );
-}
-
 /* get counter values for a thread */
 int
 kpc_get_curthread_counters(uint32_t *inoutcount, uint64_t *buf)
@@ -169,21 +168,23 @@ kpc_get_curthread_counters(uint32_t *inoutcount, uint64_t *buf)
        boolean_t enabled;
 
        /* buffer too small :( */
-       if( *inoutcount < kpc_thread_classes_count )
+       if (*inoutcount < kpc_thread_classes_count) {
                return EINVAL;
+       }
 
        /* copy data and actual size */
-       if( !thread->kpc_buf )
+       if (!thread->kpc_buf) {
                return EINVAL;
+       }
 
        enabled = ml_set_interrupts_enabled(FALSE);
 
        /* snap latest version of counters for this thread */
-       kpc_update_thread_counters( current_thread() );
-       
+       kpc_update_thread_counters( current_thread());
+
        /* copy out */
-       memcpy( buf, thread->kpc_buf, 
-               kpc_thread_classes_count * sizeof(*buf) );
+       memcpy( buf, thread->kpc_buf,
+           kpc_thread_classes_count * sizeof(*buf));
        *inoutcount = kpc_thread_classes_count;
 
        ml_set_interrupts_enabled(enabled);
@@ -191,13 +192,27 @@ kpc_get_curthread_counters(uint32_t *inoutcount, uint64_t *buf)
        return 0;
 }
 
+void
+kpc_off_cpu_update(void)
+{
+       kpc_off_cpu_active = kpc_threads_counting;
+}
+
+void
+kpc_off_cpu_internal(thread_t thread)
+{
+       if (kpc_threads_counting) {
+               kpc_update_thread_counters(thread);
+       }
+}
 
 void
 kpc_thread_create(thread_t thread)
 {
        /* nothing to do if we're not counting */
-       if(!kpc_threads_counting)
+       if (!kpc_threads_counting) {
                return;
+       }
 
        /* give the new thread a counterbuf */
        thread->kpc_buf = kpc_counterbuf_alloc();
@@ -209,8 +224,9 @@ kpc_thread_destroy(thread_t thread)
        uint64_t *buf = NULL;
 
        /* usual case: no kpc buf, just return */
-       if( !thread->kpc_buf )
+       if (!thread->kpc_buf) {
                return;
+       }
 
        /* otherwise, don't leak */
        buf = thread->kpc_buf;
@@ -218,11 +234,10 @@ kpc_thread_destroy(thread_t thread)
        kpc_counterbuf_free(buf);
 }
 
-/* ast callback on a thread */
 void
-kpc_thread_ast_handler( thread_t thread )
+kpc_thread_ast_handler(thread_t thread)
 {
-       /* see if we want an alloc */
-       if( thread->t_chud & T_KPC_ALLOC )
+       if (thread->kperf_ast & T_KPC_ALLOC) {
                thread->kpc_buf = kpc_counterbuf_alloc();
+       }
 }