1 /* $NetBSD: sys_pmc.c,v 1.9 2007/12/20 23:03:11 dsl Exp $ */
4 * Copyright (c) 2002 Wasabi Systems, Inc.
7 * Written by Allen Briggs for Wasabi Systems, 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.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: sys_pmc.c,v 1.9 2007/12/20 23:03:11 dsl Exp $");
41 #include "opt_perfctrs.h"
43 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/systm.h>
47 #include <sys/syscallargs.h>
48 #include <sys/types.h>
55 * XXX We need a multiprocessor locking protocol!
59 sys_pmc_control(struct lwp
*l
, const struct sys_pmc_control_args
*uap
, register_t
*retval
)
64 syscallarg(void *) args;
69 struct pmc_counter_cfg cfg
;
71 int ctr
, operation
, error
=0;
73 ctr
= SCARG(uap
, ctr
);
74 operation
= SCARG(uap
, op
);
79 if (!pmc_counter_isconfigured(l
->l_proc
, ctr
)) {
81 } else if (pmc_counter_isrunning(l
->l_proc
, ctr
)) {
84 pmc_enable_counter(l
->l_proc
, ctr
);
88 if (!pmc_counter_isconfigured(l
->l_proc
, ctr
)) {
90 } else if (pmc_counter_isrunning(l
->l_proc
, ctr
)) {
91 pmc_disable_counter(l
->l_proc
, ctr
);
94 case PMC_OP_CONFIGURE
:
95 args
= SCARG(uap
, args
);
97 if (pmc_counter_isrunning(l
->l_proc
, ctr
)) {
98 pmc_disable_counter(l
->l_proc
, ctr
);
100 error
= copyin(args
, &cfg
, sizeof(struct pmc_counter_cfg
));
102 error
= pmc_configure_counter(l
->l_proc
, ctr
, &cfg
);
105 case PMC_OP_PROFSTART
:
106 args
= SCARG(uap
, args
);
108 error
= copyin(args
, &cfg
, sizeof(struct pmc_counter_cfg
));
110 error
= pmc_start_profiling(ctr
, &cfg
);
113 case PMC_OP_PROFSTOP
:
114 error
= pmc_stop_profiling(ctr
);
120 KERNEL_UNLOCK_ONE(NULL
);
126 sys_pmc_get_info(struct lwp
*l
, const struct sys_pmc_get_info_args
*uap
, register_t
*retval
)
131 syscallarg(void *) args;
138 int nctrs
, ctr
, ctrt
, request
, error
=0, flags
=0;
140 ctr
= SCARG(uap
, ctr
);
141 request
= SCARG(uap
, op
);
142 args
= SCARG(uap
, args
);
144 KERNEL_LOCK(1, NULL
);
145 nctrs
= pmc_get_num_counters();
147 case PMC_INFO_NCOUNTERS
: /* args should be (int *) */
148 error
= copyout(&nctrs
, args
, sizeof(int));
151 case PMC_INFO_CPUCTR_TYPE
: /* args should be (int *) */
152 ctrt
= pmc_get_counter_type(ctr
);
153 error
= copyout(&ctrt
, args
, sizeof(int));
155 /* args should be (pmc_ctr_t *) */
156 case PMC_INFO_ACCUMULATED_COUNTER_VALUE
:
157 flags
= PMC_VALUE_FLAGS_CHILDREN
;
159 case PMC_INFO_COUNTER_VALUE
:
160 if (ctr
< 0 || ctr
>= nctrs
) {
164 error
= pmc_get_counter_value(l
->l_proc
, ctr
, flags
, &val
);
166 error
= copyout(&val
, args
, sizeof(uint64_t));
173 KERNEL_UNLOCK_ONE(NULL
);