stat.c 9.5 KB

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