5 #ifndef ctrlplane_task_h
6 #define ctrlplane_task_h
8 #include <boost/scoped_ptr.hpp>
9 #include <boost/intrusive/list.hpp>
11 #include <shared_mutex>
15 #define TBB_SUPPRESS_DEPRECATED_MESSAGES 1
17 #include <tbb/task_scheduler_init.h>
22 class SandeshTaskScheduler;
306 typedef boost::function<void(
const char *file_name, uint32_t line_no,
307 const Task *
task,
const char *description,
398 bool IsEmpty(
bool running_only =
false);
418 void Log(
const char *file_name, uint32_t line_no,
const Task *
task,
419 const char *description, uint64_t delay);
446 uint64_t inactivity_time_msec,
447 uint64_t poll_interval_msec);
A class maintaning information for every <task, instance>
TaskGroup maintains per <task-id> information including,.
A private class used to implement tbb::task An object is created when task is ready for execution and...
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
void EnqueueUnLocked(Task *task)
bool IsTaskGroupEmpty(int task_id) const
Check if there are any Tasks in the given TaskGroup. Assumes that all task ids are mutually exclusive...
void Stop()
Stops scheduling of all tasks.
TaskTbbKeepAwake * tbb_awake_task_
static boost::scoped_ptr< TaskScheduler > singleton_
void EnableTaskGroup(int task_id)
int GetTaskId(const std::string &name)
static int GetThreadCount(int thread_count=0)
Get number of tbb worker threads. For testing purposes only. Limit the number of tbb worker threads.
uint64_t enqueue_count() const
tbb::task_scheduler_init task_scheduler_
void SetMaxThreadCount(int n)
Force number of threads.
void Log(const char *file_name, uint32_t line_no, const Task *task, const char *description, uint64_t delay)
TaskGroup * QueryTaskGroup(int task_id)
Query TaskGroup for a task_id.Assumes valid entry is present for task_id.
static void SetThreadAmpFactor(int n)
following function allows one to increase max num of threads used by TBB
bool measure_delay() const
boost::function< void(const char *file_name, uint32_t line_no, const Task *task, const char *description, uint64_t delay)> LogFn
uint64_t cancel_count() const
TaskMonitor * task_monitor_
void GetSandeshData(SandeshTaskScheduler *resp, bool summary)
int CountThreadsPerPid(pid_t pid)
Platfrom-dependent subroutine in Linux and FreeBSD implementations, used only in TaskScheduler::WaitF...
TaskScheduler(int thread_count=0)
TaskScheduler constructor. TBB assumes it can use the "thread" invoking tbb::scheduler can be used fo...
void WaitForTerminateCompletion()
void DisableTaskEntry(int task_id, int instance_id)
void DisableTaskGroup(int task_id)
uint32_t execute_delay_
Log if time taken to execute exceeds the delay.
void ModifyTbbKeepAwakeTimeout(uint32_t timeout)
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
static int GetDefaultThreadCount()
void SetTrackRunTime(bool value)
TaskStats * GetTaskGroupStats(int task_id)
std::map< std::string, int > TaskIdMap
void EnableMonitor(EventManager *evm, uint64_t tbb_keepawake_time_msec, uint64_t inactivity_time_msec, uint64_t poll_interval_msec)
Enable Task monitoring.
TaskGroupDb task_group_db_
~TaskScheduler()
Frees up the task_entry_db_ allocated for scheduler.
static void Initialize(uint32_t thread_count=0, EventManager *evm=NULL)
void SetPolicy(int task_id, TaskPolicy &policy)
Sets the task exclusion policy. Adds policy entries for the task Examples:
std::string GetTaskName(int task_id) const
void EnableTaskEntry(int task_id, int instance_id)
uint64_t done_count() const
TaskEntry * GetTaskEntry(int task_id, int instance_id)
Get TaskGroup for a task_id. Grows task_entry_db_ if necessary.
void SetLatencyThreshold(const std::string &name, uint32_t execute, uint32_t schedule)
DISALLOW_COPY_AND_ASSIGN(TaskScheduler)
static TaskScheduler * GetInstance()
static int ThreadAmpFactor_
following variable allows one to increase max num of threads used by TBB
void Start()
Starts scheduling of all tasks.
TaskGroup * GetTaskGroup(int task_id)
Get TaskGroup for a task_id. Grows task_entry_db_ if necessary.
std::vector< TaskGroup * > TaskGroupDb
TaskEntry * QueryTaskEntry(int task_id, int instance_id)
Query TaskEntry for a task-id and task-instance.
int HardwareThreadCount()
uint32_t schedule_delay_
Log if time between enqueue and task-execute exceeds the delay.
void ClearTaskStats(int task_id)
TaskStats * GetTaskStats(int task_id)
void OnTaskExit(Task *task)
Method invoked on exit of a Task. Exit of a task can potentially start tasks in pendingq.
bool track_run_time() const
bool use_spawn_
Use spawn() to run a tbb::task instead of enqueue()
void RegisterLog(LogFn fn)
static bool ShouldUseSpawn()
void set_event_manager(EventManager *evm)
uint32_t schedule_delay() const
void Print()
Debug print routine.
uint32_t execute_delay() const
const TaskMonitor * task_monitor() const
const TaskTbbKeepAwake * tbb_awake_task() const
CancelReturnCode Cancel(Task *task)
Cancels a Task that can be in RUN/WAIT state. The caller needs to ensure that the task exists when Ca...
static const int kVectorGrowSize
bool IsEmpty(bool running_only=false)
Returns true if there are no tasks running and/or enqueued If running_only is true,...
std::shared_mutex id_map_mutex_
void ClearTaskGroupStats(int task_id)
void EnableLatencyThresholds(uint32_t execute, uint32_t schedule)
Enable logging of tasks exceeding configured latency.
void SetRunningTask(Task *)
This function should not be called in production code. It is only for unit testing to control current...
Task is a class to describe a computational task within OpenSDN control plane applications....
static Task * Running()
Returns a pointer to the current task the code is executing under.
friend std::ostream & operator<<(std::ostream &out, const Task &task)
Provides access to private members of a task for the output stream redirection operator.
static const int kTaskInstanceAny
Specifies value for wildcard (any or *) task data ID.
uint64_t seqno() const
Returns the sequence number of this task.
bool task_recycle_
Determines if the task must be rescheduled (reused) after its completion.
void state(State s)
Sets a state for this task.
void StartTask(TaskScheduler *scheduler)
Starts execution of a task.
TbbState tbb_state_
Stores a state of the TBB object.
void tbb_state(TbbState s)
Sets a TBB state for the task.
uint64_t seqno_
Stores the sequence number.
bool task_cancelled() const
Returns true if the task has been canceled.
DISALLOW_COPY_AND_ASSIGN(Task)
uint64_t schedule_time() const
Returns the time when the task execution was started.
State state_
Stores a state of the task.
virtual ~Task()
Destroys a task.
void set_task_recycle()
Marks this task for recycle.
int task_data_id_
The dataset id within a code path.
uint32_t schedule_delay() const
Returns the time threshold for time difference between moments when the task was started and when it ...
virtual void OnTaskCancel()
Called on task exit, if it is marked for cancellation. If the user wants to do any cleanup on task ca...
int task_code_id() const
Returns the code ID of this task.
tbb::task * task_impl_
A pointer to an Intel TBB object storing low-level information to manage the task.
int task_data_id() const
Returns the data ID of this task.
uint64_t enqueue_time() const
Returns the time when the task was enqueued for execution.
Task(int task_id, int task_data_id)
Creates a new task with the given values of task code ID and task data ID.
uint64_t schedule_time_
Contains the time when the task was started.
TbbState
Describes states of a task according to TBB library.
void set_task_complete()
Marks this task as completed (forbids recycling)
void seqno(uint64_t seqno)
Sets sequence number of the task.
virtual bool Run()=0
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
virtual std::string Description() const =0
Gives a description of the task.
bool task_cancel_
Determines if the task's execution was canceled.
uint32_t execute_delay_
Sets threshold for the task's execution time. If the threshold is exceeded, the event is logged.
State state() const
Returns a state value of a task.
uint32_t schedule_delay_
Sets threshold for delay between enqueueing and execution. If the threshold is exceeded,...
uint32_t execute_delay() const
Returns the threshold for the task execution duration.
int task_code_id_
The code path executed by the task.
uint64_t enqueue_time_
Contains the time when the task was enqueued for execution.
@ WAIT
A task is waiting in a queue.
@ RUN
A task is being run.
@ INIT
A task was initialized.
boost::intrusive::list_member_hook waitq_hook_
The class is used to specify a Task label for formulating a task exclusion list (an execution policy)...
int match_data_id
Specifies task data ID for a task execution policy. The value of -1 corresponds to wildcard (any).
TaskExclusion(int task_code_id)
Creates a new task exclusion from the given task code ID value and wildcard for task data ID.
TaskExclusion(int task_code_id, int task_data_id)
Creates a new task exclusion from the given task code ID and task data ID values.
int match_code_id
Specifies task code ID (must be a valid id >= 0) for a task execution policy.
uint64_t total_tasks_completed_
Number of total tasks ran.
uint64_t last_exit_time_
Number of time stamp of latest exist.
int run_count_
Number of entries currently running.
int defer_count_
Number of entries in deferq.
int wait_count_
Number of entries in waitq.
uint64_t enqueue_count_
Number of tasks enqueued.
std::vector< TaskExclusion > TaskPolicy
Defines a type to store an execution policy (a list of task exclusions).