stat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <math.h>
  5. #include "stat.h"
  6. #include "evlist.h"
  7. #include "evsel.h"
  8. #include "thread_map.h"
  9. void update_stats(struct stats *stats, u64 val)
  10. {
  11. double delta;
  12. stats->n++;
  13. delta = val - stats->mean;
  14. stats->mean += delta / stats->n;
  15. stats->M2 += delta*(val - stats->mean);
  16. if (val > stats->max)
  17. stats->max = val;
  18. if (val < stats->min)
  19. stats->min = val;
  20. }
  21. double avg_stats(struct stats *stats)
  22. {
  23. return stats->mean;
  24. }
  25. /*
  26. * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
  27. *
  28. * (\Sum n_i^2) - ((\Sum n_i)^2)/n
  29. * s^2 = -------------------------------
  30. * n - 1
  31. *
  32. * http://en.wikipedia.org/wiki/Stddev
  33. *
  34. * The std dev of the mean is related to the std dev by:
  35. *
  36. * s
  37. * s_mean = -------
  38. * sqrt(n)
  39. *
  40. */
  41. double stddev_stats(struct stats *stats)
  42. {
  43. double variance, variance_mean;
  44. if (stats->n < 2)
  45. return 0.0;
  46. variance = stats->M2 / (stats->n - 1);
  47. variance_mean = variance / stats->n;
  48. return sqrt(variance_mean);
  49. }
  50. double rel_stddev_stats(double stddev, double avg)
  51. {
  52. double pct = 0.0;
  53. if (avg)
  54. pct = 100.0 * stddev/avg;
  55. return pct;
  56. }
  57. bool __perf_evsel_stat__is(struct perf_evsel *evsel,
  58. enum perf_stat_evsel_id id)
  59. {
  60. struct perf_stat_evsel *ps = evsel->stats;
  61. return ps->id == id;
  62. }
  63. #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
  64. static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
  65. ID(NONE, x),
  66. ID(CYCLES_IN_TX, cpu/cycles-t/),
  67. ID(TRANSACTION_START, cpu/tx-start/),
  68. ID(ELISION_START, cpu/el-start/),
  69. ID(CYCLES_IN_TX_CP, cpu/cycles-ct/),
  70. ID(TOPDOWN_TOTAL_SLOTS, topdown-total-slots),
  71. ID(TOPDOWN_SLOTS_ISSUED, topdown-slots-issued),
  72. ID(TOPDOWN_SLOTS_RETIRED, topdown-slots-retired),
  73. ID(TOPDOWN_FETCH_BUBBLES, topdown-fetch-bubbles),
  74. ID(TOPDOWN_RECOVERY_BUBBLES, topdown-recovery-bubbles),
  75. ID(SMI_NUM, msr/smi/),
  76. ID(APERF, msr/aperf/),
  77. };
  78. #undef ID
  79. static void perf_stat_evsel_id_init(struct perf_evsel *evsel)
  80. {
  81. struct perf_stat_evsel *ps = evsel->stats;
  82. int i;
  83. /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
  84. for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
  85. if (!strcmp(perf_evsel__name(evsel), id_str[i])) {
  86. ps->id = i;
  87. break;
  88. }
  89. }
  90. }
  91. static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
  92. {
  93. int i;
  94. struct perf_stat_evsel *ps = evsel->stats;
  95. for (i = 0; i < 3; i++)
  96. init_stats(&ps->res_stats[i]);
  97. perf_stat_evsel_id_init(evsel);
  98. }
  99. static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
  100. {
  101. evsel->stats = zalloc(sizeof(struct perf_stat_evsel));
  102. if (evsel->stats == NULL)
  103. return -ENOMEM;
  104. perf_evsel__reset_stat_priv(evsel);
  105. return 0;
  106. }
  107. static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
  108. {
  109. struct perf_stat_evsel *ps = evsel->stats;
  110. if (ps)
  111. free(ps->group_data);
  112. zfree(&evsel->stats);
  113. }
  114. static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
  115. int ncpus, int nthreads)
  116. {
  117. struct perf_counts *counts;
  118. counts = perf_counts__new(ncpus, nthreads);
  119. if (counts)
  120. evsel->prev_raw_counts = counts;
  121. return counts ? 0 : -ENOMEM;
  122. }
  123. static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
  124. {
  125. perf_counts__delete(evsel->prev_raw_counts);
  126. evsel->prev_raw_counts = NULL;
  127. }
  128. static void perf_evsel__reset_prev_raw_counts(struct perf_evsel *evsel)
  129. {
  130. if (evsel->prev_raw_counts) {
  131. evsel->prev_raw_counts->aggr.val = 0;
  132. evsel->prev_raw_counts->aggr.ena = 0;
  133. evsel->prev_raw_counts->aggr.run = 0;
  134. }
  135. }
  136. static int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
  137. {
  138. int ncpus = perf_evsel__nr_cpus(evsel);
  139. int nthreads = thread_map__nr(evsel->threads);
  140. if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
  141. perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
  142. (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
  143. return -ENOMEM;
  144. return 0;
  145. }
  146. int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
  147. {
  148. struct perf_evsel *evsel;
  149. evlist__for_each_entry(evlist, evsel) {
  150. if (perf_evsel__alloc_stats(evsel, alloc_raw))
  151. goto out_free;
  152. }
  153. return 0;
  154. out_free:
  155. perf_evlist__free_stats(evlist);
  156. return -1;
  157. }
  158. void perf_evlist__free_stats(struct perf_evlist *evlist)
  159. {
  160. struct perf_evsel *evsel;
  161. evlist__for_each_entry(evlist, evsel) {
  162. perf_evsel__free_stat_priv(evsel);
  163. perf_evsel__free_counts(evsel);
  164. perf_evsel__free_prev_raw_counts(evsel);
  165. }
  166. }
  167. void perf_evlist__reset_stats(struct perf_evlist *evlist)
  168. {
  169. struct perf_evsel *evsel;
  170. evlist__for_each_entry(evlist, evsel) {
  171. perf_evsel__reset_stat_priv(evsel);
  172. perf_evsel__reset_counts(evsel);
  173. }
  174. }
  175. void perf_evlist__reset_prev_raw_counts(struct perf_evlist *evlist)
  176. {
  177. struct perf_evsel *evsel;
  178. evlist__for_each_entry(evlist, evsel)
  179. perf_evsel__reset_prev_raw_counts(evsel);
  180. }
  181. static void zero_per_pkg(struct perf_evsel *counter)
  182. {
  183. if (counter->per_pkg_mask)
  184. memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
  185. }
  186. static int check_per_pkg(struct perf_evsel *counter,
  187. struct perf_counts_values *vals, int cpu, bool *skip)
  188. {
  189. unsigned long *mask = counter->per_pkg_mask;
  190. struct cpu_map *cpus = perf_evsel__cpus(counter);
  191. int s;
  192. *skip = false;
  193. if (!counter->per_pkg)
  194. return 0;
  195. if (cpu_map__empty(cpus))
  196. return 0;
  197. if (!mask) {
  198. mask = zalloc(MAX_NR_CPUS);
  199. if (!mask)
  200. return -ENOMEM;
  201. counter->per_pkg_mask = mask;
  202. }
  203. /*
  204. * we do not consider an event that has not run as a good
  205. * instance to mark a package as used (skip=1). Otherwise
  206. * we may run into a situation where the first CPU in a package
  207. * is not running anything, yet the second is, and this function
  208. * would mark the package as used after the first CPU and would
  209. * not read the values from the second CPU.
  210. */
  211. if (!(vals->run && vals->ena))
  212. return 0;
  213. s = cpu_map__get_socket(cpus, cpu, NULL);
  214. if (s < 0)
  215. return -1;
  216. *skip = test_and_set_bit(s, mask) == 1;
  217. return 0;
  218. }
  219. static int
  220. process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
  221. int cpu, int thread,
  222. struct perf_counts_values *count)
  223. {
  224. struct perf_counts_values *aggr = &evsel->counts->aggr;
  225. static struct perf_counts_values zero;
  226. bool skip = false;
  227. if (check_per_pkg(evsel, count, cpu, &skip)) {
  228. pr_err("failed to read per-pkg counter\n");
  229. return -1;
  230. }
  231. if (skip)
  232. count = &zero;
  233. switch (config->aggr_mode) {
  234. case AGGR_THREAD:
  235. case AGGR_CORE:
  236. case AGGR_SOCKET:
  237. case AGGR_NONE:
  238. if (!evsel->snapshot)
  239. perf_evsel__compute_deltas(evsel, cpu, thread, count);
  240. perf_counts_values__scale(count, config->scale, NULL);
  241. if (config->aggr_mode == AGGR_NONE)
  242. perf_stat__update_shadow_stats(evsel, count->val, cpu,
  243. &rt_stat);
  244. if (config->aggr_mode == AGGR_THREAD) {
  245. if (config->stats)
  246. perf_stat__update_shadow_stats(evsel,
  247. count->val, 0, &config->stats[thread]);
  248. else
  249. perf_stat__update_shadow_stats(evsel,
  250. count->val, 0, &rt_stat);
  251. }
  252. break;
  253. case AGGR_GLOBAL:
  254. aggr->val += count->val;
  255. if (config->scale) {
  256. aggr->ena += count->ena;
  257. aggr->run += count->run;
  258. }
  259. case AGGR_UNSET:
  260. default:
  261. break;
  262. }
  263. return 0;
  264. }
  265. static int process_counter_maps(struct perf_stat_config *config,
  266. struct perf_evsel *counter)
  267. {
  268. int nthreads = thread_map__nr(counter->threads);
  269. int ncpus = perf_evsel__nr_cpus(counter);
  270. int cpu, thread;
  271. if (counter->system_wide)
  272. nthreads = 1;
  273. for (thread = 0; thread < nthreads; thread++) {
  274. for (cpu = 0; cpu < ncpus; cpu++) {
  275. if (process_counter_values(config, counter, cpu, thread,
  276. perf_counts(counter->counts, cpu, thread)))
  277. return -1;
  278. }
  279. }
  280. return 0;
  281. }
  282. int perf_stat_process_counter(struct perf_stat_config *config,
  283. struct perf_evsel *counter)
  284. {
  285. struct perf_counts_values *aggr = &counter->counts->aggr;
  286. struct perf_stat_evsel *ps = counter->stats;
  287. u64 *count = counter->counts->aggr.values;
  288. int i, ret;
  289. aggr->val = aggr->ena = aggr->run = 0;
  290. /*
  291. * We calculate counter's data every interval,
  292. * and the display code shows ps->res_stats
  293. * avg value. We need to zero the stats for
  294. * interval mode, otherwise overall avg running
  295. * averages will be shown for each interval.
  296. */
  297. if (config->interval) {
  298. for (i = 0; i < 3; i++)
  299. init_stats(&ps->res_stats[i]);
  300. }
  301. if (counter->per_pkg)
  302. zero_per_pkg(counter);
  303. ret = process_counter_maps(config, counter);
  304. if (ret)
  305. return ret;
  306. if (config->aggr_mode != AGGR_GLOBAL)
  307. return 0;
  308. if (!counter->snapshot)
  309. perf_evsel__compute_deltas(counter, -1, -1, aggr);
  310. perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
  311. for (i = 0; i < 3; i++)
  312. update_stats(&ps->res_stats[i], count[i]);
  313. if (verbose > 0) {
  314. fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  315. perf_evsel__name(counter), count[0], count[1], count[2]);
  316. }
  317. /*
  318. * Save the full runtime - to allow normalization during printout:
  319. */
  320. perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat);
  321. return 0;
  322. }
  323. int perf_event__process_stat_event(struct perf_tool *tool __maybe_unused,
  324. union perf_event *event,
  325. struct perf_session *session)
  326. {
  327. struct perf_counts_values count;
  328. struct stat_event *st = &event->stat;
  329. struct perf_evsel *counter;
  330. count.val = st->val;
  331. count.ena = st->ena;
  332. count.run = st->run;
  333. counter = perf_evlist__id2evsel(session->evlist, st->id);
  334. if (!counter) {
  335. pr_err("Failed to resolve counter for stat event.\n");
  336. return -EINVAL;
  337. }
  338. *perf_counts(counter->counts, st->cpu, st->thread) = count;
  339. counter->supported = true;
  340. return 0;
  341. }
  342. size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
  343. {
  344. struct stat_event *st = (struct stat_event *) event;
  345. size_t ret;
  346. ret = fprintf(fp, "\n... id %" PRIu64 ", cpu %d, thread %d\n",
  347. st->id, st->cpu, st->thread);
  348. ret += fprintf(fp, "... value %" PRIu64 ", enabled %" PRIu64 ", running %" PRIu64 "\n",
  349. st->val, st->ena, st->run);
  350. return ret;
  351. }
  352. size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
  353. {
  354. struct stat_round_event *rd = (struct stat_round_event *)event;
  355. size_t ret;
  356. ret = fprintf(fp, "\n... time %" PRIu64 ", type %s\n", rd->time,
  357. rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
  358. return ret;
  359. }
  360. size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
  361. {
  362. struct perf_stat_config sc;
  363. size_t ret;
  364. perf_event__read_stat_config(&sc, &event->stat_config);
  365. ret = fprintf(fp, "\n");
  366. ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
  367. ret += fprintf(fp, "... scale %d\n", sc.scale);
  368. ret += fprintf(fp, "... interval %u\n", sc.interval);
  369. return ret;
  370. }