OSDN Git Service

Merge "kernel.config: add CONFIG_NFS* testing"
[android-x86/system-extras.git] / simpleperf / cmd_stat_test.cpp
index b1f016c..89e75f8 100644 (file)
 #include <android-base/stringprintf.h>
 #include <android-base/test_utils.h>
 
+#include <thread>
+
 #include "command.h"
+#include "environment.h"
 #include "get_test_data.h"
 #include "test_util.h"
 
@@ -55,8 +58,15 @@ TEST(stat_cmd, event_modifier) {
 void CreateProcesses(size_t count,
                      std::vector<std::unique_ptr<Workload>>* workloads) {
   workloads->clear();
+  // Create workloads run longer than profiling time.
+  auto function = []() {
+    while (true) {
+      for (volatile int i = 0; i < 10000; ++i);
+      usleep(1);
+    }
+  };
   for (size_t i = 0; i < count; ++i) {
-    auto workload = Workload::CreateWorkload({"sleep", "1"});
+    auto workload = Workload::CreateWorkload(function);
     ASSERT_TRUE(workload != nullptr);
     ASSERT_TRUE(workload->Start());
     workloads->push_back(std::move(workload));
@@ -68,7 +78,7 @@ TEST(stat_cmd, existing_processes) {
   CreateProcesses(2, &workloads);
   std::string pid_list = android::base::StringPrintf(
       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
-  ASSERT_TRUE(StatCmd()->Run({"-p", pid_list}));
+  ASSERT_TRUE(StatCmd()->Run({"-p", pid_list, "sleep", "1"}));
 }
 
 TEST(stat_cmd, existing_threads) {
@@ -77,14 +87,14 @@ TEST(stat_cmd, existing_threads) {
   // Process id can be used as thread id in linux.
   std::string tid_list = android::base::StringPrintf(
       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
-  ASSERT_TRUE(StatCmd()->Run({"-t", tid_list}));
+  ASSERT_TRUE(StatCmd()->Run({"-t", tid_list, "sleep", "1"}));
 }
 
 TEST(stat_cmd, no_monitored_threads) { ASSERT_FALSE(StatCmd()->Run({""})); }
 
 TEST(stat_cmd, group_option) {
   ASSERT_TRUE(
-      StatCmd()->Run({"--group", "cpu-cycles,cpu-clock", "sleep", "1"}));
+      StatCmd()->Run({"--group", "cpu-clock,page-faults", "sleep", "1"}));
   ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-cycles,instructions", "--group",
                               "cpu-cycles:u,instructions:u", "--group",
                               "cpu-cycles:k,instructions:k", "sleep", "1"}));
@@ -111,6 +121,23 @@ TEST(stat_cmd, duration_option) {
   ASSERT_TRUE(StatCmd()->Run({"--duration", "1", "sleep", "2"}));
 }
 
+TEST(stat_cmd, interval_option) {
+  TemporaryFile tmp_file;
+  ASSERT_TRUE(
+    StatCmd()->Run({"--interval", "500.0", "--duration", "1.2", "-o",
+          tmp_file.path, "sleep", "2"}));
+  std::string s;
+  ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
+  size_t count = 0;
+  size_t pos = 0;
+  std::string subs = "statistics:";
+  while((pos = s.find(subs, pos)) != s.npos) {
+    pos += subs.size();
+    ++count ;
+  }
+  ASSERT_EQ(count, 3UL);
+}
+
 TEST(stat_cmd, no_modifier_for_clock_events) {
   for (const std::string& e : {"cpu-clock", "task-clock"}) {
     for (const std::string& m : {"u", "k"}) {
@@ -119,3 +146,23 @@ TEST(stat_cmd, no_modifier_for_clock_events) {
     }
   }
 }
+
+TEST(stat_cmd, handle_SIGHUP) {
+  std::thread thread([]() {
+    sleep(1);
+    kill(getpid(), SIGHUP);
+  });
+  thread.detach();
+  ASSERT_TRUE(StatCmd()->Run({"sleep", "1000000"}));
+}
+
+TEST(stat_cmd, stop_when_no_more_targets) {
+  std::atomic<int> tid(0);
+  std::thread thread([&]() {
+    tid = gettid();
+    sleep(1);
+  });
+  thread.detach();
+  while (tid == 0);
+  ASSERT_TRUE(StatCmd()->Run({"-t", std::to_string(tid)}));
+}