1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.45 2000/12/04 12:31:21 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 * );
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)
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 #if !defined(INTERPRETER)
169 initModules(init_root);
172 #if defined(PROFILING) || defined(DEBUG)
176 /* start the ticker */
177 install_vtalrm_handler();
178 initialize_virtual_timer(TICK_MILLISECS);
180 /* start our haskell execution tasks */
185 /* Initialise the stats department */
188 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
189 /* Initialise the user signal handler set */
191 /* Set up handler to run on SIGINT, etc. */
192 init_default_handlers();
195 #if !defined(INTERPRETER)
196 /* Initialise pointers from the RTS to the prelude.
197 Only for compiled code -- the interpreter
198 will call this itself later, so don't do so now.
200 fixupRTStoPreludeRefs(NULL);
203 #ifdef RTS_GTK_FRONTPANEL
204 if (RtsFlags.GcFlags.frontpanel) {
209 /* Record initialization times */
213 /* -----------------------------------------------------------------------------
214 Per-module initialisation
216 This process traverses all the compiled modules in the program
217 starting with "Main", and performing per-module initialisation for
220 So far, two things happen at initialisation time:
222 - we register stable names for each foreign-exported function
223 in that module. This prevents foreign-exported entities, and
224 things they depend on, from being garbage collected.
226 - we supply a unique integer to each statically declared cost
227 centre and cost centre stack in the program.
229 The code generator inserts a small function "__init_<module>" in each
230 module and calls the registration functions in each of the modules
231 it imports. So, if we call "__init_PrelMain", each reachable module in the
232 program will be registered (because PrelMain.mainIO calls Main.main).
234 The init* functions are compiled in the same way as STG code,
235 i.e. without normal C call/return conventions. Hence we must use
236 StgRun to call this stuff.
237 -------------------------------------------------------------------------- */
239 /* The init functions use an explicit stack...
241 #define INIT_STACK_SIZE (BLOCK_SIZE * 4)
242 F_ *init_stack = NULL;
247 initModules ( void *init_root )
252 #define cap MainRegTable
256 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
257 init_stack[init_sp++] = (F_)stg_init_ret;
258 init_stack[init_sp++] = (F_)__init_Prelude;
259 if (init_root != NULL) {
260 init_stack[init_sp++] = (F_)init_root;
263 cap.rSp = (P_)(init_stack + init_sp);
264 StgRun((StgFunPtr)stg_init, &cap);
266 #endif /* !INTERPRETER */
268 /* -----------------------------------------------------------------------------
269 * Shutting down the RTS - two ways of doing this, one which
270 * calls exit(), one that doesn't.
272 * (shutdownHaskellAndExit() is called by System.exitWith).
273 * -----------------------------------------------------------------------------
276 shutdownHaskellAndExit(int n)
284 shutdownHaskell(void)
286 if (!rts_has_started_up)
289 /* start timing the shutdown */
293 /* Finalize any remaining weak pointers */
294 finalizeWeakPointersNow();
298 /* end_gr_simulation prints global stats if requested -- HWL */
299 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
303 /* stop all running tasks */
306 /* stop the ticker */
307 initialize_virtual_timer(0);
309 /* reset the standard file descriptors to blocking mode */
310 resetNonBlockingFd(0);
311 resetNonBlockingFd(1);
312 resetNonBlockingFd(2);
315 shutdownParallelSystem(0);
318 /* stop timing the shutdown, we're about to print stats */
321 /* clean up things from the storage manager's point of view.
322 * also outputs the stats (+RTS -s) info.
326 #ifdef RTS_GTK_FRONTPANEL
327 if (RtsFlags.GcFlags.frontpanel) {
332 #if defined(PROFILING) || defined(DEBUG)
336 #if defined(PROFILING)
337 report_ccs_profiling();
340 #if defined(TICKY_TICKY)
341 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
344 rts_has_started_up=0;
349 * called from STG-land to exit the program