#include <gtest/gtest.h>
#include <sys/stat.h>
- #include <sys/syscall.h>
#include <unistd.h>
#if defined(__BIONIC__)
#include <sys/system_properties.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
+ #include "environment.h"
#include "event_attr.h"
#include "event_fd.h"
#include "event_type.h"
struct CpuToggleThreadArg {
int toggle_cpu;
std::atomic<bool> end_flag;
+ std::atomic<bool> cpu_hotplug_failed;
+
+ CpuToggleThreadArg(int cpu)
+ : toggle_cpu(cpu), end_flag(false), cpu_hotplug_failed(false) {
+ }
};
static void CpuToggleThread(CpuToggleThreadArg* arg) {
while (!arg->end_flag) {
- CHECK(SetCpuOnline(arg->toggle_cpu, true));
+ if (!SetCpuOnline(arg->toggle_cpu, true)) {
+ arg->cpu_hotplug_failed = true;
+ break;
+ }
std::this_thread::sleep_for(cpu_hotplug_interval);
- CHECK(SetCpuOnline(arg->toggle_cpu, false));
+ if (!SetCpuOnline(arg->toggle_cpu, false)) {
+ arg->cpu_hotplug_failed = true;
+ break;
+ }
std::this_thread::sleep_for(cpu_hotplug_interval);
}
}
if (!FindAHotpluggableCpu(&test_cpu)) {
return;
}
- CpuToggleThreadArg cpu_toggle_arg;
- cpu_toggle_arg.toggle_cpu = test_cpu;
- cpu_toggle_arg.end_flag = false;
+ CpuToggleThreadArg cpu_toggle_arg(test_cpu);
std::thread cpu_toggle_thread(CpuToggleThread, &cpu_toggle_arg);
std::unique_ptr<EventTypeAndModifier> event_type_modifier = ParseEventType("cpu-cycles");
auto report_step = std::chrono::seconds(15);
size_t iterations = 0;
- while (cur_time < end_time) {
+ while (cur_time < end_time && !cpu_toggle_arg.cpu_hotplug_failed) {
if (cur_time + report_step < std::chrono::steady_clock::now()) {
// Report test time.
auto diff = std::chrono::duration_cast<std::chrono::seconds>(
GTEST_LOG_(INFO) << "Test offline while recording for " << iterations << " times.";
}
}
+ if (cpu_toggle_arg.cpu_hotplug_failed) {
+ GTEST_LOG_(INFO) << "Test ends because of cpu hotplug failure.";
+ }
cpu_toggle_arg.end_flag = true;
cpu_toggle_thread.join();
}
if (!FindAHotpluggableCpu(&test_cpu)) {
return;
}
- CpuToggleThreadArg cpu_toggle_arg;
- cpu_toggle_arg.toggle_cpu = test_cpu;
- cpu_toggle_arg.end_flag = false;
+ CpuToggleThreadArg cpu_toggle_arg(test_cpu);
std::thread cpu_toggle_thread(CpuToggleThread, &cpu_toggle_arg);
std::unique_ptr<EventTypeAndModifier> event_type_modifier = ParseEventType("cpu-cycles");
auto report_step = std::chrono::seconds(15);
size_t iterations = 0;
- while (cur_time < end_time) {
+ while (cur_time < end_time && !cpu_toggle_arg.cpu_hotplug_failed) {
if (cur_time + report_step < std::chrono::steady_clock::now()) {
// Report test time.
auto diff = std::chrono::duration_cast<std::chrono::seconds>(
GTEST_LOG_(INFO) << "Test offline while ioctl(PERF_EVENT_IOC_ENABLE) for " << iterations << " times.";
}
}
+ if (cpu_toggle_arg.cpu_hotplug_failed) {
+ GTEST_LOG_(INFO) << "Test ends because of cpu hotplug failure.";
+ }
cpu_toggle_arg.end_flag = true;
cpu_toggle_thread.join();
}
};
static void CpuSpinThread(CpuSpinThreadArg* arg) {
- arg->tid = syscall(__NR_gettid);
+ arg->tid = gettid();
while (!arg->end_flag) {
cpu_set_t mask;
CPU_ZERO(&mask);
if (!FindAHotpluggableCpu(&test_cpu)) {
return;
}
- CpuToggleThreadArg cpu_toggle_arg;
- cpu_toggle_arg.toggle_cpu = test_cpu;
- cpu_toggle_arg.end_flag = false;
+ CpuToggleThreadArg cpu_toggle_arg(test_cpu);
std::thread cpu_toggle_thread(CpuToggleThread, &cpu_toggle_arg);
// Start cpu spinner.
auto report_step = std::chrono::seconds(15);
size_t iterations = 0;
- while (cur_time < end_time) {
+ while (cur_time < end_time && !cpu_toggle_arg.cpu_hotplug_failed) {
if (cur_time + report_step < std::chrono::steady_clock::now()) {
auto diff = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::steady_clock::now() - start_time);
GTEST_LOG_(INFO) << "Test offline while user process profiling for " << iterations << " times.";
}
}
+ if (cpu_toggle_arg.cpu_hotplug_failed) {
+ GTEST_LOG_(INFO) << "Test ends because of cpu hotplug failure.";
+ }
cpu_toggle_arg.end_flag = true;
cpu_toggle_thread.join();
cpu_spin_arg.end_flag = true;
cpu_spin_thread.join();
// Check if the cpu-cycle event is still available on test_cpu.
- ASSERT_TRUE(SetCpuOnline(test_cpu, true));
- ASSERT_TRUE(EventFd::OpenEventFile(attr, -1, test_cpu, nullptr, true) != nullptr);
+ if (SetCpuOnline(test_cpu, true)) {
+ ASSERT_TRUE(EventFd::OpenEventFile(attr, -1, test_cpu, nullptr, true) != nullptr);
+ }
}
// http://b/19863147.
const size_t TEST_ITERATION_COUNT = 10u;
for (size_t i = 0; i < TEST_ITERATION_COUNT; ++i) {
int record_cpu = 0;
- ASSERT_TRUE(SetCpuOnline(test_cpu, true));
+ if (!SetCpuOnline(test_cpu, true)) {
+ break;
+ }
std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(attr, getpid(), record_cpu, nullptr);
ASSERT_TRUE(event_fd != nullptr);
- ASSERT_TRUE(SetCpuOnline(test_cpu, false));
+ if (!SetCpuOnline(test_cpu, false)) {
+ break;
+ }
event_fd = nullptr;
event_fd = EventFd::OpenEventFile(attr, getpid(), record_cpu, nullptr);
ASSERT_TRUE(event_fd != nullptr);