2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <kern/debug.h>
30 #include <sys/param.h>
33 #include <sys/sysctl.h>
34 #include <libkern/libkern.h>
35 #include <kern/assert.h>
39 #include <pexpert/pexpert.h>
41 /* Various sysctl requests */
42 #define REQ_CLASSES (1)
43 #define REQ_COUNTING (2)
44 #define REQ_THREAD_COUNTING (3)
45 #define REQ_CONFIG_COUNT (4)
46 #define REQ_COUNTER_COUNT (5)
47 #define REQ_THREAD_COUNTERS (6)
48 #define REQ_COUNTERS (7)
49 #define REQ_SHADOW_COUNTERS (8)
50 #define REQ_CONFIG (9)
51 #define REQ_PERIOD (10)
52 #define REQ_ACTIONID (11)
53 #define REQ_FORCE_ALL_CTRS (12)
54 #define REQ_DISABLE_WHITELIST (13)
56 /* Type-munging casts */
57 typedef int (*getint_t
)(void);
58 typedef int (*setint_t
)(int);
61 static int kpc_initted
= 0;
63 /* locking and buffer for large data requests */
64 static lck_grp_attr_t
*sysctl_buffer_lckgrp_attr
= NULL
;
65 static lck_grp_t
*sysctl_buffer_lckgrp
= NULL
;
66 static lck_mtx_t sysctl_buffer_lock
;
67 static void *sysctl_buffer
= NULL
;
69 typedef int (*setget_func_t
)(int);
72 extern void kpc_thread_init(void); /* osfmk/kern/kpc_thread.c */
73 extern void kpc_arch_init(void);
78 sysctl_buffer_lckgrp_attr
= lck_grp_attr_alloc_init();
79 sysctl_buffer_lckgrp
= lck_grp_alloc_init("kpc",
80 sysctl_buffer_lckgrp_attr
);
81 lck_mtx_init(&sysctl_buffer_lock
, sysctl_buffer_lckgrp
, LCK_ATTR_NULL
);
89 /* abstract sysctl handlers */
91 sysctl_get_int( struct sysctl_oid
*oidp
, struct sysctl_req
*req
,
96 /* copy out the old value */
97 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
103 sysctl_getset_int( struct sysctl_oid
*oidp
, struct sysctl_req
*req
,
104 int (*get_func
)(void), int (*set_func
)(int) )
109 /* get the old value and process it */
112 /* copy out the old value, get the new value */
113 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
114 if (error
|| !req
->newptr
)
117 /* if that worked, and we're writing... */
118 error
= set_func( value
);
124 sysctl_setget_int( struct sysctl_req
*req
,
125 int (*setget_func
)(int) )
130 error
= SYSCTL_IN( req
, &value
, sizeof(value
) );
134 value
= setget_func(value
);
136 error
= SYSCTL_OUT( req
, &value
, sizeof(value
) );
142 kpc_sysctl_acquire_buffer(void)
144 if( sysctl_buffer
== NULL
)
145 sysctl_buffer
= kpc_counterbuf_alloc();
156 sysctl_kpc_get_counters(uint32_t counters
,
157 uint32_t *size
, void *buf
)
159 uint64_t *ctr_buf
= (uint64_t*)buf
;
163 count
= kpc_get_cpu_counters(counters
& KPC_ALL_CPUS
,
165 &curcpu
, &ctr_buf
[1]);
171 *size
= (count
+1) * sizeof(uint64_t);
177 sysctl_kpc_get_shadow_counters(uint32_t counters
,
178 uint32_t *size
, void *buf
)
180 uint64_t *ctr_buf
= (uint64_t*)buf
;
184 count
= kpc_get_shadow_counters(counters
& KPC_ALL_CPUS
,
186 &curcpu
, &ctr_buf
[1]);
193 *size
= (count
+1) * sizeof(uint64_t);
199 sysctl_kpc_get_thread_counters(uint32_t tid
,
200 uint32_t *size
, void *buf
)
202 uint32_t count
= *size
/ sizeof(uint64_t);
208 r
= kpc_get_curthread_counters(&count
, buf
);
210 *size
= count
* sizeof(uint64_t);
216 sysctl_kpc_get_config(uint32_t classes
, void* buf
)
218 return kpc_get_config( classes
, buf
);
222 sysctl_kpc_set_config(uint32_t classes
, void* buf
)
224 return kpc_set_config( classes
, buf
);
228 sysctl_kpc_get_period(uint32_t classes
, void* buf
)
230 return kpc_get_period( classes
, buf
);
234 sysctl_kpc_set_period(uint32_t classes
, void* buf
)
236 return kpc_set_period( classes
, buf
);
240 sysctl_kpc_get_actionid(uint32_t classes
, void* buf
)
242 return kpc_get_actionid( classes
, buf
);
246 sysctl_kpc_set_actionid(uint32_t classes
, void* buf
)
248 return kpc_set_actionid( classes
, buf
);
253 sysctl_get_bigarray( struct sysctl_req
*req
,
254 int (*get_fn
)(uint32_t, uint32_t*, void*) )
257 uint32_t bufsize
= KPC_MAX_COUNTERS
* sizeof(uint64_t); /* XXX? */
260 /* get the argument */
261 error
= SYSCTL_IN( req
, &arg
, sizeof(arg
) );
264 printf( "kpc: no arg?\n" );
268 /* get the wired buffer */
269 error
= kpc_sysctl_acquire_buffer();
273 /* atomically get the array into the wired buffer. We have a double
274 * copy, but this is better than page faulting / interrupting during
277 error
= get_fn( arg
, &bufsize
, sysctl_buffer
);
279 /* do the copy out */
281 error
= SYSCTL_OUT( req
, sysctl_buffer
, bufsize
);
286 /* given a config word, how many bytes does it take? */
288 sysctl_config_size( uint32_t config
)
290 return kpc_get_config_count(config
) * sizeof(kpc_config_t
);
294 sysctl_counter_size( uint32_t classes
)
296 return kpc_get_counter_count(classes
) * sizeof(uint64_t);
300 sysctl_actionid_size( uint32_t classes
)
302 return kpc_get_counter_count(classes
) * sizeof(int32_t);
306 sysctl_getset_bigarray( struct sysctl_req
*req
,
307 int (*size_fn
)(uint32_t arg
),
308 int (*get_fn
)(uint32_t, void*),
309 int (*set_fn
)(uint32_t, void*) )
312 uint32_t bufsize
= KPC_MAX_COUNTERS
* sizeof(uint64_t); /* XXX? */
313 uint32_t regsize
= 0;
316 /* get the config word */
317 error
= SYSCTL_IN( req
, &arg
, sizeof(arg
) );
320 printf( "kpc: no arg?\n" );
324 /* Work out size of registers */
325 regsize
= size_fn((uint32_t)arg
);
327 /* Ignore NULL requests */
331 /* ensure not too big */
332 if( regsize
> bufsize
)
335 /* get the wired buffer */
336 error
= kpc_sysctl_acquire_buffer();
343 // copy in the rest in -- sysctl remembers we did one already
344 error
= SYSCTL_IN( req
, sysctl_buffer
,
347 // if SYSCTL_IN fails it means we are only doing a read
350 error
= set_fn( (uint32_t)arg
, sysctl_buffer
);
360 error
= get_fn( (uint32_t)arg
, sysctl_buffer
);
364 // copy out the full set
365 error
= SYSCTL_OUT( req
, sysctl_buffer
, regsize
);
375 * #define SYSCTL_HANDLER_ARGS (struct sysctl_oid *oidp, \
376 * void *arg1, int arg2, \
377 * struct sysctl_req *req )
380 kpc_sysctl SYSCTL_HANDLER_ARGS
384 // __unused struct sysctl_oid *unused_oidp = oidp;
388 panic("kpc_init not called");
390 lck_mtx_lock(&sysctl_buffer_lock
);
393 switch( (uintptr_t) arg1
)
396 ret
= sysctl_get_int( oidp
, req
,
400 ret
= sysctl_getset_int( oidp
, req
,
401 (getint_t
)kpc_get_running
,
402 (setint_t
)kpc_set_running
);
404 case REQ_THREAD_COUNTING
:
405 ret
= sysctl_getset_int( oidp
, req
,
406 (getint_t
)kpc_get_thread_counting
,
407 (setint_t
)kpc_set_thread_counting
);
410 case REQ_CONFIG_COUNT
:
411 ret
= sysctl_setget_int( req
,
412 (setget_func_t
)kpc_get_config_count
);
415 case REQ_COUNTER_COUNT
:
416 ret
= sysctl_setget_int( req
,
417 (setget_func_t
)kpc_get_counter_count
);
421 case REQ_THREAD_COUNTERS
:
422 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_thread_counters
);
426 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_counters
);
429 case REQ_SHADOW_COUNTERS
:
430 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_shadow_counters
);
434 ret
= sysctl_getset_bigarray( req
,
436 sysctl_kpc_get_config
,
437 sysctl_kpc_set_config
);
441 ret
= sysctl_getset_bigarray( req
,
443 sysctl_kpc_get_period
,
444 sysctl_kpc_set_period
);
448 ret
= sysctl_getset_bigarray( req
,
449 sysctl_actionid_size
,
450 sysctl_kpc_get_actionid
,
451 sysctl_kpc_set_actionid
);
459 lck_mtx_unlock(&sysctl_buffer_lock
);
465 /*** sysctl definitions ***/
467 /* root kperf node */
468 SYSCTL_NODE(, OID_AUTO
, kpc
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
472 SYSCTL_PROC(_kpc
, OID_AUTO
, classes
,
473 CTLTYPE_INT
|CTLFLAG_RD
|CTLFLAG_ANYBODY
,
475 sizeof(int), kpc_sysctl
, "I", "Available classes");
477 SYSCTL_PROC(_kpc
, OID_AUTO
, counting
,
478 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
480 sizeof(int), kpc_sysctl
, "I", "PMCs counting");
482 SYSCTL_PROC(_kpc
, OID_AUTO
, thread_counting
,
483 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
484 (void*)REQ_THREAD_COUNTING
,
485 sizeof(int), kpc_sysctl
, "I", "Thread accumulation");
488 SYSCTL_PROC(_kpc
, OID_AUTO
, config_count
,
489 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
490 (void*)REQ_CONFIG_COUNT
,
491 sizeof(int), kpc_sysctl
, "S", "Config count");
493 SYSCTL_PROC(_kpc
, OID_AUTO
, counter_count
,
494 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
495 (void*)REQ_COUNTER_COUNT
,
496 sizeof(int), kpc_sysctl
, "S", "Counter count");
499 SYSCTL_PROC(_kpc
, OID_AUTO
, thread_counters
,
500 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
501 (void*)REQ_THREAD_COUNTERS
,
502 sizeof(uint64_t), kpc_sysctl
,
503 "QU", "Current thread counters");
505 SYSCTL_PROC(_kpc
, OID_AUTO
, counters
,
506 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
508 sizeof(uint64_t), kpc_sysctl
,
509 "QU", "Current counters");
511 SYSCTL_PROC(_kpc
, OID_AUTO
, shadow_counters
,
512 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
513 (void*)REQ_SHADOW_COUNTERS
,
514 sizeof(uint64_t), kpc_sysctl
,
515 "QU", "Current shadow counters");
517 SYSCTL_PROC(_kpc
, OID_AUTO
, config
,
518 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
520 sizeof(uint64_t), kpc_sysctl
,
521 "QU", "Set counter configs");
523 SYSCTL_PROC(_kpc
, OID_AUTO
, period
,
524 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
526 sizeof(uint64_t), kpc_sysctl
,
527 "QU", "Set counter periods");
529 SYSCTL_PROC(_kpc
, OID_AUTO
, actionid
,
530 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
532 sizeof(uint32_t), kpc_sysctl
,
533 "QU", "Set counter actionids");