Commit 0007eceaceb11520071d053acfe06ee3326b1d13

Authored by Xiao Guangrong
Committed by Arnaldo Carvalho de Melo
1 parent 034a9265c2

perf stat: Move stats related code to util/stat.c

Then, the code can be shared between kvm events and perf stat.

Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
[ Dong Hao <haodong@linux.vnet.ibm.com>: rebase it on acme's git tree ]
Signed-off-by: Dong Hao <haodong@linux.vnet.ibm.com>
Cc: Avi Kivity <avi@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: kvm@vger.kernel.org
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Runzhen Wang <runzhen@linux.vnet.ibm.com>
Cc: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com
Link: http://lkml.kernel.org/r/1347870675-31495-3-git-send-email-haodong@linux.vnet.ibm.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Showing 4 changed files with 76 additions and 54 deletions Side-by-side Diff

... ... @@ -406,6 +406,7 @@
406 406 LIB_OBJS += $(OUTPUT)util/rblist.o
407 407 LIB_OBJS += $(OUTPUT)util/intlist.o
408 408 LIB_OBJS += $(OUTPUT)util/vdso.o
  409 +LIB_OBJS += $(OUTPUT)util/stat.o
409 410  
410 411 LIB_OBJS += $(OUTPUT)ui/helpline.o
411 412 LIB_OBJS += $(OUTPUT)ui/hist.o
tools/perf/builtin-stat.c
... ... @@ -51,13 +51,13 @@
51 51 #include "util/evsel.h"
52 52 #include "util/debug.h"
53 53 #include "util/color.h"
  54 +#include "util/stat.h"
54 55 #include "util/header.h"
55 56 #include "util/cpumap.h"
56 57 #include "util/thread.h"
57 58 #include "util/thread_map.h"
58 59  
59 60 #include <sys/prctl.h>
60   -#include <math.h>
61 61 #include <locale.h>
62 62  
63 63 #define DEFAULT_SEPARATOR " "
... ... @@ -199,11 +199,6 @@
199 199  
200 200 static volatile int done = 0;
201 201  
202   -struct stats
203   -{
204   - double n, mean, M2;
205   -};
206   -
207 202 struct perf_stat {
208 203 struct stats res_stats[3];
209 204 };
... ... @@ -220,50 +215,6 @@
220 215 evsel->priv = NULL;
221 216 }
222 217  
223   -static void update_stats(struct stats *stats, u64 val)
224   -{
225   - double delta;
226   -
227   - stats->n++;
228   - delta = val - stats->mean;
229   - stats->mean += delta / stats->n;
230   - stats->M2 += delta*(val - stats->mean);
231   -}
232   -
233   -static double avg_stats(struct stats *stats)
234   -{
235   - return stats->mean;
236   -}
237   -
238   -/*
239   - * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
240   - *
241   - * (\Sum n_i^2) - ((\Sum n_i)^2)/n
242   - * s^2 = -------------------------------
243   - * n - 1
244   - *
245   - * http://en.wikipedia.org/wiki/Stddev
246   - *
247   - * The std dev of the mean is related to the std dev by:
248   - *
249   - * s
250   - * s_mean = -------
251   - * sqrt(n)
252   - *
253   - */
254   -static double stddev_stats(struct stats *stats)
255   -{
256   - double variance, variance_mean;
257   -
258   - if (!stats->n)
259   - return 0.0;
260   -
261   - variance = stats->M2 / (stats->n - 1);
262   - variance_mean = variance / stats->n;
263   -
264   - return sqrt(variance_mean);
265   -}
266   -
267 218 static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
268 219 static struct stats runtime_cycles_stats[MAX_NR_CPUS];
269 220 static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
... ... @@ -559,10 +510,7 @@
559 510  
560 511 static void print_noise_pct(double total, double avg)
561 512 {
562   - double pct = 0.0;
563   -
564   - if (avg)
565   - pct = 100.0*total/avg;
  513 + double pct = rel_stddev_stats(total, avg);
566 514  
567 515 if (csv_output)
568 516 fprintf(output, "%s%.2f%%", csv_sep, pct);
tools/perf/util/stat.c
  1 +#include <math.h>
  2 +
  3 +#include "stat.h"
  4 +
  5 +void update_stats(struct stats *stats, u64 val)
  6 +{
  7 + double delta;
  8 +
  9 + stats->n++;
  10 + delta = val - stats->mean;
  11 + stats->mean += delta / stats->n;
  12 + stats->M2 += delta*(val - stats->mean);
  13 +}
  14 +
  15 +double avg_stats(struct stats *stats)
  16 +{
  17 + return stats->mean;
  18 +}
  19 +
  20 +/*
  21 + * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
  22 + *
  23 + * (\Sum n_i^2) - ((\Sum n_i)^2)/n
  24 + * s^2 = -------------------------------
  25 + * n - 1
  26 + *
  27 + * http://en.wikipedia.org/wiki/Stddev
  28 + *
  29 + * The std dev of the mean is related to the std dev by:
  30 + *
  31 + * s
  32 + * s_mean = -------
  33 + * sqrt(n)
  34 + *
  35 + */
  36 +double stddev_stats(struct stats *stats)
  37 +{
  38 + double variance, variance_mean;
  39 +
  40 + if (!stats->n)
  41 + return 0.0;
  42 +
  43 + variance = stats->M2 / (stats->n - 1);
  44 + variance_mean = variance / stats->n;
  45 +
  46 + return sqrt(variance_mean);
  47 +}
  48 +
  49 +double rel_stddev_stats(double stddev, double avg)
  50 +{
  51 + double pct = 0.0;
  52 +
  53 + if (avg)
  54 + pct = 100.0 * stddev/avg;
  55 +
  56 + return pct;
  57 +}
tools/perf/util/stat.h
  1 +#ifndef __PERF_STATS_H
  2 +#define __PERF_STATS_H
  3 +
  4 +#include "types.h"
  5 +
  6 +struct stats
  7 +{
  8 + double n, mean, M2;
  9 +};
  10 +
  11 +void update_stats(struct stats *stats, u64 val);
  12 +double avg_stats(struct stats *stats);
  13 +double stddev_stats(struct stats *stats);
  14 +double rel_stddev_stats(double stddev, double avg);
  15 +
  16 +#endif