1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.42 2000/04/20 13:20:31 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 */
26 #if defined(PROFILING) || defined(DEBUG)
27 # include "Profiling.h"
28 # include "ProfHeap.h"
32 #include "GranSimRts.h"
33 #include "ParallelRts.h"
45 struct RTS_FLAGS RtsFlags;
47 static int rts_has_started_up = 0;
49 static ullong startTime = 0;
52 EXTFUN(__init_Prelude);
53 static void initModules ( void * );
56 setProgArgv(int argc, char *argv[])
58 /* Usually this is done by startupHaskell, so we don't need to call this.
59 However, sometimes Hugs wants to change the arguments which Haskell
60 getArgs >>= ... will be fed. So you can do that by calling here
61 _after_ calling startupHaskell.
68 getProgArgv(int *argc, char **argv[])
76 startupHaskell(int argc, char *argv[], void *init_root)
78 /* To avoid repeated initialisations of the RTS */
79 if (rts_has_started_up)
84 /* The very first thing we do is grab the start time...just in case we're
85 * collecting timing statistics.
91 * The parallel system needs to be initialised and synchronised before
94 fprintf(stderr, "startupHaskell: argv[0]=%s\n", argv[0]);
95 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
96 IAmMainThread = rtsTrue;
97 argv++; argc--; /* Strip off flag argument */
98 // IF_PAR_DEBUG(verbose,
99 fprintf(stderr, "[%x] I am Main Thread\n", mytid);
102 * Grab the number of PEs out of the argument vector, and
103 * eliminate it from further argument processing.
105 nPEs = atoi(argv[1]);
108 initEachPEHook(); /* HWL: hook to be execed on each PE */
111 /* Set the RTS flags to default values. */
112 initRtsFlagsDefaults();
114 /* Call the user hook to reset defaults, if present */
117 /* Parse the flags, separating the RTS flags from the programs args */
118 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
123 /* NB: this really must be done after processing the RTS flags */
124 fprintf(stderr, "Synchronising system (%d PEs)\n", nPEs);
125 SynchroniseSystem(); // calls initParallelSystem etc
128 /* initialise scheduler data structures (needs to be done before
134 /* And start GranSim profiling if required: */
135 if (RtsFlags.GranFlags.GranSimStats.Full)
136 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
138 /* And start GUM profiling if required: */
139 if (RtsFlags.ParFlags.ParStats.Full)
140 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
141 #endif /* PAR || GRAN */
143 /* initialize the storage manager */
146 /* initialise the stable pointer table */
147 initStablePtrTable();
149 #if defined(PROFILING) || defined(DEBUG)
153 /* run the per-module initialisation code */
154 #if !defined(INTERPRETER)
155 initModules(init_root);
158 #if defined(PROFILING) || defined(DEBUG)
162 /* start the ticker */
163 install_vtalrm_handler();
164 initialize_virtual_timer(TICK_MILLISECS);
166 /* start our haskell execution tasks */
171 /* Initialise the stats department */
174 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
175 /* Initialise the user signal handler set */
177 /* Set up handler to run on SIGINT, etc. */
178 init_default_handlers();
181 #if !defined(INTERPRETER)
182 /* Initialise pointers from the RTS to the prelude.
183 Only for compiled code -- the interpreter
184 will call this itself later, so don't do so now.
186 fixupRTStoPreludeRefs(NULL);
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 "__init_<module>" in each
210 module and calls the registration functions in each of the modules
211 it imports. So, if we call "__init_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_SIZE (BLOCK_SIZE * 4)
222 F_ *init_stack = NULL;
227 initModules ( void *init_root )
232 #define cap MainRegTable
236 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
237 init_stack[init_sp++] = (F_)stg_init_ret;
238 init_stack[init_sp++] = (F_)__init_Prelude;
239 if (init_root != NULL) {
240 init_stack[init_sp++] = (F_)init_root;
243 cap.rSp = (P_)(init_stack + init_sp);
244 StgRun((StgFunPtr)stg_init, &cap);
246 #endif /* !INTERPRETER */
248 /* -----------------------------------------------------------------------------
249 * Shutting down the RTS - two ways of doing this, one which
250 * calls exit(), one that doesn't.
252 * (shutdownHaskellAndExit() is called by System.exitWith).
253 * -----------------------------------------------------------------------------
256 shutdownHaskellAndExit(int n)
264 shutdownHaskell(void)
266 if (!rts_has_started_up)
269 /* start timing the shutdown */
273 /* Finalize any remaining weak pointers */
274 finalizeWeakPointersNow();
278 /* end_gr_simulation prints global stats if requested -- HWL */
279 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
283 /* stop all running tasks */
286 /* stop the ticker */
287 initialize_virtual_timer(0);
289 /* reset the standard file descriptors to blocking mode */
290 resetNonBlockingFd(0);
291 resetNonBlockingFd(1);
292 resetNonBlockingFd(2);
295 shutdownParallelSystem(0);
298 /* stop timing the shutdown, we're about to print stats */
301 /* clean up things from the storage manager's point of view.
302 * also outputs the stats (+RTS -s) info.
306 #if defined(PROFILING) || defined(DEBUG)
310 #if defined(PROFILING)
311 report_ccs_profiling();
314 #if defined(TICKY_TICKY)
315 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
318 rts_has_started_up=0;
323 * called from STG-land to exit the program