new RTS flag: -V to modify the resolution of the RTS timer
[ghc.git] / rts / Timer.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1995-2005
4 *
5 * Interval timer service for profiling and pre-emptive scheduling.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 /*
10 * The interval timer is used for profiling and for context switching in the
11 * threaded build.
12 *
13 * This file defines the platform-independent view of interval timing, relying
14 * on platform-specific services to install and run the timers.
15 *
16 */
17 #include "Rts.h"
18 #include "RtsFlags.h"
19 #include "Proftimer.h"
20 #include "Schedule.h"
21 #include "Timer.h"
22 #include "Ticker.h"
23 #include "Capability.h"
24 #include "RtsSignals.h"
25
26 /* ticks left before next pre-emptive context switch */
27 static int ticks_to_ctxt_switch = 0;
28
29 #if defined(THREADED_RTS)
30 /* idle ticks left before we perform a GC */
31 static int ticks_to_gc = 0;
32 #endif
33
34 /*
35 * Function: handle_tick()
36 *
37 * At each occurrence of a tick, the OS timer will invoke
38 * handle_tick().
39 */
40 static
41 void
42 handle_tick(int unused STG_UNUSED)
43 {
44 #ifdef PROFILING
45 handleProfTick();
46 #endif
47 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
48 ticks_to_ctxt_switch--;
49 if (ticks_to_ctxt_switch <= 0) {
50 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
51 context_switch = 1; /* schedule a context switch */
52 }
53 }
54
55 #if defined(THREADED_RTS)
56 /*
57 * If we've been inactive for idleGCDelayTime (set by +RTS
58 * -I), tell the scheduler to wake up and do a GC, to check
59 * for threads that are deadlocked.
60 */
61 switch (recent_activity) {
62 case ACTIVITY_YES:
63 recent_activity = ACTIVITY_MAYBE_NO;
64 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
65 RtsFlags.MiscFlags.tickInterval;
66 break;
67 case ACTIVITY_MAYBE_NO:
68 if (ticks_to_gc == 0) break; /* 0 ==> no idle GC */
69 ticks_to_gc--;
70 if (ticks_to_gc == 0) {
71 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
72 RtsFlags.MiscFlags.tickInterval;
73 recent_activity = ACTIVITY_INACTIVE;
74 blackholes_need_checking = rtsTrue;
75 /* hack: re-use the blackholes_need_checking flag */
76 wakeUpRts();
77 }
78 break;
79 default:
80 break;
81 }
82 #endif
83 }
84
85 int
86 startTimer(void)
87 {
88 #ifdef PROFILING
89 initProfTimer();
90 #endif
91
92 return startTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
93 }
94
95 int
96 stopTimer(void)
97 {
98 return stopTicker();
99 }