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