Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libpmc / pmc.c
blob5e62859f0d0eb88bd2189baa2a2d053c358a45f7
1 /* $NetBSD: pmc.c,v 1.2 2003/03/09 00:42:56 lukem Exp $ */
3 /*
4 * Copyright (c 2002 Wasabi Systems, Inc.
5 * All rights reserved.
7 * Written by Jason R. Thorpe 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
11 * are met:
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
23 * written permission.
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 __RCSID("$NetBSD: pmc.c,v 1.2 2003/03/09 00:42:56 lukem Exp $");
41 #include <sys/param.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <pmc.h>
46 #include "pmc_private.h"
48 static const struct pmc_class2evid *
49 _pmc_type_lookup(int classval)
51 const struct pmc_class2evid *class;
53 for (class = _pmc_md_classes; class->name != NULL; class++) {
54 if (class->class == classval)
55 return (class);
57 return (NULL);
60 static const struct pmc_event *
61 _pmc_evid_lookup(const char *evname)
63 const struct pmc_class2evid *class;
64 const struct pmc_event *evid;
65 int classval;
67 if (pmc_get_info(-1, PMC_INFO_CPUCTR_TYPE, &classval) == -1) {
68 /* Sets errno for us. */
69 return (NULL);
72 if ((class = _pmc_type_lookup(classval)) == NULL) {
73 errno = ESRCH;
74 return (NULL);
77 for (evid = class->evids; evid->name != NULL; evid++) {
78 if (evname[0] == evid->name[0] &&
79 strcmp(evname, evid->name) == 0)
80 break;
82 if (evid->name == NULL) {
83 errno = ESRCH;
84 return (NULL);
87 return (evid);
90 int
91 pmc_configure_counter(int ctr, const char *evname, pmc_ctr_t reset_val,
92 uint32_t flags)
94 struct pmc_counter_cfg pcfg;
95 const struct pmc_event *evid;
97 evid = _pmc_evid_lookup(evname);
98 if (evid == NULL) {
99 /* _pmc_evid_lookup() set errno */
100 return (-1);
103 pcfg.event_id = evid->val;
104 pcfg.reset_value = reset_val;
105 pcfg.flags = flags;
107 return (pmc_control(ctr, PMC_OP_CONFIGURE, &pcfg));
111 pmc_start_counter(int ctr)
113 return (pmc_control(ctr, PMC_OP_START, NULL));
117 pmc_stop_counter(int ctr)
119 return (pmc_control(ctr, PMC_OP_STOP, NULL));
123 pmc_get_num_counters(void)
125 int rv;
127 if (pmc_get_info(-1, PMC_INFO_NCOUNTERS, &rv) == -1)
128 return (0);
130 return (rv);
134 pmc_get_counter_class(void)
136 int rv;
138 if (pmc_get_info(-1, PMC_INFO_CPUCTR_TYPE, &rv) == -1)
139 return (0);
141 return (rv);
145 pmc_get_counter_type(int ctr, int *typep)
147 /* If they want class, they should call pmc_get_counter_class(). */
148 if (ctr < 0) {
149 errno = ENODEV;
150 return (-1);
153 return (pmc_get_info(ctr, PMC_INFO_CPUCTR_TYPE, typep));
157 pmc_get_counter_value(int ctr, uint64_t *valp)
159 return (pmc_get_info(ctr, PMC_INFO_COUNTER_VALUE, valp));
163 pmc_get_accumulated_counter_value(int ctr, uint64_t *valp)
165 return (pmc_get_info(ctr, PMC_INFO_ACCUMULATED_COUNTER_VALUE, valp));
168 const char *
169 pmc_get_counter_class_name(int classval)
171 const struct pmc_class2evid *class;
173 class = _pmc_type_lookup(classval);
174 if (class == NULL) {
175 errno = ESRCH;
176 return (NULL);
179 return (class->name);
182 const char *
183 pmc_get_counter_type_name(int type)
185 const struct pmc_class2evid *class;
187 class = _pmc_type_lookup(type);
188 if (class == NULL) {
189 errno = ESRCH;
190 return (NULL);
193 return (class->name);
196 const char *
197 pmc_get_counter_event_name(pmc_evid_t event)
199 const struct pmc_class2evid *class;
200 const struct pmc_event *evid;
201 int classval;
203 if (pmc_get_info(-1, PMC_INFO_CPUCTR_TYPE, &classval) == -1) {
204 /* Sets errno for us. */
205 return (NULL);
208 if ((class = _pmc_type_lookup(classval)) == NULL) {
209 errno = ESRCH;
210 return (NULL);
213 for (evid = class->evids; evid->name != NULL; evid++) {
214 if (evid->val == event)
215 break;
217 if (evid->name == NULL) {
218 errno = ESRCH;
219 return (NULL);
222 return (evid->name);
225 const struct pmc_event *
226 pmc_get_counter_event_list(void)
228 const struct pmc_class2evid *class;
229 int classval;
231 if (pmc_get_info(-1, PMC_INFO_CPUCTR_TYPE, &classval) == -1) {
232 /* Sets errno for us. */
233 return (NULL);
236 if ((class = _pmc_type_lookup(classval)) == NULL) {
237 errno = ESRCH;
238 return (NULL);
241 return (class->evids);