cmd_stat_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <gtest/gtest.h>
  17. #include <android-base/file.h>
  18. #include <android-base/stringprintf.h>
  19. #include <android-base/strings.h>
  20. #include <thread>
  21. #include "command.h"
  22. #include "environment.h"
  23. #include "event_selection_set.h"
  24. #include "get_test_data.h"
  25. #include "test_util.h"
  26. static std::unique_ptr<Command> StatCmd() {
  27. return CreateCommandInstance("stat");
  28. }
  29. TEST(stat_cmd, no_options) { ASSERT_TRUE(StatCmd()->Run({"sleep", "1"})); }
  30. TEST(stat_cmd, event_option) {
  31. ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-clock,task-clock", "sleep", "1"}));
  32. }
  33. TEST(stat_cmd, system_wide_option) {
  34. TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"})));
  35. }
  36. TEST(stat_cmd, verbose_option) {
  37. ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
  38. }
  39. TEST(stat_cmd, tracepoint_event) {
  40. TEST_IN_ROOT(ASSERT_TRUE(
  41. StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"})));
  42. }
  43. TEST(stat_cmd, rN_event) {
  44. TEST_REQUIRE_HW_COUNTER();
  45. OMIT_TEST_ON_NON_NATIVE_ABIS();
  46. size_t event_number;
  47. if (GetBuildArch() == ARCH_ARM64 || GetBuildArch() == ARCH_ARM) {
  48. // As in D5.10.2 of the ARMv8 manual, ARM defines the event number space for PMU. part of the
  49. // space is for common event numbers (which will stay the same for all ARM chips), part of the
  50. // space is for implementation defined events. Here 0x08 is a common event for instructions.
  51. event_number = 0x08;
  52. } else if (GetBuildArch() == ARCH_X86_32 || GetBuildArch() == ARCH_X86_64) {
  53. // As in volume 3 chapter 19 of the Intel manual, 0x00c0 is the event number for instruction.
  54. event_number = 0x00c0;
  55. } else {
  56. GTEST_LOG_(INFO) << "Omit arch " << GetBuildArch();
  57. return;
  58. }
  59. std::string event_name = android::base::StringPrintf("r%zx", event_number);
  60. ASSERT_TRUE(StatCmd()->Run({"-e", event_name, "sleep", "1"}));
  61. }
  62. TEST(stat_cmd, event_modifier) {
  63. TEST_REQUIRE_HW_COUNTER();
  64. ASSERT_TRUE(
  65. StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
  66. }
  67. void RunWorkloadFunction() {
  68. while (true) {
  69. for (volatile int i = 0; i < 10000; ++i);
  70. usleep(1);
  71. }
  72. }
  73. void CreateProcesses(size_t count,
  74. std::vector<std::unique_ptr<Workload>>* workloads) {
  75. workloads->clear();
  76. // Create workloads run longer than profiling time.
  77. for (size_t i = 0; i < count; ++i) {
  78. std::unique_ptr<Workload> workload;
  79. workload = Workload::CreateWorkload(RunWorkloadFunction);
  80. ASSERT_TRUE(workload != nullptr);
  81. ASSERT_TRUE(workload->Start());
  82. workloads->push_back(std::move(workload));
  83. }
  84. }
  85. TEST(stat_cmd, existing_processes) {
  86. std::vector<std::unique_ptr<Workload>> workloads;
  87. CreateProcesses(2, &workloads);
  88. std::string pid_list = android::base::StringPrintf(
  89. "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
  90. ASSERT_TRUE(StatCmd()->Run({"-p", pid_list, "sleep", "1"}));
  91. }
  92. TEST(stat_cmd, existing_threads) {
  93. std::vector<std::unique_ptr<Workload>> workloads;
  94. CreateProcesses(2, &workloads);
  95. // Process id can be used as thread id in linux.
  96. std::string tid_list = android::base::StringPrintf(
  97. "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
  98. ASSERT_TRUE(StatCmd()->Run({"-t", tid_list, "sleep", "1"}));
  99. }
  100. TEST(stat_cmd, no_monitored_threads) {
  101. ASSERT_FALSE(StatCmd()->Run({}));
  102. ASSERT_FALSE(StatCmd()->Run({""}));
  103. }
  104. TEST(stat_cmd, group_option) {
  105. TEST_REQUIRE_HW_COUNTER();
  106. ASSERT_TRUE(
  107. StatCmd()->Run({"--group", "cpu-clock,page-faults", "sleep", "1"}));
  108. ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-cycles,instructions", "--group",
  109. "cpu-cycles:u,instructions:u", "--group",
  110. "cpu-cycles:k,instructions:k", "sleep", "1"}));
  111. }
  112. TEST(stat_cmd, auto_generated_summary) {
  113. TEST_REQUIRE_HW_COUNTER();
  114. TemporaryFile tmp_file;
  115. ASSERT_TRUE(StatCmd()->Run({"--group", "instructions:u,instructions:k", "-o",
  116. tmp_file.path, "sleep", "1"}));
  117. std::string s;
  118. ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
  119. size_t pos = s.find("instructions:u");
  120. ASSERT_NE(s.npos, pos);
  121. pos = s.find("instructions:k", pos);
  122. ASSERT_NE(s.npos, pos);
  123. pos += strlen("instructions:k");
  124. // Check if the summary of instructions is generated.
  125. ASSERT_NE(s.npos, s.find("instructions", pos));
  126. }
  127. TEST(stat_cmd, duration_option) {
  128. ASSERT_TRUE(
  129. StatCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid()), "--in-app"}));
  130. ASSERT_TRUE(StatCmd()->Run({"--duration", "1", "sleep", "2"}));
  131. }
  132. TEST(stat_cmd, interval_option) {
  133. TemporaryFile tmp_file;
  134. ASSERT_TRUE(
  135. StatCmd()->Run({"--interval", "500.0", "--duration", "1.2", "-o",
  136. tmp_file.path, "sleep", "2"}));
  137. std::string s;
  138. ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
  139. size_t count = 0;
  140. size_t pos = 0;
  141. std::string subs = "statistics:";
  142. while((pos = s.find(subs, pos)) != s.npos) {
  143. pos += subs.size();
  144. ++count ;
  145. }
  146. ASSERT_EQ(count, 2UL);
  147. }
  148. TEST(stat_cmd, interval_option_in_system_wide) {
  149. TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "--interval", "100", "--duration", "0.3"})));
  150. }
  151. TEST(stat_cmd, interval_only_values_option) {
  152. ASSERT_TRUE(StatCmd()->Run({"--interval", "500", "--interval-only-values", "sleep", "2"}));
  153. TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "--interval", "100", "--interval-only-values",
  154. "--duration", "0.3"})));
  155. }
  156. TEST(stat_cmd, no_modifier_for_clock_events) {
  157. for (const std::string& e : {"cpu-clock", "task-clock"}) {
  158. for (const std::string& m : {"u", "k"}) {
  159. ASSERT_FALSE(StatCmd()->Run({"-e", e + ":" + m, "sleep", "0.1"}))
  160. << "event " << e << ":" << m;
  161. }
  162. }
  163. }
  164. TEST(stat_cmd, handle_SIGHUP) {
  165. std::thread thread([]() {
  166. sleep(1);
  167. kill(getpid(), SIGHUP);
  168. });
  169. thread.detach();
  170. ASSERT_TRUE(StatCmd()->Run({"sleep", "1000000"}));
  171. }
  172. TEST(stat_cmd, stop_when_no_more_targets) {
  173. std::atomic<int> tid(0);
  174. std::thread thread([&]() {
  175. tid = gettid();
  176. sleep(1);
  177. });
  178. thread.detach();
  179. while (tid == 0);
  180. ASSERT_TRUE(StatCmd()->Run({"-t", std::to_string(tid), "--in-app"}));
  181. }
  182. TEST(stat_cmd, sample_speed_should_be_zero) {
  183. TEST_REQUIRE_HW_COUNTER();
  184. EventSelectionSet set(true);
  185. ASSERT_TRUE(set.AddEventType("cpu-cycles"));
  186. set.AddMonitoredProcesses({getpid()});
  187. ASSERT_TRUE(set.OpenEventFiles({-1}));
  188. std::vector<EventAttrWithId> attrs = set.GetEventAttrWithId();
  189. ASSERT_GT(attrs.size(), 0u);
  190. for (auto& attr : attrs) {
  191. ASSERT_EQ(attr.attr->sample_period, 0u);
  192. ASSERT_EQ(attr.attr->sample_freq, 0u);
  193. ASSERT_EQ(attr.attr->freq, 0u);
  194. }
  195. }
  196. TEST(stat_cmd, calculating_cpu_frequency) {
  197. TEST_REQUIRE_HW_COUNTER();
  198. CaptureStdout capture;
  199. ASSERT_TRUE(capture.Start());
  200. ASSERT_TRUE(StatCmd()->Run({"--csv", "--group", "task-clock,cpu-cycles", "sleep", "1"}));
  201. std::string output = capture.Finish();
  202. double task_clock_in_ms = 0;
  203. uint64_t cpu_cycle_count = 0;
  204. double cpu_frequency = 0;
  205. for (auto& line : android::base::Split(output, "\n")) {
  206. if (line.find("task-clock") != std::string::npos) {
  207. ASSERT_EQ(sscanf(line.c_str(), "%lf(ms)", &task_clock_in_ms), 1);
  208. } else if (line.find("cpu-cycles") != std::string::npos) {
  209. ASSERT_EQ(sscanf(line.c_str(), "%" SCNu64 ",cpu-cycles,%lf", &cpu_cycle_count,
  210. &cpu_frequency), 2);
  211. }
  212. }
  213. ASSERT_NE(task_clock_in_ms, 0.0f);
  214. ASSERT_NE(cpu_cycle_count, 0u);
  215. ASSERT_NE(cpu_frequency, 0.0f);
  216. double calculated_frequency = cpu_cycle_count / task_clock_in_ms / 1e6;
  217. // Accept error up to 1e-3. Because the stat cmd print values with precision 1e-6.
  218. ASSERT_NEAR(cpu_frequency, calculated_frequency, 1e-3);
  219. }
  220. TEST(stat_cmd, set_comm_in_another_thread) {
  221. // Test a kernel bug which was fixed in 3.15. If kernel panic happens, please cherry pick kernel
  222. // patch: e041e328c4b41e perf: Fix perf_event_comm() vs. exec() assumption
  223. TEST_REQUIRE_HW_COUNTER();
  224. for (size_t loop = 0; loop < 3; ++loop) {
  225. std::atomic<int> child_tid(0);
  226. std::atomic<bool> stop_child(false);
  227. std::thread child([&]() {
  228. child_tid = gettid();
  229. // stay on a cpu to make the monitored events of the child thread on that cpu.
  230. while (!stop_child) {}
  231. });
  232. while (child_tid == 0) {}
  233. {
  234. EventSelectionSet set(true);
  235. ASSERT_TRUE(set.AddEventType("cpu-cycles"));
  236. set.AddMonitoredThreads({child_tid});
  237. ASSERT_TRUE(set.OpenEventFiles({-1}));
  238. EventSelectionSet set2(true);
  239. ASSERT_TRUE(set2.AddEventType("instructions"));
  240. set2.AddMonitoredThreads({gettid()});
  241. ASSERT_TRUE(set2.OpenEventFiles({-1}));
  242. // For kernels with the bug, setting comm will make the monitored events of the child thread
  243. // on the cpu of the current thread.
  244. ASSERT_TRUE(android::base::WriteStringToFile("child",
  245. "/proc/" + std::to_string(child_tid) + "/comm"));
  246. // Release monitored events. For kernels with the bug, the events still exist on the cpu of
  247. // the child thread.
  248. }
  249. stop_child = true;
  250. child.join();
  251. // Sleep 1s to enter and exit cpu idle, which may abort the kernel.
  252. sleep(1);
  253. }
  254. }
  255. static void TestStatingApps(const std::string& app_name) {
  256. // Bring the app to foreground.
  257. ASSERT_TRUE(Workload::RunCmd({"am", "start", app_name + "/.MainActivity"}));
  258. ASSERT_TRUE(StatCmd()->Run({"--app", app_name, "--duration", "3"}));
  259. }
  260. TEST(stat_cmd, app_option_for_debuggable_app) {
  261. TEST_REQUIRE_APPS();
  262. TestStatingApps("com.android.simpleperf.debuggable");
  263. }
  264. TEST(stat_cmd, app_option_for_profileable_app) {
  265. TEST_REQUIRE_APPS();
  266. TestStatingApps("com.android.simpleperf.profileable");
  267. }