#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"
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) {
- // Create a workload runs longer than profiling time.
- auto workload = Workload::CreateWorkload({"sleep", "1000"});
+ auto workload = Workload::CreateWorkload(function);
ASSERT_TRUE(workload != nullptr);
ASSERT_TRUE(workload->Start());
workloads->push_back(std::move(workload));
TEST(stat_cmd, group_option) {
ASSERT_TRUE(
- StatCmd()->Run({"--group", "cpu-cycles,cpu-clock", "sleep", "1"}));
- ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-cycles,cpu-clock", "--group",
- "cpu-cycles:u,cpu-clock:u", "--group",
- "cpu-cycles:k,cpu-clock:k", "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"}));
}
TEST(stat_cmd, auto_generated_summary) {
TemporaryFile tmp_file;
- ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-clock:u,cpu-clock:k", "-o",
+ ASSERT_TRUE(StatCmd()->Run({"--group", "instructions:u,instructions:k", "-o",
tmp_file.path, "sleep", "1"}));
std::string s;
ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
- size_t pos = s.find("cpu-clock:u");
+ size_t pos = s.find("instructions:u");
ASSERT_NE(s.npos, pos);
- pos = s.find("cpu-clock:k", pos);
+ pos = s.find("instructions:k", pos);
ASSERT_NE(s.npos, pos);
- pos += strlen("cpu-clock:k");
- // Check if the summary of cpu-clock is generated.
- ASSERT_NE(s.npos, s.find("cpu-clock", pos));
+ pos += strlen("instructions:k");
+ // Check if the summary of instructions is generated.
+ ASSERT_NE(s.npos, s.find("instructions", pos));
}
TEST(stat_cmd, duration_option) {
StatCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid())}));
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"}) {
+ ASSERT_FALSE(StatCmd()->Run({"-e", e + ":" + m, "sleep", "0.1"}))
+ << "event " << e << ":" << m;
+ }
+ }
+}
+
+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)}));
+}