Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __PERF_STATS_H
0003 #define __PERF_STATS_H
0004 
0005 #include <linux/types.h>
0006 #include <stdio.h>
0007 #include <sys/types.h>
0008 #include <sys/resource.h>
0009 #include "cpumap.h"
0010 #include "rblist.h"
0011 
0012 struct perf_cpu_map;
0013 struct perf_stat_config;
0014 struct timespec;
0015 
0016 struct stats {
0017     double n, mean, M2;
0018     u64 max, min;
0019 };
0020 
0021 enum perf_stat_evsel_id {
0022     PERF_STAT_EVSEL_ID__NONE = 0,
0023     PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
0024     PERF_STAT_EVSEL_ID__TRANSACTION_START,
0025     PERF_STAT_EVSEL_ID__ELISION_START,
0026     PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
0027     PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
0028     PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
0029     PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
0030     PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
0031     PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
0032     PERF_STAT_EVSEL_ID__TOPDOWN_RETIRING,
0033     PERF_STAT_EVSEL_ID__TOPDOWN_BAD_SPEC,
0034     PERF_STAT_EVSEL_ID__TOPDOWN_FE_BOUND,
0035     PERF_STAT_EVSEL_ID__TOPDOWN_BE_BOUND,
0036     PERF_STAT_EVSEL_ID__TOPDOWN_HEAVY_OPS,
0037     PERF_STAT_EVSEL_ID__TOPDOWN_BR_MISPREDICT,
0038     PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_LAT,
0039     PERF_STAT_EVSEL_ID__TOPDOWN_MEM_BOUND,
0040     PERF_STAT_EVSEL_ID__SMI_NUM,
0041     PERF_STAT_EVSEL_ID__APERF,
0042     PERF_STAT_EVSEL_ID__MAX,
0043 };
0044 
0045 struct perf_stat_evsel {
0046     struct stats         res_stats[3];
0047     enum perf_stat_evsel_id  id;
0048     u64         *group_data;
0049 };
0050 
0051 enum aggr_mode {
0052     AGGR_NONE,
0053     AGGR_GLOBAL,
0054     AGGR_SOCKET,
0055     AGGR_DIE,
0056     AGGR_CORE,
0057     AGGR_THREAD,
0058     AGGR_UNSET,
0059     AGGR_NODE,
0060     AGGR_MAX
0061 };
0062 
0063 enum {
0064     CTX_BIT_USER    = 1 << 0,
0065     CTX_BIT_KERNEL  = 1 << 1,
0066     CTX_BIT_HV  = 1 << 2,
0067     CTX_BIT_HOST    = 1 << 3,
0068     CTX_BIT_IDLE    = 1 << 4,
0069     CTX_BIT_MAX = 1 << 5,
0070 };
0071 
0072 #define NUM_CTX CTX_BIT_MAX
0073 
0074 enum stat_type {
0075     STAT_NONE = 0,
0076     STAT_NSECS,
0077     STAT_CYCLES,
0078     STAT_STALLED_CYCLES_FRONT,
0079     STAT_STALLED_CYCLES_BACK,
0080     STAT_BRANCHES,
0081     STAT_CACHEREFS,
0082     STAT_L1_DCACHE,
0083     STAT_L1_ICACHE,
0084     STAT_LL_CACHE,
0085     STAT_ITLB_CACHE,
0086     STAT_DTLB_CACHE,
0087     STAT_CYCLES_IN_TX,
0088     STAT_TRANSACTION,
0089     STAT_ELISION,
0090     STAT_TOPDOWN_TOTAL_SLOTS,
0091     STAT_TOPDOWN_SLOTS_ISSUED,
0092     STAT_TOPDOWN_SLOTS_RETIRED,
0093     STAT_TOPDOWN_FETCH_BUBBLES,
0094     STAT_TOPDOWN_RECOVERY_BUBBLES,
0095     STAT_TOPDOWN_RETIRING,
0096     STAT_TOPDOWN_BAD_SPEC,
0097     STAT_TOPDOWN_FE_BOUND,
0098     STAT_TOPDOWN_BE_BOUND,
0099     STAT_TOPDOWN_HEAVY_OPS,
0100     STAT_TOPDOWN_BR_MISPREDICT,
0101     STAT_TOPDOWN_FETCH_LAT,
0102     STAT_TOPDOWN_MEM_BOUND,
0103     STAT_SMI_NUM,
0104     STAT_APERF,
0105     STAT_MAX
0106 };
0107 
0108 struct runtime_stat {
0109     struct rblist value_list;
0110 };
0111 
0112 struct rusage_stats {
0113     struct stats ru_utime_usec_stat;
0114     struct stats ru_stime_usec_stat;
0115 };
0116 
0117 typedef struct aggr_cpu_id (*aggr_get_id_t)(struct perf_stat_config *config, struct perf_cpu cpu);
0118 
0119 struct perf_stat_config {
0120     enum aggr_mode       aggr_mode;
0121     bool             scale;
0122     bool             no_inherit;
0123     bool             identifier;
0124     bool             csv_output;
0125     bool             json_output;
0126     bool             interval_clear;
0127     bool             metric_only;
0128     bool             null_run;
0129     bool             ru_display;
0130     bool             big_num;
0131     bool             no_merge;
0132     bool             hybrid_merge;
0133     bool             walltime_run_table;
0134     bool             all_kernel;
0135     bool             all_user;
0136     bool             percore_show_thread;
0137     bool             summary;
0138     bool             no_csv_summary;
0139     bool             metric_no_group;
0140     bool             metric_no_merge;
0141     bool             stop_read_counter;
0142     bool             quiet;
0143     bool             iostat_run;
0144     FILE            *output;
0145     unsigned int         interval;
0146     unsigned int         timeout;
0147     int          initial_delay;
0148     unsigned int         unit_width;
0149     unsigned int         metric_only_len;
0150     int          times;
0151     int          run_count;
0152     int          print_free_counters_hint;
0153     int          print_mixed_hw_group_error;
0154     struct runtime_stat *stats;
0155     int          stats_num;
0156     const char      *csv_sep;
0157     struct stats        *walltime_nsecs_stats;
0158     struct rusage        ru_data;
0159     struct rusage_stats      *ru_stats;
0160     struct cpu_aggr_map *aggr_map;
0161     aggr_get_id_t        aggr_get_id;
0162     struct cpu_aggr_map *cpus_aggr_map;
0163     u64         *walltime_run;
0164     struct rblist        metric_events;
0165     int          ctl_fd;
0166     int          ctl_fd_ack;
0167     bool             ctl_fd_close;
0168     const char      *cgroup_list;
0169     unsigned int        topdown_level;
0170 };
0171 
0172 void perf_stat__set_big_num(int set);
0173 void perf_stat__set_no_csv_summary(int set);
0174 
0175 void update_stats(struct stats *stats, u64 val);
0176 double avg_stats(struct stats *stats);
0177 double stddev_stats(struct stats *stats);
0178 double rel_stddev_stats(double stddev, double avg);
0179 
0180 static inline void init_stats(struct stats *stats)
0181 {
0182     stats->n    = 0.0;
0183     stats->mean = 0.0;
0184     stats->M2   = 0.0;
0185     stats->min  = (u64) -1;
0186     stats->max  = 0;
0187 }
0188 
0189 static inline void init_rusage_stats(struct rusage_stats *ru_stats) {
0190     init_stats(&ru_stats->ru_utime_usec_stat);
0191     init_stats(&ru_stats->ru_stime_usec_stat);
0192 }
0193 
0194 static inline void update_rusage_stats(struct rusage_stats *ru_stats, struct rusage* rusage) {
0195     const u64 us_to_ns = 1000;
0196     const u64 s_to_ns = 1000000000;
0197     update_stats(&ru_stats->ru_utime_usec_stat,
0198                  (rusage->ru_utime.tv_usec * us_to_ns + rusage->ru_utime.tv_sec * s_to_ns));
0199     update_stats(&ru_stats->ru_stime_usec_stat,
0200                  (rusage->ru_stime.tv_usec * us_to_ns + rusage->ru_stime.tv_sec * s_to_ns));
0201 }
0202 
0203 struct evsel;
0204 struct evlist;
0205 
0206 struct perf_aggr_thread_value {
0207     struct evsel *counter;
0208     struct aggr_cpu_id id;
0209     double uval;
0210     u64 val;
0211     u64 run;
0212     u64 ena;
0213 };
0214 
0215 bool __perf_stat_evsel__is(struct evsel *evsel, enum perf_stat_evsel_id id);
0216 
0217 #define perf_stat_evsel__is(evsel, id) \
0218     __perf_stat_evsel__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
0219 
0220 extern struct runtime_stat rt_stat;
0221 extern struct stats walltime_nsecs_stats;
0222 extern struct rusage_stats ru_stats;
0223 
0224 typedef void (*print_metric_t)(struct perf_stat_config *config,
0225                    void *ctx, const char *color, const char *unit,
0226                    const char *fmt, double val);
0227 typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
0228 
0229 void runtime_stat__init(struct runtime_stat *st);
0230 void runtime_stat__exit(struct runtime_stat *st);
0231 void perf_stat__init_shadow_stats(void);
0232 void perf_stat__reset_shadow_stats(void);
0233 void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
0234 void perf_stat__update_shadow_stats(struct evsel *counter, u64 count,
0235                     int cpu_map_idx, struct runtime_stat *st);
0236 struct perf_stat_output_ctx {
0237     void *ctx;
0238     print_metric_t print_metric;
0239     new_line_t new_line;
0240     bool force_header;
0241 };
0242 
0243 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
0244                    struct evsel *evsel,
0245                    double avg, int cpu,
0246                    struct perf_stat_output_ctx *out,
0247                    struct rblist *metric_events,
0248                    struct runtime_stat *st);
0249 void perf_stat__collect_metric_expr(struct evlist *);
0250 
0251 int evlist__alloc_stats(struct evlist *evlist, bool alloc_raw);
0252 void evlist__free_stats(struct evlist *evlist);
0253 void evlist__reset_stats(struct evlist *evlist);
0254 void evlist__reset_prev_raw_counts(struct evlist *evlist);
0255 void evlist__copy_prev_raw_counts(struct evlist *evlist);
0256 void evlist__save_aggr_prev_raw_counts(struct evlist *evlist);
0257 
0258 int perf_stat_process_counter(struct perf_stat_config *config,
0259                   struct evsel *counter);
0260 struct perf_tool;
0261 union perf_event;
0262 struct perf_session;
0263 struct target;
0264 
0265 int perf_event__process_stat_event(struct perf_session *session,
0266                    union perf_event *event);
0267 
0268 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
0269 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
0270 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
0271 
0272 int create_perf_stat_counter(struct evsel *evsel,
0273                  struct perf_stat_config *config,
0274                  struct target *target,
0275                  int cpu_map_idx);
0276 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
0277                 struct target *_target, struct timespec *ts, int argc, const char **argv);
0278 
0279 struct metric_expr;
0280 double test_generic_metric(struct metric_expr *mexp, int cpu_map_idx, struct runtime_stat *st);
0281 #endif