1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.33 2000/03/14 09:55:05 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" /* fixupPreludeRefs */
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 /* Initialise pointers from the RTS to the prelude */
163 /* Record initialization times */
167 /* -----------------------------------------------------------------------------
168 Per-module initialisation
170 This process traverses all the compiled modules in the program
171 starting with "Main", and performing per-module initialisation for
174 So far, two things happen at initialisation time:
176 - we register stable names for each foreign-exported function
177 in that module. This prevents foreign-exported entities, and
178 things they depend on, from being garbage collected.
180 - we supply a unique integer to each statically declared cost
181 centre and cost centre stack in the program.
183 The code generator inserts a small function "__init_<moddule>" in each
184 module and calls the registration functions in each of the modules
185 it imports. So, if we call "__init_Main", each reachable module in the
186 program will be registered.
188 The init* functions are compiled in the same way as STG code,
189 i.e. without normal C call/return conventions. Hence we must use
190 StgRun to call this stuff.
191 -------------------------------------------------------------------------- */
195 /* The init functions use an explicit stack...
197 #define INIT_STACK_SIZE (BLOCK_SIZE * 4)
203 /* this storage will be reclaimed by the garbage collector,
206 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
208 StgRun((StgFunPtr)stg_init, NULL/* no reg table */);
211 #endif /* !INTERPRETER */
213 /* -----------------------------------------------------------------------------
214 * Shutting down the RTS - two ways of doing this, one which
215 * calls exit(), one that doesn't.
217 * (shutdownHaskellAndExit() is called by System.exitWith).
218 * -----------------------------------------------------------------------------
221 shutdownHaskellAndExit(int n)
229 shutdownHaskell(void)
231 if (!rts_has_started_up)
234 /* start timing the shutdown */
238 /* Finalize any remaining weak pointers */
239 finalizeWeakPointersNow();
243 /* end_gr_simulation prints global stats if requested -- HWL */
244 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
248 /* stop all running tasks */
251 /* stop the ticker */
252 initialize_virtual_timer(0);
254 /* reset the standard file descriptors to blocking mode */
255 resetNonBlockingFd(0);
256 resetNonBlockingFd(1);
257 resetNonBlockingFd(2);
259 /* stop timing the shutdown, we're about to print stats */
262 /* clean up things from the storage manager's point of view.
263 * also outputs the stats (+RTS -s) info.
267 #if defined(PROFILING) || defined(DEBUG)
271 #if defined(PROFILING)
272 report_ccs_profiling();
275 #if defined(TICKY_TICKY)
276 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
279 rts_has_started_up=0;
282 shutdownParallelSystem(0);
288 * called from STG-land to exit the program