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"
26 * Split argument lists
28 int prog_argc = 0; /* an "int" so as to match normal "argc" */
29 char **prog_argv = NULL;
30 int full_prog_argc = 0; /* an "int" so as to match normal "argc" */
31 char **full_prog_argv = NULL;
32 char *prog_name = NULL; /* 'basename' of prog_argv[0] */
33 int rts_argc = 0; /* ditto */
34 char *rts_argv[MAX_RTS_ARGS];
37 * constants, used later
42 /* -----------------------------------------------------------------------------
44 -------------------------------------------------------------------------- */
46 static int /* return NULL on error */
49 int argc, char *argv[],
50 int rts_argc, char *rts_argv[],
51 const char *FILENAME_FMT,
54 static I_ decode(const char *s);
55 static void bad_option(const char *s);
57 /* -----------------------------------------------------------------------------
58 * Command-line option parsing routines.
59 * ---------------------------------------------------------------------------*/
61 void initRtsFlagsDefaults(void)
63 RtsFlags.GcFlags.statsFile = NULL;
64 RtsFlags.GcFlags.giveStats = NO_GC_STATS;
66 RtsFlags.GcFlags.maxStkSize = (8 * 1024 * 1024) / sizeof(W_);
67 RtsFlags.GcFlags.initialStkSize = 1024 / sizeof(W_);
69 RtsFlags.GcFlags.minAllocAreaSize = (512 * 1024) / BLOCK_SIZE;
70 RtsFlags.GcFlags.minOldGenSize = (1024 * 1024) / BLOCK_SIZE;
71 RtsFlags.GcFlags.maxHeapSize = 0; /* off by default */
72 RtsFlags.GcFlags.heapSizeSuggestion = 0; /* none */
73 RtsFlags.GcFlags.pcFreeHeap = 3; /* 3% */
74 RtsFlags.GcFlags.oldGenFactor = 2;
75 RtsFlags.GcFlags.generations = 2;
76 RtsFlags.GcFlags.steps = 2;
77 RtsFlags.GcFlags.squeezeUpdFrames = rtsTrue;
78 RtsFlags.GcFlags.compact = rtsFalse;
79 RtsFlags.GcFlags.compactThreshold = 30.0;
80 RtsFlags.GcFlags.sweep = rtsFalse;
81 #ifdef RTS_GTK_FRONTPANEL
82 RtsFlags.GcFlags.frontpanel = rtsFalse;
84 RtsFlags.GcFlags.idleGCDelayTime = 300; /* millisecs */
87 /* ToDo: Perhaps by adjusting this value we can make linking without
88 * -static work (i.e., not generate a core-dumping executable)? */
89 # if SIZEOF_VOID_P == 8
90 RtsFlags.GcFlags.heapBase = 0x180000000L;
92 # error I have no idea where to begin the heap on a non-64-bit osf3 machine.
95 RtsFlags.GcFlags.heapBase = 0; /* means don't care */
99 RtsFlags.DebugFlags.scheduler = rtsFalse;
100 RtsFlags.DebugFlags.interpreter = rtsFalse;
101 RtsFlags.DebugFlags.weak = rtsFalse;
102 RtsFlags.DebugFlags.gccafs = rtsFalse;
103 RtsFlags.DebugFlags.gc = rtsFalse;
104 RtsFlags.DebugFlags.block_alloc = rtsFalse;
105 RtsFlags.DebugFlags.sanity = rtsFalse;
106 RtsFlags.DebugFlags.stable = rtsFalse;
107 RtsFlags.DebugFlags.stm = rtsFalse;
108 RtsFlags.DebugFlags.prof = rtsFalse;
109 RtsFlags.DebugFlags.apply = rtsFalse;
110 RtsFlags.DebugFlags.linker = rtsFalse;
111 RtsFlags.DebugFlags.squeeze = rtsFalse;
112 RtsFlags.DebugFlags.hpc = rtsFalse;
113 RtsFlags.DebugFlags.sparks = rtsFalse;
116 #if defined(PROFILING)
117 RtsFlags.CcFlags.doCostCentres = 0;
118 #endif /* PROFILING */
120 RtsFlags.ProfFlags.doHeapProfile = rtsFalse;
121 RtsFlags.ProfFlags.profileInterval = 100;
124 RtsFlags.ProfFlags.includeTSOs = rtsFalse;
125 RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
126 RtsFlags.ProfFlags.maxRetainerSetSize = 8;
127 RtsFlags.ProfFlags.ccsLength = 25;
128 RtsFlags.ProfFlags.modSelector = NULL;
129 RtsFlags.ProfFlags.descrSelector = NULL;
130 RtsFlags.ProfFlags.typeSelector = NULL;
131 RtsFlags.ProfFlags.ccSelector = NULL;
132 RtsFlags.ProfFlags.ccsSelector = NULL;
133 RtsFlags.ProfFlags.retainerSelector = NULL;
134 RtsFlags.ProfFlags.bioSelector = NULL;
138 RtsFlags.TraceFlags.trace_stderr = rtsFalse;
139 RtsFlags.TraceFlags.timestamp = rtsFalse;
140 RtsFlags.TraceFlags.scheduler = 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> Use in-place compaction instead of copying in the oldest generation",
201 " when live data is at least <n>% of the maximum heap size set with",
202 " -M (default: 30%)",
203 " -c Use in-place compaction for all oldest generation collections",
204 " (the default is to use copying)",
205 " -w Use mark-region for the oldest generation (experimental)",
206 #if defined(THREADED_RTS)
207 " -I<sec> Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
210 " -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
211 " -s[<file>] Summary GC statistics (if <file> omitted, uses stderr)",
212 " -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
213 #ifdef RTS_GTK_FRONTPANEL
214 " -f Display front panel (requires X11 & GTK+)",
218 " -Z Don't squeeze out update frames on stack overflow",
219 " -B Sound the bell at the start of each garbage collection",
220 #if defined(PROFILING)
222 " -px Time/allocation profile (XML) (output file <program>.prof)",
223 " -p Time/allocation profile (output file <program>.prof)",
224 " -P More detailed Time/Allocation profile",
225 " -Pa Give information about *all* cost centres",
227 # if defined(PROFILING)
229 " -hx Heap residency profile (XML) (output file <program>.prof)",
230 " -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
231 " break-down: c = cost centre stack (default)",
233 " d = closure description",
234 " y = type description",
236 " b = biography (LAG,DRAG,VOID,USE)",
237 " A subset of closures may be selected thusly:",
238 " -hc<cc>,... specific cost centre(s) (top of stack only)",
239 " -hC<cc>,... specific cost centre(s) (anywhere in stack)",
240 " -hm<mod>... all cost centres from the specified modules(s)",
241 " -hd<des>,... closures with specified closure descriptions",
242 " -hy<typ>... closures with specified type descriptions",
243 " -hr<cc>... closures with specified retainers",
244 " -hb<bio>... closures with specified biographies (lag,drag,void,use)",
246 " -R<size> Set the maximum retainer set size (default: 8)",
248 " -L<chars> Maximum length of a cost-centre stack in a heap profile",
251 " -xt Include threads (TSOs) in a heap profile",
253 " -xc Show current cost centre stack on raising an exception",
256 #endif /* PROFILING or PAR */
260 " -v Log events to stderr",
261 " -l Log events in binary format to the file <program>.eventlog",
262 " -vt Include time stamps when tracing events to stderr with -v",
264 " -ls Log scheduler events",
268 #if !defined(PROFILING)
270 " -hT Heap residency profile (output file <program>.hp)",
272 " -i<sec> Time between heap samples (seconds, default: 0.1)",
274 #if defined(TICKY_TICKY)
275 " -r<file> Produce ticky-ticky statistics (with -rstderr for stderr)",
278 " -C<secs> Context-switch interval in seconds.",
279 " 0 or no argument means switch as often as possible.",
280 " Default: 0.02 sec; resolution is set by -V below.",
281 " -V<secs> Master tick interval in seconds (0 == disable timer).",
282 " This sets the resolution for -C and the profile timer -i.",
283 " Default: 0.02 sec.",
286 " -Ds DEBUG: scheduler",
287 " -Di DEBUG: interpreter",
289 " -DG DEBUG: gccafs",
292 " -DS DEBUG: sanity",
293 " -Dt DEBUG: stable",
295 " -De DEBUG: event logging",
297 " -Dl DEBUG: linker",
299 " -Dz DEBUG: stack squezing",
300 " -Dc DEBUG: program coverage",
301 " -Dr DEBUG: sparks",
303 " NOTE: all -D options also enable -v automatically. Use -l to create a",
304 " binary event log file instead.",
307 #if defined(THREADED_RTS) && !defined(NOSMP)
308 " -N<n> Use <n> processors (default: 1)",
309 " -N Determine the number of processors to use automatically",
310 " -q1 Use one OS thread for GC (turns off parallel GC)",
311 " -qg<n> Use parallel GC only for generations >= <n> (default: 1)",
312 " -qb Disable load-balancing in the parallel GC",
313 " -qa Use the OS to set thread affinity",
314 " -qm Don't automatically migrate threads between CPUs",
315 " -qw Migrate a thread to the current CPU when it is woken up",
317 " --install-signal-handlers=<yes|no>",
318 " Install signal handlers (default: yes)",
319 #if defined(THREADED_RTS)
320 " -e<size> Size of spark pools (default 100)",
322 #if defined(THREADED_RTS)
323 " -e<n> Maximum number of outstanding local sparks (default: 4096)",
325 #if defined(x86_64_HOST_ARCH)
326 " -xm Base address to mmap memory in the GHCi linker",
327 " (hex; must be <80000000)",
329 #if defined(USE_PAPI)
330 " -aX CPU performance counter measurements using PAPI",
331 " (use with the -s<file> option). X is one of:",
334 " 1 - level 1 cache misses",
335 " 2 - level 2 cache misses",
336 " b - branch mispredictions",
337 " s - stalled cycles",
338 " e - cache miss and branch misprediction events",
341 "RTS options may also be specified using the GHCRTS environment variable.",
343 "Other RTS options may be available for programs compiled a different way.",
344 "The GHC User's Guide has full details.",
349 STATIC_INLINE rtsBool
350 strequal(const char *a, const char * b)
352 return(strcmp(a, b) == 0);
356 splitRtsFlags(char *s, int *rts_argc, char *rts_argv[])
362 while (isspace(*c1)) { c1++; };
364 while (!isspace(*c2) && *c2 != '\0') { c2++; };
366 if (c1 == c2) { break; }
368 if (*rts_argc < MAX_RTS_ARGS-1) {
369 s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
370 strncpy(s, c1, c2-c1);
372 rts_argv[(*rts_argc)++] = s;
374 barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
378 } while (*c1 != '\0');
382 setupRtsFlags(int *argc, char *argv[], int *rts_argc, char *rts_argv[])
384 rtsBool error = rtsFalse;
395 // process arguments from the ghc_rts_opts global variable first.
396 // (arguments from the GHCRTS environment variable and the command
397 // line override these).
399 if (ghc_rts_opts != NULL) {
400 splitRtsFlags(ghc_rts_opts, rts_argc, rts_argv);
404 // process arguments from the GHCRTS environment variable next
405 // (arguments from the command line override these).
407 char *ghc_rts = getenv("GHCRTS");
409 if (ghc_rts != NULL) {
410 splitRtsFlags(ghc_rts, rts_argc, rts_argv);
414 // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
415 // argv[0] must be PGM argument -- leave in argv
417 for (mode = PGM; arg < total_arg; arg++) {
418 // The '--RTS' argument disables all future +RTS ... -RTS processing.
419 if (strequal("--RTS", argv[arg])) {
423 // The '--' argument is passed through to the program, but
424 // disables all further +RTS ... -RTS processing.
425 else if (strequal("--", argv[arg])) {
428 else if (strequal("+RTS", argv[arg])) {
431 else if (strequal("-RTS", argv[arg])) {
434 else if (mode == RTS && *rts_argc < MAX_RTS_ARGS-1) {
435 rts_argv[(*rts_argc)++] = argv[arg];
437 else if (mode == PGM) {
438 argv[(*argc)++] = argv[arg];
441 barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
444 // process remaining program arguments
445 for (; arg < total_arg; arg++) {
446 argv[(*argc)++] = argv[arg];
448 argv[*argc] = (char *) 0;
449 rts_argv[*rts_argc] = (char *) 0;
451 // Process RTS (rts_argv) part: mainly to determine statsfile
452 for (arg = 0; arg < *rts_argc; arg++) {
453 if (rts_argv[arg][0] != '-') {
455 errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
459 switch(rts_argv[arg][1]) {
461 /* process: general args, then PROFILING-only ones, then
462 CONCURRENT-only, TICKY-only (same order as defined in
463 RtsFlags.lh); within those groups, mostly in
464 case-insensitive alphabetical order. Final group is
465 x*, which allows for more options.
469 # define TICKY_BUILD_ONLY(x) x
471 # define TICKY_BUILD_ONLY(x) \
472 errorBelch("not built for: ticky-ticky stats"); \
477 # define PROFILING_BUILD_ONLY(x) x
479 # define PROFILING_BUILD_ONLY(x) \
480 errorBelch("not built for: -prof"); \
485 # define TRACING_BUILD_ONLY(x) x
487 # define TRACING_BUILD_ONLY(x) \
488 errorBelch("not built for: -par-prof"); \
493 # define THREADED_BUILD_ONLY(x) x
495 # define THREADED_BUILD_ONLY(x) \
496 errorBelch("not built for: -smp"); \
500 /* =========== GENERAL ========================== */
505 /* This isn't going to allow us to keep related options
506 together as we add more --* flags. We really need a
507 proper options parser. */
509 if (strequal("install-signal-handlers=yes",
510 &rts_argv[arg][2])) {
511 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
513 else if (strequal("install-signal-handlers=no",
514 &rts_argv[arg][2])) {
515 RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
517 else if (strequal("machine-readable",
518 &rts_argv[arg][2])) {
519 RtsFlags.MiscFlags.machineReadable = rtsTrue;
521 else if (strequal("info",
522 &rts_argv[arg][2])) {
527 errorBelch("unknown RTS option: %s",rts_argv[arg]);
532 RtsFlags.GcFlags.minAllocAreaSize
533 = decode(rts_argv[arg]+2) / BLOCK_SIZE;
534 if (RtsFlags.GcFlags.minAllocAreaSize <= 0) {
535 bad_option(rts_argv[arg]);
541 switch(rts_argv[arg][2]) {
543 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
546 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
549 RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
552 RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
555 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
558 if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
559 errorBelch("maximum number of PAPI events reached");
560 stg_exit(EXIT_FAILURE);
562 RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
563 RtsFlags.PapiFlags.userEvents[RtsFlags.PapiFlags.numUserEvents++] = rts_argv[arg] + 3;
566 bad_option( rts_argv[arg] );
572 RtsFlags.GcFlags.ringBell = rtsTrue;
576 if (rts_argv[arg][2] != '\0') {
577 RtsFlags.GcFlags.compactThreshold =
578 atof(rts_argv[arg]+2);
580 RtsFlags.GcFlags.compact = rtsTrue;
585 RtsFlags.GcFlags.sweep = rtsTrue;
589 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
591 if (RtsFlags.GcFlags.oldGenFactor < 0)
592 bad_option( rts_argv[arg] );
600 for (c = rts_argv[arg] + 2; *c != '\0'; c++) {
603 RtsFlags.DebugFlags.scheduler = rtsTrue;
606 RtsFlags.DebugFlags.interpreter = rtsTrue;
609 RtsFlags.DebugFlags.weak = rtsTrue;
612 RtsFlags.DebugFlags.gccafs = rtsTrue;
615 RtsFlags.DebugFlags.gc = rtsTrue;
618 RtsFlags.DebugFlags.block_alloc = rtsTrue;
621 RtsFlags.DebugFlags.sanity = rtsTrue;
624 RtsFlags.DebugFlags.stable = rtsTrue;
627 RtsFlags.DebugFlags.prof = rtsTrue;
630 RtsFlags.DebugFlags.linker = rtsTrue;
633 RtsFlags.DebugFlags.apply = rtsTrue;
636 RtsFlags.DebugFlags.stm = rtsTrue;
639 RtsFlags.DebugFlags.squeeze = rtsTrue;
642 RtsFlags.DebugFlags.hpc = rtsTrue;
645 RtsFlags.DebugFlags.sparks = rtsTrue;
648 bad_option( rts_argv[arg] );
651 // -Dx also turns on -v. Use -l to direct trace
652 // events to the .eventlog file instead.
653 RtsFlags.TraceFlags.trace_stderr = rtsTrue;
659 RtsFlags.GcFlags.maxStkSize =
660 decode(rts_argv[arg]+2) / sizeof(W_);
662 if (RtsFlags.GcFlags.maxStkSize == 0)
663 bad_option( rts_argv[arg] );
667 RtsFlags.GcFlags.initialStkSize =
668 decode(rts_argv[arg]+2) / sizeof(W_);
670 if (RtsFlags.GcFlags.initialStkSize == 0)
671 bad_option( rts_argv[arg] );
675 RtsFlags.GcFlags.maxHeapSize =
676 decode(rts_argv[arg]+2) / BLOCK_SIZE;
677 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
679 if (RtsFlags.GcFlags.maxHeapSize <= 0) {
680 bad_option(rts_argv[arg]);
685 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
687 if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
688 RtsFlags.GcFlags.pcFreeHeap > 100)
689 bad_option( rts_argv[arg] );
693 RtsFlags.GcFlags.generations = decode(rts_argv[arg]+2);
694 if (RtsFlags.GcFlags.generations < 1) {
695 bad_option(rts_argv[arg]);
700 RtsFlags.GcFlags.steps = decode(rts_argv[arg]+2);
701 if (RtsFlags.GcFlags.steps < 1) {
702 bad_option(rts_argv[arg]);
707 RtsFlags.GcFlags.heapSizeSuggestion =
708 decode(rts_argv[arg]+2) / BLOCK_SIZE;
711 #ifdef RTS_GTK_FRONTPANEL
713 RtsFlags.GcFlags.frontpanel = rtsTrue;
717 case 'I': /* idle GC delay */
718 if (rts_argv[arg][2] == '\0') {
723 /* Convert to millisecs */
724 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
725 RtsFlags.GcFlags.idleGCDelayTime = cst;
730 RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
734 RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
738 RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
744 r = open_stats_file(arg, *argc, argv,
745 *rts_argc, rts_argv, NULL,
746 &RtsFlags.GcFlags.statsFile);
747 if (r == -1) { error = rtsTrue; }
752 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
755 /* =========== PROFILING ========================== */
759 switch(rts_argv[arg][2]) {
761 RtsFlags.TraceFlags.trace_stderr = rtsFalse;
764 RtsFlags.TraceFlags.scheduler = rtsTrue;
767 errorBelch("unknown RTS option: %s",rts_argv[arg]);
772 errorBelch("not built for: -eventlog");
776 case 'P': /* detailed cost centre profiling (time/alloc) */
777 case 'p': /* cost centre profiling (time/alloc) */
778 PROFILING_BUILD_ONLY(
779 switch (rts_argv[arg][2]) {
781 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
784 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
787 if (rts_argv[arg][1] == 'P') {
788 RtsFlags.CcFlags.doCostCentres =
789 COST_CENTRES_VERBOSE;
791 RtsFlags.CcFlags.doCostCentres =
792 COST_CENTRES_SUMMARY;
799 PROFILING_BUILD_ONLY(
800 RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
803 PROFILING_BUILD_ONLY(
804 RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
805 if(RtsFlags.ProfFlags.ccsLength <= 0) {
806 bad_option(rts_argv[arg]);
809 case 'h': /* serial heap profile */
810 #if !defined(PROFILING)
811 switch (rts_argv[arg][2]) {
814 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
817 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
821 PROFILING_BUILD_ONLY(
822 switch (rts_argv[arg][2]) {
836 if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
838 char *left = strchr(rts_argv[arg], '{');
839 char *right = strrchr(rts_argv[arg], '}');
841 // curly braces are optional, for
846 left = rts_argv[arg] + 3;
849 right = rts_argv[arg] + strlen(rts_argv[arg]);
853 switch (rts_argv[arg][2]) {
854 case 'c': // cost centre label select
855 RtsFlags.ProfFlags.ccSelector = left;
858 RtsFlags.ProfFlags.ccsSelector = left;
861 case 'm': // cost centre module select
862 RtsFlags.ProfFlags.modSelector = left;
865 case 'd': // closure descr select
866 RtsFlags.ProfFlags.descrSelector = left;
869 case 'y': // closure type select
870 RtsFlags.ProfFlags.typeSelector = left;
873 case 'r': // retainer select
874 RtsFlags.ProfFlags.retainerSelector = left;
877 case 'b': // biography select
878 RtsFlags.ProfFlags.bioSelector = left;
885 if (RtsFlags.ProfFlags.doHeapProfile != 0) {
886 errorBelch("multiple heap profile options");
891 switch (rts_argv[arg][2]) {
895 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
899 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
903 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
907 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
911 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
915 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
921 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
925 #endif /* PROFILING */
928 case 'i': /* heap sample interval */
929 if (rts_argv[arg][2] == '\0') {
934 /* Convert to milliseconds */
935 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
936 RtsFlags.ProfFlags.profileInterval = cst;
940 /* =========== CONCURRENT ========================= */
941 case 'C': /* context switch interval */
942 if (rts_argv[arg][2] == '\0')
943 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
947 /* Convert to milliseconds */
948 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
949 RtsFlags.ConcFlags.ctxtSwitchTime = cst;
953 case 'V': /* master tick interval */
954 if (rts_argv[arg][2] == '\0') {
955 // turns off ticks completely
956 RtsFlags.MiscFlags.tickInterval = 0;
960 /* Convert to milliseconds */
961 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
962 RtsFlags.MiscFlags.tickInterval = cst;
966 #if defined(THREADED_RTS) && !defined(NOSMP)
969 if (rts_argv[arg][2] == '\0') {
970 #if defined(PROFILING)
971 RtsFlags.ParFlags.nNodes = 1;
973 RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
976 RtsFlags.ParFlags.nNodes
977 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
978 if (RtsFlags.ParFlags.nNodes <= 0) {
979 errorBelch("bad value for -N");
982 #if defined(PROFILING)
983 if (RtsFlags.ParFlags.nNodes > 1) {
984 errorBelch("bad option %s: only -N1 is supported with profiling", rts_argv[arg]);
993 switch (rts_argv[arg][2]) {
995 // backwards compat only
996 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
999 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1006 switch (rts_argv[arg][2]) {
1008 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1012 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1015 if (rts_argv[arg][3] != '\0') {
1016 RtsFlags.ParFlags.parGcGen
1017 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1019 errorBelch("bad value for -qg");
1024 RtsFlags.ParFlags.parGcLoadBalancing = rtsFalse;
1027 RtsFlags.ParFlags.setAffinity = rtsTrue;
1030 RtsFlags.ParFlags.migrate = rtsFalse;
1033 RtsFlags.ParFlags.wakeupMigrate = rtsTrue;
1036 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1042 /* =========== PARALLEL =========================== */
1044 THREADED_BUILD_ONLY(
1045 if (rts_argv[arg][2] != '\0') {
1046 RtsFlags.ParFlags.maxLocalSparks
1047 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1048 if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1049 errorBelch("bad value for -e");
1055 /* =========== TICKY ============================== */
1057 case 'r': /* Basic profiling stats */
1060 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1064 r = open_stats_file(arg, *argc, argv,
1065 *rts_argc, rts_argv, TICKY_FILENAME_FMT,
1066 &RtsFlags.TickyFlags.tickyFile);
1067 if (r == -1) { error = rtsTrue; }
1071 /* =========== TRACING ---------=================== */
1074 switch(rts_argv[arg][2]) {
1077 RtsFlags.TraceFlags.trace_stderr = rtsTrue;
1080 RtsFlags.TraceFlags.timestamp = rtsTrue;
1085 // ignored for backwards-compat
1088 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1094 /* =========== EXTENDED OPTIONS =================== */
1096 case 'x': /* Extend the argument space */
1097 switch(rts_argv[arg][2]) {
1099 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1103 case 'b': /* heapBase in hex; undocumented */
1104 if (rts_argv[arg][3] != '\0') {
1105 RtsFlags.GcFlags.heapBase
1106 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1108 errorBelch("-xb: requires argument");
1113 #if defined(x86_64_HOST_ARCH)
1114 case 'm': /* linkerMemBase */
1115 if (rts_argv[arg][3] != '\0') {
1116 RtsFlags.MiscFlags.linkerMemBase
1117 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1118 if (RtsFlags.MiscFlags.linkerMemBase > 0x80000000) {
1119 errorBelch("-xm: value must be <80000000");
1123 RtsFlags.MiscFlags.linkerMemBase = 0;
1128 case 'c': /* Debugging tool: show current cost centre on an exception */
1129 PROFILING_BUILD_ONLY(
1130 RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1134 case 't': /* Include memory used by TSOs in a heap profile */
1135 PROFILING_BUILD_ONLY(
1136 RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1140 /* The option prefix '-xx' is reserved for future extension. KSW 1999-11. */
1143 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1147 break; /* defensive programming */
1149 /* =========== OH DEAR ============================ */
1151 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1158 if (RtsFlags.MiscFlags.tickInterval < 0) {
1159 RtsFlags.MiscFlags.tickInterval = 50;
1162 // If the master timer is disabled, turn off the other timers.
1163 if (RtsFlags.MiscFlags.tickInterval == 0) {
1164 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1165 RtsFlags.GcFlags.idleGCDelayTime = 0;
1166 RtsFlags.ProfFlags.profileInterval = 0;
1169 // Determine what tick interval we should use for the RTS timer
1170 // by taking the shortest of the various intervals that we need to
1172 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1173 RtsFlags.MiscFlags.tickInterval =
1174 stg_min(RtsFlags.ConcFlags.ctxtSwitchTime,
1175 RtsFlags.MiscFlags.tickInterval);
1178 if (RtsFlags.GcFlags.idleGCDelayTime > 0) {
1179 RtsFlags.MiscFlags.tickInterval =
1180 stg_min(RtsFlags.GcFlags.idleGCDelayTime,
1181 RtsFlags.MiscFlags.tickInterval);
1184 if (RtsFlags.ProfFlags.profileInterval > 0) {
1185 RtsFlags.MiscFlags.tickInterval =
1186 stg_min(RtsFlags.ProfFlags.profileInterval,
1187 RtsFlags.MiscFlags.tickInterval);
1190 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1191 RtsFlags.ConcFlags.ctxtSwitchTicks =
1192 RtsFlags.ConcFlags.ctxtSwitchTime /
1193 RtsFlags.MiscFlags.tickInterval;
1195 RtsFlags.ConcFlags.ctxtSwitchTicks = 0;
1198 if (RtsFlags.ProfFlags.profileInterval > 0) {
1199 RtsFlags.ProfFlags.profileIntervalTicks =
1200 RtsFlags.ProfFlags.profileInterval /
1201 RtsFlags.MiscFlags.tickInterval;
1203 RtsFlags.ProfFlags.profileIntervalTicks = 0;
1210 for (p = usage_text; *p; p++)
1211 errorBelch("%s", *p);
1212 stg_exit(EXIT_FAILURE);
1218 stats_fprintf(FILE *f, char *s, ...)
1230 static int /* return -1 on error */
1233 int argc, char *argv[],
1234 int rts_argc, char *rts_argv[],
1235 const char *FILENAME_FMT,
1240 if (strequal(rts_argv[arg]+2, "stderr")
1241 || (FILENAME_FMT == NULL && rts_argv[arg][2] == '\0')) {
1242 f = NULL; /* NULL means use debugBelch */
1244 if (rts_argv[arg][2] != '\0') { /* stats file specified */
1245 f = fopen(rts_argv[arg]+2,"w");
1247 char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
1248 sprintf(stats_filename, FILENAME_FMT, argv[0]);
1249 f = fopen(stats_filename,"w");
1252 errorBelch("Can't open stats file %s\n", rts_argv[arg]+2);
1259 /* Write argv and rtsv into start of stats file */
1261 for(count = 0; count < argc; count++) {
1262 stats_fprintf(f, "%s ", argv[count]);
1264 stats_fprintf(f, "+RTS ");
1265 for(count = 0; count < rts_argc; count++)
1266 stats_fprintf(f, "%s ", rts_argv[count]);
1267 stats_fprintf(f, "\n");
1275 decode(const char *s)
1286 if (c == 'g' || c == 'G')
1287 m *= 1000*1000*1000; /* UNchecked! */
1288 else if (c == 'm' || c == 'M')
1289 m *= 1000*1000; /* We do not use powers of 2 (1024) */
1290 else if (c == 'k' || c == 'K') /* to avoid possible bad effects on */
1291 m *= 1000; /* a direct-mapped cache. */
1292 else if (c == 'w' || c == 'W')
1298 static void GNU_ATTRIBUTE(__noreturn__)
1299 bad_option(const char *s)
1301 errorBelch("bad RTS option: %s", s);
1302 stg_exit(EXIT_FAILURE);
1305 /* -----------------------------------------------------------------------------
1306 Getting/Setting the program's arguments.
1308 These are used by System.Environment, and parts of the RTS.
1309 -------------------------------------------------------------------------- */
1312 setProgName(char *argv[])
1314 /* Remove directory from argv[0] -- default files in current directory */
1315 #if !defined(mingw32_HOST_OS)
1317 if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
1318 prog_name = last_slash+1;
1320 prog_name = argv[0];
1323 char* last_slash = argv[0] + (strlen(argv[0]) - 1);
1324 while ( last_slash > argv[0] ) {
1325 if ( *last_slash == '/' || *last_slash == '\\' ) {
1326 prog_name = last_slash+1;
1331 prog_name = argv[0];
1336 getProgArgv(int *argc, char **argv[])
1338 if (argc) { *argc = prog_argc; }
1339 if (argv) { *argv = prog_argv; }
1343 setProgArgv(int argc, char *argv[])
1345 /* Usually this is done by startupHaskell, so we don't need to call this.
1346 However, sometimes Hugs wants to change the arguments which Haskell
1347 getArgs >>= ... will be fed. So you can do that by calling here
1348 _after_ calling startupHaskell.
1352 setProgName(prog_argv);
1355 /* These functions record and recall the full arguments, including the
1356 +RTS ... -RTS options. The reason for adding them was so that the
1357 ghc-inplace program can pass /all/ the arguments on to the real ghc. */
1359 getFullProgArgv(int *argc, char **argv[])
1361 if (argc) { *argc = full_prog_argc; }
1362 if (argv) { *argv = full_prog_argv; }
1366 setFullProgArgv(int argc, char *argv[])
1369 full_prog_argc = argc;
1370 full_prog_argv = stgCallocBytes(argc + 1, sizeof (char *),
1371 "setFullProgArgv 1");
1372 for (i = 0; i < argc; i++) {
1373 full_prog_argv[i] = stgMallocBytes(strlen(argv[i]) + 1,
1374 "setFullProgArgv 2");
1375 strcpy(full_prog_argv[i], argv[i]);
1377 full_prog_argv[argc] = NULL;