1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.48 2001/02/09 13:09:16 simonmar Exp $
4 * (c) The GHC Team, 1998-2000
6 * Main function for a standalone Haskell program.
8 * ---------------------------------------------------------------------------*/
14 #include "Storage.h" /* initStorage, exitStorage */
15 #include "StablePriv.h" /* initStablePtrTable */
16 #include "Schedule.h" /* initScheduler */
17 #include "Stats.h" /* initStats */
23 #include "StgStartup.h"
24 #include "Prelude.h" /* fixupRTStoPreludeRefs */
31 #if defined(RTS_GTK_FRONTPANEL)
32 #include "FrontPanel.h"
35 #if defined(PROFILING) || defined(DEBUG)
36 # include "Profiling.h"
37 # include "ProfHeap.h"
41 #include "GranSimRts.h"
42 #include "ParallelRts.h"
54 struct RTS_FLAGS RtsFlags;
56 static int rts_has_started_up = 0;
58 static ullong startTime = 0;
61 EXTFUN(__init_Prelude);
62 static void initModules ( void (*)(void) );
65 setProgArgv(int argc, char *argv[])
67 /* Usually this is done by startupHaskell, so we don't need to call this.
68 However, sometimes Hugs wants to change the arguments which Haskell
69 getArgs >>= ... will be fed. So you can do that by calling here
70 _after_ calling startupHaskell.
77 getProgArgv(int *argc, char **argv[])
85 startupHaskell(int argc, char *argv[], void (*init_root)(void))
87 /* To avoid repeated initialisations of the RTS */
88 if (rts_has_started_up)
93 /* The very first thing we do is grab the start time...just in case we're
94 * collecting timing statistics.
100 * The parallel system needs to be initialised and synchronised before
101 * the program is run.
103 fprintf(stderr, "startupHaskell: argv[0]=%s\n", argv[0]);
104 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
105 IAmMainThread = rtsTrue;
106 argv++; argc--; /* Strip off flag argument */
107 // IF_PAR_DEBUG(verbose,
108 fprintf(stderr, "[%x] I am Main Thread\n", mytid);
111 * Grab the number of PEs out of the argument vector, and
112 * eliminate it from further argument processing.
114 nPEs = atoi(argv[1]);
117 initEachPEHook(); /* HWL: hook to be execed on each PE */
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 fprintf(stderr, "Synchronising system (%d PEs)\n", nPEs);
134 SynchroniseSystem(); // calls initParallelSystem etc
137 /* initialise scheduler data structures (needs to be done before
143 /* And start GranSim profiling if required: */
144 if (RtsFlags.GranFlags.GranSimStats.Full)
145 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
147 /* And start GUM profiling if required: */
148 if (RtsFlags.ParFlags.ParStats.Full)
149 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
150 #endif /* PAR || GRAN */
152 /* initialize the storage manager */
155 /* initialise the object linker, if necessary */
160 /* initialise the stable pointer table */
161 initStablePtrTable();
163 #if defined(PROFILING) || defined(DEBUG)
167 /* run the per-module initialisation code */
168 initModules(init_root);
170 #if defined(PROFILING) || defined(DEBUG)
174 /* start the ticker */
175 install_vtalrm_handler();
176 initialize_virtual_timer(TICK_MILLISECS);
178 /* start our haskell execution tasks */
183 /* Initialise the stats department */
186 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
187 /* Initialise the user signal handler set */
189 /* Set up handler to run on SIGINT, etc. */
190 init_default_handlers();
193 #ifdef RTS_GTK_FRONTPANEL
194 if (RtsFlags.GcFlags.frontpanel) {
199 /* Record initialization times */
203 /* -----------------------------------------------------------------------------
204 Per-module initialisation
206 This process traverses all the compiled modules in the program
207 starting with "Main", and performing per-module initialisation for
210 So far, two things happen at initialisation time:
212 - we register stable names for each foreign-exported function
213 in that module. This prevents foreign-exported entities, and
214 things they depend on, from being garbage collected.
216 - we supply a unique integer to each statically declared cost
217 centre and cost centre stack in the program.
219 The code generator inserts a small function "__init_<module>" in each
220 module and calls the registration functions in each of the modules
221 it imports. So, if we call "__init_PrelMain", each reachable module in the
222 program will be registered (because PrelMain.mainIO calls Main.main).
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_SIZE (BLOCK_SIZE * 4)
232 F_ *init_stack = NULL;
236 initModules ( void (*init_root)(void) )
241 #define cap MainRegTable
245 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
246 init_stack[init_sp++] = (F_)stg_init_ret;
247 init_stack[init_sp++] = (F_)__init_Prelude;
248 if (init_root != NULL) {
249 init_stack[init_sp++] = (F_)init_root;
252 cap.rSp = (P_)(init_stack + init_sp);
253 StgRun((StgFunPtr)stg_init, &cap);
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)
272 shutdownHaskell(void)
274 if (!rts_has_started_up)
277 /* start timing the shutdown */
281 /* Finalize any remaining weak pointers */
282 finalizeWeakPointersNow();
286 /* end_gr_simulation prints global stats if requested -- HWL */
287 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
291 /* stop all running tasks */
294 /* stop the ticker */
295 initialize_virtual_timer(0);
297 /* reset the standard file descriptors to blocking mode */
298 resetNonBlockingFd(0);
299 resetNonBlockingFd(1);
300 resetNonBlockingFd(2);
303 shutdownParallelSystem(0);
306 /* stop timing the shutdown, we're about to print stats */
309 /* clean up things from the storage manager's point of view.
310 * also outputs the stats (+RTS -s) info.
314 #ifdef RTS_GTK_FRONTPANEL
315 if (RtsFlags.GcFlags.frontpanel) {
320 #if defined(PROFILING) || defined(DEBUG)
324 #if defined(PROFILING)
325 report_ccs_profiling();
328 #if defined(TICKY_TICKY)
329 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
332 rts_has_started_up=0;
337 * called from STG-land to exit the program