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"
14 #include "Profiling.h"
27 * Split argument lists
29 int prog_argc = 0; /* an "int" so as to match normal "argc" */
30 char **prog_argv = NULL;
31 int full_prog_argc = 0; /* an "int" so as to match normal "argc" */
32 char **full_prog_argv = NULL;
33 char *prog_name = NULL; /* 'basename' of prog_argv[0] */
34 int rts_argc = 0; /* ditto */
35 char *rts_argv[MAX_RTS_ARGS];
38 * constants, used later
43 /* -----------------------------------------------------------------------------
45 -------------------------------------------------------------------------- */
47 static int /* return NULL on error */
50 int argc, char *argv[],
51 int rts_argc, char *rts_argv[],
52 const char *FILENAME_FMT,
55 static I_ decode(const char *s);
56 static void bad_option(const char *s);
58 /* -----------------------------------------------------------------------------
59 * Command-line option parsing routines.
60 * ---------------------------------------------------------------------------*/
62 void initRtsFlagsDefaults(void)
64 RtsFlags.GcFlags.statsFile = NULL;
65 RtsFlags.GcFlags.giveStats = NO_GC_STATS;
67 RtsFlags.GcFlags.maxStkSize = (8 * 1024 * 1024) / sizeof(W_);
68 RtsFlags.GcFlags.initialStkSize = 1024 / sizeof(W_);
70 RtsFlags.GcFlags.minAllocAreaSize = (512 * 1024) / BLOCK_SIZE;
71 RtsFlags.GcFlags.minOldGenSize = (1024 * 1024) / BLOCK_SIZE;
72 RtsFlags.GcFlags.maxHeapSize = 0; /* off by default */
73 RtsFlags.GcFlags.heapSizeSuggestion = 0; /* none */
74 RtsFlags.GcFlags.pcFreeHeap = 3; /* 3% */
75 RtsFlags.GcFlags.oldGenFactor = 2;
76 RtsFlags.GcFlags.generations = 2;
77 RtsFlags.GcFlags.steps = 2;
78 RtsFlags.GcFlags.squeezeUpdFrames = rtsTrue;
79 RtsFlags.GcFlags.compact = rtsFalse;
80 RtsFlags.GcFlags.compactThreshold = 30.0;
81 RtsFlags.GcFlags.sweep = rtsFalse;
82 #ifdef RTS_GTK_FRONTPANEL
83 RtsFlags.GcFlags.frontpanel = rtsFalse;
85 RtsFlags.GcFlags.idleGCDelayTime = 300; /* millisecs */
88 /* ToDo: Perhaps by adjusting this value we can make linking without
89 * -static work (i.e., not generate a core-dumping executable)? */
90 # if SIZEOF_VOID_P == 8
91 RtsFlags.GcFlags.heapBase = 0x180000000L;
93 # error I have no idea where to begin the heap on a non-64-bit osf3 machine.
96 RtsFlags.GcFlags.heapBase = 0; /* means don't care */
100 RtsFlags.DebugFlags.scheduler = rtsFalse;
101 RtsFlags.DebugFlags.interpreter = rtsFalse;
102 RtsFlags.DebugFlags.weak = rtsFalse;
103 RtsFlags.DebugFlags.gccafs = rtsFalse;
104 RtsFlags.DebugFlags.gc = rtsFalse;
105 RtsFlags.DebugFlags.block_alloc = rtsFalse;
106 RtsFlags.DebugFlags.sanity = rtsFalse;
107 RtsFlags.DebugFlags.stable = rtsFalse;
108 RtsFlags.DebugFlags.stm = rtsFalse;
109 RtsFlags.DebugFlags.prof = rtsFalse;
110 RtsFlags.DebugFlags.eventlog = rtsFalse;
111 RtsFlags.DebugFlags.apply = rtsFalse;
112 RtsFlags.DebugFlags.linker = rtsFalse;
113 RtsFlags.DebugFlags.squeeze = rtsFalse;
114 RtsFlags.DebugFlags.hpc = rtsFalse;
115 RtsFlags.DebugFlags.timestamp = rtsFalse;
118 #if defined(PROFILING)
119 RtsFlags.CcFlags.doCostCentres = 0;
120 #endif /* PROFILING */
122 RtsFlags.ProfFlags.doHeapProfile = rtsFalse;
123 RtsFlags.ProfFlags.profileInterval = 100;
126 RtsFlags.ProfFlags.includeTSOs = rtsFalse;
127 RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
128 RtsFlags.ProfFlags.maxRetainerSetSize = 8;
129 RtsFlags.ProfFlags.ccsLength = 25;
130 RtsFlags.ProfFlags.modSelector = NULL;
131 RtsFlags.ProfFlags.descrSelector = NULL;
132 RtsFlags.ProfFlags.typeSelector = NULL;
133 RtsFlags.ProfFlags.ccSelector = NULL;
134 RtsFlags.ProfFlags.ccsSelector = NULL;
135 RtsFlags.ProfFlags.retainerSelector = NULL;
136 RtsFlags.ProfFlags.bioSelector = NULL;
140 RtsFlags.EventLogFlags.doEventLogging = rtsFalse;
143 RtsFlags.MiscFlags.tickInterval = 20; /* In milliseconds */
144 RtsFlags.ConcFlags.ctxtSwitchTime = 20; /* In milliseconds */
146 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
147 RtsFlags.MiscFlags.machineReadable = rtsFalse;
148 RtsFlags.MiscFlags.linkerMemBase = 0;
151 RtsFlags.ParFlags.nNodes = 1;
152 RtsFlags.ParFlags.migrate = rtsTrue;
153 RtsFlags.ParFlags.wakeupMigrate = rtsFalse;
154 RtsFlags.ParFlags.parGcEnabled = 1;
155 RtsFlags.ParFlags.parGcGen = 1;
156 RtsFlags.ParFlags.parGcLoadBalancing = 1;
157 RtsFlags.ParFlags.setAffinity = 0;
160 #if defined(THREADED_RTS)
161 RtsFlags.ParFlags.maxLocalSparks = 4096;
162 #endif /* THREADED_RTS */
165 RtsFlags.TickyFlags.showTickyStats = rtsFalse;
166 RtsFlags.TickyFlags.tickyFile = NULL;
170 /* By default no special measurements taken */
171 RtsFlags.PapiFlags.eventType = 0;
172 RtsFlags.PapiFlags.numUserEvents = 0;
179 "Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
181 " +RTS Indicates run time system options follow",
182 " -RTS Indicates program arguments follow",
183 " --RTS Indicates that ALL subsequent arguments will be given to the",
184 " program (including any of these RTS flags)",
186 "The following run time system options are available:",
188 " -? Prints this message and exits; the program is not executed",
189 " --info Print information about the RTS used by this program",
191 " -K<size> Sets the maximum stack size (default 8M) Egs: -K32k -K512k",
192 " -k<size> Sets the initial thread stack size (default 1k) Egs: -k4k -k2m",
194 " -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
195 " -M<size> Sets the maximum heap size (default unlimited) Egs: -M256k -M1G",
196 " -H<size> Sets the minimum heap size (default 0M) Egs: -H24m -H1G",
197 " -m<n> Minimum % of heap which must be available (default 3%)",
198 " -G<n> Number of generations (default: 2)",
199 " -T<n> Number of steps in younger generations (default: 2)",
200 " -c<n> Auto-enable compaction of the oldest generation when live data is",
201 " at least <n>% of the maximum heap size set with -M (default: 30%)",
202 " -c Enable compaction for all major collections",
203 " -w Use mark-region for the oldest generation (experimental)",
204 #if defined(THREADED_RTS)
205 " -I<sec> Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
208 " -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
209 " -s[<file>] Summary GC statistics (if <file> omitted, uses stderr)",
210 " -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
211 #ifdef RTS_GTK_FRONTPANEL
212 " -f Display front panel (requires X11 & GTK+)",
216 " -Z Don't squeeze out update frames on stack overflow",
217 " -B Sound the bell at the start of each garbage collection",
218 #if defined(PROFILING)
220 " -px Time/allocation profile (XML) (output file <program>.prof)",
221 " -p Time/allocation profile (output file <program>.prof)",
222 " -P More detailed Time/Allocation profile",
223 " -Pa Give information about *all* cost centres",
225 # if defined(PROFILING)
227 " -hx Heap residency profile (XML) (output file <program>.prof)",
228 " -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
229 " break-down: c = cost centre stack (default)",
231 " d = closure description",
232 " y = type description",
234 " b = biography (LAG,DRAG,VOID,USE)",
235 " A subset of closures may be selected thusly:",
236 " -hc<cc>,... specific cost centre(s) (top of stack only)",
237 " -hC<cc>,... specific cost centre(s) (anywhere in stack)",
238 " -hm<mod>... all cost centres from the specified modules(s)",
239 " -hd<des>,... closures with specified closure descriptions",
240 " -hy<typ>... closures with specified type descriptions",
241 " -hr<cc>... closures with specified retainers",
242 " -hb<bio>... closures with specified biographies (lag,drag,void,use)",
244 " -R<size> Set the maximum retainer set size (default: 8)",
246 " -L<chars> Maximum length of a cost-centre stack in a heap profile",
249 " -xt Include threads (TSOs) in a heap profile",
251 " -xc Show current cost centre stack on raising an exception",
254 #endif /* PROFILING or PAR */
258 " -l Log runtime events (generates binary trace file <program>.eventlog)",
262 #if !defined(PROFILING)
264 " -hT Heap residency profile (output file <program>.hp)",
266 " -i<sec> Time between heap samples (seconds, default: 0.1)",
268 #if defined(TICKY_TICKY)
269 " -r<file> Produce ticky-ticky statistics (with -rstderr for stderr)",
272 " -C<secs> Context-switch interval in seconds.",
273 " 0 or no argument means switch as often as possible.",
274 " Default: 0.02 sec; resolution is set by -V below.",
275 " -V<secs> Master tick interval in seconds (0 == disable timer).",
276 " This sets the resolution for -C and the profile timer -i.",
277 " Default: 0.02 sec.",
279 " -vt Time-stamp debug messages",
282 " -Ds DEBUG: scheduler",
283 " -Di DEBUG: interpreter",
285 " -DG DEBUG: gccafs",
288 " -DS DEBUG: sanity",
289 " -Dt DEBUG: stable",
291 " -De DEBUG: event logging",
293 " -Dl DEBUG: linker",
295 " -Dz DEBUG: stack squezing",
296 " -Dc DEBUG: program coverage",
299 #if defined(THREADED_RTS) && !defined(NOSMP)
300 " -N<n> Use <n> processors (default: 1)",
301 " -N Determine the number of processors to use automatically",
302 " -q1 Use one OS thread for GC (turns off parallel GC)",
303 " -qg<n> Use parallel GC only for generations >= <n> (default: 1)",
304 " -qb Disable load-balancing in the parallel GC",
305 " -qa Use the OS to set thread affinity",
306 " -qm Don't automatically migrate threads between CPUs",
307 " -qw Migrate a thread to the current CPU when it is woken up",
309 " --install-signal-handlers=<yes|no>",
310 " Install signal handlers (default: yes)",
311 #if defined(THREADED_RTS)
312 " -e<size> Size of spark pools (default 100)",
314 #if defined(THREADED_RTS)
315 " -e<n> Maximum number of outstanding local sparks (default: 4096)",
317 #if defined(x86_64_HOST_ARCH)
318 " -xm Base address to mmap memory in the GHCi linker",
319 " (hex; must be <80000000)",
321 #if defined(USE_PAPI)
322 " -aX CPU performance counter measurements using PAPI",
323 " (use with the -s<file> option). X is one of:",
326 " 1 - level 1 cache misses",
327 " 2 - level 2 cache misses",
328 " b - branch mispredictions",
329 " s - stalled cycles",
330 " e - cache miss and branch misprediction events",
333 "RTS options may also be specified using the GHCRTS environment variable.",
335 "Other RTS options may be available for programs compiled a different way.",
336 "The GHC User's Guide has full details.",
341 STATIC_INLINE rtsBool
342 strequal(const char *a, const char * b)
344 return(strcmp(a, b) == 0);
348 splitRtsFlags(char *s, int *rts_argc, char *rts_argv[])
354 while (isspace(*c1)) { c1++; };
356 while (!isspace(*c2) && *c2 != '\0') { c2++; };
358 if (c1 == c2) { break; }
360 if (*rts_argc < MAX_RTS_ARGS-1) {
361 s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
362 strncpy(s, c1, c2-c1);
364 rts_argv[(*rts_argc)++] = s;
366 barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
370 } while (*c1 != '\0');
374 setupRtsFlags(int *argc, char *argv[], int *rts_argc, char *rts_argv[])
376 rtsBool error = rtsFalse;
387 // process arguments from the ghc_rts_opts global variable first.
388 // (arguments from the GHCRTS environment variable and the command
389 // line override these).
391 if (ghc_rts_opts != NULL) {
392 splitRtsFlags(ghc_rts_opts, rts_argc, rts_argv);
396 // process arguments from the GHCRTS environment variable next
397 // (arguments from the command line override these).
399 char *ghc_rts = getenv("GHCRTS");
401 if (ghc_rts != NULL) {
402 splitRtsFlags(ghc_rts, rts_argc, rts_argv);
406 // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
407 // argv[0] must be PGM argument -- leave in argv
409 for (mode = PGM; arg < total_arg; arg++) {
410 // The '--RTS' argument disables all future +RTS ... -RTS processing.
411 if (strequal("--RTS", argv[arg])) {
415 // The '--' argument is passed through to the program, but
416 // disables all further +RTS ... -RTS processing.
417 else if (strequal("--", argv[arg])) {
420 else if (strequal("+RTS", argv[arg])) {
423 else if (strequal("-RTS", argv[arg])) {
426 else if (mode == RTS && *rts_argc < MAX_RTS_ARGS-1) {
427 rts_argv[(*rts_argc)++] = argv[arg];
429 else if (mode == PGM) {
430 argv[(*argc)++] = argv[arg];
433 barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
436 // process remaining program arguments
437 for (; arg < total_arg; arg++) {
438 argv[(*argc)++] = argv[arg];
440 argv[*argc] = (char *) 0;
441 rts_argv[*rts_argc] = (char *) 0;
443 // Process RTS (rts_argv) part: mainly to determine statsfile
444 for (arg = 0; arg < *rts_argc; arg++) {
445 if (rts_argv[arg][0] != '-') {
447 errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
451 switch(rts_argv[arg][1]) {
453 /* process: general args, then PROFILING-only ones, then
454 CONCURRENT-only, TICKY-only (same order as defined in
455 RtsFlags.lh); within those groups, mostly in
456 case-insensitive alphabetical order. Final group is
457 x*, which allows for more options.
461 # define TICKY_BUILD_ONLY(x) x
463 # define TICKY_BUILD_ONLY(x) \
464 errorBelch("not built for: ticky-ticky stats"); \
469 # define PROFILING_BUILD_ONLY(x) x
471 # define PROFILING_BUILD_ONLY(x) \
472 errorBelch("not built for: -prof"); \
477 # define EVENTLOG_BUILD_ONLY(x) x
479 # define EVENTLOG_BUILD_ONLY(x) \
480 errorBelch("not built for: -par-prof"); \
485 # define THREADED_BUILD_ONLY(x) x
487 # define THREADED_BUILD_ONLY(x) \
488 errorBelch("not built for: -smp"); \
492 /* =========== GENERAL ========================== */
497 /* This isn't going to allow us to keep related options
498 together as we add more --* flags. We really need a
499 proper options parser. */
501 if (strequal("install-signal-handlers=yes",
502 &rts_argv[arg][2])) {
503 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
505 else if (strequal("install-signal-handlers=no",
506 &rts_argv[arg][2])) {
507 RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
509 else if (strequal("machine-readable",
510 &rts_argv[arg][2])) {
511 RtsFlags.MiscFlags.machineReadable = rtsTrue;
513 else if (strequal("info",
514 &rts_argv[arg][2])) {
519 errorBelch("unknown RTS option: %s",rts_argv[arg]);
524 RtsFlags.GcFlags.minAllocAreaSize
525 = decode(rts_argv[arg]+2) / BLOCK_SIZE;
526 if (RtsFlags.GcFlags.minAllocAreaSize <= 0) {
527 bad_option(rts_argv[arg]);
533 switch(rts_argv[arg][2]) {
535 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
538 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
541 RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
544 RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
547 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
550 if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
551 errorBelch("maximum number of PAPI events reached");
552 stg_exit(EXIT_FAILURE);
554 RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
555 RtsFlags.PapiFlags.userEvents[RtsFlags.PapiFlags.numUserEvents++] = rts_argv[arg] + 3;
558 bad_option( rts_argv[arg] );
564 RtsFlags.GcFlags.ringBell = rtsTrue;
568 if (rts_argv[arg][2] != '\0') {
569 RtsFlags.GcFlags.compactThreshold =
570 atof(rts_argv[arg]+2);
572 RtsFlags.GcFlags.compact = rtsTrue;
577 RtsFlags.GcFlags.sweep = rtsTrue;
581 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
583 if (RtsFlags.GcFlags.oldGenFactor < 0)
584 bad_option( rts_argv[arg] );
592 for (c = rts_argv[arg] + 2; *c != '\0'; c++) {
595 RtsFlags.DebugFlags.scheduler = rtsTrue;
598 RtsFlags.DebugFlags.interpreter = rtsTrue;
601 RtsFlags.DebugFlags.weak = rtsTrue;
604 RtsFlags.DebugFlags.gccafs = rtsTrue;
607 RtsFlags.DebugFlags.gc = rtsTrue;
610 RtsFlags.DebugFlags.block_alloc = rtsTrue;
613 RtsFlags.DebugFlags.sanity = rtsTrue;
616 RtsFlags.DebugFlags.stable = rtsTrue;
619 RtsFlags.DebugFlags.prof = rtsTrue;
622 RtsFlags.DebugFlags.eventlog = rtsTrue;
625 RtsFlags.DebugFlags.linker = rtsTrue;
628 RtsFlags.DebugFlags.apply = rtsTrue;
631 RtsFlags.DebugFlags.stm = rtsTrue;
634 RtsFlags.DebugFlags.squeeze = rtsTrue;
637 RtsFlags.DebugFlags.hpc = rtsTrue;
640 bad_option( rts_argv[arg] );
648 RtsFlags.GcFlags.maxStkSize =
649 decode(rts_argv[arg]+2) / sizeof(W_);
651 if (RtsFlags.GcFlags.maxStkSize == 0)
652 bad_option( rts_argv[arg] );
656 RtsFlags.GcFlags.initialStkSize =
657 decode(rts_argv[arg]+2) / sizeof(W_);
659 if (RtsFlags.GcFlags.initialStkSize == 0)
660 bad_option( rts_argv[arg] );
664 RtsFlags.GcFlags.maxHeapSize =
665 decode(rts_argv[arg]+2) / BLOCK_SIZE;
666 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
668 if (RtsFlags.GcFlags.maxHeapSize <= 0) {
669 bad_option(rts_argv[arg]);
674 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
676 if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
677 RtsFlags.GcFlags.pcFreeHeap > 100)
678 bad_option( rts_argv[arg] );
682 RtsFlags.GcFlags.generations = decode(rts_argv[arg]+2);
683 if (RtsFlags.GcFlags.generations < 1) {
684 bad_option(rts_argv[arg]);
689 RtsFlags.GcFlags.steps = decode(rts_argv[arg]+2);
690 if (RtsFlags.GcFlags.steps < 1) {
691 bad_option(rts_argv[arg]);
696 RtsFlags.GcFlags.heapSizeSuggestion =
697 decode(rts_argv[arg]+2) / BLOCK_SIZE;
700 #ifdef RTS_GTK_FRONTPANEL
702 RtsFlags.GcFlags.frontpanel = rtsTrue;
706 case 'I': /* idle GC delay */
707 if (rts_argv[arg][2] == '\0') {
712 /* Convert to millisecs */
713 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
714 RtsFlags.GcFlags.idleGCDelayTime = cst;
719 RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
723 RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
727 RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
733 r = open_stats_file(arg, *argc, argv,
734 *rts_argc, rts_argv, NULL,
735 &RtsFlags.GcFlags.statsFile);
736 if (r == -1) { error = rtsTrue; }
741 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
744 /* =========== PROFILING ========================== */
748 RtsFlags.EventLogFlags.doEventLogging = rtsTrue;
750 errorBelch("not built for: -eventlog");
754 case 'P': /* detailed cost centre profiling (time/alloc) */
755 case 'p': /* cost centre profiling (time/alloc) */
756 PROFILING_BUILD_ONLY(
757 switch (rts_argv[arg][2]) {
759 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
762 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
765 if (rts_argv[arg][1] == 'P') {
766 RtsFlags.CcFlags.doCostCentres =
767 COST_CENTRES_VERBOSE;
769 RtsFlags.CcFlags.doCostCentres =
770 COST_CENTRES_SUMMARY;
777 PROFILING_BUILD_ONLY(
778 RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
781 PROFILING_BUILD_ONLY(
782 RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
783 if(RtsFlags.ProfFlags.ccsLength <= 0) {
784 bad_option(rts_argv[arg]);
787 case 'h': /* serial heap profile */
788 #if !defined(PROFILING)
789 switch (rts_argv[arg][2]) {
792 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
795 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
799 PROFILING_BUILD_ONLY(
800 switch (rts_argv[arg][2]) {
814 if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
816 char *left = strchr(rts_argv[arg], '{');
817 char *right = strrchr(rts_argv[arg], '}');
819 // curly braces are optional, for
824 left = rts_argv[arg] + 3;
827 right = rts_argv[arg] + strlen(rts_argv[arg]);
831 switch (rts_argv[arg][2]) {
832 case 'c': // cost centre label select
833 RtsFlags.ProfFlags.ccSelector = left;
836 RtsFlags.ProfFlags.ccsSelector = left;
839 case 'm': // cost centre module select
840 RtsFlags.ProfFlags.modSelector = left;
843 case 'd': // closure descr select
844 RtsFlags.ProfFlags.descrSelector = left;
847 case 'y': // closure type select
848 RtsFlags.ProfFlags.typeSelector = left;
851 case 'r': // retainer select
852 RtsFlags.ProfFlags.retainerSelector = left;
855 case 'b': // biography select
856 RtsFlags.ProfFlags.bioSelector = left;
863 if (RtsFlags.ProfFlags.doHeapProfile != 0) {
864 errorBelch("multiple heap profile options");
869 switch (rts_argv[arg][2]) {
873 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
877 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
881 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
885 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
889 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
893 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
899 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
903 #endif /* PROFILING */
906 case 'i': /* heap sample interval */
907 if (rts_argv[arg][2] == '\0') {
912 /* Convert to milliseconds */
913 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
914 RtsFlags.ProfFlags.profileInterval = cst;
918 /* =========== CONCURRENT ========================= */
919 case 'C': /* context switch interval */
920 if (rts_argv[arg][2] == '\0')
921 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
925 /* Convert to milliseconds */
926 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
927 RtsFlags.ConcFlags.ctxtSwitchTime = cst;
931 case 'V': /* master tick interval */
932 if (rts_argv[arg][2] == '\0') {
933 // turns off ticks completely
934 RtsFlags.MiscFlags.tickInterval = 0;
938 /* Convert to milliseconds */
939 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
940 RtsFlags.MiscFlags.tickInterval = cst;
944 #if defined(THREADED_RTS) && !defined(NOSMP)
947 if (rts_argv[arg][2] == '\0') {
948 #if defined(PROFILING)
949 RtsFlags.ParFlags.nNodes = 1;
951 RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
954 RtsFlags.ParFlags.nNodes
955 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
956 if (RtsFlags.ParFlags.nNodes <= 0) {
957 errorBelch("bad value for -N");
960 #if defined(PROFILING)
961 if (RtsFlags.ParFlags.nNodes > 1) {
962 errorBelch("bad option %s: only -N1 is supported with profiling", rts_argv[arg]);
971 switch (rts_argv[arg][2]) {
973 // backwards compat only
974 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
977 errorBelch("unknown RTS option: %s",rts_argv[arg]);
984 switch (rts_argv[arg][2]) {
986 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
990 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
993 if (rts_argv[arg][3] != '\0') {
994 RtsFlags.ParFlags.parGcGen
995 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
997 errorBelch("bad value for -qg");
1002 RtsFlags.ParFlags.parGcLoadBalancing = rtsFalse;
1005 RtsFlags.ParFlags.setAffinity = rtsTrue;
1008 RtsFlags.ParFlags.migrate = rtsFalse;
1011 RtsFlags.ParFlags.wakeupMigrate = rtsTrue;
1014 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1020 /* =========== PARALLEL =========================== */
1022 THREADED_BUILD_ONLY(
1023 if (rts_argv[arg][2] != '\0') {
1024 RtsFlags.ParFlags.maxLocalSparks
1025 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1026 if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1027 errorBelch("bad value for -e");
1033 /* =========== TICKY ============================== */
1035 case 'r': /* Basic profiling stats */
1038 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1042 r = open_stats_file(arg, *argc, argv,
1043 *rts_argc, rts_argv, TICKY_FILENAME_FMT,
1044 &RtsFlags.TickyFlags.tickyFile);
1045 if (r == -1) { error = rtsTrue; }
1049 /* =========== TRACING ---------=================== */
1052 switch(rts_argv[arg][2]) {
1054 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1058 RtsFlags.DebugFlags.timestamp = rtsTrue;
1062 // ignored for backwards-compat
1065 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1071 /* =========== EXTENDED OPTIONS =================== */
1073 case 'x': /* Extend the argument space */
1074 switch(rts_argv[arg][2]) {
1076 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1080 case 'b': /* heapBase in hex; undocumented */
1081 if (rts_argv[arg][3] != '\0') {
1082 RtsFlags.GcFlags.heapBase
1083 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1085 errorBelch("-xb: requires argument");
1090 #if defined(x86_64_HOST_ARCH)
1091 case 'm': /* linkerMemBase */
1092 if (rts_argv[arg][3] != '\0') {
1093 RtsFlags.MiscFlags.linkerMemBase
1094 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1095 if (RtsFlags.MiscFlags.linkerMemBase > 0x80000000) {
1096 errorBelch("-xm: value must be <80000000");
1100 RtsFlags.MiscFlags.linkerMemBase = 0;
1105 case 'c': /* Debugging tool: show current cost centre on an exception */
1106 PROFILING_BUILD_ONLY(
1107 RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1111 case 't': /* Include memory used by TSOs in a heap profile */
1112 PROFILING_BUILD_ONLY(
1113 RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1117 /* The option prefix '-xx' is reserved for future extension. KSW 1999-11. */
1120 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1124 break; /* defensive programming */
1126 /* =========== OH DEAR ============================ */
1128 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1135 if (RtsFlags.MiscFlags.tickInterval < 0) {
1136 RtsFlags.MiscFlags.tickInterval = 50;
1139 // If the master timer is disabled, turn off the other timers.
1140 if (RtsFlags.MiscFlags.tickInterval == 0) {
1141 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1142 RtsFlags.GcFlags.idleGCDelayTime = 0;
1143 RtsFlags.ProfFlags.profileInterval = 0;
1146 // Determine what tick interval we should use for the RTS timer
1147 // by taking the shortest of the various intervals that we need to
1149 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1150 RtsFlags.MiscFlags.tickInterval =
1151 stg_min(RtsFlags.ConcFlags.ctxtSwitchTime,
1152 RtsFlags.MiscFlags.tickInterval);
1155 if (RtsFlags.GcFlags.idleGCDelayTime > 0) {
1156 RtsFlags.MiscFlags.tickInterval =
1157 stg_min(RtsFlags.GcFlags.idleGCDelayTime,
1158 RtsFlags.MiscFlags.tickInterval);
1161 if (RtsFlags.ProfFlags.profileInterval > 0) {
1162 RtsFlags.MiscFlags.tickInterval =
1163 stg_min(RtsFlags.ProfFlags.profileInterval,
1164 RtsFlags.MiscFlags.tickInterval);
1167 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1168 RtsFlags.ConcFlags.ctxtSwitchTicks =
1169 RtsFlags.ConcFlags.ctxtSwitchTime /
1170 RtsFlags.MiscFlags.tickInterval;
1172 RtsFlags.ConcFlags.ctxtSwitchTicks = 0;
1175 if (RtsFlags.ProfFlags.profileInterval > 0) {
1176 RtsFlags.ProfFlags.profileIntervalTicks =
1177 RtsFlags.ProfFlags.profileInterval /
1178 RtsFlags.MiscFlags.tickInterval;
1180 RtsFlags.ProfFlags.profileIntervalTicks = 0;
1187 for (p = usage_text; *p; p++)
1188 errorBelch("%s", *p);
1189 stg_exit(EXIT_FAILURE);
1195 stats_fprintf(FILE *f, char *s, ...)
1207 static int /* return -1 on error */
1210 int argc, char *argv[],
1211 int rts_argc, char *rts_argv[],
1212 const char *FILENAME_FMT,
1217 if (strequal(rts_argv[arg]+2, "stderr")
1218 || (FILENAME_FMT == NULL && rts_argv[arg][2] == '\0')) {
1219 f = NULL; /* NULL means use debugBelch */
1221 if (rts_argv[arg][2] != '\0') { /* stats file specified */
1222 f = fopen(rts_argv[arg]+2,"w");
1224 char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
1225 sprintf(stats_filename, FILENAME_FMT, argv[0]);
1226 f = fopen(stats_filename,"w");
1229 errorBelch("Can't open stats file %s\n", rts_argv[arg]+2);
1236 /* Write argv and rtsv into start of stats file */
1238 for(count = 0; count < argc; count++) {
1239 stats_fprintf(f, "%s ", argv[count]);
1241 stats_fprintf(f, "+RTS ");
1242 for(count = 0; count < rts_argc; count++)
1243 stats_fprintf(f, "%s ", rts_argv[count]);
1244 stats_fprintf(f, "\n");
1252 decode(const char *s)
1263 if (c == 'g' || c == 'G')
1264 m *= 1000*1000*1000; /* UNchecked! */
1265 else if (c == 'm' || c == 'M')
1266 m *= 1000*1000; /* We do not use powers of 2 (1024) */
1267 else if (c == 'k' || c == 'K') /* to avoid possible bad effects on */
1268 m *= 1000; /* a direct-mapped cache. */
1269 else if (c == 'w' || c == 'W')
1276 bad_option(const char *s)
1278 errorBelch("bad RTS option: %s", s);
1279 stg_exit(EXIT_FAILURE);
1282 /* -----------------------------------------------------------------------------
1283 Getting/Setting the program's arguments.
1285 These are used by System.Environment, and parts of the RTS.
1286 -------------------------------------------------------------------------- */
1289 setProgName(char *argv[])
1291 /* Remove directory from argv[0] -- default files in current directory */
1292 #if !defined(mingw32_HOST_OS)
1294 if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
1295 prog_name = last_slash+1;
1297 prog_name = argv[0];
1300 char* last_slash = argv[0] + (strlen(argv[0]) - 1);
1301 while ( last_slash > argv[0] ) {
1302 if ( *last_slash == '/' || *last_slash == '\\' ) {
1303 prog_name = last_slash+1;
1308 prog_name = argv[0];
1313 getProgArgv(int *argc, char **argv[])
1315 if (argc) { *argc = prog_argc; }
1316 if (argv) { *argv = prog_argv; }
1320 setProgArgv(int argc, char *argv[])
1322 /* Usually this is done by startupHaskell, so we don't need to call this.
1323 However, sometimes Hugs wants to change the arguments which Haskell
1324 getArgs >>= ... will be fed. So you can do that by calling here
1325 _after_ calling startupHaskell.
1329 setProgName(prog_argv);
1332 /* These functions record and recall the full arguments, including the
1333 +RTS ... -RTS options. The reason for adding them was so that the
1334 ghc-inplace program can pass /all/ the arguments on to the real ghc. */
1336 getFullProgArgv(int *argc, char **argv[])
1338 if (argc) { *argc = full_prog_argc; }
1339 if (argv) { *argv = full_prog_argv; }
1343 setFullProgArgv(int argc, char *argv[])
1346 full_prog_argc = argc;
1347 full_prog_argv = stgCallocBytes(argc + 1, sizeof (char *),
1348 "setFullProgArgv 1");
1349 for (i = 0; i < argc; i++) {
1350 full_prog_argv[i] = stgMallocBytes(strlen(argv[i]) + 1,
1351 "setFullProgArgv 2");
1352 strcpy(full_prog_argv[i], argv[i]);
1354 full_prog_argv[argc] = NULL;