586991a4db88f939576d752bd4e1c79513221ecd
[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 "Storage.h"
21 #include "Schedule.h"
22 #include "Timer.h"
23 #include "Ticker.h"
24 #include "Capability.h"
25 #include "RtsSignals.h"
26
27 /* ticks left before next pre-emptive context switch */
28 static int ticks_to_ctxt_switch = 0;
29
30 #if defined(THREADED_RTS)
31 /* idle ticks left before we perform a GC */
32 static int ticks_to_gc = 0;
33 #endif
34
35 /*
36 * Function: handle_tick()
37 *
38 * At each occurrence of a tick, the OS timer will invoke
39 * handle_tick().
40 */
41 static
42 void
43 handle_tick(int unused STG_UNUSED)
44 {
45 handleProfTick();
46 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
47 ticks_to_ctxt_switch--;
48 if (ticks_to_ctxt_switch <= 0) {
49 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
50 context_switch = 1; /* schedule a context switch */
51 }
52 }
53
54 #if defined(THREADED_RTS)
55 /*
56 * If we've been inactive for idleGCDelayTime (set by +RTS
57 * -I), tell the scheduler to wake up and do a GC, to check
58 * for threads that are deadlocked.
59 */
60 switch (recent_activity) {
61 case ACTIVITY_YES:
62 recent_activity = ACTIVITY_MAYBE_NO;
63 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
64 RtsFlags.MiscFlags.tickInterval;
65 break;
66 case ACTIVITY_MAYBE_NO:
67 if (ticks_to_gc == 0) break; /* 0 ==> no idle GC */
68 ticks_to_gc--;
69 if (ticks_to_gc == 0) {
70 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
71 RtsFlags.MiscFlags.tickInterval;
72 recent_activity = ACTIVITY_INACTIVE;
73 blackholes_need_checking = rtsTrue;
74 /* hack: re-use the blackholes_need_checking flag */
75 wakeUpRts();
76 }
77 break;
78 default:
79 break;
80 }
81 #endif
82 }
83
84 void
85 startTimer(void)
86 {
87 initProfTimer();
88 if (RtsFlags.MiscFlags.tickInterval != 0) {
89 startTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
90 }
91 }
92
93 void
94 stopTimer(void)
95 {
96 if (RtsFlags.MiscFlags.tickInterval != 0) {
97 stopTicker();
98 }
99 }