2 * Copyright (c) 2003-2007, Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * PMC interface used by the base kernel.
37 #ifndef _SYS_PMCKERN_H_
38 #define _SYS_PMCKERN_H_
40 #include <sys/param.h>
41 #include <sys/kernel.h>
46 #define PMC_FN_PROCESS_EXEC 1
47 #define PMC_FN_CSW_IN 2
48 #define PMC_FN_CSW_OUT 3
49 #define PMC_FN_DO_SAMPLES 4
50 #define PMC_FN_KLD_LOAD 5
51 #define PMC_FN_KLD_UNLOAD 6
53 #define PMC_FN_MUNMAP 8
54 #define PMC_FN_USER_CALLCHAIN 9
56 struct pmckern_procexec {
57 int pm_credentialschanged;
58 uintfptr_t pm_entryaddr;
61 struct pmckern_map_in {
62 void *pm_file; /* filename or vnode pointer */
63 uintfptr_t pm_address; /* address object is loaded at */
66 struct pmckern_map_out {
67 uintfptr_t pm_address; /* start address of region */
68 size_t pm_size; /* size of unmapped region */
72 extern int (*pmc_hook)(struct thread *_td, int _function, void *_arg);
73 extern int (*pmc_intr)(int _cpu, struct trapframe *_frame);
75 /* SX lock protecting the hook */
76 extern struct sx pmc_sx;
78 /* Per-cpu flags indicating availability of sampling data */
79 extern volatile cpumask_t pmc_cpumask;
81 /* Count of system-wide sampling PMCs in existence */
82 extern volatile int pmc_ss_count;
84 /* kernel version number */
85 extern const int pmc_kernel_version;
87 /* Hook invocation; for use within the kernel */
88 #define PMC_CALL_HOOK(t, cmd, arg) \
91 if (pmc_hook != NULL) \
92 (pmc_hook)((t), (cmd), (arg)); \
93 sx_sunlock(&pmc_sx); \
96 /* Hook invocation that needs an exclusive lock */
97 #define PMC_CALL_HOOK_X(t, cmd, arg) \
100 if (pmc_hook != NULL) \
101 (pmc_hook)((t), (cmd), (arg)); \
102 sx_xunlock(&pmc_sx); \
106 * Some hook invocations (e.g., from context switch and clock handling
107 * code) need to be lock-free.
109 #define PMC_CALL_HOOK_UNLOCKED(t, cmd, arg) \
111 if (pmc_hook != NULL) \
112 (pmc_hook)((t), (cmd), (arg)); \
115 #define PMC_SWITCH_CONTEXT(t,cmd) PMC_CALL_HOOK_UNLOCKED(t,cmd,NULL)
117 /* Check if a process is using HWPMCs.*/
118 #define PMC_PROC_IS_USING_PMCS(p) \
119 (__predict_false(atomic_load_acq_int(&(p)->p_flag) & \
122 #define PMC_SYSTEM_SAMPLING_ACTIVE() (pmc_ss_count > 0)
124 /* Check if a CPU has recorded samples. */
125 #define PMC_CPU_HAS_SAMPLES(C) (__predict_false(pmc_cpumask & (1 << (C))))
130 int pmc_cpu_is_disabled(int _cpu); /* deprecated */
131 int pmc_cpu_is_active(int _cpu);
132 int pmc_cpu_is_present(int _cpu);
133 int pmc_cpu_is_primary(int _cpu);
134 unsigned int pmc_cpu_max(void);
137 int pmc_cpu_max_active(void);
140 #endif /* _SYS_PMCKERN_H_ */