stat.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/time.h>
  8. #include <sys/resource.h>
  9. #include <sys/wait.h>
  10. #include "xyarray.h"
  11. #include "rblist.h"
  12. #include "perf.h"
  13. #include "event.h"
  14. struct stats {
  15. double n, mean, M2;
  16. u64 max, min;
  17. };
  18. enum perf_stat_evsel_id {
  19. PERF_STAT_EVSEL_ID__NONE = 0,
  20. PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
  21. PERF_STAT_EVSEL_ID__TRANSACTION_START,
  22. PERF_STAT_EVSEL_ID__ELISION_START,
  23. PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
  24. PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
  25. PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
  26. PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
  27. PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
  28. PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
  29. PERF_STAT_EVSEL_ID__SMI_NUM,
  30. PERF_STAT_EVSEL_ID__APERF,
  31. PERF_STAT_EVSEL_ID__MAX,
  32. };
  33. struct perf_stat_evsel {
  34. struct stats res_stats[3];
  35. enum perf_stat_evsel_id id;
  36. u64 *group_data;
  37. };
  38. enum aggr_mode {
  39. AGGR_NONE,
  40. AGGR_GLOBAL,
  41. AGGR_SOCKET,
  42. AGGR_CORE,
  43. AGGR_THREAD,
  44. AGGR_UNSET,
  45. };
  46. enum {
  47. CTX_BIT_USER = 1 << 0,
  48. CTX_BIT_KERNEL = 1 << 1,
  49. CTX_BIT_HV = 1 << 2,
  50. CTX_BIT_HOST = 1 << 3,
  51. CTX_BIT_IDLE = 1 << 4,
  52. CTX_BIT_MAX = 1 << 5,
  53. };
  54. #define NUM_CTX CTX_BIT_MAX
  55. enum stat_type {
  56. STAT_NONE = 0,
  57. STAT_NSECS,
  58. STAT_CYCLES,
  59. STAT_STALLED_CYCLES_FRONT,
  60. STAT_STALLED_CYCLES_BACK,
  61. STAT_BRANCHES,
  62. STAT_CACHEREFS,
  63. STAT_L1_DCACHE,
  64. STAT_L1_ICACHE,
  65. STAT_LL_CACHE,
  66. STAT_ITLB_CACHE,
  67. STAT_DTLB_CACHE,
  68. STAT_CYCLES_IN_TX,
  69. STAT_TRANSACTION,
  70. STAT_ELISION,
  71. STAT_TOPDOWN_TOTAL_SLOTS,
  72. STAT_TOPDOWN_SLOTS_ISSUED,
  73. STAT_TOPDOWN_SLOTS_RETIRED,
  74. STAT_TOPDOWN_FETCH_BUBBLES,
  75. STAT_TOPDOWN_RECOVERY_BUBBLES,
  76. STAT_SMI_NUM,
  77. STAT_APERF,
  78. STAT_MAX
  79. };
  80. struct runtime_stat {
  81. struct rblist value_list;
  82. };
  83. typedef int (*aggr_get_id_t)(struct perf_stat_config *config,
  84. struct cpu_map *m, int cpu);
  85. struct perf_stat_config {
  86. enum aggr_mode aggr_mode;
  87. bool scale;
  88. bool no_inherit;
  89. bool identifier;
  90. bool csv_output;
  91. bool interval_clear;
  92. bool metric_only;
  93. bool null_run;
  94. bool ru_display;
  95. bool big_num;
  96. bool no_merge;
  97. bool walltime_run_table;
  98. FILE *output;
  99. unsigned int interval;
  100. unsigned int timeout;
  101. unsigned int initial_delay;
  102. unsigned int unit_width;
  103. unsigned int metric_only_len;
  104. int times;
  105. int run_count;
  106. int print_free_counters_hint;
  107. int print_mixed_hw_group_error;
  108. struct runtime_stat *stats;
  109. int stats_num;
  110. const char *csv_sep;
  111. struct stats *walltime_nsecs_stats;
  112. struct rusage ru_data;
  113. struct cpu_map *aggr_map;
  114. aggr_get_id_t aggr_get_id;
  115. struct cpu_map *cpus_aggr_map;
  116. u64 *walltime_run;
  117. struct rblist metric_events;
  118. };
  119. void update_stats(struct stats *stats, u64 val);
  120. double avg_stats(struct stats *stats);
  121. double stddev_stats(struct stats *stats);
  122. double rel_stddev_stats(double stddev, double avg);
  123. static inline void init_stats(struct stats *stats)
  124. {
  125. stats->n = 0.0;
  126. stats->mean = 0.0;
  127. stats->M2 = 0.0;
  128. stats->min = (u64) -1;
  129. stats->max = 0;
  130. }
  131. struct perf_evsel;
  132. struct perf_evlist;
  133. struct perf_aggr_thread_value {
  134. struct perf_evsel *counter;
  135. int id;
  136. double uval;
  137. u64 val;
  138. u64 run;
  139. u64 ena;
  140. };
  141. bool __perf_evsel_stat__is(struct perf_evsel *evsel,
  142. enum perf_stat_evsel_id id);
  143. #define perf_stat_evsel__is(evsel, id) \
  144. __perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
  145. extern struct runtime_stat rt_stat;
  146. extern struct stats walltime_nsecs_stats;
  147. typedef void (*print_metric_t)(struct perf_stat_config *config,
  148. void *ctx, const char *color, const char *unit,
  149. const char *fmt, double val);
  150. typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
  151. void runtime_stat__init(struct runtime_stat *st);
  152. void runtime_stat__exit(struct runtime_stat *st);
  153. void perf_stat__init_shadow_stats(void);
  154. void perf_stat__reset_shadow_stats(void);
  155. void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
  156. void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
  157. int cpu, struct runtime_stat *st);
  158. struct perf_stat_output_ctx {
  159. void *ctx;
  160. print_metric_t print_metric;
  161. new_line_t new_line;
  162. bool force_header;
  163. };
  164. void perf_stat__print_shadow_stats(struct perf_stat_config *config,
  165. struct perf_evsel *evsel,
  166. double avg, int cpu,
  167. struct perf_stat_output_ctx *out,
  168. struct rblist *metric_events,
  169. struct runtime_stat *st);
  170. void perf_stat__collect_metric_expr(struct perf_evlist *);
  171. int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw);
  172. void perf_evlist__free_stats(struct perf_evlist *evlist);
  173. void perf_evlist__reset_stats(struct perf_evlist *evlist);
  174. int perf_stat_process_counter(struct perf_stat_config *config,
  175. struct perf_evsel *counter);
  176. struct perf_tool;
  177. union perf_event;
  178. struct perf_session;
  179. int perf_event__process_stat_event(struct perf_session *session,
  180. union perf_event *event);
  181. size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
  182. size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
  183. size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
  184. int create_perf_stat_counter(struct perf_evsel *evsel,
  185. struct perf_stat_config *config,
  186. struct target *target);
  187. int perf_stat_synthesize_config(struct perf_stat_config *config,
  188. struct perf_tool *tool,
  189. struct perf_evlist *evlist,
  190. perf_event__handler_t process,
  191. bool attrs);
  192. void
  193. perf_evlist__print_counters(struct perf_evlist *evlist,
  194. struct perf_stat_config *config,
  195. struct target *_target,
  196. struct timespec *ts,
  197. int argc, const char **argv);
  198. #endif