From de2df99daed9b3ed18dd57c433540933db3a99e5 Mon Sep 17 00:00:00 2001 From: Stephen Roderick Date: Sun, 12 Jun 2011 12:36:11 -0400 Subject: [PATCH 3/3] tests: Add CPU affinity test for Linux only --- tests/tasks_test.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 69 insertions(+), 1 deletions(-) diff --git a/tests/tasks_test.cpp b/tests/tasks_test.cpp index cc65e21..817e45c 100644 --- a/tests/tasks_test.cpp +++ b/tests/tasks_test.cpp @@ -63,9 +63,12 @@ struct TestPeriodic bool stepped; TimeService::ticks ts; + // LINUX: cpu of last step(), 0 if not yet set + // non-LINUX: 0 + int cpu; TestPeriodic() - : overfail(0), underfail(0), succ(0), stepped(false) + : overfail(0), underfail(0), succ(0), stepped(false), cpu(0) { } @@ -93,6 +96,10 @@ struct TestPeriodic } ts = TimeService::Instance()->getTicks(); } +#if defined( OROCOS_TARGET_GNULINUX ) + cpu = sched_getcpu(); + BOOST_REQUIRE_NE(ENOSYS, cpu); +#endif } void finalize() { if (overfail || underfail) @@ -103,6 +110,7 @@ struct TestPeriodic overfail = 0; underfail = 0; succ = 0; + cpu = 0; stepped = false; } }; @@ -346,6 +354,66 @@ BOOST_AUTO_TEST_CASE( testThreads ) t->run(0); } +#if defined( OROCOS_TARGET_GNULINUX ) +// run on just the target CPU +void testAffinity2(boost::scoped_ptr& run, + boost::scoped_ptr& t, + int targetCPU) +{ + bool r = false; + + t->run( run.get() ); + + BOOST_CHECK(t->setCpuAffinity(1 << targetCPU)); + BOOST_CHECK_EQUAL((1 << targetCPU), t->getCpuAffinity()); + + if ( t->getScheduler() == RTT::OS::HighestPriority) { + r = t->start(); + BOOST_CHECK_MESSAGE( r, "Failed to start Thread"); + r = t->stop(); + BOOST_CHECK_MESSAGE( r, "Failed to stop Thread"); + BOOST_CHECK_MESSAGE( run->stepped == true, "Step not executed" ); + BOOST_CHECK_EQUAL(targetCPU, run->cpu); + BOOST_CHECK_LT(0, run->succ); + run->reset(); + } + BOOST_CHECK_EQUAL(0, run->cpu); + r = t->start(); + BOOST_CHECK_MESSAGE( r, "Failed to start Thread"); + sleep(1); + r = t->stop(); + BOOST_CHECK_MESSAGE( r, "Failed to stop Thread" ); + BOOST_CHECK_MESSAGE( run->stepped == true, "Step not executed" ); + BOOST_CHECK_EQUAL(targetCPU, run->cpu); + BOOST_CHECK_LT(0, run->succ); + + t->run(0); +} + +BOOST_AUTO_TEST_CASE( testAffinity ) +{ + // this test is kind of irrelevant with only 1 CPU + int numCPU = sysconf( _SC_NPROCESSORS_ONLN ); + if (1 < numCPU) + { + boost::scoped_ptr run( new TestPeriodic() ); + boost::scoped_ptr t( new RTT::OS::Thread(ORO_SCHED_RT, RTT::OS::HighestPriority, 0.1, + ~0, "PThread") ); + // returned affinity depends on the number of actual CPUs, and won't be "~0" + unsigned mask=0; + for (int i=0; igetCpuAffinity()); + + // test just a couple of cases + testAffinity2(run, t, 0); + testAffinity2(run, t, numCPU-1); + } + // else ignore test as insufficient number of CPUs +} +#endif BOOST_AUTO_TEST_CASE( testNonPeriodic ) { -- 1.7.4.4