[PATCH v6 04/11] perf util: Update per-thread shadow stats

2017-12-04 Thread Jin Yao
The functions perf_stat__update_shadow_stats() is called
to update the shadow stats on a set of static variables.

But the static variables are the limitations to be extended
to support per-thread shadow stats.

This patch lets the perf_stat__update_shadow_stats() support
to update the shadow stats on a input parameter 'stat' and
uses update_runtime_stat() to update the stats. It will not
directly update the static variables as before.

Signed-off-by: Jin Yao 
---
 tools/perf/builtin-script.c   |  3 +-
 tools/perf/builtin-stat.c |  3 +-
 tools/perf/util/stat-shadow.c | 86 +--
 tools/perf/util/stat.c|  8 ++--
 tools/perf/util/stat.h|  2 +-
 5 files changed, 68 insertions(+), 34 deletions(-)

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 39d8b55..81b3950 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -1548,7 +1548,8 @@ static void perf_sample__fprint_metric(struct perf_script 
*script,
val = sample->period * evsel->scale;
perf_stat__update_shadow_stats(evsel,
   val,
-  sample->cpu);
+  sample->cpu,
+  _stat);
evsel_script(evsel)->val = val;
if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
for_each_group_member (ev2, evsel->leader) {
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index a027b47..3f4a2c2 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -1214,7 +1214,8 @@ static void aggr_update_shadow(void)
val += perf_counts(counter->counts, cpu, 
0)->val;
}
perf_stat__update_shadow_stats(counter, val,
-  
first_shadow_cpu(counter, id));
+   first_shadow_cpu(counter, id),
+   _stat);
}
}
 }
diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index e60c321..11c921d 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -116,19 +116,29 @@ static void saved_value_delete(struct rblist *rblist 
__maybe_unused,
 
 static struct saved_value *saved_value_lookup(struct perf_evsel *evsel,
  int cpu,
- bool create)
+ bool create,
+ enum stat_type type,
+ int ctx,
+ struct runtime_stat *stat)
 {
+   struct rblist *rblist;
struct rb_node *nd;
struct saved_value dm = {
.cpu = cpu,
.evsel = evsel,
+   .type = type,
+   .ctx = ctx,
+   .stat = stat,
};
-   nd = rblist__find(_saved_values, );
+
+   rblist = >value_list;
+
+   nd = rblist__find(rblist, );
if (nd)
return container_of(nd, struct saved_value, rb_node);
if (create) {
-   rblist__add_node(_saved_values, );
-   nd = rblist__find(_saved_values, );
+   rblist__add_node(rblist, );
+   nd = rblist__find(rblist, );
if (nd)
return container_of(nd, struct saved_value, rb_node);
}
@@ -217,13 +227,24 @@ void perf_stat__reset_shadow_stats(void)
}
 }
 
+static void update_runtime_stat(struct runtime_stat *stat,
+   enum stat_type type,
+   int ctx, int cpu, u64 count)
+{
+   struct saved_value *v = saved_value_lookup(NULL, cpu, true,
+  type, ctx, stat);
+
+   if (v)
+   update_stats(>stats, count);
+}
+
 /*
  * Update various tracking values we maintain to print
  * more semantic information such as miss/hit ratios,
  * instruction rates, etc:
  */
 void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
-   int cpu)
+   int cpu, struct runtime_stat *stat)
 {
int ctx = evsel_context(counter);
 
@@ -231,50 +252,58 @@ void perf_stat__update_shadow_stats(struct perf_evsel 
*counter, u64 count,
 
if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
-   update_stats(_nsecs_stats[cpu], count);
+   update_runtime_stat(stat, STAT_NSECS, 0, cpu, count);
else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
-   update_stats(_cycles_stats[ctx][cpu], count);
+   

[PATCH v6 04/11] perf util: Update per-thread shadow stats

2017-12-04 Thread Jin Yao
The functions perf_stat__update_shadow_stats() is called
to update the shadow stats on a set of static variables.

But the static variables are the limitations to be extended
to support per-thread shadow stats.

This patch lets the perf_stat__update_shadow_stats() support
to update the shadow stats on a input parameter 'stat' and
uses update_runtime_stat() to update the stats. It will not
directly update the static variables as before.

Signed-off-by: Jin Yao 
---
 tools/perf/builtin-script.c   |  3 +-
 tools/perf/builtin-stat.c |  3 +-
 tools/perf/util/stat-shadow.c | 86 +--
 tools/perf/util/stat.c|  8 ++--
 tools/perf/util/stat.h|  2 +-
 5 files changed, 68 insertions(+), 34 deletions(-)

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 39d8b55..81b3950 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -1548,7 +1548,8 @@ static void perf_sample__fprint_metric(struct perf_script 
*script,
val = sample->period * evsel->scale;
perf_stat__update_shadow_stats(evsel,
   val,
-  sample->cpu);
+  sample->cpu,
+  _stat);
evsel_script(evsel)->val = val;
if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
for_each_group_member (ev2, evsel->leader) {
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index a027b47..3f4a2c2 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -1214,7 +1214,8 @@ static void aggr_update_shadow(void)
val += perf_counts(counter->counts, cpu, 
0)->val;
}
perf_stat__update_shadow_stats(counter, val,
-  
first_shadow_cpu(counter, id));
+   first_shadow_cpu(counter, id),
+   _stat);
}
}
 }
diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index e60c321..11c921d 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -116,19 +116,29 @@ static void saved_value_delete(struct rblist *rblist 
__maybe_unused,
 
 static struct saved_value *saved_value_lookup(struct perf_evsel *evsel,
  int cpu,
- bool create)
+ bool create,
+ enum stat_type type,
+ int ctx,
+ struct runtime_stat *stat)
 {
+   struct rblist *rblist;
struct rb_node *nd;
struct saved_value dm = {
.cpu = cpu,
.evsel = evsel,
+   .type = type,
+   .ctx = ctx,
+   .stat = stat,
};
-   nd = rblist__find(_saved_values, );
+
+   rblist = >value_list;
+
+   nd = rblist__find(rblist, );
if (nd)
return container_of(nd, struct saved_value, rb_node);
if (create) {
-   rblist__add_node(_saved_values, );
-   nd = rblist__find(_saved_values, );
+   rblist__add_node(rblist, );
+   nd = rblist__find(rblist, );
if (nd)
return container_of(nd, struct saved_value, rb_node);
}
@@ -217,13 +227,24 @@ void perf_stat__reset_shadow_stats(void)
}
 }
 
+static void update_runtime_stat(struct runtime_stat *stat,
+   enum stat_type type,
+   int ctx, int cpu, u64 count)
+{
+   struct saved_value *v = saved_value_lookup(NULL, cpu, true,
+  type, ctx, stat);
+
+   if (v)
+   update_stats(>stats, count);
+}
+
 /*
  * Update various tracking values we maintain to print
  * more semantic information such as miss/hit ratios,
  * instruction rates, etc:
  */
 void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
-   int cpu)
+   int cpu, struct runtime_stat *stat)
 {
int ctx = evsel_context(counter);
 
@@ -231,50 +252,58 @@ void perf_stat__update_shadow_stats(struct perf_evsel 
*counter, u64 count,
 
if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
-   update_stats(_nsecs_stats[cpu], count);
+   update_runtime_stat(stat, STAT_NSECS, 0, cpu, count);
else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
-   update_stats(_cycles_stats[ctx][cpu], count);
+   update_runtime_stat(stat,