stat.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
  129. {
  130. int ncpus = perf_evsel__nr_cpus(evsel);
  131. int nthreads = thread_map__nr(evsel->threads);
  132. if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
  133. perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
  134. (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
  135. return -ENOMEM;
  136. return 0;
  137. }
  138. int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
  139. {
  140. struct perf_evsel *evsel;
  141. evlist__for_each_entry(evlist, evsel) {
  142. if (perf_evsel__alloc_stats(evsel, alloc_raw))
  143. goto out_free;
  144. }
  145. return 0;
  146. out_free:
  147. perf_evlist__free_stats(evlist);
  148. return -1;
  149. }
  150. void perf_evlist__free_stats(struct perf_evlist *evlist)
  151. {
  152. struct perf_evsel *evsel;
  153. evlist__for_each_entry(evlist, evsel) {
  154. perf_evsel__free_stat_priv(evsel);
  155. perf_evsel__free_counts(evsel);
  156. perf_evsel__free_prev_raw_counts(evsel);
  157. }
  158. }
  159. void perf_evlist__reset_stats(struct perf_evlist *evlist)
  160. {
  161. struct perf_evsel *evsel;
  162. evlist__for_each_entry(evlist, evsel) {
  163. perf_evsel__reset_stat_priv(evsel);
  164. perf_evsel__reset_counts(evsel);
  165. }
  166. }
  167. static void zero_per_pkg(struct perf_evsel *counter)
  168. {
  169. if (counter->per_pkg_mask)
  170. memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
  171. }
  172. static int check_per_pkg(struct perf_evsel *counter,
  173. struct perf_counts_values *vals, int cpu, bool *skip)
  174. {
  175. unsigned long *mask = counter->per_pkg_mask;
  176. struct cpu_map *cpus = perf_evsel__cpus(counter);
  177. int s;
  178. *skip = false;
  179. if (!counter->per_pkg)
  180. return 0;
  181. if (cpu_map__empty(cpus))
  182. return 0;
  183. if (!mask) {
  184. mask = zalloc(MAX_NR_CPUS);
  185. if (!mask)
  186. return -ENOMEM;
  187. counter->per_pkg_mask = mask;
  188. }
  189. /*
  190. * we do not consider an event that has not run as a good
  191. * instance to mark a package as used (skip=1). Otherwise
  192. * we may run into a situation where the first CPU in a package
  193. * is not running anything, yet the second is, and this function
  194. * would mark the package as used after the first CPU and would
  195. * not read the values from the second CPU.
  196. */
  197. if (!(vals->run && vals->ena))
  198. return 0;
  199. s = cpu_map__get_socket(cpus, cpu, NULL);
  200. if (s < 0)
  201. return -1;
  202. *skip = test_and_set_bit(s, mask) == 1;
  203. return 0;
  204. }
  205. static int
  206. process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
  207. int cpu, int thread,
  208. struct perf_counts_values *count)
  209. {
  210. struct perf_counts_values *aggr = &evsel->counts->aggr;
  211. static struct perf_counts_values zero;
  212. bool skip = false;
  213. if (check_per_pkg(evsel, count, cpu, &skip)) {
  214. pr_err("failed to read per-pkg counter\n");
  215. return -1;
  216. }
  217. if (skip)
  218. count = &zero;
  219. switch (config->aggr_mode) {
  220. case AGGR_THREAD:
  221. case AGGR_CORE:
  222. case AGGR_SOCKET:
  223. case AGGR_NONE:
  224. if (!evsel->snapshot)
  225. perf_evsel__compute_deltas(evsel, cpu, thread, count);
  226. perf_counts_values__scale(count, config->scale, NULL);
  227. if (config->aggr_mode == AGGR_NONE)
  228. perf_stat__update_shadow_stats(evsel, count->val, cpu,
  229. &rt_stat);
  230. if (config->aggr_mode == AGGR_THREAD) {
  231. if (config->stats)
  232. perf_stat__update_shadow_stats(evsel,
  233. count->val, 0, &config->stats[thread]);
  234. else
  235. perf_stat__update_shadow_stats(evsel,
  236. count->val, 0, &rt_stat);
  237. }
  238. break;
  239. case AGGR_GLOBAL:
  240. aggr->val += count->val;
  241. if (config->scale) {
  242. aggr->ena += count->ena;
  243. aggr->run += count->run;
  244. }
  245. case AGGR_UNSET:
  246. default:
  247. break;
  248. }
  249. return 0;
  250. }
  251. static int process_counter_maps(struct perf_stat_config *config,
  252. struct perf_evsel *counter)
  253. {
  254. int nthreads = thread_map__nr(counter->threads);
  255. int ncpus = perf_evsel__nr_cpus(counter);
  256. int cpu, thread;
  257. if (counter->system_wide)
  258. nthreads = 1;
  259. for (thread = 0; thread < nthreads; thread++) {
  260. for (cpu = 0; cpu < ncpus; cpu++) {
  261. if (process_counter_values(config, counter, cpu, thread,
  262. perf_counts(counter->counts, cpu, thread)))
  263. return -1;
  264. }
  265. }
  266. return 0;
  267. }
  268. int perf_stat_process_counter(struct perf_stat_config *config,
  269. struct perf_evsel *counter)
  270. {
  271. struct perf_counts_values *aggr = &counter->counts->aggr;
  272. struct perf_stat_evsel *ps = counter->stats;
  273. u64 *count = counter->counts->aggr.values;
  274. int i, ret;
  275. aggr->val = aggr->ena = aggr->run = 0;
  276. /*
  277. * We calculate counter's data every interval,
  278. * and the display code shows ps->res_stats
  279. * avg value. We need to zero the stats for
  280. * interval mode, otherwise overall avg running
  281. * averages will be shown for each interval.
  282. */
  283. if (config->interval)
  284. init_stats(ps->res_stats);
  285. if (counter->per_pkg)
  286. zero_per_pkg(counter);
  287. ret = process_counter_maps(config, counter);
  288. if (ret)
  289. return ret;
  290. if (config->aggr_mode != AGGR_GLOBAL)
  291. return 0;
  292. if (!counter->snapshot)
  293. perf_evsel__compute_deltas(counter, -1, -1, aggr);
  294. perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
  295. for (i = 0; i < 3; i++)
  296. update_stats(&ps->res_stats[i], count[i]);
  297. if (verbose > 0) {
  298. fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  299. perf_evsel__name(counter), count[0], count[1], count[2]);
  300. }
  301. /*
  302. * Save the full runtime - to allow normalization during printout:
  303. */
  304. perf_stat__update_shadow_stats(counter, *count, 0, &rt_stat);
  305. return 0;
  306. }
  307. int perf_event__process_stat_event(struct perf_tool *tool __maybe_unused,
  308. union perf_event *event,
  309. struct perf_session *session)
  310. {
  311. struct perf_counts_values count;
  312. struct stat_event *st = &event->stat;
  313. struct perf_evsel *counter;
  314. count.val = st->val;
  315. count.ena = st->ena;
  316. count.run = st->run;
  317. counter = perf_evlist__id2evsel(session->evlist, st->id);
  318. if (!counter) {
  319. pr_err("Failed to resolve counter for stat event.\n");
  320. return -EINVAL;
  321. }
  322. *perf_counts(counter->counts, st->cpu, st->thread) = count;
  323. counter->supported = true;
  324. return 0;
  325. }
  326. size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
  327. {
  328. struct stat_event *st = (struct stat_event *) event;
  329. size_t ret;
  330. ret = fprintf(fp, "\n... id %" PRIu64 ", cpu %d, thread %d\n",
  331. st->id, st->cpu, st->thread);
  332. ret += fprintf(fp, "... value %" PRIu64 ", enabled %" PRIu64 ", running %" PRIu64 "\n",
  333. st->val, st->ena, st->run);
  334. return ret;
  335. }
  336. size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
  337. {
  338. struct stat_round_event *rd = (struct stat_round_event *)event;
  339. size_t ret;
  340. ret = fprintf(fp, "\n... time %" PRIu64 ", type %s\n", rd->time,
  341. rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
  342. return ret;
  343. }
  344. size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
  345. {
  346. struct perf_stat_config sc;
  347. size_t ret;
  348. perf_event__read_stat_config(&sc, &event->stat_config);
  349. ret = fprintf(fp, "\n");
  350. ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
  351. ret += fprintf(fp, "... scale %d\n", sc.scale);
  352. ret += fprintf(fp, "... interval %u\n", sc.interval);
  353. return ret;
  354. }