@@ -166,6 +166,35 @@ static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
166166 evsel -> prev_raw_counts = NULL ;
167167}
168168
169+ static void perf_evlist__free_stats (struct perf_evlist * evlist )
170+ {
171+ struct perf_evsel * evsel ;
172+
173+ list_for_each_entry (evsel , & evlist -> entries , node ) {
174+ perf_evsel__free_stat_priv (evsel );
175+ perf_evsel__free_counts (evsel );
176+ perf_evsel__free_prev_raw_counts (evsel );
177+ }
178+ }
179+
180+ static int perf_evlist__alloc_stats (struct perf_evlist * evlist , bool alloc_raw )
181+ {
182+ struct perf_evsel * evsel ;
183+
184+ list_for_each_entry (evsel , & evlist -> entries , node ) {
185+ if (perf_evsel__alloc_stat_priv (evsel ) < 0 ||
186+ perf_evsel__alloc_counts (evsel , perf_evsel__nr_cpus (evsel )) < 0 ||
187+ (alloc_raw && perf_evsel__alloc_prev_raw_counts (evsel ) < 0 ))
188+ goto out_free ;
189+ }
190+
191+ return 0 ;
192+
193+ out_free :
194+ perf_evlist__free_stats (evlist );
195+ return -1 ;
196+ }
197+
169198static struct stats runtime_nsecs_stats [MAX_NR_CPUS ];
170199static struct stats runtime_cycles_stats [MAX_NR_CPUS ];
171200static struct stats runtime_stalled_cycles_front_stats [MAX_NR_CPUS ];
@@ -179,8 +208,15 @@ static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
179208static struct stats runtime_dtlb_cache_stats [MAX_NR_CPUS ];
180209static struct stats walltime_nsecs_stats ;
181210
182- static void reset_stats ( void )
211+ static void perf_stat__reset_stats ( struct perf_evlist * evlist )
183212{
213+ struct perf_evsel * evsel ;
214+
215+ list_for_each_entry (evsel , & evlist -> entries , node ) {
216+ perf_evsel__reset_stat_priv (evsel );
217+ perf_evsel__reset_counts (evsel , perf_evsel__nr_cpus (evsel ));
218+ }
219+
184220 memset (runtime_nsecs_stats , 0 , sizeof (runtime_nsecs_stats ));
185221 memset (runtime_cycles_stats , 0 , sizeof (runtime_cycles_stats ));
186222 memset (runtime_stalled_cycles_front_stats , 0 , sizeof (runtime_stalled_cycles_front_stats ));
@@ -1308,7 +1344,6 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
13081344 "perf stat [<options>] [<command>]" ,
13091345 NULL
13101346 };
1311- struct perf_evsel * pos ;
13121347 int status = - ENOMEM , run_idx ;
13131348 const char * mode ;
13141349
@@ -1420,17 +1455,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
14201455 return -1 ;
14211456 }
14221457
1423- list_for_each_entry (pos , & evsel_list -> entries , node ) {
1424- if (perf_evsel__alloc_stat_priv (pos ) < 0 ||
1425- perf_evsel__alloc_counts (pos , perf_evsel__nr_cpus (pos )) < 0 )
1426- goto out_free_fd ;
1427- }
1428- if (interval ) {
1429- list_for_each_entry (pos , & evsel_list -> entries , node ) {
1430- if (perf_evsel__alloc_prev_raw_counts (pos ) < 0 )
1431- goto out_free_fd ;
1432- }
1433- }
1458+ if (perf_evlist__alloc_stats (evsel_list , interval ))
1459+ goto out_free_maps ;
14341460
14351461 /*
14361462 * We dont want to block the signals - that would cause
@@ -1454,22 +1480,15 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
14541480 status = run_perf_stat (argc , argv );
14551481 if (forever && status != -1 ) {
14561482 print_stat (argc , argv );
1457- list_for_each_entry (pos , & evsel_list -> entries , node ) {
1458- perf_evsel__reset_stat_priv (pos );
1459- perf_evsel__reset_counts (pos , perf_evsel__nr_cpus (pos ));
1460- }
1461- reset_stats ();
1483+ perf_stat__reset_stats (evsel_list );
14621484 }
14631485 }
14641486
14651487 if (!forever && status != -1 && !interval )
14661488 print_stat (argc , argv );
1467- out_free_fd :
1468- list_for_each_entry (pos , & evsel_list -> entries , node ) {
1469- perf_evsel__free_stat_priv (pos );
1470- perf_evsel__free_counts (pos );
1471- perf_evsel__free_prev_raw_counts (pos );
1472- }
1489+
1490+ perf_evlist__free_stats (evsel_list );
1491+ out_free_maps :
14731492 perf_evlist__delete_maps (evsel_list );
14741493out :
14751494 perf_evlist__delete (evsel_list );
0 commit comments