169aad6f1SArnaldo Carvalho de Melo #ifndef __PERF_EVSEL_H 269aad6f1SArnaldo Carvalho de Melo #define __PERF_EVSEL_H 1 369aad6f1SArnaldo Carvalho de Melo 469aad6f1SArnaldo Carvalho de Melo #include <linux/list.h> 5c52b12edSArnaldo Carvalho de Melo #include <stdbool.h> 607ac002fSArnaldo Carvalho de Melo #include <stddef.h> 7d2709c7cSDavid Howells #include <linux/perf_event.h> 869aad6f1SArnaldo Carvalho de Melo #include "types.h" 969aad6f1SArnaldo Carvalho de Melo #include "xyarray.h" 10023695d9SStephane Eranian #include "cgroup.h" 11e248de33SArnaldo Carvalho de Melo #include "hist.h" 1269aad6f1SArnaldo Carvalho de Melo 13c52b12edSArnaldo Carvalho de Melo struct perf_counts_values { 14c52b12edSArnaldo Carvalho de Melo union { 15c52b12edSArnaldo Carvalho de Melo struct { 16c52b12edSArnaldo Carvalho de Melo u64 val; 17c52b12edSArnaldo Carvalho de Melo u64 ena; 18c52b12edSArnaldo Carvalho de Melo u64 run; 19c52b12edSArnaldo Carvalho de Melo }; 20c52b12edSArnaldo Carvalho de Melo u64 values[3]; 21c52b12edSArnaldo Carvalho de Melo }; 22c52b12edSArnaldo Carvalho de Melo }; 23c52b12edSArnaldo Carvalho de Melo 24c52b12edSArnaldo Carvalho de Melo struct perf_counts { 25c52b12edSArnaldo Carvalho de Melo s8 scaled; 26c52b12edSArnaldo Carvalho de Melo struct perf_counts_values aggr; 27c52b12edSArnaldo Carvalho de Melo struct perf_counts_values cpu[]; 28c52b12edSArnaldo Carvalho de Melo }; 29c52b12edSArnaldo Carvalho de Melo 3070db7533SArnaldo Carvalho de Melo struct perf_evsel; 3170db7533SArnaldo Carvalho de Melo 3270db7533SArnaldo Carvalho de Melo /* 3370db7533SArnaldo Carvalho de Melo * Per fd, to map back from PERF_SAMPLE_ID to evsel, only used when there are 3470db7533SArnaldo Carvalho de Melo * more than one entry in the evlist. 3570db7533SArnaldo Carvalho de Melo */ 3670db7533SArnaldo Carvalho de Melo struct perf_sample_id { 3770db7533SArnaldo Carvalho de Melo struct hlist_node node; 3870db7533SArnaldo Carvalho de Melo u64 id; 3970db7533SArnaldo Carvalho de Melo struct perf_evsel *evsel; 4070db7533SArnaldo Carvalho de Melo }; 4170db7533SArnaldo Carvalho de Melo 42f0c55bcfSStephane Eranian /** struct perf_evsel - event selector 43f0c55bcfSStephane Eranian * 44f0c55bcfSStephane Eranian * @name - Can be set to retain the original event name passed by the user, 45f0c55bcfSStephane Eranian * so that when showing results in tools such as 'perf stat', we 46f0c55bcfSStephane Eranian * show the name used, not some alias. 47f0c55bcfSStephane Eranian */ 4869aad6f1SArnaldo Carvalho de Melo struct perf_evsel { 4969aad6f1SArnaldo Carvalho de Melo struct list_head node; 5069aad6f1SArnaldo Carvalho de Melo struct perf_event_attr attr; 5169aad6f1SArnaldo Carvalho de Melo char *filter; 5269aad6f1SArnaldo Carvalho de Melo struct xyarray *fd; 53a91e5431SArnaldo Carvalho de Melo struct xyarray *sample_id; 54a91e5431SArnaldo Carvalho de Melo u64 *id; 55c52b12edSArnaldo Carvalho de Melo struct perf_counts *counts; 5669aad6f1SArnaldo Carvalho de Melo int idx; 57f4d83436SRobert Richter u32 ids; 58e248de33SArnaldo Carvalho de Melo struct hists hists; 59f0c55bcfSStephane Eranian char *name; 60fcf65bf1SArnaldo Carvalho de Melo struct event_format *tp_format; 61a91e5431SArnaldo Carvalho de Melo union { 6269aad6f1SArnaldo Carvalho de Melo void *priv; 63a91e5431SArnaldo Carvalho de Melo off_t id_offset; 64a91e5431SArnaldo Carvalho de Melo }; 65023695d9SStephane Eranian struct cgroup_sel *cgrp; 66ee29be62SArnaldo Carvalho de Melo struct { 67ee29be62SArnaldo Carvalho de Melo void *func; 68ee29be62SArnaldo Carvalho de Melo void *data; 69ee29be62SArnaldo Carvalho de Melo } handler; 707ae92e74SYan, Zheng struct cpu_map *cpus; 71bde09467SArnaldo Carvalho de Melo unsigned int sample_size; 722cee77c4SDavid Ahern bool supported; 730807d2d8SArnaldo Carvalho de Melo bool needs_swap; 74f5b1135bSJiri Olsa /* parse modifier helper */ 75f5b1135bSJiri Olsa int exclude_GH; 766a4bb04cSJiri Olsa struct perf_evsel *leader; 776a4bb04cSJiri Olsa char *group_name; 7869aad6f1SArnaldo Carvalho de Melo }; 7969aad6f1SArnaldo Carvalho de Melo 8086bd5e86SArnaldo Carvalho de Melo struct cpu_map; 8186bd5e86SArnaldo Carvalho de Melo struct thread_map; 8270082dd9SArnaldo Carvalho de Melo struct perf_evlist; 830f82ebc4SArnaldo Carvalho de Melo struct perf_record_opts; 8486bd5e86SArnaldo Carvalho de Melo 8523a2f3abSLin Ming struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx); 86efd2b924SArnaldo Carvalho de Melo struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx); 87201b7334SArnaldo Carvalho de Melo 88201b7334SArnaldo Carvalho de Melo struct event_format *event_format__new(const char *sys, const char *name); 89201b7334SArnaldo Carvalho de Melo 90ef1d1af2SArnaldo Carvalho de Melo void perf_evsel__init(struct perf_evsel *evsel, 91ef1d1af2SArnaldo Carvalho de Melo struct perf_event_attr *attr, int idx); 92ef1d1af2SArnaldo Carvalho de Melo void perf_evsel__exit(struct perf_evsel *evsel); 9369aad6f1SArnaldo Carvalho de Melo void perf_evsel__delete(struct perf_evsel *evsel); 9469aad6f1SArnaldo Carvalho de Melo 950f82ebc4SArnaldo Carvalho de Melo void perf_evsel__config(struct perf_evsel *evsel, 96cac21425SJiri Olsa struct perf_record_opts *opts); 970f82ebc4SArnaldo Carvalho de Melo 980b668bc9SArnaldo Carvalho de Melo bool perf_evsel__is_cache_op_valid(u8 type, u8 op); 990b668bc9SArnaldo Carvalho de Melo 1000b668bc9SArnaldo Carvalho de Melo #define PERF_EVSEL__MAX_ALIASES 8 1010b668bc9SArnaldo Carvalho de Melo 1020b668bc9SArnaldo Carvalho de Melo extern const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] 1030b668bc9SArnaldo Carvalho de Melo [PERF_EVSEL__MAX_ALIASES]; 1040b668bc9SArnaldo Carvalho de Melo extern const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] 1050b668bc9SArnaldo Carvalho de Melo [PERF_EVSEL__MAX_ALIASES]; 1068ad7013bSArnaldo Carvalho de Melo extern const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] 1070b668bc9SArnaldo Carvalho de Melo [PERF_EVSEL__MAX_ALIASES]; 1088ad7013bSArnaldo Carvalho de Melo extern const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX]; 1098ad7013bSArnaldo Carvalho de Melo extern const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX]; 1100b668bc9SArnaldo Carvalho de Melo int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, 1110b668bc9SArnaldo Carvalho de Melo char *bf, size_t size); 1127289f83cSArnaldo Carvalho de Melo const char *perf_evsel__name(struct perf_evsel *evsel); 113c410431cSArnaldo Carvalho de Melo 11469aad6f1SArnaldo Carvalho de Melo int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads); 11570db7533SArnaldo Carvalho de Melo int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads); 116c52b12edSArnaldo Carvalho de Melo int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus); 11769aad6f1SArnaldo Carvalho de Melo void perf_evsel__free_fd(struct perf_evsel *evsel); 11870db7533SArnaldo Carvalho de Melo void perf_evsel__free_id(struct perf_evsel *evsel); 119c52b12edSArnaldo Carvalho de Melo void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads); 12069aad6f1SArnaldo Carvalho de Melo 1217be5ebe8SArnaldo Carvalho de Melo void __perf_evsel__set_sample_bit(struct perf_evsel *evsel, 1227be5ebe8SArnaldo Carvalho de Melo enum perf_event_sample_format bit); 1237be5ebe8SArnaldo Carvalho de Melo void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel, 1247be5ebe8SArnaldo Carvalho de Melo enum perf_event_sample_format bit); 1257be5ebe8SArnaldo Carvalho de Melo 1267be5ebe8SArnaldo Carvalho de Melo #define perf_evsel__set_sample_bit(evsel, bit) \ 1277be5ebe8SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit) 1287be5ebe8SArnaldo Carvalho de Melo 1297be5ebe8SArnaldo Carvalho de Melo #define perf_evsel__reset_sample_bit(evsel, bit) \ 1307be5ebe8SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit) 1317be5ebe8SArnaldo Carvalho de Melo 132*7a5a5ca5SArnaldo Carvalho de Melo void perf_evsel__set_sample_id(struct perf_evsel *evsel); 133*7a5a5ca5SArnaldo Carvalho de Melo 134745cefc5SArnaldo Carvalho de Melo int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads, 135745cefc5SArnaldo Carvalho de Melo const char *filter); 136745cefc5SArnaldo Carvalho de Melo 137f08199d3SArnaldo Carvalho de Melo int perf_evsel__open_per_cpu(struct perf_evsel *evsel, 1386a4bb04cSJiri Olsa struct cpu_map *cpus); 139f08199d3SArnaldo Carvalho de Melo int perf_evsel__open_per_thread(struct perf_evsel *evsel, 1406a4bb04cSJiri Olsa struct thread_map *threads); 141f08199d3SArnaldo Carvalho de Melo int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, 1426a4bb04cSJiri Olsa struct thread_map *threads); 143727ab04eSArnaldo Carvalho de Melo void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads); 14448290609SArnaldo Carvalho de Melo 1455555ded4SArnaldo Carvalho de Melo struct perf_sample; 1465555ded4SArnaldo Carvalho de Melo 1475d2074eaSArnaldo Carvalho de Melo void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample, 1485555ded4SArnaldo Carvalho de Melo const char *name); 1495555ded4SArnaldo Carvalho de Melo u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample, 1505555ded4SArnaldo Carvalho de Melo const char *name); 1515555ded4SArnaldo Carvalho de Melo 1525d2074eaSArnaldo Carvalho de Melo static inline char *perf_evsel__strval(struct perf_evsel *evsel, 1535d2074eaSArnaldo Carvalho de Melo struct perf_sample *sample, 1545d2074eaSArnaldo Carvalho de Melo const char *name) 1555d2074eaSArnaldo Carvalho de Melo { 1565d2074eaSArnaldo Carvalho de Melo return perf_evsel__rawptr(evsel, sample, name); 1575d2074eaSArnaldo Carvalho de Melo } 1585d2074eaSArnaldo Carvalho de Melo 159efd2b924SArnaldo Carvalho de Melo struct format_field; 160efd2b924SArnaldo Carvalho de Melo 161efd2b924SArnaldo Carvalho de Melo struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name); 162efd2b924SArnaldo Carvalho de Melo 163daec78a0SArnaldo Carvalho de Melo #define perf_evsel__match(evsel, t, c) \ 164daec78a0SArnaldo Carvalho de Melo (evsel->attr.type == PERF_TYPE_##t && \ 165daec78a0SArnaldo Carvalho de Melo evsel->attr.config == PERF_COUNT_##c) 166daec78a0SArnaldo Carvalho de Melo 167863e451fSJiri Olsa static inline bool perf_evsel__match2(struct perf_evsel *e1, 168863e451fSJiri Olsa struct perf_evsel *e2) 169863e451fSJiri Olsa { 170863e451fSJiri Olsa return (e1->attr.type == e2->attr.type) && 171863e451fSJiri Olsa (e1->attr.config == e2->attr.config); 172863e451fSJiri Olsa } 173863e451fSJiri Olsa 174c52b12edSArnaldo Carvalho de Melo int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, 175c52b12edSArnaldo Carvalho de Melo int cpu, int thread, bool scale); 176c52b12edSArnaldo Carvalho de Melo 177c52b12edSArnaldo Carvalho de Melo /** 178c52b12edSArnaldo Carvalho de Melo * perf_evsel__read_on_cpu - Read out the results on a CPU and thread 179c52b12edSArnaldo Carvalho de Melo * 180c52b12edSArnaldo Carvalho de Melo * @evsel - event selector to read value 181c52b12edSArnaldo Carvalho de Melo * @cpu - CPU of interest 182c52b12edSArnaldo Carvalho de Melo * @thread - thread of interest 183c52b12edSArnaldo Carvalho de Melo */ 184c52b12edSArnaldo Carvalho de Melo static inline int perf_evsel__read_on_cpu(struct perf_evsel *evsel, 185c52b12edSArnaldo Carvalho de Melo int cpu, int thread) 186c52b12edSArnaldo Carvalho de Melo { 187c52b12edSArnaldo Carvalho de Melo return __perf_evsel__read_on_cpu(evsel, cpu, thread, false); 188c52b12edSArnaldo Carvalho de Melo } 189c52b12edSArnaldo Carvalho de Melo 190c52b12edSArnaldo Carvalho de Melo /** 191c52b12edSArnaldo Carvalho de Melo * perf_evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled 192c52b12edSArnaldo Carvalho de Melo * 193c52b12edSArnaldo Carvalho de Melo * @evsel - event selector to read value 194c52b12edSArnaldo Carvalho de Melo * @cpu - CPU of interest 195c52b12edSArnaldo Carvalho de Melo * @thread - thread of interest 196c52b12edSArnaldo Carvalho de Melo */ 197c52b12edSArnaldo Carvalho de Melo static inline int perf_evsel__read_on_cpu_scaled(struct perf_evsel *evsel, 198c52b12edSArnaldo Carvalho de Melo int cpu, int thread) 199c52b12edSArnaldo Carvalho de Melo { 200c52b12edSArnaldo Carvalho de Melo return __perf_evsel__read_on_cpu(evsel, cpu, thread, true); 201c52b12edSArnaldo Carvalho de Melo } 202c52b12edSArnaldo Carvalho de Melo 203c52b12edSArnaldo Carvalho de Melo int __perf_evsel__read(struct perf_evsel *evsel, int ncpus, int nthreads, 204c52b12edSArnaldo Carvalho de Melo bool scale); 205c52b12edSArnaldo Carvalho de Melo 206c52b12edSArnaldo Carvalho de Melo /** 207c52b12edSArnaldo Carvalho de Melo * perf_evsel__read - Read the aggregate results on all CPUs 208c52b12edSArnaldo Carvalho de Melo * 209c52b12edSArnaldo Carvalho de Melo * @evsel - event selector to read value 210c52b12edSArnaldo Carvalho de Melo * @ncpus - Number of cpus affected, from zero 211c52b12edSArnaldo Carvalho de Melo * @nthreads - Number of threads affected, from zero 212c52b12edSArnaldo Carvalho de Melo */ 213c52b12edSArnaldo Carvalho de Melo static inline int perf_evsel__read(struct perf_evsel *evsel, 214c52b12edSArnaldo Carvalho de Melo int ncpus, int nthreads) 215c52b12edSArnaldo Carvalho de Melo { 216c52b12edSArnaldo Carvalho de Melo return __perf_evsel__read(evsel, ncpus, nthreads, false); 217c52b12edSArnaldo Carvalho de Melo } 218c52b12edSArnaldo Carvalho de Melo 219c52b12edSArnaldo Carvalho de Melo /** 220c52b12edSArnaldo Carvalho de Melo * perf_evsel__read_scaled - Read the aggregate results on all CPUs, scaled 221c52b12edSArnaldo Carvalho de Melo * 222c52b12edSArnaldo Carvalho de Melo * @evsel - event selector to read value 223c52b12edSArnaldo Carvalho de Melo * @ncpus - Number of cpus affected, from zero 224c52b12edSArnaldo Carvalho de Melo * @nthreads - Number of threads affected, from zero 225c52b12edSArnaldo Carvalho de Melo */ 226c52b12edSArnaldo Carvalho de Melo static inline int perf_evsel__read_scaled(struct perf_evsel *evsel, 227c52b12edSArnaldo Carvalho de Melo int ncpus, int nthreads) 228c52b12edSArnaldo Carvalho de Melo { 229c52b12edSArnaldo Carvalho de Melo return __perf_evsel__read(evsel, ncpus, nthreads, true); 230c52b12edSArnaldo Carvalho de Melo } 231c52b12edSArnaldo Carvalho de Melo 2324bf9ce1bSJiri Olsa void hists__init(struct hists *hists); 2334bf9ce1bSJiri Olsa 234a3f698feSArnaldo Carvalho de Melo int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event, 2350807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample); 2360c21f736SArnaldo Carvalho de Melo 2370c21f736SArnaldo Carvalho de Melo static inline struct perf_evsel *perf_evsel__next(struct perf_evsel *evsel) 2380c21f736SArnaldo Carvalho de Melo { 2390c21f736SArnaldo Carvalho de Melo return list_entry(evsel->node.next, struct perf_evsel, node); 2400c21f736SArnaldo Carvalho de Melo } 24107ac002fSArnaldo Carvalho de Melo 242823254edSNamhyung Kim static inline bool perf_evsel__is_group_leader(const struct perf_evsel *evsel) 24307ac002fSArnaldo Carvalho de Melo { 244823254edSNamhyung Kim return evsel->leader == evsel; 24507ac002fSArnaldo Carvalho de Melo } 24669aad6f1SArnaldo Carvalho de Melo #endif /* __PERF_EVSEL_H */ 247