1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.69 2003/01/29 09:54:33 simonmar Exp $
4 * (c) The GHC Team, 1998-2002
6 * Main function for a standalone Haskell program.
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
15 #include "Storage.h" /* initStorage, exitStorage */
16 #include "StablePriv.h" /* initStablePtrTable */
17 #include "Schedule.h" /* initScheduler */
18 #include "Stats.h" /* initStats */
24 #include "StgStartup.h"
25 #include "Prelude.h" /* fixupRTStoPreludeRefs */
28 #include "ThreadLabels.h"
30 #if defined(RTS_GTK_FRONTPANEL)
31 #include "FrontPanel.h"
34 #if defined(PROFILING) || defined(DEBUG)
35 # include "Profiling.h"
36 # include "ProfHeap.h"
37 # include "RetainerProfile.h"
41 # include "GranSimRts.h"
44 #if defined(GRAN) || defined(PAR)
45 # include "ParallelRts.h"
49 # include "Parallel.h"
56 struct RTS_FLAGS RtsFlags;
58 // Count of how many outstanding hs_init()s there have been.
59 static int hs_init_count = 0;
61 /* -----------------------------------------------------------------------------
63 -------------------------------------------------------------------------- */
66 hs_init(int *argc, char **argv[])
69 if (hs_init_count > 1) {
70 // second and subsequent inits are ignored
74 /* The very first thing we do is grab the start time...just in case we're
75 * collecting timing statistics.
81 * The parallel system needs to be initialised and synchronised before
84 startupParallelSystem(argv);
86 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
91 argv[1] = argv[0]; /* ignore the nPEs argument */
95 /* Set the RTS flags to default values. */
96 initRtsFlagsDefaults();
98 /* Call the user hook to reset defaults, if present */
101 /* Parse the flags, separating the RTS flags from the programs args */
102 if (argc != NULL && argv != NULL) {
103 setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
109 /* NB: this really must be done after processing the RTS flags */
110 IF_PAR_DEBUG(verbose,
111 fprintf(stderr, "==== Synchronising system (%d PEs)\n", nPEs));
112 synchroniseSystem(); // calls initParallelSystem etc
115 /* initialise scheduler data structures (needs to be done before
121 /* And start GranSim profiling if required: */
122 if (RtsFlags.GranFlags.GranSimStats.Full)
123 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
125 /* And start GUM profiling if required: */
126 if (RtsFlags.ParFlags.ParStats.Full)
127 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
128 #endif /* PAR || GRAN */
130 /* initialize the storage manager */
133 /* initialise the stable pointer table */
134 initStablePtrTable();
136 /* initialise thread label table (tso->char*) */
137 initThreadLabelTable();
139 #if defined(PROFILING) || defined(DEBUG)
143 #if defined(PROFILING) || defined(DEBUG)
147 /* start the virtual timer 'subsystem'. */
148 startVirtTimer(TICK_MILLISECS);
150 /* Initialise the stats department */
153 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
154 /* Initialise the user signal handler set */
156 /* Set up handler to run on SIGINT, etc. */
157 initDefaultHandlers();
160 #ifdef RTS_GTK_FRONTPANEL
161 if (RtsFlags.GcFlags.frontpanel) {
166 /* Record initialization times */
170 // Compatibility interface
172 startupHaskell(int argc, char *argv[], void (*init_root)(void))
174 hs_init(&argc, &argv);
175 hs_add_root(init_root);
179 /* -----------------------------------------------------------------------------
180 Getting/Setting the program's arguments.
182 These are used by System.Environment.
183 -------------------------------------------------------------------------- */
186 getProgArgv(int *argc, char **argv[])
193 setProgArgv(int argc, char *argv[])
195 /* Usually this is done by startupHaskell, so we don't need to call this.
196 However, sometimes Hugs wants to change the arguments which Haskell
197 getArgs >>= ... will be fed. So you can do that by calling here
198 _after_ calling startupHaskell.
204 /* -----------------------------------------------------------------------------
205 Per-module initialisation
207 This process traverses all the compiled modules in the program
208 starting with "Main", and performing per-module initialisation for
211 So far, two things happen at initialisation time:
213 - we register stable names for each foreign-exported function
214 in that module. This prevents foreign-exported entities, and
215 things they depend on, from being garbage collected.
217 - we supply a unique integer to each statically declared cost
218 centre and cost centre stack in the program.
220 The code generator inserts a small function "__stginit_<module>" in each
221 module and calls the registration functions in each of the modules it
224 The init* functions are compiled in the same way as STG code,
225 i.e. without normal C call/return conventions. Hence we must use
226 StgRun to call this stuff.
227 -------------------------------------------------------------------------- */
229 /* The init functions use an explicit stack...
231 #define INIT_STACK_BLOCKS 4
232 static F_ *init_stack = NULL;
235 hs_add_root(void (*init_root)(void))
241 #define cap MainCapability
245 if (hs_init_count <= 0) {
246 barf("hs_add_root() must be called after hs_init()");
250 bd = allocGroup(INIT_STACK_BLOCKS);
251 init_stack = (F_ *)bd->start;
252 init_stack[init_sp++] = (F_)stg_init_ret;
253 if (init_root != NULL) {
254 init_stack[init_sp++] = (F_)init_root;
257 cap.r.rSp = (P_)(init_stack + init_sp);
258 StgRun((StgFunPtr)stg_init, &cap.r);
263 /* -----------------------------------------------------------------------------
264 Shutting down the RTS
265 -------------------------------------------------------------------------- */
270 if (hs_init_count <= 0) {
271 barf("too many hs_exit()s");
274 if (hs_init_count > 0) {
275 // ignore until it's the last one
279 /* start timing the shutdown */
282 /* stop all running tasks */
286 /* Finalize any remaining weak pointers */
287 finalizeWeakPointersNow();
291 /* end_gr_simulation prints global stats if requested -- HWL */
292 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
296 /* stop the ticker */
299 /* reset the standard file descriptors to blocking mode */
300 resetNonBlockingFd(0);
301 resetNonBlockingFd(1);
302 resetNonBlockingFd(2);
305 /* controlled exit; good thread! */
306 shutdownParallelSystem(0);
308 /* global statistics in parallel system */
312 /* stop timing the shutdown, we're about to print stats */
315 /* clean up things from the storage manager's point of view.
316 * also outputs the stats (+RTS -s) info.
320 #ifdef RTS_GTK_FRONTPANEL
321 if (RtsFlags.GcFlags.frontpanel) {
326 #if defined(PROFILING)
327 reportCCSProfiling();
330 #if defined(PROFILING) || defined(DEBUG)
335 // Originally, this was in report_ccs_profiling(). Now, retainer
336 // profiling might tack some extra stuff on to the end of this file
337 // during endProfiling().
341 #if defined(TICKY_TICKY)
342 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
346 // Compatibility interfaces
348 shutdownHaskell(void)
354 shutdownHaskellAndExit(int n)
356 if (hs_init_count == 1) {
360 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
369 * called from STG-land to exit the program
373 static int exit_started=rtsFalse;
380 /* HACK: avoid a loop when exiting due to a stupid error */
383 exit_started=rtsTrue;
385 IF_PAR_DEBUG(verbose, fprintf(stderr,"==-- stg_exit %d on [%x]...", n, mytid));
386 shutdownParallelSystem(n);