Remove Debian from SECURITY.txt
[binutils-gdb.git] / gprofng / common / hwcentry.h
blob0b923fc35769e1dc780c9a10c992047e4efd9856
1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
2 Contributed by Oracle.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #ifndef _HWCENTRY_H
22 #define _HWCENTRY_H
24 #ifndef LIBCOLLECTOR_SRC /* not running in libcollector */
25 #include <stdio.h> /* FILE */
26 #endif /* --- LIBCOLLECTOR_SRC --- */
27 #include <stdlib.h> /* size_t */
28 #include "hwc_cpus.h"
29 #include "gp-time.h"
31 #ifdef __cplusplus
32 extern "C"
34 #endif
36 /* ABS backtrack types */
37 typedef enum
39 /* !! Lowest 2 bits are used to indicate load and store, respectively !! */
40 /* Example: On SPARC, backtrack.c did this: if (ABS_memop & inst_type) ... */
41 ABST_NONE = 0x0,
42 ABST_LOAD = 0x1,
43 ABST_STORE = 0x2,
44 ABST_LDST = 0x3,
45 ABST_COUNT = 0x4,
46 ABST_US_DTLBM = 0xF,
47 ABST_NOPC = 0x100,
48 ABST_CLKDS = 0x103, // Obsolete
49 ABST_EXACT = 0x203,
50 ABST_LDST_SPARC64 = 0x303,
51 ABST_EXACT_PEBS_PLUS1 = 0x403
52 /* full description below... */
53 } ABST_type;
55 #define ABST_PLUS_BY_DEFAULT(n) ((n)==ABST_EXACT || (n)==ABST_EXACT_PEBS_PLUS1)
56 #define ABST_BACKTRACK_ENABLED(n) ((n)!=ABST_NONE && (n)!=ABST_NOPC)
57 #define ABST_MEMSPACE_ENABLED(n) ((n)!=ABST_NONE && (n)!=ABST_NOPC && (n)!=ABST_COUNT)
59 /* ABS determines the type of backtracking available for a particular metric.
60 * Backtracking is enabled with the "+" in "-h +<countername>...".
62 * When Backtracking is not possible:
64 * ABST_NONE=0: Either the user did not specify "+", or backtracking
65 * is not applicable to the metric, for example:
66 * clk cycles,
67 * instruct counts (dispatch + branch + prefetch),
68 * i$,
69 * FP ops
70 * ABST_NOPC=0x100 Used for non-program-related external events, for example:
71 * system interface events,
72 * memory controller counters
73 * Of all ABST_type options, only ABST_NOPC prevents hwprofile.c
74 * from recording PC/stack information.
76 * When backtracking is allowed:
78 * ABST_LOAD=1: data read events, used with metrics like:
79 * D$, E$, P$ read misses and hits.
80 * [DC+EC+PC]_rd*, Re_*_miss*,
81 * EC_snoop_cb(?)
82 * ABST_STORE=2: data write events, used with metrics like:
83 * D$ writes and write related misses
84 * DC_wr/wr-miss, EC_wb, WC=writecache, Rstall_storeQ
85 * [EC+PC=pcache]_snoop_inv(?), WC_snoop_cb(?),
86 * ABST_LDST=3: data reads/writes, used with metrics like:
87 * E$ references, misses.
88 * ABST_COUNT=4: dedicated assembly instruction: '%hi(0xfc000)'
89 * See SW_count_n metric on sparc.
90 * ABST_US_DTLBM=0xF: for load-store on Sparc -- seems to be used only
91 * for "unskidded DTLB_miss" with DTLB_miss metric.
92 * Checks two adjacent instructions for Data access.
93 * ABST_CLKDS=0x103: data reads/writes, used with Clock-based Dataspace
94 * profiling. Ultrasparc T2 and earlier.
95 * ABST_EXACT=0x203: data reads/writes, precise trap with no skid
96 * ABST_LDST_SPARC64=0x303: Fujitsu SPARC64 load/store
97 * ABST_EXACT_PEBS_PLUS1=0x403: data reads/writes, precise sampling with 1 instr. skid
100 /* Hwcentry - structure for defining a counter.
101 * Some fields have different usage when returned from
102 * hwc_lookup(), hwc_post_lookup(), or hwc_scan_*().
103 * Each function will describe its return values in more detail.
105 typedef struct
107 char *name; /* user HWC specification */
108 char *int_name; /* internal HWC specification */
109 regno_t reg_num; /* register in CPU, aka picnum, or REGNO_ANY */
110 char *metric; /* descriptive name, for well-known counters only */
111 volatile int val; /* default or actual overflow value */
112 int timecvt; /* multiplier to convert metric to time, 0 if N/A */
113 ABST_type memop; /* type of backtracking allowed */
114 char *short_desc; /* optional one-liner description, or NULL */
115 /* the fields above this line are expected, in order, by the tables in hwctable.c */
116 /* ================================================== */
117 /* the fields below this line are more flexible */
118 unsigned int use_perf_event_type : 16; /* Set 1 to use two fields below */
119 unsigned int type : 16; /* Type of perf_event_attr */
120 long long config; /* perf_event_type -specific configuration */
121 long long config1; /* perf_event_type -specific configuration */
122 int sort_order; /* "tag" to associate experiment record with HWC def */
123 hrtime_t min_time; /* target minimum time between overflow events. 0 is off. See HWCTIME_* macros */
124 hrtime_t min_time_default; /* if min_time==HWCTIME_AUTO, use this value instead. 0 is off. */
125 int ref_val; /* if min_time==HWCTIME_AUTO, use this time. 0 is off. */
126 int lval, hval; /* temporary to allow DBX to build until dbx glue.cc fixed */
127 } Hwcentry;
129 // Hwcentry.min_time canned values
130 #define HWCTIME_TBD ((hrtime_t)( -1LL)) /* self-adjusting enabled but nsecs not yet selected */
131 #define HWCTIME_HI ( 1 * 1000 * 1000LL ) /* 1 msec represented in nsecs */
132 #define HWCTIME_ON ( 10 * 1000 * 1000LL ) /* 10 msec represented in nsecs */
133 #define HWCTIME_LO ( 100 * 1000 * 1000LL ) /* 100 msec represented in nsecs */
135 #define HWC_VAL_HI(refVal) (((refVal)/10) + 1)
136 #define HWC_VAL_ON(refVal) (refVal)
137 #define HWC_VAL_LO(refVal) (((refVal)*10)/100*100 + 1) // zero's out lower digits, add 1
138 #define HWC_VAL_CUSTOM(refVal, targetNanoSec) ((double)(refVal)*(targetNanoSec)/HWCTIME_ON)
140 #define HWCENTRY_USES_SAMPLING(h) ((h)->memop==ABST_EXACT_PEBS_PLUS1)
142 extern int hwc_lookup (int forKernel, hrtime_t min_time_default,
143 const char *uname, Hwcentry *list[], unsigned listsz,
144 char **emsg, char **wmsg);
145 /* Parses counter cmdline string. Returns counter definitions.
146 * Input:
147 * <forKernel> lookup using which table: 0-collect or 1-er_kernel
148 * <min_time_default> minimum nseconds between events if Hwcentry.min_time == HWCTIME_TBD. 0 to disable.
149 * <uname> command line HWC definition of format:
150 * <ctr_def>...[{','|(whitespace)}<ctr_n_def>] where
151 * <ctr_def> == [+]<ctr>[/<reg#>][,<interval>]
152 * <list> array of pointers to store counter definitions
153 * <listsz> number of elements in <list>
154 * Returns:
155 * Success:
156 * Returns number of valid counters in <list> and <list>'s elements
157 * will be initialized as follows:
159 * <list[]->name>:
160 * Copy of the <uname> with the following modification:
161 * if backtracking is not supported, the + will be removed.
162 * <list[]->int_name>:
163 * For well-known and convenience ctrs, the internal HWC specification,
164 * e.g. BSQ_cache_reference~emask=0x0100.
165 * For raw ctrs, this will be a copy of <name>.
166 * <list[]->reg_num>:
167 * Register number if specified by user or table, REGNO_ANY otherwise.
168 * <list[]->metric>:
169 * For well-known counters, descriptive name, e.g. "D$ Read Misses".
170 * NULL otherwise.
171 * <list[]->val>:
172 * Overflow value selected by user, default value otherwise.
173 * <list[]->timecvt>:
174 * Value from tables.
175 * <list[]->memop>:
176 * If + is selected and backtracking is allowed, value from table.
177 * ABST_NONE or ABST_NOPC otherwise.
179 * It is the responsibility of the caller to free 'name' and 'int_name'.
180 * 'metric' is a static string and shouldn't be freed.
181 * 'emsg' will point to NULL
183 * Failure:
184 * Frees all allocated elements.
185 * emsg will point to a string with an error message to print
186 * returns -1
189 extern char *hwc_validate_ctrs (int forKernel, Hwcentry *list[], unsigned listsz);
190 /* Validates that the vector of specified HW counters can be loaded (more-or-less)
191 * Some invalid combinations, especially on Linux will not be detected
194 extern int hwc_get_cpc_cpuver ();
195 /* Return the cpc_cpuver for this system. Other possible values:
196 * CPUVER_GENERIC=0, CPU could not be determined, but HWCs are ok.
197 * CPUVER_UNDEFINED=-1, HWCs are not available.
200 extern char *hwc_get_docref (char *buf, size_t buflen);
201 /* Return a CPU HWC document reference, or NULL. */
203 extern char *hwc_get_default_cntrs2 (int forKernel, int style);
204 /* like hwc_get_default_cntrs() for style==1 */
205 /* but allows other styles of formatting as well */
206 /* deprecate and eventually remove hwc_get_default_cntrs() */
208 extern char *hwc_get_orig_default_cntrs (int forKernel);
209 /* Get the default HW counter string as set in the table */
210 /* NULL means none is defined in the table */
212 extern void hwc_update_val (Hwcentry *ctr);
213 /* Check time-based intervals and update Hwcentry.val as needed */
215 extern char *hwc_get_cpuname (char *buf, size_t buflen);
216 /* Return the cpc cpu name for this system, or NULL. */
218 extern unsigned hwc_get_max_regs ();
219 /* Return number of counters registers for this system. */
221 extern unsigned hwc_get_max_concurrent (int forKernel);
222 /* Return the max number of simultaneous counters for this system. */
224 extern char **hwc_get_attrs (int forKernel);
225 /* Return:
226 * Array of attributes (strings) supported by this system.
227 * Last element in array is null.
228 * Array and its elements should NOT be freed by the caller.
231 extern unsigned hwc_scan_attrs (void (*action)(const char *attr,
232 const char *desc));
233 /* Scan the HW counter attributes, and call function for each attribute.
234 * Input:
235 * <action>:
236 * If NULL, no action is performed, but count is still returned.
237 * Otherwise called for each type of attributes, or if none exist,
238 * called once with NULL parameter.
239 * Return: count of times <action> would have been called w/ non-NULL data.
242 extern Hwcentry *hwc_post_lookup (Hwcentry * pret_ctr, char *uname,
243 char * int_name, int cpc_cpuver);
244 /* When post-processing a run, look up a Hwcentry for given type of system.
245 * Input:
246 * <pret_ctr>: storage for counter definition
247 * <uname>: well-known name, convenience name, or complete HWC defintion.
248 * <int_name>: Hwcentry->int_name or NULL for don't care
249 * <cpc_cpuver>: version of cpu used for experiment.
250 * Return:
251 * <pret_ctr>'s elements set as follows:
253 * <pret_ctr->name>:
254 * Copy of <uname> with the following modifications:
255 * 1) + and /<regnum> will be stripped off
256 * 2) attributes will be sorted and values will shown in hex.
257 * <pret_ctr->int_name>:
258 * For well-known/convenience counters, the internal HWC specification
259 * from the table, e.g. BSQ_cache_reference~emask=0x0100.
260 * Otherwise, a copy of <uname>.
261 * <pret_ctr->reg_num>:
262 * Register number if specified by user or table,
263 * REGNO_ANY othewise.
264 * <pret_ctr->metric>:
265 * For well-known counters, descriptive name, e.g. "D$ Read Misses".
266 * NULL otherwise.
267 * <pret_ctr->timecvt>:
268 * For well-known/convenience/hidden counters, value from table.
269 * 0 otherwise.
270 * <pret_ctr->memop>:
271 * For well-known/convenience/hidden counters, value from table.
272 * ABST_NONE otherwise.
273 * <pret_ctr->sort_order>:
274 * Set to 0.
276 * It is the responsibility of the caller to free 'name' and 'int_name'.
277 * 'metric' is a static string and shouldn't be freed.
280 extern Hwcentry **hwc_get_std_ctrs (int forKernel);
281 /* Return:
282 * Array of well-known counters supported by this system.
283 * Last element in array will be NULL.
284 * Array and its elements should NOT be freed by the caller.
287 extern unsigned hwc_scan_std_ctrs (void (*action)(const Hwcentry *));
288 /* Call <action> for each well-known counter.
289 * Input:
290 * <action>:
291 * If NULL, no action is performed, but count is still returned.
292 * Otherwise called for each type of attributes, or if none exist,
293 * called once with NULL parameter.
294 * Return:
295 * Count of times <action> would have been called w/ non-NULL data.
296 * If <action> is not NULL, Hwcentry fields will be set as follows:
297 * <ctr->name>:
298 * HWC alias name, e.g. dcrm.
299 * <ctr->int_name>:
300 * The internal HWC specification, e.g. BSQ_cache_reference~emask=0x0100.
301 * <ctr->reg_num>:
302 * Register number if specified by the table, REGNO_ANY otherwise.
303 * <ctr->metric>:
304 * Descriptive name, e.g. "D$ Read Misses".
305 * <ctr->lval>:
306 * Low-resolution overflow value.
307 * <ctr->val>:
308 * Default overflow value.
309 * <ctr->hval>:
310 * High-resolution overflow value.
311 * <ctr->timecvt>:
312 * multiplier to convert metric to time, 0 otherwise.
313 * <ctr->memop>:
314 * ABST_* type for this counter.
315 * <ctr->reg_list>:
316 * Array of legal <reg_num> values. Terminated by REGNO_ANY.
318 * Note: All fields point to static data, none should be freed.
321 extern Hwcentry **hwc_get_raw_ctrs (int forKernel);
322 /* Return:
323 * Table of raw (not well-known) counters supported by this system.
324 * Last element in array will be NULL.
325 * Table and its elements should NOT be freed by the caller.
328 extern unsigned hwc_scan_raw_ctrs (void (*action)(const Hwcentry *));
329 /* Call <action> for each raw counter.
330 * Input:
331 * <action>:
332 * If NULL, no action is performed, but count is still returned.
333 * Otherwise called for each type of attributes, or if none exist,
334 * called once with NULL parameter.
335 * Return:
336 * Count of times <action> would have been called w/ non-NULL data.
337 * If <action> is not NULL, Hwcentry fields will be set as follows:
338 * <ctr->name>:
339 * HWC raw name without attributes, e.g. BSQ_cache_reference.
340 * <ctr->int_name>:
341 * NULL.
342 * <ctr->metric>:
343 * NULL.
344 * The remainder of the fields are the same as for
345 * hwc_scan_std_ctrs().
347 * Note: All fields point to static data, none should be freed.
350 extern void
351 hwc_usage (int forKernel, const char *cmd, const char *dataspace_msg);
352 /* Print an i18n'd description of "-h" usage, used by collect and er_kernel.
355 extern void hwc_usage_f (int forKernel, FILE *f, const char *cmd,
356 const char *dataspace_msg, int show_syntax,
357 int show_short_desc);
358 /* Print an i18n'd description of "-h" usage to a FILE. Used by GUI. */
360 extern char *hwc_rate_string (const Hwcentry *pctr, int force_numeric_format);
361 /* Returns {"on"|"hi"|"lo"|""|<value>}. Return value must be freed by caller. */
363 extern char *hwc_i18n_metric (const Hwcentry *ctr);
364 /* Get a basic lable for a counter, properly i18n'd.
365 * Note: NOT MT SAFE.
366 * Examples:
367 * CPU Cycles
368 * DC_rd Events
369 * Pseudocode:
370 * if(ctr->metric != NULL) {
371 * sprintf(metricbuf, PTXT(ctr->metric) );
372 * } else if (ctr->name != NULL) {
373 * sprintf(metricbuf, GTXT("%s Events"), ctr->name );
374 * } else if (ctr->int_name != NULL) {
375 * sprintf(metricbuf, GTXT("%s Events"), ctr->int_name );
377 * Return: pointer to a buffer containing the above description.
380 extern char *hwc_hwcentry_string (char *buf, size_t buflen, const Hwcentry *ctr);
381 /* Get a i18n'd description of a HW counter's options.
382 * Examples of well-known counters:
383 * cycles[/{0|1}],9999991 ('CPU Cycles', alias for Cycle_cnt; CPU-cycles)
384 * dcr[/0],1000003 ('D$ Read Refs', alias for DC_rd; load events)
385 * Examples of raw counters:
386 * Cycle_cnt[/{0|1}],1000003 (CPU-cycles)
387 * DC_rd[/0],1000003 (load events)
388 * Return: <buf>, filled in.
391 extern char *hwc_hwcentry_specd_string (char *buf, size_t buflen, const Hwcentry *ctr);
392 /* Get a i18n'd description of a HW counter's specific configuration.
393 * Examples of well-known counters:
394 * cycles,9999991 ('CPU Cycles')
395 * +dcr/0,1000003 ('D$ Read Refs')
396 * Examples of raw counters:
397 * Cycle_cnt,1000003
398 * +DC_rd/0,1000003
399 * Return: <buf>, filled in.
402 extern const char *hwc_memop_string (ABST_type memop);
403 /* Get a i18n'd description of a variable of type ABST_type.
404 * Return: pointer to static string.
407 #ifdef __cplusplus
409 #endif
411 #endif