simpleperf: fix --interval option in system wide stating.

Bug: none
Test: run simpleperf_unit_test.
Change-Id: Ida90ecd465e5c63f0f813cde63d70acfeb1281da
diff --git a/simpleperf/cmd_stat.cpp b/simpleperf/cmd_stat.cpp
index fce4fb1..34b4436 100644
--- a/simpleperf/cmd_stat.cpp
+++ b/simpleperf/cmd_stat.cpp
@@ -416,6 +416,12 @@
   }
 
   // 4. Add signal/periodic Events.
+  IOEventLoop* loop = event_selection_set_.GetIOEventLoop();
+  if (interval_in_ms_ != 0) {
+    if (!loop->UsePreciseTimer()) {
+      return false;
+    }
+  }
   std::chrono::time_point<std::chrono::steady_clock> start_time;
   std::vector<CountersInfo> counters;
   if (system_wide_collection_ || (!cpus_.empty() && cpus_[0] != -1)) {
@@ -426,12 +432,6 @@
   if (need_to_check_targets && !event_selection_set_.StopWhenNoMoreTargets()) {
     return false;
   }
-  IOEventLoop* loop = event_selection_set_.GetIOEventLoop();
-  if (interval_in_ms_ != 0) {
-    if (!loop->UsePreciseTimer()) {
-      return false;
-    }
-  }
   if (!loop->AddSignalEvents({SIGCHLD, SIGINT, SIGTERM, SIGHUP},
                              [&]() { return loop->ExitLoop(); })) {
     return false;
diff --git a/simpleperf/cmd_stat_test.cpp b/simpleperf/cmd_stat_test.cpp
index e8c722d..b6896e1 100644
--- a/simpleperf/cmd_stat_test.cpp
+++ b/simpleperf/cmd_stat_test.cpp
@@ -148,6 +148,10 @@
   ASSERT_EQ(count, 3UL);
 }
 
+TEST(stat_cmd, interval_option_in_system_wide) {
+  TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "--interval", "100", "--duration", "0.3"})));
+}
+
 TEST(stat_cmd, no_modifier_for_clock_events) {
   for (const std::string& e : {"cpu-clock", "task-clock"}) {
     for (const std::string& m : {"u", "k"}) {