libcpc.h revision c7a079a873b863c236656bd0db7b2cf390841b4d
1N/A/*
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License (the "License").
1N/A * You may not use this file except in compliance with the License.
1N/A *
1N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * When distributing Covered Code, include this CDDL HEADER in each
1N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A * If applicable, add the following below this CDDL HEADER, with the
1N/A * fields enclosed by brackets "[]" replaced with your own identifying
1N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1N/A *
1N/A * CDDL HEADER END
1N/A */
1N/A/*
1N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A#ifndef _LIBCPC_H
1N/A#define _LIBCPC_H
1N/A
1N/A#include <sys/types.h>
1N/A#include <sys/cpc_impl.h>
1N/A#include <inttypes.h>
1N/A#include <libpctx.h>
1N/A#include <stdarg.h>
1N/A#include <signal.h>
1N/A#include <string.h>
1N/A#include <ucontext.h>
1N/A#include <sys/processor.h>
1N/A
1N/A/*
1N/A * This library allows hardware performance counters present in
1N/A * certain processors to be used by applications to monitor their
1N/A * own statistics, the statistics of others, or the statistics of a given CPU.
1N/A */
1N/A
1N/A#ifdef __cplusplus
1N/Aextern "C" {
1N/A#endif
1N/A
1N/Atypedef struct __cpc cpc_t;
1N/Atypedef struct __cpc_set cpc_set_t;
1N/Atypedef struct __cpc_buf cpc_buf_t;
1N/A
1N/A/*
1N/A * Current library version must be passed to cpc_open().
1N/A */
1N/A#define CPC_VER_CURRENT 2
1N/A
1N/A/*
1N/A * Initializes the library for use and returns a pointer to an identifier that
1N/A * must be used as the cpc argument in subsequent libcpc calls.
1N/A */
1N/Aextern cpc_t *cpc_open(int ver);
1N/Aextern int cpc_close(cpc_t *cpc);
1N/A
1N/A/*
1N/A * Query information about the underlying processor.
1N/A */
1N/Aextern uint_t cpc_npic(cpc_t *cpc);
1N/Aextern uint_t cpc_caps(cpc_t *cpc);
1N/Aextern const char *cpc_cciname(cpc_t *cpc);
1N/Aextern const char *cpc_cpuref(cpc_t *cpc);
1N/A
1N/A/*
1N/A * A vprintf-like error handling routine can be passed to the
1N/A * library for use by more sophisticated callers.
1N/A * If specified as NULL, errors are written to stderr.
1N/A */
1N/Atypedef void (cpc_errhndlr_t)(const char *fn, int subcode, const char *fmt,
1N/A va_list ap);
1N/Aextern int cpc_seterrhndlr(cpc_t *cpc, cpc_errhndlr_t *fn);
1N/A
1N/Aextern cpc_set_t *cpc_set_create(cpc_t *cpc);
1N/Aextern int cpc_set_destroy(cpc_t *cpc, cpc_set_t *set);
1N/A
1N/A/*
1N/A * If successful, returns an index for the new request within the set which is
1N/A * needed later to retrieve the request's data.
1N/A * Returns -1 if unsuccessful and sets errno to indicate the error.
1N/A */
1N/Aextern int cpc_set_add_request(cpc_t *cpc, cpc_set_t *set, const char *event,
1N/A uint64_t preset, uint_t flags, uint_t nattrs, const cpc_attr_t *attrs);
1N/A
1N/Aextern cpc_buf_t *cpc_buf_create(cpc_t *cpc, cpc_set_t *set);
1N/Aextern int cpc_buf_destroy(cpc_t *cpc, cpc_buf_t *buf);
1N/A
1N/A/*
1N/A * Binds the set to the current LWP.
1N/A */
1N/Aextern int cpc_bind_curlwp(cpc_t *cpc, cpc_set_t *set, uint_t flags);
1N/A
1N/A/*
1N/A * Binds the set to the specified LWP in a process controlled via libpctx.
1N/A */
1N/Aextern int cpc_bind_pctx(cpc_t *cpc, pctx_t *pctx, id_t id, cpc_set_t *set,
1N/A uint_t flags);
1N/A
1N/A/*
1N/A * Binds the set to the specified CPU. The process must have sufficient
1N/A * privileges to bind to the CPU via processor_bind(2). An LWP can only
1N/A * bind to one CPU at a time. To measure more than one CPU simultaneously,
1N/A * one LWP must be created for each CPU.
1N/A */
1N/Aextern int cpc_bind_cpu(cpc_t *cpc, processorid_t id, cpc_set_t *set,
1N/A uint_t flags);
1N/A
1N/A/*
1N/A * Set the starting value for the indexed counter, and restart counting for a
1N/A * set that was frozen by a counter overflow.
1N/A */
1N/Aextern int cpc_request_preset(cpc_t *cpc, int index, uint64_t preset);
1N/Aextern int cpc_set_restart(cpc_t *cpc, cpc_set_t *set);
1N/A
1N/A/*
1N/A * Unbinds the set and frees up associated resources. cpc_buf_t's must be
1N/A * explicitly freed via cpc_buf_destroy().
1N/A */
1N/Aextern int cpc_unbind(cpc_t *cpc, cpc_set_t *set);
1N/A
1N/A/*
* Samples a set into a cpc_buf_t. The provided set must be bound, and the
* buf must have been created with the set being sampled.
*/
extern int cpc_set_sample(cpc_t *cpc, cpc_set_t *set, cpc_buf_t *buf);
extern void cpc_buf_sub(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b);
extern void cpc_buf_add(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b);
extern void cpc_buf_copy(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *src);
extern void cpc_buf_zero(cpc_t *cpc, cpc_buf_t *buf);
/*
* Gets or sets the value of the request specified by index.
*/
extern int cpc_buf_get(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t *val);
extern int cpc_buf_set(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t val);
extern hrtime_t cpc_buf_hrtime(cpc_t *cpc, cpc_buf_t *buf);
extern uint64_t cpc_buf_tick(cpc_t *cpc, cpc_buf_t *buf);
extern void cpc_walk_requests(cpc_t *cpc, cpc_set_t *set, void *arg,
void (*action)(void *arg, int index, const char *event, uint64_t preset,
uint_t flags, int nattrs, const cpc_attr_t *attrs));
extern void cpc_walk_events_all(cpc_t *cpc, void *arg,
void (*action)(void *arg, const char *event));
extern void cpc_walk_generic_events_all(cpc_t *cpc, void *arg,
void (*action)(void *arg, const char *event));
extern void cpc_walk_events_pic(cpc_t *cpc, uint_t picno, void *arg,
void (*action)(void *arg, uint_t picno, const char *event));
extern void cpc_walk_generic_events_pic(cpc_t *cpc, uint_t picno, void *arg,
void (*action)(void *arg, uint_t picno, const char *event));
extern void cpc_walk_attrs(cpc_t *cpc, void *arg,
void (*action)(void *arg, const char *attr));
extern int cpc_enable(cpc_t *cpc);
extern int cpc_disable(cpc_t *cpc);
#if defined(__sparc) || defined(__i386)
/*
* Obsolete libcpc interfaces.
*/
#define CPC_VER_NONE 0
typedef struct _cpc_event cpc_event_t;
extern uint_t cpc_version(uint_t ver);
extern int cpc_access();
extern int cpc_getcpuver(void);
extern const char *cpc_getcciname(int cpuver);
extern const char *cpc_getcpuref(int cpuver);
extern uint_t cpc_getnpic(int cpuver);
typedef void (cpc_errfn_t)(const char *fn, const char *fmt, va_list ap);
extern void cpc_seterrfn(cpc_errfn_t *errfn);
extern const char *cpc_getusage(int cpuver);
extern void cpc_walk_names(int cpuver, int regno, void *arg,
void (*action)(void *arg, int regno, const char *name, uint8_t bits));
extern int cpc_strtoevent(int cpuver, const char *spec, cpc_event_t *event);
extern char *cpc_eventtostr(cpc_event_t *event);
extern void cpc_event_accum(cpc_event_t *accum, cpc_event_t *event);
extern void cpc_event_diff(cpc_event_t *diff,
cpc_event_t *left, cpc_event_t *right);
extern int cpc_bind_event(cpc_event_t *event, int flags);
extern int cpc_take_sample(cpc_event_t *event);
extern int cpc_count_usr_events(int enable);
extern int cpc_count_sys_events(int enable);
extern int cpc_rele(void);
extern int cpc_pctx_bind_event(pctx_t *pctx,
id_t lwpid, cpc_event_t *event, int flags);
extern int cpc_pctx_take_sample(pctx_t *pctx, id_t lwpid, cpc_event_t *event);
extern int cpc_pctx_rele(pctx_t *pctx, id_t lwpid);
extern int cpc_pctx_invalidate(pctx_t *pctx, id_t lwpid);
extern int cpc_shared_open(void);
extern void cpc_shared_close(int fd);
extern int cpc_shared_bind_event(int fd, cpc_event_t *event, int flags);
extern int cpc_shared_take_sample(int fd, cpc_event_t *event);
extern int cpc_shared_rele(int fd);
#endif /* __sparc || __i386 */
#ifdef __cplusplus
}
#endif
#endif /* _LIBCPC_H */