xref: /illumos-gate/usr/src/lib/libcpc/common/libcpc.h (revision b885580b43755ee4ea1e280b85428893d2ba9291)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c7a079a8SJonathan Haslam  * Common Development and Distribution License (the "License").
6c7a079a8SJonathan Haslam  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b885580bSAlexander Kolbasov  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef	_LIBCPC_H
277c478bd9Sstevel@tonic-gate #define	_LIBCPC_H
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
317c478bd9Sstevel@tonic-gate #include <inttypes.h>
327c478bd9Sstevel@tonic-gate #include <libpctx.h>
337c478bd9Sstevel@tonic-gate #include <stdarg.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <ucontext.h>
377c478bd9Sstevel@tonic-gate #include <sys/processor.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * This library allows hardware performance counters present in
417c478bd9Sstevel@tonic-gate  * certain processors to be used by applications to monitor their
427c478bd9Sstevel@tonic-gate  * own statistics, the statistics of others, or the statistics of a given CPU.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
467c478bd9Sstevel@tonic-gate extern "C" {
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate typedef struct __cpc cpc_t;
507c478bd9Sstevel@tonic-gate typedef struct __cpc_set cpc_set_t;
517c478bd9Sstevel@tonic-gate typedef struct __cpc_buf cpc_buf_t;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Current library version must be passed to cpc_open().
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate #define	CPC_VER_CURRENT		2
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * Initializes the library for use and returns a pointer to an identifier that
607c478bd9Sstevel@tonic-gate  * must be used as the cpc argument in subsequent libcpc calls.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate extern cpc_t *cpc_open(int ver);
637c478bd9Sstevel@tonic-gate extern int cpc_close(cpc_t *cpc);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Query information about the underlying processor.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate extern uint_t cpc_npic(cpc_t *cpc);
697c478bd9Sstevel@tonic-gate extern uint_t cpc_caps(cpc_t *cpc);
707c478bd9Sstevel@tonic-gate extern const char *cpc_cciname(cpc_t *cpc);
717c478bd9Sstevel@tonic-gate extern const char *cpc_cpuref(cpc_t *cpc);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * A vprintf-like error handling routine can be passed to the
757c478bd9Sstevel@tonic-gate  * library for use by more sophisticated callers.
767c478bd9Sstevel@tonic-gate  * If specified as NULL, errors are written to stderr.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate typedef void (cpc_errhndlr_t)(const char *fn, int subcode, const char *fmt,
797c478bd9Sstevel@tonic-gate     va_list ap);
807c478bd9Sstevel@tonic-gate extern int cpc_seterrhndlr(cpc_t *cpc, cpc_errhndlr_t *fn);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate extern cpc_set_t *cpc_set_create(cpc_t *cpc);
837c478bd9Sstevel@tonic-gate extern int cpc_set_destroy(cpc_t *cpc, cpc_set_t *set);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * If successful, returns an index for the new request within the set which is
877c478bd9Sstevel@tonic-gate  * needed later to retrieve the request's data.
887c478bd9Sstevel@tonic-gate  * Returns -1 if unsuccessful and sets errno to indicate the error.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate extern int cpc_set_add_request(cpc_t *cpc, cpc_set_t *set, const char *event,
917c478bd9Sstevel@tonic-gate     uint64_t preset, uint_t flags, uint_t nattrs, const cpc_attr_t *attrs);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate extern cpc_buf_t *cpc_buf_create(cpc_t *cpc, cpc_set_t *set);
947c478bd9Sstevel@tonic-gate extern int cpc_buf_destroy(cpc_t *cpc, cpc_buf_t *buf);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Binds the set to the current LWP.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate extern int cpc_bind_curlwp(cpc_t *cpc, cpc_set_t *set, uint_t flags);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Binds the set to the specified LWP in a process controlled via libpctx.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate extern int cpc_bind_pctx(cpc_t *cpc, pctx_t *pctx, id_t id, cpc_set_t *set,
1057c478bd9Sstevel@tonic-gate 	    uint_t flags);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Binds the set to the specified CPU.  The process must have sufficient
1097c478bd9Sstevel@tonic-gate  * privileges to bind to the CPU via processor_bind(2).  An LWP can only
1107c478bd9Sstevel@tonic-gate  * bind to one CPU at a time.  To measure more than one CPU simultaneously,
1117c478bd9Sstevel@tonic-gate  * one LWP must be created for each CPU.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate extern int cpc_bind_cpu(cpc_t *cpc, processorid_t id, cpc_set_t *set,
1147c478bd9Sstevel@tonic-gate 	uint_t flags);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Set the starting value for the indexed counter, and restart counting for a
1187c478bd9Sstevel@tonic-gate  * set that was frozen by a counter overflow.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate extern int cpc_request_preset(cpc_t *cpc, int index, uint64_t preset);
1217c478bd9Sstevel@tonic-gate extern int cpc_set_restart(cpc_t *cpc, cpc_set_t *set);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Unbinds the set and frees up associated resources. cpc_buf_t's must be
1257c478bd9Sstevel@tonic-gate  * explicitly freed via cpc_buf_destroy().
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate extern int cpc_unbind(cpc_t *cpc, cpc_set_t *set);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Samples a set into a cpc_buf_t. The provided set must be bound, and the
1317c478bd9Sstevel@tonic-gate  * buf must have been created with the set being sampled.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate extern int cpc_set_sample(cpc_t *cpc, cpc_set_t *set, cpc_buf_t *buf);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate extern void cpc_buf_sub(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b);
1367c478bd9Sstevel@tonic-gate extern void cpc_buf_add(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b);
1377c478bd9Sstevel@tonic-gate extern void cpc_buf_copy(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *src);
1387c478bd9Sstevel@tonic-gate extern void cpc_buf_zero(cpc_t *cpc, cpc_buf_t *buf);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * Gets or sets the value of the request specified by index.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate extern int cpc_buf_get(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t *val);
1447c478bd9Sstevel@tonic-gate extern int cpc_buf_set(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t val);
1457c478bd9Sstevel@tonic-gate extern hrtime_t cpc_buf_hrtime(cpc_t *cpc, cpc_buf_t *buf);
1467c478bd9Sstevel@tonic-gate extern uint64_t cpc_buf_tick(cpc_t *cpc, cpc_buf_t *buf);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate extern void cpc_walk_requests(cpc_t *cpc, cpc_set_t *set, void *arg,
1497c478bd9Sstevel@tonic-gate     void (*action)(void *arg, int index, const char *event, uint64_t preset,
1507c478bd9Sstevel@tonic-gate 	uint_t flags, int nattrs, const cpc_attr_t *attrs));
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate extern void cpc_walk_events_all(cpc_t *cpc, void *arg,
1537c478bd9Sstevel@tonic-gate     void (*action)(void *arg, const char *event));
154c7a079a8SJonathan Haslam extern void cpc_walk_generic_events_all(cpc_t *cpc, void *arg,
155c7a079a8SJonathan Haslam     void (*action)(void *arg, const char *event));
1567c478bd9Sstevel@tonic-gate extern void cpc_walk_events_pic(cpc_t *cpc, uint_t picno, void *arg,
1577c478bd9Sstevel@tonic-gate     void (*action)(void *arg, uint_t picno, const char *event));
158c7a079a8SJonathan Haslam extern void cpc_walk_generic_events_pic(cpc_t *cpc, uint_t picno, void *arg,
159c7a079a8SJonathan Haslam     void (*action)(void *arg, uint_t picno, const char *event));
1607c478bd9Sstevel@tonic-gate extern void cpc_walk_attrs(cpc_t *cpc, void *arg,
1617c478bd9Sstevel@tonic-gate     void (*action)(void *arg, const char *attr));
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate extern int cpc_enable(cpc_t *cpc);
1647c478bd9Sstevel@tonic-gate extern int cpc_disable(cpc_t *cpc);
1657c478bd9Sstevel@tonic-gate 
166*b885580bSAlexander Kolbasov extern void cpc_terminate(cpc_t *);
167*b885580bSAlexander Kolbasov 
1687c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__i386)
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * Obsolete libcpc interfaces.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate #define	CPC_VER_NONE 0
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate typedef struct _cpc_event cpc_event_t;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate extern uint_t cpc_version(uint_t ver);
1787c478bd9Sstevel@tonic-gate extern int cpc_access();
1797c478bd9Sstevel@tonic-gate extern int cpc_getcpuver(void);
1807c478bd9Sstevel@tonic-gate extern const char *cpc_getcciname(int cpuver);
1817c478bd9Sstevel@tonic-gate extern const char *cpc_getcpuref(int cpuver);
1827c478bd9Sstevel@tonic-gate extern uint_t cpc_getnpic(int cpuver);
1837c478bd9Sstevel@tonic-gate typedef void (cpc_errfn_t)(const char *fn, const char *fmt, va_list ap);
1847c478bd9Sstevel@tonic-gate extern void cpc_seterrfn(cpc_errfn_t *errfn);
1857c478bd9Sstevel@tonic-gate extern const char *cpc_getusage(int cpuver);
1867c478bd9Sstevel@tonic-gate extern void cpc_walk_names(int cpuver, int regno, void *arg,
1877c478bd9Sstevel@tonic-gate     void (*action)(void *arg, int regno, const char *name, uint8_t bits));
1887c478bd9Sstevel@tonic-gate extern int cpc_strtoevent(int cpuver, const char *spec, cpc_event_t *event);
1897c478bd9Sstevel@tonic-gate extern char *cpc_eventtostr(cpc_event_t *event);
1907c478bd9Sstevel@tonic-gate extern void cpc_event_accum(cpc_event_t *accum, cpc_event_t *event);
1917c478bd9Sstevel@tonic-gate extern void cpc_event_diff(cpc_event_t *diff,
1927c478bd9Sstevel@tonic-gate     cpc_event_t *left, cpc_event_t *right);
1937c478bd9Sstevel@tonic-gate extern int cpc_bind_event(cpc_event_t *event, int flags);
1947c478bd9Sstevel@tonic-gate extern int cpc_take_sample(cpc_event_t *event);
1957c478bd9Sstevel@tonic-gate extern int cpc_count_usr_events(int enable);
1967c478bd9Sstevel@tonic-gate extern int cpc_count_sys_events(int enable);
1977c478bd9Sstevel@tonic-gate extern int cpc_rele(void);
1987c478bd9Sstevel@tonic-gate extern int cpc_pctx_bind_event(pctx_t *pctx,
1997c478bd9Sstevel@tonic-gate     id_t lwpid, cpc_event_t *event, int flags);
2007c478bd9Sstevel@tonic-gate extern int cpc_pctx_take_sample(pctx_t *pctx, id_t lwpid, cpc_event_t *event);
2017c478bd9Sstevel@tonic-gate extern int cpc_pctx_rele(pctx_t *pctx, id_t lwpid);
2027c478bd9Sstevel@tonic-gate extern int cpc_pctx_invalidate(pctx_t *pctx, id_t lwpid);
2037c478bd9Sstevel@tonic-gate extern int cpc_shared_open(void);
2047c478bd9Sstevel@tonic-gate extern void cpc_shared_close(int fd);
2057c478bd9Sstevel@tonic-gate extern int cpc_shared_bind_event(int fd, cpc_event_t *event, int flags);
2067c478bd9Sstevel@tonic-gate extern int cpc_shared_take_sample(int fd, cpc_event_t *event);
2077c478bd9Sstevel@tonic-gate extern int cpc_shared_rele(int fd);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #endif /* __sparc || __i386 */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate #endif
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate #endif	/* _LIBCPC_H */
216