perf tool_pmu: Use old_count when computing count values for time events
Ian Rogers
reviewerPATCH 1/3 perf tool_pmu: Use old_count when computing count values for time events
When running in interval mode every third count of a time event isn't showing properly:
```
$ perf stat -e duration_time -a -I 1000
1.001082862 1,002,290,425 duration_time
2.004264262 1,003,183,516 duration_time
3.007381401 <not counted> duration_time
4.011160141 1,003,705,631 duration_time
5.014515385 1,003,290,110 duration_time
6.018539680 <not counted> duration_time
7.022065321 1,003,591,720 duration_time
```The regression came in with a different fix, found through bisection, commit 68cb1567439f ("perf tool_pmu: Fix aggregation on duration_time"). The issue is caused by the enabled and running time of the event matching the old_count's and creating a delta of 0, which is indicative of an error.
--- tools/perf/builtin-stat.c | 16 +++++++++-- tools/perf/util/tool_pmu.c | 59 +++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 29 deletions(-)
tools/perf/builtin-stat.c +13 -3
@@ -282,17 +282,27 @@ static int read_single_counter(struct evsel *counter, int cpu_map_idx, int threaif (err && cpu_map_idx ==0 && (evsel__tool_event (counter) ==TOOL_PMU__EVENT_USER_TIME ||evsel__tool_event (counter) ==TOOL_PMU__EVENT_SYSTEM_TIME )) { -u64 val, *start_time;struct perf_counts_values *count =perf_counts (counter->counts, cpu_map_idx, thread); +struct perf_counts_values *old_count =NULL ; +u64 val; + +if (counter->prev_raw_counts) + old_count =perf_counts (counter->prev_raw_counts, cpu_map_idx, thread); - start_time =xyarray__entry (counter->start_times, cpu_map_idx, thread);if (evsel__tool_event (counter) ==TOOL_PMU__EVENT_USER_TIME ) val = ru_stats.ru_utime_usec_stat.mean;else val = ru_stats.ru_stime_usec_stat.mean; - count->ena = count->run = *start_time + val; + count->val = val; +if (old_count) { + count->run = old_count->run +1 ; + count->ena = old_count->ena +1 ; + }else { + count->run++; + count->ena++; + }return 0 ; }return err;
tools/perf/util/tool_pmu.c +33 -26
101 lines not on this page · raw message
Signature · 1 line
2.51.2.1006.ga50a493c49-goog
Ian Rogers
reviewerPATCH 2/3 perf stat-shadow: Read tool events directly
When reading time values for metrics don't use the globals updated in builtin-stat, just read the events as regular events. The only exception is for time events where nanoseconds need converting to seconds as metrics assume time metrics are in seconds.
--- tools/perf/util/stat-shadow.c | 123 +++++++++++++--------------------- 1 file changed, 45 insertions(+), 78 deletions(-)
tools/perf/util/stat-shadow.c +45 -78
@@ -371,6 +371,18 @@ static void print_nsecs(struct perf_stat_config *config, } } +static double tool_pmu__scale_for_metric (const struct evsel *evsel) +{ +enum tool_pmu_event event =evsel__tool_event (evsel); + +if (event ==TOOL_PMU__EVENT_DURATION_TIME || + event ==TOOL_PMU__EVENT_USER_TIME || + event ==TOOL_PMU__EVENT_SYSTEM_TIME ) +return 1e-9 ;/* Scale nanoseconds to seconds. */ + +return 1.0 ; +} + static int prepare_metric(const struct metric_expr *mexp, const struct evsel *evsel, struct expr_parse_ctx *pctx,
@@ -382,90 +394,45 @@ static int prepare_metric(const struct metric_expr *mexp,for (i =0 ; metric_events[i]; i++) {char *n; -double val; +double val, scale =tool_pmu__scale_for_metric (metric_events[i]);int source_count =0 ; +struct perf_stat_evsel *ps = metric_events[i]->stats; +struct perf_stat_aggr *aggr; -if (evsel__is_tool (metric_events[i])) { -struct stats *stats; -double scale; - -switch (evsel__tool_event (metric_events[i])) { -case TOOL_PMU__EVENT_DURATION_TIME : - stats = &walltime_nsecs_stats; - scale =1e-9 ; -break ; -case TOOL_PMU__EVENT_USER_TIME : - stats = &ru_stats.ru_utime_usec_stat; - scale =1e-6 ; -break ; -case TOOL_PMU__EVENT_SYSTEM_TIME : - stats = &ru_stats.ru_stime_usec_stat; - scale =1e-6 ; +/* +* If there are multiple uncore PMUs and we're not reading the +* leader's stats, determine the stats for the appropriate +* uncore PMU. +*/ +if (evsel && evsel->metric_leader && + evsel->pmu != evsel->metric_leader->pmu && + mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) { +struct evsel *pos; + +evlist__for_each_entry (evsel->evlist, pos) { +if (pos->pmu != evsel->pmu) +continue ; +if (pos->metric_leader != mexp->metric_events[i]) +continue ; + ps = pos->stats; + source_count =1 ;break ; -case TOOL_PMU__EVENT_NONE : -pr_err ("Invalid tool event 'none'" ); -abort (); -case TOOL_PMU__EVENT_MAX : -pr_err ("Invalid tool event 'max'" ); -abort (); -case TOOL_PMU__EVENT_HAS_PMEM : -case TOOL_PMU__EVENT_NUM_CORES : -case TOOL_PMU__EVENT_NUM_CPUS : -case TOOL_PMU__EVENT_NUM_CPUS_ONLINE : -case TOOL_PMU__EVENT_NUM_DIES : -case TOOL_PMU__EVENT_NUM_PACKAGES : -case TOOL_PMU__EVENT_SLOTS : -case TOOL_PMU__EVENT_SMT_ON : -case TOOL_PMU__EVENT_SYSTEM_TSC_FREQ : -default : -pr_err ("Unexpected tool event '%s'" ,evsel__name (metric_events[i])); -abort (); } - val =avg_stats (stats) * scale; - source_count =1 ; - }else { -struct perf_stat_evsel *ps = metric_events[i]->stats; -struct perf_stat_aggr *aggr; - + } + aggr = &ps->aggr[aggr_idx]; +if (!aggr || !metric_events[i]->supported) {/* -* If there are multiple uncore PMUs and we're not -* reading the leader's stats, determine the stats for -* the appropriate uncore PMU. +* Not supported events will have a count of 0, which +* can be confusing in a metric. Explicitly set the +* value to NAN. Not counted events (enable time of 0) +* are read as 0. */ -if (evsel && evsel->metric_leader && - evsel->pmu != evsel->metric_leader->pmu && - mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) { -struct evsel *pos; - -evlist__for_each_entry (evsel->evlist, pos) { -if (pos->pmu != evsel->pmu) -continue ; -if (pos->metric_leader != mexp->metric_events[i]) -continue ; - ps = pos->stats; - source_count =1 ; -break ; - } - } - aggr = &ps->aggr[aggr_idx]; -if (!aggr) -break ; - -if (!metric_events[i]->supported) { -/* -* Not supported events will have a count of 0, -* which can be confusing in a -* metric. Explicitly set the value to NAN. Not -* counted events (enable time of 0) are read as -* 0. -*/ - val =NAN ; - source_count =0 ; - }else { - val = aggr->counts.val; -if (!source_count) - source_count =evsel__source_count (metric_events[i]); - } + val =NAN ; + source_count =0 ; + }else { + val = aggr->counts.val * scale; +if (!source_count) + source_count =evsel__source_count (metric_events[i]); } n =strdup (evsel__metric_id (metric_events[i]));if (!n)
Signature · 1 line
2.51.2.1006.ga50a493c49-goog
Namhyung Kim
maintainerRe: [PATCH v1 2/3] perf stat-shadow: Read tool events directly
Hi Ian,
Quoted 49 lines · Ian Rogers
When reading time values for metrics don't use the globals updated in builtin-stat, just read the events as regular events. The only exception is for time events where nanoseconds need converting to seconds as metrics assume time metrics are in seconds.
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/stat-shadow.c | 123 +++++++++++++--------------------- 1 file changed, 45 insertions(+), 78 deletions(-)tools/perf/util/stat-shadow.c +15
-7@@ -371,6 +371,18 @@ static void print_nsecs(struct perf_stat_config *config, } } +static double tool_pmu__scale_for_metric (const struct evsel *evsel) +{ +enum tool_pmu_event event =evsel__tool_event (evsel); + +if (event ==TOOL_PMU__EVENT_DURATION_TIME || + event ==TOOL_PMU__EVENT_USER_TIME || + event ==TOOL_PMU__EVENT_SYSTEM_TIME ) +return 1e-9 ;/* Scale nanoseconds to seconds. */ + +return 1.0 ; +} + static int prepare_metric(const struct metric_expr *mexp, const struct evsel *evsel, struct expr_parse_ctx *pctx,@@ -382,90 +394,45 @@ static int prepare_metric(const struct metric_expr *mexp,for (i =0 ; metric_events[i]; i++) {char *n; -double val; +double val, scale =tool_pmu__scale_for_metric (metric_events[i]);int source_count =0 ; +struct perf_stat_evsel *ps = metric_events[i]->stats; +struct perf_stat_aggr *aggr; -if (evsel__is_tool (metric_events[i])) { -struct stats *stats; -double scale; - -switch (evsel__tool_event (metric_events[i])) { -case TOOL_PMU__EVENT_DURATION_TIME :
tools/perf/util/stat-shadow.c static int prepare_metric(const struct metric_expr *mexp, · line 403
Quoted 10 lines
tools/perf/util/stat-shadow.c +0
-10- stats = &walltime_nsecs_stats; - scale =1e-9 ; -break ; -case TOOL_PMU__EVENT_USER_TIME : - stats = &ru_stats.ru_utime_usec_stat; - scale =1e-6 ; -break ; -case TOOL_PMU__EVENT_SYSTEM_TIME : - stats = &ru_stats.ru_stime_usec_stat; - scale =1e-6 ;
Do {USER,SYSTEM}_TIME become nanosecond now?
Thanks,
Namhyung
Quoted 101 lines
tools/perf/util/stat-shadow.c +30
-61+/* +* If there are multiple uncore PMUs and we're not reading the +* leader's stats, determine the stats for the appropriate +* uncore PMU. +*/ +if (evsel && evsel->metric_leader && + evsel->pmu != evsel->metric_leader->pmu && + mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) { +struct evsel *pos; + +evlist__for_each_entry (evsel->evlist, pos) { +if (pos->pmu != evsel->pmu) +continue ; +if (pos->metric_leader != mexp->metric_events[i]) +continue ; + ps = pos->stats; + source_count =1 ;break ; -case TOOL_PMU__EVENT_NONE : -pr_err ("Invalid tool event 'none'" ); -abort (); -case TOOL_PMU__EVENT_MAX : -pr_err ("Invalid tool event 'max'" ); -abort (); -case TOOL_PMU__EVENT_HAS_PMEM : -case TOOL_PMU__EVENT_NUM_CORES : -case TOOL_PMU__EVENT_NUM_CPUS : -case TOOL_PMU__EVENT_NUM_CPUS_ONLINE : -case TOOL_PMU__EVENT_NUM_DIES : -case TOOL_PMU__EVENT_NUM_PACKAGES : -case TOOL_PMU__EVENT_SLOTS : -case TOOL_PMU__EVENT_SMT_ON : -case TOOL_PMU__EVENT_SYSTEM_TSC_FREQ : -default : -pr_err ("Unexpected tool event '%s'" ,evsel__name (metric_events[i])); -abort (); } - val =avg_stats (stats) * scale; - source_count =1 ; - }else { -struct perf_stat_evsel *ps = metric_events[i]->stats; -struct perf_stat_aggr *aggr; - + } + aggr = &ps->aggr[aggr_idx]; +if (!aggr || !metric_events[i]->supported) {/* -* If there are multiple uncore PMUs and we're not -* reading the leader's stats, determine the stats for -* the appropriate uncore PMU. +* Not supported events will have a count of 0, which +* can be confusing in a metric. Explicitly set the +* value to NAN. Not counted events (enable time of 0) +* are read as 0. */ -if (evsel && evsel->metric_leader && - evsel->pmu != evsel->metric_leader->pmu && - mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) { -struct evsel *pos; - -evlist__for_each_entry (evsel->evlist, pos) { -if (pos->pmu != evsel->pmu) -continue ; -if (pos->metric_leader != mexp->metric_events[i]) -continue ; - ps = pos->stats; - source_count =1 ; -break ; - } - } - aggr = &ps->aggr[aggr_idx]; -if (!aggr) -break ; - -if (!metric_events[i]->supported) { -/* -* Not supported events will have a count of 0, -* which can be confusing in a -* metric. Explicitly set the value to NAN. Not -* counted events (enable time of 0) are read as -* 0. -*/ - val =NAN ; - source_count =0 ; - }else { - val = aggr->counts.val; -if (!source_count) - source_count =evsel__source_count (metric_events[i]); - } + val =NAN ; + source_count =0 ; + }else { + val = aggr->counts.val * scale; +if (!source_count) + source_count =evsel__source_count (metric_events[i]); } n =strdup (evsel__metric_id (metric_events[i]));if (!n)Signature · 1 line
2.51.2.1006.ga50a493c49-goog
Ian Rogers
reviewerRe: [PATCH v1 2/3] perf stat-shadow: Read tool events directly
Quoted 57 lines · Namhyung Kim
Hi Ian,
Quoted 53 lines · Ian Rogers
When reading time values for metrics don't use the globals updated in builtin-stat, just read the events as regular events. The only exception is for time events where nanoseconds need converting to seconds as metrics assume time metrics are in seconds.
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/stat-shadow.c | 123 +++++++++++++--------------------- 1 file changed, 45 insertions(+), 78 deletions(-)tools/perf/util/stat-shadow.c +15
-11@@ -371,6 +371,18 @@ static void print_nsecs(struct perf_stat_config *config, } } +static double tool_pmu__scale_for_metric (const struct evsel *evsel) +{ +enum tool_pmu_event event =evsel__tool_event (evsel); + +if (event ==TOOL_PMU__EVENT_DURATION_TIME || + event ==TOOL_PMU__EVENT_USER_TIME || + event ==TOOL_PMU__EVENT_SYSTEM_TIME ) +return 1e-9 ;/* Scale nanoseconds to seconds. */ + +return 1.0 ; +} + static int prepare_metric(const struct metric_expr *mexp, const struct evsel *evsel, struct expr_parse_ctx *pctx,@@ -382,90 +394,45 @@ static int prepare_metric(const struct metric_expr *mexp,for (i =0 ; metric_events[i]; i++) {char *n; -double val; +double val, scale =tool_pmu__scale_for_metric (metric_events[i]);int source_count =0 ; +struct perf_stat_evsel *ps = metric_events[i]->stats; +struct perf_stat_aggr *aggr; -if (evsel__is_tool (metric_events[i])) { -struct stats *stats; -double scale; - -switch (evsel__tool_event (metric_events[i])) { -case TOOL_PMU__EVENT_DURATION_TIME : - stats = &walltime_nsecs_stats; - scale =1e-9 ; -break ; -case TOOL_PMU__EVENT_USER_TIME :
Quoted 8 lines
tools/perf/util/stat-shadow.c static int prepare_metric(const struct metric_expr *mexp, · line 403
Quoted 6 lines
tools/perf/util/stat-shadow.c +0
-6- stats = &ru_stats.ru_utime_usec_stat; - scale =1e-6 ; -break ; -case TOOL_PMU__EVENT_SYSTEM_TIME : - stats = &ru_stats.ru_stime_usec_stat; - scale =1e-6 ;Do {USER,SYSTEM}_TIME become nanosecond now?
The behavior is the same. In the event system/user time are read as microseconds while duration time is nanoseconds, the system/user times are scaled to be nanoseconds for some kind of consistency. In the metrics we were doing a similar scaling but scaling nanoseconds and microseconds to be seconds. Using the events directly we know the counts are always in nanoseconds so we just need to do 1 scaling factor.
Thanks,
Ian
Quoted 101 lines
Quoted 101 lines
tools/perf/util/stat-shadow.c +30
-62+/* +* If there are multiple uncore PMUs and we're not reading the +* leader's stats, determine the stats for the appropriate +* uncore PMU. +*/ +if (evsel && evsel->metric_leader && + evsel->pmu != evsel->metric_leader->pmu && + mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) { +struct evsel *pos; + +evlist__for_each_entry (evsel->evlist, pos) { +if (pos->pmu != evsel->pmu) +continue ; +if (pos->metric_leader != mexp->metric_events[i]) +continue ; + ps = pos->stats; + source_count =1 ;break ; -case TOOL_PMU__EVENT_NONE : -pr_err ("Invalid tool event 'none'" ); -abort (); -case TOOL_PMU__EVENT_MAX : -pr_err ("Invalid tool event 'max'" ); -abort (); -case TOOL_PMU__EVENT_HAS_PMEM : -case TOOL_PMU__EVENT_NUM_CORES : -case TOOL_PMU__EVENT_NUM_CPUS : -case TOOL_PMU__EVENT_NUM_CPUS_ONLINE : -case TOOL_PMU__EVENT_NUM_DIES : -case TOOL_PMU__EVENT_NUM_PACKAGES : -case TOOL_PMU__EVENT_SLOTS : -case TOOL_PMU__EVENT_SMT_ON : -case TOOL_PMU__EVENT_SYSTEM_TSC_FREQ : -default : -pr_err ("Unexpected tool event '%s'" ,evsel__name (metric_events[i])); -abort (); } - val =avg_stats (stats) * scale; - source_count =1 ; - }else { -struct perf_stat_evsel *ps = metric_events[i]->stats; -struct perf_stat_aggr *aggr; - + } + aggr = &ps->aggr[aggr_idx]; +if (!aggr || !metric_events[i]->supported) {/* -* If there are multiple uncore PMUs and we're not -* reading the leader's stats, determine the stats for -* the appropriate uncore PMU. +* Not supported events will have a count of 0, which +* can be confusing in a metric. Explicitly set the +* value to NAN. Not counted events (enable time of 0) +* are read as 0. */ -if (evsel && evsel->metric_leader && - evsel->pmu != evsel->metric_leader->pmu && - mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) { -struct evsel *pos; - -evlist__for_each_entry (evsel->evlist, pos) { -if (pos->pmu != evsel->pmu) -continue ; -if (pos->metric_leader != mexp->metric_events[i]) -continue ; - ps = pos->stats; - source_count =1 ; -break ; - } - } - aggr = &ps->aggr[aggr_idx]; -if (!aggr) -break ; - -if (!metric_events[i]->supported) { -/* -* Not supported events will have a count of 0, -* which can be confusing in a -* metric. Explicitly set the value to NAN. Not -* counted events (enable time of 0) are read as -* 0. -*/ - val =NAN ; - source_count =0 ; - }else { - val = aggr->counts.val; -if (!source_count) - source_count =evsel__source_count (metric_events[i]); - } + val =NAN ; + source_count =0 ; + }else { + val = aggr->counts.val * scale; +if (!source_count) + source_count =evsel__source_count (metric_events[i]); } n =strdup (evsel__metric_id (metric_events[i]));if (!n) --2.51.2.1006.ga50a493c49-goog
Ian Rogers
reviewerPATCH 3/3 perf stat: Reduce scope of ru_stats
In reply to Ian RogersThe ru_stats are used to capture user and system time stats when a process exits. These are then applied to user and system time tool events if their reads fail due to the process terminating. Reduce the scope now the metric code no longer reads these values.
--- tools/perf/builtin-stat.c | 14 +++++++++++++- tools/perf/util/config.c | 1 - tools/perf/util/stat-shadow.c | 2 -- tools/perf/util/stat.h | 16 ---------------- 4 files changed, 13 insertions(+), 20 deletions(-)
tools/perf/builtin-stat.c +13 -1
@@ -132,6 +132,7 @@ static bool interval_count;static const char *output_name;static int output_fd;static char *metrics; +static struct rusage_stats ru_stats;struct perf_stat {bool record;
@@ -729,6 +730,17 @@ static int create_perf_stat_counter(struct evsel *evsel, evsel->core.threads); } +static void update_rusage_stats (const struct rusage *rusage) +{ +const u64 us_to_ns =1000 ; +const u64 s_to_ns =1000000000 ; + +update_stats (&ru_stats.ru_utime_usec_stat, + (rusage->ru_utime.tv_usec * us_to_ns + rusage->ru_utime.tv_sec * s_to_ns)); +update_stats (&ru_stats.ru_stime_usec_stat, + (rusage->ru_stime.tv_usec * us_to_ns + rusage->ru_stime.tv_sec * s_to_ns)); +} +static int __run_perf_stat (int argc,const char **argv,int run_idx) {int interval = stat_config.interval;
@@ -978,7 +990,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)evlist__reset_aggr_stats (evsel_list); }else {update_stats (&walltime_nsecs_stats, t1 - t0); -update_rusage_stats (&ru_stats, &stat_config.ru_data); +update_rusage_stats (&stat_config.ru_data); }/*
tools/perf/util/config.c +0 -1
@@ -45,7 +45,6 @@ struct perf_stat_config stat_config = { .run_count = 1, .metric_only_len = METRIC_ONLY_LEN, .walltime_nsecs_stats = &walltime_nsecs_stats, - .ru_stats = &ru_stats, .big_num = true, .ctl_fd = -1, .ctl_fd_ack = -1,
tools/perf/util/stat-shadow.c +0 -2
@@ -18,7 +18,6 @@#include "tool_pmu.h" struct stats walltime_nsecs_stats; -struct rusage_stats ru_stats;enum {CTX_BIT_USER =1 <<0 ,
@@ -74,7 +73,6 @@ static int evsel_context(const struct evsel *evsel)void perf_stat__reset_shadow_stats (void ) {memset (&walltime_nsecs_stats,0 ,sizeof (walltime_nsecs_stats)); -memset (&ru_stats,0 ,sizeof (ru_stats)); }static enum stat_type evsel__stat_type (struct evsel *evsel)
tools/perf/util/stat.h +0 -16
@@ -102,7 +102,6 @@ struct perf_stat_config {const char *csv_sep;struct stats *walltime_nsecs_stats;struct rusage ru_data; -struct rusage_stats *ru_stats;struct cpu_aggr_map *aggr_map;aggr_get_id_t aggr_get_id;struct cpu_aggr_map *cpus_aggr_map;
@@ -132,25 +131,10 @@ static inline void init_stats(struct stats *stats) stats->max =0 ; } -static inline void init_rusage_stats (struct rusage_stats *ru_stats) { -init_stats (&ru_stats->ru_utime_usec_stat); -init_stats (&ru_stats->ru_stime_usec_stat); -} - -static inline void update_rusage_stats (struct rusage_stats *ru_stats,struct rusage * rusage) { -const u64 us_to_ns =1000 ; -const u64 s_to_ns =1000000000 ; -update_stats (&ru_stats->ru_utime_usec_stat, - (rusage->ru_utime.tv_usec * us_to_ns + rusage->ru_utime.tv_sec * s_to_ns)); -update_stats (&ru_stats->ru_stime_usec_stat, - (rusage->ru_stime.tv_usec * us_to_ns + rusage->ru_stime.tv_sec * s_to_ns)); -} -struct evsel ;struct evlist ;extern struct stats walltime_nsecs_stats; -extern struct rusage_stats ru_stats;enum metric_threshold_classify {METRIC_THRESHOLD_UNKNOWN ,
Signature · 1 line
2.51.2.1006.ga50a493c49-goog