FIX #2234: don't generate <prog>.prof unless we're going to put something in it
[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 #if defined(DEBUG)
144     /* Start off by initialising the allocator debugging so we can
145      * use it anywhere */
146     initAllocator();
147 #endif
148
149     /* Next we do is grab the start time...just in case we're
150      * collecting timing statistics.
151      */
152     stat_startInit();
153
154 #ifdef PAR
155     /*
156      * The parallel system needs to be initialised and synchronised before
157      * the program is run.  
158      */ 
159     startupParallelSystem(argv);
160      
161     if (*argv[0] == '-') { /* Strip off mainPE flag argument */
162       argv++; 
163       argc--;                   
164     }
165
166     argv[1] = argv[0];   /* ignore the nPEs argument */
167     argv++; argc--;
168 #endif
169
170     /* Initialise the performance tracking library */
171 #ifdef USE_PAPI
172     {
173         int ver;
174         if ((ver = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) {
175             if (ver > 0) {
176                 errorBelch("PAPI_library_init: wrong version: %x", ver);
177                 stg_exit(EXIT_FAILURE);
178             } else {
179                 sysErrorBelch("PAPI_library_init");
180                 stg_exit(EXIT_FAILURE);
181             }
182         }
183     }
184 #ifdef THREADED_RTS
185     {
186         int err;
187         if ((err = PAPI_thread_init(osThreadId)) < 0) {
188             barf("PAPI_thread_init: %d",err);
189         }
190     }
191 #endif
192 #endif
193
194     /* Set the RTS flags to default values. */
195
196     initRtsFlagsDefaults();
197
198     /* Call the user hook to reset defaults, if present */
199     defaultsHook();
200
201     /* Parse the flags, separating the RTS flags from the programs args */
202     if (argc != NULL && argv != NULL) {
203         setFullProgArgv(*argc,*argv);
204         setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
205         setProgArgv(*argc,*argv);
206     }
207
208     /* initTracing must be after setupRtsFlags() */
209     initTracing();
210
211 #if defined(PAR)
212     /* NB: this really must be done after processing the RTS flags */
213     IF_PAR_DEBUG(verbose,
214                  debugBelch("==== Synchronising system (%d PEs)\n", nPEs));
215     synchroniseSystem();             // calls initParallelSystem etc
216 #endif  /* PAR */
217
218     /* initialise scheduler data structures (needs to be done before
219      * initStorage()).
220      */
221     initScheduler();
222
223 #if defined(GRAN)
224     /* And start GranSim profiling if required: */
225     if (RtsFlags.GranFlags.GranSimStats.Full)
226       init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
227 #elif defined(PAR)
228     /* And start GUM profiling if required: */
229     if (RtsFlags.ParFlags.ParStats.Full)
230       init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
231 #endif  /* PAR || GRAN */
232
233     /* initialize the storage manager */
234     initStorage();
235
236     /* initialise the stable pointer table */
237     initStablePtrTable();
238
239     /* initialise the shared Typeable store */
240     initTypeableStore();
241
242     /* initialise file locking, if necessary */
243 #if !defined(mingw32_HOST_OS)    
244     initFileLocking();
245 #endif
246
247 #if defined(DEBUG)
248     /* initialise thread label table (tso->char*) */
249     initThreadLabelTable();
250 #endif
251
252     initProfiling1();
253
254     /* start the virtual timer 'subsystem'. */
255     initTimer();
256     startTimer();
257
258     /* Initialise the stats department */
259     initStats();
260
261 #if defined(RTS_USER_SIGNALS)
262     if (RtsFlags.MiscFlags.install_signal_handlers) {
263         /* Initialise the user signal handler set */
264         initUserSignals();
265         /* Set up handler to run on SIGINT, etc. */
266         initDefaultHandlers();
267     }
268 #endif
269  
270 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
271     startupAsyncIO();
272 #endif
273
274 #ifdef RTS_GTK_FRONTPANEL
275     if (RtsFlags.GcFlags.frontpanel) {
276         initFrontPanel();
277     }
278 #endif
279
280 #if X86_INIT_FPU
281     x86_init_fpu();
282 #endif
283
284     /* Record initialization times */
285     stat_endInit();
286 }
287
288 // Compatibility interface
289 void
290 startupHaskell(int argc, char *argv[], void (*init_root)(void))
291 {
292     hs_init(&argc, &argv);
293     if(init_root)
294         hs_add_root(init_root);
295 }
296
297
298 /* -----------------------------------------------------------------------------
299    Per-module initialisation
300
301    This process traverses all the compiled modules in the program
302    starting with "Main", and performing per-module initialisation for
303    each one.
304
305    So far, two things happen at initialisation time:
306
307       - we register stable names for each foreign-exported function
308         in that module.  This prevents foreign-exported entities, and
309         things they depend on, from being garbage collected.
310
311       - we supply a unique integer to each statically declared cost
312         centre and cost centre stack in the program.
313
314    The code generator inserts a small function "__stginit_<module>" in each
315    module and calls the registration functions in each of the modules it
316    imports.
317
318    The init* functions are compiled in the same way as STG code,
319    i.e. without normal C call/return conventions.  Hence we must use
320    StgRun to call this stuff.
321    -------------------------------------------------------------------------- */
322
323 /* The init functions use an explicit stack... 
324  */
325 #define INIT_STACK_BLOCKS  4
326 static F_ *init_stack = NULL;
327
328 void
329 hs_add_root(void (*init_root)(void))
330 {
331     bdescr *bd;
332     nat init_sp;
333     Capability *cap = &MainCapability;
334
335     if (hs_init_count <= 0) {
336         barf("hs_add_root() must be called after hs_init()");
337     }
338
339     /* The initialisation stack grows downward, with sp pointing 
340        to the last occupied word */
341     init_sp = INIT_STACK_BLOCKS*BLOCK_SIZE_W;
342     bd = allocGroup_lock(INIT_STACK_BLOCKS);
343     init_stack = (F_ *)bd->start;
344     init_stack[--init_sp] = (F_)stg_init_finish;
345     if (init_root != NULL) {
346         init_stack[--init_sp] = (F_)init_root;
347     }
348     
349     cap->r.rSp = (P_)(init_stack + init_sp);
350     StgRun((StgFunPtr)stg_init, &cap->r);
351
352     freeGroup_lock(bd);
353
354     startupHpc();
355
356     // This must be done after module initialisation.
357     // ToDo: make this work in the presence of multiple hs_add_root()s.
358     initProfiling2();
359
360     // ditto.
361 #if defined(THREADED_RTS)
362     ioManagerStart();
363 #endif
364 }
365
366 /* ----------------------------------------------------------------------------
367  * Shutting down the RTS
368  *
369  * The wait_foreign parameter means:
370  *       True  ==> wait for any threads doing foreign calls now.
371  *       False ==> threads doing foreign calls may return in the
372  *                 future, but will immediately block on a mutex.
373  *                 (capability->lock).
374  * 
375  * If this RTS is a DLL that we're about to unload, then you want
376  * safe=True, otherwise the thread might return to code that has been
377  * unloaded.  If this is a standalone program that is about to exit,
378  * then you can get away with safe=False, which is better because we
379  * won't hang on exit if there is a blocked foreign call outstanding.
380  *
381  ------------------------------------------------------------------------- */
382
383 static void
384 hs_exit_(rtsBool wait_foreign)
385 {
386     if (hs_init_count <= 0) {
387         errorBelch("warning: too many hs_exit()s");
388         return;
389     }
390     hs_init_count--;
391     if (hs_init_count > 0) {
392         // ignore until it's the last one
393         return;
394     }
395
396     /* start timing the shutdown */
397     stat_startExit();
398     
399 #if defined(RTS_USER_SIGNALS)
400     if (RtsFlags.MiscFlags.install_signal_handlers) {
401         freeSignalHandlers();
402     }
403 #endif
404
405 #if defined(THREADED_RTS)
406     ioManagerDie();
407 #endif
408
409     /* stop all running tasks */
410     exitScheduler(wait_foreign);
411     
412 #if defined(GRAN)
413     /* end_gr_simulation prints global stats if requested -- HWL */
414     if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
415         end_gr_simulation();
416 #endif
417     
418     /* stop the ticker */
419     stopTimer();
420     exitTimer();
421
422     /* reset the standard file descriptors to blocking mode */
423     resetNonBlockingFd(0);
424     resetNonBlockingFd(1);
425     resetNonBlockingFd(2);
426
427 #if HAVE_TERMIOS_H
428     // Reset the terminal settings on the standard file descriptors,
429     // if we changed them.  See System.Posix.Internals.tcSetAttr for
430     // more details, including the reason we termporarily disable
431     // SIGTTOU here.
432     { 
433         int fd;
434         sigset_t sigset, old_sigset;
435         sigemptyset(&sigset);
436         sigaddset(&sigset, SIGTTOU);
437         sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
438         for (fd = 0; fd <= 2; fd++) {
439             struct termios* ts = (struct termios*)__hscore_get_saved_termios(fd);
440             if (ts != NULL) {
441                 tcsetattr(fd,TCSANOW,ts);
442             }
443         }
444         sigprocmask(SIG_SETMASK, &old_sigset, NULL);
445     }
446 #endif
447
448 #if defined(PAR)
449     /* controlled exit; good thread! */
450     shutdownParallelSystem(0);
451     
452     /* global statistics in parallel system */
453     PAR_TICKY_PAR_END();
454 #endif
455
456     /* stop timing the shutdown, we're about to print stats */
457     stat_endExit();
458     
459     /* shutdown the hpc support (if needed) */
460     exitHpc();
461
462     // clean up things from the storage manager's point of view.
463     // also outputs the stats (+RTS -s) info.
464     exitStorage();
465     
466     /* free the tasks */
467     freeScheduler();
468
469     /* free shared Typeable store */
470     exitTypeableStore();
471
472     /* free file locking tables, if necessary */
473 #if !defined(mingw32_HOST_OS)    
474     freeFileLocking();
475 #endif
476
477     /* free the stable pointer table */
478     exitStablePtrTable();
479
480 #if defined(DEBUG)
481     /* free the thread label table */
482     freeThreadLabelTable();
483 #endif
484
485 #ifdef RTS_GTK_FRONTPANEL
486     if (RtsFlags.GcFlags.frontpanel) {
487         stopFrontPanel();
488     }
489 #endif
490
491 #if defined(PROFILING) 
492     reportCCSProfiling();
493 #endif
494
495     endProfiling();
496     freeProfiling1();
497
498 #ifdef PROFILING
499     // Originally, this was in report_ccs_profiling().  Now, retainer
500     // profiling might tack some extra stuff on to the end of this file
501     // during endProfiling().
502     if (prof_file != NULL) fclose(prof_file);
503 #endif
504
505 #if defined(TICKY_TICKY)
506     if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
507 #endif
508
509 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
510     shutdownAsyncIO(wait_foreign);
511 #endif
512
513     /* free hash table storage */
514     exitHashTable();
515
516     // Finally, free all our storage
517     freeStorage();
518
519 #if defined(DEBUG)
520     /* and shut down the allocator debugging */
521     shutdownAllocator();
522 #endif
523
524 }
525
526 // The real hs_exit():
527 void
528 hs_exit(void)
529 {
530     hs_exit_(rtsTrue);
531     // be safe; this might be a DLL
532 }
533
534 // Compatibility interfaces
535 void
536 shutdownHaskell(void)
537 {
538     hs_exit();
539 }
540
541 void
542 shutdownHaskellAndExit(int n)
543 {
544     if (hs_init_count == 1) {
545         OnExitHook();
546         hs_exit_(rtsFalse);
547         // we're about to exit(), no need to wait for foreign calls to return.
548 #if defined(PAR)
549         /* really exit (stg_exit() would call shutdownParallelSystem() again) */
550         exit(n);
551 #else
552         stg_exit(n);
553 #endif
554     }
555 }
556
557 /* 
558  * called from STG-land to exit the program
559  */
560
561 #ifdef PAR
562 static int exit_started=rtsFalse;
563 #endif
564
565 void (*exitFn)(int) = 0;
566
567 void  
568 stg_exit(int n)
569
570 #ifdef PAR
571   /* HACK: avoid a loop when exiting due to a stupid error */
572   if (exit_started) 
573     return;
574   exit_started=rtsTrue;
575
576   IF_PAR_DEBUG(verbose, debugBelch("==-- stg_exit %d on [%x]...", n, mytid));
577   shutdownParallelSystem(n);
578 #endif
579   if (exitFn)
580     (*exitFn)(n);
581   exit(n);
582 }