1

There is a test program to work with setitimer on Linux (kernel 2.6; HZ=100). It sets various itimers to send signal every 10 ms (actually it is set as 9ms, but the timeslice is 10 ms). Then program runs for some fixed time (e.g. 30 sec) and counts signals.

Is it guaranteed that signal count will be proportional to running time? Will count be the same in every run and with every timer type (-r -p -v)?

Note, on the system should be no other cpu-active processes; and the question is about fixed-HZ kernel.

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>

/* Use 9 ms timer */
#define usecs 9000

int events = 0;
void count(int a) {
    events++;
}

int main(int argc, char**argv)
{
    int timer,j,i,k=0;
    struct itimerval timerval = {
        .it_interval = {.tv_sec=0, .tv_usec=usecs},
        .it_value = {.tv_sec=0, .tv_usec=usecs}
    };
    if ( (argc!=2) || (argv[1][0]!='-') ) {
        printf("Usage: %s -[rpv]\n  -r - ITIMER_REAL\n  -p - ITIMER_PROF\n  -v - ITIMER_VIRTUAL\n", argv[0]);
        exit(0);
    }
    switch(argv[1][1]) {
        case'r': 
            timer=ITIMER_REAL;
            break;
        case'p':
            timer=ITIMER_PROF;
            break;
        case'v':
            timer=ITIMER_VIRTUAL;
    };
    signal(SIGALRM,count);
    signal(SIGPROF,count);
    signal(SIGVTALRM,count);
    setitimer(timer, &timerval, NULL);
    /* constants should be tuned to some huge value */
    for (j=0; j<4; j++)
      for (i=0; i<2000000000; i++)
        k += k*argc + 5*k + argc*3;
    printf("%d events\n",events);
    return 0;
}
osgx
  • 90,338
  • 53
  • 357
  • 513

1 Answers1

0

Is it guaranteed that signal count will be proportional to running time?

Yes. In general, for all the three timers the longer the code runs, the more the number of signals received.

Will count be the same in every run and with every timer type (-r -p -v)?

No.

When the timer is set using ITIMER_REAL, the timer decrements in real time.

When it is set using ITIMER_VIRTUAL, the timer decrements only when the process is executing in the user address space. So, it doesn't decrement when the process makes a system call or during interrupt service routines. So we can expect that #real_signals > #virtual_signals

ITIMER_PROF timers decrement both during user space execution of the process and when the OS is executing on behalf of the process i.e. during system calls. So #prof_signals > #virtual_signals

ITIMER_PROF doesn't decrement when OS is not executing on behalf of the process. So #real_signals > #prof_signals

To summarise, #real_signals > #prof_signals > #virtual_signals.

  • About second question: Let's fix type of timer as REAL; then start our program 5 times. Will the total signal count be the same or not? – osgx Oct 31 '12 at 04:48
  • Yes. The counts should be same in this case. Under high load conditions, it is possible that the counts will differ. – Prakash Murali Oct 31 '12 at 05:07