1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.34 2000/03/14 14:34:47 sewardj 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)
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 static void initModules ( void );
55 startupHaskell(int argc, char *argv[])
57 /* To avoid repeated initialisations of the RTS */
58 if (rts_has_started_up)
63 /* The very first thing we do is grab the start time...just in case we're
64 * collecting timing statistics.
70 * The parallel system needs to be initialised and synchronised before
73 fprintf(stderr, "startupHaskell: argv[0]=%s\n", argv[0]);
74 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
75 IAmMainThread = rtsTrue;
76 argv++; argc--; /* Strip off flag argument */
77 // IF_PAR_DEBUG(verbose,
78 fprintf(stderr, "[%x] I am Main Thread\n", mytid);
81 * Grab the number of PEs out of the argument vector, and
82 * eliminate it from further argument processing.
87 initEachPEHook(); /* HWL: hook to be execed on each PE */
90 /* Set the RTS flags to default values. */
91 initRtsFlagsDefaults();
93 /* Call the user hook to reset defaults, if present */
96 /* Parse the flags, separating the RTS flags from the programs args */
97 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
102 /* NB: this really must be done after processing the RTS flags */
103 fprintf(stderr, "Synchronising system (%d PEs)\n", nPEs);
104 SynchroniseSystem(); // calls initParallelSystem etc
107 /* initialise scheduler data structures (needs to be done before
113 /* And start GranSim profiling if required: */
114 if (RtsFlags.GranFlags.GranSimStats.Full)
115 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
117 /* And start GUM profiling if required: */
118 if (RtsFlags.ParFlags.ParStats.Full)
119 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
120 #endif /* PAR || GRAN */
122 /* initialize the storage manager */
125 /* initialise the stable pointer table */
126 initStablePtrTable();
128 #if defined(PROFILING) || defined(DEBUG)
132 /* run the per-module initialisation code */
133 #if !defined(INTERPRETER)
137 #if defined(PROFILING) || defined(DEBUG)
141 /* start the ticker */
142 install_vtalrm_handler();
143 initialize_virtual_timer(TICK_MILLISECS);
145 /* start our haskell execution tasks */
150 /* Initialise the stats department */
153 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
154 /* Initialise the user signal handler set */
156 /* Set up handler to run on SIGINT, etc. */
157 init_default_handlers();
160 #if !defined(INTERPRETER)
161 /* Initialise pointers from the RTS to the prelude.
162 Only for compiled code -- the interpreter
163 will call this itself later, so don't do so now.
165 fixupRTStoPreludeRefs(NULL);
168 /* Record initialization times */
172 /* -----------------------------------------------------------------------------
173 Per-module initialisation
175 This process traverses all the compiled modules in the program
176 starting with "Main", and performing per-module initialisation for
179 So far, two things happen at initialisation time:
181 - we register stable names for each foreign-exported function
182 in that module. This prevents foreign-exported entities, and
183 things they depend on, from being garbage collected.
185 - we supply a unique integer to each statically declared cost
186 centre and cost centre stack in the program.
188 The code generator inserts a small function "__init_<module>" in each
189 module and calls the registration functions in each of the modules
190 it imports. So, if we call "__init_Main", each reachable module in the
191 program will be registered.
193 The init* functions are compiled in the same way as STG code,
194 i.e. without normal C call/return conventions. Hence we must use
195 StgRun to call this stuff.
196 -------------------------------------------------------------------------- */
200 /* The init functions use an explicit stack...
202 #define INIT_STACK_SIZE (BLOCK_SIZE * 4)
208 /* this storage will be reclaimed by the garbage collector,
211 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
213 StgRun((StgFunPtr)stg_init, NULL/* no reg table */);
216 #endif /* !INTERPRETER */
218 /* -----------------------------------------------------------------------------
219 * Shutting down the RTS - two ways of doing this, one which
220 * calls exit(), one that doesn't.
222 * (shutdownHaskellAndExit() is called by System.exitWith).
223 * -----------------------------------------------------------------------------
226 shutdownHaskellAndExit(int n)
234 shutdownHaskell(void)
236 if (!rts_has_started_up)
239 /* start timing the shutdown */
243 /* Finalize any remaining weak pointers */
244 finalizeWeakPointersNow();
248 /* end_gr_simulation prints global stats if requested -- HWL */
249 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
253 /* stop all running tasks */
256 /* stop the ticker */
257 initialize_virtual_timer(0);
259 /* reset the standard file descriptors to blocking mode */
260 resetNonBlockingFd(0);
261 resetNonBlockingFd(1);
262 resetNonBlockingFd(2);
264 /* stop timing the shutdown, we're about to print stats */
267 /* clean up things from the storage manager's point of view.
268 * also outputs the stats (+RTS -s) info.
272 #if defined(PROFILING) || defined(DEBUG)
276 #if defined(PROFILING)
277 report_ccs_profiling();
280 #if defined(TICKY_TICKY)
281 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
284 rts_has_started_up=0;
287 shutdownParallelSystem(0);
293 * called from STG-land to exit the program