@@ -110,7 +110,6 @@ static int big_num_opt = -1;
110110static const char * csv_sep = NULL ;
111111static bool csv_output = false;
112112static bool group = false;
113- static FILE * output = NULL ;
114113static const char * pre_cmd = NULL ;
115114static const char * post_cmd = NULL ;
116115static bool sync_run = false;
@@ -305,7 +304,7 @@ static int process_counter(struct perf_evsel *counter)
305304 update_stats (& ps -> res_stats [i ], count [i ]);
306305
307306 if (verbose ) {
308- fprintf (output , "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n" ,
307+ fprintf (stat_config . output , "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n" ,
309308 perf_evsel__name (counter ), count [0 ], count [1 ], count [2 ]);
310309 }
311310
@@ -548,13 +547,13 @@ static int run_perf_stat(int argc, const char **argv)
548547static void print_running (u64 run , u64 ena )
549548{
550549 if (csv_output ) {
551- fprintf (output , "%s%" PRIu64 "%s%.2f" ,
550+ fprintf (stat_config . output , "%s%" PRIu64 "%s%.2f" ,
552551 csv_sep ,
553552 run ,
554553 csv_sep ,
555554 ena ? 100.0 * run / ena : 100.0 );
556555 } else if (run != ena ) {
557- fprintf (output , " (%.2f%%)" , 100.0 * run / ena );
556+ fprintf (stat_config . output , " (%.2f%%)" , 100.0 * run / ena );
558557 }
559558}
560559
@@ -563,9 +562,9 @@ static void print_noise_pct(double total, double avg)
563562 double pct = rel_stddev_stats (total , avg );
564563
565564 if (csv_output )
566- fprintf (output , "%s%.2f%%" , csv_sep , pct );
565+ fprintf (stat_config . output , "%s%.2f%%" , csv_sep , pct );
567566 else if (pct )
568- fprintf (output , " ( +-%6.2f%% )" , pct );
567+ fprintf (stat_config . output , " ( +-%6.2f%% )" , pct );
569568}
570569
571570static void print_noise (struct perf_evsel * evsel , double avg )
@@ -583,7 +582,7 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
583582{
584583 switch (stat_config .aggr_mode ) {
585584 case AGGR_CORE :
586- fprintf (output , "S%d-C%*d%s%*d%s" ,
585+ fprintf (stat_config . output , "S%d-C%*d%s%*d%s" ,
587586 cpu_map__id_to_socket (id ),
588587 csv_output ? 0 : -8 ,
589588 cpu_map__id_to_cpu (id ),
@@ -593,7 +592,7 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
593592 csv_sep );
594593 break ;
595594 case AGGR_SOCKET :
596- fprintf (output , "S%*d%s%*d%s" ,
595+ fprintf (stat_config . output , "S%*d%s%*d%s" ,
597596 csv_output ? 0 : -5 ,
598597 id ,
599598 csv_sep ,
@@ -602,12 +601,12 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
602601 csv_sep );
603602 break ;
604603 case AGGR_NONE :
605- fprintf (output , "CPU%*d%s" ,
604+ fprintf (stat_config . output , "CPU%*d%s" ,
606605 csv_output ? 0 : -4 ,
607606 perf_evsel__cpus (evsel )-> map [id ], csv_sep );
608607 break ;
609608 case AGGR_THREAD :
610- fprintf (output , "%*s-%*d%s" ,
609+ fprintf (stat_config . output , "%*s-%*d%s" ,
611610 csv_output ? 0 : 16 ,
612611 thread_map__comm (evsel -> threads , id ),
613612 csv_output ? 0 : -8 ,
@@ -622,6 +621,7 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
622621
623622static void nsec_printout (int id , int nr , struct perf_evsel * evsel , double avg )
624623{
624+ FILE * output = stat_config .output ;
625625 double msecs = avg / 1e6 ;
626626 const char * fmt_v , * fmt_n ;
627627 char name [25 ];
@@ -658,6 +658,7 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
658658
659659static void abs_printout (int id , int nr , struct perf_evsel * evsel , double avg )
660660{
661+ FILE * output = stat_config .output ;
661662 double sc = evsel -> scale ;
662663 const char * fmt ;
663664 int cpu = cpu_map__id_to_cpu (id );
@@ -697,6 +698,7 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
697698
698699static void print_aggr (char * prefix )
699700{
701+ FILE * output = stat_config .output ;
700702 struct perf_evsel * counter ;
701703 int cpu , cpu2 , s , s2 , id , nr ;
702704 double uval ;
@@ -765,6 +767,7 @@ static void print_aggr(char *prefix)
765767
766768static void print_aggr_thread (struct perf_evsel * counter , char * prefix )
767769{
770+ FILE * output = stat_config .output ;
768771 int nthreads = thread_map__nr (counter -> threads );
769772 int ncpus = cpu_map__nr (counter -> cpus );
770773 int cpu , thread ;
@@ -803,6 +806,7 @@ static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
803806 */
804807static void print_counter_aggr (struct perf_evsel * counter , char * prefix )
805808{
809+ FILE * output = stat_config .output ;
806810 struct perf_stat * ps = counter -> priv ;
807811 double avg = avg_stats (& ps -> res_stats [0 ]);
808812 int scaled = counter -> counts -> scaled ;
@@ -854,6 +858,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
854858 */
855859static void print_counter (struct perf_evsel * counter , char * prefix )
856860{
861+ FILE * output = stat_config .output ;
857862 u64 ena , run , val ;
858863 double uval ;
859864 int cpu ;
@@ -908,6 +913,7 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
908913
909914static void print_interval (char * prefix , struct timespec * ts )
910915{
916+ FILE * output = stat_config .output ;
911917 static int num_print_interval ;
912918
913919 sprintf (prefix , "%6lu.%09lu%s" , ts -> tv_sec , ts -> tv_nsec , csv_sep );
@@ -938,6 +944,7 @@ static void print_interval(char *prefix, struct timespec *ts)
938944
939945static void print_header (int argc , const char * * argv )
940946{
947+ FILE * output = stat_config .output ;
941948 int i ;
942949
943950 fflush (stdout );
@@ -967,6 +974,8 @@ static void print_header(int argc, const char **argv)
967974
968975static void print_footer (void )
969976{
977+ FILE * output = stat_config .output ;
978+
970979 if (!null_run )
971980 fprintf (output , "\n" );
972981 fprintf (output , " %17.9f seconds time elapsed" ,
@@ -1013,7 +1022,7 @@ static void print_counters(struct timespec *ts, int argc, const char **argv)
10131022 if (!interval && !csv_output )
10141023 print_footer ();
10151024
1016- fflush (output );
1025+ fflush (stat_config . output );
10171026}
10181027
10191028static volatile int signr = -1 ;
@@ -1322,6 +1331,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
13221331 };
13231332 int status = - EINVAL , run_idx ;
13241333 const char * mode ;
1334+ FILE * output = stderr ;
13251335
13261336 setlocale (LC_ALL , "" );
13271337
@@ -1332,7 +1342,6 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
13321342 argc = parse_options (argc , argv , options , stat_usage ,
13331343 PARSE_OPT_STOP_AT_NON_OPTION );
13341344
1335- output = stderr ;
13361345 if (output_name && strcmp (output_name , "-" ))
13371346 output = NULL ;
13381347
@@ -1369,6 +1378,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
13691378 }
13701379 }
13711380
1381+ stat_config .output = output ;
1382+
13721383 if (csv_sep ) {
13731384 csv_output = true;
13741385 if (!strcmp (csv_sep , "\\t" ))
0 commit comments