OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TaskScheduler Class Reference

The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is added to the run queue or the pending queue depending as to whether there is a runable or pending task ahead of it that violates the mutual exclusion policies. When tasks exit the scheduler re-examines the tasks on the pending queue which may now be runnable. It is important that this process is efficient such that exit events do not scan tasks that are not waiting on a particular task id or task instance to have a 0 count. More...

#include <task.h>

Collaboration diagram for TaskScheduler:

Public Types

enum  CancelReturnCode { CANCELLED, FAILED, QUEUED }
 
typedef boost::function< void(const
char *file_name, uint32_t
line_no, const Task *task,
const char *description,
uint64_t delay)> 
LogFn
 

Public Member Functions

 TaskScheduler (int thread_count=0)
 TaskScheduler constructor. TBB assumes it can use the "thread" invoking tbb::scheduler can be used for task scheduling. But, in our case we dont want "main" thread to be part of tbb. So, initialize TBB with one thread more than its default. More...
 
 ~TaskScheduler ()
 Frees up the task_entry_db_ allocated for scheduler. More...
 
void Enqueue (Task *task)
 Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq. Enqueueing may may result in the task being immedietly added to the run queue or to a pending queue. Tasks may not be added to the run queue in violation of their exclusion policy. More...
 
void EnqueueUnLocked (Task *task)
 
CancelReturnCode Cancel (Task *task)
 Cancels a Task that can be in RUN/WAIT state. The caller needs to ensure that the task exists when Cancel() is invoked. More...
 
void SetPolicy (int task_id, TaskPolicy &policy)
 Sets the task exclusion policy. Adds policy entries for the task Examples: More...
 
bool GetRunStatus ()
 
int GetTaskId (const std::string &name)
 
std::string GetTaskName (int task_id) const
 
TaskStatsGetTaskGroupStats (int task_id)
 
TaskStatsGetTaskStats (int task_id)
 
TaskStatsGetTaskStats (int task_id, int instance_id)
 
void ClearTaskGroupStats (int task_id)
 
void ClearTaskStats (int task_id)
 
void ClearTaskStats (int task_id, int instance_id)
 
TaskGroupGetTaskGroup (int task_id)
 Get TaskGroup for a task_id. Grows task_entry_db_ if necessary. More...
 
TaskGroupQueryTaskGroup (int task_id)
 Query TaskGroup for a task_id.Assumes valid entry is present for task_id. More...
 
bool IsTaskGroupEmpty (int task_id) const
 Check if there are any Tasks in the given TaskGroup. Assumes that all task ids are mutually exclusive with bgp::Config. More...
 
TaskEntryGetTaskEntry (int task_id, int instance_id)
 Get TaskGroup for a task_id. Grows task_entry_db_ if necessary. More...
 
TaskEntryQueryTaskEntry (int task_id, int instance_id)
 Query TaskEntry for a task-id and task-instance. More...
 
void OnTaskExit (Task *task)
 Method invoked on exit of a Task. Exit of a task can potentially start tasks in pendingq. More...
 
void Stop ()
 Stops scheduling of all tasks. More...
 
void Start ()
 Starts scheduling of all tasks. More...
 
void Print ()
 Debug print routine. More...
 
bool IsEmpty (bool running_only=false)
 Returns true if there are no tasks running and/or enqueued If running_only is true, enqueued tasks are ignored i.e. return true if there are no running tasks. Ignore TaskGroup or TaskEntry if it is disabled. More...
 
void Terminate ()
 
int HardwareThreadCount ()
 
uint64_t enqueue_count () const
 
uint64_t done_count () const
 
uint64_t cancel_count () const
 
void SetMaxThreadCount (int n)
 Force number of threads. More...
 
void GetSandeshData (SandeshTaskScheduler *resp, bool summary)
 
void Log (const char *file_name, uint32_t line_no, const Task *task, const char *description, uint64_t delay)
 
void RegisterLog (LogFn fn)
 
void SetTrackRunTime (bool value)
 
bool track_run_time () const
 
void EnableLatencyThresholds (uint32_t execute, uint32_t schedule)
 Enable logging of tasks exceeding configured latency. More...
 
uint32_t schedule_delay () const
 
uint32_t execute_delay () const
 
bool measure_delay () const
 
void SetLatencyThreshold (const std::string &name, uint32_t execute, uint32_t schedule)
 
uint32_t schedule_delay (Task *task) const
 
uint32_t execute_delay (Task *task) const
 
void set_event_manager (EventManager *evm)
 
void DisableTaskGroup (int task_id)
 
void EnableTaskGroup (int task_id)
 
void DisableTaskEntry (int task_id, int instance_id)
 
void EnableTaskEntry (int task_id, int instance_id)
 
void ModifyTbbKeepAwakeTimeout (uint32_t timeout)
 
void EnableMonitor (EventManager *evm, uint64_t tbb_keepawake_time_msec, uint64_t inactivity_time_msec, uint64_t poll_interval_msec)
 Enable Task monitoring. More...
 
const TaskMonitortask_monitor () const
 
const TaskTbbKeepAwaketbb_awake_task () const
 
bool use_spawn () const
 

Static Public Member Functions

static void Initialize (uint32_t thread_count=0, EventManager *evm=NULL)
 
static TaskSchedulerGetInstance ()
 
static int GetThreadCount (int thread_count=0)
 Get number of tbb worker threads. For testing purposes only. Limit the number of tbb worker threads. More...
 
static bool ShouldUseSpawn ()
 
static int GetDefaultThreadCount ()
 
static void SetThreadAmpFactor (int n)
 following function allows one to increase max num of threads used by TBB More...
 

Private Types

typedef std::vector< TaskGroup * > TaskGroupDb
 
typedef std::map< std::string,
int > 
TaskIdMap
 

Private Member Functions

void SetRunningTask (Task *)
 This function should not be called in production code. It is only for unit testing to control current running task This function modifies the running task as specified by the input. More...
 
void ClearRunningTask ()
 
void WaitForTerminateCompletion ()
 
int CountThreadsPerPid (pid_t pid)
 Platfrom-dependent subroutine in Linux and FreeBSD implementations, used only in TaskScheduler::WaitForTerminateCompletion(). In Linux, make sure that all the [tbb] threads launched have completely exited. We do so by looking for the Threads count of this process in /proc/<pid>/status. More...
 
 DISALLOW_COPY_AND_ASSIGN (TaskScheduler)
 

Private Attributes

bool use_spawn_
 Use spawn() to run a tbb::task instead of enqueue() More...
 
TaskEntrystop_entry_
 
tbb::task_scheduler_init task_scheduler_
 
tbb::mutex mutex_
 
bool running_
 
uint64_t seqno_
 
TaskGroupDb task_group_db_
 
tbb::reader_writer_lock id_map_mutex_
 
TaskIdMap id_map_
 
int id_max_
 
LogFn log_fn_
 
int hw_thread_count_
 
bool track_run_time_
 
bool measure_delay_
 
uint32_t schedule_delay_
 Log if time between enqueue and task-execute exceeds the delay. More...
 
uint32_t execute_delay_
 Log if time taken to execute exceeds the delay. More...
 
uint64_t enqueue_count_
 
uint64_t done_count_
 
uint64_t cancel_count_
 
EventManagerevm_
 
TaskTbbKeepAwaketbb_awake_task_
 
TaskMonitortask_monitor_
 

Static Private Attributes

static const int kVectorGrowSize = 16
 
static boost::scoped_ptr
< TaskScheduler
singleton_
 
static int ThreadAmpFactor_ = 1
 following variable allows one to increase max num of threads used by TBB More...
 

Friends

class ConcurrencyScope
 

Detailed Description

The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is added to the run queue or the pending queue depending as to whether there is a runable or pending task ahead of it that violates the mutual exclusion policies. When tasks exit the scheduler re-examines the tasks on the pending queue which may now be runnable. It is important that this process is efficient such that exit events do not scan tasks that are not waiting on a particular task id or task instance to have a 0 count.

Definition at line 178 of file task.h.

Member Typedef Documentation

typedef boost::function<void(const char *file_name, uint32_t line_no, const Task *task, const char *description, uint64_t delay)> TaskScheduler::LogFn

Definition at line 182 of file task.h.

typedef std::vector<TaskGroup *> TaskScheduler::TaskGroupDb
private

Definition at line 332 of file task.h.

typedef std::map<std::string, int> TaskScheduler::TaskIdMap
private

Definition at line 333 of file task.h.

Member Enumeration Documentation

Enumerator
CANCELLED 
FAILED 
QUEUED 

Definition at line 205 of file task.h.

Constructor & Destructor Documentation

TaskScheduler::TaskScheduler ( int  thread_count = 0)

TaskScheduler constructor. TBB assumes it can use the "thread" invoking tbb::scheduler can be used for task scheduling. But, in our case we dont want "main" thread to be part of tbb. So, initialize TBB with one thread more than its default.

Definition at line 444 of file task.cc.

Here is the call graph for this function:

TaskScheduler::~TaskScheduler ( )

Frees up the task_entry_db_ allocated for scheduler.

Definition at line 455 of file task.cc.

Member Function Documentation

TaskScheduler::CancelReturnCode TaskScheduler::Cancel ( Task task)

Cancels a Task that can be in RUN/WAIT state. The caller needs to ensure that the task exists when Cancel() is invoked.

Definition at line 699 of file task.cc.

Here is the call graph for this function:

uint64_t TaskScheduler::cancel_count ( ) const
inline

Definition at line 287 of file task.h.

void TaskScheduler::ClearRunningTask ( )
private

Definition at line 1006 of file task.cc.

void TaskScheduler::ClearTaskGroupStats ( int  task_id)

Definition at line 875 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::ClearTaskStats ( int  task_id)

Definition at line 883 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::ClearTaskStats ( int  task_id,
int  instance_id 
)

Definition at line 891 of file task.cc.

Here is the call graph for this function:

int TaskScheduler::CountThreadsPerPid ( pid_t  pid)
private

Platfrom-dependent subroutine in Linux and FreeBSD implementations, used only in TaskScheduler::WaitForTerminateCompletion(). In Linux, make sure that all the [tbb] threads launched have completely exited. We do so by looking for the Threads count of this process in /proc/<pid>/status.

In FreeBSD use libprocstat to check how many threads is running in specific process.

Definition at line 923 of file task.cc.

void TaskScheduler::DisableTaskEntry ( int  task_id,
int  instance_id 
)

Definition at line 1032 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::DisableTaskGroup ( int  task_id)

Definition at line 1015 of file task.cc.

Here is the call graph for this function:

TaskScheduler::DISALLOW_COPY_AND_ASSIGN ( TaskScheduler  )
private
uint64_t TaskScheduler::done_count ( ) const
inline

Definition at line 286 of file task.h.

void TaskScheduler::EnableLatencyThresholds ( uint32_t  execute,
uint32_t  schedule 
)

Enable logging of tasks exceeding configured latency.

Definition at line 595 of file task.cc.

void TaskScheduler::EnableMonitor ( EventManager evm,
uint64_t  tbb_keepawake_time_msec,
uint64_t  inactivity_time_msec,
uint64_t  poll_interval_msec 
)

Enable Task monitoring.

Definition at line 511 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::EnableTaskEntry ( int  task_id,
int  instance_id 
)

Definition at line 1037 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::EnableTaskGroup ( int  task_id)

Definition at line 1025 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::Enqueue ( Task task)

Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq. Enqueueing may may result in the task being immedietly added to the run queue or to a pending queue. Tasks may not be added to the run queue in violation of their exclusion policy.

Definition at line 636 of file task.cc.

Here is the call graph for this function:

uint64_t TaskScheduler::enqueue_count ( ) const
inline

Definition at line 285 of file task.h.

void TaskScheduler::EnqueueUnLocked ( Task task)

Definition at line 642 of file task.cc.

Here is the call graph for this function:

uint32_t TaskScheduler::execute_delay ( ) const
inline

Definition at line 302 of file task.h.

uint32_t TaskScheduler::execute_delay ( Task task) const

Definition at line 541 of file task.cc.

Here is the call graph for this function:

int TaskScheduler::GetDefaultThreadCount ( )
static

Definition at line 429 of file task.cc.

TaskScheduler * TaskScheduler::GetInstance ( )
static

Definition at line 547 of file task.cc.

Here is the call graph for this function:

bool TaskScheduler::GetRunStatus ( )
inline

Definition at line 227 of file task.h.

void TaskScheduler::GetSandeshData ( SandeshTaskScheduler *  resp,
bool  summary 
)

Definition at line 1622 of file task.cc.

Here is the call graph for this function:

TaskEntry * TaskScheduler::GetTaskEntry ( int  task_id,
int  instance_id 
)

Get TaskGroup for a task_id. Grows task_entry_db_ if necessary.

Definition at line 583 of file task.cc.

Here is the call graph for this function:

TaskGroup * TaskScheduler::GetTaskGroup ( int  task_id)

Get TaskGroup for a task_id. Grows task_entry_db_ if necessary.

Definition at line 554 of file task.cc.

TaskStats * TaskScheduler::GetTaskGroupStats ( int  task_id)

Definition at line 899 of file task.cc.

Here is the call graph for this function:

int TaskScheduler::GetTaskId ( const std::string &  name)

Definition at line 856 of file task.cc.

std::string TaskScheduler::GetTaskName ( int  task_id) const

Definition at line 846 of file task.cc.

TaskStats * TaskScheduler::GetTaskStats ( int  task_id)

Definition at line 907 of file task.cc.

Here is the call graph for this function:

TaskStats * TaskScheduler::GetTaskStats ( int  task_id,
int  instance_id 
)

Definition at line 915 of file task.cc.

Here is the call graph for this function:

int TaskScheduler::GetThreadCount ( int  thread_count = 0)
static

Get number of tbb worker threads. For testing purposes only. Limit the number of tbb worker threads.

Definition at line 407 of file task.cc.

int TaskScheduler::HardwareThreadCount ( )
inline

Definition at line 276 of file task.h.

void TaskScheduler::Initialize ( uint32_t  thread_count = 0,
EventManager evm = NULL 
)
static

Definition at line 478 of file task.cc.

Here is the call graph for this function:

bool TaskScheduler::IsEmpty ( bool  running_only = false)

Returns true if there are no tasks running and/or enqueued If running_only is true, enqueued tasks are ignored i.e. return true if there are no running tasks. Ignore TaskGroup or TaskEntry if it is disabled.

Definition at line 823 of file task.cc.

Here is the call graph for this function:

bool TaskScheduler::IsTaskGroupEmpty ( int  task_id) const

Check if there are any Tasks in the given TaskGroup. Assumes that all task ids are mutually exclusive with bgp::Config.

Definition at line 574 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::Log ( const char *  file_name,
uint32_t  line_no,
const Task task,
const char *  description,
uint64_t  delay 
)

Definition at line 523 of file task.cc.

bool TaskScheduler::measure_delay ( ) const
inline

Definition at line 304 of file task.h.

void TaskScheduler::ModifyTbbKeepAwakeTimeout ( uint32_t  timeout)

Definition at line 505 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::OnTaskExit ( Task task)

Method invoked on exit of a Task. Exit of a task can potentially start tasks in pendingq.

Definition at line 762 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::Print ( )

Debug print routine.

Definition at line 808 of file task.cc.

Here is the call graph for this function:

TaskEntry * TaskScheduler::QueryTaskEntry ( int  task_id,
int  instance_id 
)

Query TaskEntry for a task-id and task-instance.

Definition at line 588 of file task.cc.

Here is the call graph for this function:

TaskGroup * TaskScheduler::QueryTaskGroup ( int  task_id)

Query TaskGroup for a task_id.Assumes valid entry is present for task_id.

Definition at line 570 of file task.cc.

void TaskScheduler::RegisterLog ( LogFn  fn)

Definition at line 531 of file task.cc.

uint32_t TaskScheduler::schedule_delay ( ) const
inline

Definition at line 301 of file task.h.

uint32_t TaskScheduler::schedule_delay ( Task task) const

Definition at line 535 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::set_event_manager ( EventManager evm)

Definition at line 493 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::SetLatencyThreshold ( const std::string &  name,
uint32_t  execute,
uint32_t  schedule 
)

Definition at line 602 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::SetMaxThreadCount ( int  n)

Force number of threads.

void TaskScheduler::SetPolicy ( int  task_id,
TaskPolicy policy 
)

Sets the task exclusion policy. Adds policy entries for the task Examples:

  • Policy <tid0> => <tid1, -1> <tid2, inst2> will result in following:
    • task_db_[tid0] : Rule <tid1, -1> is added to policyq
    • task_group_db_[tid0, inst2] : Rule <tid2, inst2> is added to policyq
  • The symmetry of policy will result in following additional rules,
    • task_db_[tid1] : Rule <tid0, -1> is added to policyq
    • task_group_db_[tid2, inst2] : Rule <tid0, inst2> is added to policyq

Definition at line 610 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::SetRunningTask ( Task unit_test)
private

This function should not be called in production code. It is only for unit testing to control current running task This function modifies the running task as specified by the input.

Definition at line 1001 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::SetThreadAmpFactor ( int  n)
static

following function allows one to increase max num of threads used by TBB

Definition at line 1011 of file task.cc.

void TaskScheduler::SetTrackRunTime ( bool  value)
inline

Definition at line 296 of file task.h.

bool TaskScheduler::ShouldUseSpawn ( )
static

Definition at line 433 of file task.cc.

void TaskScheduler::Start ( )

Starts scheduling of all tasks.

Definition at line 798 of file task.cc.

Here is the call graph for this function:

void TaskScheduler::Stop ( )

Stops scheduling of all tasks.

Definition at line 792 of file task.cc.

const TaskMonitor* TaskScheduler::task_monitor ( ) const
inline

Definition at line 322 of file task.h.

const TaskTbbKeepAwake* TaskScheduler::tbb_awake_task ( ) const
inline

Definition at line 323 of file task.h.

void TaskScheduler::Terminate ( )

Definition at line 978 of file task.cc.

Here is the call graph for this function:

bool TaskScheduler::track_run_time ( ) const
inline

Definition at line 297 of file task.h.

bool TaskScheduler::use_spawn ( ) const
inline

Definition at line 324 of file task.h.

void TaskScheduler::WaitForTerminateCompletion ( )
private

Definition at line 952 of file task.cc.

Here is the call graph for this function:

Friends And Related Function Documentation

friend class ConcurrencyScope
friend

Definition at line 331 of file task.h.

Member Data Documentation

uint64_t TaskScheduler::cancel_count_
private

Definition at line 389 of file task.h.

uint64_t TaskScheduler::done_count_
private

Definition at line 388 of file task.h.

uint64_t TaskScheduler::enqueue_count_
private

Definition at line 387 of file task.h.

EventManager* TaskScheduler::evm_
private

Definition at line 390 of file task.h.

uint32_t TaskScheduler::execute_delay_
private

Log if time taken to execute exceeds the delay.

Definition at line 385 of file task.h.

int TaskScheduler::hw_thread_count_
private

Definition at line 376 of file task.h.

TaskIdMap TaskScheduler::id_map_
private

Definition at line 372 of file task.h.

tbb::reader_writer_lock TaskScheduler::id_map_mutex_
private

Definition at line 371 of file task.h.

int TaskScheduler::id_max_
private

Definition at line 373 of file task.h.

const int TaskScheduler::kVectorGrowSize = 16
staticprivate

Definition at line 335 of file task.h.

LogFn TaskScheduler::log_fn_
private

Definition at line 375 of file task.h.

bool TaskScheduler::measure_delay_
private

Definition at line 379 of file task.h.

tbb::mutex TaskScheduler::mutex_
mutableprivate

Definition at line 366 of file task.h.

bool TaskScheduler::running_
private

Definition at line 367 of file task.h.

uint32_t TaskScheduler::schedule_delay_
private

Log if time between enqueue and task-execute exceeds the delay.

Definition at line 382 of file task.h.

uint64_t TaskScheduler::seqno_
private

Definition at line 368 of file task.h.

boost::scoped_ptr< TaskScheduler > TaskScheduler::singleton_
staticprivate

Definition at line 336 of file task.h.

TaskEntry* TaskScheduler::stop_entry_
private

Definition at line 363 of file task.h.

TaskGroupDb TaskScheduler::task_group_db_
private

Definition at line 369 of file task.h.

TaskMonitor* TaskScheduler::task_monitor_
private

Definition at line 397 of file task.h.

tbb::task_scheduler_init TaskScheduler::task_scheduler_
private

Definition at line 365 of file task.h.

TaskTbbKeepAwake* TaskScheduler::tbb_awake_task_
private

Definition at line 396 of file task.h.

int TaskScheduler::ThreadAmpFactor_ = 1
staticprivate

following variable allows one to increase max num of threads used by TBB

Definition at line 394 of file task.h.

bool TaskScheduler::track_run_time_
private

Definition at line 378 of file task.h.

bool TaskScheduler::use_spawn_
private

Use spawn() to run a tbb::task instead of enqueue()

Definition at line 362 of file task.h.


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