RtsStartup.c 8.87 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: RtsStartup.c,v 1.64 2002/06/26 08:18:41 stolz Exp $
3
 *
4
 * (c) The GHC Team, 1998-2000
5 6 7 8 9
 *
 * Main function for a standalone Haskell program.
 *
 * ---------------------------------------------------------------------------*/

10
#include "PosixSource.h"
11 12 13 14 15
#include "Rts.h"
#include "RtsAPI.h"
#include "RtsUtils.h"
#include "RtsFlags.h"  
#include "Storage.h"    /* initStorage, exitStorage */
16
#include "StablePriv.h" /* initStablePtrTable */
17 18
#include "Schedule.h"   /* initScheduler */
#include "Stats.h"      /* initStats */
19 20
#include "Signals.h"
#include "Itimer.h"
21
#include "Weak.h"
22
#include "Ticky.h"
23 24
#include "StgRun.h"
#include "StgStartup.h"
25
#include "Prelude.h"		/* fixupRTStoPreludeRefs */
26 27
#include "HsFFI.h"
#include "Linker.h"
28
#include "ThreadLabels.h"
29

30 31 32 33
#if defined(RTS_GTK_FRONTPANEL)
#include "FrontPanel.h"
#endif

34
#if defined(PROFILING) || defined(DEBUG)
35
# include "Profiling.h"
36
# include "ProfHeap.h"
37
# include "RetainerProfile.h"
38 39
#endif

40
#if defined(GRAN)
41 42 43 44 45
# include "GranSimRts.h"
#endif

#if defined(GRAN) || defined(PAR)
# include "ParallelRts.h"
46 47 48
#endif

#if defined(PAR)
49 50
# include "Parallel.h"
# include "LLC.h"
51 52 53 54 55 56 57
#endif

/*
 * Flag Structure
 */
struct RTS_FLAGS RtsFlags;

sof's avatar
sof committed
58
static int rts_has_started_up = 0;
59
#if defined(PAR)
60
ullong startTime = 0;
61
#endif
sof's avatar
sof committed
62

ken's avatar
ken committed
63
EXTFUN(__stginit_Prelude);
64
static void initModules ( void (*)(void) );
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
void
setProgArgv(int argc, char *argv[])
{
   /* Usually this is done by startupHaskell, so we don't need to call this. 
      However, sometimes Hugs wants to change the arguments which Haskell
      getArgs >>= ... will be fed.  So you can do that by calling here
      _after_ calling startupHaskell.
   */
   prog_argc = argc;
   prog_argv = argv;
}

void
getProgArgv(int *argc, char **argv[])
{
   *argc = prog_argc;
   *argv = prog_argv;
}


sof's avatar
sof committed
86
void
87
startupHaskell(int argc, char *argv[], void (*init_root)(void))
88
{
89
   /* To avoid repeated initialisations of the RTS */
sof's avatar
sof committed
90 91 92 93 94 95 96 97 98
  if (rts_has_started_up) {
    /* RTS is up and running, so only run the per-module initialisation code */
    if (init_root) {
      initModules(init_root);
    }
    return;
  } else {
    rts_has_started_up=1;
  }
sof's avatar
sof committed
99

100 101 102
    /* The very first thing we do is grab the start time...just in case we're
     * collecting timing statistics.
     */
103
    stat_startInit();
104 105

#ifdef PAR
106 107 108 109 110 111 112 113 114
    /*
     * The parallel system needs to be initialised and synchronised before
     * the program is run.  
     */ 
    startupParallelSystem(argv);
     
    if (*argv[0] == '-') { /* Strip off mainPE flag argument */
      argv++; 
      argc--;			
115
    }
116 117

    argv[1] = argv[0];   /* ignore the nPEs argument */
118 119 120 121 122 123 124 125 126 127 128 129 130 131
    argv++; argc--;
#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;

132 133
#if defined(PAR)
    /* NB: this really must be done after processing the RTS flags */
134 135 136
    IF_PAR_DEBUG(verbose,
                 fprintf(stderr, "==== Synchronising system (%d PEs)\n", nPEs));
    synchroniseSystem();             // calls initParallelSystem etc
137 138
#endif	/* PAR */

139 140 141 142 143
    /* initialise scheduler data structures (needs to be done before
     * initStorage()).
     */
    initScheduler();

144 145 146 147 148 149 150 151 152 153
#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 */

154 155 156 157 158 159
    /* initialize the storage manager */
    initStorage();

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

160 161 162
    /* initialise thread label table (tso->char*) */
    initThreadLabelTable();

163
#if defined(PROFILING) || defined(DEBUG)
164 165 166 167
    initProfiling1();
#endif

    /* run the per-module initialisation code */
168
    initModules(init_root);
169 170 171

#if defined(PROFILING) || defined(DEBUG)
    initProfiling2();
172 173
#endif

sof's avatar
sof committed
174 175
    /* start the virtual timer 'subsystem'. */
    startVirtTimer(TICK_MILLISECS);
176

177 178 179
    /* Initialise the stats department */
    initStats();

sof's avatar
sof committed
180
#if !defined(mingw32_TARGET_OS) && !defined(PAR)
sof's avatar
sof committed
181
    /* Initialise the user signal handler set */
182
    initUserSignals();
183
    /* Set up handler to run on SIGINT, etc. */
184
    initDefaultHandlers();
sof's avatar
sof committed
185
#endif
sof's avatar
sof committed
186
 
187 188 189 190 191 192
#ifdef RTS_GTK_FRONTPANEL
    if (RtsFlags.GcFlags.frontpanel) {
	initFrontPanel();
    }
#endif

193
    /* Record initialization times */
194
    stat_endInit();
195 196
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/* -----------------------------------------------------------------------------
   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.

ken's avatar
ken committed
213 214 215
   The code generator inserts a small function "__stginit_<module>" in each
   module and calls the registration functions in each of the modules it
   imports.  So, if we call "__stginit_PrelMain", each reachable module in the
216
   program will be registered (because PrelMain.mainIO calls Main.main).
217 218 219 220 221 222 223 224

   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.
   -------------------------------------------------------------------------- */

/* The init functions use an explicit stack... 
 */
225
#define INIT_STACK_BLOCKS  4
226 227
F_ *init_stack = NULL;
nat init_sp = 0;
228 229

static void
230
initModules ( void (*init_root)(void) )
231
{
232
    bdescr *bd;
233
#ifdef SMP
234
    Capability cap;
235
#else
236
#define cap MainCapability
237
#endif
238 239

    init_sp = 0;
sof's avatar
sof committed
240
    bd = allocGroup(INIT_STACK_BLOCKS);
241
    init_stack = (F_ *)bd->start;
242
    init_stack[init_sp++] = (F_)stg_init_ret;
ken's avatar
ken committed
243
    init_stack[init_sp++] = (F_)__stginit_Prelude;
244 245 246 247
    if (init_root != NULL) {
	init_stack[init_sp++] = (F_)init_root;
    }
    
248 249
    cap.r.rSp = (P_)(init_stack + init_sp);
    StgRun((StgFunPtr)stg_init, &cap.r);
250 251

    freeGroup(bd);
252
}
253

254
/* -----------------------------------------------------------------------------
sof's avatar
sof committed
255 256 257 258
 * Shutting down the RTS - two ways of doing this, one which
 * calls exit(), one that doesn't.
 *
 * (shutdownHaskellAndExit() is called by System.exitWith).
259
 * -----------------------------------------------------------------------------
sof's avatar
sof committed
260 261 262 263
 */
void
shutdownHaskellAndExit(int n)
{
264
  OnExitHook();
sof's avatar
sof committed
265
  shutdownHaskell();
266 267 268 269
#if defined(PAR)
  /* really exit (stg_exit() would call shutdownParallelSystem() again) */
  exit(n);
#else
sof's avatar
sof committed
270
  stg_exit(n);
sof's avatar
sof committed
271
#endif
sof's avatar
sof committed
272 273
}

274 275 276
void
shutdownHaskell(void)
{
sof's avatar
sof committed
277 278
  if (!rts_has_started_up)
     return;
sof's avatar
sof committed
279
  rts_has_started_up=0;
sof's avatar
sof committed
280
  
281 282 283
  /* start timing the shutdown */
  stat_startExit();

sof's avatar
sof committed
284 285 286
  /* stop all running tasks */
  exitScheduler();

287
#if !defined(GRAN)
288 289
  /* Finalize any remaining weak pointers */
  finalizeWeakPointersNow();
290
#endif
291 292

#if defined(GRAN)
293 294
  /* end_gr_simulation prints global stats if requested -- HWL */
  if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
295 296 297
    end_gr_simulation();
#endif

298
  /* stop the ticker */
sof's avatar
sof committed
299
  stopVirtTimer();
sof's avatar
sof committed
300
  
301 302 303 304 305
  /* reset the standard file descriptors to blocking mode */
  resetNonBlockingFd(0);
  resetNonBlockingFd(1);
  resetNonBlockingFd(2);

306
#if defined(PAR)
307
  /* controlled exit; good thread! */
308
  shutdownParallelSystem(0);
309 310 311

  /* global statistics in parallel system */
  PAR_TICKY_PAR_END();
312 313
#endif

314 315 316 317 318 319 320 321
  /* 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();

322 323 324 325 326 327
#ifdef RTS_GTK_FRONTPANEL
    if (RtsFlags.GcFlags.frontpanel) {
	stopFrontPanel();
    }
#endif

328
#if defined(PROFILING) 
329
  reportCCSProfiling();
330 331
#endif

332 333 334 335
#if defined(PROFILING) || defined(DEBUG)
  endProfiling();
#endif

336 337 338 339 340
#ifdef PROFILING
  // Originally, this was in report_ccs_profiling().  Now, retainer
  // profiling might tack some extra stuff on to the end of this file
  // during endProfiling().
  fclose(prof_file);
341 342 343
#endif

#if defined(TICKY_TICKY)
344
  if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
345
#endif
346
}
347 348

/* 
349
 * called from STG-land to exit the program
350 351
 */

352 353 354 355
#ifdef PAR
static int exit_started=rtsFalse;
#endif

356 357
void  
stg_exit(I_ n)
358 359 360 361 362 363 364 365 366
{ 
#ifdef PAR
  /* HACK: avoid a loop when exiting due to a stupid error */
  if (exit_started) 
    return;
  exit_started=rtsTrue;

  IF_PAR_DEBUG(verbose, fprintf(stderr,"==-- stg_exit %d on [%x]...", n, mytid));
  shutdownParallelSystem(n);
367
#endif
368
  exit(n);
369
}
370