Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix cpuset-threads tests #6278

Merged
merged 2 commits into from
Mar 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 50 additions & 11 deletions test/server/options_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "server/options_impl.h"

#if defined(__linux__)
#include <sched.h>
#include "server/options_impl_platform_linux.h"
#endif
#include "test/mocks/api/mocks.h"
Expand Down Expand Up @@ -332,45 +333,83 @@ TEST_F(OptionsImplTest, SetBothConcurrencyAndCpuset) {

#if defined(__linux__)

using testing::DoAll;
using testing::Return;
using testing::SetArgPointee;

class OptionsImplPlatformLinuxTest : public testing::Test {
public:
};

TEST_F(OptionsImplPlatformLinuxTest, AffinityTest1) {
// Success case: cpuset size and hardware thread count are the same.
unsigned int fake_cpuset_size = std::thread::hardware_concurrency();
unsigned int fake_hw_threads = fake_cpuset_size;
unsigned int fake_hw_threads = 4;
cpu_set_t test_set;
Api::MockLinuxOsSysCalls linux_os_sys_calls;
TestThreadsafeSingletonInjector<Api::LinuxOsSysCallsImpl> linux_os_calls(&linux_os_sys_calls);

EXPECT_EQ(OptionsImplPlatformLinux::getCpuAffinityCount(fake_hw_threads), fake_cpuset_size);
// Set cpuset size to be four.
CPU_ZERO(&test_set);
for (int i = 0; i < 4; i++) {
CPU_SET(i, &test_set);
}

EXPECT_CALL(linux_os_sys_calls, sched_getaffinity(_, _, _))
.WillOnce(DoAll(SetArgPointee<2>(test_set), Return(Api::SysCallIntResult{0, 0})));
EXPECT_EQ(OptionsImplPlatformLinux::getCpuAffinityCount(fake_hw_threads), 4);
}

TEST_F(OptionsImplPlatformLinuxTest, AffinityTest2) {
// Success case: cpuset size is half of the hardware thread count.
unsigned int fake_cpuset_size = std::thread::hardware_concurrency();
unsigned int fake_hw_threads = 2 * fake_cpuset_size;
unsigned int fake_hw_threads = 16;
cpu_set_t test_set;
Api::MockLinuxOsSysCalls linux_os_sys_calls;
TestThreadsafeSingletonInjector<Api::LinuxOsSysCallsImpl> linux_os_calls(&linux_os_sys_calls);

// Set cpuset size to be eight.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally I prefer to see really explicit stuff like this in tests, but in this particular case I think it might be clearer to use a loop. Or if there's some specific meaning to the non-contiguous CPU numbers you've set here it'd be worth expanding the comment to explain why that's done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, changed to for loop. My original idea was to emphasize that the CPU ids are arbitrary and only the cpuset size matters. But I agree that it looks better this way.

CPU_ZERO(&test_set);
for (int i = 0; i < 8; i++) {
CPU_SET(i, &test_set);
}

EXPECT_EQ(OptionsImplPlatformLinux::getCpuAffinityCount(fake_hw_threads), fake_cpuset_size);
EXPECT_CALL(linux_os_sys_calls, sched_getaffinity(_, _, _))
.WillOnce(DoAll(SetArgPointee<2>(test_set), Return(Api::SysCallIntResult{0, 0})));
EXPECT_EQ(OptionsImplPlatformLinux::getCpuAffinityCount(fake_hw_threads), 8);
}

TEST_F(OptionsImplPlatformLinuxTest, AffinityTest3) {
// Failure case: cpuset size is bigger than the hardware thread count.
unsigned int fake_cpuset_size = std::thread::hardware_concurrency();
unsigned int fake_hw_threads = fake_cpuset_size - 1;
unsigned int fake_hw_threads = 4;
cpu_set_t test_set;
Api::MockLinuxOsSysCalls linux_os_sys_calls;
TestThreadsafeSingletonInjector<Api::LinuxOsSysCallsImpl> linux_os_calls(&linux_os_sys_calls);

// Set cpuset size to be eight.
CPU_ZERO(&test_set);
for (int i = 0; i < 8; i++) {
CPU_SET(i, &test_set);
}

EXPECT_CALL(linux_os_sys_calls, sched_getaffinity(_, _, _))
.WillOnce(DoAll(SetArgPointee<2>(test_set), Return(Api::SysCallIntResult{0, 0})));
EXPECT_EQ(OptionsImplPlatformLinux::getCpuAffinityCount(fake_hw_threads), fake_hw_threads);
}

TEST_F(OptionsImplPlatformLinuxTest, AffinityTest4) {
// When sched_getaffinity() fails, expect to get the hardware thread count.
unsigned int fake_cpuset_size = std::thread::hardware_concurrency();
unsigned int fake_hw_threads = 2 * fake_cpuset_size;
unsigned int fake_hw_threads = 8;
cpu_set_t test_set;
Api::MockLinuxOsSysCalls linux_os_sys_calls;
TestThreadsafeSingletonInjector<Api::LinuxOsSysCallsImpl> linux_os_calls(&linux_os_sys_calls);

// Set cpuset size to be four.
CPU_ZERO(&test_set);
for (int i = 0; i < 4; i++) {
CPU_SET(i, &test_set);
}

EXPECT_CALL(linux_os_sys_calls, sched_getaffinity(_, _, _))
.WillOnce(Return(Api::SysCallIntResult{-1, 0}));
.WillOnce(DoAll(SetArgPointee<2>(test_set), Return(Api::SysCallIntResult{-1, 0})));
EXPECT_EQ(OptionsImplPlatformLinux::getCpuAffinityCount(fake_hw_threads), fake_hw_threads);
}

Expand Down