Docsity

Kernel Timers and Work Queues in Linux, Slides of Computer Applications

Partial preview of the text

Download Kernel Timers and Work Queues in Linux and more Slides Computer Applications in PDF only on Docsity!

Kernel timing issues

An introduction to the use of kernel timers and work queues

Kernel timers

  • Linux offers a facility that lets drivers put a process to sleep until a fixed amount of time has elapsed (as measured in jiffies)
  • When the timer expires, a driver-defined action will be performed, which can ‘wake up’ the process that was put to sleep, or could perform some alternative action (for example, the kernel timer could re-start)

jiffies overflow

  • Won’t overflow for at least 16 months
  • Linux kernel got modified to ‘fix’ overflow
  • Now the declaration is in ‘linux/jiffies.h’: unsigned long long jiffies_64; and a new instruction in ‘do_timer()’ ((u64)&jiffies_64)++; which compiles to assembly language as add $1, jiffies+ adc $0, jiffies+

Kernel timer syntax

  • Declare a timer: struct timer_list mytimer;
  • Initialize this timer: init_timer( &mytimer ); mytimer.func = mytimeraction; mytimer.data = (unsigned long)mydata; mytimer.expires = <number-of-jiffies>
  • Install this timer: add_timer( &mytimer );
  • Modify this timer: mod_timer( &mytimer, );
  • Delete this timer: del_timer( &mytimer );
  • Delete it safely: del_timer_sync( &mytimer);

‘trytimer.c’

  • We have posted an example that shows how a Linux kernel timer can be used to perform a periodic action (such as using ‘printk()’ to issue a message every time the time expires, then restart the timer
  • Notice that our demo is not able to issue messages directly to the console – its timer-function executes without a ‘tty’

Delaying work

  • If a device-driver needs to perform actions that require using process resources (like a tty), or that may possibly ‘sleep’, then it can defer that work – using a ‘workqueue’

‘tryworkq.c’ and ‘defermsg.c’

  • We have posted demo-modules that show the use of workqueues to perform actions later, either as soon as a ‘process context’ is available, or after a prescribed time
  • Further details on the options for using an existing kernel workqueue or a workqueue of your own creation may be found in our textbook (Chapter 7 of LDD3)

Applying these ideas

  • To demonstrate a programming situation in which using kernel timers is valuable, we created the ‘ foo.c ’ device-driver, plus an application that uses it (‘watchfoo.cpp’)
  • You can compile and install the module, then execute the application: $ watchfoo
  • But you will notice there are two ‘problems’ (excess cpu usage and loop-termination)

In-class exercise

  • Modify ‘foo.c’ (call it ‘timedfoo.c’) as follows
  • Create an integer flag-variable (‘ready’) as a global object in your module
  • When your ‘read()’ function gets called, it should sleep until ‘ready’ equals TRUE; it should set ‘ready’ equal to FALSE when it awakens, but should set a timer to expire after 1/10 seconds
  • Your timer’s action-function should set ‘ready’ back to TRUE, then wake up any sleeping tasks

Implementation hints

  • You need a wait-queue (so your driver’s ‘reader’ tasks can sleep on it)
  • You need a timer action-function
  • You need to organize your timer-function’s data-items into a single structure (because the timer-function has only one argument)
  • Your timer-function must do two things:
    • Change ‘ready’ to TRUE
    • Wake up any ‘sleepers’

‘sizeof’ and ‘offsetof’

  • Our GNU compilers permit use of these C/C++ operators on object types
  • The ‘sizeof’ operator returns the number of bytes of memory the compiler allocated for storing the specified object
  • The ‘offsetof’ operator returns the number of bytes in a structure which precede the specified structure-member

A ‘struct’ example

struct mystruct { char w; short x; long y; long long z; } my_instance;

You can use the ‘sizeof’ operator to find out how much memory gets allocated to any ‘struct mystruct’ object, like this: int nbytes = sizeof( my_instance );

You can use the ‘offsetof’’ operator to find out where within a given structure a particular field occurs, like this: int offset_z = offsetof( struct mystruct, z );

Using ‘container_of()’

struct mystruct { char w; short x; long y; long long z; } my_instance = { 1, 2, 3, 4 };

If you have a pointer to one of the fields in some instance of a this kind of ‘struct’ object, then you could use the ‘container_of()’ macro to get a pointer to that ‘struct’ object itself, like this:

long *ptr = &my_instance.y; struct mystruct *p = container_of( ptr, struct mystruct, y );

This would be useful if you now wanted to access other members:

printk( “w=%d x=%d y=%d z=%d \n”, p->w, p->x, p->y, p->z );

  • #include <linux/workqueue.h>
  • void dowork( struct work_struct *data );
  • DECLARE_DELAYED_WORK( mywork, dowork );
  • struct workqueue_struct *myqueue;
  • myqueue = create_singlethread_workqueue( “mywork” );