calculate and report slop (wasted space at the end of blocks)
[ghc-hetmet.git] / rts / RtsStartup.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2002
4  *
5  * Main function for a standalone Haskell program.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 // PAPI uses caddr_t, which is not POSIX
10 // #include "PosixSource.h"
11
12 #include "Rts.h"
13 #include "RtsAPI.h"
14 #include "RtsUtils.h"
15 #include "RtsFlags.h"  
16 #include "OSThreads.h"
17 #include "Schedule.h"   /* initScheduler */
18 #include "Stats.h"      /* initStats */
19 #include "STM.h"        /* initSTM */
20 #include "Signals.h"
21 #include "RtsSignals.h"
22 #include "ThrIOManager.h"
23 #include "Timer.h"      /* startTimer, stopTimer */
24 #include "Weak.h"
25 #include "Ticky.h"
26 #include "StgRun.h"
27 #include "Prelude.h"            /* fixupRTStoPreludeRefs */
28 #include "HsFFI.h"
29 #include "Linker.h"
30 #include "ThreadLabels.h"
31 #include "BlockAlloc.h"
32 #include "Trace.h"
33 #include "RtsTypeable.h"
34 #include "Stable.h"
35 #include "Hpc.h"
36 #include "FileLock.h"
37
38 #if defined(RTS_GTK_FRONTPANEL)
39 #include "FrontPanel.h"
40 #endif
41
42 # include "Profiling.h"
43
44 #if defined(PROFILING)
45 # include "ProfHeap.h"
46 # include "RetainerProfile.h"
47 #endif
48
49 #if defined(GRAN)
50 # include "GranSimRts.h"
51 #endif
52
53 #if defined(GRAN) || defined(PAR)
54 # include "ParallelRts.h"
55 #endif
56
57 #if defined(PAR)
58 # include "Parallel.h"
59 # include "LLC.h"
60 #endif
61
62 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
63 #include "win32/AsyncIO.h"
64 #endif
65
66 #include <stdlib.h>
67
68 #ifdef HAVE_TERMIOS_H
69 #include <termios.h>
70 #endif
71 #ifdef HAVE_SIGNAL_H
72 #include <signal.h>
73 #endif
74
75 #if USE_PAPI
76 #include "Papi.h"
77 #endif
78
79 // Count of how many outstanding hs_init()s there have been.
80 static int hs_init_count = 0;
81
82 // Here we save the terminal settings on the standard file
83 // descriptors, if we need to change them (eg. to support NoBuffering
84 // input).
85 static void *saved_termios[3] = {NULL,NULL,NULL};
86
87 void*
88 __hscore_get_saved_termios(int fd)
89 {
90   return (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) ?
91     saved_termios[fd] : NULL;
92 }
93
94 void
95 __hscore_set_saved_termios(int fd, void* ts)
96 {
97   if (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) {
98     saved_termios[fd] = ts;
99   }
100 }
101
102 /* -----------------------------------------------------------------------------
103    Initialise floating point unit on x86 (currently disabled. why?)
104    (see comment in ghc/compiler/nativeGen/MachInstrs.lhs).
105    -------------------------------------------------------------------------- */
106
107 #define X86_INIT_FPU 0
108
109 #if X86_INIT_FPU
110 static void
111 x86_init_fpu ( void )
112 {
113   __volatile unsigned short int fpu_cw;
114
115   // Grab the control word
116   __asm __volatile ("fnstcw %0" : "=m" (fpu_cw));
117
118 #if 0
119   printf("fpu_cw: %x\n", fpu_cw);
120 #endif
121
122   // Set bits 8-9 to 10 (64-bit precision).
123   fpu_cw = (fpu_cw & 0xfcff) | 0x0200;
124
125   // Store the new control word back
126   __asm __volatile ("fldcw %0" : : "m" (fpu_cw));
127 }
128 #endif
129
130 /* -----------------------------------------------------------------------------
131    Starting up the RTS
132    -------------------------------------------------------------------------- */
133
134 void
135 hs_init(int *argc, char **argv[])
136 {
137     hs_init_count++;
138     if (hs_init_count > 1) {
139         // second and subsequent inits are ignored
140         return;
141     }
142
143     /* Initialise the stats department, phase 0 */
144     initStats0();
145
146     /* Next we do is grab the start time...just in case we're
147      * collecting timing statistics.
148      */
149     stat_startInit();
150
151 #if defined(DEBUG)
152     /* Start off by initialising the allocator debugging so we can
153      * use it anywhere */
154     initAllocator();
155 #endif
156
157 #ifdef PAR
158     /*
159      * The parallel system needs to be initialised and synchronised before
160      * the program is run.  
161      */ 
162     startupParallelSystem(argv);
163      
164     if (*argv[0] == '-') { /* Strip off mainPE flag argument */
165       argv++; 
166       argc--;                   
167     }
168
169     argv[1] = argv[0];   /* ignore the nPEs argument */
170     argv++; argc--;
171 #endif
172
173     /* Set the RTS flags to default values. */
174
175     initRtsFlagsDefaults();
176
177     /* Call the user hook to reset defaults, if present */
178     defaultsHook();
179
180     /* Parse the flags, separating the RTS flags from the programs args */
181     if (argc != NULL && argv != NULL) {
182         setFullProgArgv(*argc,*argv);
183         setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
184         setProgArgv(*argc,*argv);
185     }
186
187     /* Initialise the stats department, phase 1 */
188     initStats1();
189
190 #ifdef USE_PAPI
191     papi_init();
192 #endif
193
194     /* initTracing must be after setupRtsFlags() */
195     initTracing();
196
197 #if defined(PAR)
198     /* NB: this really must be done after processing the RTS flags */
199     IF_PAR_DEBUG(verbose,
200                  debugBelch("==== Synchronising system (%d PEs)\n", nPEs));
201     synchroniseSystem();             // calls initParallelSystem etc
202 #endif  /* PAR */
203
204     /* initialise scheduler data structures (needs to be done before
205      * initStorage()).
206      */
207     initScheduler();
208
209 #if defined(GRAN)
210     /* And start GranSim profiling if required: */
211     if (RtsFlags.GranFlags.GranSimStats.Full)
212       init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
213 #elif defined(PAR)
214     /* And start GUM profiling if required: */
215     if (RtsFlags.ParFlags.ParStats.Full)
216       init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
217 #endif  /* PAR || GRAN */
218
219     /* initialize the storage manager */
220     initStorage();
221
222     /* initialise the stable pointer table */
223     initStablePtrTable();
224
225     /* initialise the shared Typeable store */
226     initTypeableStore();
227
228     /* initialise file locking, if necessary */
229 #if !defined(mingw32_HOST_OS)    
230     initFileLocking();
231 #endif
232
233 #if defined(DEBUG)
234     /* initialise thread label table (tso->char*) */
235     initThreadLabelTable();
236 #endif
237
238     initProfiling1();
239
240     /* start the virtual timer 'subsystem'. */
241     initTimer();
242     startTimer();
243
244 #if defined(RTS_USER_SIGNALS)
245     if (RtsFlags.MiscFlags.install_signal_handlers) {
246         /* Initialise the user signal handler set */
247         initUserSignals();
248         /* Set up handler to run on SIGINT, etc. */
249         initDefaultHandlers();
250     }
251 #endif
252  
253 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
254     startupAsyncIO();
255 #endif
256
257 #ifdef RTS_GTK_FRONTPANEL
258     if (RtsFlags.GcFlags.frontpanel) {
259         initFrontPanel();
260     }
261 #endif
262
263 #if X86_INIT_FPU
264     x86_init_fpu();
265 #endif
266
267     /* Record initialization times */
268     stat_endInit();
269 }
270
271 // Compatibility interface
272 void
273 startupHaskell(int argc, char *argv[], void (*init_root)(void))
274 {
275     hs_init(&argc, &argv);
276     if(init_root)
277         hs_add_root(init_root);
278 }
279
280
281 /* -----------------------------------------------------------------------------
282    Per-module initialisation
283
284    This process traverses all the compiled modules in the program
285    starting with "Main", and performing per-module initialisation for
286    each one.
287
288    So far, two things happen at initialisation time:
289
290       - we register stable names for each foreign-exported function
291         in that module.  This prevents foreign-exported entities, and
292         things they depend on, from being garbage collected.
293
294       - we supply a unique integer to each statically declared cost
295         centre and cost centre stack in the program.
296
297    The code generator inserts a small function "__stginit_<module>" in each
298    module and calls the registration functions in each of the modules it
299    imports.
300
301    The init* functions are compiled in the same way as STG code,
302    i.e. without normal C call/return conventions.  Hence we must use
303    StgRun to call this stuff.
304    -------------------------------------------------------------------------- */
305
306 /* The init functions use an explicit stack... 
307  */
308 #define INIT_STACK_BLOCKS  4
309 static F_ *init_stack = NULL;
310
311 void
312 hs_add_root(void (*init_root)(void))
313 {
314     bdescr *bd;
315     nat init_sp;
316     Capability *cap;
317
318     cap = rts_lock();
319
320     if (hs_init_count <= 0) {
321         barf("hs_add_root() must be called after hs_init()");
322     }
323
324     /* The initialisation stack grows downward, with sp pointing 
325        to the last occupied word */
326     init_sp = INIT_STACK_BLOCKS*BLOCK_SIZE_W;
327     bd = allocGroup_lock(INIT_STACK_BLOCKS);
328     init_stack = (F_ *)bd->start;
329     init_stack[--init_sp] = (F_)stg_init_finish;
330     if (init_root != NULL) {
331         init_stack[--init_sp] = (F_)init_root;
332     }
333     
334     cap->r.rSp = (P_)(init_stack + init_sp);
335     StgRun((StgFunPtr)stg_init, &cap->r);
336
337     freeGroup_lock(bd);
338
339     startupHpc();
340
341     // This must be done after module initialisation.
342     // ToDo: make this work in the presence of multiple hs_add_root()s.
343     initProfiling2();
344
345     rts_unlock(cap);
346
347     // ditto.
348 #if defined(THREADED_RTS)
349     ioManagerStart();
350 #endif
351 }
352
353 /* ----------------------------------------------------------------------------
354  * Shutting down the RTS
355  *
356  * The wait_foreign parameter means:
357  *       True  ==> wait for any threads doing foreign calls now.
358  *       False ==> threads doing foreign calls may return in the
359  *                 future, but will immediately block on a mutex.
360  *                 (capability->lock).
361  * 
362  * If this RTS is a DLL that we're about to unload, then you want
363  * safe=True, otherwise the thread might return to code that has been
364  * unloaded.  If this is a standalone program that is about to exit,
365  * then you can get away with safe=False, which is better because we
366  * won't hang on exit if there is a blocked foreign call outstanding.
367  *
368  ------------------------------------------------------------------------- */
369
370 static void
371 hs_exit_(rtsBool wait_foreign)
372 {
373     if (hs_init_count <= 0) {
374         errorBelch("warning: too many hs_exit()s");
375         return;
376     }
377     hs_init_count--;
378     if (hs_init_count > 0) {
379         // ignore until it's the last one
380         return;
381     }
382
383     /* start timing the shutdown */
384     stat_startExit();
385     
386 #if defined(RTS_USER_SIGNALS)
387     if (RtsFlags.MiscFlags.install_signal_handlers) {
388         freeSignalHandlers();
389     }
390 #endif
391
392 #if defined(THREADED_RTS)
393     ioManagerDie();
394 #endif
395
396     /* stop all running tasks */
397     exitScheduler(wait_foreign);
398     
399 #if defined(GRAN)
400     /* end_gr_simulation prints global stats if requested -- HWL */
401     if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
402         end_gr_simulation();
403 #endif
404     
405     /* stop the ticker */
406     stopTimer();
407     exitTimer();
408
409     /* reset the standard file descriptors to blocking mode */
410     resetNonBlockingFd(0);
411     resetNonBlockingFd(1);
412     resetNonBlockingFd(2);
413
414 #if HAVE_TERMIOS_H
415     // Reset the terminal settings on the standard file descriptors,
416     // if we changed them.  See System.Posix.Internals.tcSetAttr for
417     // more details, including the reason we termporarily disable
418     // SIGTTOU here.
419     { 
420         int fd;
421         sigset_t sigset, old_sigset;
422         sigemptyset(&sigset);
423         sigaddset(&sigset, SIGTTOU);
424         sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
425         for (fd = 0; fd <= 2; fd++) {
426             struct termios* ts = (struct termios*)__hscore_get_saved_termios(fd);
427             if (ts != NULL) {
428                 tcsetattr(fd,TCSANOW,ts);
429             }
430         }
431         sigprocmask(SIG_SETMASK, &old_sigset, NULL);
432     }
433 #endif
434
435 #if defined(PAR)
436     /* controlled exit; good thread! */
437     shutdownParallelSystem(0);
438     
439     /* global statistics in parallel system */
440     PAR_TICKY_PAR_END();
441 #endif
442
443     /* stop timing the shutdown, we're about to print stats */
444     stat_endExit();
445     
446     /* shutdown the hpc support (if needed) */
447     exitHpc();
448
449     // clean up things from the storage manager's point of view.
450     // also outputs the stats (+RTS -s) info.
451     exitStorage();
452     
453     /* free the tasks */
454     freeScheduler();
455
456     /* free shared Typeable store */
457     exitTypeableStore();
458
459     /* free file locking tables, if necessary */
460 #if !defined(mingw32_HOST_OS)    
461     freeFileLocking();
462 #endif
463
464     /* free the stable pointer table */
465     exitStablePtrTable();
466
467 #if defined(DEBUG)
468     /* free the thread label table */
469     freeThreadLabelTable();
470 #endif
471
472 #ifdef RTS_GTK_FRONTPANEL
473     if (RtsFlags.GcFlags.frontpanel) {
474         stopFrontPanel();
475     }
476 #endif
477
478 #if defined(PROFILING) 
479     reportCCSProfiling();
480 #endif
481
482     endProfiling();
483     freeProfiling1();
484
485 #ifdef PROFILING
486     // Originally, this was in report_ccs_profiling().  Now, retainer
487     // profiling might tack some extra stuff on to the end of this file
488     // during endProfiling().
489     if (prof_file != NULL) fclose(prof_file);
490 #endif
491
492 #if defined(TICKY_TICKY)
493     if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
494 #endif
495
496 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
497     shutdownAsyncIO(wait_foreign);
498 #endif
499
500     /* free hash table storage */
501     exitHashTable();
502
503     // Finally, free all our storage
504     freeStorage();
505
506 #if defined(DEBUG)
507     /* and shut down the allocator debugging */
508     shutdownAllocator();
509 #endif
510
511 }
512
513 // The real hs_exit():
514 void
515 hs_exit(void)
516 {
517     hs_exit_(rtsTrue);
518     // be safe; this might be a DLL
519 }
520
521 // Compatibility interfaces
522 void
523 shutdownHaskell(void)
524 {
525     hs_exit();
526 }
527
528 void
529 shutdownHaskellAndExit(int n)
530 {
531     if (hs_init_count == 1) {
532         OnExitHook();
533         hs_exit_(rtsFalse);
534         // we're about to exit(), no need to wait for foreign calls to return.
535 #if defined(PAR)
536         /* really exit (stg_exit() would call shutdownParallelSystem() again) */
537         exit(n);
538 #else
539         stg_exit(n);
540 #endif
541     }
542 }
543
544 /* 
545  * called from STG-land to exit the program
546  */
547
548 #ifdef PAR
549 static int exit_started=rtsFalse;
550 #endif
551
552 void (*exitFn)(int) = 0;
553
554 void  
555 stg_exit(int n)
556
557 #ifdef PAR
558   /* HACK: avoid a loop when exiting due to a stupid error */
559   if (exit_started) 
560     return;
561   exit_started=rtsTrue;
562
563   IF_PAR_DEBUG(verbose, debugBelch("==-- stg_exit %d on [%x]...", n, mytid));
564   shutdownParallelSystem(n);
565 #endif
566   if (exitFn)
567     (*exitFn)(n);
568   exit(n);
569 }