[project @ 2004-09-03 15:28:18 by simonmar]
[ghc-hetmet.git] / ghc / rts / Proftimer.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Proftimer.c,v 1.13 2004/09/03 15:28:37 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Profiling interval timer
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #if defined (PROFILING)
11
12 #include "PosixSource.h"
13
14 #include "Rts.h"
15 #include "Profiling.h"
16 #include "Timer.h"
17 #include "Proftimer.h"
18 #include "RtsFlags.h"
19
20 static rtsBool do_prof_ticks = rtsFalse;       // enable profiling ticks
21 static rtsBool do_heap_prof_ticks = rtsFalse;  // enable heap profiling ticks
22
23 // Number of ticks until next heap census
24 static int ticks_to_heap_profile;
25
26 // Time for a heap profile on the next context switch
27 rtsBool performHeapProfile;
28
29 void
30 stopProfTimer( void )
31 {
32     do_prof_ticks = rtsFalse;
33 }
34
35 void
36 startProfTimer( void )
37 {
38     do_prof_ticks = rtsTrue;
39 }
40
41 void
42 stopHeapProfTimer( void )
43 {
44     do_heap_prof_ticks = rtsFalse;
45 }
46
47 void
48 startHeapProfTimer( void )
49 {
50     if (RtsFlags.ProfFlags.doHeapProfile && 
51         RtsFlags.ProfFlags.profileIntervalTicks > 0) {
52         do_heap_prof_ticks = rtsTrue;
53     }
54 }
55
56 void
57 initProfTimer( void )
58 {
59     performHeapProfile = rtsFalse;
60
61     RtsFlags.ProfFlags.profileIntervalTicks = 
62         RtsFlags.ProfFlags.profileInterval / TICK_MILLISECS;
63
64     ticks_to_heap_profile = RtsFlags.ProfFlags.profileIntervalTicks;
65
66     startHeapProfTimer();
67 }
68
69
70 void
71 handleProfTick(void)
72 {
73     if (do_prof_ticks) {
74         CCCS->time_ticks++;
75     }
76
77     if (do_heap_prof_ticks) {
78         ticks_to_heap_profile--;
79         if (ticks_to_heap_profile <= 0) {
80             ticks_to_heap_profile = RtsFlags.ProfFlags.profileIntervalTicks;
81             performHeapProfile = rtsTrue;
82         }
83     }
84 }
85
86 #endif /* PROFILING */