Hadrian: bump Cabal submodule, install extra dynamic flavours of RTS
[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
18 #include "PosixSource.h"
19 #include "Rts.h"
20
21 #include "Timer.h"
22 #include "Proftimer.h"
23 #include "Schedule.h"
24 #include "Ticker.h"
25 #include "Capability.h"
26 #include "RtsSignals.h"
27
28 /* ticks left before next pre-emptive context switch */
29 static int ticks_to_ctxt_switch = 0;
30
31 /* idle ticks left before we perform a GC */
32 static int ticks_to_gc = 0;
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 handleProfTick();
45 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
46 ticks_to_ctxt_switch--;
47 if (ticks_to_ctxt_switch <= 0) {
48 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
49 contextSwitchAllCapabilities(); /* schedule a context switch */
50 }
51 }
52
53 /*
54 * If we've been inactive for idleGCDelayTime (set by +RTS
55 * -I), tell the scheduler to wake up and do a GC, to check
56 * for threads that are deadlocked.
57 */
58 switch (recent_activity) {
59 case ACTIVITY_YES:
60 recent_activity = ACTIVITY_MAYBE_NO;
61 ticks_to_gc = RtsFlags.GcFlags.idleGCDelayTime /
62 RtsFlags.MiscFlags.tickInterval;
63 break;
64 case ACTIVITY_MAYBE_NO:
65 if (ticks_to_gc == 0) {
66 if (RtsFlags.GcFlags.doIdleGC) {
67 recent_activity = ACTIVITY_INACTIVE;
68 #if defined(THREADED_RTS)
69 wakeUpRts();
70 // The scheduler will call stopTimer() when it has done
71 // the GC.
72 #endif
73 } else {
74 recent_activity = ACTIVITY_DONE_GC;
75 // disable timer signals (see #1623, #5991, #9105)
76 // but only if we're not profiling (e.g. passed -h or -p RTS
77 // flags). If we are profiling we need to keep the timer active
78 // so that samples continue to be collected.
79 #if defined(PROFILING)
80 if (!(RtsFlags.ProfFlags.doHeapProfile
81 || RtsFlags.CcFlags.doCostCentres)) {
82 stopTimer();
83 }
84 #else
85 stopTimer();
86 #endif
87 }
88 } else {
89 ticks_to_gc--;
90 }
91 break;
92 default:
93 break;
94 }
95 }
96
97 // This global counter is used to allow multiple threads to stop the
98 // timer temporarily with a stopTimer()/startTimer() pair. If
99 // timer_enabled == 0 timer is enabled
100 // timer_disabled == N, N > 0 timer is disabled by N threads
101 // When timer_enabled makes a transition to 0, we enable the timer,
102 // and when it makes a transition to non-0 we disable it.
103
104 static StgWord timer_disabled;
105
106 void
107 initTimer(void)
108 {
109 initProfTimer();
110 if (RtsFlags.MiscFlags.tickInterval != 0) {
111 initTicker(RtsFlags.MiscFlags.tickInterval, handle_tick);
112 }
113 timer_disabled = 1;
114 }
115
116 void
117 startTimer(void)
118 {
119 if (atomic_dec(&timer_disabled) == 0) {
120 if (RtsFlags.MiscFlags.tickInterval != 0) {
121 startTicker();
122 }
123 }
124 }
125
126 void
127 stopTimer(void)
128 {
129 if (atomic_inc(&timer_disabled, 1) == 1) {
130 if (RtsFlags.MiscFlags.tickInterval != 0) {
131 stopTicker();
132 }
133 }
134 }
135
136 void
137 exitTimer (bool wait)
138 {
139 if (RtsFlags.MiscFlags.tickInterval != 0) {
140 exitTicker(wait);
141 }
142 }