1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.63 2002/05/11 00:16:12 sof 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 */
29 #if defined(RTS_GTK_FRONTPANEL)
30 #include "FrontPanel.h"
33 #if defined(PROFILING) || defined(DEBUG)
34 # include "Profiling.h"
35 # include "ProfHeap.h"
36 # include "RetainerProfile.h"
40 # include "GranSimRts.h"
43 #if defined(GRAN) || defined(PAR)
44 # include "ParallelRts.h"
48 # include "Parallel.h"
55 struct RTS_FLAGS RtsFlags;
57 static int rts_has_started_up = 0;
62 EXTFUN(__stginit_Prelude);
63 static void initModules ( void (*)(void) );
66 setProgArgv(int argc, char *argv[])
68 /* Usually this is done by startupHaskell, so we don't need to call this.
69 However, sometimes Hugs wants to change the arguments which Haskell
70 getArgs >>= ... will be fed. So you can do that by calling here
71 _after_ calling startupHaskell.
78 getProgArgv(int *argc, char **argv[])
86 startupHaskell(int argc, char *argv[], void (*init_root)(void))
88 /* To avoid repeated initialisations of the RTS */
89 if (rts_has_started_up) {
90 /* RTS is up and running, so only run the per-module initialisation code */
92 initModules(init_root);
99 /* The very first thing we do is grab the start time...just in case we're
100 * collecting timing statistics.
106 * The parallel system needs to be initialised and synchronised before
107 * the program is run.
109 startupParallelSystem(argv);
111 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
116 argv[1] = argv[0]; /* ignore the nPEs argument */
120 /* Set the RTS flags to default values. */
121 initRtsFlagsDefaults();
123 /* Call the user hook to reset defaults, if present */
126 /* Parse the flags, separating the RTS flags from the programs args */
127 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
132 /* NB: this really must be done after processing the RTS flags */
133 IF_PAR_DEBUG(verbose,
134 fprintf(stderr, "==== Synchronising system (%d PEs)\n", nPEs));
135 synchroniseSystem(); // calls initParallelSystem etc
138 /* initialise scheduler data structures (needs to be done before
144 /* And start GranSim profiling if required: */
145 if (RtsFlags.GranFlags.GranSimStats.Full)
146 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
148 /* And start GUM profiling if required: */
149 if (RtsFlags.ParFlags.ParStats.Full)
150 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
151 #endif /* PAR || GRAN */
153 /* initialize the storage manager */
156 /* initialise the stable pointer table */
157 initStablePtrTable();
159 #if defined(PROFILING) || defined(DEBUG)
163 /* run the per-module initialisation code */
164 initModules(init_root);
166 #if defined(PROFILING) || defined(DEBUG)
170 /* start the virtual timer 'subsystem'. */
171 startVirtTimer(TICK_MILLISECS);
173 /* Initialise the stats department */
176 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
177 /* Initialise the user signal handler set */
179 /* Set up handler to run on SIGINT, etc. */
180 initDefaultHandlers();
183 #ifdef RTS_GTK_FRONTPANEL
184 if (RtsFlags.GcFlags.frontpanel) {
189 /* Record initialization times */
193 /* -----------------------------------------------------------------------------
194 Per-module initialisation
196 This process traverses all the compiled modules in the program
197 starting with "Main", and performing per-module initialisation for
200 So far, two things happen at initialisation time:
202 - we register stable names for each foreign-exported function
203 in that module. This prevents foreign-exported entities, and
204 things they depend on, from being garbage collected.
206 - we supply a unique integer to each statically declared cost
207 centre and cost centre stack in the program.
209 The code generator inserts a small function "__stginit_<module>" in each
210 module and calls the registration functions in each of the modules it
211 imports. So, if we call "__stginit_PrelMain", each reachable module in the
212 program will be registered (because PrelMain.mainIO calls Main.main).
214 The init* functions are compiled in the same way as STG code,
215 i.e. without normal C call/return conventions. Hence we must use
216 StgRun to call this stuff.
217 -------------------------------------------------------------------------- */
219 /* The init functions use an explicit stack...
221 #define INIT_STACK_BLOCKS 4
222 F_ *init_stack = NULL;
226 initModules ( void (*init_root)(void) )
232 #define cap MainCapability
236 bd = allocGroup(INIT_STACK_BLOCKS);
237 init_stack = (F_ *)bd->start;
238 init_stack[init_sp++] = (F_)stg_init_ret;
239 init_stack[init_sp++] = (F_)__stginit_Prelude;
240 if (init_root != NULL) {
241 init_stack[init_sp++] = (F_)init_root;
244 cap.r.rSp = (P_)(init_stack + init_sp);
245 StgRun((StgFunPtr)stg_init, &cap.r);
250 /* -----------------------------------------------------------------------------
251 * Shutting down the RTS - two ways of doing this, one which
252 * calls exit(), one that doesn't.
254 * (shutdownHaskellAndExit() is called by System.exitWith).
255 * -----------------------------------------------------------------------------
258 shutdownHaskellAndExit(int n)
263 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
271 shutdownHaskell(void)
273 if (!rts_has_started_up)
275 rts_has_started_up=0;
277 /* start timing the shutdown */
280 /* stop all running tasks */
284 /* Finalize any remaining weak pointers */
285 finalizeWeakPointersNow();
289 /* end_gr_simulation prints global stats if requested -- HWL */
290 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
294 /* stop the ticker */
297 /* reset the standard file descriptors to blocking mode */
298 resetNonBlockingFd(0);
299 resetNonBlockingFd(1);
300 resetNonBlockingFd(2);
303 /* controlled exit; good thread! */
304 shutdownParallelSystem(0);
306 /* global statistics in parallel system */
310 /* stop timing the shutdown, we're about to print stats */
313 /* clean up things from the storage manager's point of view.
314 * also outputs the stats (+RTS -s) info.
318 #ifdef RTS_GTK_FRONTPANEL
319 if (RtsFlags.GcFlags.frontpanel) {
324 #if defined(PROFILING)
325 reportCCSProfiling();
328 #if defined(PROFILING) || defined(DEBUG)
333 // Originally, this was in report_ccs_profiling(). Now, retainer
334 // profiling might tack some extra stuff on to the end of this file
335 // during endProfiling().
339 #if defined(TICKY_TICKY)
340 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
345 * called from STG-land to exit the program
349 static int exit_started=rtsFalse;
356 /* HACK: avoid a loop when exiting due to a stupid error */
359 exit_started=rtsTrue;
361 IF_PAR_DEBUG(verbose, fprintf(stderr,"==-- stg_exit %d on [%x]...", n, mytid));
362 shutdownParallelSystem(n);