10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57640SJonathan.Haslam@Sun.COM * Common Development and Distribution License (the "License"). 67640SJonathan.Haslam@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*11389SAlexander.Kolbasov@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _LIBCPC_H 270Sstevel@tonic-gate #define _LIBCPC_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/cpc_impl.h> 310Sstevel@tonic-gate #include <inttypes.h> 320Sstevel@tonic-gate #include <libpctx.h> 330Sstevel@tonic-gate #include <stdarg.h> 340Sstevel@tonic-gate #include <signal.h> 350Sstevel@tonic-gate #include <string.h> 360Sstevel@tonic-gate #include <ucontext.h> 370Sstevel@tonic-gate #include <sys/processor.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate /* 400Sstevel@tonic-gate * This library allows hardware performance counters present in 410Sstevel@tonic-gate * certain processors to be used by applications to monitor their 420Sstevel@tonic-gate * own statistics, the statistics of others, or the statistics of a given CPU. 430Sstevel@tonic-gate */ 440Sstevel@tonic-gate 450Sstevel@tonic-gate #ifdef __cplusplus 460Sstevel@tonic-gate extern "C" { 470Sstevel@tonic-gate #endif 480Sstevel@tonic-gate 490Sstevel@tonic-gate typedef struct __cpc cpc_t; 500Sstevel@tonic-gate typedef struct __cpc_set cpc_set_t; 510Sstevel@tonic-gate typedef struct __cpc_buf cpc_buf_t; 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * Current library version must be passed to cpc_open(). 550Sstevel@tonic-gate */ 560Sstevel@tonic-gate #define CPC_VER_CURRENT 2 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * Initializes the library for use and returns a pointer to an identifier that 600Sstevel@tonic-gate * must be used as the cpc argument in subsequent libcpc calls. 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate extern cpc_t *cpc_open(int ver); 630Sstevel@tonic-gate extern int cpc_close(cpc_t *cpc); 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* 660Sstevel@tonic-gate * Query information about the underlying processor. 670Sstevel@tonic-gate */ 680Sstevel@tonic-gate extern uint_t cpc_npic(cpc_t *cpc); 690Sstevel@tonic-gate extern uint_t cpc_caps(cpc_t *cpc); 700Sstevel@tonic-gate extern const char *cpc_cciname(cpc_t *cpc); 710Sstevel@tonic-gate extern const char *cpc_cpuref(cpc_t *cpc); 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * A vprintf-like error handling routine can be passed to the 750Sstevel@tonic-gate * library for use by more sophisticated callers. 760Sstevel@tonic-gate * If specified as NULL, errors are written to stderr. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate typedef void (cpc_errhndlr_t)(const char *fn, int subcode, const char *fmt, 790Sstevel@tonic-gate va_list ap); 800Sstevel@tonic-gate extern int cpc_seterrhndlr(cpc_t *cpc, cpc_errhndlr_t *fn); 810Sstevel@tonic-gate 820Sstevel@tonic-gate extern cpc_set_t *cpc_set_create(cpc_t *cpc); 830Sstevel@tonic-gate extern int cpc_set_destroy(cpc_t *cpc, cpc_set_t *set); 840Sstevel@tonic-gate 850Sstevel@tonic-gate /* 860Sstevel@tonic-gate * If successful, returns an index for the new request within the set which is 870Sstevel@tonic-gate * needed later to retrieve the request's data. 880Sstevel@tonic-gate * Returns -1 if unsuccessful and sets errno to indicate the error. 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate extern int cpc_set_add_request(cpc_t *cpc, cpc_set_t *set, const char *event, 910Sstevel@tonic-gate uint64_t preset, uint_t flags, uint_t nattrs, const cpc_attr_t *attrs); 920Sstevel@tonic-gate 930Sstevel@tonic-gate extern cpc_buf_t *cpc_buf_create(cpc_t *cpc, cpc_set_t *set); 940Sstevel@tonic-gate extern int cpc_buf_destroy(cpc_t *cpc, cpc_buf_t *buf); 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * Binds the set to the current LWP. 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate extern int cpc_bind_curlwp(cpc_t *cpc, cpc_set_t *set, uint_t flags); 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * Binds the set to the specified LWP in a process controlled via libpctx. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate extern int cpc_bind_pctx(cpc_t *cpc, pctx_t *pctx, id_t id, cpc_set_t *set, 1050Sstevel@tonic-gate uint_t flags); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate /* 1080Sstevel@tonic-gate * Binds the set to the specified CPU. The process must have sufficient 1090Sstevel@tonic-gate * privileges to bind to the CPU via processor_bind(2). An LWP can only 1100Sstevel@tonic-gate * bind to one CPU at a time. To measure more than one CPU simultaneously, 1110Sstevel@tonic-gate * one LWP must be created for each CPU. 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate extern int cpc_bind_cpu(cpc_t *cpc, processorid_t id, cpc_set_t *set, 1140Sstevel@tonic-gate uint_t flags); 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate /* 1170Sstevel@tonic-gate * Set the starting value for the indexed counter, and restart counting for a 1180Sstevel@tonic-gate * set that was frozen by a counter overflow. 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate extern int cpc_request_preset(cpc_t *cpc, int index, uint64_t preset); 1210Sstevel@tonic-gate extern int cpc_set_restart(cpc_t *cpc, cpc_set_t *set); 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate /* 1240Sstevel@tonic-gate * Unbinds the set and frees up associated resources. cpc_buf_t's must be 1250Sstevel@tonic-gate * explicitly freed via cpc_buf_destroy(). 1260Sstevel@tonic-gate */ 1270Sstevel@tonic-gate extern int cpc_unbind(cpc_t *cpc, cpc_set_t *set); 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* 1300Sstevel@tonic-gate * Samples a set into a cpc_buf_t. The provided set must be bound, and the 1310Sstevel@tonic-gate * buf must have been created with the set being sampled. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate extern int cpc_set_sample(cpc_t *cpc, cpc_set_t *set, cpc_buf_t *buf); 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate extern void cpc_buf_sub(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b); 1360Sstevel@tonic-gate extern void cpc_buf_add(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b); 1370Sstevel@tonic-gate extern void cpc_buf_copy(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *src); 1380Sstevel@tonic-gate extern void cpc_buf_zero(cpc_t *cpc, cpc_buf_t *buf); 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate /* 1410Sstevel@tonic-gate * Gets or sets the value of the request specified by index. 1420Sstevel@tonic-gate */ 1430Sstevel@tonic-gate extern int cpc_buf_get(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t *val); 1440Sstevel@tonic-gate extern int cpc_buf_set(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t val); 1450Sstevel@tonic-gate extern hrtime_t cpc_buf_hrtime(cpc_t *cpc, cpc_buf_t *buf); 1460Sstevel@tonic-gate extern uint64_t cpc_buf_tick(cpc_t *cpc, cpc_buf_t *buf); 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate extern void cpc_walk_requests(cpc_t *cpc, cpc_set_t *set, void *arg, 1490Sstevel@tonic-gate void (*action)(void *arg, int index, const char *event, uint64_t preset, 1500Sstevel@tonic-gate uint_t flags, int nattrs, const cpc_attr_t *attrs)); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate extern void cpc_walk_events_all(cpc_t *cpc, void *arg, 1530Sstevel@tonic-gate void (*action)(void *arg, const char *event)); 1547640SJonathan.Haslam@Sun.COM extern void cpc_walk_generic_events_all(cpc_t *cpc, void *arg, 1557640SJonathan.Haslam@Sun.COM void (*action)(void *arg, const char *event)); 1560Sstevel@tonic-gate extern void cpc_walk_events_pic(cpc_t *cpc, uint_t picno, void *arg, 1570Sstevel@tonic-gate void (*action)(void *arg, uint_t picno, const char *event)); 1587640SJonathan.Haslam@Sun.COM extern void cpc_walk_generic_events_pic(cpc_t *cpc, uint_t picno, void *arg, 1597640SJonathan.Haslam@Sun.COM void (*action)(void *arg, uint_t picno, const char *event)); 1600Sstevel@tonic-gate extern void cpc_walk_attrs(cpc_t *cpc, void *arg, 1610Sstevel@tonic-gate void (*action)(void *arg, const char *attr)); 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate extern int cpc_enable(cpc_t *cpc); 1640Sstevel@tonic-gate extern int cpc_disable(cpc_t *cpc); 1650Sstevel@tonic-gate 166*11389SAlexander.Kolbasov@Sun.COM extern void cpc_terminate(cpc_t *); 167*11389SAlexander.Kolbasov@Sun.COM 1680Sstevel@tonic-gate #if defined(__sparc) || defined(__i386) 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* 1710Sstevel@tonic-gate * Obsolete libcpc interfaces. 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate #define CPC_VER_NONE 0 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate typedef struct _cpc_event cpc_event_t; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate extern uint_t cpc_version(uint_t ver); 1780Sstevel@tonic-gate extern int cpc_access(); 1790Sstevel@tonic-gate extern int cpc_getcpuver(void); 1800Sstevel@tonic-gate extern const char *cpc_getcciname(int cpuver); 1810Sstevel@tonic-gate extern const char *cpc_getcpuref(int cpuver); 1820Sstevel@tonic-gate extern uint_t cpc_getnpic(int cpuver); 1830Sstevel@tonic-gate typedef void (cpc_errfn_t)(const char *fn, const char *fmt, va_list ap); 1840Sstevel@tonic-gate extern void cpc_seterrfn(cpc_errfn_t *errfn); 1850Sstevel@tonic-gate extern const char *cpc_getusage(int cpuver); 1860Sstevel@tonic-gate extern void cpc_walk_names(int cpuver, int regno, void *arg, 1870Sstevel@tonic-gate void (*action)(void *arg, int regno, const char *name, uint8_t bits)); 1880Sstevel@tonic-gate extern int cpc_strtoevent(int cpuver, const char *spec, cpc_event_t *event); 1890Sstevel@tonic-gate extern char *cpc_eventtostr(cpc_event_t *event); 1900Sstevel@tonic-gate extern void cpc_event_accum(cpc_event_t *accum, cpc_event_t *event); 1910Sstevel@tonic-gate extern void cpc_event_diff(cpc_event_t *diff, 1920Sstevel@tonic-gate cpc_event_t *left, cpc_event_t *right); 1930Sstevel@tonic-gate extern int cpc_bind_event(cpc_event_t *event, int flags); 1940Sstevel@tonic-gate extern int cpc_take_sample(cpc_event_t *event); 1950Sstevel@tonic-gate extern int cpc_count_usr_events(int enable); 1960Sstevel@tonic-gate extern int cpc_count_sys_events(int enable); 1970Sstevel@tonic-gate extern int cpc_rele(void); 1980Sstevel@tonic-gate extern int cpc_pctx_bind_event(pctx_t *pctx, 1990Sstevel@tonic-gate id_t lwpid, cpc_event_t *event, int flags); 2000Sstevel@tonic-gate extern int cpc_pctx_take_sample(pctx_t *pctx, id_t lwpid, cpc_event_t *event); 2010Sstevel@tonic-gate extern int cpc_pctx_rele(pctx_t *pctx, id_t lwpid); 2020Sstevel@tonic-gate extern int cpc_pctx_invalidate(pctx_t *pctx, id_t lwpid); 2030Sstevel@tonic-gate extern int cpc_shared_open(void); 2040Sstevel@tonic-gate extern void cpc_shared_close(int fd); 2050Sstevel@tonic-gate extern int cpc_shared_bind_event(int fd, cpc_event_t *event, int flags); 2060Sstevel@tonic-gate extern int cpc_shared_take_sample(int fd, cpc_event_t *event); 2070Sstevel@tonic-gate extern int cpc_shared_rele(int fd); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate #endif /* __sparc || __i386 */ 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate #ifdef __cplusplus 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate #endif 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate #endif /* _LIBCPC_H */ 216