Applying kernel timers Could we prevent a device-driver from consuming an excessive amount of processor time? Degraded system performance • • • • • • Recall our ‘Interrupt Activity Monitor’ demo It’s posted on class website (‘activity.cpp’) Repeatedly called a driver’s ‘read’ function But used over 99-percent of the cpu time! (We discovered that fact with ‘top’ utility) Name of the driver module was ‘activity.c’ Kernel software timers • • • • We also learned how to use kernel timers An easy-to-use programming interface Data-object: struct timer_list; And four kernel functions: init_timer(); add_timer(); mod_timer(); del_timer(); Sleep/Wakeup • • • • Earlier we encountered Linux task ‘states’ A Linux task can easily be ‘put to sleep’ While asleep a task doesn’t use cpu time A timer-function could ‘wake up’ our task How it might work • Our device-driver’s ‘read()’ functon: – Before reading any new data, check to see how long it’s been since we last did a ‘read’ – If we discover we read data quite recently, then we could set-a-timer and go-to-sleep – When awakened we’ll return the new data • This proposed scheme is like a ‘throttle’ What does timer-function do? • • • • • Our driver needs a ‘ready’ flag: 0 or 1 The ‘read()’ function sets: ready = FALSE The timer-function sets: ready = TRUE ‘read()’ will sleep until: ready == TRUE And timer-function will ‘wake up’ driver Let’s try it! • Some demo-code is on our class website: – Device-driver module: ‘tsc.c’ – Application program: ‘watchtsc.cpp’ • First: Try out the demo (use ‘top’ utility) • Next: Modify the driver-module – Add a wait-queue – Add a kernel timer • Finally: Use ‘top’ again to check cpu usage TimeStamp Counter • • • • • Pentium cpu has a built-in TSC register Incremented once every cpu clock-cycle Register is 64-bits wide (8-bytes) It won’t overflow for at least a decade! Accessed using special instruction: ‘rdtsc’ EAX=least significant 32-bits of TSC EDX=most signigicant 32-bits of TSC