OpenSDN source code
Task Class Referenceabstract

Task is a class to describe a computational task within OpenSDN control plane applications. A task is a labelled sequence of instructions (code) and data processed by them in a single thread. OpenSDN Task wraps over tbb::task. Tasks are labelled using a pair of numbers: More...

#include <task.h>

Inheritance diagram for Task:

Public Types

enum  State { INIT , WAIT , RUN }
 Task states. More...
 
enum  TbbState { TBB_INIT , TBB_ENQUEUED , TBB_EXEC , TBB_DONE }
 Describes states of a task according to TBB library. More...
 

Public Member Functions

 Task (int task_id, int task_data_id)
 Creates a new task with the given values of task code ID and task data ID. More...
 
 Task (int task_id)
 Creates a new task with the given value of task code ID and wildcard for task data ID. More...
 
virtual ~Task ()
 Destroys a task. More...
 
virtual bool Run ()=0
 Code to execute in a task. Returns true if task is completed. Return false to reschedule the task. More...
 
virtual void OnTaskCancel ()
 Called on task exit, if it is marked for cancellation. If the user wants to do any cleanup on task cancellation, then he/she can overload this function. More...
 
State state () const
 Returns a state value of a task. More...
 
int task_code_id () const
 Returns the code ID of this task. More...
 
int task_data_id () const
 Returns the data ID of this task. More...
 
uint64_t seqno () const
 Returns the sequence number of this task. More...
 
bool task_cancelled () const
 Returns true if the task has been canceled. More...
 
virtual std::string Description () const =0
 Gives a description of the task. More...
 
uint64_t enqueue_time () const
 Returns the time when the task was enqueued for execution. More...
 
uint64_t schedule_time () const
 Returns the time when the task execution was started. More...
 
uint32_t execute_delay () const
 Returns the threshold for the task execution duration. More...
 
uint32_t schedule_delay () const
 Returns the time threshold for time difference between moments when the task was started and when it was enqueue. More...
 

Static Public Member Functions

static TaskRunning ()
 Returns a pointer to the current task the code is executing under. More...
 

Static Public Attributes

static const int kTaskInstanceAny = -1
 Specifies value for wildcard (any or *) task data ID. More...
 

Private Member Functions

void seqno (uint64_t seqno)
 Sets sequence number of the task. More...
 
void tbb_state (TbbState s)
 Sets a TBB state for the task. More...
 
void state (State s)
 Sets a state for this task. More...
 
void set_task_recycle ()
 Marks this task for recycle. More...
 
void set_task_complete ()
 Marks this task as completed (forbids recycling) More...
 
void StartTask (TaskScheduler *scheduler)
 Starts execution of a task. More...
 
 DISALLOW_COPY_AND_ASSIGN (Task)
 

Private Attributes

int task_code_id_
 The code path executed by the task. More...
 
int task_data_id_
 The dataset id within a code path. More...
 
tbb::tasktask_impl_
 A pointer to an Intel TBB object storing low-level information to manage the task. More...
 
State state_
 Stores a state of the task. More...
 
TbbState tbb_state_
 Stores a state of the TBB object. More...
 
uint64_t seqno_
 Stores the sequence number. More...
 
bool task_recycle_
 Determines if the task must be rescheduled (reused) after its completion. More...
 
bool task_cancel_
 Determines if the task's execution was canceled. More...
 
uint64_t enqueue_time_
 Contains the time when the task was enqueued for execution. More...
 
uint64_t schedule_time_
 Contains the time when the task was started. More...
 
uint32_t execute_delay_
 Sets threshold for the task's execution time. If the threshold is exceeded, the event is logged. More...
 
uint32_t schedule_delay_
 Sets threshold for delay between enqueueing and execution. If the threshold is exceeded, the event is logged. More...
 
boost::intrusive::list_member_hook waitq_hook_
 

Friends

class TaskEntry
 Gives access to private members for TaskEntry class. More...
 
class TaskScheduler
 Gives access to private members for TaskScheduler class. More...
 
class TaskImpl
 Gives access to private members for TaskImpl class. More...
 
std::ostream & operator<< (std::ostream &out, const Task &task)
 Provides access to private members of a task for the output stream redirection operator. More...
 

Detailed Description

Task is a class to describe a computational task within OpenSDN control plane applications. A task is a labelled sequence of instructions (code) and data processed by them in a single thread. OpenSDN Task wraps over tbb::task. Tasks are labelled using a pair of numbers:

  • task code ID (or task ID), which corresponds to a version of code to run into the task;
  • task data ID, which corresponds to a version of data supplied to the task and processed by code.

This labelling is used to apply execution policies determining which tasks are allowed to be executed in parallel with others and which tasks are forbidden to run in parallel. The labels are expressed as <tcid, tdid>, where tcid is a task code ID and tdid is a task data ID.

If a task with tcid has tdid equal to -1, then any number of tasks with label <tcid,-1> can run at a time. If a task has task data ID larger or equal to 0, then only one task with this given label (i.e. <tcid,tdid>) can run at a time.

When there are multiple tasks ready to run, they are scheduled in their order of enqueue.

Additionaly, parallel execution of tasks can be managed using task execution policies. Task execution policies are specified per a task with the given code ID tcid0 and arbitrary task data ID in a form of a list of task labels: <tcid0,-1> => <tcid1,tdid1>, <tcid2,tdid2>, ..., <tcidN,tdidN>.

This list specifies which tasks can't be executed in parallel with a task having specified task code ID (tcid0). Each label <tcidN, tdidN> in a policy (i.e. the list) is called task exclusion because it specifies that the task with this task code ID and task data ID cannot run in parallel with <tcid0, -1>. When tdid is equal to -1 in a task exclusion, it corresponds to wildcard (*), i.e. all possible values of the task data ID tdid.

For example, if we have a policy:

  • <tcid0,-1> => <tcid1, -1> <tcid2, 2> <tcid3, 3>

The policy states that:

  • Task <tcid0,-1> cannot run as long as <tcid1, -1> is running;
  • Task <tcid0, 2> cannot run as long as task <tcid2, 2> is running;
  • Task <tcid0, 3> cannot run as long as task <tcid3, 3> is running.

Policy rules are symmetric. I.e., the previous example states also:

  • Task <tcid1,-1> cannot run as long as <tcid0,-1> is running;
  • Task <tcid2, 2> cannot run as long as task <tcid0, 2> is running;
  • Task <tcid3, 3> cannot run as long as task <tcid0, 3> is running.

Definition at line 79 of file task.h.

Member Enumeration Documentation

◆ State

Task states.

Enumerator
INIT 

A task was initialized.

WAIT 

A task is waiting in a queue.

RUN 

A task is being run.

Definition at line 83 of file task.h.

◆ TbbState

Describes states of a task according to TBB library.

Enumerator
TBB_INIT 
TBB_ENQUEUED 
TBB_EXEC 
TBB_DONE 

Definition at line 96 of file task.h.

Constructor & Destructor Documentation

◆ Task() [1/2]

Task::Task ( int  task_id,
int  task_data_id 
)

Creates a new task with the given values of task code ID and task data ID.

Definition at line 1534 of file task.cc.

◆ Task() [2/2]

Task::Task ( int  task_id)

Creates a new task with the given value of task code ID and wildcard for task data ID.

Definition at line 1540 of file task.cc.

◆ ~Task()

virtual Task::~Task ( )
inlinevirtual

Destroys a task.

Definition at line 116 of file task.h.

Member Function Documentation

◆ Description()

◆ DISALLOW_COPY_AND_ASSIGN()

Task::DISALLOW_COPY_AND_ASSIGN ( Task  )
private

◆ enqueue_time()

uint64_t Task::enqueue_time ( ) const
inline

Returns the time when the task was enqueued for execution.

Definition at line 156 of file task.h.

◆ execute_delay()

uint32_t Task::execute_delay ( ) const
inline

Returns the threshold for the task execution duration.

Definition at line 162 of file task.h.

◆ OnTaskCancel()

virtual void Task::OnTaskCancel ( )
inlinevirtual

Called on task exit, if it is marked for cancellation. If the user wants to do any cleanup on task cancellation, then he/she can overload this function.

Reimplemented in Timer::TimerTask.

Definition at line 125 of file task.h.

◆ Run()

◆ Running()

Task * Task::Running ( )
static

Returns a pointer to the current task the code is executing under.

Definition at line 1567 of file task.cc.

◆ schedule_delay()

uint32_t Task::schedule_delay ( ) const
inline

Returns the time threshold for time difference between moments when the task was started and when it was enqueue.

Definition at line 166 of file task.h.

◆ schedule_time()

uint64_t Task::schedule_time ( ) const
inline

Returns the time when the task execution was started.

Definition at line 159 of file task.h.

◆ seqno() [1/2]

uint64_t Task::seqno ( ) const
inline

Returns the sequence number of this task.

Definition at line 139 of file task.h.

◆ seqno() [2/2]

void Task::seqno ( uint64_t  seqno)
inlineprivate

Sets sequence number of the task.

Definition at line 180 of file task.h.

Here is the call graph for this function:

◆ set_task_complete()

void Task::set_task_complete ( )
inlineprivate

Marks this task as completed (forbids recycling)

Definition at line 192 of file task.h.

◆ set_task_recycle()

void Task::set_task_recycle ( )
inlineprivate

Marks this task for recycle.

Definition at line 189 of file task.h.

◆ StartTask()

void Task::StartTask ( TaskScheduler scheduler)
private

Starts execution of a task.

Definition at line 1547 of file task.cc.

Here is the call graph for this function:

◆ state() [1/2]

State Task::state ( ) const
inline

Returns a state value of a task.

Definition at line 130 of file task.h.

◆ state() [2/2]

void Task::state ( State  s)
inlineprivate

Sets a state for this task.

Definition at line 186 of file task.h.

◆ task_cancelled()

bool Task::task_cancelled ( ) const
inline

Returns true if the task has been canceled.

Definition at line 150 of file task.h.

◆ task_code_id()

int Task::task_code_id ( ) const
inline

Returns the code ID of this task.

Definition at line 133 of file task.h.

◆ task_data_id()

int Task::task_data_id ( ) const
inline

Returns the data ID of this task.

Definition at line 136 of file task.h.

◆ tbb_state()

void Task::tbb_state ( TbbState  s)
inlineprivate

Sets a TBB state for the task.

Definition at line 183 of file task.h.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const Task task 
)
friend

Provides access to private members of a task for the output stream redirection operator.

◆ TaskEntry

friend class TaskEntry
friend

Gives access to private members for TaskEntry class.

Definition at line 171 of file task.h.

◆ TaskImpl

friend class TaskImpl
friend

Gives access to private members for TaskImpl class.

Definition at line 177 of file task.h.

◆ TaskScheduler

friend class TaskScheduler
friend

Gives access to private members for TaskScheduler class.

Definition at line 174 of file task.h.

Member Data Documentation

◆ enqueue_time_

uint64_t Task::enqueue_time_
private

Contains the time when the task was enqueued for execution.

Definition at line 225 of file task.h.

◆ execute_delay_

uint32_t Task::execute_delay_
private

Sets threshold for the task's execution time. If the threshold is exceeded, the event is logged.

Definition at line 232 of file task.h.

◆ kTaskInstanceAny

const int Task::kTaskInstanceAny = -1
static

Specifies value for wildcard (any or *) task data ID.

Definition at line 104 of file task.h.

◆ schedule_delay_

uint32_t Task::schedule_delay_
private

Sets threshold for delay between enqueueing and execution. If the threshold is exceeded, the event is logged.

Definition at line 236 of file task.h.

◆ schedule_time_

uint64_t Task::schedule_time_
private

Contains the time when the task was started.

Definition at line 228 of file task.h.

◆ seqno_

uint64_t Task::seqno_
private

Stores the sequence number.

Definition at line 214 of file task.h.

◆ state_

State Task::state_
private

Stores a state of the task.

Definition at line 208 of file task.h.

◆ task_cancel_

bool Task::task_cancel_
private

Determines if the task's execution was canceled.

Definition at line 221 of file task.h.

◆ task_code_id_

int Task::task_code_id_
private

The code path executed by the task.

Definition at line 198 of file task.h.

◆ task_data_id_

int Task::task_data_id_
private

The dataset id within a code path.

Definition at line 201 of file task.h.

◆ task_impl_

tbb::task* Task::task_impl_
private

A pointer to an Intel TBB object storing low-level information to manage the task.

Definition at line 205 of file task.h.

◆ task_recycle_

bool Task::task_recycle_
private

Determines if the task must be rescheduled (reused) after its completion.

Definition at line 218 of file task.h.

◆ tbb_state_

TbbState Task::tbb_state_
private

Stores a state of the TBB object.

Definition at line 211 of file task.h.

◆ waitq_hook_

boost::intrusive::list_member_hook Task::waitq_hook_
private

Definition at line 239 of file task.h.


The documentation for this class was generated from the following files: