stat.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_STATS_H
  3. #define __PERF_STATS_H
  4. #include <linux/types.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/resource.h>
  8. #include "cpumap.h"
  9. #include "counts.h"
  10. struct perf_cpu_map;
  11. struct perf_stat_config;
  12. struct timespec;
  13. struct stats {
  14. double n, mean, M2;
  15. u64 max, min;
  16. };
  17. /* hold aggregated event info */
  18. struct perf_stat_aggr {
  19. /* aggregated values */
  20. struct perf_counts_values counts;
  21. /* number of entries (CPUs) aggregated */
  22. int nr;
  23. /* whether any entry has failed to read/process event */
  24. bool failed;
  25. /* to mark this data is processed already */
  26. bool used;
  27. };
  28. /* per-evsel event stats */
  29. struct perf_stat_evsel {
  30. /* used for repeated runs */
  31. struct stats res_stats;
  32. /* number of allocated 'aggr' */
  33. int nr_aggr;
  34. /* aggregated event values */
  35. struct perf_stat_aggr *aggr;
  36. /* used for group read */
  37. u64 *group_data;
  38. };
  39. enum aggr_mode {
  40. AGGR_NONE,
  41. AGGR_GLOBAL,
  42. AGGR_SOCKET,
  43. AGGR_DIE,
  44. AGGR_CLUSTER,
  45. AGGR_CACHE,
  46. AGGR_CORE,
  47. AGGR_THREAD,
  48. AGGR_UNSET,
  49. AGGR_NODE,
  50. AGGR_MAX
  51. };
  52. typedef struct aggr_cpu_id (*aggr_get_id_t)(struct perf_stat_config *config, struct perf_cpu cpu);
  53. struct perf_stat_config {
  54. enum aggr_mode aggr_mode;
  55. u32 aggr_level;
  56. bool scale;
  57. bool no_inherit;
  58. bool identifier;
  59. bool csv_output;
  60. bool json_output;
  61. bool interval_clear;
  62. bool metric_only;
  63. bool null_run;
  64. bool ru_display;
  65. bool big_num;
  66. bool hybrid_merge;
  67. bool walltime_run_table;
  68. bool all_kernel;
  69. bool all_user;
  70. bool percore_show_thread;
  71. bool summary;
  72. bool no_csv_summary;
  73. bool metric_no_group;
  74. bool metric_no_merge;
  75. bool metric_no_threshold;
  76. bool hardware_aware_grouping;
  77. bool stop_read_counter;
  78. bool iostat_run;
  79. char *user_requested_cpu_list;
  80. bool system_wide;
  81. FILE *output;
  82. unsigned int interval;
  83. unsigned int timeout;
  84. unsigned int unit_width;
  85. unsigned int metric_only_len;
  86. int times;
  87. int run_count;
  88. int print_free_counters_hint;
  89. const char *csv_sep;
  90. struct stats *walltime_nsecs_stats;
  91. struct rusage ru_data;
  92. struct cpu_aggr_map *aggr_map;
  93. aggr_get_id_t aggr_get_id;
  94. struct cpu_aggr_map *cpus_aggr_map;
  95. u64 *walltime_run;
  96. int ctl_fd;
  97. int ctl_fd_ack;
  98. bool ctl_fd_close;
  99. const char *cgroup_list;
  100. unsigned int topdown_level;
  101. };
  102. extern struct perf_stat_config stat_config;
  103. void perf_stat__set_big_num(int set);
  104. void update_stats(struct stats *stats, u64 val);
  105. double avg_stats(struct stats *stats);
  106. double stddev_stats(struct stats *stats);
  107. double rel_stddev_stats(double stddev, double avg);
  108. static inline void init_stats(struct stats *stats)
  109. {
  110. stats->n = 0.0;
  111. stats->mean = 0.0;
  112. stats->M2 = 0.0;
  113. stats->min = (u64) -1;
  114. stats->max = 0;
  115. }
  116. struct evsel;
  117. struct evlist;
  118. enum metric_threshold_classify {
  119. METRIC_THRESHOLD_UNKNOWN,
  120. METRIC_THRESHOLD_BAD,
  121. METRIC_THRESHOLD_NEARLY_BAD,
  122. METRIC_THRESHOLD_LESS_GOOD,
  123. METRIC_THRESHOLD_GOOD,
  124. };
  125. const char *metric_threshold_classify__color(enum metric_threshold_classify thresh);
  126. typedef void (*print_metric_t)(struct perf_stat_config *config,
  127. void *ctx,
  128. enum metric_threshold_classify thresh,
  129. const char *fmt,
  130. const char *unit,
  131. double val);
  132. typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
  133. /* Used to print the display name of the Default metricgroup for now. */
  134. typedef void (*print_metricgroup_header_t)(struct perf_stat_config *config,
  135. void *ctx, const char *metricgroup_name);
  136. void perf_stat__reset_shadow_stats(void);
  137. struct perf_stat_output_ctx {
  138. void *ctx;
  139. print_metric_t print_metric;
  140. new_line_t new_line;
  141. print_metricgroup_header_t print_metricgroup_header;
  142. bool force_header;
  143. };
  144. void perf_stat__print_shadow_stats(struct perf_stat_config *config,
  145. struct evsel *evsel,
  146. int aggr_idx,
  147. struct perf_stat_output_ctx *out);
  148. bool perf_stat__skip_metric_event(struct evsel *evsel);
  149. void *perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config *config,
  150. struct evsel *evsel,
  151. int aggr_idx,
  152. int *num,
  153. void *from,
  154. struct perf_stat_output_ctx *out);
  155. int evlist__alloc_stats(struct perf_stat_config *config,
  156. struct evlist *evlist, bool alloc_raw);
  157. void evlist__free_stats(struct evlist *evlist);
  158. void evlist__reset_stats(struct evlist *evlist);
  159. void evlist__reset_prev_raw_counts(struct evlist *evlist);
  160. void evlist__copy_prev_raw_counts(struct evlist *evlist);
  161. void evlist__save_aggr_prev_raw_counts(struct evlist *evlist);
  162. int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr);
  163. void evlist__reset_aggr_stats(struct evlist *evlist);
  164. void evlist__copy_res_stats(struct perf_stat_config *config, struct evlist *evlist);
  165. int perf_stat_process_counter(struct perf_stat_config *config,
  166. struct evsel *counter);
  167. void perf_stat_merge_counters(struct perf_stat_config *config, struct evlist *evlist);
  168. void perf_stat_process_percore(struct perf_stat_config *config, struct evlist *evlist);
  169. struct perf_tool;
  170. union perf_event;
  171. struct perf_session;
  172. struct target;
  173. int perf_event__process_stat_event(const struct perf_tool *tool,
  174. struct perf_session *session,
  175. union perf_event *event);
  176. size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
  177. size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
  178. size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
  179. void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
  180. struct target *_target, struct timespec *ts, int argc, const char **argv);
  181. struct metric_expr;
  182. double test_generic_metric(struct metric_expr *mexp, int aggr_idx);
  183. #endif