1 /* -----------------------------------------------------------------------------
2 * $Id: Itimer.c,v 1.35 2003/03/29 00:27:11 sof Exp $
4 * (c) The GHC Team, 1995-1999
6 * Interval timer for profiling and pre-emptive scheduling.
8 * ---------------------------------------------------------------------------*/
11 * The interval timer is used for profiling and for context switching in the
12 * threaded build. Though POSIX 1003.1b includes a standard interface for
13 * such things, no one really seems to be implementing them yet. Even
14 * Solaris 2.3 only seems to provide support for @CLOCK_REAL@, whereas we're
15 * keen on getting access to @CLOCK_VIRTUAL@.
17 * Hence, we use the old-fashioned @setitimer@ that just about everyone seems
18 * to support. So much for standards.
24 #include "Proftimer.h"
27 /* As recommended in the autoconf manual */
28 # ifdef TIME_WITH_SYS_TIME
29 # include <sys/time.h>
32 # ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
45 install_vtalrm_handler(TickProc handle_tick)
47 struct sigaction action;
49 action.sa_handler = handle_tick;
51 sigemptyset(&action.sa_mask);
54 return sigaction(SIGVTALRM, &action, NULL);
58 startTicker(nat ms, TickProc handle_tick)
60 # ifndef HAVE_SETITIMER
61 /* fprintf(stderr, "No virtual timer on this system\n"); */
66 install_vtalrm_handler(handle_tick);
68 timestamp = getourtimeofday();
70 it.it_value.tv_sec = ms / 1000;
71 it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
72 it.it_interval = it.it_value;
73 return (setitimer(ITIMER_VIRTUAL, &it, NULL));
80 # ifndef HAVE_SETITIMER
81 /* fprintf(stderr, "No virtual timer on this system\n"); */
86 it.it_value.tv_sec = 0;
87 it.it_value.tv_usec = 0;
88 it.it_interval = it.it_value;
89 return (setitimer(ITIMER_VIRTUAL, &it, NULL));
94 /* This is a potential POSIX version */
102 timestamp = getourtimeofday();
104 se.sigev_notify = SIGEV_SIGNAL;
105 se.sigev_signo = SIGVTALRM;
106 se.sigev_value.sival_int = SIGVTALRM;
107 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
108 barf("can't create virtual timer");
110 it.it_value.tv_sec = ms / 1000;
111 it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
112 it.it_interval = it.it_value;
113 return timer_settime(tid, TIMER_RELTIME, &it, NULL);
120 struct itimerspec it;
123 timestamp = getourtimeofday();
125 se.sigev_notify = SIGEV_SIGNAL;
126 se.sigev_signo = SIGVTALRM;
127 se.sigev_value.sival_int = SIGVTALRM;
128 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
129 barf("can't create virtual timer");
131 it.it_value.tv_sec = 0;
132 it.it_value.tv_nsec = 0;
133 it.it_interval = it.it_value;
134 return timer_settime(tid, TIMER_RELTIME, &it, NULL);
139 /* Currently unused */
141 block_vtalrm_signal(void)
145 sigemptyset(&signals);
146 sigaddset(&signals, SIGVTALRM);
148 (void) sigprocmask(SIG_BLOCK, &signals, NULL);
152 unblock_vtalrm_signal(void)
156 sigemptyset(&signals);
157 sigaddset(&signals, SIGVTALRM);
159 (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
163 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
164 * only calling it 50 times/s, it shouldn't have any great impact.
167 getourtimeofday(void)
170 gettimeofday(&tv, (struct timezone *) NULL);
171 return (tv.tv_sec * TICK_FREQUENCY +
172 tv.tv_usec * TICK_FREQUENCY / 1000000);