1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.49 2001/02/11 17:51:08 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 */
28 #if defined(RTS_GTK_FRONTPANEL)
29 #include "FrontPanel.h"
32 #if defined(PROFILING) || defined(DEBUG)
33 # include "Profiling.h"
34 # include "ProfHeap.h"
38 #include "GranSimRts.h"
39 #include "ParallelRts.h"
51 struct RTS_FLAGS RtsFlags;
53 static int rts_has_started_up = 0;
55 static ullong startTime = 0;
58 EXTFUN(__init_Prelude);
59 static void initModules ( void (*)(void) );
62 setProgArgv(int argc, char *argv[])
64 /* Usually this is done by startupHaskell, so we don't need to call this.
65 However, sometimes Hugs wants to change the arguments which Haskell
66 getArgs >>= ... will be fed. So you can do that by calling here
67 _after_ calling startupHaskell.
74 getProgArgv(int *argc, char **argv[])
82 startupHaskell(int argc, char *argv[], void (*init_root)(void))
84 /* To avoid repeated initialisations of the RTS */
85 if (rts_has_started_up)
90 /* The very first thing we do is grab the start time...just in case we're
91 * collecting timing statistics.
97 * The parallel system needs to be initialised and synchronised before
100 fprintf(stderr, "startupHaskell: argv[0]=%s\n", argv[0]);
101 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
102 IAmMainThread = rtsTrue;
103 argv++; argc--; /* Strip off flag argument */
104 // IF_PAR_DEBUG(verbose,
105 fprintf(stderr, "[%x] I am Main Thread\n", mytid);
108 * Grab the number of PEs out of the argument vector, and
109 * eliminate it from further argument processing.
111 nPEs = atoi(argv[1]);
114 initEachPEHook(); /* HWL: hook to be execed on each PE */
117 /* Set the RTS flags to default values. */
118 initRtsFlagsDefaults();
120 /* Call the user hook to reset defaults, if present */
123 /* Parse the flags, separating the RTS flags from the programs args */
124 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
129 /* NB: this really must be done after processing the RTS flags */
130 fprintf(stderr, "Synchronising system (%d PEs)\n", nPEs);
131 SynchroniseSystem(); // calls initParallelSystem etc
134 /* initialise scheduler data structures (needs to be done before
140 /* And start GranSim profiling if required: */
141 if (RtsFlags.GranFlags.GranSimStats.Full)
142 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
144 /* And start GUM profiling if required: */
145 if (RtsFlags.ParFlags.ParStats.Full)
146 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
147 #endif /* PAR || GRAN */
149 /* initialize the storage manager */
152 /* initialise the stable pointer table */
153 initStablePtrTable();
155 #if defined(PROFILING) || defined(DEBUG)
159 /* run the per-module initialisation code */
160 initModules(init_root);
162 #if defined(PROFILING) || defined(DEBUG)
166 /* start the ticker */
167 install_vtalrm_handler();
168 initialize_virtual_timer(TICK_MILLISECS);
170 /* start our haskell execution tasks */
175 /* Initialise the stats department */
178 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
179 /* Initialise the user signal handler set */
181 /* Set up handler to run on SIGINT, etc. */
182 init_default_handlers();
185 #ifdef RTS_GTK_FRONTPANEL
186 if (RtsFlags.GcFlags.frontpanel) {
191 /* Record initialization times */
195 /* -----------------------------------------------------------------------------
196 Per-module initialisation
198 This process traverses all the compiled modules in the program
199 starting with "Main", and performing per-module initialisation for
202 So far, two things happen at initialisation time:
204 - we register stable names for each foreign-exported function
205 in that module. This prevents foreign-exported entities, and
206 things they depend on, from being garbage collected.
208 - we supply a unique integer to each statically declared cost
209 centre and cost centre stack in the program.
211 The code generator inserts a small function "__init_<module>" in each
212 module and calls the registration functions in each of the modules
213 it imports. So, if we call "__init_PrelMain", each reachable module in the
214 program will be registered (because PrelMain.mainIO calls Main.main).
216 The init* functions are compiled in the same way as STG code,
217 i.e. without normal C call/return conventions. Hence we must use
218 StgRun to call this stuff.
219 -------------------------------------------------------------------------- */
221 /* The init functions use an explicit stack...
223 #define INIT_STACK_SIZE (BLOCK_SIZE * 4)
224 F_ *init_stack = NULL;
228 initModules ( void (*init_root)(void) )
233 #define cap MainRegTable
237 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
238 init_stack[init_sp++] = (F_)stg_init_ret;
239 init_stack[init_sp++] = (F_)__init_Prelude;
240 if (init_root != NULL) {
241 init_stack[init_sp++] = (F_)init_root;
244 cap.rSp = (P_)(init_stack + init_sp);
245 StgRun((StgFunPtr)stg_init, &cap);
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 #ifdef RTS_GTK_FRONTPANEL
307 if (RtsFlags.GcFlags.frontpanel) {
312 #if defined(PROFILING) || defined(DEBUG)
316 #if defined(PROFILING)
317 report_ccs_profiling();
320 #if defined(TICKY_TICKY)
321 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
324 rts_has_started_up=0;
329 * called from STG-land to exit the program