13# include <click/cxxprotect.h>
16# include <sys/mutex.h>
18# include <click/cxxunprotect.h>
23#define PASS_GT(a, b) ((int)(a - b) > 0)
25typedef bool (*TaskCallback)(
Task *,
void *);
26typedef TaskCallback TaskHook CLICK_DEPRECATED;
49class Task :
private TaskLink {
public:
52 enum { STRIDE1 = 1U<<16, MAX_STRIDE = 1U<<31 };
53 enum { MAX_TICKETS = 1<<15, DEFAULT_TICKETS = 1<<10 };
55#if HAVE_ADAPTIVE_SCHEDULER
56 enum { MAX_UTILIZATION = 1000 };
180 return _status.is_scheduled;
191 _status.is_scheduled =
false;
207 _status.is_scheduled =
true;
209 if (_pending_nextptr.x < 2)
210 complete_schedule(0);
249 _status.is_scheduled =
true;
261 _status.is_scheduled =
false;
262 _status.is_strong_unscheduled =
true;
272 _status.is_strong_unscheduled =
false;
296#if HAVE_ADAPTIVE_SCHEDULER
297 inline unsigned runs()
const;
298 inline unsigned work_done()
const;
299 inline unsigned utilization()
const;
300 inline void clear_runs();
303 inline int cycles()
const;
304 inline unsigned cycle_runs()
const;
305 inline void update_cycles(
unsigned c);
309 inline TaskCallback hook() const CLICK_DEPRECATED;
310 inline
void *thunk() const CLICK_DEPRECATED;
326 int16_t home_thread_id;
327 uint8_t is_scheduled;
328 uint8_t is_strong_unscheduled;
336#if HAVE_ADAPTIVE_SCHEDULER
342 unsigned _cycle_runs;
353 Pending _pending_nextptr;
359#if CLICK_DEBUG_SCHEDULING
362 inline bool on_scheduled_list()
const;
363 inline bool on_pending_list()
const {
364 return _pending_nextptr.x != 0;
366 inline bool needs_cleanup()
const;
367#if CLICK_DEBUG_SCHEDULING
371 void add_pending(
bool always);
374 void complete_schedule(
RouterThread* process_pending_thread);
375 inline void remove_from_scheduled_list();
397 _stride(0), _tickets(-1),
399 _hook(f), _thunk(user_data),
400#if HAVE_ADAPTIVE_SCHEDULER
401 _runs(0), _work_done(0),
406 _thread(0), _owner(0)
408 _status.home_thread_id = tid;
409 _status.is_scheduled = _status.is_strong_unscheduled =
false;
410 _pending_nextptr.x = 0;
420 _stride(0), _tickets(-1),
423#if HAVE_ADAPTIVE_SCHEDULER
424 _runs(0), _work_done(0),
429 _thread(0), _owner(0)
431 _status.home_thread_id = -2;
432 _status.is_scheduled = _status.is_strong_unscheduled =
false;
433 _pending_nextptr.x = 0;
443Task::on_scheduled_list()
const
446 return _schedpos >= 0;
453Task::needs_cleanup()
const
460 b = _pending_nextptr.x;
462 }
while (a != _schedpos || b != _pending_nextptr.x);
463 return a >= 0 || b != 0;
469 b = _pending_nextptr.x;
471 }
while (a != _prev || b != _pending_nextptr.x);
472 return a != 0 || b != 0;
497 return _status.home_thread_id;
507Task::remove_from_scheduled_list()
509 if (on_scheduled_list()) {
511 Task *back = _thread->_task_heap.back().t;
512 _thread->_task_heap.pop_back();
513 if (_thread->_task_heap.size() > 0)
514 _thread->task_reheapify_from(_schedpos, back);
518 _next->_prev = _prev;
519 _prev->_next = _next;
557 _stride = STRIDE1 / n;
558 assert(_stride < MAX_STRIDE);
586 click_cycles_t start_cycles = click_get_cycles(),
587 start_child_cycles = _owner->_child_cycles;
596 work_done = _hook(
this, _thunk);
597#if HAVE_ADAPTIVE_SCHEDULER
599 _work_done += work_done;
602 click_cycles_t all_delta = click_get_cycles() - start_cycles,
603 own_delta = all_delta - (_owner->_child_cycles - start_child_cycles);
604 _owner->_task_calls += 1;
605 _owner->_task_own_cycles += own_delta;
610#if HAVE_ADAPTIVE_SCHEDULER
618Task::work_done()
const
624Task::utilization()
const
626 return (_runs ? (MAX_UTILIZATION * _work_done) / _runs : 0);
632 _runs = _work_done = 0;
640 return _cycles.unscaled_average();
644Task::cycle_runs()
const
650Task::update_cycles(
unsigned c)
An atomic 32-bit integer.
Base class for Click elements.
Definition element.hh:42
virtual bool run_task(Task *task)
Run the element's task.
Definition element.cc:3267
Router * router() const
Return the element's router.
Definition element.hh:515
A router configuration.
Definition router.hh:28
A set of Tasks scheduled on the same CPU.
Definition routerthread.hh:32
Represents a frequently-scheduled computational task.
Definition task.hh:49
int home_thread_id() const
Return the task's home thread ID.
Definition task.hh:495
void unschedule_notify(Element *e)
Unschedule the task, sending a notification.
Definition task.cc:147
void set_tickets(int n)
Set the task's ticket count.
Definition task.hh:550
void adjust_tickets(int delta)
Add delta to the Task's ticket count.
Definition task.hh:569
Master * master() const
Return the master where this task will be scheduled.
Definition task.cc:133
void move_thread(int new_thread_id)
Move the Task to a new home thread.
Definition task.cc:379
bool fire()
Fire the task by calling its callback function.
Definition task.hh:583
void reschedule()
Reschedule the task.
Definition task.hh:206
~Task()
Destroy a task.
Definition task.cc:126
bool scheduled() const
Return true iff the task is currently scheduled to run.
Definition task.hh:179
void strong_reschedule()
Reschedule the Task, undoing a prior strong_unschedule().
Definition task.hh:271
Element * element() const
Return the task's owning element.
Definition task.hh:108
bool initialized() const
Return true iff the task has been initialize()d.
Definition task.hh:437
void reschedule_notify(Element *e)
Reschedule the task, sending thread migration messages.
Definition task.cc:140
void * user_data() const
Return the task callback function's user data.
Definition task.hh:103
int tickets() const
Return the task's number of tickets.
Definition task.hh:537
Task(TaskCallback f, void *user_data, int tid=-2)
Construct a task that calls f with user_data argument.
Definition task.hh:391
void initialize(Element *owner, bool schedule)
Initialize the Task, and optionally schedule it.
Definition task.cc:283
void strong_unschedule()
Unschedule the Task until strong_reschedule().
Definition task.hh:260
RouterThread * thread() const
Return the thread on which this task is currently scheduled, or would be scheduled.
Definition task.hh:501
void unschedule()
Unschedule the task.
Definition task.hh:190
TaskCallback callback() const
Return the task's callback function.
Definition task.hh:98
void fast_reschedule()
Reschedule a task from the task's callback function.
Definition task.hh:248
Router * router() const
Return the router to which this task belongs.
Definition task.hh:135
Click's classes for supporting exponentially weighted moving averages.
DirectEWMAX< FixedEWMAXParameters< 4, 10 > > DirectEWMA
A DirectEWMAX with stability shift 4 (alpha 1/16), scaling factor 10 (10 bits of fraction),...
Definition ewma.hh:265
The RouterThread class implementing the Click driver loop.
Classes for synchronizing among multiple CPUs, particularly in the Linux kernel.