]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/kpc_thread.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / osfmk / kern / kpc_thread.c
index 692aa02fe1bb21eebda81983abeff8304856e672..248ea3c56b29ab35e13c1ecd550825cd6e0ab5e6 100644 (file)
 #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)
 {
@@ -107,11 +119,12 @@ kpc_set_thread_counting(uint32_t classes)
                /* and schedule an AST for this thread... */
                if( !current_thread()->kpc_buf )
                {
-                       current_thread()->t_chud |= T_KPC_ALLOC;
+                       current_thread()->kperf_flags |= T_KPC_ALLOC;
                        act_set_kperf(current_thread());
-               }       
+               }
        }
 
+    kpc_off_cpu_update();
        lck_mtx_unlock(&kpc_thread_lock);
 
        return 0;
@@ -125,12 +138,7 @@ 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, 
@@ -141,13 +149,12 @@ kpc_update_thread_counters( thread_t thread )
                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;
+               current_thread()->kperf_flags |= T_KPC_ALLOC;
                act_set_kperf(current_thread());
-       }       
+       }
 
        /* 3. switch the PMC block pointers */
        tmp = cpu->cpu_kpc_buf[1];
@@ -155,12 +162,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)
@@ -191,6 +192,19 @@ 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)
@@ -223,6 +237,6 @@ void
 kpc_thread_ast_handler( thread_t thread )
 {
        /* see if we want an alloc */
-       if( thread->t_chud & T_KPC_ALLOC )
+       if( thread->kperf_flags & T_KPC_ALLOC )
                thread->kpc_buf = kpc_counterbuf_alloc();
 }