stat.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. };
  68. #undef ID
  69. void perf_stat_evsel_id_init(struct perf_evsel *evsel)
  70. {
  71. struct perf_stat_evsel *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_evsel *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_evsel));
  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,
  160. struct perf_counts_values *vals, int cpu, bool *skip)
  161. {
  162. unsigned long *mask = counter->per_pkg_mask;
  163. struct cpu_map *cpus = perf_evsel__cpus(counter);
  164. int s;
  165. *skip = false;
  166. if (!counter->per_pkg)
  167. return 0;
  168. if (cpu_map__empty(cpus))
  169. return 0;
  170. if (!mask) {
  171. mask = zalloc(MAX_NR_CPUS);
  172. if (!mask)
  173. return -ENOMEM;
  174. counter->per_pkg_mask = mask;
  175. }
  176. /*
  177. * we do not consider an event that has not run as a good
  178. * instance to mark a package as used (skip=1). Otherwise
  179. * we may run into a situation where the first CPU in a package
  180. * is not running anything, yet the second is, and this function
  181. * would mark the package as used after the first CPU and would
  182. * not read the values from the second CPU.
  183. */
  184. if (!(vals->run && vals->ena))
  185. return 0;
  186. s = cpu_map__get_socket(cpus, cpu, NULL);
  187. if (s < 0)
  188. return -1;
  189. *skip = test_and_set_bit(s, mask) == 1;
  190. return 0;
  191. }
  192. static int
  193. process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel,
  194. int cpu, int thread,
  195. struct perf_counts_values *count)
  196. {
  197. struct perf_counts_values *aggr = &evsel->counts->aggr;
  198. static struct perf_counts_values zero;
  199. bool skip = false;
  200. if (check_per_pkg(evsel, count, cpu, &skip)) {
  201. pr_err("failed to read per-pkg counter\n");
  202. return -1;
  203. }
  204. if (skip)
  205. count = &zero;
  206. switch (config->aggr_mode) {
  207. case AGGR_THREAD:
  208. case AGGR_CORE:
  209. case AGGR_SOCKET:
  210. case AGGR_NONE:
  211. if (!evsel->snapshot)
  212. perf_evsel__compute_deltas(evsel, cpu, thread, count);
  213. perf_counts_values__scale(count, config->scale, NULL);
  214. if (config->aggr_mode == AGGR_NONE)
  215. perf_stat__update_shadow_stats(evsel, count->values, cpu);
  216. break;
  217. case AGGR_GLOBAL:
  218. aggr->val += count->val;
  219. if (config->scale) {
  220. aggr->ena += count->ena;
  221. aggr->run += count->run;
  222. }
  223. case AGGR_UNSET:
  224. default:
  225. break;
  226. }
  227. return 0;
  228. }
  229. static int process_counter_maps(struct perf_stat_config *config,
  230. struct perf_evsel *counter)
  231. {
  232. int nthreads = thread_map__nr(counter->threads);
  233. int ncpus = perf_evsel__nr_cpus(counter);
  234. int cpu, thread;
  235. if (counter->system_wide)
  236. nthreads = 1;
  237. for (thread = 0; thread < nthreads; thread++) {
  238. for (cpu = 0; cpu < ncpus; cpu++) {
  239. if (process_counter_values(config, counter, cpu, thread,
  240. perf_counts(counter->counts, cpu, thread)))
  241. return -1;
  242. }
  243. }
  244. return 0;
  245. }
  246. int perf_stat_process_counter(struct perf_stat_config *config,
  247. struct perf_evsel *counter)
  248. {
  249. struct perf_counts_values *aggr = &counter->counts->aggr;
  250. struct perf_stat_evsel *ps = counter->priv;
  251. u64 *count = counter->counts->aggr.values;
  252. int i, ret;
  253. aggr->val = aggr->ena = aggr->run = 0;
  254. /*
  255. * We calculate counter's data every interval,
  256. * and the display code shows ps->res_stats
  257. * avg value. We need to zero the stats for
  258. * interval mode, otherwise overall avg running
  259. * averages will be shown for each interval.
  260. */
  261. if (config->interval)
  262. init_stats(ps->res_stats);
  263. if (counter->per_pkg)
  264. zero_per_pkg(counter);
  265. ret = process_counter_maps(config, counter);
  266. if (ret)
  267. return ret;
  268. if (config->aggr_mode != AGGR_GLOBAL)
  269. return 0;
  270. if (!counter->snapshot)
  271. perf_evsel__compute_deltas(counter, -1, -1, aggr);
  272. perf_counts_values__scale(aggr, config->scale, &counter->counts->scaled);
  273. for (i = 0; i < 3; i++)
  274. update_stats(&ps->res_stats[i], count[i]);
  275. if (verbose) {
  276. fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  277. perf_evsel__name(counter), count[0], count[1], count[2]);
  278. }
  279. /*
  280. * Save the full runtime - to allow normalization during printout:
  281. */
  282. perf_stat__update_shadow_stats(counter, count, 0);
  283. return 0;
  284. }