stat.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. struct perf_counts *perf_counts__new(int ncpus, int nthreads)
  82. {
  83. struct perf_counts *counts = zalloc(sizeof(*counts));
  84. if (counts) {
  85. struct xyarray *values;
  86. values = xyarray__new(ncpus, nthreads, sizeof(struct perf_counts_values));
  87. if (!values) {
  88. free(counts);
  89. return NULL;
  90. }
  91. counts->values = values;
  92. }
  93. return counts;
  94. }
  95. void perf_counts__delete(struct perf_counts *counts)
  96. {
  97. if (counts) {
  98. xyarray__delete(counts->values);
  99. free(counts);
  100. }
  101. }
  102. static void perf_counts__reset(struct perf_counts *counts)
  103. {
  104. xyarray__reset(counts->values);
  105. }
  106. void perf_evsel__reset_counts(struct perf_evsel *evsel)
  107. {
  108. perf_counts__reset(evsel->counts);
  109. }
  110. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus, int nthreads)
  111. {
  112. evsel->counts = perf_counts__new(ncpus, nthreads);
  113. return evsel->counts != NULL ? 0 : -ENOMEM;
  114. }
  115. void perf_evsel__free_counts(struct perf_evsel *evsel)
  116. {
  117. perf_counts__delete(evsel->counts);
  118. evsel->counts = NULL;
  119. }
  120. void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
  121. {
  122. int i;
  123. struct perf_stat *ps = evsel->priv;
  124. for (i = 0; i < 3; i++)
  125. init_stats(&ps->res_stats[i]);
  126. perf_stat_evsel_id_init(evsel);
  127. }
  128. int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
  129. {
  130. evsel->priv = zalloc(sizeof(struct perf_stat));
  131. if (evsel->priv == NULL)
  132. return -ENOMEM;
  133. perf_evsel__reset_stat_priv(evsel);
  134. return 0;
  135. }
  136. void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
  137. {
  138. zfree(&evsel->priv);
  139. }
  140. int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel,
  141. int ncpus, int nthreads)
  142. {
  143. struct perf_counts *counts;
  144. counts = perf_counts__new(ncpus, nthreads);
  145. if (counts)
  146. evsel->prev_raw_counts = counts;
  147. return counts ? 0 : -ENOMEM;
  148. }
  149. void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
  150. {
  151. perf_counts__delete(evsel->prev_raw_counts);
  152. evsel->prev_raw_counts = NULL;
  153. }
  154. int perf_evsel__alloc_stats(struct perf_evsel *evsel, bool alloc_raw)
  155. {
  156. int ncpus = perf_evsel__nr_cpus(evsel);
  157. int nthreads = thread_map__nr(evsel->threads);
  158. if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
  159. perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
  160. (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
  161. return -ENOMEM;
  162. return 0;
  163. }
  164. int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
  165. {
  166. struct perf_evsel *evsel;
  167. evlist__for_each(evlist, evsel) {
  168. if (perf_evsel__alloc_stats(evsel, alloc_raw))
  169. goto out_free;
  170. }
  171. return 0;
  172. out_free:
  173. perf_evlist__free_stats(evlist);
  174. return -1;
  175. }
  176. void perf_evlist__free_stats(struct perf_evlist *evlist)
  177. {
  178. struct perf_evsel *evsel;
  179. evlist__for_each(evlist, evsel) {
  180. perf_evsel__free_stat_priv(evsel);
  181. perf_evsel__free_counts(evsel);
  182. perf_evsel__free_prev_raw_counts(evsel);
  183. }
  184. }
  185. void perf_evlist__reset_stats(struct perf_evlist *evlist)
  186. {
  187. struct perf_evsel *evsel;
  188. evlist__for_each(evlist, evsel) {
  189. perf_evsel__reset_stat_priv(evsel);
  190. perf_evsel__reset_counts(evsel);
  191. }
  192. }