1/*
2 * Copyright (c) 2013 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29
30/*  Sample KPC data into kperf */
31
32#include <mach/mach_types.h>
33#include <kern/thread.h> /* thread_* */
34#include <kern/debug.h> /* panic */
35// #include <sys/proc.h>
36
37#include <chud/chud_xnu.h>
38#include <kperf/kperf.h>
39
40#include <kperf/buffer.h>
41#include <kperf/context.h>
42
43#include <kperf/kperf_kpc.h>
44
45/* If we have kperf enabled, but not KPC */
46#if KPC
47
48void
49kperf_kpc_cpu_sample( struct kpcdata *kpcd, int sample_config )
50{
51	kpcd->running  = kpc_get_running();
52	kpcd->counterc = kpc_get_cpu_counters(0, kpcd->running,
53	                                      &kpcd->curcpu, kpcd->counterv);
54	if( !sample_config )
55		kpcd->configc = 0;
56	else
57	{
58		kpcd->configc = kpc_get_config_count(kpcd->running);
59		kpc_get_config(kpcd->running, kpcd->configv);
60	}
61
62}
63
64void
65kperf_kpc_cpu_log( struct kpcdata *kpcd )
66{
67	unsigned i;
68
69	/* cut a config for instruments -- what's running and
70	 * how many fixed counters there are
71	 */
72	BUF_DATA(PERF_KPC_CONFIG,
73	         kpcd->running,
74	         kpcd->counterc,
75	         kpc_get_counter_count(KPC_CLASS_FIXED_MASK),
76	         kpcd->configc);
77
78#if __LP64__
79	/* config registers, if they were asked for */
80	for (i = 0; i < ((kpcd->configc+3) / 4); i++) {
81		BUF_DATA( PERF_KPC_CFG_REG,
82		          kpcd->configv[0 + i * 4],
83		          kpcd->configv[1 + i * 4],
84		          kpcd->configv[2 + i * 4],
85		          kpcd->configv[3 + i * 4] );
86	}
87
88	/* and the actual data -- 64-bit trace entries */
89	for (i = 0; i < ((kpcd->counterc+3) / 4); i++) {
90		BUF_DATA( PERF_KPC_DATA,
91		          kpcd->counterv[0 + i * 4],
92		          kpcd->counterv[1 + i * 4],
93		          kpcd->counterv[2 + i * 4],
94		          kpcd->counterv[3 + i * 4] );
95	}
96
97#else
98	/* config registers, if requested */
99	for (i = 0; i < ((kpcd->configc+1) / 2); i++) {
100		BUF_DATA( PERF_KPC_CFG_REG32,
101		          (kpcd->configv[0 + i * 2] >> 32ULL),
102		           kpcd->configv[0 + i * 2] & 0xffffffffULL,
103		          (kpcd->configv[1 + i * 2] >> 32ULL),
104		           kpcd->configv[1 + i * 2] & 0xffffffffULL );
105	}
106
107	/* and the actual data -- two counters per tracepoint */
108	for (i = 0; i < ((kpcd->counterc+1) / 2); i++) {
109		BUF_DATA( PERF_KPC_DATA32,
110		          (kpcd->counterv[0 + i * 2] >> 32ULL),
111		           kpcd->counterv[0 + i * 2] & 0xffffffffULL,
112		          (kpcd->counterv[1 + i * 2] >> 32ULL),
113		           kpcd->counterv[1 + i * 2] & 0xffffffffULL );
114	}
115#endif
116}
117
118#endif /* KPC */
119