stat.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 *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. };
  68. #undef ID
  69. void perf_stat_evsel_id_init(struct perf_evsel *evsel)
  70. {
  71. struct perf_stat *ps = evsel->priv;
  72. int i;
  73. /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
  74. for (i = 0; i < PERF_STAT_EVSEL_ID__MAX; i++) {
  75. if (!strcmp(perf_evsel__name(evsel), id_str[i])) {
  76. ps->id = i;
  77. break;
  78. }
  79. }
  80. }
  81. void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
  82. {
  83. int i;
  84. struct perf_stat *ps = evsel->priv;
  85. for (i = 0; i < 3; i++)
  86. init_stats(&ps->res_stats[i]);
  87. perf_stat_evsel_id_init(evsel);
  88. }
  89. int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
  90. {
  91. evsel->priv = zalloc(sizeof(struct perf_stat));
  92. if (evsel->priv == NULL)
  93. return -ENOMEM;
  94. perf_evsel__reset_stat_priv(evsel);
  95. return 0;
  96. }
  97. void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
  98. {
  99. zfree(&evsel->priv);
  100. }
  101. int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
  102. int ncpus, int nthreads)
  103. {
  104. struct perf_counts *counts;
  105. counts = perf_counts__new(ncpus, nthreads);
  106. if (counts)
  107. evsel->prev_raw_counts = counts;
  108. return counts ? 0 : -ENOMEM;
  109. }
  110. void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
  111. {
  112. perf_counts__delete(evsel->prev_raw_counts);
  113. evsel->prev_raw_counts = NULL;
  114. }
  115. int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
  116. {
  117. int ncpus = perf_evsel__nr_cpus(evsel);
  118. int nthreads = thread_map__nr(evsel->threads);
  119. if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
  120. perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
  121. (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
  122. return -ENOMEM;
  123. return 0;
  124. }
  125. int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
  126. {
  127. struct perf_evsel *evsel;
  128. evlist__for_each(evlist, evsel) {
  129. if (perf_evsel__alloc_stats(evsel, alloc_raw))
  130. goto out_free;
  131. }
  132. return 0;
  133. out_free:
  134. perf_evlist__free_stats(evlist);
  135. return -1;
  136. }
  137. void perf_evlist__free_stats(struct perf_evlist *evlist)
  138. {
  139. struct perf_evsel *evsel;
  140. evlist__for_each(evlist, evsel) {
  141. perf_evsel__free_stat_priv(evsel);
  142. perf_evsel__free_counts(evsel);
  143. perf_evsel__free_prev_raw_counts(evsel);
  144. }
  145. }
  146. void perf_evlist__reset_stats(struct perf_evlist *evlist)
  147. {
  148. struct perf_evsel *evsel;
  149. evlist__for_each(evlist, evsel) {
  150. perf_evsel__reset_stat_priv(evsel);
  151. perf_evsel__reset_counts(evsel);
  152. }
  153. }
  154. static void zero_per_pkg(struct perf_evsel *counter)
  155. {
  156. if (counter->per_pkg_mask)
  157. memset(counter->per_pkg_mask, 0, MAX_NR_CPUS);
  158. }
  159. static int check_per_pkg(struct perf_evsel *counter, int cpu, bool *skip)
  160. {
  161. unsigned long *mask = counter->per_pkg_mask;
  162. struct cpu_map *cpus = perf_evsel__cpus(counter);
  163. int s;
  164. *skip = false;
  165. if (!counter->per_pkg)
  166. return 0;
  167. if (cpu_map__empty(cpus))
  168. return 0;
  169. if (!mask) {
  170. mask = zalloc(MAX_NR_CPUS);
  171. if (!mask)
  172. return -ENOMEM;
  173. counter->per_pkg_mask = mask;
  174. }
  175. s = cpu_map__get_socket(cpus, cpu);
  176. if (s < 0)
  177. return -1;
  178. *skip = test_and_set_bit(s, mask) == 1;
  179. return 0;
  180. }
  181. static int
  182. process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
  183. int cpu, int thread,
  184. struct perf_counts_values *count)
  185. {
  186. struct perf_counts_values *aggr = &evsel->counts->aggr;
  187. static struct perf_counts_values zero;
  188. bool skip = false;
  189. if (check_per_pkg(evsel, cpu, &skip)) {
  190. pr_err("failed to read per-pkg counter\n");
  191. return -1;
  192. }
  193. if (skip)
  194. count = &zero;
  195. switch (config->aggr_mode) {
  196. case AGGR_THREAD:
  197. case AGGR_CORE:
  198. case AGGR_SOCKET:
  199. case AGGR_NONE:
  200. if (!evsel->snapshot)
  201. perf_evsel__compute_deltas(evsel, cpu, thread, count);
  202. perf_counts_values__scale(count, config->scale, NULL);
  203. if (config->aggr_mode == AGGR_NONE)
  204. perf_stat__update_shadow_stats(evsel, count->values, cpu);
  205. break;
  206. case AGGR_GLOBAL:
  207. aggr->val += count->val;
  208. if (config->scale) {
  209. aggr->ena += count->ena;
  210. aggr->run += count->run;
  211. }
  212. default:
  213. break;
  214. }
  215. return 0;
  216. }
  217. static int process_counter_maps(struct perf_stat_config *config,
  218. struct perf_evsel *counter)
  219. {
  220. int nthreads = thread_map__nr(counter->threads);
  221. int ncpus = perf_evsel__nr_cpus(counter);
  222. int cpu, thread;
  223. if (counter->system_wide)
  224. nthreads = 1;
  225. for (thread = 0; thread < nthreads; thread++) {
  226. for (cpu = 0; cpu < ncpus; cpu++) {
  227. if (process_counter_values(config, counter, cpu, thread,
  228. perf_counts(counter->counts, cpu, thread)))
  229. return -1;
  230. }
  231. }
  232. return 0;
  233. }
  234. int perf_stat_process_counter(struct perf_stat_config *config,
  235. struct perf_evsel *counter)
  236. {
  237. struct perf_counts_values *aggr = &counter->counts->aggr;
  238. struct perf_stat *ps = counter->priv;
  239. u64 *count = counter->counts->aggr.values;
  240. int i, ret;
  241. aggr->val = aggr->ena = aggr->run = 0;
  242. init_stats(ps->res_stats);
  243. if (counter->per_pkg)
  244. zero_per_pkg(counter);
  245. ret = process_counter_maps(config, counter);
  246. if (ret)
  247. return ret;
  248. if (config->aggr_mode != AGGR_GLOBAL)
  249. return 0;
  250. if (!counter->snapshot)
  251. perf_evsel__compute_deltas(counter, -1, -1, aggr);
  252. perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
  253. for (i = 0; i < 3; i++)
  254. update_stats(&ps->res_stats[i], count[i]);
  255. if (verbose) {
  256. fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  257. perf_evsel__name(counter), count[0], count[1], count[2]);
  258. }
  259. /*
  260. * Save the full runtime - to allow normalization during printout:
  261. */
  262. perf_stat__update_shadow_stats(counter, count, 0);
  263. return 0;
  264. }