xref: /illumos-gate/usr/src/lib/libcpc/common/libcpc.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #ifndef	_LIBCPC_H
28*7c478bd9Sstevel@tonic-gate #define	_LIBCPC_H
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
34*7c478bd9Sstevel@tonic-gate #include <inttypes.h>
35*7c478bd9Sstevel@tonic-gate #include <libpctx.h>
36*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
37*7c478bd9Sstevel@tonic-gate #include <signal.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <ucontext.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/processor.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * This library allows hardware performance counters present in
44*7c478bd9Sstevel@tonic-gate  * certain processors to be used by applications to monitor their
45*7c478bd9Sstevel@tonic-gate  * own statistics, the statistics of others, or the statistics of a given CPU.
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
49*7c478bd9Sstevel@tonic-gate extern "C" {
50*7c478bd9Sstevel@tonic-gate #endif
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate typedef struct __cpc cpc_t;
53*7c478bd9Sstevel@tonic-gate typedef struct __cpc_set cpc_set_t;
54*7c478bd9Sstevel@tonic-gate typedef struct __cpc_buf cpc_buf_t;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * Current library version must be passed to cpc_open().
58*7c478bd9Sstevel@tonic-gate  */
59*7c478bd9Sstevel@tonic-gate #define	CPC_VER_CURRENT		2
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * Initializes the library for use and returns a pointer to an identifier that
63*7c478bd9Sstevel@tonic-gate  * must be used as the cpc argument in subsequent libcpc calls.
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate extern cpc_t *cpc_open(int ver);
66*7c478bd9Sstevel@tonic-gate extern int cpc_close(cpc_t *cpc);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * Query information about the underlying processor.
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate extern uint_t cpc_npic(cpc_t *cpc);
72*7c478bd9Sstevel@tonic-gate extern uint_t cpc_caps(cpc_t *cpc);
73*7c478bd9Sstevel@tonic-gate extern const char *cpc_cciname(cpc_t *cpc);
74*7c478bd9Sstevel@tonic-gate extern const char *cpc_cpuref(cpc_t *cpc);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * A vprintf-like error handling routine can be passed to the
78*7c478bd9Sstevel@tonic-gate  * library for use by more sophisticated callers.
79*7c478bd9Sstevel@tonic-gate  * If specified as NULL, errors are written to stderr.
80*7c478bd9Sstevel@tonic-gate  */
81*7c478bd9Sstevel@tonic-gate typedef void (cpc_errhndlr_t)(const char *fn, int subcode, const char *fmt,
82*7c478bd9Sstevel@tonic-gate     va_list ap);
83*7c478bd9Sstevel@tonic-gate extern int cpc_seterrhndlr(cpc_t *cpc, cpc_errhndlr_t *fn);
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate extern cpc_set_t *cpc_set_create(cpc_t *cpc);
86*7c478bd9Sstevel@tonic-gate extern int cpc_set_destroy(cpc_t *cpc, cpc_set_t *set);
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * If successful, returns an index for the new request within the set which is
90*7c478bd9Sstevel@tonic-gate  * needed later to retrieve the request's data.
91*7c478bd9Sstevel@tonic-gate  * Returns -1 if unsuccessful and sets errno to indicate the error.
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate extern int cpc_set_add_request(cpc_t *cpc, cpc_set_t *set, const char *event,
94*7c478bd9Sstevel@tonic-gate     uint64_t preset, uint_t flags, uint_t nattrs, const cpc_attr_t *attrs);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate extern cpc_buf_t *cpc_buf_create(cpc_t *cpc, cpc_set_t *set);
97*7c478bd9Sstevel@tonic-gate extern int cpc_buf_destroy(cpc_t *cpc, cpc_buf_t *buf);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * Binds the set to the current LWP.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate extern int cpc_bind_curlwp(cpc_t *cpc, cpc_set_t *set, uint_t flags);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * Binds the set to the specified LWP in a process controlled via libpctx.
106*7c478bd9Sstevel@tonic-gate  */
107*7c478bd9Sstevel@tonic-gate extern int cpc_bind_pctx(cpc_t *cpc, pctx_t *pctx, id_t id, cpc_set_t *set,
108*7c478bd9Sstevel@tonic-gate 	    uint_t flags);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate /*
111*7c478bd9Sstevel@tonic-gate  * Binds the set to the specified CPU.  The process must have sufficient
112*7c478bd9Sstevel@tonic-gate  * privileges to bind to the CPU via processor_bind(2).  An LWP can only
113*7c478bd9Sstevel@tonic-gate  * bind to one CPU at a time.  To measure more than one CPU simultaneously,
114*7c478bd9Sstevel@tonic-gate  * one LWP must be created for each CPU.
115*7c478bd9Sstevel@tonic-gate  */
116*7c478bd9Sstevel@tonic-gate extern int cpc_bind_cpu(cpc_t *cpc, processorid_t id, cpc_set_t *set,
117*7c478bd9Sstevel@tonic-gate 	uint_t flags);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate /*
120*7c478bd9Sstevel@tonic-gate  * Set the starting value for the indexed counter, and restart counting for a
121*7c478bd9Sstevel@tonic-gate  * set that was frozen by a counter overflow.
122*7c478bd9Sstevel@tonic-gate  */
123*7c478bd9Sstevel@tonic-gate extern int cpc_request_preset(cpc_t *cpc, int index, uint64_t preset);
124*7c478bd9Sstevel@tonic-gate extern int cpc_set_restart(cpc_t *cpc, cpc_set_t *set);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate /*
127*7c478bd9Sstevel@tonic-gate  * Unbinds the set and frees up associated resources. cpc_buf_t's must be
128*7c478bd9Sstevel@tonic-gate  * explicitly freed via cpc_buf_destroy().
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate extern int cpc_unbind(cpc_t *cpc, cpc_set_t *set);
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate /*
133*7c478bd9Sstevel@tonic-gate  * Samples a set into a cpc_buf_t. The provided set must be bound, and the
134*7c478bd9Sstevel@tonic-gate  * buf must have been created with the set being sampled.
135*7c478bd9Sstevel@tonic-gate  */
136*7c478bd9Sstevel@tonic-gate extern int cpc_set_sample(cpc_t *cpc, cpc_set_t *set, cpc_buf_t *buf);
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate extern void cpc_buf_sub(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b);
139*7c478bd9Sstevel@tonic-gate extern void cpc_buf_add(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b);
140*7c478bd9Sstevel@tonic-gate extern void cpc_buf_copy(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *src);
141*7c478bd9Sstevel@tonic-gate extern void cpc_buf_zero(cpc_t *cpc, cpc_buf_t *buf);
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*
144*7c478bd9Sstevel@tonic-gate  * Gets or sets the value of the request specified by index.
145*7c478bd9Sstevel@tonic-gate  */
146*7c478bd9Sstevel@tonic-gate extern int cpc_buf_get(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t *val);
147*7c478bd9Sstevel@tonic-gate extern int cpc_buf_set(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t val);
148*7c478bd9Sstevel@tonic-gate extern hrtime_t cpc_buf_hrtime(cpc_t *cpc, cpc_buf_t *buf);
149*7c478bd9Sstevel@tonic-gate extern uint64_t cpc_buf_tick(cpc_t *cpc, cpc_buf_t *buf);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate extern void cpc_walk_requests(cpc_t *cpc, cpc_set_t *set, void *arg,
152*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, int index, const char *event, uint64_t preset,
153*7c478bd9Sstevel@tonic-gate 	uint_t flags, int nattrs, const cpc_attr_t *attrs));
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate extern void cpc_walk_events_all(cpc_t *cpc, void *arg,
156*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, const char *event));
157*7c478bd9Sstevel@tonic-gate extern void cpc_walk_events_pic(cpc_t *cpc, uint_t picno, void *arg,
158*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, uint_t picno, const char *event));
159*7c478bd9Sstevel@tonic-gate extern void cpc_walk_attrs(cpc_t *cpc, void *arg,
160*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, const char *attr));
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate extern int cpc_enable(cpc_t *cpc);
163*7c478bd9Sstevel@tonic-gate extern int cpc_disable(cpc_t *cpc);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__i386)
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate /*
168*7c478bd9Sstevel@tonic-gate  * Obsolete libcpc interfaces.
169*7c478bd9Sstevel@tonic-gate  */
170*7c478bd9Sstevel@tonic-gate #define	CPC_VER_NONE 0
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate typedef struct _cpc_event cpc_event_t;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate extern uint_t cpc_version(uint_t ver);
175*7c478bd9Sstevel@tonic-gate extern int cpc_access();
176*7c478bd9Sstevel@tonic-gate extern int cpc_getcpuver(void);
177*7c478bd9Sstevel@tonic-gate extern const char *cpc_getcciname(int cpuver);
178*7c478bd9Sstevel@tonic-gate extern const char *cpc_getcpuref(int cpuver);
179*7c478bd9Sstevel@tonic-gate extern uint_t cpc_getnpic(int cpuver);
180*7c478bd9Sstevel@tonic-gate typedef void (cpc_errfn_t)(const char *fn, const char *fmt, va_list ap);
181*7c478bd9Sstevel@tonic-gate extern void cpc_seterrfn(cpc_errfn_t *errfn);
182*7c478bd9Sstevel@tonic-gate extern const char *cpc_getusage(int cpuver);
183*7c478bd9Sstevel@tonic-gate extern void cpc_walk_names(int cpuver, int regno, void *arg,
184*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, int regno, const char *name, uint8_t bits));
185*7c478bd9Sstevel@tonic-gate extern int cpc_strtoevent(int cpuver, const char *spec, cpc_event_t *event);
186*7c478bd9Sstevel@tonic-gate extern char *cpc_eventtostr(cpc_event_t *event);
187*7c478bd9Sstevel@tonic-gate extern void cpc_event_accum(cpc_event_t *accum, cpc_event_t *event);
188*7c478bd9Sstevel@tonic-gate extern void cpc_event_diff(cpc_event_t *diff,
189*7c478bd9Sstevel@tonic-gate     cpc_event_t *left, cpc_event_t *right);
190*7c478bd9Sstevel@tonic-gate extern int cpc_bind_event(cpc_event_t *event, int flags);
191*7c478bd9Sstevel@tonic-gate extern int cpc_take_sample(cpc_event_t *event);
192*7c478bd9Sstevel@tonic-gate extern int cpc_count_usr_events(int enable);
193*7c478bd9Sstevel@tonic-gate extern int cpc_count_sys_events(int enable);
194*7c478bd9Sstevel@tonic-gate extern int cpc_rele(void);
195*7c478bd9Sstevel@tonic-gate extern int cpc_pctx_bind_event(pctx_t *pctx,
196*7c478bd9Sstevel@tonic-gate     id_t lwpid, cpc_event_t *event, int flags);
197*7c478bd9Sstevel@tonic-gate extern int cpc_pctx_take_sample(pctx_t *pctx, id_t lwpid, cpc_event_t *event);
198*7c478bd9Sstevel@tonic-gate extern int cpc_pctx_rele(pctx_t *pctx, id_t lwpid);
199*7c478bd9Sstevel@tonic-gate extern int cpc_pctx_invalidate(pctx_t *pctx, id_t lwpid);
200*7c478bd9Sstevel@tonic-gate extern int cpc_shared_open(void);
201*7c478bd9Sstevel@tonic-gate extern void cpc_shared_close(int fd);
202*7c478bd9Sstevel@tonic-gate extern int cpc_shared_bind_event(int fd, cpc_event_t *event, int flags);
203*7c478bd9Sstevel@tonic-gate extern int cpc_shared_take_sample(int fd, cpc_event_t *event);
204*7c478bd9Sstevel@tonic-gate extern int cpc_shared_rele(int fd);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate #endif /* __sparc || __i386 */
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate #endif
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate #endif	/* _LIBCPC_H */
213