Skip to content

Commit

Permalink
Add cpu affinity feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
ressac authored and Peter Soetens committed May 5, 2011
1 parent 8c45e02 commit e013a1f
Show file tree
Hide file tree
Showing 23 changed files with 305 additions and 18 deletions.
24 changes: 20 additions & 4 deletions rtt/Activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,27 @@ namespace RTT
using namespace detail;

Activity::Activity(RunnableInterface* _r, const std::string& name )
: ActivityInterface(_r), os::Thread(ORO_SCHED_OTHER, 0, 0.0, name )
: ActivityInterface(_r), os::Thread(ORO_SCHED_OTHER, 0, 0.0, ~0, name )
{
}

Activity::Activity(int priority, RunnableInterface* r, const std::string& name )
: ActivityInterface(r), os::Thread(ORO_SCHED_RT, priority, 0.0, name )
: ActivityInterface(r), os::Thread(ORO_SCHED_RT, priority, 0.0, ~0, name )
{
}

Activity::Activity(int priority, Seconds period, RunnableInterface* r, const std::string& name )
: ActivityInterface(r), os::Thread(ORO_SCHED_RT, priority, period, name )
: ActivityInterface(r), os::Thread(ORO_SCHED_RT, priority, period, ~0, name )
{
}

Activity::Activity(int scheduler, int priority, Seconds period, RunnableInterface* r, const std::string& name )
: ActivityInterface(r), os::Thread(scheduler, priority, period, name )
: ActivityInterface(r), os::Thread(scheduler, priority, period, ~0, name )
{
}

Activity::Activity(int scheduler, int priority, Seconds period, unsigned cpu_affinity, RunnableInterface* r, const std::string& name )
: ActivityInterface(r), os::Thread(scheduler, priority, period, cpu_affinity, name )
{
}

Expand Down Expand Up @@ -149,4 +154,15 @@ namespace RTT
bool Activity::isPeriodic() const {
return Thread::isPeriodic();
}

unsigned Activity::getCpuAffinity() const
{
return Thread::getCpuAffinity();
}

bool Activity::setCpuAffinity(unsigned cpu)
{
return Thread::setCpuAffinity(cpu);
}

}
23 changes: 23 additions & 0 deletions rtt/Activity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ namespace RTT
Activity(int scheduler, int priority, Seconds period,
base::RunnableInterface* r = 0, const std::string& name ="Activity");

/**
* @brief Create an Activity with a given scheduler type, priority, period and cpu affinity.
*
* @param scheduler
* The scheduler in which the activity's thread must run. Use ORO_SCHED_OTHER or
* ORO_SCHED_RT.
* @param priority
* The priority of this activity.
* @param period
* The periodicity of the Activity
* @param cpu_affinity
* The prefered cpu to run on (a mask)
* @param r
* The optional base::RunnableInterface to run exclusively within this Activity
* @param name The name of the underlying thread.
*/
Activity(int scheduler, int priority, Seconds period, unsigned cpu_affinity,
base::RunnableInterface* r = 0, const std::string& name ="Activity");

/**
* Stops and terminates a Activity
*/
Expand All @@ -141,6 +160,10 @@ namespace RTT

virtual bool setPeriod(Seconds period);

virtual unsigned getCpuAffinity() const;

virtual bool setCpuAffinity(unsigned cpu);

virtual os::ThreadInterface* thread();

/**
Expand Down
2 changes: 2 additions & 0 deletions rtt/TaskContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ namespace RTT
this->addOperation("isRunning", &TaskContext::isRunning, this, ClientThread).doc("Is this TaskContext started ?");
this->addOperation("getPeriod", &TaskContext::getPeriod, this, ClientThread).doc("Get the configured execution period. -1.0: no thread associated, 0.0: non periodic, > 0.0: the period.");
this->addOperation("setPeriod", &TaskContext::setPeriod, this, ClientThread).doc("Set the execution period in seconds.").arg("s", "Period in seconds.");
this->addOperation("getCpuAffinity", &TaskContext::getCpuAffinity, this, ClientThread).doc("Get the configured cpu affinity.");
this->addOperation("setCpuAffinity", &TaskContext::setCpuAffinity, this, ClientThread).doc("Set the cpu affinity.").arg("cpu", "Cpu mask.");
this->addOperation("isActive", &TaskContext::isActive, this, ClientThread).doc("Is the Execution Engine of this TaskContext active ?");
this->addOperation("inFatalError", &TaskContext::inFatalError, this, ClientThread).doc("Check if this TaskContext is in the FatalError state.");
this->addOperation("error", &TaskContext::error, this, ClientThread).doc("Enter the RunTimeError state (= errorHook() ).");
Expand Down
14 changes: 14 additions & 0 deletions rtt/base/ActivityInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ namespace RTT
*/
virtual bool setPeriod(Seconds s) = 0;


/**
* Get the cpu affinity of this activity
*
* @return The cpu affinity of this activity.
*/
virtual unsigned getCpuAffinity() const = 0;

/**
* Set the cpu affinity of this activity.
* @return true if it could be updated, false otherwise.
*/
virtual bool setCpuAffinity(unsigned cpu) = 0;

/**
* Execute this activity such that it \a executes a step or loop of the RunnableInterface.
* When you invoke execute() you intend to call the step() or loop() methods.
Expand Down
12 changes: 11 additions & 1 deletion rtt/base/TaskCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ namespace RTT {
return this->engine() && this->engine()->getActivity() && this->engine()->getActivity()->isActive();
}

double TaskCore::getPeriod() const
Seconds TaskCore::getPeriod() const
{
return this->engine()->getActivity() ? this->engine()->getActivity()->getPeriod() : -1.0;
}
Expand All @@ -279,6 +279,16 @@ namespace RTT {
return this->engine()->getActivity() ? this->engine()->getActivity()->setPeriod(s) : false;
}

unsigned TaskCore::getCpuAffinity() const
{
return this->engine()->getActivity() ? this->engine()->getActivity()->getCpuAffinity() : ~0;
}

bool TaskCore::setCpuAffinity(unsigned cpu)
{
return this->engine()->getActivity() ? this->engine()->getActivity()->setCpuAffinity(cpu) : false;
}

bool TaskCore::configureHook() {
return true;
}
Expand Down
13 changes: 13 additions & 0 deletions rtt/base/TaskCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,19 @@ namespace RTT
*/
virtual bool setPeriod(Seconds s);

/**
* Get the configured cpu affinity of this component.
* @see ActivityInterface::getCpuAffinity()
*/
virtual unsigned getCpuAffinity() const;

/**
* Sets the cpu affinity of this component.
* @return false if not allowed by the component's activity.
* @see ActivityInterface::setCpuAffinity()
*/
virtual bool setCpuAffinity(unsigned cpu);

/**
* Inspect if the component is in the FatalError state.
* There is no possibility to recover from this state.
Expand Down
17 changes: 17 additions & 0 deletions rtt/extras/PeriodicActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ namespace RTT {
this->init();
}

PeriodicActivity::PeriodicActivity(int scheduler, int priority, Seconds period, unsigned cpu_affinity, RunnableInterface* r )
: ActivityInterface(r), running(false), active(false),
thread_( TimerThread::Instance(scheduler, priority, period, cpu_affinity) )
{
this->init();
}

PeriodicActivity::PeriodicActivity(TimerThreadPtr thread, RunnableInterface* r )
: ActivityInterface(r), running(false), active(false),
thread_( thread )
Expand Down Expand Up @@ -158,6 +165,16 @@ namespace RTT {
return false;
}

unsigned PeriodicActivity::getCpuAffinity() const
{
return thread_->getCpuAffinity();
}

bool PeriodicActivity::setCpuAffinity(unsigned cpu)
{
return thread_->setCpuAffinity(cpu);
}

bool PeriodicActivity::initialize() {
if (runner != 0)
return runner->initialize();
Expand Down
23 changes: 23 additions & 0 deletions rtt/extras/PeriodicActivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ namespace RTT
*/
PeriodicActivity(int scheduler, int priority, Seconds period, base::RunnableInterface* r=0 );

/**
* @brief Create a Periodic Activity with a given scheduler type, priority and period.
*
* @param scheduler
* The scheduler in which the activitie's thread must run. Use ORO_SCHED_OTHER or
* ORO_SCHED_RT.
* @param priority
* The priority of this activity. A lookup will be done to locate a suitable
* TimerThread with the same priority. If it exists and it has the same \a period,
* this activity will be executed in that thread. Otherwise, a new TimerThread is created.
* @param period
* The periodicity of the PeriodicActivity
* @param cpu_affinity
* The prefered cpu to run on (a mask)
* @param r
* The optional base::RunnableInterface to run exclusively within this Activity
*/
PeriodicActivity(int scheduler, int priority, Seconds period, unsigned cpu_affinity, base::RunnableInterface* r=0 );


/**
* @brief Create a Periodic Activity executing in a given thread.
Expand Down Expand Up @@ -167,6 +186,10 @@ namespace RTT

virtual bool setPeriod(Seconds s);

virtual unsigned getCpuAffinity() const;

virtual bool setCpuAffinity(unsigned cpu);

virtual os::ThreadInterface* thread();

/**
Expand Down
10 changes: 10 additions & 0 deletions rtt/extras/SequentialActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ namespace RTT {
return false;
}

unsigned SequentialActivity::getCpuAffinity() const
{
return ~0;
}

bool SequentialActivity::setCpuAffinity(unsigned cpu)
{
return false;
}

os::ThreadInterface* SequentialActivity::thread()
{
return os::MainThread::Instance();
Expand Down
4 changes: 4 additions & 0 deletions rtt/extras/SequentialActivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ namespace RTT

bool setPeriod(Seconds s);

unsigned getCpuAffinity() const;

bool setCpuAffinity(unsigned cpu);

os::ThreadInterface* thread();

bool initialize();
Expand Down
12 changes: 12 additions & 0 deletions rtt/extras/SlaveActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ namespace RTT {
return true;
}

unsigned SlaveActivity::getCpuAffinity() const
{
if (mmaster)
return mmaster->getCpuAffinity();
return ~0;
}

bool SlaveActivity::setCpuAffinity(unsigned cpu)
{
return false;
}

os::ThreadInterface* SlaveActivity::thread()
{
return mmaster ? mmaster->thread() : os::MainThread::Instance();
Expand Down
4 changes: 4 additions & 0 deletions rtt/extras/SlaveActivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ namespace RTT

bool setPeriod(Seconds s);

unsigned getCpuAffinity() const;

bool setCpuAffinity(unsigned cpu);

os::ThreadInterface* thread();

bool initialize();
Expand Down
15 changes: 10 additions & 5 deletions rtt/extras/TimerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ namespace RTT {
}

TimerThreadPtr TimerThread::Instance(int scheduler, int pri, double per)
{
return Instance(scheduler, pri, per, ~0);
}

TimerThreadPtr TimerThread::Instance(int scheduler, int pri, double per, unsigned cpu_affinity)
{
// Since the period is stored as nsecs, we convert per to NS in order
// to get a match.
Expand All @@ -77,19 +82,19 @@ namespace RTT {
}
++it;
}
TimerThreadPtr ret( new TimerThread(scheduler, pri, "TimerThreadInstance", per) );
TimerThreadPtr ret( new TimerThread(scheduler, pri, "TimerThreadInstance", per, cpu_affinity) );
TimerThreads.push_back( ret );
return ret;
}

TimerThread::TimerThread(int priority, const std::string& name, double periodicity)
: Thread( ORO_SCHED_RT, priority, periodicity, name), cleanup(false)
TimerThread::TimerThread(int priority, const std::string& name, double periodicity, unsigned cpu_affinity)
: Thread( ORO_SCHED_RT, priority, periodicity, cpu_affinity, name), cleanup(false)
{
tasks.reserve(MAX_ACTIVITIES);
}

TimerThread::TimerThread(int scheduler, int priority, const std::string& name, double periodicity)
: Thread(scheduler, priority, periodicity, name), cleanup(false)
TimerThread::TimerThread(int scheduler, int priority, const std::string& name, double periodicity, unsigned cpu_affinity)
: Thread(scheduler, priority, periodicity, cpu_affinity, name), cleanup(false)
{
tasks.reserve(MAX_ACTIVITIES);
}
Expand Down
8 changes: 6 additions & 2 deletions rtt/extras/TimerThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace RTT
* @param periodicity
* The periodicity of this thread in seconds (e.g. 0.001 = 1000Hz )
*/
TimerThread(int priority, const std::string& name, double periodicity);
TimerThread(int priority, const std::string& name, double periodicity, unsigned cpu_affinity = ~0);

/**
* Create a periodic Timer thread with a given scheduler type.
Expand All @@ -94,7 +94,7 @@ namespace RTT
* @param periodicity
* The periodicity of this thread in seconds (e.g. 0.001 = 1000Hz )
*/
TimerThread(int scheduler, int priority, const std::string& name, double periodicity);
TimerThread(int scheduler, int priority, const std::string& name, double periodicity, unsigned cpu_affinity = ~0);

/**
* Destructor
Expand All @@ -118,6 +118,10 @@ namespace RTT
* Create a TimerThread with a given scheduler, priority and periodicity.
*/
static TimerThreadPtr Instance(int scheduler, int priority, double periodicity);
/**
* Create a TimerThread with a given scheduler, priority and periodicity.
*/
static TimerThreadPtr Instance(int scheduler, int priority, double periodicity, unsigned cpu_affinity);
protected:
virtual bool initialize();
virtual void step();
Expand Down
Loading

0 comments on commit e013a1f

Please sign in to comment.