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