5 #ifndef ctrlplane_task_h
6 #define ctrlplane_task_h
8 #include <boost/scoped_ptr.hpp>
9 #include <boost/intrusive/list.hpp>
12 #include <tbb/mutex.h>
13 #include <tbb/reader_writer_lock.h>
15 #include <tbb/task_scheduler_init.h>
20 class SandeshTaskScheduler;
103 Task(
int task_id,
int task_instance);
109 virtual bool Run() = 0;
180 typedef boost::function<void(
const char *file_name, uint32_t line_no,
181 const Task *
task,
const char *description,
272 bool IsEmpty(
bool running_only =
false);
292 void Log(
const char *file_name, uint32_t line_no,
const Task *
task,
293 const char *description, uint64_t delay);
320 uint64_t inactivity_time_msec,
321 uint64_t poll_interval_msec);
int task_id_
The code path executed by the task.
void DisableTaskEntry(int task_id, int instance_id)
void EnableMonitor(EventManager *evm, uint64_t tbb_keepawake_time_msec, uint64_t inactivity_time_msec, uint64_t poll_interval_msec)
Enable Task monitoring.
std::vector< TaskGroup * > TaskGroupDb
int wait_count_
Number of entries in waitq.
TaskMonitor * task_monitor_
const TaskTbbKeepAwake * tbb_awake_task() const
TaskEntry * QueryTaskEntry(int task_id, int instance_id)
Query TaskEntry for a task-id and task-instance.
static const int kVectorGrowSize
void GetSandeshData(SandeshTaskScheduler *resp, bool summary)
void EnableTaskEntry(int task_id, int instance_id)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
static int GetDefaultThreadCount()
static void Initialize(uint32_t thread_count=0, EventManager *evm=NULL)
static Task * Running()
Returns a pointer to the current task the code is executing under.
TaskEntry * GetTaskEntry(int task_id, int instance_id)
Get TaskGroup for a task_id. Grows task_entry_db_ if necessary.
uint64_t total_tasks_completed_
Number of total tasks ran.
static const int kTaskInstanceAny
uint32_t schedule_delay() const
static boost::scoped_ptr< TaskScheduler > singleton_
TaskStats * GetTaskStats(int task_id)
tbb::reader_writer_lock id_map_mutex_
DISALLOW_COPY_AND_ASSIGN(Task)
void SetPolicy(int task_id, TaskPolicy &policy)
Sets the task exclusion policy. Adds policy entries for the task Examples:
uint32_t schedule_delay_
Log if time between enqueue and task-execute exceeds the delay.
void SetTbbState(TbbState s)
uint32_t execute_delay() const
DISALLOW_COPY_AND_ASSIGN(TaskScheduler)
uint32_t execute_delay() const
bool measure_delay() const
virtual void OnTaskCancel()
Called on task exit, if it is marked for cancellation. If the user wants to do any cleanup on task ca...
TaskGroupDb task_group_db_
TaskGroup * QueryTaskGroup(int task_id)
Query TaskGroup for a task_id.Assumes valid entry is present for task_id.
void Start()
Starts scheduling of all tasks.
TaskStats * GetTaskGroupStats(int task_id)
void set_event_manager(EventManager *evm)
int run_count_
Number of entries currently running.
TaskScheduler(int thread_count=0)
TaskScheduler constructor. TBB assumes it can use the "thread" invoking tbb::scheduler can be used fo...
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...
TaskGroup maintains per <task-id> information including,.
int match_id
must be a valid id (>= 0).
int GetTaskId(const std::string &name)
A class maintaning information for every <task, instance>
void StartTask(TaskScheduler *scheduler)
Starts execution of a task.
int CountThreadsPerPid(pid_t pid)
Platfrom-dependent subroutine in Linux and FreeBSD implementations, used only in TaskScheduler::WaitF...
void EnableLatencyThresholds(uint32_t execute, uint32_t schedule)
Enable logging of tasks exceeding configured latency.
virtual bool Run()=0
Code to execute. Returns true if task is completed. Return false to reschedule the task...
bool track_run_time() const
uint64_t last_exit_time_
Number of time stamp of latest exist.
void Print()
Debug print routine.
void WaitForTerminateCompletion()
void SetTrackRunTime(bool value)
static void SetThreadAmpFactor(int n)
following function allows one to increase max num of threads used by TBB
void RegisterLog(LogFn fn)
const TaskMonitor * task_monitor() const
tbb::task_scheduler_init task_scheduler_
bool task_cancelled() const
static TaskScheduler * GetInstance()
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq...
A private class used to implement tbb::task An object is created when task is ready for execution and...
boost::intrusive::list_member_hook waitq_hook_
virtual std::string Description() const =0
TaskTbbKeepAwake * tbb_awake_task_
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.
void SetSeqNo(uint64_t seqno)
void ModifyTbbKeepAwakeTimeout(uint32_t timeout)
int GetTaskInstance() const
int defer_count_
Number of entries in deferq.
void SetRunningTask(Task *)
This function should not be called in production code. It is only for unit testing to control current...
void ClearTaskGroupStats(int task_id)
void SetMaxThreadCount(int n)
Force number of threads.
std::vector< TaskExclusion > TaskPolicy
~TaskScheduler()
Frees up the task_entry_db_ allocated for scheduler.
uint64_t cancel_count() const
uint64_t enqueue_count_
Number of tasks enqueued.
uint32_t execute_delay_
Log if time taken to execute exceeds the delay.
TaskGroup * GetTaskGroup(int task_id)
Get TaskGroup for a task_id. Grows task_entry_db_ if necessary.
uint64_t enqueue_time() const
std::string GetTaskName(int task_id) const
uint64_t GetSeqno() const
void DisableTaskGroup(int task_id)
void Log(const char *file_name, uint32_t line_no, const Task *task, const char *description, uint64_t delay)
static int ThreadAmpFactor_
following variable allows one to increase max num of threads used by TBB
TaskExclusion(int task_id, int instance_id)
static bool ShouldUseSpawn()
Task(int task_id, int task_instance)
void Stop()
Stops scheduling of all tasks.
uint64_t schedule_time() const
TaskExclusion(int task_id)
int task_instance_
The dataset id within a code path.
void EnableTaskGroup(int task_id)
std::map< std::string, int > TaskIdMap
friend std::ostream & operator<<(std::ostream &out, const Task &task)
bool use_spawn_
Use spawn() to run a tbb::task instead of enqueue()
void SetLatencyThreshold(const std::string &name, uint32_t execute, uint32_t schedule)
uint64_t done_count() const
void OnTaskExit(Task *task)
Method invoked on exit of a Task. Exit of a task can potentially start tasks in pendingq.
bool IsTaskGroupEmpty(int task_id) const
Check if there are any Tasks in the given TaskGroup. Assumes that all task ids are mutually exclusive...
Task is a wrapper over tbb::task to support policies.
uint32_t schedule_delay() const
void EnqueueUnLocked(Task *task)
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
int HardwareThreadCount()
boost::function< void(const char *file_name, uint32_t line_no, const Task *task, const char *description, uint64_t delay)> LogFn
int match_instance
-1 (wildcard) or user specified id.
void ClearTaskStats(int task_id)