%Time Management %Thadeu Cascardo # Tickes, Jiffies * The timer ticks with a frequency, interrupting the system * Necessary for preemptive scheduling, using a time slice * Linux counts the number of ticks in the *jiffies* variable * It's interrupted *HZ* times in a second # Busy waiting * time\\_after * time\\_before * cpu\\_relax * udelay # Scheduling * schedule * set\\_current\\_state * TASK\\_INTERRUPTIBLE * TASK\\_UNINTERRUPTIBLE * TASK\\_RUNNING * schedule\\_timeout * schedule\\_timeout\\_interruptible * msleep * msleep\\_interruptible # Timers * linux/timer.h * struct timer\\_list - unsigned long expires - void function(unsigned long) - unsigned long data * init\\_timer * TIMER\\_INITIALIZER * add\\_timer * del\\_timer * mod\\_timer * del\\_timer\\_sync # Work queues * Queues of functions to be called in a kernel thread * They execute in process context, so they may sleep * However, accessing user space memory does not make sense here * Works should not take long or they will delay other works execution * There's a default workqueue, keventd or events # Works * include linux/workqueue.h * struct work\\_struct * DECLARE\\_WORK(name, func) * INIT\\_WORK(work, func) * queue\\_work(wq, work) * schedule\\_work(work) * flush\\_work(work) * cancel\\_work\\_sync(work) # Delayed work * struct delayed\\_work * DECLARE\\_DELAYED\\_WORK(name, func) * INIT\\_DELAYED\\_WORK(dwork, func) * queue\\_delayed\\_work(wq, dwork, delay) * schedule\\_delayed\\_work(dwork, delay) * flush\\_delayed\\_work(dwork) * cancel\\_delayed\\_work(dwork) * cancel\\_delayed\\_work\\_sync(dwork) # Workqueue * create\\_workqueue(name) * create\\_singlethread\\_workqueue(name) * destroy\\_workqueue(wq) * flush\\_workqueue(wq) * flush\\_scheduled\\_work() # Tasklets * include linux/interrupt.h * struct tasklet\\_struct * DECLARE\\_TASKLET(name, func, data) * tasklet\\_init(tsk, func, data) * tasklet\\_schedule * tasklet\\_hi\\_schedule * tasklet\\_enable * tasklet\\_disable * tasklet\\_kill # Completion * include linux/completion.h * struct completion * DECLARE\\_COMPLETION(name) * init\\_completion(comp) * wait\\_for\\_completion(comp) * wait\\_for\\_completion\\_interruptible * wait\\_for\\_completion\\_timeout * complete # Wait queue * include linux/wait.h * wait\\_queue\\_head\\_t * DECLARE\\_WAIT\\_QUEUE\\_HEAD(name) * init\\_waitqueue\\_head(wqh) * wake\\_up(wqh) * wait\\_event(wqh, cond) * wait\\_event\\_timeout(wqh, cond, timeout) * wait\\_event\\_interruptible(wqh, cond)