1 /* -----------------------------------------------------------------------------
3 * (c) The AQUA Project, Glasgow University, 1994-1997
4 * (c) The GHC Team, 1998-2006
6 * Functions for parsing the argument list.
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
15 #include "Profiling.h"
28 * Split argument lists
30 int prog_argc = 0; /* an "int" so as to match normal "argc" */
31 char **prog_argv = NULL;
32 int full_prog_argc = 0; /* an "int" so as to match normal "argc" */
33 char **full_prog_argv = NULL;
34 char *prog_name = NULL; /* 'basename' of prog_argv[0] */
35 int rts_argc = 0; /* ditto */
36 char **rts_argv = NULL;
37 #if defined(mingw32_HOST_OS)
38 // On Windows, we want to use GetCommandLineW rather than argc/argv,
39 // but we need to mutate the command line arguments for withProgName and
40 // friends. The System.Environment module achieves that using this bit of
42 int win32_prog_argc = 0;
43 wchar_t **win32_prog_argv = NULL;
47 * constants, used later
52 /* -----------------------------------------------------------------------------
54 -------------------------------------------------------------------------- */
56 static void procRtsOpts (int rts_argc0, RtsOptsEnabledEnum enabled);
58 static void normaliseRtsOpts (void);
60 static void initStatsFile (FILE *f);
62 static int openStatsFile (char *filename, const char *FILENAME_FMT,
65 static StgWord64 decodeSize (const char *flag, nat offset,
66 StgWord64 min, StgWord64 max);
68 static void bad_option (const char *s);
71 static void read_trace_flags(char *arg);
74 static void errorUsage (void) GNU_ATTRIBUTE(__noreturn__);
76 static char * copyArg (char *arg);
77 static char ** copyArgv (int argc, char *argv[]);
78 static void freeArgv (int argc, char *argv[]);
80 /* -----------------------------------------------------------------------------
81 * Command-line option parsing routines.
82 * ---------------------------------------------------------------------------*/
84 void initRtsFlagsDefaults(void)
86 RtsFlags.GcFlags.statsFile = NULL;
87 RtsFlags.GcFlags.giveStats = NO_GC_STATS;
89 RtsFlags.GcFlags.maxStkSize = (8 * 1024 * 1024) / sizeof(W_);
90 RtsFlags.GcFlags.initialStkSize = 1024 / sizeof(W_);
91 RtsFlags.GcFlags.stkChunkSize = (32 * 1024) / sizeof(W_);
92 RtsFlags.GcFlags.stkChunkBufferSize = (1 * 1024) / sizeof(W_);
94 RtsFlags.GcFlags.minAllocAreaSize = (512 * 1024) / BLOCK_SIZE;
95 RtsFlags.GcFlags.minOldGenSize = (1024 * 1024) / BLOCK_SIZE;
96 RtsFlags.GcFlags.maxHeapSize = 0; /* off by default */
97 RtsFlags.GcFlags.heapSizeSuggestion = 0; /* none */
98 RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsFalse;
99 RtsFlags.GcFlags.pcFreeHeap = 3; /* 3% */
100 RtsFlags.GcFlags.oldGenFactor = 2;
101 RtsFlags.GcFlags.generations = 2;
102 RtsFlags.GcFlags.squeezeUpdFrames = rtsTrue;
103 RtsFlags.GcFlags.compact = rtsFalse;
104 RtsFlags.GcFlags.compactThreshold = 30.0;
105 RtsFlags.GcFlags.sweep = rtsFalse;
106 #ifdef RTS_GTK_FRONTPANEL
107 RtsFlags.GcFlags.frontpanel = rtsFalse;
109 RtsFlags.GcFlags.idleGCDelayTime = 300; /* millisecs */
112 /* ToDo: Perhaps by adjusting this value we can make linking without
113 * -static work (i.e., not generate a core-dumping executable)? */
114 # if SIZEOF_VOID_P == 8
115 RtsFlags.GcFlags.heapBase = 0x180000000L;
117 # error I have no idea where to begin the heap on a non-64-bit osf3 machine.
120 RtsFlags.GcFlags.heapBase = 0; /* means don't care */
124 RtsFlags.DebugFlags.scheduler = rtsFalse;
125 RtsFlags.DebugFlags.interpreter = rtsFalse;
126 RtsFlags.DebugFlags.weak = rtsFalse;
127 RtsFlags.DebugFlags.gccafs = rtsFalse;
128 RtsFlags.DebugFlags.gc = rtsFalse;
129 RtsFlags.DebugFlags.block_alloc = rtsFalse;
130 RtsFlags.DebugFlags.sanity = rtsFalse;
131 RtsFlags.DebugFlags.stable = rtsFalse;
132 RtsFlags.DebugFlags.stm = rtsFalse;
133 RtsFlags.DebugFlags.prof = rtsFalse;
134 RtsFlags.DebugFlags.apply = rtsFalse;
135 RtsFlags.DebugFlags.linker = rtsFalse;
136 RtsFlags.DebugFlags.squeeze = rtsFalse;
137 RtsFlags.DebugFlags.hpc = rtsFalse;
138 RtsFlags.DebugFlags.sparks = rtsFalse;
141 #if defined(PROFILING)
142 RtsFlags.CcFlags.doCostCentres = 0;
143 #endif /* PROFILING */
145 RtsFlags.ProfFlags.doHeapProfile = rtsFalse;
146 RtsFlags.ProfFlags.profileInterval = 100;
149 RtsFlags.ProfFlags.includeTSOs = rtsFalse;
150 RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
151 RtsFlags.ProfFlags.maxRetainerSetSize = 8;
152 RtsFlags.ProfFlags.ccsLength = 25;
153 RtsFlags.ProfFlags.modSelector = NULL;
154 RtsFlags.ProfFlags.descrSelector = NULL;
155 RtsFlags.ProfFlags.typeSelector = NULL;
156 RtsFlags.ProfFlags.ccSelector = NULL;
157 RtsFlags.ProfFlags.ccsSelector = NULL;
158 RtsFlags.ProfFlags.retainerSelector = NULL;
159 RtsFlags.ProfFlags.bioSelector = NULL;
163 RtsFlags.TraceFlags.tracing = TRACE_NONE;
164 RtsFlags.TraceFlags.timestamp = rtsFalse;
165 RtsFlags.TraceFlags.scheduler = rtsFalse;
168 RtsFlags.MiscFlags.tickInterval = 20; /* In milliseconds */
169 RtsFlags.ConcFlags.ctxtSwitchTime = 20; /* In milliseconds */
171 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
172 RtsFlags.MiscFlags.machineReadable = rtsFalse;
173 RtsFlags.MiscFlags.linkerMemBase = 0;
176 RtsFlags.ParFlags.nNodes = 1;
177 RtsFlags.ParFlags.migrate = rtsTrue;
178 RtsFlags.ParFlags.parGcEnabled = 1;
179 RtsFlags.ParFlags.parGcGen = 0;
180 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
181 RtsFlags.ParFlags.parGcLoadBalancingGen = 1;
182 RtsFlags.ParFlags.setAffinity = 0;
185 #if defined(THREADED_RTS)
186 RtsFlags.ParFlags.maxLocalSparks = 4096;
187 #endif /* THREADED_RTS */
190 RtsFlags.TickyFlags.showTickyStats = rtsFalse;
191 RtsFlags.TickyFlags.tickyFile = NULL;
195 /* By default no special measurements taken */
196 RtsFlags.PapiFlags.eventType = 0;
197 RtsFlags.PapiFlags.numUserEvents = 0;
204 "Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
206 " +RTS Indicates run time system options follow",
207 " -RTS Indicates program arguments follow",
208 " --RTS Indicates that ALL subsequent arguments will be given to the",
209 " program (including any of these RTS flags)",
211 "The following run time system options are available:",
213 " -? Prints this message and exits; the program is not executed",
214 " --info Print information about the RTS used by this program",
216 " -K<size> Sets the maximum stack size (default 8M) Egs: -K32k -K512k",
217 " -ki<size> Sets the initial thread stack size (default 1k) Egs: -ki4k -ki2m",
218 " -kc<size> Sets the stack chunk size (default 32k)",
219 " -kb<size> Sets the stack chunk buffer size (default 1k)",
221 " -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
222 " -M<size> Sets the maximum heap size (default unlimited) Egs: -M256k -M1G",
223 " -H<size> Sets the minimum heap size (default 0M) Egs: -H24m -H1G",
224 " -m<n> Minimum % of heap which must be available (default 3%)",
225 " -G<n> Number of generations (default: 2)",
226 " -c<n> Use in-place compaction instead of copying in the oldest generation",
227 " when live data is at least <n>% of the maximum heap size set with",
228 " -M (default: 30%)",
229 " -c Use in-place compaction for all oldest generation collections",
230 " (the default is to use copying)",
231 " -w Use mark-region for the oldest generation (experimental)",
232 #if defined(THREADED_RTS)
233 " -I<sec> Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
236 " -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
237 " -s[<file>] Summary GC statistics (if <file> omitted, uses stderr)",
238 " -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
239 #ifdef RTS_GTK_FRONTPANEL
240 " -f Display front panel (requires X11 & GTK+)",
244 " -Z Don't squeeze out update frames on stack overflow",
245 " -B Sound the bell at the start of each garbage collection",
246 #if defined(PROFILING)
248 " -px Time/allocation profile (XML) (output file <program>.prof)",
249 " -p Time/allocation profile (output file <program>.prof)",
250 " -P More detailed Time/Allocation profile",
251 " -Pa Give information about *all* cost centres",
253 # if defined(PROFILING)
255 " -hx Heap residency profile (XML) (output file <program>.prof)",
256 " -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
257 " break-down: c = cost centre stack (default)",
259 " d = closure description",
260 " y = type description",
262 " b = biography (LAG,DRAG,VOID,USE)",
263 " A subset of closures may be selected thusly:",
264 " -hc<cc>,... specific cost centre(s) (top of stack only)",
265 " -hC<cc>,... specific cost centre(s) (anywhere in stack)",
266 " -hm<mod>... all cost centres from the specified modules(s)",
267 " -hd<des>,... closures with specified closure descriptions",
268 " -hy<typ>... closures with specified type descriptions",
269 " -hr<cc>... closures with specified retainers",
270 " -hb<bio>... closures with specified biographies (lag,drag,void,use)",
272 " -R<size> Set the maximum retainer set size (default: 8)",
274 " -L<chars> Maximum length of a cost-centre stack in a heap profile",
277 " -xt Include threads (TSOs) in a heap profile",
279 " -xc Show current cost centre stack on raising an exception",
281 #endif /* PROFILING or PAR */
285 " -l[flags] Log events in binary format to the file <program>.eventlog",
287 " -v[flags] Log events to stderr",
289 " where [flags] can contain:",
290 " s scheduler events",
292 " t add time stamps (only useful with -v)",
296 #if !defined(PROFILING)
298 " -hT Heap residency profile (output file <program>.hp)",
300 " -i<sec> Time between heap samples (seconds, default: 0.1)",
302 #if defined(TICKY_TICKY)
303 " -r<file> Produce ticky-ticky statistics (with -rstderr for stderr)",
306 " -C<secs> Context-switch interval in seconds.",
307 " 0 or no argument means switch as often as possible.",
308 " Default: 0.02 sec; resolution is set by -V below.",
309 " -V<secs> Master tick interval in seconds (0 == disable timer).",
310 " This sets the resolution for -C and the profile timer -i.",
311 " Default: 0.02 sec.",
314 " -Ds DEBUG: scheduler",
315 " -Di DEBUG: interpreter",
317 " -DG DEBUG: gccafs",
320 " -DS DEBUG: sanity",
321 " -Dt DEBUG: stable",
323 " -De DEBUG: event logging",
325 " -Dl DEBUG: linker",
327 " -Dz DEBUG: stack squeezing",
328 " -Dc DEBUG: program coverage",
329 " -Dr DEBUG: sparks",
331 " NOTE: DEBUG events are sent to stderr by default; add -l to create a",
332 " binary event log file instead.",
335 #if defined(THREADED_RTS) && !defined(NOSMP)
336 " -N<n> Use <n> processors (default: 1)",
337 " -N Determine the number of processors to use automatically",
338 " -qg[<n>] Use parallel GC only for generations >= <n>",
339 " (default: 0, -qg alone turns off parallel GC)",
340 " -qb[<n>] Use load-balancing in the parallel GC only for generations >= <n>",
341 " (default: 1, -qb alone turns off load-balancing)",
342 " -qa Use the OS to set thread affinity (experimental)",
343 " -qm Don't automatically migrate threads between CPUs",
345 " --install-signal-handlers=<yes|no>",
346 " Install signal handlers (default: yes)",
347 #if defined(THREADED_RTS)
348 " -e<n> Maximum number of outstanding local sparks (default: 4096)",
350 #if defined(x86_64_HOST_ARCH)
351 " -xm Base address to mmap memory in the GHCi linker",
352 " (hex; must be <80000000)",
354 #if defined(USE_PAPI)
355 " -aX CPU performance counter measurements using PAPI",
356 " (use with the -s<file> option). X is one of:",
359 " 1 - level 1 cache misses",
360 " 2 - level 2 cache misses",
361 " b - branch mispredictions",
362 " s - stalled cycles",
363 " e - cache miss and branch misprediction events",
364 " +PAPI_EVENT - collect papi preset event PAPI_EVENT",
365 " #NATIVE_EVENT - collect native event NATIVE_EVENT (in hex)",
368 "RTS options may also be specified using the GHCRTS environment variable.",
370 "Other RTS options may be available for programs compiled a different way.",
371 "The GHC User's Guide has full details.",
376 STATIC_INLINE rtsBool
377 strequal(const char *a, const char * b)
379 return(strcmp(a, b) == 0);
382 static void splitRtsFlags(char *s)
388 while (isspace(*c1)) { c1++; };
390 while (!isspace(*c2) && *c2 != '\0') { c2++; };
392 if (c1 == c2) { break; }
394 s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
395 strncpy(s, c1, c2-c1);
397 rts_argv[rts_argc++] = s;
400 } while (*c1 != '\0');
403 /* -----------------------------------------------------------------------------
404 Parse the command line arguments, collecting options for the RTS.
407 - argv[] is *modified*, any RTS options have been stripped out
408 - *argc contains the new count of arguments in argv[]
410 - rts_argv[] (global) contains a copy of the collected RTS args
411 - rts_argc (global) contains the count of args in rts_argv
413 - prog_argv[] (global) contains a copy of the non-RTS args (== argv)
414 - prog_argc (global) contains the count of args in prog_argv
416 - prog_name (global) contains the basename of prog_argv[0]
418 -------------------------------------------------------------------------- */
420 void setupRtsFlags (int *argc, char *argv[])
433 rts_argv = stgCallocBytes(total_arg + 1, sizeof (char *), "setupRtsFlags");
435 rts_argc0 = rts_argc;
437 // process arguments from the ghc_rts_opts global variable first.
438 // (arguments from the GHCRTS environment variable and the command
439 // line override these).
441 if (ghc_rts_opts != NULL) {
442 splitRtsFlags(ghc_rts_opts);
443 // opts from ghc_rts_opts are always enabled:
444 procRtsOpts(rts_argc0, RtsOptsAll);
445 rts_argc0 = rts_argc;
449 // process arguments from the GHCRTS environment variable next
450 // (arguments from the command line override these).
452 char *ghc_rts = getenv("GHCRTS");
454 if (ghc_rts != NULL) {
455 if (rtsOptsEnabled == RtsOptsNone) {
456 errorBelch("Warning: Ignoring GHCRTS variable as RTS options are disabled.\n Link with -rtsopts to enable them.");
457 // We don't actually exit, just warn
459 splitRtsFlags(ghc_rts);
460 procRtsOpts(rts_argc0, rtsOptsEnabled);
461 rts_argc0 = rts_argc;
466 // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
467 // argv[0] must be PGM argument -- leave in argv
469 for (mode = PGM; arg < total_arg; arg++) {
470 // The '--RTS' argument disables all future +RTS ... -RTS processing.
471 if (strequal("--RTS", argv[arg])) {
475 // The '--' argument is passed through to the program, but
476 // disables all further +RTS ... -RTS processing.
477 else if (strequal("--", argv[arg])) {
480 else if (strequal("+RTS", argv[arg])) {
483 else if (strequal("-RTS", argv[arg])) {
486 else if (mode == RTS) {
487 rts_argv[rts_argc++] = copyArg(argv[arg]);
490 argv[(*argc)++] = argv[arg];
493 // process remaining program arguments
494 for (; arg < total_arg; arg++) {
495 argv[(*argc)++] = argv[arg];
497 argv[*argc] = (char *) 0;
498 rts_argv[rts_argc] = (char *) 0;
500 procRtsOpts(rts_argc0, rtsOptsEnabled);
504 setProgArgv(*argc, argv);
506 if (RtsFlags.GcFlags.statsFile != NULL) {
507 initStatsFile (RtsFlags.GcFlags.statsFile);
509 if (RtsFlags.TickyFlags.tickyFile != NULL) {
510 initStatsFile (RtsFlags.GcFlags.statsFile);
514 /* -----------------------------------------------------------------------------
515 * procRtsOpts: Process rts_argv between rts_argc0 and rts_argc.
516 * -------------------------------------------------------------------------- */
518 static void procRtsOpts (int rts_argc0, RtsOptsEnabledEnum enabled)
520 rtsBool error = rtsFalse;
523 // Process RTS (rts_argv) part: mainly to determine statsfile
524 for (arg = rts_argc0; arg < rts_argc; arg++) {
525 if (rts_argv[arg][0] != '-') {
527 errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
532 if (enabled == RtsOptsNone) {
533 errorBelch("RTS options are disabled. Link with -rtsopts to enable them.");
534 stg_exit(EXIT_FAILURE);
537 switch(rts_argv[arg][1]) {
539 if (strequal("info", &rts_argv[arg][2])) {
548 if (enabled == RtsOptsSafeOnly) {
549 errorBelch("Most RTS options are disabled. Link with -rtsopts to enable them.");
550 stg_exit(EXIT_FAILURE);
553 switch(rts_argv[arg][1]) {
555 /* process: general args, then PROFILING-only ones, then
556 CONCURRENT-only, TICKY-only (same order as defined in
557 RtsFlags.lh); within those groups, mostly in
558 case-insensitive alphabetical order. Final group is
559 x*, which allows for more options.
563 # define TICKY_BUILD_ONLY(x) x
565 # define TICKY_BUILD_ONLY(x) \
566 errorBelch("the flag %s requires the program to be built with -ticky", rts_argv[arg]); \
571 # define PROFILING_BUILD_ONLY(x) x
573 # define PROFILING_BUILD_ONLY(x) \
574 errorBelch("the flag %s requires the program to be built with -prof", rts_argv[arg]); \
579 # define TRACING_BUILD_ONLY(x) x
581 # define TRACING_BUILD_ONLY(x) \
582 errorBelch("the flag %s requires the program to be built with -eventlog or -debug", rts_argv[arg]); \
587 # define THREADED_BUILD_ONLY(x) x
589 # define THREADED_BUILD_ONLY(x) \
590 errorBelch("the flag %s requires the program to be built with -threaded", rts_argv[arg]); \
595 # define DEBUG_BUILD_ONLY(x) x
597 # define DEBUG_BUILD_ONLY(x) \
598 errorBelch("the flag %s requires the program to be built with -debug", rts_argv[arg]); \
602 /* =========== GENERAL ========================== */
607 /* This isn't going to allow us to keep related options
608 together as we add more --* flags. We really need a
609 proper options parser. */
611 if (strequal("install-signal-handlers=yes",
612 &rts_argv[arg][2])) {
613 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
615 else if (strequal("install-signal-handlers=no",
616 &rts_argv[arg][2])) {
617 RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
619 else if (strequal("machine-readable",
620 &rts_argv[arg][2])) {
621 RtsFlags.MiscFlags.machineReadable = rtsTrue;
623 else if (strequal("info",
624 &rts_argv[arg][2])) {
629 errorBelch("unknown RTS option: %s",rts_argv[arg]);
634 RtsFlags.GcFlags.minAllocAreaSize
635 = decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_INT_MAX)
641 switch(rts_argv[arg][2]) {
643 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
646 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
649 RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
652 RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
655 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
659 if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
660 errorBelch("maximum number of PAPI events reached");
661 stg_exit(EXIT_FAILURE);
663 nat eventNum = RtsFlags.PapiFlags.numUserEvents++;
664 char kind = rts_argv[arg][2];
665 nat eventKind = kind == '+' ? PAPI_PRESET_EVENT_KIND : PAPI_NATIVE_EVENT_KIND;
667 RtsFlags.PapiFlags.userEvents[eventNum] = rts_argv[arg] + 3;
668 RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
669 RtsFlags.PapiFlags.userEventsKind[eventNum] = eventKind;
672 bad_option( rts_argv[arg] );
678 RtsFlags.GcFlags.ringBell = rtsTrue;
682 if (rts_argv[arg][2] != '\0') {
683 RtsFlags.GcFlags.compactThreshold =
684 atof(rts_argv[arg]+2);
686 RtsFlags.GcFlags.compact = rtsTrue;
691 RtsFlags.GcFlags.sweep = rtsTrue;
695 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
697 if (RtsFlags.GcFlags.oldGenFactor < 0)
698 bad_option( rts_argv[arg] );
706 for (c = rts_argv[arg] + 2; *c != '\0'; c++) {
709 RtsFlags.DebugFlags.scheduler = rtsTrue;
712 RtsFlags.DebugFlags.interpreter = rtsTrue;
715 RtsFlags.DebugFlags.weak = rtsTrue;
718 RtsFlags.DebugFlags.gccafs = rtsTrue;
721 RtsFlags.DebugFlags.gc = rtsTrue;
724 RtsFlags.DebugFlags.block_alloc = rtsTrue;
727 RtsFlags.DebugFlags.sanity = rtsTrue;
730 RtsFlags.DebugFlags.stable = rtsTrue;
733 RtsFlags.DebugFlags.prof = rtsTrue;
736 RtsFlags.DebugFlags.linker = rtsTrue;
739 RtsFlags.DebugFlags.apply = rtsTrue;
742 RtsFlags.DebugFlags.stm = rtsTrue;
745 RtsFlags.DebugFlags.squeeze = rtsTrue;
748 RtsFlags.DebugFlags.hpc = rtsTrue;
751 RtsFlags.DebugFlags.sparks = rtsTrue;
754 bad_option( rts_argv[arg] );
757 // -Dx also turns on -v. Use -l to direct trace
758 // events to the .eventlog file instead.
759 RtsFlags.TraceFlags.tracing = TRACE_STDERR;
764 RtsFlags.GcFlags.maxStkSize =
765 decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
769 switch(rts_argv[arg][2]) {
771 RtsFlags.GcFlags.stkChunkSize =
772 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
775 RtsFlags.GcFlags.stkChunkBufferSize =
776 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
779 RtsFlags.GcFlags.initialStkSize =
780 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
783 RtsFlags.GcFlags.initialStkSize =
784 decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
790 RtsFlags.GcFlags.maxHeapSize =
791 decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE;
792 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
796 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
798 if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
799 RtsFlags.GcFlags.pcFreeHeap > 100)
800 bad_option( rts_argv[arg] );
804 RtsFlags.GcFlags.generations =
805 decodeSize(rts_argv[arg], 2, 1, HS_INT_MAX);
809 if (rts_argv[arg][2] == '\0') {
810 RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsTrue;
812 RtsFlags.GcFlags.heapSizeSuggestion =
813 (nat)(decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE);
817 #ifdef RTS_GTK_FRONTPANEL
819 RtsFlags.GcFlags.frontpanel = rtsTrue;
823 case 'I': /* idle GC delay */
824 if (rts_argv[arg][2] == '\0') {
829 /* Convert to millisecs */
830 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
831 RtsFlags.GcFlags.idleGCDelayTime = cst;
836 RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
840 RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
844 RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
850 r = openStatsFile(rts_argv[arg]+2, NULL,
851 &RtsFlags.GcFlags.statsFile);
852 if (r == -1) { error = rtsTrue; }
857 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
860 /* =========== PROFILING ========================== */
862 case 'P': /* detailed cost centre profiling (time/alloc) */
863 case 'p': /* cost centre profiling (time/alloc) */
864 PROFILING_BUILD_ONLY(
865 switch (rts_argv[arg][2]) {
867 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
870 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
873 if (rts_argv[arg][1] == 'P') {
874 RtsFlags.CcFlags.doCostCentres =
875 COST_CENTRES_VERBOSE;
877 RtsFlags.CcFlags.doCostCentres =
878 COST_CENTRES_SUMMARY;
885 PROFILING_BUILD_ONLY(
886 RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
889 PROFILING_BUILD_ONLY(
890 RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
891 if(RtsFlags.ProfFlags.ccsLength <= 0) {
892 bad_option(rts_argv[arg]);
895 case 'h': /* serial heap profile */
896 #if !defined(PROFILING)
897 switch (rts_argv[arg][2]) {
900 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
903 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
907 PROFILING_BUILD_ONLY(
908 switch (rts_argv[arg][2]) {
922 if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
924 char *left = strchr(rts_argv[arg], '{');
925 char *right = strrchr(rts_argv[arg], '}');
927 // curly braces are optional, for
932 left = rts_argv[arg] + 3;
935 right = rts_argv[arg] + strlen(rts_argv[arg]);
939 switch (rts_argv[arg][2]) {
940 case 'c': // cost centre label select
941 RtsFlags.ProfFlags.ccSelector = left;
944 RtsFlags.ProfFlags.ccsSelector = left;
947 case 'm': // cost centre module select
948 RtsFlags.ProfFlags.modSelector = left;
951 case 'd': // closure descr select
952 RtsFlags.ProfFlags.descrSelector = left;
955 case 'y': // closure type select
956 RtsFlags.ProfFlags.typeSelector = left;
959 case 'r': // retainer select
960 RtsFlags.ProfFlags.retainerSelector = left;
963 case 'b': // biography select
964 RtsFlags.ProfFlags.bioSelector = left;
971 if (RtsFlags.ProfFlags.doHeapProfile != 0) {
972 errorBelch("multiple heap profile options");
977 switch (rts_argv[arg][2]) {
981 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
985 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
989 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
993 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
997 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
1001 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
1007 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
1011 #endif /* PROFILING */
1014 case 'i': /* heap sample interval */
1015 if (rts_argv[arg][2] == '\0') {
1020 /* Convert to milliseconds */
1021 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1022 RtsFlags.ProfFlags.profileInterval = cst;
1026 /* =========== CONCURRENT ========================= */
1027 case 'C': /* context switch interval */
1028 if (rts_argv[arg][2] == '\0')
1029 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1033 /* Convert to milliseconds */
1034 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1035 RtsFlags.ConcFlags.ctxtSwitchTime = cst;
1039 case 'V': /* master tick interval */
1040 if (rts_argv[arg][2] == '\0') {
1041 // turns off ticks completely
1042 RtsFlags.MiscFlags.tickInterval = 0;
1046 /* Convert to milliseconds */
1047 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1048 RtsFlags.MiscFlags.tickInterval = cst;
1054 THREADED_BUILD_ONLY(
1055 if (rts_argv[arg][2] == '\0') {
1056 #if defined(PROFILING)
1057 RtsFlags.ParFlags.nNodes = 1;
1059 RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
1062 RtsFlags.ParFlags.nNodes
1063 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1064 if (RtsFlags.ParFlags.nNodes <= 0) {
1065 errorBelch("bad value for -N");
1068 #if defined(PROFILING)
1069 if (RtsFlags.ParFlags.nNodes > 1) {
1070 errorBelch("bad option %s: only -N1 is supported with profiling", rts_argv[arg]);
1078 THREADED_BUILD_ONLY(
1079 switch (rts_argv[arg][2]) {
1081 // backwards compat only
1082 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1085 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1092 THREADED_BUILD_ONLY(
1093 switch (rts_argv[arg][2]) {
1095 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1099 if (rts_argv[arg][3] == '\0') {
1100 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1102 RtsFlags.ParFlags.parGcEnabled = rtsTrue;
1103 RtsFlags.ParFlags.parGcGen
1104 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1108 if (rts_argv[arg][3] == '\0') {
1109 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsFalse;
1112 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
1113 RtsFlags.ParFlags.parGcLoadBalancingGen
1114 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1118 RtsFlags.ParFlags.setAffinity = rtsTrue;
1121 RtsFlags.ParFlags.migrate = rtsFalse;
1124 // -qw was removed; accepted for backwards compat
1127 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1133 /* =========== PARALLEL =========================== */
1135 THREADED_BUILD_ONLY(
1136 if (rts_argv[arg][2] != '\0') {
1137 RtsFlags.ParFlags.maxLocalSparks
1138 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1139 if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1140 errorBelch("bad value for -e");
1146 /* =========== TICKY ============================== */
1148 case 'r': /* Basic profiling stats */
1151 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1155 r = openStatsFile(rts_argv[arg]+2,
1157 &RtsFlags.TickyFlags.tickyFile);
1158 if (r == -1) { error = rtsTrue; }
1162 /* =========== TRACING ---------=================== */
1166 RtsFlags.TraceFlags.tracing = TRACE_EVENTLOG;
1167 read_trace_flags(&rts_argv[arg][2]);
1173 RtsFlags.TraceFlags.tracing = TRACE_STDERR;
1174 read_trace_flags(&rts_argv[arg][2]);
1178 /* =========== EXTENDED OPTIONS =================== */
1180 case 'x': /* Extend the argument space */
1181 switch(rts_argv[arg][2]) {
1183 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1187 case 'b': /* heapBase in hex; undocumented */
1188 if (rts_argv[arg][3] != '\0') {
1189 RtsFlags.GcFlags.heapBase
1190 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1192 errorBelch("-xb: requires argument");
1197 #if defined(x86_64_HOST_ARCH)
1198 case 'm': /* linkerMemBase */
1199 if (rts_argv[arg][3] != '\0') {
1200 RtsFlags.MiscFlags.linkerMemBase
1201 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1202 if (RtsFlags.MiscFlags.linkerMemBase > 0x80000000) {
1203 errorBelch("-xm: value must be <80000000");
1207 RtsFlags.MiscFlags.linkerMemBase = 0;
1212 case 'c': /* Debugging tool: show current cost centre on an exception */
1213 PROFILING_BUILD_ONLY(
1214 RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1218 case 't': /* Include memory used by TSOs in a heap profile */
1219 PROFILING_BUILD_ONLY(
1220 RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1224 /* The option prefix '-xx' is reserved for future extension. KSW 1999-11. */
1227 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1231 break; /* defensive programming */
1233 /* =========== OH DEAR ============================ */
1235 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1242 if (error) errorUsage();
1245 /* -----------------------------------------------------------------------------
1246 * normaliseRtsOpts: Set some derived values, and make sure things are
1247 * within sensible ranges.
1248 * -------------------------------------------------------------------------- */
1250 static void normaliseRtsOpts (void)
1252 if (RtsFlags.MiscFlags.tickInterval < 0) {
1253 RtsFlags.MiscFlags.tickInterval = 50;
1256 // If the master timer is disabled, turn off the other timers.
1257 if (RtsFlags.MiscFlags.tickInterval == 0) {
1258 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1259 RtsFlags.GcFlags.idleGCDelayTime = 0;
1260 RtsFlags.ProfFlags.profileInterval = 0;
1263 // Determine what tick interval we should use for the RTS timer
1264 // by taking the shortest of the various intervals that we need to
1266 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1267 RtsFlags.MiscFlags.tickInterval =
1268 stg_min(RtsFlags.ConcFlags.ctxtSwitchTime,
1269 RtsFlags.MiscFlags.tickInterval);
1272 if (RtsFlags.GcFlags.idleGCDelayTime > 0) {
1273 RtsFlags.MiscFlags.tickInterval =
1274 stg_min(RtsFlags.GcFlags.idleGCDelayTime,
1275 RtsFlags.MiscFlags.tickInterval);
1278 if (RtsFlags.ProfFlags.profileInterval > 0) {
1279 RtsFlags.MiscFlags.tickInterval =
1280 stg_min(RtsFlags.ProfFlags.profileInterval,
1281 RtsFlags.MiscFlags.tickInterval);
1284 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1285 RtsFlags.ConcFlags.ctxtSwitchTicks =
1286 RtsFlags.ConcFlags.ctxtSwitchTime /
1287 RtsFlags.MiscFlags.tickInterval;
1289 RtsFlags.ConcFlags.ctxtSwitchTicks = 0;
1292 if (RtsFlags.ProfFlags.profileInterval > 0) {
1293 RtsFlags.ProfFlags.profileIntervalTicks =
1294 RtsFlags.ProfFlags.profileInterval /
1295 RtsFlags.MiscFlags.tickInterval;
1297 RtsFlags.ProfFlags.profileIntervalTicks = 0;
1300 if (RtsFlags.GcFlags.stkChunkBufferSize >
1301 RtsFlags.GcFlags.stkChunkSize / 2) {
1302 errorBelch("stack chunk buffer size (-kb) must be less than 50%% of the stack chunk size (-kc)");
1307 static void errorUsage (void)
1312 for (p = usage_text; *p; p++)
1313 errorBelch("%s", *p);
1314 stg_exit(EXIT_FAILURE);
1318 stats_fprintf(FILE *f, char *s, ...)
1330 /* -----------------------------------------------------------------------------
1331 * openStatsFile: open a file in which to put some runtime stats
1332 * -------------------------------------------------------------------------- */
1334 static int // return -1 on error
1335 openStatsFile (char *filename, // filename, or NULL
1336 const char *filename_fmt, // if filename == NULL, use
1337 // this fmt with sprintf to
1338 // generate the filename. %s
1339 // expands to the program name.
1340 FILE **file_ret) // return the FILE*
1344 if (strequal(filename, "stderr")
1345 || (filename_fmt == NULL && *filename == '\0')) {
1346 f = NULL; /* NULL means use debugBelch */
1348 if (*filename != '\0') { /* stats file specified */
1349 f = fopen(filename,"w");
1351 char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
1352 sprintf(stats_filename, filename_fmt, prog_name);
1353 f = fopen(stats_filename,"w");
1356 errorBelch("Can't open stats file %s\n", filename);
1365 /* -----------------------------------------------------------------------------
1366 * initStatsFile: write a line to the file containing the program name
1367 * and the arguments it was invoked with.
1368 -------------------------------------------------------------------------- */
1370 static void initStatsFile (FILE *f)
1372 /* Write prog_argv and rts_argv into start of stats file */
1374 for (count = 0; count < prog_argc; count++) {
1375 stats_fprintf(f, "%s ", prog_argv[count]);
1377 stats_fprintf(f, "+RTS ");
1378 for (count = 0; count < rts_argc; count++)
1379 stats_fprintf(f, "%s ", rts_argv[count]);
1380 stats_fprintf(f, "\n");
1383 /* -----------------------------------------------------------------------------
1384 * decodeSize: parse a string containing a size, like 300K or 1.2M
1385 -------------------------------------------------------------------------- */
1388 decodeSize(const char *flag, nat offset, StgWord64 min, StgWord64 max)
1406 if (c == 'g' || c == 'G')
1407 m *= 1024*1024*1024;
1408 else if (c == 'm' || c == 'M')
1410 else if (c == 'k' || c == 'K')
1412 else if (c == 'w' || c == 'W')
1418 if (m < 0 || val < min || val > max) {
1419 // printf doesn't like 64-bit format specs on Windows
1420 // apparently, so fall back to unsigned long.
1421 errorBelch("error in RTS option %s: size outside allowed range (%lu - %lu)", flag, (lnat)min, (lnat)max);
1422 stg_exit(EXIT_FAILURE);
1428 #if defined(TRACING)
1429 static void read_trace_flags(char *arg)
1433 for (c = arg; *c != '\0'; c++) {
1438 RtsFlags.TraceFlags.scheduler = rtsTrue;
1441 RtsFlags.TraceFlags.timestamp = rtsTrue;
1444 // ignored for backwards-compat
1447 errorBelch("unknown trace option: %c",*c);
1454 static void GNU_ATTRIBUTE(__noreturn__)
1455 bad_option(const char *s)
1457 errorBelch("bad RTS option: %s", s);
1458 stg_exit(EXIT_FAILURE);
1461 /* ----------------------------------------------------------------------------
1462 Copying and freeing argc/argv
1463 ------------------------------------------------------------------------- */
1465 static char * copyArg(char *arg)
1467 char *new_arg = stgMallocBytes(strlen(arg) + 1, "copyArg");
1468 strcpy(new_arg, arg);
1472 static char ** copyArgv(int argc, char *argv[])
1477 new_argv = stgCallocBytes(argc + 1, sizeof (char *), "copyArgv 1");
1478 for (i = 0; i < argc; i++) {
1479 new_argv[i] = copyArg(argv[i]);
1481 new_argv[argc] = NULL;
1485 static void freeArgv(int argc, char *argv[])
1489 for (i = 0; i < argc; i++) {
1496 /* -----------------------------------------------------------------------------
1497 Getting/Setting the program's arguments.
1499 These are used by System.Environment, and parts of the RTS.
1500 -------------------------------------------------------------------------- */
1503 setProgName(char *argv[])
1505 /* Remove directory from argv[0] -- default files in current directory */
1506 #if !defined(mingw32_HOST_OS)
1508 if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
1509 prog_name = last_slash+1;
1511 prog_name = argv[0];
1514 char* last_slash = argv[0] + (strlen(argv[0]) - 1);
1515 while ( last_slash > argv[0] ) {
1516 if ( *last_slash == '/' || *last_slash == '\\' ) {
1517 prog_name = last_slash+1;
1522 prog_name = argv[0];
1527 getProgArgv(int *argc, char **argv[])
1529 if (argc) { *argc = prog_argc; }
1530 if (argv) { *argv = prog_argv; }
1534 setProgArgv(int argc, char *argv[])
1537 prog_argv = copyArgv(argc,argv);
1538 setProgName(prog_argv);
1544 freeArgv(prog_argc,prog_argv);
1549 /* ----------------------------------------------------------------------------
1550 The full argv - a copy of the original program's argc/argv
1551 ------------------------------------------------------------------------- */
1554 setFullProgArgv(int argc, char *argv[])
1556 full_prog_argc = argc;
1557 full_prog_argv = copyArgv(argc,argv);
1560 /* These functions record and recall the full arguments, including the
1561 +RTS ... -RTS options. The reason for adding them was so that the
1562 ghc-inplace program can pass /all/ the arguments on to the real ghc. */
1564 getFullProgArgv(int *argc, char **argv[])
1566 if (argc) { *argc = full_prog_argc; }
1567 if (argv) { *argv = full_prog_argv; }
1571 freeFullProgArgv (void)
1573 freeArgv(full_prog_argc, full_prog_argv);
1575 full_prog_argv = NULL;
1578 /* ----------------------------------------------------------------------------
1580 ------------------------------------------------------------------------- */
1582 #if defined(mingw32_HOST_OS)
1583 void freeWin32ProgArgv (void);
1586 freeWin32ProgArgv (void)
1588 freeArgv(win32_prog_argc, win32_prog_argv);
1592 if (win32_prog_argv != NULL) {
1593 for (i = 0; i < win32_prog_argc; i++) {
1594 stgFree(win32_prog_argv[i]);
1596 stgFree(win32_prog_argv);
1599 win32_prog_argc = 0;
1600 win32_prog_argv = NULL;
1604 getWin32ProgArgv(int *argc, wchar_t **argv[])
1606 *argc = win32_prog_argc;
1607 *argv = win32_prog_argv;
1611 setWin32ProgArgv(int argc, wchar_t *argv[])
1615 freeWin32ProgArgv();
1617 win32_prog_argc = argc;
1619 win32_prog_argv = NULL;
1623 win32_prog_argv = stgCallocBytes(argc + 1, sizeof (wchar_t *),
1624 "setWin32ProgArgv 1");
1625 for (i = 0; i < argc; i++) {
1626 win32_prog_argv[i] = stgMallocBytes((wcslen(argv[i]) + 1) * sizeof(wchar_t),
1627 "setWin32ProgArgv 2");
1628 wcscpy(win32_prog_argv[i], argv[i]);
1630 win32_prog_argv[argc] = NULL;
1634 /* ----------------------------------------------------------------------------
1636 ------------------------------------------------------------------------- */
1641 freeArgv(rts_argc,rts_argv);
1646 /* ----------------------------------------------------------------------------
1648 ------------------------------------------------------------------------- */
1650 void freeRtsArgs(void)
1652 #if defined(mingw32_HOST_OS)
1653 freeWin32ProgArgv();