New flag +RTS -qi<n>, avoid waking up idle Capabilities to do parallel GC
[ghc.git] / rts / RtsFlags.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The AQUA Project, Glasgow University, 1994-1997
4 * (c) The GHC Team, 1998-2006
5 *
6 * Functions for parsing the argument list.
7 *
8 * ---------------------------------------------------------------------------*/
9
10 #include "PosixSource.h"
11 #include "Rts.h"
12
13 #include "RtsUtils.h"
14 #include "Profiling.h"
15 #include "RtsFlags.h"
16
17 #ifdef HAVE_CTYPE_H
18 #include <ctype.h>
19 #endif
20
21 #include <string.h>
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30
31 // Flag Structure
32 RTS_FLAGS RtsFlags;
33
34 /*
35 * Split argument lists
36 */
37 int prog_argc = 0; /* an "int" so as to match normal "argc" */
38 char **prog_argv = NULL;
39 int full_prog_argc = 0; /* an "int" so as to match normal "argc" */
40 char **full_prog_argv = NULL;
41 char *prog_name = NULL; /* 'basename' of prog_argv[0] */
42 int rts_argc = 0; /* ditto */
43 char **rts_argv = NULL;
44 #if defined(mingw32_HOST_OS)
45 // On Windows, we want to use GetCommandLineW rather than argc/argv,
46 // but we need to mutate the command line arguments for withProgName and
47 // friends. The System.Environment module achieves that using this bit of
48 // shared state:
49 int win32_prog_argc = 0;
50 wchar_t **win32_prog_argv = NULL;
51 #endif
52
53 /*
54 * constants, used later
55 */
56 #define RTS 1
57 #define PGM 0
58
59 /* -----------------------------------------------------------------------------
60 Static function decls
61 -------------------------------------------------------------------------- */
62
63 static void procRtsOpts (int rts_argc0, RtsOptsEnabledEnum enabled);
64
65 static void normaliseRtsOpts (void);
66
67 static void initStatsFile (FILE *f);
68
69 static int openStatsFile (char *filename, const char *FILENAME_FMT,
70 FILE **file_ret);
71
72 static StgWord64 decodeSize (const char *flag, nat offset,
73 StgWord64 min, StgWord64 max);
74
75 static void bad_option (const char *s);
76
77 #ifdef TRACING
78 static void read_trace_flags(char *arg);
79 #endif
80
81 static void errorUsage (void) GNU_ATTRIBUTE(__noreturn__);
82
83 static char * copyArg (char *arg);
84 static char ** copyArgv (int argc, char *argv[]);
85 static void freeArgv (int argc, char *argv[]);
86
87 /* -----------------------------------------------------------------------------
88 * Command-line option parsing routines.
89 * ---------------------------------------------------------------------------*/
90
91 void initRtsFlagsDefaults(void)
92 {
93 RtsFlags.GcFlags.statsFile = NULL;
94 RtsFlags.GcFlags.giveStats = NO_GC_STATS;
95
96 RtsFlags.GcFlags.maxStkSize = (8 * 1024 * 1024) / sizeof(W_);
97 RtsFlags.GcFlags.initialStkSize = 1024 / sizeof(W_);
98 RtsFlags.GcFlags.stkChunkSize = (32 * 1024) / sizeof(W_);
99 RtsFlags.GcFlags.stkChunkBufferSize = (1 * 1024) / sizeof(W_);
100
101 RtsFlags.GcFlags.minAllocAreaSize = (512 * 1024) / BLOCK_SIZE;
102 RtsFlags.GcFlags.minOldGenSize = (1024 * 1024) / BLOCK_SIZE;
103 RtsFlags.GcFlags.maxHeapSize = 0; /* off by default */
104 RtsFlags.GcFlags.heapSizeSuggestion = 0; /* none */
105 RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsFalse;
106 RtsFlags.GcFlags.pcFreeHeap = 3; /* 3% */
107 RtsFlags.GcFlags.oldGenFactor = 2;
108 RtsFlags.GcFlags.generations = 2;
109 RtsFlags.GcFlags.squeezeUpdFrames = rtsTrue;
110 RtsFlags.GcFlags.compact = rtsFalse;
111 RtsFlags.GcFlags.compactThreshold = 30.0;
112 RtsFlags.GcFlags.sweep = rtsFalse;
113 #ifdef RTS_GTK_FRONTPANEL
114 RtsFlags.GcFlags.frontpanel = rtsFalse;
115 #endif
116 RtsFlags.GcFlags.idleGCDelayTime = USToTime(300000); // 300ms
117
118 #if osf3_HOST_OS
119 /* ToDo: Perhaps by adjusting this value we can make linking without
120 * -static work (i.e., not generate a core-dumping executable)? */
121 # if SIZEOF_VOID_P == 8
122 RtsFlags.GcFlags.heapBase = 0x180000000L;
123 # else
124 # error I have no idea where to begin the heap on a non-64-bit osf3 machine.
125 # endif
126 #else
127 RtsFlags.GcFlags.heapBase = 0; /* means don't care */
128 #endif
129
130 #ifdef DEBUG
131 RtsFlags.DebugFlags.scheduler = rtsFalse;
132 RtsFlags.DebugFlags.interpreter = rtsFalse;
133 RtsFlags.DebugFlags.weak = rtsFalse;
134 RtsFlags.DebugFlags.gccafs = rtsFalse;
135 RtsFlags.DebugFlags.gc = rtsFalse;
136 RtsFlags.DebugFlags.block_alloc = rtsFalse;
137 RtsFlags.DebugFlags.sanity = rtsFalse;
138 RtsFlags.DebugFlags.stable = rtsFalse;
139 RtsFlags.DebugFlags.stm = rtsFalse;
140 RtsFlags.DebugFlags.prof = rtsFalse;
141 RtsFlags.DebugFlags.apply = rtsFalse;
142 RtsFlags.DebugFlags.linker = rtsFalse;
143 RtsFlags.DebugFlags.squeeze = rtsFalse;
144 RtsFlags.DebugFlags.hpc = rtsFalse;
145 RtsFlags.DebugFlags.sparks = rtsFalse;
146 #endif
147
148 #if defined(PROFILING)
149 RtsFlags.CcFlags.doCostCentres = 0;
150 #endif /* PROFILING */
151
152 RtsFlags.ProfFlags.doHeapProfile = rtsFalse;
153 RtsFlags.ProfFlags. heapProfileInterval = USToTime(100000); // 100ms
154
155 #ifdef PROFILING
156 RtsFlags.ProfFlags.includeTSOs = rtsFalse;
157 RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
158 RtsFlags.ProfFlags.maxRetainerSetSize = 8;
159 RtsFlags.ProfFlags.ccsLength = 25;
160 RtsFlags.ProfFlags.modSelector = NULL;
161 RtsFlags.ProfFlags.descrSelector = NULL;
162 RtsFlags.ProfFlags.typeSelector = NULL;
163 RtsFlags.ProfFlags.ccSelector = NULL;
164 RtsFlags.ProfFlags.ccsSelector = NULL;
165 RtsFlags.ProfFlags.retainerSelector = NULL;
166 RtsFlags.ProfFlags.bioSelector = NULL;
167 #endif
168
169 #ifdef TRACING
170 RtsFlags.TraceFlags.tracing = TRACE_NONE;
171 RtsFlags.TraceFlags.timestamp = rtsFalse;
172 RtsFlags.TraceFlags.scheduler = rtsFalse;
173 RtsFlags.TraceFlags.gc = rtsFalse;
174 RtsFlags.TraceFlags.sparks_sampled= rtsFalse;
175 RtsFlags.TraceFlags.sparks_full = rtsFalse;
176 RtsFlags.TraceFlags.user = rtsFalse;
177 #endif
178
179 #ifdef PROFILING
180 // When profiling we want a lot more ticks
181 RtsFlags.MiscFlags.tickInterval = USToTime(1000); // 1ms
182 #else
183 RtsFlags.MiscFlags.tickInterval = DEFAULT_TICK_INTERVAL;
184 #endif
185 RtsFlags.ConcFlags.ctxtSwitchTime = USToTime(20000); // 20ms
186
187 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
188 RtsFlags.MiscFlags.machineReadable = rtsFalse;
189 RtsFlags.MiscFlags.linkerMemBase = 0;
190
191 #ifdef THREADED_RTS
192 RtsFlags.ParFlags.nNodes = 1;
193 RtsFlags.ParFlags.migrate = rtsTrue;
194 RtsFlags.ParFlags.parGcEnabled = 1;
195 RtsFlags.ParFlags.parGcGen = 0;
196 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
197 RtsFlags.ParFlags.parGcLoadBalancingGen = 1;
198 RtsFlags.ParFlags.parGcNoSyncWithIdle = 0;
199 RtsFlags.ParFlags.setAffinity = 0;
200 #endif
201
202 #if defined(THREADED_RTS)
203 RtsFlags.ParFlags.maxLocalSparks = 4096;
204 #endif /* THREADED_RTS */
205
206 #ifdef TICKY_TICKY
207 RtsFlags.TickyFlags.showTickyStats = rtsFalse;
208 RtsFlags.TickyFlags.tickyFile = NULL;
209 #endif
210
211 #ifdef USE_PAPI
212 /* By default no special measurements taken */
213 RtsFlags.PapiFlags.eventType = 0;
214 RtsFlags.PapiFlags.numUserEvents = 0;
215 #endif
216 }
217
218 static const char *
219 usage_text[] = {
220 "",
221 "Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
222 "",
223 " +RTS Indicates run time system options follow",
224 " -RTS Indicates program arguments follow",
225 " --RTS Indicates that ALL subsequent arguments will be given to the",
226 " program (including any of these RTS flags)",
227 "",
228 "The following run time system options are available:",
229 "",
230 " -? Prints this message and exits; the program is not executed",
231 " --info Print information about the RTS used by this program",
232 "",
233 " -K<size> Sets the maximum stack size (default 8M) Egs: -K32k -K512k",
234 " -ki<size> Sets the initial thread stack size (default 1k) Egs: -ki4k -ki2m",
235 " -kc<size> Sets the stack chunk size (default 32k)",
236 " -kb<size> Sets the stack chunk buffer size (default 1k)",
237 "",
238 " -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
239 " -M<size> Sets the maximum heap size (default unlimited) Egs: -M256k -M1G",
240 " -H<size> Sets the minimum heap size (default 0M) Egs: -H24m -H1G",
241 " -m<n> Minimum % of heap which must be available (default 3%)",
242 " -G<n> Number of generations (default: 2)",
243 " -c<n> Use in-place compaction instead of copying in the oldest generation",
244 " when live data is at least <n>% of the maximum heap size set with",
245 " -M (default: 30%)",
246 " -c Use in-place compaction for all oldest generation collections",
247 " (the default is to use copying)",
248 " -w Use mark-region for the oldest generation (experimental)",
249 #if defined(THREADED_RTS)
250 " -I<sec> Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
251 #endif
252 "",
253 " -T Collect GC statistics (useful for in-program statistics access)",
254 " -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
255 " -s[<file>] Summary GC statistics (if <file> omitted, uses stderr)",
256 " -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
257 #ifdef RTS_GTK_FRONTPANEL
258 " -f Display front panel (requires X11 & GTK+)",
259 #endif
260 "",
261 "",
262 " -Z Don't squeeze out update frames on stack overflow",
263 " -B Sound the bell at the start of each garbage collection",
264 #if defined(PROFILING)
265 "",
266 " -p Time/allocation profile (output file <program>.prof)",
267 " -P More detailed Time/Allocation profile",
268 " -Pa Give information about *all* cost centres",
269
270 # if defined(PROFILING)
271 "",
272 " -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
273 " break-down: c = cost centre stack (default)",
274 " m = module",
275 " d = closure description",
276 " y = type description",
277 " r = retainer",
278 " b = biography (LAG,DRAG,VOID,USE)",
279 " A subset of closures may be selected thusly:",
280 " -hc<cc>,... specific cost centre(s) (top of stack only)",
281 " -hC<cc>,... specific cost centre(s) (anywhere in stack)",
282 " -hm<mod>... all cost centres from the specified modules(s)",
283 " -hd<des>,... closures with specified closure descriptions",
284 " -hy<typ>... closures with specified type descriptions",
285 " -hr<cc>... closures with specified retainers",
286 " -hb<bio>... closures with specified biographies (lag,drag,void,use)",
287 "",
288 " -R<size> Set the maximum retainer set size (default: 8)",
289 "",
290 " -L<chars> Maximum length of a cost-centre stack in a heap profile",
291 " (default: 25)",
292 "",
293 " -xt Include threads (TSOs) in a heap profile",
294 "",
295 " -xc Show current cost centre stack on raising an exception",
296 # endif
297 #endif /* PROFILING or PAR */
298
299 #ifdef TRACING
300 "",
301 " -l[flags] Log events in binary format to the file <program>.eventlog",
302 # ifdef DEBUG
303 " -v[flags] Log events to stderr",
304 # endif
305 " where [flags] can contain:",
306 " s scheduler events",
307 " g GC events",
308 " p par spark events (sampled)",
309 " f par spark events (full detail)",
310 " u user events (emitted from Haskell code)",
311 " a all event classes above",
312 # ifdef DEBUG
313 " t add time stamps (only useful with -v)",
314 # endif
315 " -x disable an event class, for any flag above",
316 " the initial enabled event classes are 'sgpu'",
317 #endif
318
319 #if !defined(PROFILING)
320 "",
321 " -h Heap residency profile (output file <program>.hp)",
322 #endif
323 " -i<sec> Time between heap profile samples (seconds, default: 0.1)",
324 "",
325 #if defined(TICKY_TICKY)
326 " -r<file> Produce ticky-ticky statistics (with -rstderr for stderr)",
327 "",
328 #endif
329 " -C<secs> Context-switch interval in seconds.",
330 " 0 or no argument means switch as often as possible.",
331 " Default: 0.02 sec.",
332 " -V<secs> Master tick interval in seconds (0 == disable timer).",
333 " This sets the resolution for -C and the heap profile timer -i,",
334 " and is the frequence of time profile samples.",
335 #ifdef PROFILING
336 " Default: 0.001 sec.",
337 #else
338 " Default: 0.01 sec.",
339 #endif
340 "",
341 #if defined(DEBUG)
342 " -Ds DEBUG: scheduler",
343 " -Di DEBUG: interpreter",
344 " -Dw DEBUG: weak",
345 " -DG DEBUG: gccafs",
346 " -Dg DEBUG: gc",
347 " -Db DEBUG: block",
348 " -DS DEBUG: sanity",
349 " -Dt DEBUG: stable",
350 " -Dp DEBUG: prof",
351 " -Da DEBUG: apply",
352 " -Dl DEBUG: linker",
353 " -Dm DEBUG: stm",
354 " -Dz DEBUG: stack squeezing",
355 " -Dc DEBUG: program coverage",
356 " -Dr DEBUG: sparks",
357 "",
358 " NOTE: DEBUG events are sent to stderr by default; add -l to create a",
359 " binary event log file instead.",
360 "",
361 #endif /* DEBUG */
362 #if defined(THREADED_RTS) && !defined(NOSMP)
363 " -N<n> Use <n> processors (default: 1)",
364 " -N Determine the number of processors to use automatically",
365 " -qg[<n>] Use parallel GC only for generations >= <n>",
366 " (default: 0, -qg alone turns off parallel GC)",
367 " -qb[<n>] Use load-balancing in the parallel GC only for generations >= <n>",
368 " (default: 1, -qb alone turns off load-balancing)",
369 " -qa Use the OS to set thread affinity (experimental)",
370 " -qm Don't automatically migrate threads between CPUs",
371 " -qi<n> If a processor has been idle for the last <n> GCs, do not",
372 " wake it up for a non-load-balancing parallel GC.",
373 " (0 disables, default: 0)",
374 #endif
375 " --install-signal-handlers=<yes|no>",
376 " Install signal handlers (default: yes)",
377 #if defined(THREADED_RTS)
378 " -e<n> Maximum number of outstanding local sparks (default: 4096)",
379 #endif
380 #if defined(x86_64_HOST_ARCH)
381 " -xm Base address to mmap memory in the GHCi linker",
382 " (hex; must be <80000000)",
383 #endif
384 #if defined(USE_PAPI)
385 " -aX CPU performance counter measurements using PAPI",
386 " (use with the -s<file> option). X is one of:",
387 "",
388 /* " y - cycles", */
389 " 1 - level 1 cache misses",
390 " 2 - level 2 cache misses",
391 " b - branch mispredictions",
392 " s - stalled cycles",
393 " e - cache miss and branch misprediction events",
394 " +PAPI_EVENT - collect papi preset event PAPI_EVENT",
395 " #NATIVE_EVENT - collect native event NATIVE_EVENT (in hex)",
396 #endif
397 "",
398 "RTS options may also be specified using the GHCRTS environment variable.",
399 "",
400 "Other RTS options may be available for programs compiled a different way.",
401 "The GHC User's Guide has full details.",
402 "",
403 0
404 };
405
406 STATIC_INLINE rtsBool
407 strequal(const char *a, const char * b)
408 {
409 return(strcmp(a, b) == 0);
410 }
411
412 static void splitRtsFlags(const char *s)
413 {
414 const char *c1, *c2;
415 char *t;
416
417 c1 = s;
418 do {
419 while (isspace(*c1)) { c1++; };
420 c2 = c1;
421 while (!isspace(*c2) && *c2 != '\0') { c2++; };
422
423 if (c1 == c2) { break; }
424
425 t = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
426 strncpy(t, c1, c2-c1);
427 t[c2-c1] = '\0';
428 rts_argv[rts_argc++] = t;
429
430 c1 = c2;
431 } while (*c1 != '\0');
432 }
433
434 /* -----------------------------------------------------------------------------
435 Parse the command line arguments, collecting options for the RTS.
436
437 On return:
438 - argv[] is *modified*, any RTS options have been stripped out
439 - *argc contains the new count of arguments in argv[]
440
441 - rts_argv[] (global) contains a copy of the collected RTS args
442 - rts_argc (global) contains the count of args in rts_argv
443
444 - prog_argv[] (global) contains a copy of the non-RTS args (== argv)
445 - prog_argc (global) contains the count of args in prog_argv
446
447 - prog_name (global) contains the basename of prog_argv[0]
448
449 -------------------------------------------------------------------------- */
450
451 void setupRtsFlags (int *argc, char *argv[],
452 RtsOptsEnabledEnum rtsOptsEnabled,
453 const char *ghc_rts_opts)
454 {
455 nat mode;
456 nat total_arg;
457 nat arg, rts_argc0;
458
459 setProgName (argv);
460 total_arg = *argc;
461 arg = 1;
462
463 *argc = 1;
464 rts_argc = 0;
465
466 rts_argv = stgCallocBytes(total_arg + 1, sizeof (char *), "setupRtsFlags");
467
468 rts_argc0 = rts_argc;
469
470 // process arguments from the ghc_rts_opts global variable first.
471 // (arguments from the GHCRTS environment variable and the command
472 // line override these).
473 {
474 if (ghc_rts_opts != NULL) {
475 splitRtsFlags(ghc_rts_opts);
476 // opts from ghc_rts_opts are always enabled:
477 procRtsOpts(rts_argc0, RtsOptsAll);
478 rts_argc0 = rts_argc;
479 }
480 }
481
482 // process arguments from the GHCRTS environment variable next
483 // (arguments from the command line override these).
484 {
485 char *ghc_rts = getenv("GHCRTS");
486
487 if (ghc_rts != NULL) {
488 if (rtsOptsEnabled == RtsOptsNone) {
489 errorBelch("Warning: Ignoring GHCRTS variable as RTS options are disabled.\n Link with -rtsopts to enable them.");
490 // We don't actually exit, just warn
491 } else {
492 splitRtsFlags(ghc_rts);
493 procRtsOpts(rts_argc0, rtsOptsEnabled);
494 rts_argc0 = rts_argc;
495 }
496 }
497 }
498
499 // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
500 // argv[0] must be PGM argument -- leave in argv
501
502 for (mode = PGM; arg < total_arg; arg++) {
503 // The '--RTS' argument disables all future +RTS ... -RTS processing.
504 if (strequal("--RTS", argv[arg])) {
505 arg++;
506 break;
507 }
508 // The '--' argument is passed through to the program, but
509 // disables all further +RTS ... -RTS processing.
510 else if (strequal("--", argv[arg])) {
511 break;
512 }
513 else if (strequal("+RTS", argv[arg])) {
514 mode = RTS;
515 }
516 else if (strequal("-RTS", argv[arg])) {
517 mode = PGM;
518 }
519 else if (mode == RTS) {
520 rts_argv[rts_argc++] = copyArg(argv[arg]);
521 }
522 else {
523 argv[(*argc)++] = argv[arg];
524 }
525 }
526 // process remaining program arguments
527 for (; arg < total_arg; arg++) {
528 argv[(*argc)++] = argv[arg];
529 }
530 argv[*argc] = (char *) 0;
531 rts_argv[rts_argc] = (char *) 0;
532
533 procRtsOpts(rts_argc0, rtsOptsEnabled);
534
535 normaliseRtsOpts();
536
537 setProgArgv(*argc, argv);
538
539 if (RtsFlags.GcFlags.statsFile != NULL) {
540 initStatsFile (RtsFlags.GcFlags.statsFile);
541 }
542 if (RtsFlags.TickyFlags.tickyFile != NULL) {
543 initStatsFile (RtsFlags.GcFlags.statsFile);
544 }
545 }
546
547 /* -----------------------------------------------------------------------------
548 * procRtsOpts: Process rts_argv between rts_argc0 and rts_argc.
549 * -------------------------------------------------------------------------- */
550
551 static void checkSuid(RtsOptsEnabledEnum enabled)
552 {
553 if (enabled == RtsOptsSafeOnly) {
554 #if defined(HAVE_UNISTD_H) && defined(HAVE_SYS_TYPES_H) && !defined(mingw32_HOST_OS)
555 /* This doesn't cover linux/posix capabilities like CAP_DAC_OVERRIDE,
556 we'd have to link with -lcap for that. */
557 if ((getuid() != geteuid()) || (getgid() != getegid())) {
558 errorBelch("RTS options are disabled for setuid binaries. Link with -rtsopts to enable them.");
559 stg_exit(EXIT_FAILURE);
560 }
561 #endif
562 }
563 }
564
565 static void checkUnsafe(RtsOptsEnabledEnum enabled)
566 {
567 if (enabled == RtsOptsSafeOnly) {
568 errorBelch("Most RTS options are disabled. Link with -rtsopts to enable them.");
569 stg_exit(EXIT_FAILURE);
570 }
571 }
572
573 static void procRtsOpts (int rts_argc0, RtsOptsEnabledEnum rtsOptsEnabled)
574 {
575 rtsBool error = rtsFalse;
576 int arg;
577
578 if (!(rts_argc0 < rts_argc)) return;
579
580 if (rtsOptsEnabled == RtsOptsNone) {
581 errorBelch("RTS options are disabled. Link with -rtsopts to enable them.");
582 stg_exit(EXIT_FAILURE);
583 }
584
585 checkSuid(rtsOptsEnabled);
586
587 // Process RTS (rts_argv) part: mainly to determine statsfile
588 for (arg = rts_argc0; arg < rts_argc; arg++) {
589
590 /* We handle RtsOptsSafeOnly mode by declaring each option as
591 either OPTION_SAFE or OPTION_UNSAFE. To make sure we cover
592 every branch we use an option_checked flag which is reset
593 at the start each iteration and checked at the end. */
594 rtsBool option_checked = rtsFalse;
595
596 #define OPTION_SAFE option_checked = rtsTrue;
597 #define OPTION_UNSAFE checkUnsafe(rtsOptsEnabled); option_checked = rtsTrue;
598
599 if (rts_argv[arg][0] != '-') {
600 fflush(stdout);
601 errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
602 error = rtsTrue;
603
604 } else {
605
606 switch(rts_argv[arg][1]) {
607
608 /* process: general args, then PROFILING-only ones, then
609 CONCURRENT-only, TICKY-only (same order as defined in
610 RtsFlags.lh); within those groups, mostly in
611 case-insensitive alphabetical order. Final group is
612 x*, which allows for more options.
613 */
614
615 #ifdef TICKY_TICKY
616 # define TICKY_BUILD_ONLY(x) x
617 #else
618 # define TICKY_BUILD_ONLY(x) \
619 errorBelch("the flag %s requires the program to be built with -ticky", rts_argv[arg]); \
620 error = rtsTrue;
621 #endif
622
623 #ifdef PROFILING
624 # define PROFILING_BUILD_ONLY(x) x
625 #else
626 # define PROFILING_BUILD_ONLY(x) \
627 errorBelch("the flag %s requires the program to be built with -prof", rts_argv[arg]); \
628 error = rtsTrue;
629 #endif
630
631 #ifdef TRACING
632 # define TRACING_BUILD_ONLY(x) x
633 #else
634 # define TRACING_BUILD_ONLY(x) \
635 errorBelch("the flag %s requires the program to be built with -eventlog or -debug", rts_argv[arg]); \
636 error = rtsTrue;
637 #endif
638
639 #ifdef THREADED_RTS
640 # define THREADED_BUILD_ONLY(x) x
641 #else
642 # define THREADED_BUILD_ONLY(x) \
643 errorBelch("the flag %s requires the program to be built with -threaded", rts_argv[arg]); \
644 error = rtsTrue;
645 #endif
646
647 #ifdef DEBUG
648 # define DEBUG_BUILD_ONLY(x) x
649 #else
650 # define DEBUG_BUILD_ONLY(x) \
651 errorBelch("the flag %s requires the program to be built with -debug", rts_argv[arg]); \
652 error = rtsTrue;
653 #endif
654
655 /* =========== GENERAL ========================== */
656 case '?':
657 OPTION_SAFE;
658 error = rtsTrue;
659 break;
660
661 /* This isn't going to allow us to keep related options
662 together as we add more --* flags. We really need a
663 proper options parser. */
664 case '-':
665 if (strequal("install-signal-handlers=yes",
666 &rts_argv[arg][2])) {
667 OPTION_UNSAFE;
668 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
669 }
670 else if (strequal("install-signal-handlers=no",
671 &rts_argv[arg][2])) {
672 OPTION_UNSAFE;
673 RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
674 }
675 else if (strequal("machine-readable",
676 &rts_argv[arg][2])) {
677 OPTION_UNSAFE;
678 RtsFlags.MiscFlags.machineReadable = rtsTrue;
679 }
680 else if (strequal("info",
681 &rts_argv[arg][2])) {
682 OPTION_SAFE;
683 printRtsInfo();
684 stg_exit(0);
685 }
686 else {
687 OPTION_SAFE;
688 errorBelch("unknown RTS option: %s",rts_argv[arg]);
689 error = rtsTrue;
690 }
691 break;
692 case 'A':
693 OPTION_UNSAFE;
694 RtsFlags.GcFlags.minAllocAreaSize
695 = decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_INT_MAX)
696 / BLOCK_SIZE;
697 break;
698
699 #ifdef USE_PAPI
700 case 'a':
701 OPTION_UNSAFE;
702 switch(rts_argv[arg][2]) {
703 case '1':
704 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
705 break;
706 case '2':
707 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
708 break;
709 case 'b':
710 RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
711 break;
712 case 's':
713 RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
714 break;
715 case 'e':
716 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
717 break;
718 case '+':
719 case '#':
720 if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
721 errorBelch("maximum number of PAPI events reached");
722 stg_exit(EXIT_FAILURE);
723 }
724 nat eventNum = RtsFlags.PapiFlags.numUserEvents++;
725 char kind = rts_argv[arg][2];
726 nat eventKind = kind == '+' ? PAPI_PRESET_EVENT_KIND : PAPI_NATIVE_EVENT_KIND;
727
728 RtsFlags.PapiFlags.userEvents[eventNum] = rts_argv[arg] + 3;
729 RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
730 RtsFlags.PapiFlags.userEventsKind[eventNum] = eventKind;
731 break;
732 default:
733 bad_option( rts_argv[arg] );
734 }
735 break;
736 #endif
737
738 case 'B':
739 OPTION_UNSAFE;
740 RtsFlags.GcFlags.ringBell = rtsTrue;
741 break;
742
743 case 'c':
744 OPTION_UNSAFE;
745 if (rts_argv[arg][2] != '\0') {
746 RtsFlags.GcFlags.compactThreshold =
747 atof(rts_argv[arg]+2);
748 } else {
749 RtsFlags.GcFlags.compact = rtsTrue;
750 }
751 break;
752
753 case 'w':
754 OPTION_UNSAFE;
755 RtsFlags.GcFlags.sweep = rtsTrue;
756 break;
757
758 case 'F':
759 OPTION_UNSAFE;
760 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
761
762 if (RtsFlags.GcFlags.oldGenFactor < 0)
763 bad_option( rts_argv[arg] );
764 break;
765
766 case 'D':
767 OPTION_SAFE;
768 DEBUG_BUILD_ONLY(
769 {
770 char *c;
771
772 for (c = rts_argv[arg] + 2; *c != '\0'; c++) {
773 switch (*c) {
774 case 's':
775 RtsFlags.DebugFlags.scheduler = rtsTrue;
776 break;
777 case 'i':
778 RtsFlags.DebugFlags.interpreter = rtsTrue;
779 break;
780 case 'w':
781 RtsFlags.DebugFlags.weak = rtsTrue;
782 break;
783 case 'G':
784 RtsFlags.DebugFlags.gccafs = rtsTrue;
785 break;
786 case 'g':
787 RtsFlags.DebugFlags.gc = rtsTrue;
788 break;
789 case 'b':
790 RtsFlags.DebugFlags.block_alloc = rtsTrue;
791 break;
792 case 'S':
793 RtsFlags.DebugFlags.sanity = rtsTrue;
794 break;
795 case 't':
796 RtsFlags.DebugFlags.stable = rtsTrue;
797 break;
798 case 'p':
799 RtsFlags.DebugFlags.prof = rtsTrue;
800 break;
801 case 'l':
802 RtsFlags.DebugFlags.linker = rtsTrue;
803 break;
804 case 'a':
805 RtsFlags.DebugFlags.apply = rtsTrue;
806 break;
807 case 'm':
808 RtsFlags.DebugFlags.stm = rtsTrue;
809 break;
810 case 'z':
811 RtsFlags.DebugFlags.squeeze = rtsTrue;
812 break;
813 case 'c':
814 RtsFlags.DebugFlags.hpc = rtsTrue;
815 break;
816 case 'r':
817 RtsFlags.DebugFlags.sparks = rtsTrue;
818 break;
819 default:
820 bad_option( rts_argv[arg] );
821 }
822 }
823 // -Dx also turns on -v. Use -l to direct trace
824 // events to the .eventlog file instead.
825 RtsFlags.TraceFlags.tracing = TRACE_STDERR;
826 })
827 break;
828
829 case 'K':
830 OPTION_UNSAFE;
831 RtsFlags.GcFlags.maxStkSize =
832 decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
833 break;
834
835 case 'k':
836 OPTION_UNSAFE;
837 switch(rts_argv[arg][2]) {
838 case 'c':
839 RtsFlags.GcFlags.stkChunkSize =
840 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
841 break;
842 case 'b':
843 RtsFlags.GcFlags.stkChunkBufferSize =
844 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
845 break;
846 case 'i':
847 RtsFlags.GcFlags.initialStkSize =
848 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
849 break;
850 default:
851 RtsFlags.GcFlags.initialStkSize =
852 decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
853 break;
854 }
855 break;
856
857 case 'M':
858 OPTION_UNSAFE;
859 RtsFlags.GcFlags.maxHeapSize =
860 decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE;
861 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
862 break;
863
864 case 'm':
865 OPTION_UNSAFE;
866 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
867
868 if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
869 RtsFlags.GcFlags.pcFreeHeap > 100)
870 bad_option( rts_argv[arg] );
871 break;
872
873 case 'G':
874 OPTION_UNSAFE;
875 RtsFlags.GcFlags.generations =
876 decodeSize(rts_argv[arg], 2, 1, HS_INT_MAX);
877 break;
878
879 case 'H':
880 OPTION_UNSAFE;
881 if (rts_argv[arg][2] == '\0') {
882 RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsTrue;
883 } else {
884 RtsFlags.GcFlags.heapSizeSuggestion =
885 (nat)(decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE);
886 }
887 break;
888
889 #ifdef RTS_GTK_FRONTPANEL
890 case 'f':
891 OPTION_UNSAFE;
892 RtsFlags.GcFlags.frontpanel = rtsTrue;
893 break;
894 #endif
895
896 case 'I': /* idle GC delay */
897 OPTION_UNSAFE;
898 if (rts_argv[arg][2] == '\0') {
899 /* use default */
900 } else {
901 RtsFlags.GcFlags.idleGCDelayTime =
902 fsecondsToTime(atof(rts_argv[arg]+2));
903 }
904 break;
905
906 case 'T':
907 OPTION_SAFE;
908 RtsFlags.GcFlags.giveStats = COLLECT_GC_STATS;
909 break; /* Don't initialize statistics file. */
910
911 case 'S':
912 OPTION_SAFE; /* but see below */
913 RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
914 goto stats;
915
916 case 's':
917 OPTION_SAFE; /* but see below */
918 RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
919 goto stats;
920
921 case 't':
922 OPTION_SAFE; /* but see below */
923 RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
924 goto stats;
925
926 stats:
927 {
928 int r;
929 if (rts_argv[arg][2] != '\0') {
930 OPTION_UNSAFE;
931 }
932 r = openStatsFile(rts_argv[arg]+2, NULL,
933 &RtsFlags.GcFlags.statsFile);
934 if (r == -1) { error = rtsTrue; }
935 }
936 break;
937
938 case 'Z':
939 OPTION_UNSAFE;
940 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
941 break;
942
943 /* =========== PROFILING ========================== */
944
945 case 'P': /* detailed cost centre profiling (time/alloc) */
946 case 'p': /* cost centre profiling (time/alloc) */
947 OPTION_SAFE;
948 PROFILING_BUILD_ONLY(
949 switch (rts_argv[arg][2]) {
950 case 'a':
951 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
952 break;
953 default:
954 if (rts_argv[arg][1] == 'P') {
955 RtsFlags.CcFlags.doCostCentres =
956 COST_CENTRES_VERBOSE;
957 } else {
958 RtsFlags.CcFlags.doCostCentres =
959 COST_CENTRES_SUMMARY;
960 }
961 break;
962 }
963 ) break;
964
965 case 'R':
966 OPTION_SAFE;
967 PROFILING_BUILD_ONLY(
968 RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
969 ) break;
970 case 'L':
971 OPTION_SAFE;
972 PROFILING_BUILD_ONLY(
973 RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
974 if(RtsFlags.ProfFlags.ccsLength <= 0) {
975 bad_option(rts_argv[arg]);
976 }
977 ) break;
978 case 'h': /* serial heap profile */
979 #if !defined(PROFILING)
980 OPTION_UNSAFE;
981 switch (rts_argv[arg][2]) {
982 case '\0':
983 case 'T':
984 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
985 break;
986 default:
987 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
988 error = rtsTrue;
989 }
990 #else
991 OPTION_SAFE;
992 PROFILING_BUILD_ONLY(
993 switch (rts_argv[arg][2]) {
994 case '\0':
995 case 'C':
996 case 'c':
997 case 'M':
998 case 'm':
999 case 'D':
1000 case 'd':
1001 case 'Y':
1002 case 'y':
1003 case 'R':
1004 case 'r':
1005 case 'B':
1006 case 'b':
1007 if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
1008 {
1009 char *left = strchr(rts_argv[arg], '{');
1010 char *right = strrchr(rts_argv[arg], '}');
1011
1012 // curly braces are optional, for
1013 // backwards compat.
1014 if (left)
1015 left = left+1;
1016 else
1017 left = rts_argv[arg] + 3;
1018
1019 if (!right)
1020 right = rts_argv[arg] + strlen(rts_argv[arg]);
1021
1022 *right = '\0';
1023
1024 switch (rts_argv[arg][2]) {
1025 case 'c': // cost centre label select
1026 RtsFlags.ProfFlags.ccSelector = left;
1027 break;
1028 case 'C':
1029 RtsFlags.ProfFlags.ccsSelector = left;
1030 break;
1031 case 'M':
1032 case 'm': // cost centre module select
1033 RtsFlags.ProfFlags.modSelector = left;
1034 break;
1035 case 'D':
1036 case 'd': // closure descr select
1037 RtsFlags.ProfFlags.descrSelector = left;
1038 break;
1039 case 'Y':
1040 case 'y': // closure type select
1041 RtsFlags.ProfFlags.typeSelector = left;
1042 break;
1043 case 'R':
1044 case 'r': // retainer select
1045 RtsFlags.ProfFlags.retainerSelector = left;
1046 break;
1047 case 'B':
1048 case 'b': // biography select
1049 RtsFlags.ProfFlags.bioSelector = left;
1050 break;
1051 }
1052 }
1053 break;
1054 }
1055
1056 if (RtsFlags.ProfFlags.doHeapProfile != 0) {
1057 errorBelch("multiple heap profile options");
1058 error = rtsTrue;
1059 break;
1060 }
1061
1062 switch (rts_argv[arg][2]) {
1063 case '\0':
1064 case 'C':
1065 case 'c':
1066 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
1067 break;
1068 case 'M':
1069 case 'm':
1070 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
1071 break;
1072 case 'D':
1073 case 'd':
1074 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
1075 break;
1076 case 'Y':
1077 case 'y':
1078 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
1079 break;
1080 case 'R':
1081 case 'r':
1082 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
1083 break;
1084 case 'B':
1085 case 'b':
1086 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
1087 break;
1088 }
1089 break;
1090
1091 default:
1092 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
1093 error = rtsTrue;
1094 }
1095 )
1096 #endif /* PROFILING */
1097 break;
1098
1099 case 'i': /* heap sample interval */
1100 OPTION_UNSAFE;
1101 if (rts_argv[arg][2] == '\0') {
1102 /* use default */
1103 } else {
1104 RtsFlags.ProfFlags.heapProfileInterval =
1105 fsecondsToTime(atof(rts_argv[arg]+2));
1106 }
1107 break;
1108
1109 /* =========== CONCURRENT ========================= */
1110 case 'C': /* context switch interval */
1111 OPTION_UNSAFE;
1112 if (rts_argv[arg][2] == '\0')
1113 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1114 else {
1115 RtsFlags.ConcFlags.ctxtSwitchTime =
1116 fsecondsToTime(atof(rts_argv[arg]+2));
1117 }
1118 break;
1119
1120 case 'V': /* master tick interval */
1121 OPTION_UNSAFE;
1122 if (rts_argv[arg][2] == '\0') {
1123 // turns off ticks completely
1124 RtsFlags.MiscFlags.tickInterval = 0;
1125 } else {
1126 RtsFlags.MiscFlags.tickInterval =
1127 fsecondsToTime(atof(rts_argv[arg]+2));
1128 }
1129 break;
1130
1131 #if !defined(NOSMP)
1132 case 'N':
1133 OPTION_SAFE;
1134 THREADED_BUILD_ONLY(
1135 if (rts_argv[arg][2] == '\0') {
1136 #if defined(PROFILING)
1137 RtsFlags.ParFlags.nNodes = 1;
1138 #else
1139 RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
1140 #endif
1141 } else {
1142 int nNodes;
1143 OPTION_SAFE; /* but see extra checks below... */
1144 nNodes = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1145 if (nNodes <= 0) {
1146 errorBelch("bad value for -N");
1147 error = rtsTrue;
1148 }
1149 if (rtsOptsEnabled == RtsOptsSafeOnly &&
1150 nNodes > (int)getNumberOfProcessors()) {
1151 errorBelch("Using large values for -N is not allowed by default. Link with -rtsopts to allow full control.");
1152 stg_exit(EXIT_FAILURE);
1153 }
1154 RtsFlags.ParFlags.nNodes = (nat)nNodes;
1155 }
1156 ) break;
1157
1158 case 'g':
1159 OPTION_UNSAFE;
1160 THREADED_BUILD_ONLY(
1161 switch (rts_argv[arg][2]) {
1162 case '1':
1163 // backwards compat only
1164 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1165 break;
1166 default:
1167 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1168 error = rtsTrue;
1169 break;
1170 }
1171 ) break;
1172
1173 case 'q':
1174 OPTION_UNSAFE;
1175 THREADED_BUILD_ONLY(
1176 switch (rts_argv[arg][2]) {
1177 case '\0':
1178 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1179 error = rtsTrue;
1180 break;
1181 case 'g':
1182 if (rts_argv[arg][3] == '\0') {
1183 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1184 } else {
1185 RtsFlags.ParFlags.parGcEnabled = rtsTrue;
1186 RtsFlags.ParFlags.parGcGen
1187 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1188 }
1189 break;
1190 case 'b':
1191 if (rts_argv[arg][3] == '\0') {
1192 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsFalse;
1193 }
1194 else {
1195 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
1196 RtsFlags.ParFlags.parGcLoadBalancingGen
1197 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1198 }
1199 break;
1200 case 'i':
1201 RtsFlags.ParFlags.parGcNoSyncWithIdle
1202 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1203 break;
1204 case 'a':
1205 RtsFlags.ParFlags.setAffinity = rtsTrue;
1206 break;
1207 case 'm':
1208 RtsFlags.ParFlags.migrate = rtsFalse;
1209 break;
1210 case 'w':
1211 // -qw was removed; accepted for backwards compat
1212 break;
1213 default:
1214 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1215 error = rtsTrue;
1216 break;
1217 }
1218 ) break;
1219 #endif
1220 /* =========== PARALLEL =========================== */
1221 case 'e':
1222 OPTION_UNSAFE;
1223 THREADED_BUILD_ONLY(
1224 if (rts_argv[arg][2] != '\0') {
1225 RtsFlags.ParFlags.maxLocalSparks
1226 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1227 if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1228 errorBelch("bad value for -e");
1229 error = rtsTrue;
1230 }
1231 }
1232 ) break;
1233
1234 /* =========== TICKY ============================== */
1235
1236 case 'r': /* Basic profiling stats */
1237 OPTION_SAFE;
1238 TICKY_BUILD_ONLY(
1239
1240 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1241
1242 {
1243 int r;
1244 if (rts_argv[arg][2] != '\0') {
1245 OPTION_UNSAFE;
1246 }
1247 r = openStatsFile(rts_argv[arg]+2,
1248 TICKY_FILENAME_FMT,
1249 &RtsFlags.TickyFlags.tickyFile);
1250 if (r == -1) { error = rtsTrue; }
1251 }
1252 ) break;
1253
1254 /* =========== TRACING ---------=================== */
1255
1256 case 'l':
1257 OPTION_SAFE;
1258 TRACING_BUILD_ONLY(
1259 RtsFlags.TraceFlags.tracing = TRACE_EVENTLOG;
1260 read_trace_flags(&rts_argv[arg][2]);
1261 );
1262 break;
1263
1264 case 'v':
1265 OPTION_SAFE;
1266 DEBUG_BUILD_ONLY(
1267 RtsFlags.TraceFlags.tracing = TRACE_STDERR;
1268 read_trace_flags(&rts_argv[arg][2]);
1269 );
1270 break;
1271
1272 /* =========== EXTENDED OPTIONS =================== */
1273
1274 case 'x': /* Extend the argument space */
1275 switch(rts_argv[arg][2]) {
1276 case '\0':
1277 OPTION_SAFE;
1278 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1279 error = rtsTrue;
1280 break;
1281
1282 case 'b': /* heapBase in hex; undocumented */
1283 OPTION_UNSAFE;
1284 if (rts_argv[arg][3] != '\0') {
1285 RtsFlags.GcFlags.heapBase
1286 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1287 } else {
1288 errorBelch("-xb: requires argument");
1289 error = rtsTrue;
1290 }
1291 break;
1292
1293 #if defined(x86_64_HOST_ARCH)
1294 case 'm': /* linkerMemBase */
1295 OPTION_UNSAFE;
1296 if (rts_argv[arg][3] != '\0') {
1297 RtsFlags.MiscFlags.linkerMemBase
1298 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1299 if (RtsFlags.MiscFlags.linkerMemBase > 0x80000000) {
1300 errorBelch("-xm: value must be <80000000");
1301 error = rtsTrue;
1302 }
1303 } else {
1304 RtsFlags.MiscFlags.linkerMemBase = 0;
1305 }
1306 break;
1307 #endif
1308
1309 case 'c': /* Debugging tool: show current cost centre on an exception */
1310 OPTION_SAFE;
1311 PROFILING_BUILD_ONLY(
1312 RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1313 );
1314 break;
1315
1316 case 't': /* Include memory used by TSOs in a heap profile */
1317 OPTION_SAFE;
1318 PROFILING_BUILD_ONLY(
1319 RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1320 );
1321 break;
1322
1323 /* The option prefix '-xx' is reserved for future extension. KSW 1999-11. */
1324
1325 default:
1326 OPTION_SAFE;
1327 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1328 error = rtsTrue;
1329 break;
1330 }
1331 break; /* defensive programming */
1332
1333 /* =========== OH DEAR ============================ */
1334 default:
1335 OPTION_SAFE;
1336 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1337 error = rtsTrue;
1338 break;
1339 }
1340
1341 if (!option_checked) {
1342 /* Naughty! Someone didn't use OPTION_UNSAFE / OPTION_SAFE for
1343 an option above */
1344 errorBelch("Internal error in the RTS options parser");
1345 stg_exit(EXIT_FAILURE);
1346 }
1347 }
1348 }
1349
1350 if (error) errorUsage();
1351 }
1352
1353 /* -----------------------------------------------------------------------------
1354 * normaliseRtsOpts: Set some derived values, and make sure things are
1355 * within sensible ranges.
1356 * -------------------------------------------------------------------------- */
1357
1358 static void normaliseRtsOpts (void)
1359 {
1360 if (RtsFlags.MiscFlags.tickInterval < 0) {
1361 RtsFlags.MiscFlags.tickInterval = DEFAULT_TICK_INTERVAL;
1362 }
1363
1364 // If the master timer is disabled, turn off the other timers.
1365 if (RtsFlags.MiscFlags.tickInterval == 0) {
1366 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1367 RtsFlags.GcFlags.idleGCDelayTime = 0;
1368 RtsFlags.ProfFlags.heapProfileInterval = 0;
1369 }
1370
1371 // Determine what tick interval we should use for the RTS timer
1372 // by taking the shortest of the various intervals that we need to
1373 // monitor.
1374 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1375 RtsFlags.MiscFlags.tickInterval =
1376 stg_min(RtsFlags.ConcFlags.ctxtSwitchTime,
1377 RtsFlags.MiscFlags.tickInterval);
1378 }
1379
1380 if (RtsFlags.GcFlags.idleGCDelayTime > 0) {
1381 RtsFlags.MiscFlags.tickInterval =
1382 stg_min(RtsFlags.GcFlags.idleGCDelayTime,
1383 RtsFlags.MiscFlags.tickInterval);
1384 }
1385
1386 if (RtsFlags.ProfFlags.heapProfileInterval > 0) {
1387 RtsFlags.MiscFlags.tickInterval =
1388 stg_min(RtsFlags.ProfFlags.heapProfileInterval,
1389 RtsFlags.MiscFlags.tickInterval);
1390 }
1391
1392 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1393 RtsFlags.ConcFlags.ctxtSwitchTicks =
1394 RtsFlags.ConcFlags.ctxtSwitchTime /
1395 RtsFlags.MiscFlags.tickInterval;
1396 } else {
1397 RtsFlags.ConcFlags.ctxtSwitchTicks = 0;
1398 }
1399
1400 if (RtsFlags.ProfFlags.heapProfileInterval > 0) {
1401 RtsFlags.ProfFlags.heapProfileIntervalTicks =
1402 RtsFlags.ProfFlags.heapProfileInterval /
1403 RtsFlags.MiscFlags.tickInterval;
1404 } else {
1405 RtsFlags.ProfFlags.heapProfileIntervalTicks = 0;
1406 }
1407
1408 if (RtsFlags.GcFlags.stkChunkBufferSize >
1409 RtsFlags.GcFlags.stkChunkSize / 2) {
1410 errorBelch("stack chunk buffer size (-kb) must be less than 50%% of the stack chunk size (-kc)");
1411 errorUsage();
1412 }
1413 }
1414
1415 static void errorUsage (void)
1416 {
1417 const char **p;
1418
1419 fflush(stdout);
1420 for (p = usage_text; *p; p++)
1421 errorBelch("%s", *p);
1422 stg_exit(EXIT_FAILURE);
1423 }
1424
1425 static void
1426 stats_fprintf(FILE *f, char *s, ...)
1427 {
1428 va_list ap;
1429 va_start(ap,s);
1430 if (f == NULL) {
1431 vdebugBelch(s, ap);
1432 } else {
1433 vfprintf(f, s, ap);
1434 }
1435 va_end(ap);
1436 }
1437
1438 /* -----------------------------------------------------------------------------
1439 * openStatsFile: open a file in which to put some runtime stats
1440 * -------------------------------------------------------------------------- */
1441
1442 static int // return -1 on error
1443 openStatsFile (char *filename, // filename, or NULL
1444 const char *filename_fmt, // if filename == NULL, use
1445 // this fmt with sprintf to
1446 // generate the filename. %s
1447 // expands to the program name.
1448 FILE **file_ret) // return the FILE*
1449 {
1450 FILE *f = NULL;
1451
1452 if (strequal(filename, "stderr")
1453 || (filename_fmt == NULL && *filename == '\0')) {
1454 f = NULL; /* NULL means use debugBelch */
1455 } else {
1456 if (*filename != '\0') { /* stats file specified */
1457 f = fopen(filename,"w");
1458 } else {
1459 char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
1460 sprintf(stats_filename, filename_fmt, prog_name);
1461 f = fopen(stats_filename,"w");
1462 }
1463 if (f == NULL) {
1464 errorBelch("Can't open stats file %s\n", filename);
1465 return -1;
1466 }
1467 }
1468 *file_ret = f;
1469
1470 return 0;
1471 }
1472
1473 /* -----------------------------------------------------------------------------
1474 * initStatsFile: write a line to the file containing the program name
1475 * and the arguments it was invoked with.
1476 -------------------------------------------------------------------------- */
1477
1478 static void initStatsFile (FILE *f)
1479 {
1480 /* Write prog_argv and rts_argv into start of stats file */
1481 int count;
1482 for (count = 0; count < prog_argc; count++) {
1483 stats_fprintf(f, "%s ", prog_argv[count]);
1484 }
1485 stats_fprintf(f, "+RTS ");
1486 for (count = 0; count < rts_argc; count++)
1487 stats_fprintf(f, "%s ", rts_argv[count]);
1488 stats_fprintf(f, "\n");
1489 }
1490
1491 /* -----------------------------------------------------------------------------
1492 * decodeSize: parse a string containing a size, like 300K or 1.2M
1493 -------------------------------------------------------------------------- */
1494
1495 static StgWord64
1496 decodeSize(const char *flag, nat offset, StgWord64 min, StgWord64 max)
1497 {
1498 char c;
1499 const char *s;
1500 StgDouble m;
1501 StgWord64 val;
1502
1503 s = flag + offset;
1504
1505 if (!*s)
1506 {
1507 m = 0;
1508 }
1509 else
1510 {
1511 m = atof(s);
1512 c = s[strlen(s)-1];
1513
1514 if (c == 'g' || c == 'G')
1515 m *= 1024*1024*1024;
1516 else if (c == 'm' || c == 'M')
1517 m *= 1024*1024;
1518 else if (c == 'k' || c == 'K')
1519 m *= 1024;
1520 else if (c == 'w' || c == 'W')
1521 m *= sizeof(W_);
1522 }
1523
1524 val = (StgWord64)m;
1525
1526 if (m < 0 || val < min || val > max) {
1527 // printf doesn't like 64-bit format specs on Windows
1528 // apparently, so fall back to unsigned long.
1529 errorBelch("error in RTS option %s: size outside allowed range (%lu - %lu)", flag, (lnat)min, (lnat)max);
1530 stg_exit(EXIT_FAILURE);
1531 }
1532
1533 return val;
1534 }
1535
1536 #if defined(TRACING)
1537 static void read_trace_flags(char *arg)
1538 {
1539 char *c;
1540 rtsBool enabled = rtsTrue;
1541 /* Syntax for tracing flags currently looks like:
1542 *
1543 * -l To turn on eventlog tracing with default trace classes
1544 * -lx Turn on class 'x' (for some class listed below)
1545 * -l-x Turn off class 'x'
1546 * -la Turn on all classes
1547 * -l-a Turn off all classes
1548 *
1549 * This lets users say things like:
1550 * -la-p "all but sparks"
1551 * -l-ap "only sparks"
1552 */
1553
1554 /* Start by turning on the default tracing flags.
1555 *
1556 * Currently this is all the trace classes, except full-detail sparks.
1557 * Similarly, in future we might default to slightly less verbose
1558 * scheduler or GC tracing.
1559 */
1560 RtsFlags.TraceFlags.scheduler = rtsTrue;
1561 RtsFlags.TraceFlags.gc = rtsTrue;
1562 RtsFlags.TraceFlags.sparks_sampled = rtsTrue;
1563 RtsFlags.TraceFlags.user = rtsTrue;
1564
1565 for (c = arg; *c != '\0'; c++) {
1566 switch(*c) {
1567 case '\0':
1568 break;
1569 case '-':
1570 enabled = rtsFalse;
1571 break;
1572 case 'a':
1573 RtsFlags.TraceFlags.scheduler = enabled;
1574 RtsFlags.TraceFlags.gc = enabled;
1575 RtsFlags.TraceFlags.sparks_sampled = enabled;
1576 RtsFlags.TraceFlags.sparks_full = enabled;
1577 RtsFlags.TraceFlags.user = enabled;
1578 enabled = rtsTrue;
1579 break;
1580
1581 case 's':
1582 RtsFlags.TraceFlags.scheduler = enabled;
1583 enabled = rtsTrue;
1584 break;
1585 case 'p':
1586 RtsFlags.TraceFlags.sparks_sampled = enabled;
1587 enabled = rtsTrue;
1588 break;
1589 case 'f':
1590 RtsFlags.TraceFlags.sparks_full = enabled;
1591 enabled = rtsTrue;
1592 break;
1593 case 't':
1594 RtsFlags.TraceFlags.timestamp = enabled;
1595 enabled = rtsTrue;
1596 break;
1597 case 'g':
1598 RtsFlags.TraceFlags.gc = enabled;
1599 enabled = rtsTrue;
1600 break;
1601 case 'u':
1602 RtsFlags.TraceFlags.user = enabled;
1603 enabled = rtsTrue;
1604 break;
1605 default:
1606 errorBelch("unknown trace option: %c",*c);
1607 break;
1608 }
1609 }
1610 }
1611 #endif
1612
1613 static void GNU_ATTRIBUTE(__noreturn__)
1614 bad_option(const char *s)
1615 {
1616 errorBelch("bad RTS option: %s", s);
1617 stg_exit(EXIT_FAILURE);
1618 }
1619
1620 /* ----------------------------------------------------------------------------
1621 Copying and freeing argc/argv
1622 ------------------------------------------------------------------------- */
1623
1624 static char * copyArg(char *arg)
1625 {
1626 char *new_arg = stgMallocBytes(strlen(arg) + 1, "copyArg");
1627 strcpy(new_arg, arg);
1628 return new_arg;
1629 }
1630
1631 static char ** copyArgv(int argc, char *argv[])
1632 {
1633 int i;
1634 char **new_argv;
1635
1636 new_argv = stgCallocBytes(argc + 1, sizeof (char *), "copyArgv 1");
1637 for (i = 0; i < argc; i++) {
1638 new_argv[i] = copyArg(argv[i]);
1639 }
1640 new_argv[argc] = NULL;
1641 return new_argv;
1642 }
1643
1644 static void freeArgv(int argc, char *argv[])
1645 {
1646 int i;
1647 if (argv != NULL) {
1648 for (i = 0; i < argc; i++) {
1649 stgFree(argv[i]);
1650 }
1651 stgFree(argv);
1652 }
1653 }
1654
1655 /* -----------------------------------------------------------------------------
1656 Getting/Setting the program's arguments.
1657
1658 These are used by System.Environment, and parts of the RTS.
1659 -------------------------------------------------------------------------- */
1660
1661 void
1662 setProgName(char *argv[])
1663 {
1664 /* Remove directory from argv[0] -- default files in current directory */
1665 #if !defined(mingw32_HOST_OS)
1666 char *last_slash;
1667 if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
1668 prog_name = last_slash+1;
1669 } else {
1670 prog_name = argv[0];
1671 }
1672 #else
1673 char* last_slash = argv[0] + (strlen(argv[0]) - 1);
1674 while ( last_slash > argv[0] ) {
1675 if ( *last_slash == '/' || *last_slash == '\\' ) {
1676 prog_name = last_slash+1;
1677 return;
1678 }
1679 last_slash--;
1680 }
1681 prog_name = argv[0];
1682 #endif
1683 }
1684
1685 void
1686 getProgArgv(int *argc, char **argv[])
1687 {
1688 if (argc) { *argc = prog_argc; }
1689 if (argv) { *argv = prog_argv; }
1690 }
1691
1692 void
1693 setProgArgv(int argc, char *argv[])
1694 {
1695 prog_argc = argc;
1696 prog_argv = copyArgv(argc,argv);
1697 setProgName(prog_argv);
1698 }
1699
1700 static void
1701 freeProgArgv(void)
1702 {
1703 freeArgv(prog_argc,prog_argv);
1704 prog_argc = 0;
1705 prog_argv = NULL;
1706 }
1707
1708 /* ----------------------------------------------------------------------------
1709 The full argv - a copy of the original program's argc/argv
1710 ------------------------------------------------------------------------- */
1711
1712 void
1713 setFullProgArgv(int argc, char *argv[])
1714 {
1715 full_prog_argc = argc;
1716 full_prog_argv = copyArgv(argc,argv);
1717 }
1718
1719 /* These functions record and recall the full arguments, including the
1720 +RTS ... -RTS options. The reason for adding them was so that the
1721 ghc-inplace program can pass /all/ the arguments on to the real ghc. */
1722 void
1723 getFullProgArgv(int *argc, char **argv[])
1724 {
1725 if (argc) { *argc = full_prog_argc; }
1726 if (argv) { *argv = full_prog_argv; }
1727 }
1728
1729 void
1730 freeFullProgArgv (void)
1731 {
1732 freeArgv(full_prog_argc, full_prog_argv);
1733 full_prog_argc = 0;
1734 full_prog_argv = NULL;
1735 }
1736
1737 /* ----------------------------------------------------------------------------
1738 The Win32 argv
1739 ------------------------------------------------------------------------- */
1740
1741 #if defined(mingw32_HOST_OS)
1742 void freeWin32ProgArgv (void);
1743
1744 void
1745 freeWin32ProgArgv (void)
1746 {
1747 int i;
1748
1749 if (win32_prog_argv != NULL) {
1750 for (i = 0; i < win32_prog_argc; i++) {
1751 stgFree(win32_prog_argv[i]);
1752 }
1753 stgFree(win32_prog_argv);
1754 }
1755
1756 win32_prog_argc = 0;
1757 win32_prog_argv = NULL;
1758 }
1759
1760 void
1761 getWin32ProgArgv(int *argc, wchar_t **argv[])
1762 {
1763 *argc = win32_prog_argc;
1764 *argv = win32_prog_argv;
1765 }
1766
1767 void
1768 setWin32ProgArgv(int argc, wchar_t *argv[])
1769 {
1770 int i;
1771
1772 freeWin32ProgArgv();
1773
1774 win32_prog_argc = argc;
1775 if (argv == NULL) {
1776 win32_prog_argv = NULL;
1777 return;
1778 }
1779
1780 win32_prog_argv = stgCallocBytes(argc + 1, sizeof (wchar_t *),
1781 "setWin32ProgArgv 1");
1782 for (i = 0; i < argc; i++) {
1783 win32_prog_argv[i] = stgMallocBytes((wcslen(argv[i]) + 1) * sizeof(wchar_t),
1784 "setWin32ProgArgv 2");
1785 wcscpy(win32_prog_argv[i], argv[i]);
1786 }
1787 win32_prog_argv[argc] = NULL;
1788 }
1789 #endif
1790
1791 /* ----------------------------------------------------------------------------
1792 The RTS argv
1793 ------------------------------------------------------------------------- */
1794
1795 static void
1796 freeRtsArgv(void)
1797 {
1798 freeArgv(rts_argc,rts_argv);
1799 rts_argc = 0;
1800 rts_argv = NULL;
1801 }
1802
1803 /* ----------------------------------------------------------------------------
1804 All argvs
1805 ------------------------------------------------------------------------- */
1806
1807 void freeRtsArgs(void)
1808 {
1809 #if defined(mingw32_HOST_OS)
1810 freeWin32ProgArgv();
1811 #endif
1812 freeFullProgArgv();
1813 freeProgArgv();
1814 freeRtsArgv();
1815 }