RtsStartup.c 7.48 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: RtsStartup.c,v 1.34 2000/03/14 14:34:47 sewardj Exp $
3
 *
4
 * (c) The GHC Team, 1998-2000
5 6 7 8 9 10 11 12 13 14
 *
 * Main function for a standalone Haskell program.
 *
 * ---------------------------------------------------------------------------*/

#include "Rts.h"
#include "RtsAPI.h"
#include "RtsUtils.h"
#include "RtsFlags.h"  
#include "Storage.h"    /* initStorage, exitStorage */
15
#include "StablePriv.h" /* initStablePtrTable */
16 17
#include "Schedule.h"   /* initScheduler */
#include "Stats.h"      /* initStats */
18 19
#include "Signals.h"
#include "Itimer.h"
20
#include "Weak.h"
21
#include "Ticky.h"
22 23
#include "StgRun.h"
#include "StgStartup.h"
24
#include "Prelude.h"		/* fixupRTStoPreludeRefs */
25

26
#if defined(PROFILING) || defined(DEBUG)
27
# include "ProfRts.h"
28
# include "ProfHeap.h"
29 30
#endif

31 32 33 34 35 36
#if defined(GRAN)
#include "GranSimRts.h"
#include "ParallelRts.h"
#endif

#if defined(PAR)
37 38 39 40 41 42 43 44 45 46
#include "ParInit.h"
#include "Parallel.h"
#include "LLC.h"
#endif

/*
 * Flag Structure
 */
struct RTS_FLAGS RtsFlags;

sof's avatar
sof committed
47
static int rts_has_started_up = 0;
48 49 50
#if defined(PAR)
static ullong startTime = 0;
#endif
sof's avatar
sof committed
51

52 53
static void initModules ( void );

sof's avatar
sof committed
54 55
void
startupHaskell(int argc, char *argv[])
56
{
sof's avatar
sof committed
57 58 59 60 61 62
    /* To avoid repeated initialisations of the RTS */
   if (rts_has_started_up)
     return;
   else
     rts_has_started_up=1;

63 64 65 66 67 68 69
    /* The very first thing we do is grab the start time...just in case we're
     * collecting timing statistics.
     */
    start_time();

#ifdef PAR
/*
70 71
 * The parallel system needs to be initialised and synchronised before
 * the program is run.  
72
 */
73
    fprintf(stderr, "startupHaskell: argv[0]=%s\n", argv[0]);
74 75 76
    if (*argv[0] == '-') {     /* Look to see whether we're the Main Thread */
	IAmMainThread = rtsTrue;
        argv++; argc--;			/* Strip off flag argument */
77 78
	// IF_PAR_DEBUG(verbose,
		     fprintf(stderr, "[%x] I am Main Thread\n", mytid);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    }
    /* 
     * Grab the number of PEs out of the argument vector, and
     * eliminate it from further argument processing.
     */
    nPEs = atoi(argv[1]);
    argv[1] = argv[0];
    argv++; argc--;
    initEachPEHook();                  /* HWL: hook to be execed on each PE */
#endif

    /* Set the RTS flags to default values. */
    initRtsFlagsDefaults();

    /* Call the user hook to reset defaults, if present */
    defaultsHook();

    /* Parse the flags, separating the RTS flags from the programs args */
    setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
    prog_argc = argc;
    prog_argv = argv;

101 102 103 104
#if defined(PAR)
    /* NB: this really must be done after processing the RTS flags */
    fprintf(stderr, "Synchronising system (%d PEs)\n", nPEs);
    SynchroniseSystem();             // calls initParallelSystem etc
105 106
#endif	/* PAR */

107 108 109 110 111
    /* initialise scheduler data structures (needs to be done before
     * initStorage()).
     */
    initScheduler();

112 113 114 115 116 117 118 119 120 121
#if defined(GRAN)
    /* And start GranSim profiling if required: */
    if (RtsFlags.GranFlags.GranSimStats.Full)
      init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
#elif defined(PAR)
    /* And start GUM profiling if required: */
    if (RtsFlags.ParFlags.ParStats.Full)
      init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
#endif	/* PAR || GRAN */

122 123 124 125 126 127 128
    /* initialize the storage manager */
    initStorage();

    /* initialise the stable pointer table */
    initStablePtrTable();

#if defined(PROFILING) || defined(DEBUG)
129 130 131 132
    initProfiling1();
#endif

    /* run the per-module initialisation code */
133
#if !defined(INTERPRETER)
134
    initModules();
135
#endif
136 137 138

#if defined(PROFILING) || defined(DEBUG)
    initProfiling2();
139 140
#endif

141 142 143 144
    /* start the ticker */
    install_vtalrm_handler();
    initialize_virtual_timer(TICK_MILLISECS);

145 146 147 148
    /* start our haskell execution tasks */
#ifdef SMP
    startTasks();
#endif
149 150 151 152

    /* Initialise the stats department */
    initStats();

sof's avatar
sof committed
153
#if !defined(mingw32_TARGET_OS) && !defined(PAR)
sof's avatar
sof committed
154
    /* Initialise the user signal handler set */
155
    initUserSignals();
156 157
    /* Set up handler to run on SIGINT, etc. */
    init_default_handlers();
sof's avatar
sof committed
158
#endif
sof's avatar
sof committed
159
 
160 161 162 163 164 165 166
#if !defined(INTERPRETER)
    /* Initialise pointers from the RTS to the prelude.  
       Only for compiled code -- the interpreter
       will call this itself later, so don't do so now.
    */
    fixupRTStoPreludeRefs(NULL);
#endif
167

168 169 170 171
    /* Record initialization times */
    end_init();
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/* -----------------------------------------------------------------------------
   Per-module initialisation

   This process traverses all the compiled modules in the program
   starting with "Main", and performing per-module initialisation for
   each one.

   So far, two things happen at initialisation time:

      - we register stable names for each foreign-exported function
        in that module.  This prevents foreign-exported entities, and
	things they depend on, from being garbage collected.

      - we supply a unique integer to each statically declared cost
        centre and cost centre stack in the program.

188
   The code generator inserts a small function "__init_<module>" in each
189 190 191 192 193 194 195 196 197
   module and calls the registration functions in each of the modules
   it imports.  So, if we call "__init_Main", each reachable module in the
   program will be registered.

   The init* functions are compiled in the same way as STG code,
   i.e. without normal C call/return conventions.  Hence we must use
   StgRun to call this stuff.
   -------------------------------------------------------------------------- */

198 199
#ifndef INTERPRETER

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/* The init functions use an explicit stack... 
 */
#define INIT_STACK_SIZE  (BLOCK_SIZE * 4)
F_ *init_stack;

static void
initModules ( void )
{
  /* this storage will be reclaimed by the garbage collector,
   * as a large block.
   */
  init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));

  StgRun((StgFunPtr)stg_init, NULL/* no reg table */);
}

216 217
#endif /* !INTERPRETER */

218
/* -----------------------------------------------------------------------------
sof's avatar
sof committed
219 220 221 222
 * Shutting down the RTS - two ways of doing this, one which
 * calls exit(), one that doesn't.
 *
 * (shutdownHaskellAndExit() is called by System.exitWith).
223
 * -----------------------------------------------------------------------------
sof's avatar
sof committed
224 225 226 227
 */
void
shutdownHaskellAndExit(int n)
{
228
  OnExitHook();
sof's avatar
sof committed
229 230 231 232
  shutdownHaskell();
  stg_exit(n);
}

233 234 235
void
shutdownHaskell(void)
{
sof's avatar
sof committed
236 237 238
  if (!rts_has_started_up)
     return;

239 240 241
  /* start timing the shutdown */
  stat_startExit();

242
#if !defined(GRAN)
243 244
  /* Finalize any remaining weak pointers */
  finalizeWeakPointersNow();
245
#endif
246 247

#if defined(GRAN)
248 249
  /* end_gr_simulation prints global stats if requested -- HWL */
  if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
250 251 252
    end_gr_simulation();
#endif

253 254 255
  /* stop all running tasks */
  exitScheduler();

256 257
  /* stop the ticker */
  initialize_virtual_timer(0);
sof's avatar
sof committed
258
  
259 260 261 262 263
  /* reset the standard file descriptors to blocking mode */
  resetNonBlockingFd(0);
  resetNonBlockingFd(1);
  resetNonBlockingFd(2);

264 265 266 267 268 269 270 271
  /* stop timing the shutdown, we're about to print stats */
  stat_endExit();

  /* clean up things from the storage manager's point of view.
   * also outputs the stats (+RTS -s) info.
   */
  exitStorage();

272 273 274 275 276
#if defined(PROFILING) || defined(DEBUG)
  endProfiling();
#endif

#if defined(PROFILING) 
277
  report_ccs_profiling();
278 279 280
#endif

#if defined(TICKY_TICKY)
281
  if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
282 283
#endif

sof's avatar
sof committed
284
  rts_has_started_up=0;
285

286 287 288 289 290
#if defined(PAR)
  shutdownParallelSystem(0);
#endif

}
291 292

/* 
293
 * called from STG-land to exit the program
294 295 296 297 298
 */

void  
stg_exit(I_ n)
{
299
#if 0 /* def PAR */
300 301 302 303 304
  par_exit(n);
#else
  exit(n);
#endif
}
305