1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.43 2000/10/06 15:35:47 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(PROFILING) || defined(DEBUG)
32 # include "Profiling.h"
33 # include "ProfHeap.h"
37 #include "GranSimRts.h"
38 #include "ParallelRts.h"
50 struct RTS_FLAGS RtsFlags;
52 static int rts_has_started_up = 0;
54 static ullong startTime = 0;
57 EXTFUN(__init_Prelude);
58 static void initModules ( void * );
61 setProgArgv(int argc, char *argv[])
63 /* Usually this is done by startupHaskell, so we don't need to call this.
64 However, sometimes Hugs wants to change the arguments which Haskell
65 getArgs >>= ... will be fed. So you can do that by calling here
66 _after_ calling startupHaskell.
73 getProgArgv(int *argc, char **argv[])
81 startupHaskell(int argc, char *argv[], void *init_root)
83 /* To avoid repeated initialisations of the RTS */
84 if (rts_has_started_up)
89 /* The very first thing we do is grab the start time...just in case we're
90 * collecting timing statistics.
96 * The parallel system needs to be initialised and synchronised before
99 fprintf(stderr, "startupHaskell: argv[0]=%s\n", argv[0]);
100 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
101 IAmMainThread = rtsTrue;
102 argv++; argc--; /* Strip off flag argument */
103 // IF_PAR_DEBUG(verbose,
104 fprintf(stderr, "[%x] I am Main Thread\n", mytid);
107 * Grab the number of PEs out of the argument vector, and
108 * eliminate it from further argument processing.
110 nPEs = atoi(argv[1]);
113 initEachPEHook(); /* HWL: hook to be execed on each PE */
116 /* Set the RTS flags to default values. */
117 initRtsFlagsDefaults();
119 /* Call the user hook to reset defaults, if present */
122 /* Parse the flags, separating the RTS flags from the programs args */
123 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
128 /* NB: this really must be done after processing the RTS flags */
129 fprintf(stderr, "Synchronising system (%d PEs)\n", nPEs);
130 SynchroniseSystem(); // calls initParallelSystem etc
133 /* initialise scheduler data structures (needs to be done before
139 /* And start GranSim profiling if required: */
140 if (RtsFlags.GranFlags.GranSimStats.Full)
141 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
143 /* And start GUM profiling if required: */
144 if (RtsFlags.ParFlags.ParStats.Full)
145 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
146 #endif /* PAR || GRAN */
148 /* initialize the storage manager */
151 /* initialise the object linker, if necessary */
156 /* initialise the stable pointer table */
157 initStablePtrTable();
159 #if defined(PROFILING) || defined(DEBUG)
163 /* run the per-module initialisation code */
164 #if !defined(INTERPRETER)
165 initModules(init_root);
168 #if defined(PROFILING) || defined(DEBUG)
172 /* start the ticker */
173 install_vtalrm_handler();
174 initialize_virtual_timer(TICK_MILLISECS);
176 /* start our haskell execution tasks */
181 /* Initialise the stats department */
184 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
185 /* Initialise the user signal handler set */
187 /* Set up handler to run on SIGINT, etc. */
188 init_default_handlers();
191 #if !defined(INTERPRETER)
192 /* Initialise pointers from the RTS to the prelude.
193 Only for compiled code -- the interpreter
194 will call this itself later, so don't do so now.
196 fixupRTStoPreludeRefs(NULL);
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;
237 initModules ( void *init_root )
242 #define cap MainRegTable
246 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
247 init_stack[init_sp++] = (F_)stg_init_ret;
248 init_stack[init_sp++] = (F_)__init_Prelude;
249 if (init_root != NULL) {
250 init_stack[init_sp++] = (F_)init_root;
253 cap.rSp = (P_)(init_stack + init_sp);
254 StgRun((StgFunPtr)stg_init, &cap);
256 #endif /* !INTERPRETER */
258 /* -----------------------------------------------------------------------------
259 * Shutting down the RTS - two ways of doing this, one which
260 * calls exit(), one that doesn't.
262 * (shutdownHaskellAndExit() is called by System.exitWith).
263 * -----------------------------------------------------------------------------
266 shutdownHaskellAndExit(int n)
274 shutdownHaskell(void)
276 if (!rts_has_started_up)
279 /* start timing the shutdown */
283 /* Finalize any remaining weak pointers */
284 finalizeWeakPointersNow();
288 /* end_gr_simulation prints global stats if requested -- HWL */
289 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
293 /* stop all running tasks */
296 /* stop the ticker */
297 initialize_virtual_timer(0);
299 /* reset the standard file descriptors to blocking mode */
300 resetNonBlockingFd(0);
301 resetNonBlockingFd(1);
302 resetNonBlockingFd(2);
305 shutdownParallelSystem(0);
308 /* stop timing the shutdown, we're about to print stats */
311 /* clean up things from the storage manager's point of view.
312 * also outputs the stats (+RTS -s) info.
316 #if defined(PROFILING) || defined(DEBUG)
320 #if defined(PROFILING)
321 report_ccs_profiling();
324 #if defined(TICKY_TICKY)
325 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
328 rts_has_started_up=0;
333 * called from STG-land to exit the program