4/22/2014

How To determine Linux Kernel Timer Interrupt Frequency

How To determine Linux Kernel Timer Interrupt Frequency

Linux timer interrupt frequency is an important parameter for near to real-time and multimedia applications running on Linux. The timer interrupt frequency directly impacts the capability of any near-to real time and multimedia application to process events at high frequencies. The term high in this context means usually greater than 100 Hz (10 ms).
In the old days the kernel configuration setting CONFIG_HZ was the most important setting for the kernel timer frequency. It was defined at compilation time of the kernel and settings varied between distributions and kernel versions.
Today, a number of other kernel settings - such as NO_HZ, HIGH_RES_TIMERS - impact the kernel timer interrupt frequency as well. Finally the availability of High Precision Event Timer (HPET) support is also an important feature.
All these settings may be retrieved easily from the current kernel configuration. For example:
01$ cat /boot/config-`uname -r` | grep HZ
02# CONFIG_HZ_1000 is not set
03# CONFIG_HZ_300 is not set
04CONFIG_MACHZ_WDT=m
05CONFIG_NO_HZ=y
06CONFIG_HZ=100
07CONFIG_HZ_100=y
08# CONFIG_HZ_250 is not set
09 
10$ cat /boot/config-`uname -r` | grep HIGH_RES_TIMERS
11CONFIG_HIGH_RES_TIMERS=y
However, for a near to real-time or multimedia application the effective achievable timer interrupt frequency counts. This frequency can be estimated quite well with timer interrupts. The small example program below depicts an algorithm to determine the actual achievable timer interrupt frequency using by actually requesting timer interrupts at high frequencies.
01#include
02#include
03#include
04#include
05#include
06 
07 
08#define USECREQ 250
09#define LOOPS   1000
10 
11void event_handler (int signum)
12{
13 static unsigned long cnt = 0;
14 static struct timeval tsFirst;
15 if (cnt == 0) {
16   gettimeofday (&tsFirst, 0);
17 }
18 cnt ++;
19 if (cnt >= LOOPS) {
20   struct timeval tsNow;
21   struct timeval diff;
22   setitimer (ITIMER_REAL, NULL, NULL);
23   gettimeofday (&tsNow, 0);
24   timersub(&tsNow, &tsFirst, &diff);
25   unsigned long long udiff = (diff.tv_sec * 1000000) + diff.tv_usec;
26   double delta = (double)(udiff/cnt)/1000000;
27   int hz = (unsigned)(1.0/delta);
28   printf ("kernel timer interrupt frequency is approx. %d Hz", hz);
29   if (hz >= (int) (1.0/((double)(USECREQ)/1000000))) {
30     printf (" or higher");
31   }      
32   printf ("\n");
33   exit (0);
34 }
35}
36 
37int main (int argc, char **argv)
38{
39 struct sigaction sa;
40 struct itimerval timer;
41 
42 memset (&sa, 0, sizeof (sa));
43 sa.sa_handler = &event_handler;
44 sigaction (SIGALRM, &sa, NULL);
45 timer.it_value.tv_sec = 0;
46 timer.it_value.tv_usec = USECREQ;
47 timer.it_interval.tv_sec = 0;
48 timer.it_interval.tv_usec = USECREQ;
49 setitimer (ITIMER_REAL, &timer, NULL);
50 while (1);
51}
To check your particular system regarding time interrupt frequency capabilities please follow the instructions below create a local copy of this small program, as e.g. frequency-test.c. Issue is that this setting has an important impact on near to real-time applications.
  • Create a local copy of this small program, as e.g. frequency-test.c
  • Compile it with gcc: "gcc frequency-test.c"
  • Run it: "./a.out"
On a Ubuntu 8.04 LTS server (Hardy) ge got:
1$ ./a.out
2kernel timer interrupt frequency is approx. 4016 Hz or higher
On a Ubuntu 10.04 LTS server (Lucid) ge got:
1$ ./a.out
2kernel timer interrupt frequency is approx. 4016 Hz or higher
On a little bit older OpenSUSE with kernel 2.6.22.5-31-bigsmp we achieved:
1$ ./a.out
2kernel timer interrupt frequency is approx. 249 Hz
Both systems are multicore server chassis.
If the timer interrupt frequency determined on your system is for example around 250Hz, it will be very difficult for any near to real-time or multimedia application to send out an isochronous data stream of 250 packets per second (pps).

댓글 없음: