1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.66 2002/08/16 13:29:06 simonmar Exp $
4 * (c) The GHC Team, 1998-2000
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"
58 struct RTS_FLAGS RtsFlags;
60 static int rts_has_started_up = 0;
65 EXTFUN(__stginit_Prelude);
66 static void initModules ( void (*)(void) );
69 setProgArgv(int argc, char *argv[])
71 /* Usually this is done by startupHaskell, so we don't need to call this.
72 However, sometimes Hugs wants to change the arguments which Haskell
73 getArgs >>= ... will be fed. So you can do that by calling here
74 _after_ calling startupHaskell.
81 getProgArgv(int *argc, char **argv[])
89 startupHaskell(int argc, char *argv[], void (*init_root)(void))
91 /* To avoid repeated initialisations of the RTS */
92 if (rts_has_started_up) {
93 /* RTS is up and running, so only run the per-module initialisation code */
95 initModules(init_root);
102 /* The very first thing we do is grab the start time...just in case we're
103 * collecting timing statistics.
109 * The parallel system needs to be initialised and synchronised before
110 * the program is run.
112 startupParallelSystem(argv);
114 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
119 argv[1] = argv[0]; /* ignore the nPEs argument */
123 /* Set the RTS flags to default values. */
124 initRtsFlagsDefaults();
126 /* Call the user hook to reset defaults, if present */
129 /* Parse the flags, separating the RTS flags from the programs args */
130 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
135 /* NB: this really must be done after processing the RTS flags */
136 IF_PAR_DEBUG(verbose,
137 fprintf(stderr, "==== Synchronising system (%d PEs)\n", nPEs));
138 synchroniseSystem(); // calls initParallelSystem etc
141 /* initialise scheduler data structures (needs to be done before
147 /* And start GranSim profiling if required: */
148 if (RtsFlags.GranFlags.GranSimStats.Full)
149 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
151 /* And start GUM profiling if required: */
152 if (RtsFlags.ParFlags.ParStats.Full)
153 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
154 #endif /* PAR || GRAN */
156 /* initialize the storage manager */
159 /* initialise the stable pointer table */
160 initStablePtrTable();
162 /* initialise thread label table (tso->char*) */
163 initThreadLabelTable();
165 #if defined(PROFILING) || defined(DEBUG)
169 /* run the per-module initialisation code */
170 initModules(init_root);
172 #if defined(PROFILING) || defined(DEBUG)
176 /* start the virtual timer 'subsystem'. */
177 startVirtTimer(TICK_MILLISECS);
179 /* Initialise the stats department */
182 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
183 /* Initialise the user signal handler set */
185 /* Set up handler to run on SIGINT, etc. */
186 initDefaultHandlers();
189 #ifdef RTS_GTK_FRONTPANEL
190 if (RtsFlags.GcFlags.frontpanel) {
195 /* Record initialization times */
199 /* -----------------------------------------------------------------------------
200 Per-module initialisation
202 This process traverses all the compiled modules in the program
203 starting with "Main", and performing per-module initialisation for
206 So far, two things happen at initialisation time:
208 - we register stable names for each foreign-exported function
209 in that module. This prevents foreign-exported entities, and
210 things they depend on, from being garbage collected.
212 - we supply a unique integer to each statically declared cost
213 centre and cost centre stack in the program.
215 The code generator inserts a small function "__stginit_<module>" in each
216 module and calls the registration functions in each of the modules it
217 imports. So, if we call "__stginit_PrelMain", each reachable module in the
218 program will be registered (because PrelMain.mainIO calls Main.main).
220 The init* functions are compiled in the same way as STG code,
221 i.e. without normal C call/return conventions. Hence we must use
222 StgRun to call this stuff.
223 -------------------------------------------------------------------------- */
225 /* The init functions use an explicit stack...
227 #define INIT_STACK_BLOCKS 4
228 F_ *init_stack = NULL;
231 initModules ( void (*init_root)(void) )
237 #define cap MainCapability
242 bd = allocGroup(INIT_STACK_BLOCKS);
243 init_stack = (F_ *)bd->start;
244 init_stack[init_sp++] = (F_)stg_init_ret;
245 init_stack[init_sp++] = (F_)__stginit_Prelude;
246 if (init_root != NULL) {
247 init_stack[init_sp++] = (F_)init_root;
250 cap.r.rSp = (P_)(init_stack + init_sp);
251 StgRun((StgFunPtr)stg_init, &cap.r);
256 /* -----------------------------------------------------------------------------
257 * Shutting down the RTS - two ways of doing this, one which
258 * calls exit(), one that doesn't.
260 * (shutdownHaskellAndExit() is called by System.exitWith).
261 * -----------------------------------------------------------------------------
264 shutdownHaskellAndExit(int n)
269 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
277 shutdownHaskell(void)
279 if (!rts_has_started_up)
281 rts_has_started_up=0;
283 /* start timing the shutdown */
286 /* stop all running tasks */
290 /* Finalize any remaining weak pointers */
291 finalizeWeakPointersNow();
295 /* end_gr_simulation prints global stats if requested -- HWL */
296 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
300 /* stop the ticker */
303 /* reset the standard file descriptors to blocking mode */
304 resetNonBlockingFd(0);
305 resetNonBlockingFd(1);
306 resetNonBlockingFd(2);
309 /* controlled exit; good thread! */
310 shutdownParallelSystem(0);
312 /* global statistics in parallel system */
316 /* stop timing the shutdown, we're about to print stats */
319 /* clean up things from the storage manager's point of view.
320 * also outputs the stats (+RTS -s) info.
324 #ifdef RTS_GTK_FRONTPANEL
325 if (RtsFlags.GcFlags.frontpanel) {
330 #if defined(PROFILING)
331 reportCCSProfiling();
334 #if defined(PROFILING) || defined(DEBUG)
339 // Originally, this was in report_ccs_profiling(). Now, retainer
340 // profiling might tack some extra stuff on to the end of this file
341 // during endProfiling().
345 #if defined(TICKY_TICKY)
346 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
351 * called from STG-land to exit the program
355 static int exit_started=rtsFalse;
362 /* HACK: avoid a loop when exiting due to a stupid error */
365 exit_started=rtsTrue;
367 IF_PAR_DEBUG(verbose, fprintf(stderr,"==-- stg_exit %d on [%x]...", n, mytid));
368 shutdownParallelSystem(n);