1.\" 2.\" $FreeBSD$ 3.\" 4.Dd February 15, 2002 5.Dt POLLING 4 6.Os 7.Sh NAME 8.Nm polling 9.Nd device polling support 10.Sh SYNOPSIS 11.Cd "options DEVICE_POLLING" 12.Cd "options HZ=1000" 13.Sh DESCRIPTION 14.Dq "Device polling" 15(polling for brevity) refers to a technique to 16handle devices that does not rely on the latter to generate 17interrupts when they need attention, but rather lets the CPU poll 18devices to service their needs. 19This might seem inefficient and counterintuitive, but when done 20properly, 21.Nm 22gives more control to the operating system on 23when and how to handle devices, with a number of advantages in terms 24of system responsivity and performance. 25.Pp 26In particular, 27.Nm 28reduces the overhead for context 29switches which is incurred when servicing interrupts, and 30gives more control on the scheduling of the CPU between various 31tasks (user processes, software interrupts, device handling) 32which ultimately reduces the chances of livelock in the system. 33.Sh PRINCIPLES OF OPERATION 34In the normal, interrupt-based mode, devices generate an interrupt 35whenever they need attention. 36This in turn causes a 37context switch and the execution of a interrupt handler 38which performs whatever processing is needed by the device. 39The duration of the interrupt handler is potentially unbounded 40unless the device driver has been programmed with real-time 41concerns in mind (which is generally not the case for 42.Fx 43drivers). 44Furthermore, under heavy traffic, the system might be 45persistently processing interrupts without being able to 46complete other work, either in the kernel or in userland. 47.Pp 48.Nm Polling 49disables interrupts by polling devices at appropriate 50times, i.e., on clock interrupts, system calls and within the idle loop. 51This way, the context switch overhead is removed. 52Furthermore, 53the operating system can control accurately how much work to spend 54in handling device events, and thus prevent livelock by reserving 55some amount of CPU to other tasks. 56.Pp 57.Nm Polling 58is enabled with a 59.Xr sysctl 8 60variable 61.Va kern.polling.enable 62whereas the percentage of CPU cycles reserved to userland processes is 63controlled by the 64.Xr sysctl 8 65variable 66.Va kern.polling.user_frac 67whose range is 0 to 100 (50 is the default value). 68.Pp 69When 70.Nm 71is enabled, and provided that there is work to do, 72up to 73.Va kern.polling.user_frac 74percent of the CPU cycles is reserved to userland tasks, the 75remaining fraction being available for device processing. 76.Pp 77Enabling 78.Nm 79also changes the way network software interrupts 80are scheduled, so there is never the risk of livelock because 81packets are not processed to completion. 82.Pp 83There are other variables which control or monitor the behaviour 84of devices operating in polling mode, but they are unlikely to 85require modifications, and are documented in the source file 86.Pa sys/kern/kern_poll.c . 87.Sh SUPPORTED DEVICES 88.Nm Polling 89requires explicit modifications to the device drivers. 90As of this writing, the 91.Xr dc 4 , 92.Xr fxp 4 , 93.Xr rl 4 , 94and 95.Xr sis 4 96devices are supported, with other in the works. 97The modifications are rather straightforward, consisting in 98the extraction of the inner part of the interrupt service routine 99and writing a callback function, 100.Fn *_poll , 101which is invoked 102to probe the device for events and process them. 103See the 104conditionally compiled sections of the devices mentioned above 105for more details. 106.Pp 107Because in the worst case devices are only polled on 108clock interrupts, in order to reduce the latency in processing 109packets, it is advisable to increase the frequency of the clock 110to at least 1000 HZ. 111.Sh HISTORY 112Device polling was introduced in February 2002 by 113.An Luigi Rizzo Aq luigi@iet.unipi.it . 114