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);
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);
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_
tbb::reader_writer_lock id_map_mutex_
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,...
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 wrapper over tbb::task to support policies.
int GetTaskInstance() const
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)
void SetTbbState(TbbState s)
int task_id_
The code path executed by the task.
static const int kTaskInstanceAny
int task_instance_
The dataset id within a code path.
void StartTask(TaskScheduler *scheduler)
Starts execution of a task.
bool task_cancelled() const
DISALLOW_COPY_AND_ASSIGN(Task)
uint64_t schedule_time() const
void SetSeqNo(uint64_t seqno)
uint32_t schedule_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...
uint64_t GetSeqno() const
uint64_t enqueue_time() const
Task(int task_id, int task_instance)
virtual bool Run()=0
Code to execute. Returns true if task is completed. Return false to reschedule the task.
virtual std::string Description() const =0
uint32_t execute_delay() const
boost::intrusive::list_member_hook waitq_hook_
int match_id
must be a valid id (>= 0).
int match_instance
-1 (wildcard) or user specified id.
TaskExclusion(int task_id, int instance_id)
TaskExclusion(int task_id)
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