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" 12759ff497SNamhyung Kim #include "symbol.h" 1369aad6f1SArnaldo Carvalho de Melo 14c52b12edSArnaldo Carvalho de Melo struct perf_counts_values { 15c52b12edSArnaldo Carvalho de Melo union { 16c52b12edSArnaldo Carvalho de Melo struct { 17c52b12edSArnaldo Carvalho de Melo u64 val; 18c52b12edSArnaldo Carvalho de Melo u64 ena; 19c52b12edSArnaldo Carvalho de Melo u64 run; 20c52b12edSArnaldo Carvalho de Melo }; 21c52b12edSArnaldo Carvalho de Melo u64 values[3]; 22c52b12edSArnaldo Carvalho de Melo }; 23c52b12edSArnaldo Carvalho de Melo }; 24c52b12edSArnaldo Carvalho de Melo 25c52b12edSArnaldo Carvalho de Melo struct perf_counts { 26c52b12edSArnaldo Carvalho de Melo s8 scaled; 27c52b12edSArnaldo Carvalho de Melo struct perf_counts_values aggr; 28c52b12edSArnaldo Carvalho de Melo struct perf_counts_values cpu[]; 29c52b12edSArnaldo Carvalho de Melo }; 30c52b12edSArnaldo Carvalho de Melo 3170db7533SArnaldo Carvalho de Melo struct perf_evsel; 3270db7533SArnaldo Carvalho de Melo 3370db7533SArnaldo Carvalho de Melo /* 3470db7533SArnaldo Carvalho de Melo * Per fd, to map back from PERF_SAMPLE_ID to evsel, only used when there are 3570db7533SArnaldo Carvalho de Melo * more than one entry in the evlist. 3670db7533SArnaldo Carvalho de Melo */ 3770db7533SArnaldo Carvalho de Melo struct perf_sample_id { 3870db7533SArnaldo Carvalho de Melo struct hlist_node node; 3970db7533SArnaldo Carvalho de Melo u64 id; 4070db7533SArnaldo Carvalho de Melo struct perf_evsel *evsel; 41*e4caec0dSJiri Olsa 42*e4caec0dSJiri Olsa /* Holds total ID period value for PERF_SAMPLE_READ processing. */ 43*e4caec0dSJiri Olsa u64 period; 4470db7533SArnaldo Carvalho de Melo }; 4570db7533SArnaldo Carvalho de Melo 46f0c55bcfSStephane Eranian /** struct perf_evsel - event selector 47f0c55bcfSStephane Eranian * 48f0c55bcfSStephane Eranian * @name - Can be set to retain the original event name passed by the user, 49f0c55bcfSStephane Eranian * so that when showing results in tools such as 'perf stat', we 50f0c55bcfSStephane Eranian * show the name used, not some alias. 51f0c55bcfSStephane Eranian */ 5269aad6f1SArnaldo Carvalho de Melo struct perf_evsel { 5369aad6f1SArnaldo Carvalho de Melo struct list_head node; 5469aad6f1SArnaldo Carvalho de Melo struct perf_event_attr attr; 5569aad6f1SArnaldo Carvalho de Melo char *filter; 5669aad6f1SArnaldo Carvalho de Melo struct xyarray *fd; 57a91e5431SArnaldo Carvalho de Melo struct xyarray *sample_id; 58a91e5431SArnaldo Carvalho de Melo u64 *id; 59c52b12edSArnaldo Carvalho de Melo struct perf_counts *counts; 60c7a79c47SStephane Eranian struct perf_counts *prev_raw_counts; 6169aad6f1SArnaldo Carvalho de Melo int idx; 62f4d83436SRobert Richter u32 ids; 63e248de33SArnaldo Carvalho de Melo struct hists hists; 64f0c55bcfSStephane Eranian char *name; 65fcf65bf1SArnaldo Carvalho de Melo struct event_format *tp_format; 66a91e5431SArnaldo Carvalho de Melo union { 6769aad6f1SArnaldo Carvalho de Melo void *priv; 68a91e5431SArnaldo Carvalho de Melo off_t id_offset; 69a91e5431SArnaldo Carvalho de Melo }; 70023695d9SStephane Eranian struct cgroup_sel *cgrp; 71ee29be62SArnaldo Carvalho de Melo struct { 72ee29be62SArnaldo Carvalho de Melo void *func; 73ee29be62SArnaldo Carvalho de Melo void *data; 74ee29be62SArnaldo Carvalho de Melo } handler; 757ae92e74SYan, Zheng struct cpu_map *cpus; 76bde09467SArnaldo Carvalho de Melo unsigned int sample_size; 772cee77c4SDavid Ahern bool supported; 780807d2d8SArnaldo Carvalho de Melo bool needs_swap; 79f5b1135bSJiri Olsa /* parse modifier helper */ 80f5b1135bSJiri Olsa int exclude_GH; 8197f63e4aSNamhyung Kim int nr_members; 826a4bb04cSJiri Olsa struct perf_evsel *leader; 836a4bb04cSJiri Olsa char *group_name; 8469aad6f1SArnaldo Carvalho de Melo }; 8569aad6f1SArnaldo Carvalho de Melo 8629d720edSNamhyung Kim #define hists_to_evsel(h) container_of(h, struct perf_evsel, hists) 8729d720edSNamhyung Kim 8886bd5e86SArnaldo Carvalho de Melo struct cpu_map; 8986bd5e86SArnaldo Carvalho de Melo struct thread_map; 9070082dd9SArnaldo Carvalho de Melo struct perf_evlist; 910f82ebc4SArnaldo Carvalho de Melo struct perf_record_opts; 9286bd5e86SArnaldo Carvalho de Melo 9323a2f3abSLin Ming struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx); 94efd2b924SArnaldo Carvalho de Melo struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx); 95201b7334SArnaldo Carvalho de Melo 96201b7334SArnaldo Carvalho de Melo struct event_format *event_format__new(const char *sys, const char *name); 97201b7334SArnaldo Carvalho de Melo 98ef1d1af2SArnaldo Carvalho de Melo void perf_evsel__init(struct perf_evsel *evsel, 99ef1d1af2SArnaldo Carvalho de Melo struct perf_event_attr *attr, int idx); 100ef1d1af2SArnaldo Carvalho de Melo void perf_evsel__exit(struct perf_evsel *evsel); 10169aad6f1SArnaldo Carvalho de Melo void perf_evsel__delete(struct perf_evsel *evsel); 10269aad6f1SArnaldo Carvalho de Melo 1030f82ebc4SArnaldo Carvalho de Melo void perf_evsel__config(struct perf_evsel *evsel, 104cac21425SJiri Olsa struct perf_record_opts *opts); 1050f82ebc4SArnaldo Carvalho de Melo 1060b668bc9SArnaldo Carvalho de Melo bool perf_evsel__is_cache_op_valid(u8 type, u8 op); 1070b668bc9SArnaldo Carvalho de Melo 1080b668bc9SArnaldo Carvalho de Melo #define PERF_EVSEL__MAX_ALIASES 8 1090b668bc9SArnaldo Carvalho de Melo 1100b668bc9SArnaldo Carvalho de Melo extern const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] 1110b668bc9SArnaldo Carvalho de Melo [PERF_EVSEL__MAX_ALIASES]; 1120b668bc9SArnaldo Carvalho de Melo extern const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] 1130b668bc9SArnaldo Carvalho de Melo [PERF_EVSEL__MAX_ALIASES]; 1148ad7013bSArnaldo Carvalho de Melo extern const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] 1150b668bc9SArnaldo Carvalho de Melo [PERF_EVSEL__MAX_ALIASES]; 1168ad7013bSArnaldo Carvalho de Melo extern const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX]; 1178ad7013bSArnaldo Carvalho de Melo extern const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX]; 1180b668bc9SArnaldo Carvalho de Melo int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, 1190b668bc9SArnaldo Carvalho de Melo char *bf, size_t size); 1207289f83cSArnaldo Carvalho de Melo const char *perf_evsel__name(struct perf_evsel *evsel); 121717e263fSNamhyung Kim const char *perf_evsel__group_name(struct perf_evsel *evsel); 122717e263fSNamhyung Kim int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size); 123c410431cSArnaldo Carvalho de Melo 12469aad6f1SArnaldo Carvalho de Melo int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads); 12570db7533SArnaldo Carvalho de Melo int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads); 126c52b12edSArnaldo Carvalho de Melo int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus); 127a7e191c3SFrederik Deweerdt void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus); 12869aad6f1SArnaldo Carvalho de Melo void perf_evsel__free_fd(struct perf_evsel *evsel); 12970db7533SArnaldo Carvalho de Melo void perf_evsel__free_id(struct perf_evsel *evsel); 13043f8e76eSNamhyung Kim void perf_evsel__free_counts(struct perf_evsel *evsel); 131c52b12edSArnaldo Carvalho de Melo void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads); 13269aad6f1SArnaldo Carvalho de Melo 1337be5ebe8SArnaldo Carvalho de Melo void __perf_evsel__set_sample_bit(struct perf_evsel *evsel, 1347be5ebe8SArnaldo Carvalho de Melo enum perf_event_sample_format bit); 1357be5ebe8SArnaldo Carvalho de Melo void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel, 1367be5ebe8SArnaldo Carvalho de Melo enum perf_event_sample_format bit); 1377be5ebe8SArnaldo Carvalho de Melo 1387be5ebe8SArnaldo Carvalho de Melo #define perf_evsel__set_sample_bit(evsel, bit) \ 1397be5ebe8SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit) 1407be5ebe8SArnaldo Carvalho de Melo 1417be5ebe8SArnaldo Carvalho de Melo #define perf_evsel__reset_sample_bit(evsel, bit) \ 1427be5ebe8SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit) 1437be5ebe8SArnaldo Carvalho de Melo 1447a5a5ca5SArnaldo Carvalho de Melo void perf_evsel__set_sample_id(struct perf_evsel *evsel); 1457a5a5ca5SArnaldo Carvalho de Melo 146745cefc5SArnaldo Carvalho de Melo int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads, 147745cefc5SArnaldo Carvalho de Melo const char *filter); 148745cefc5SArnaldo Carvalho de Melo 149f08199d3SArnaldo Carvalho de Melo int perf_evsel__open_per_cpu(struct perf_evsel *evsel, 1506a4bb04cSJiri Olsa struct cpu_map *cpus); 151f08199d3SArnaldo Carvalho de Melo int perf_evsel__open_per_thread(struct perf_evsel *evsel, 1526a4bb04cSJiri Olsa struct thread_map *threads); 153f08199d3SArnaldo Carvalho de Melo int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, 1546a4bb04cSJiri Olsa struct thread_map *threads); 155727ab04eSArnaldo Carvalho de Melo void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads); 15648290609SArnaldo Carvalho de Melo 1575555ded4SArnaldo Carvalho de Melo struct perf_sample; 1585555ded4SArnaldo Carvalho de Melo 1595d2074eaSArnaldo Carvalho de Melo void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample, 1605555ded4SArnaldo Carvalho de Melo const char *name); 1615555ded4SArnaldo Carvalho de Melo u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample, 1625555ded4SArnaldo Carvalho de Melo const char *name); 1635555ded4SArnaldo Carvalho de Melo 1645d2074eaSArnaldo Carvalho de Melo static inline char *perf_evsel__strval(struct perf_evsel *evsel, 1655d2074eaSArnaldo Carvalho de Melo struct perf_sample *sample, 1665d2074eaSArnaldo Carvalho de Melo const char *name) 1675d2074eaSArnaldo Carvalho de Melo { 1685d2074eaSArnaldo Carvalho de Melo return perf_evsel__rawptr(evsel, sample, name); 1695d2074eaSArnaldo Carvalho de Melo } 1705d2074eaSArnaldo Carvalho de Melo 171efd2b924SArnaldo Carvalho de Melo struct format_field; 172efd2b924SArnaldo Carvalho de Melo 173efd2b924SArnaldo Carvalho de Melo struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name); 174efd2b924SArnaldo Carvalho de Melo 175daec78a0SArnaldo Carvalho de Melo #define perf_evsel__match(evsel, t, c) \ 176daec78a0SArnaldo Carvalho de Melo (evsel->attr.type == PERF_TYPE_##t && \ 177daec78a0SArnaldo Carvalho de Melo evsel->attr.config == PERF_COUNT_##c) 178daec78a0SArnaldo Carvalho de Melo 179863e451fSJiri Olsa static inline bool perf_evsel__match2(struct perf_evsel *e1, 180863e451fSJiri Olsa struct perf_evsel *e2) 181863e451fSJiri Olsa { 182863e451fSJiri Olsa return (e1->attr.type == e2->attr.type) && 183863e451fSJiri Olsa (e1->attr.config == e2->attr.config); 184863e451fSJiri Olsa } 185863e451fSJiri Olsa 186c52b12edSArnaldo Carvalho de Melo int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, 187c52b12edSArnaldo Carvalho de Melo int cpu, int thread, bool scale); 188c52b12edSArnaldo Carvalho de Melo 189c52b12edSArnaldo Carvalho de Melo /** 190c52b12edSArnaldo Carvalho de Melo * perf_evsel__read_on_cpu - Read out the results on a CPU and thread 191c52b12edSArnaldo Carvalho de Melo * 192c52b12edSArnaldo Carvalho de Melo * @evsel - event selector to read value 193c52b12edSArnaldo Carvalho de Melo * @cpu - CPU of interest 194c52b12edSArnaldo Carvalho de Melo * @thread - thread of interest 195c52b12edSArnaldo Carvalho de Melo */ 196c52b12edSArnaldo Carvalho de Melo static inline int perf_evsel__read_on_cpu(struct perf_evsel *evsel, 197c52b12edSArnaldo Carvalho de Melo int cpu, int thread) 198c52b12edSArnaldo Carvalho de Melo { 199c52b12edSArnaldo Carvalho de Melo return __perf_evsel__read_on_cpu(evsel, cpu, thread, false); 200c52b12edSArnaldo Carvalho de Melo } 201c52b12edSArnaldo Carvalho de Melo 202c52b12edSArnaldo Carvalho de Melo /** 203c52b12edSArnaldo Carvalho de Melo * perf_evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled 204c52b12edSArnaldo Carvalho de Melo * 205c52b12edSArnaldo Carvalho de Melo * @evsel - event selector to read value 206c52b12edSArnaldo Carvalho de Melo * @cpu - CPU of interest 207c52b12edSArnaldo Carvalho de Melo * @thread - thread of interest 208c52b12edSArnaldo Carvalho de Melo */ 209c52b12edSArnaldo Carvalho de Melo static inline int perf_evsel__read_on_cpu_scaled(struct perf_evsel *evsel, 210c52b12edSArnaldo Carvalho de Melo int cpu, int thread) 211c52b12edSArnaldo Carvalho de Melo { 212c52b12edSArnaldo Carvalho de Melo return __perf_evsel__read_on_cpu(evsel, cpu, thread, true); 213c52b12edSArnaldo Carvalho de Melo } 214c52b12edSArnaldo Carvalho de Melo 215c52b12edSArnaldo Carvalho de Melo int __perf_evsel__read(struct perf_evsel *evsel, int ncpus, int nthreads, 216c52b12edSArnaldo Carvalho de Melo bool scale); 217c52b12edSArnaldo Carvalho de Melo 218c52b12edSArnaldo Carvalho de Melo /** 219c52b12edSArnaldo Carvalho de Melo * perf_evsel__read - Read the aggregate results on all CPUs 220c52b12edSArnaldo Carvalho de Melo * 221c52b12edSArnaldo Carvalho de Melo * @evsel - event selector to read value 222c52b12edSArnaldo Carvalho de Melo * @ncpus - Number of cpus affected, from zero 223c52b12edSArnaldo Carvalho de Melo * @nthreads - Number of threads affected, from zero 224c52b12edSArnaldo Carvalho de Melo */ 225c52b12edSArnaldo Carvalho de Melo static inline int perf_evsel__read(struct perf_evsel *evsel, 226c52b12edSArnaldo Carvalho de Melo int ncpus, int nthreads) 227c52b12edSArnaldo Carvalho de Melo { 228c52b12edSArnaldo Carvalho de Melo return __perf_evsel__read(evsel, ncpus, nthreads, false); 229c52b12edSArnaldo Carvalho de Melo } 230c52b12edSArnaldo Carvalho de Melo 231c52b12edSArnaldo Carvalho de Melo /** 232c52b12edSArnaldo Carvalho de Melo * perf_evsel__read_scaled - Read the aggregate results on all CPUs, scaled 233c52b12edSArnaldo Carvalho de Melo * 234c52b12edSArnaldo Carvalho de Melo * @evsel - event selector to read value 235c52b12edSArnaldo Carvalho de Melo * @ncpus - Number of cpus affected, from zero 236c52b12edSArnaldo Carvalho de Melo * @nthreads - Number of threads affected, from zero 237c52b12edSArnaldo Carvalho de Melo */ 238c52b12edSArnaldo Carvalho de Melo static inline int perf_evsel__read_scaled(struct perf_evsel *evsel, 239c52b12edSArnaldo Carvalho de Melo int ncpus, int nthreads) 240c52b12edSArnaldo Carvalho de Melo { 241c52b12edSArnaldo Carvalho de Melo return __perf_evsel__read(evsel, ncpus, nthreads, true); 242c52b12edSArnaldo Carvalho de Melo } 243c52b12edSArnaldo Carvalho de Melo 2444bf9ce1bSJiri Olsa void hists__init(struct hists *hists); 2454bf9ce1bSJiri Olsa 246a3f698feSArnaldo Carvalho de Melo int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event, 2470807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample); 2480c21f736SArnaldo Carvalho de Melo 2490c21f736SArnaldo Carvalho de Melo static inline struct perf_evsel *perf_evsel__next(struct perf_evsel *evsel) 2500c21f736SArnaldo Carvalho de Melo { 2510c21f736SArnaldo Carvalho de Melo return list_entry(evsel->node.next, struct perf_evsel, node); 2520c21f736SArnaldo Carvalho de Melo } 25307ac002fSArnaldo Carvalho de Melo 254759ff497SNamhyung Kim /** 255759ff497SNamhyung Kim * perf_evsel__is_group_leader - Return whether given evsel is a leader event 256759ff497SNamhyung Kim * 257759ff497SNamhyung Kim * @evsel - evsel selector to be tested 258759ff497SNamhyung Kim * 259759ff497SNamhyung Kim * Return %true if @evsel is a group leader or a stand-alone event 260759ff497SNamhyung Kim */ 261823254edSNamhyung Kim static inline bool perf_evsel__is_group_leader(const struct perf_evsel *evsel) 26207ac002fSArnaldo Carvalho de Melo { 263823254edSNamhyung Kim return evsel->leader == evsel; 26407ac002fSArnaldo Carvalho de Melo } 2650698aeddSArnaldo Carvalho de Melo 266759ff497SNamhyung Kim /** 267759ff497SNamhyung Kim * perf_evsel__is_group_event - Return whether given evsel is a group event 268759ff497SNamhyung Kim * 269759ff497SNamhyung Kim * @evsel - evsel selector to be tested 270759ff497SNamhyung Kim * 271759ff497SNamhyung Kim * Return %true iff event group view is enabled and @evsel is a actual group 272759ff497SNamhyung Kim * leader which has other members in the group 273759ff497SNamhyung Kim */ 274759ff497SNamhyung Kim static inline bool perf_evsel__is_group_event(struct perf_evsel *evsel) 275759ff497SNamhyung Kim { 276759ff497SNamhyung Kim if (!symbol_conf.event_group) 277759ff497SNamhyung Kim return false; 278759ff497SNamhyung Kim 279759ff497SNamhyung Kim return perf_evsel__is_group_leader(evsel) && evsel->nr_members > 1; 280759ff497SNamhyung Kim } 281759ff497SNamhyung Kim 2820698aeddSArnaldo Carvalho de Melo struct perf_attr_details { 2830698aeddSArnaldo Carvalho de Melo bool freq; 2840698aeddSArnaldo Carvalho de Melo bool verbose; 285e35ef355SArnaldo Carvalho de Melo bool event_group; 2860698aeddSArnaldo Carvalho de Melo }; 2870698aeddSArnaldo Carvalho de Melo 2880698aeddSArnaldo Carvalho de Melo int perf_evsel__fprintf(struct perf_evsel *evsel, 2890698aeddSArnaldo Carvalho de Melo struct perf_attr_details *details, FILE *fp); 290c0a54341SArnaldo Carvalho de Melo 291c0a54341SArnaldo Carvalho de Melo bool perf_evsel__fallback(struct perf_evsel *evsel, int err, 292c0a54341SArnaldo Carvalho de Melo char *msg, size_t msgsize); 29356e52e85SArnaldo Carvalho de Melo int perf_evsel__open_strerror(struct perf_evsel *evsel, 29456e52e85SArnaldo Carvalho de Melo struct perf_target *target, 29556e52e85SArnaldo Carvalho de Melo int err, char *msg, size_t size); 29697f63e4aSNamhyung Kim 29797f63e4aSNamhyung Kim static inline int perf_evsel__group_idx(struct perf_evsel *evsel) 29897f63e4aSNamhyung Kim { 29997f63e4aSNamhyung Kim return evsel->idx - evsel->leader->idx; 30097f63e4aSNamhyung Kim } 301717e263fSNamhyung Kim 302717e263fSNamhyung Kim #define for_each_group_member(_evsel, _leader) \ 303717e263fSNamhyung Kim for ((_evsel) = list_entry((_leader)->node.next, struct perf_evsel, node); \ 304717e263fSNamhyung Kim (_evsel) && (_evsel)->leader == (_leader); \ 305717e263fSNamhyung Kim (_evsel) = list_entry((_evsel)->node.next, struct perf_evsel, node)) 306717e263fSNamhyung Kim 30769aad6f1SArnaldo Carvalho de Melo #endif /* __PERF_EVSEL_H */ 308