1 1.1.1.3 christos /* Copyright (C) 2021-2025 Free Software Foundation, Inc. 2 1.1 christos Contributed by Oracle. 3 1.1 christos 4 1.1 christos This file is part of GNU Binutils. 5 1.1 christos 6 1.1 christos This program is free software; you can redistribute it and/or modify 7 1.1 christos it under the terms of the GNU General Public License as published by 8 1.1 christos the Free Software Foundation; either version 3, or (at your option) 9 1.1 christos any later version. 10 1.1 christos 11 1.1 christos This program is distributed in the hope that it will be useful, 12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 christos GNU General Public License for more details. 15 1.1 christos 16 1.1 christos You should have received a copy of the GNU General Public License 17 1.1 christos along with this program; if not, write to the Free Software 18 1.1 christos Foundation, 51 Franklin Street - Fifth Floor, Boston, 19 1.1 christos MA 02110-1301, USA. */ 20 1.1 christos 21 1.1 christos /* Hardware counter profiling */ 22 1.1 christos 23 1.1 christos #ifndef __HWCFUNCS_H 24 1.1 christos #define __HWCFUNCS_H 25 1.1 christos 26 1.1 christos #ifdef LIBCOLLECTOR_SRC /* running in libcollector */ 27 1.1 christos #define hwcfuncs_int_logerr __collector_hwcfuncs_int_logerr 28 1.1 christos #define hwcfuncs_parse_ctr __collector_hwcfuncs_parse_ctr 29 1.1 christos #define hwcfuncs_parse_attrs __collector_hwcfuncs_parse_attrs 30 1.1 christos #define hwcfuncs_bind_descriptor __collector_hwcfuncs_bind_descriptor 31 1.1 christos #define hwcfuncs_bind_hwcentry __collector_hwcfuncs_bind_hwcentry 32 1.1 christos #define hwcfuncs_get_ctrs __collector_hwcfuncs_get_ctrs 33 1.1 christos #define hwcfuncs_errmsg_get __collector_hwcfuncs_errmsg_get 34 1.1 christos #endif /* --- LIBCOLLECTOR_SRC --- */ 35 1.1 christos 36 1.1 christos #include <signal.h> /* siginfo_t */ 37 1.1 christos #include <limits.h> /* UINT64_t */ 38 1.1 christos #include <sys/types.h> 39 1.1 christos #include <stdint.h> 40 1.1 christos 41 1.1 christos #include "hwcentry.h" /* for Hwcentry type */ 42 1.1 christos #include "gp-time.h" 43 1.1 christos 44 1.1 christos typedef unsigned int uint_t; 45 1.1 christos 46 1.1 christos #ifdef __cplusplus 47 1.1 christos extern "C" { 48 1.1 christos #endif 49 1.1 christos 50 1.1 christos /*---------------------------------------------------------------------------*/ 51 1.1 christos /* compile options */ 52 1.1 christos 53 1.1 christos #define HWC_DEBUG 0 /* 0/1 to enable extra HWC debug */ 54 1.1 christos 55 1.1 christos /*---------------------------------------------------------------------------*/ 56 1.1 christos /* typedefs */ 57 1.1 christos /* generic hw event */ 58 1.1 christos typedef struct _hwc_event_t 59 1.1 christos { /* generalized counter event */ 60 1.1 christos hrtime_t ce_hrt; /* gethrtime() */ 61 1.1 christos uint64_t ce_pic[MAX_PICS]; /* counter samples or start values */ 62 1.1 christos } hwc_event_t; 63 1.1 christos 64 1.1 christos /* supplementary data that accompanies some hw events */ 65 1.1 christos typedef struct 66 1.1 christos { /* supplementary data fields */ 67 1.1 christos uint64_t smpl_pc; /* pc related to event */ 68 1.1 christos uint64_t smpl_data_source; /* chip-specific data source encoding */ 69 1.1 christos uint64_t smpl_latency; /* latency related to event */ 70 1.1 christos uint64_t smpl_mem_addr; /* memory address related to event */ 71 1.1 christos } hwc_sample_t; 72 1.1 christos #define HWCFUNCS_INVALID_U64 0xFEEDBEEFDEADBEEFllu /* identifies fields as unused */ 73 1.1 christos 74 1.1 christos typedef struct { /* supplementary data fields */ 75 1.1 christos hwc_sample_t sample[MAX_PICS]; /* counter samples or start values */ 76 1.1 christos } hwc_event_samples_t; 77 1.1 christos 78 1.1 christos #define HWCFUNCS_SAMPLE_RESET(sample) \ 79 1.1 christos do { \ 80 1.1 christos (sample)->smpl_pc =HWCFUNCS_INVALID_U64; \ 81 1.1 christos (sample)->smpl_data_source =HWCFUNCS_INVALID_U64; \ 82 1.1 christos (sample)->smpl_latency =HWCFUNCS_INVALID_U64; \ 83 1.1 christos (sample)->smpl_mem_addr =HWCFUNCS_INVALID_U64; \ 84 1.1 christos } while(0) 85 1.1 christos 86 1.1 christos #define HWCFUNCS_SAMPLE_IS_RESET(sample) \ 87 1.1 christos ( \ 88 1.1 christos (sample)->smpl_pc ==HWCFUNCS_INVALID_U64 && \ 89 1.1 christos (sample)->smpl_data_source==HWCFUNCS_INVALID_U64 && \ 90 1.1 christos (sample)->smpl_latency ==HWCFUNCS_INVALID_U64 && \ 91 1.1 christos (sample)->smpl_mem_addr ==HWCFUNCS_INVALID_U64 \ 92 1.1 christos ) 93 1.1 christos 94 1.1 christos /*---------------------------------------------------------------------------*/ 95 1.1 christos /* macros */ 96 1.1 christos 97 1.1 christos #define HW_INTERVAL_MAX UINT64_MAX 98 1.1 christos #define HW_INTERVAL_PRESET(x) (HW_INTERVAL_MAX - ((uint64_t)(x) - 1)) 99 1.1 christos 100 1.1 christos /* parsing */ 101 1.1 christos #define HWCFUNCS_MAX_ATTRS 20 102 1.1 christos #define HWCFUNCS_PARSE_ATTR '~' 103 1.1 christos #define HWCFUNCS_PARSE_EQUAL '=' 104 1.1 christos #define HWCFUNCS_PARSE_BACKTRACK '+' 105 1.1 christos #define HWCFUNCS_PARSE_BACKTRACK_OFF '-' 106 1.1 christos #define HWCFUNCS_PARSE_REGNUM '/' 107 1.1 christos #define HWCFUNCS_PARSE_VALUE ',' 108 1.1 christos 109 1.1 christos /* error codes */ 110 1.1 christos #define HWCFUNCS_ERROR_GENERIC (-1) 111 1.1 christos #define HWCFUNCS_ERROR_NOT_SUPPORTED (-2) 112 1.1 christos #define HWCFUNCS_ERROR_ALREADY_CALLED (-3) 113 1.1 christos #define HWCFUNCS_ERROR_HWCINIT (-4) 114 1.1 christos #define HWCFUNCS_ERROR_HWCARGS (-5) 115 1.1 christos #define HWCFUNCS_ERROR_MEMORY (-6) 116 1.1 christos #define HWCFUNCS_ERROR_UNAVAIL (-7) 117 1.1 christos #define HWCFUNCS_ERROR_UNEXPECTED (-99) 118 1.1 christos 119 1.1 christos /*---------------------------------------------------------------------------*/ 120 1.1 christos /* prototypes */ 121 1.1 christos 122 1.1 christos typedef void (*hwcfuncs_abort_fn_t) (int errnum, const char *msg); 123 1.1 christos 124 1.1 christos extern void hwcfuncs_int_logerr(const char *format,...); 125 1.1 christos /* Log an error to the internal error buffer. See hwcfuncs_errmsg_get(). 126 1.1 christos Note: Not MT-safe; don't even enable logging in an MT environment. 127 1.1 christos Recommend using this call only during init. 128 1.1 christos Note: when a libcpc call fails, it may automatically call 129 1.1 christos cpcN_capture_errmsg() to log the error message in the same internal buffer. 130 1.1 christos Recommend using this call only for non-cpc failures. 131 1.1 christos */ 132 1.1 christos 133 1.1 christos #define HWCFUNCS_SUPPORT_OVERFLOW_PROFILING 0x01llu 134 1.1 christos #define HWCFUNCS_SUPPORT_PEBS_SAMPLING 0x02llu 135 1.1 christos #define HWCFUNCS_SUPPORT_OVERFLOW_CTR_ID 0x04llu // OS identifies which counter overflowed 136 1.1.1.3 christos #define SUPPORT_MEMORYSPACE_PROFILING 0x08 137 1.1 christos /* get info about session 138 1.1 christos Input: 139 1.1 christos <cpuver>: if not NULL, returns value of CPC cpu version 140 1.1 christos <cciname>: if not NULL, returns name of CPU 141 1.1 christos <npics>: if not NULL, returns maximum # of HWCs 142 1.1 christos <docref>: if not NULL, returns documentation reference 143 1.1 christos <support>: if not NULL, returns bitmask (see above) of hwc support 144 1.1 christos Return: none 145 1.1 christos */ 146 1.1 christos 147 1.1 christos typedef void* (*hwcfuncs_tsd_get_fn_t) (void); 148 1.1 christos typedef void (hwcf_hwc_cb_t) (uint_t cpcregno, const char *name); 149 1.1 christos typedef void (hwcf_attr_cb_t) (const char *attr); 150 1.1 christos 151 1.1 christos extern void 152 1.1 christos hwcfuncs_parse_ctr (const char *counter_def, int *pplus, char **pnameOnly, 153 1.1 christos char **pattrs, char **pregstr, regno_t *pregno); 154 1.1 christos /* Parse a counter definition string (value must already be stripped off). 155 1.1 christos Input: 156 1.1 christos <counter_def>: input whose format is 157 1.1 christos [+|-]<countername>[~attrs...][/<regno>] 158 1.1 christos pointers to return values: Any can be NULL. 159 1.1 christos Return: 160 1.1 christos <plus>: 1 if [+] is found, -1 if [-] is found, 0 otherwise 161 1.1 christos <pnameonly>: strdup(<countername>) 162 1.1 christos <pattrs>: strdup([~attrs...]) if specified, NULL otherwise. 163 1.1 christos <pregstr>: strdup(/<regno>) if specified, NULL otherwise. 164 1.1 christos <pregno>: <regno> if readable, REGNO_ANY if not specd, or -2 otherwise. 165 1.1 christos */ 166 1.1 christos 167 1.1 christos typedef struct 168 1.1 christos { 169 1.1 christos char *ca_name; 170 1.1 christos uint64_t ca_val; 171 1.1 christos } hwcfuncs_attr_t; /* matches cpc_attr_t */ 172 1.1 christos 173 1.1 christos void * hwcfuncs_parse_attrs (const char *countername, 174 1.1 christos hwcfuncs_attr_t attrs[], unsigned max_attrs, 175 1.1 christos uint_t *pnum_attrs, char **errstring); 176 1.1 christos /* Extract the attribute fields from <countername>. 177 1.1 christos Input: 178 1.1 christos <countername>: string whose format is 179 1.1 christos [+]<ctrname>[~attributes...][/<regno>][,...] 180 1.1 christos <attrs>: array of attributes to be returned 181 1.1 christos <max_attrs>: number of elements in <attrs> 182 1.1 christos <pnum_attrs>: if not NULL, will return how many attrs were found. 183 1.1 christos <errstring>: pointer to a buffer for storing error info, or NULL. 184 1.1 christos Return: upon success, a pointer to an allocated copy of <countername>, or 185 1.1 christos NULL if there's a failure. (A copy is made in order to provide storage 186 1.1 christos for the ca_name fields in the <attrs> array.) 187 1.1 christos 188 1.1 christos The pointer should be freed when <attrs> is no longer in use. 189 1.1 christos <attrs> will be filled in data from countername. 190 1.1 christos <pnum_attrs> will have the number of elements in <attrs>. May be 191 1.1 christos non-zero even if return value indicates an error. 192 1.1 christos <errstring> NULL if no error, otherwise, a malloc'd GTXT string. 193 1.1 christos */ 194 1.1 christos 195 1.1 christos extern int hwcfuncs_bind_descriptor (const char *defstring); 196 1.1 christos /* Bind counters to resources. 197 1.1 christos Input: 198 1.1 christos <defstring>: string whose format is 199 1.1 christos :%s:%s:0x%x:%d:%d,0x%x[:%s...repeat for each ctr] 200 1.1 christos where the fields are: 201 1.1 christos :<userName>:<internalCtr>:<register>:<timeoutVal>:<tag>:<memop> 202 1.1 christos Return: 0 if successful 203 1.1 christos HWCFUNCS_ERROR_HWCINIT if resources unavailable 204 1.1 christos HWCFUNCS_ERROR_HWCARGS if counters were not specified correctly 205 1.1 christos */ 206 1.1 christos 207 1.1 christos extern int hwcfuncs_bind_hwcentry (const Hwcentry *entries[], 208 1.1 christos unsigned numctrs); 209 1.1 christos /* Bind counters to resources. 210 1.1 christos Input: 211 1.1 christos <entries>: array of counters 212 1.1 christos <numctrs>: number of items in <entries> 213 1.1 christos Return: 0 if successful 214 1.1 christos HWCFUNCS_ERROR_HWCINIT if resources unavailable 215 1.1 christos HWCFUNCS_ERROR_HWCARGS if counters were not specified correctly 216 1.1 christos */ 217 1.1 christos 218 1.1 christos extern Hwcentry **hwcfuncs_get_ctrs (unsigned *defcnt); 219 1.1 christos /* Get descriptions of the currently bound counters. 220 1.1 christos Input: 221 1.1 christos <defcnt>: if not NULL, returns number of counter definitions. 222 1.1 christos Return: 223 1.1 christos table of counter definition pointers 224 1.1 christos */ 225 1.1 christos 226 1.1 christos extern char *hwcfuncs_errmsg_get (char * buf, size_t bufsize, 227 1.1 christos int enable_capture); 228 1.1 christos /* Gets a recent HWC error message. 229 1.1 christos To clear previous error messages and insure error message is enabled, 230 1.1 christos call hwcfuncs_errmsg_get(NULL,0,1). 231 1.1 christos Once enabled, one error is stored in an internal buffer. A call to this 232 1.1 christos function will clear the buffer and allow a new message to be captured. 233 1.1 christos Note: Not MT-safe - don't enable this feature in an MT environment. 234 1.1 christos Input: 235 1.1 christos <buf>: pointer to buffer or NULL. 236 1.1 christos <bufsize>: size of <buf> 237 1.1 christos <enable_capture>: 0 - disable buffering, 1 - enable buffering. 238 1.1 christos Return: error string or an empty string. 239 1.1 christos */ 240 1.1 christos 241 1.1 christos #ifdef __cplusplus 242 1.1 christos } 243 1.1 christos #endif 244 1.1 christos 245 1.1 christos #endif /* ! __HWCFUNCS_H */ 246