2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
21 #if defined(__BIONIC__)
22 #include <android-base/properties.h>
28 #include <unordered_map>
30 #include <android-base/file.h>
31 #include <android-base/logging.h>
32 #include <android-base/stringprintf.h>
34 #include "environment.h"
35 #include "event_attr.h"
37 #include "event_type.h"
40 static auto test_duration_for_long_tests = std::chrono::seconds(120);
41 static auto cpu_hotplug_interval = std::chrono::microseconds(1000);
42 static bool verbose_mode = false;
44 #if defined(__BIONIC__)
45 class ScopedMpdecisionKiller {
47 ScopedMpdecisionKiller() {
48 have_mpdecision_ = IsMpdecisionRunning();
49 if (have_mpdecision_) {
54 ~ScopedMpdecisionKiller() {
55 if (have_mpdecision_) {
61 bool IsMpdecisionRunning() {
62 std::string value = android::base::GetProperty("init.svc.mpdecision", "");
63 if (value.empty() || value.find("stopped") != std::string::npos) {
69 void DisableMpdecision() {
70 CHECK(android::base::SetProperty("ctl.stop", "mpdecision"));
71 // Need to wait until mpdecision is actually stopped.
72 std::this_thread::sleep_for(std::chrono::milliseconds(500));
73 CHECK(!IsMpdecisionRunning());
76 void EnableMpdecision() {
77 CHECK(android::base::SetProperty("ctl.start", "mpdecision"));
78 std::this_thread::sleep_for(std::chrono::milliseconds(500));
79 CHECK(IsMpdecisionRunning());
82 bool have_mpdecision_;
85 class ScopedMpdecisionKiller {
87 ScopedMpdecisionKiller() {
92 static bool IsCpuOnline(int cpu, bool* has_error) {
93 std::string filename = android::base::StringPrintf("/sys/devices/system/cpu/cpu%d/online", cpu);
95 bool ret = android::base::ReadFileToString(filename, &content);
97 PLOG(ERROR) << "failed to read file " << filename;
102 return (content.find('1') != std::string::npos);
105 static bool SetCpuOnline(int cpu, bool online) {
107 bool ret = IsCpuOnline(cpu, &has_error);
114 std::string filename = android::base::StringPrintf("/sys/devices/system/cpu/cpu%d/online", cpu);
115 std::string content = online ? "1" : "0";
116 ret = android::base::WriteStringToFile(content, filename);
118 ret = IsCpuOnline(cpu, &has_error);
125 PLOG(ERROR) << "failed to write " << content << " to " << filename;
128 // Kernel needs time to offline/online cpus, so use a loop to wait here.
129 size_t retry_count = 0;
131 ret = IsCpuOnline(cpu, &has_error);
138 LOG(ERROR) << "reading cpu retry count = " << retry_count << ", requested = " << online
139 << ", real = " << ret;
140 if (++retry_count == 10000) {
141 LOG(ERROR) << "setting cpu " << cpu << (online ? " online" : " offline") << " seems not to take effect";
144 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 static int GetCpuCount() {
150 return static_cast<int>(sysconf(_SC_NPROCESSORS_CONF));
153 class CpuOnlineRestorer {
155 CpuOnlineRestorer() {
156 for (int cpu = 1; cpu < GetCpuCount(); ++cpu) {
158 bool ret = IsCpuOnline(cpu, &has_error);
162 online_map_[cpu] = ret;
166 ~CpuOnlineRestorer() {
167 for (const auto& pair : online_map_) {
168 SetCpuOnline(pair.first, pair.second);
173 std::unordered_map<int, bool> online_map_;
176 bool FindAHotpluggableCpu(int* hotpluggable_cpu) {
178 GTEST_LOG_(INFO) << "This test needs root privilege to hotplug cpu.";
181 for (int cpu = 1; cpu < GetCpuCount(); ++cpu) {
183 bool online = IsCpuOnline(cpu, &has_error);
187 if (SetCpuOnline(cpu, !online)) {
188 *hotpluggable_cpu = cpu;
192 GTEST_LOG_(INFO) << "There is no hotpluggable cpu.";
196 struct CpuToggleThreadArg {
198 std::atomic<bool> end_flag;
199 std::atomic<bool> cpu_hotplug_failed;
201 CpuToggleThreadArg(int cpu)
202 : toggle_cpu(cpu), end_flag(false), cpu_hotplug_failed(false) {
206 static void CpuToggleThread(CpuToggleThreadArg* arg) {
207 while (!arg->end_flag) {
208 if (!SetCpuOnline(arg->toggle_cpu, true)) {
209 arg->cpu_hotplug_failed = true;
212 std::this_thread::sleep_for(cpu_hotplug_interval);
213 if (!SetCpuOnline(arg->toggle_cpu, false)) {
214 arg->cpu_hotplug_failed = true;
217 std::this_thread::sleep_for(cpu_hotplug_interval);
221 // http://b/25193162.
222 TEST(cpu_offline, offline_while_recording) {
223 ScopedMpdecisionKiller scoped_mpdecision_killer;
224 CpuOnlineRestorer cpuonline_restorer;
225 if (GetCpuCount() == 1) {
226 GTEST_LOG_(INFO) << "This test does nothing, because there is only one cpu in the system.";
229 // Start cpu hotpluger.
231 if (!FindAHotpluggableCpu(&test_cpu)) {
234 CpuToggleThreadArg cpu_toggle_arg(test_cpu);
235 std::thread cpu_toggle_thread(CpuToggleThread, &cpu_toggle_arg);
237 std::unique_ptr<EventTypeAndModifier> event_type_modifier = ParseEventType("cpu-cycles");
238 ASSERT_TRUE(event_type_modifier != nullptr);
239 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type_modifier->event_type);
241 attr.enable_on_exec = 0;
243 auto start_time = std::chrono::steady_clock::now();
244 auto cur_time = start_time;
245 auto end_time = std::chrono::steady_clock::now() + test_duration_for_long_tests;
246 auto report_step = std::chrono::seconds(15);
247 size_t iterations = 0;
249 while (cur_time < end_time && !cpu_toggle_arg.cpu_hotplug_failed) {
250 if (cur_time + report_step < std::chrono::steady_clock::now()) {
252 auto diff = std::chrono::duration_cast<std::chrono::seconds>(
253 std::chrono::steady_clock::now() - start_time);
255 GTEST_LOG_(INFO) << "Have Tested " << (diff.count() / 60.0) << " minutes.";
257 cur_time = std::chrono::steady_clock::now();
260 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(attr, -1, test_cpu, nullptr, false);
261 if (event_fd == nullptr) {
262 // Failed to open because the test_cpu is offline.
267 GTEST_LOG_(INFO) << "Test offline while recording for " << iterations << " times.";
270 if (cpu_toggle_arg.cpu_hotplug_failed) {
271 GTEST_LOG_(INFO) << "Test ends because of cpu hotplug failure.";
273 cpu_toggle_arg.end_flag = true;
274 cpu_toggle_thread.join();
277 // http://b/25193162.
278 TEST(cpu_offline, offline_while_ioctl_enable) {
279 ScopedMpdecisionKiller scoped_mpdecision_killer;
280 CpuOnlineRestorer cpuonline_restorer;
281 if (GetCpuCount() == 1) {
282 GTEST_LOG_(INFO) << "This test does nothing, because there is only one cpu in the system.";
285 // Start cpu hotpluger.
287 if (!FindAHotpluggableCpu(&test_cpu)) {
290 CpuToggleThreadArg cpu_toggle_arg(test_cpu);
291 std::thread cpu_toggle_thread(CpuToggleThread, &cpu_toggle_arg);
293 std::unique_ptr<EventTypeAndModifier> event_type_modifier = ParseEventType("cpu-cycles");
294 ASSERT_TRUE(event_type_modifier != nullptr);
295 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type_modifier->event_type);
297 attr.enable_on_exec = 0;
299 auto start_time = std::chrono::steady_clock::now();
300 auto cur_time = start_time;
301 auto end_time = std::chrono::steady_clock::now() + test_duration_for_long_tests;
302 auto report_step = std::chrono::seconds(15);
303 size_t iterations = 0;
305 while (cur_time < end_time && !cpu_toggle_arg.cpu_hotplug_failed) {
306 if (cur_time + report_step < std::chrono::steady_clock::now()) {
308 auto diff = std::chrono::duration_cast<std::chrono::seconds>(
309 std::chrono::steady_clock::now() - start_time);
311 GTEST_LOG_(INFO) << "Have Tested " << (diff.count() / 60.0) << " minutes.";
313 cur_time = std::chrono::steady_clock::now();
316 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(attr, -1, test_cpu, nullptr, false);
317 if (event_fd == nullptr) {
318 // Failed to open because the test_cpu is offline.
321 // Wait a little for the event to be installed on test_cpu's perf context.
322 std::this_thread::sleep_for(std::chrono::milliseconds(1));
323 ASSERT_TRUE(event_fd->EnableEvent());
326 GTEST_LOG_(INFO) << "Test offline while ioctl(PERF_EVENT_IOC_ENABLE) for " << iterations << " times.";
329 if (cpu_toggle_arg.cpu_hotplug_failed) {
330 GTEST_LOG_(INFO) << "Test ends because of cpu hotplug failure.";
332 cpu_toggle_arg.end_flag = true;
333 cpu_toggle_thread.join();
336 struct CpuSpinThreadArg {
338 std::atomic<pid_t> tid;
339 std::atomic<bool> end_flag;
342 static void CpuSpinThread(CpuSpinThreadArg* arg) {
344 while (!arg->end_flag) {
347 CPU_SET(arg->spin_cpu, &mask);
348 // If toggle_cpu is offline, setaffinity fails. So call it in a loop to
349 // make sure current thread mostly runs on toggle_cpu.
350 sched_setaffinity(arg->tid, sizeof(mask), &mask);
354 // http://b/28086229.
355 TEST(cpu_offline, offline_while_user_process_profiling) {
356 ScopedMpdecisionKiller scoped_mpdecision_killer;
357 CpuOnlineRestorer cpuonline_restorer;
358 // Start cpu hotpluger.
360 if (!FindAHotpluggableCpu(&test_cpu)) {
363 CpuToggleThreadArg cpu_toggle_arg(test_cpu);
364 std::thread cpu_toggle_thread(CpuToggleThread, &cpu_toggle_arg);
366 // Start cpu spinner.
367 CpuSpinThreadArg cpu_spin_arg;
368 cpu_spin_arg.spin_cpu = test_cpu;
369 cpu_spin_arg.tid = 0;
370 cpu_spin_arg.end_flag = false;
371 std::thread cpu_spin_thread(CpuSpinThread, &cpu_spin_arg);
372 while (cpu_spin_arg.tid == 0) {
373 std::this_thread::sleep_for(std::chrono::milliseconds(1));
376 std::unique_ptr<EventTypeAndModifier> event_type_modifier = ParseEventType("cpu-cycles");
377 ASSERT_TRUE(event_type_modifier != nullptr);
378 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type_modifier->event_type);
379 // Enable profiling in perf_event_open system call.
381 attr.enable_on_exec = 0;
383 auto start_time = std::chrono::steady_clock::now();
384 auto cur_time = start_time;
385 auto end_time = start_time + test_duration_for_long_tests;
386 auto report_step = std::chrono::seconds(15);
387 size_t iterations = 0;
389 while (cur_time < end_time && !cpu_toggle_arg.cpu_hotplug_failed) {
390 if (cur_time + report_step < std::chrono::steady_clock::now()) {
391 auto diff = std::chrono::duration_cast<std::chrono::seconds>(
392 std::chrono::steady_clock::now() - start_time);
394 GTEST_LOG_(INFO) << "Have Tested " << (diff.count() / 60.0) << " minutes.";
396 cur_time = std::chrono::steady_clock::now();
398 // Test if the cpu pmu is still usable.
399 ASSERT_TRUE(EventFd::OpenEventFile(attr, 0, -1, nullptr, true) != nullptr);
401 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(attr, cpu_spin_arg.tid,
402 test_cpu, nullptr, false);
403 if (event_fd == nullptr) {
404 // Failed to open because the test_cpu is offline.
407 // profile for a while.
408 std::this_thread::sleep_for(std::chrono::milliseconds(1));
411 GTEST_LOG_(INFO) << "Test offline while user process profiling for " << iterations << " times.";
414 if (cpu_toggle_arg.cpu_hotplug_failed) {
415 GTEST_LOG_(INFO) << "Test ends because of cpu hotplug failure.";
417 cpu_toggle_arg.end_flag = true;
418 cpu_toggle_thread.join();
419 cpu_spin_arg.end_flag = true;
420 cpu_spin_thread.join();
421 // Check if the cpu-cycle event is still available on test_cpu.
422 if (SetCpuOnline(test_cpu, true)) {
423 ASSERT_TRUE(EventFd::OpenEventFile(attr, -1, test_cpu, nullptr, true) != nullptr);
427 // http://b/19863147.
428 TEST(cpu_offline, offline_while_recording_on_another_cpu) {
429 ScopedMpdecisionKiller scoped_mpdecision_killer;
430 CpuOnlineRestorer cpuonline_restorer;
432 if (GetCpuCount() == 1) {
433 GTEST_LOG_(INFO) << "This test does nothing, because there is only one cpu in the system.";
437 if (!FindAHotpluggableCpu(&test_cpu)) {
440 std::unique_ptr<EventTypeAndModifier> event_type_modifier = ParseEventType("cpu-cycles");
441 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type_modifier->event_type);
443 attr.enable_on_exec = 0;
445 const size_t TEST_ITERATION_COUNT = 10u;
446 for (size_t i = 0; i < TEST_ITERATION_COUNT; ++i) {
448 if (!SetCpuOnline(test_cpu, true)) {
451 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(attr, getpid(), record_cpu, nullptr);
452 ASSERT_TRUE(event_fd != nullptr);
453 if (!SetCpuOnline(test_cpu, false)) {
457 event_fd = EventFd::OpenEventFile(attr, getpid(), record_cpu, nullptr);
458 ASSERT_TRUE(event_fd != nullptr);
462 int main(int argc, char** argv) {
463 for (int i = 1; i < argc; ++i) {
464 if (strcmp(argv[i], "--help") == 0) {
465 printf("--long_test_duration <second> Set test duration for long tests. Default is 120s.\n");
466 printf("--cpu_hotplug_interval <microseconds> Set cpu hotplug interval. Default is 1000us.\n");
467 printf("--verbose Show verbose log.\n");
468 } else if (strcmp(argv[i], "--long_test_duration") == 0) {
470 int second_count = atoi(argv[i+1]);
471 if (second_count <= 0) {
472 fprintf(stderr, "Invalid arg for --long_test_duration.\n");
475 test_duration_for_long_tests = std::chrono::seconds(second_count);
478 } else if (strcmp(argv[i], "--cpu_hotplug_interval") == 0) {
480 int microsecond_count = atoi(argv[i+1]);
481 if (microsecond_count <= 0) {
482 fprintf(stderr, "Invalid arg for --cpu_hotplug_interval\n");
485 cpu_hotplug_interval = std::chrono::microseconds(microsecond_count);
488 } else if (strcmp(argv[i], "--verbose") == 0) {
492 android::base::InitLogging(argv, android::base::StderrLogger);
493 testing::InitGoogleTest(&argc, argv);
494 return RUN_ALL_TESTS();