1204c593565b19f522ba14c6c1df6e2c561c6610
[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
16 #ifdef HAVE_CTYPE_H
17 #include <ctype.h>
18 #endif
19
20 #include <string.h>
21
22 // Flag Structure
23 RTS_FLAGS RtsFlags;
24
25 /*
26 * Split argument lists
27 */
28 int prog_argc = 0; /* an "int" so as to match normal "argc" */
29 char **prog_argv = NULL;
30 int full_prog_argc = 0; /* an "int" so as to match normal "argc" */
31 char **full_prog_argv = NULL;
32 char *prog_name = NULL; /* 'basename' of prog_argv[0] */
33 int rts_argc = 0; /* ditto */
34 char *rts_argv[MAX_RTS_ARGS];
35
36 /*
37 * constants, used later
38 */
39 #define RTS 1
40 #define PGM 0
41
42 /* -----------------------------------------------------------------------------
43 Static function decls
44 -------------------------------------------------------------------------- */
45
46 static int /* return NULL on error */
47 open_stats_file (
48 I_ arg,
49 int argc, char *argv[],
50 int rts_argc, char *rts_argv[],
51 const char *FILENAME_FMT,
52 FILE **file_ret);
53
54 static I_ decode(const char *s);
55 static void bad_option(const char *s);
56
57 /* -----------------------------------------------------------------------------
58 * Command-line option parsing routines.
59 * ---------------------------------------------------------------------------*/
60
61 void initRtsFlagsDefaults(void)
62 {
63 RtsFlags.GcFlags.statsFile = NULL;
64 RtsFlags.GcFlags.giveStats = NO_GC_STATS;
65
66 RtsFlags.GcFlags.maxStkSize = (8 * 1024 * 1024) / sizeof(W_);
67 RtsFlags.GcFlags.initialStkSize = 1024 / sizeof(W_);
68
69 RtsFlags.GcFlags.minAllocAreaSize = (512 * 1024) / BLOCK_SIZE;
70 RtsFlags.GcFlags.minOldGenSize = (1024 * 1024) / BLOCK_SIZE;
71 RtsFlags.GcFlags.maxHeapSize = 0; /* off by default */
72 RtsFlags.GcFlags.heapSizeSuggestion = 0; /* none */
73 RtsFlags.GcFlags.pcFreeHeap = 3; /* 3% */
74 RtsFlags.GcFlags.oldGenFactor = 2;
75 RtsFlags.GcFlags.generations = 2;
76 RtsFlags.GcFlags.steps = 2;
77 RtsFlags.GcFlags.squeezeUpdFrames = rtsTrue;
78 RtsFlags.GcFlags.compact = rtsFalse;
79 RtsFlags.GcFlags.compactThreshold = 30.0;
80 RtsFlags.GcFlags.sweep = rtsFalse;
81 #ifdef RTS_GTK_FRONTPANEL
82 RtsFlags.GcFlags.frontpanel = rtsFalse;
83 #endif
84 RtsFlags.GcFlags.idleGCDelayTime = 300; /* millisecs */
85
86 #if osf3_HOST_OS
87 /* ToDo: Perhaps by adjusting this value we can make linking without
88 * -static work (i.e., not generate a core-dumping executable)? */
89 # if SIZEOF_VOID_P == 8
90 RtsFlags.GcFlags.heapBase = 0x180000000L;
91 # else
92 # error I have no idea where to begin the heap on a non-64-bit osf3 machine.
93 # endif
94 #else
95 RtsFlags.GcFlags.heapBase = 0; /* means don't care */
96 #endif
97
98 #ifdef DEBUG
99 RtsFlags.DebugFlags.scheduler = rtsFalse;
100 RtsFlags.DebugFlags.interpreter = rtsFalse;
101 RtsFlags.DebugFlags.weak = rtsFalse;
102 RtsFlags.DebugFlags.gccafs = rtsFalse;
103 RtsFlags.DebugFlags.gc = rtsFalse;
104 RtsFlags.DebugFlags.block_alloc = rtsFalse;
105 RtsFlags.DebugFlags.sanity = rtsFalse;
106 RtsFlags.DebugFlags.stable = rtsFalse;
107 RtsFlags.DebugFlags.stm = rtsFalse;
108 RtsFlags.DebugFlags.prof = rtsFalse;
109 RtsFlags.DebugFlags.eventlog = rtsFalse;
110 RtsFlags.DebugFlags.apply = rtsFalse;
111 RtsFlags.DebugFlags.linker = rtsFalse;
112 RtsFlags.DebugFlags.squeeze = rtsFalse;
113 RtsFlags.DebugFlags.hpc = rtsFalse;
114 RtsFlags.DebugFlags.timestamp = rtsFalse;
115 #endif
116
117 #if defined(PROFILING)
118 RtsFlags.CcFlags.doCostCentres = 0;
119 #endif /* PROFILING */
120
121 RtsFlags.ProfFlags.doHeapProfile = rtsFalse;
122 RtsFlags.ProfFlags.profileInterval = 100;
123
124 #ifdef PROFILING
125 RtsFlags.ProfFlags.includeTSOs = rtsFalse;
126 RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
127 RtsFlags.ProfFlags.maxRetainerSetSize = 8;
128 RtsFlags.ProfFlags.ccsLength = 25;
129 RtsFlags.ProfFlags.modSelector = NULL;
130 RtsFlags.ProfFlags.descrSelector = NULL;
131 RtsFlags.ProfFlags.typeSelector = NULL;
132 RtsFlags.ProfFlags.ccSelector = NULL;
133 RtsFlags.ProfFlags.ccsSelector = NULL;
134 RtsFlags.ProfFlags.retainerSelector = NULL;
135 RtsFlags.ProfFlags.bioSelector = NULL;
136 #endif
137
138 #ifdef EVENTLOG
139 RtsFlags.EventLogFlags.doEventLogging = rtsFalse;
140 #endif
141
142 RtsFlags.MiscFlags.tickInterval = 20; /* In milliseconds */
143 RtsFlags.ConcFlags.ctxtSwitchTime = 20; /* In milliseconds */
144
145 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
146 RtsFlags.MiscFlags.machineReadable = rtsFalse;
147 RtsFlags.MiscFlags.linkerMemBase = 0;
148
149 #ifdef THREADED_RTS
150 RtsFlags.ParFlags.nNodes = 1;
151 RtsFlags.ParFlags.migrate = rtsTrue;
152 RtsFlags.ParFlags.wakeupMigrate = rtsFalse;
153 RtsFlags.ParFlags.parGcEnabled = 1;
154 RtsFlags.ParFlags.parGcGen = 1;
155 RtsFlags.ParFlags.parGcLoadBalancing = 1;
156 RtsFlags.ParFlags.setAffinity = 0;
157 #endif
158
159 #if defined(THREADED_RTS)
160 RtsFlags.ParFlags.maxLocalSparks = 4096;
161 #endif /* THREADED_RTS */
162
163 #ifdef TICKY_TICKY
164 RtsFlags.TickyFlags.showTickyStats = rtsFalse;
165 RtsFlags.TickyFlags.tickyFile = NULL;
166 #endif
167
168 #ifdef USE_PAPI
169 /* By default no special measurements taken */
170 RtsFlags.PapiFlags.eventType = 0;
171 RtsFlags.PapiFlags.numUserEvents = 0;
172 #endif
173 }
174
175 static const char *
176 usage_text[] = {
177 "",
178 "Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
179 "",
180 " +RTS Indicates run time system options follow",
181 " -RTS Indicates program arguments follow",
182 " --RTS Indicates that ALL subsequent arguments will be given to the",
183 " program (including any of these RTS flags)",
184 "",
185 "The following run time system options are available:",
186 "",
187 " -? Prints this message and exits; the program is not executed",
188 " --info Print information about the RTS used by this program",
189 "",
190 " -K<size> Sets the maximum stack size (default 8M) Egs: -K32k -K512k",
191 " -k<size> Sets the initial thread stack size (default 1k) Egs: -k4k -k2m",
192 "",
193 " -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
194 " -M<size> Sets the maximum heap size (default unlimited) Egs: -M256k -M1G",
195 " -H<size> Sets the minimum heap size (default 0M) Egs: -H24m -H1G",
196 " -m<n> Minimum % of heap which must be available (default 3%)",
197 " -G<n> Number of generations (default: 2)",
198 " -T<n> Number of steps in younger generations (default: 2)",
199 " -c<n> Auto-enable compaction of the oldest generation when live data is",
200 " at least <n>% of the maximum heap size set with -M (default: 30%)",
201 " -c Enable compaction for all major collections",
202 " -w Use mark-region for the oldest generation (experimental)",
203 #if defined(THREADED_RTS)
204 " -I<sec> Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
205 #endif
206 "",
207 " -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
208 " -s[<file>] Summary GC statistics (if <file> omitted, uses stderr)",
209 " -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
210 #ifdef RTS_GTK_FRONTPANEL
211 " -f Display front panel (requires X11 & GTK+)",
212 #endif
213 "",
214 "",
215 " -Z Don't squeeze out update frames on stack overflow",
216 " -B Sound the bell at the start of each garbage collection",
217 #if defined(PROFILING)
218 "",
219 " -px Time/allocation profile (XML) (output file <program>.prof)",
220 " -p Time/allocation profile (output file <program>.prof)",
221 " -P More detailed Time/Allocation profile",
222 " -Pa Give information about *all* cost centres",
223
224 # if defined(PROFILING)
225 "",
226 " -hx Heap residency profile (XML) (output file <program>.prof)",
227 " -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
228 " break-down: c = cost centre stack (default)",
229 " m = module",
230 " d = closure description",
231 " y = type description",
232 " r = retainer",
233 " b = biography (LAG,DRAG,VOID,USE)",
234 " A subset of closures may be selected thusly:",
235 " -hc<cc>,... specific cost centre(s) (top of stack only)",
236 " -hC<cc>,... specific cost centre(s) (anywhere in stack)",
237 " -hm<mod>... all cost centres from the specified modules(s)",
238 " -hd<des>,... closures with specified closure descriptions",
239 " -hy<typ>... closures with specified type descriptions",
240 " -hr<cc>... closures with specified retainers",
241 " -hb<bio>... closures with specified biographies (lag,drag,void,use)",
242 "",
243 " -R<size> Set the maximum retainer set size (default: 8)",
244 "",
245 " -L<chars> Maximum length of a cost-centre stack in a heap profile",
246 " (default: 25)",
247 "",
248 " -xt Include threads (TSOs) in a heap profile",
249 "",
250 " -xc Show current cost centre stack on raising an exception",
251 "",
252 # endif
253 #endif /* PROFILING or PAR */
254
255 #ifdef EVENTLOG
256 "",
257 " -l Log runtime events (generates binary trace file <program>.eventlog)",
258 "",
259 #endif
260
261 #if !defined(PROFILING)
262 "",
263 " -hT Heap residency profile (output file <program>.hp)",
264 #endif
265 " -i<sec> Time between heap samples (seconds, default: 0.1)",
266 "",
267 #if defined(TICKY_TICKY)
268 " -r<file> Produce ticky-ticky statistics (with -rstderr for stderr)",
269 "",
270 #endif
271 " -C<secs> Context-switch interval in seconds.",
272 " 0 or no argument means switch as often as possible.",
273 " Default: 0.02 sec; resolution is set by -V below.",
274 " -V<secs> Master tick interval in seconds (0 == disable timer).",
275 " This sets the resolution for -C and the profile timer -i.",
276 " Default: 0.02 sec.",
277 "",
278 " -vt Time-stamp debug messages",
279 "",
280 #if defined(DEBUG)
281 " -Ds DEBUG: scheduler",
282 " -Di DEBUG: interpreter",
283 " -Dw DEBUG: weak",
284 " -DG DEBUG: gccafs",
285 " -Dg DEBUG: gc",
286 " -Db DEBUG: block",
287 " -DS DEBUG: sanity",
288 " -Dt DEBUG: stable",
289 " -Dp DEBUG: prof",
290 " -De DEBUG: event logging",
291 " -Da DEBUG: apply",
292 " -Dl DEBUG: linker",
293 " -Dm DEBUG: stm",
294 " -Dz DEBUG: stack squezing",
295 " -Dc DEBUG: program coverage",
296 "",
297 #endif /* DEBUG */
298 #if defined(THREADED_RTS) && !defined(NOSMP)
299 " -N<n> Use <n> processors (default: 1)",
300 " -N Determine the number of processors to use automatically",
301 " -q1 Use one OS thread for GC (turns off parallel GC)",
302 " -qg<n> Use parallel GC only for generations >= <n> (default: 1)",
303 " -qb Disable load-balancing in the parallel GC",
304 " -qa Use the OS to set thread affinity",
305 " -qm Don't automatically migrate threads between CPUs",
306 " -qw Migrate a thread to the current CPU when it is woken up",
307 #endif
308 " --install-signal-handlers=<yes|no>",
309 " Install signal handlers (default: yes)",
310 #if defined(THREADED_RTS)
311 " -e<size> Size of spark pools (default 100)",
312 #endif
313 #if defined(THREADED_RTS)
314 " -e<n> Maximum number of outstanding local sparks (default: 4096)",
315 #endif
316 #if defined(x86_64_HOST_ARCH)
317 " -xm Base address to mmap memory in the GHCi linker",
318 " (hex; must be <80000000)",
319 #endif
320 #if defined(USE_PAPI)
321 " -aX CPU performance counter measurements using PAPI",
322 " (use with the -s<file> option). X is one of:",
323 "",
324 /* " y - cycles", */
325 " 1 - level 1 cache misses",
326 " 2 - level 2 cache misses",
327 " b - branch mispredictions",
328 " s - stalled cycles",
329 " e - cache miss and branch misprediction events",
330 #endif
331 "",
332 "RTS options may also be specified using the GHCRTS environment variable.",
333 "",
334 "Other RTS options may be available for programs compiled a different way.",
335 "The GHC User's Guide has full details.",
336 "",
337 0
338 };
339
340 STATIC_INLINE rtsBool
341 strequal(const char *a, const char * b)
342 {
343 return(strcmp(a, b) == 0);
344 }
345
346 static void
347 splitRtsFlags(char *s, int *rts_argc, char *rts_argv[])
348 {
349 char *c1, *c2;
350
351 c1 = s;
352 do {
353 while (isspace(*c1)) { c1++; };
354 c2 = c1;
355 while (!isspace(*c2) && *c2 != '\0') { c2++; };
356
357 if (c1 == c2) { break; }
358
359 if (*rts_argc < MAX_RTS_ARGS-1) {
360 s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
361 strncpy(s, c1, c2-c1);
362 s[c2-c1] = '\0';
363 rts_argv[(*rts_argc)++] = s;
364 } else {
365 barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
366 }
367
368 c1 = c2;
369 } while (*c1 != '\0');
370 }
371
372 void
373 setupRtsFlags(int *argc, char *argv[], int *rts_argc, char *rts_argv[])
374 {
375 rtsBool error = rtsFalse;
376 I_ mode;
377 I_ arg, total_arg;
378
379 setProgName (argv);
380 total_arg = *argc;
381 arg = 1;
382
383 *argc = 1;
384 *rts_argc = 0;
385
386 // process arguments from the ghc_rts_opts global variable first.
387 // (arguments from the GHCRTS environment variable and the command
388 // line override these).
389 {
390 if (ghc_rts_opts != NULL) {
391 splitRtsFlags(ghc_rts_opts, rts_argc, rts_argv);
392 }
393 }
394
395 // process arguments from the GHCRTS environment variable next
396 // (arguments from the command line override these).
397 {
398 char *ghc_rts = getenv("GHCRTS");
399
400 if (ghc_rts != NULL) {
401 splitRtsFlags(ghc_rts, rts_argc, rts_argv);
402 }
403 }
404
405 // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
406 // argv[0] must be PGM argument -- leave in argv
407
408 for (mode = PGM; arg < total_arg; arg++) {
409 // The '--RTS' argument disables all future +RTS ... -RTS processing.
410 if (strequal("--RTS", argv[arg])) {
411 arg++;
412 break;
413 }
414 // The '--' argument is passed through to the program, but
415 // disables all further +RTS ... -RTS processing.
416 else if (strequal("--", argv[arg])) {
417 break;
418 }
419 else if (strequal("+RTS", argv[arg])) {
420 mode = RTS;
421 }
422 else if (strequal("-RTS", argv[arg])) {
423 mode = PGM;
424 }
425 else if (mode == RTS && *rts_argc < MAX_RTS_ARGS-1) {
426 rts_argv[(*rts_argc)++] = argv[arg];
427 }
428 else if (mode == PGM) {
429 argv[(*argc)++] = argv[arg];
430 }
431 else {
432 barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
433 }
434 }
435 // process remaining program arguments
436 for (; arg < total_arg; arg++) {
437 argv[(*argc)++] = argv[arg];
438 }
439 argv[*argc] = (char *) 0;
440 rts_argv[*rts_argc] = (char *) 0;
441
442 // Process RTS (rts_argv) part: mainly to determine statsfile
443 for (arg = 0; arg < *rts_argc; arg++) {
444 if (rts_argv[arg][0] != '-') {
445 fflush(stdout);
446 errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
447 error = rtsTrue;
448
449 } else {
450 switch(rts_argv[arg][1]) {
451
452 /* process: general args, then PROFILING-only ones, then
453 CONCURRENT-only, TICKY-only (same order as defined in
454 RtsFlags.lh); within those groups, mostly in
455 case-insensitive alphabetical order. Final group is
456 x*, which allows for more options.
457 */
458
459 #ifdef TICKY_TICKY
460 # define TICKY_BUILD_ONLY(x) x
461 #else
462 # define TICKY_BUILD_ONLY(x) \
463 errorBelch("not built for: ticky-ticky stats"); \
464 error = rtsTrue;
465 #endif
466
467 #ifdef PROFILING
468 # define PROFILING_BUILD_ONLY(x) x
469 #else
470 # define PROFILING_BUILD_ONLY(x) \
471 errorBelch("not built for: -prof"); \
472 error = rtsTrue;
473 #endif
474
475 #ifdef EVENTLOG
476 # define EVENTLOG_BUILD_ONLY(x) x
477 #else
478 # define EVENTLOG_BUILD_ONLY(x) \
479 errorBelch("not built for: -par-prof"); \
480 error = rtsTrue;
481 #endif
482
483 #ifdef THREADED_RTS
484 # define THREADED_BUILD_ONLY(x) x
485 #else
486 # define THREADED_BUILD_ONLY(x) \
487 errorBelch("not built for: -smp"); \
488 error = rtsTrue;
489 #endif
490
491 /* =========== GENERAL ========================== */
492 case '?':
493 error = rtsTrue;
494 break;
495
496 /* This isn't going to allow us to keep related options
497 together as we add more --* flags. We really need a
498 proper options parser. */
499 case '-':
500 if (strequal("install-signal-handlers=yes",
501 &rts_argv[arg][2])) {
502 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
503 }
504 else if (strequal("install-signal-handlers=no",
505 &rts_argv[arg][2])) {
506 RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
507 }
508 else if (strequal("machine-readable",
509 &rts_argv[arg][2])) {
510 RtsFlags.MiscFlags.machineReadable = rtsTrue;
511 }
512 else if (strequal("info",
513 &rts_argv[arg][2])) {
514 printRtsInfo();
515 exit(0);
516 }
517 else {
518 errorBelch("unknown RTS option: %s",rts_argv[arg]);
519 error = rtsTrue;
520 }
521 break;
522 case 'A':
523 RtsFlags.GcFlags.minAllocAreaSize
524 = decode(rts_argv[arg]+2) / BLOCK_SIZE;
525 if (RtsFlags.GcFlags.minAllocAreaSize <= 0) {
526 bad_option(rts_argv[arg]);
527 }
528 break;
529
530 #ifdef USE_PAPI
531 case 'a':
532 switch(rts_argv[arg][2]) {
533 case '1':
534 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
535 break;
536 case '2':
537 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
538 break;
539 case 'b':
540 RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
541 break;
542 case 's':
543 RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
544 break;
545 case 'e':
546 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
547 break;
548 case '+':
549 if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
550 errorBelch("maximum number of PAPI events reached");
551 stg_exit(EXIT_FAILURE);
552 }
553 RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
554 RtsFlags.PapiFlags.userEvents[RtsFlags.PapiFlags.numUserEvents++] = rts_argv[arg] + 3;
555 break;
556 default:
557 bad_option( rts_argv[arg] );
558 }
559 break;
560 #endif
561
562 case 'B':
563 RtsFlags.GcFlags.ringBell = rtsTrue;
564 break;
565
566 case 'c':
567 if (rts_argv[arg][2] != '\0') {
568 RtsFlags.GcFlags.compactThreshold =
569 atof(rts_argv[arg]+2);
570 } else {
571 RtsFlags.GcFlags.compact = rtsTrue;
572 }
573 break;
574
575 case 'w':
576 RtsFlags.GcFlags.sweep = rtsTrue;
577 break;
578
579 case 'F':
580 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
581
582 if (RtsFlags.GcFlags.oldGenFactor < 0)
583 bad_option( rts_argv[arg] );
584 break;
585
586 #ifdef DEBUG
587 case 'D':
588 {
589 char *c;
590
591 for (c = rts_argv[arg] + 2; *c != '\0'; c++) {
592 switch (*c) {
593 case 's':
594 RtsFlags.DebugFlags.scheduler = rtsTrue;
595 break;
596 case 'i':
597 RtsFlags.DebugFlags.interpreter = rtsTrue;
598 break;
599 case 'w':
600 RtsFlags.DebugFlags.weak = rtsTrue;
601 break;
602 case 'G':
603 RtsFlags.DebugFlags.gccafs = rtsTrue;
604 break;
605 case 'g':
606 RtsFlags.DebugFlags.gc = rtsTrue;
607 break;
608 case 'b':
609 RtsFlags.DebugFlags.block_alloc = rtsTrue;
610 break;
611 case 'S':
612 RtsFlags.DebugFlags.sanity = rtsTrue;
613 break;
614 case 't':
615 RtsFlags.DebugFlags.stable = rtsTrue;
616 break;
617 case 'p':
618 RtsFlags.DebugFlags.prof = rtsTrue;
619 break;
620 case 'e':
621 RtsFlags.DebugFlags.eventlog = rtsTrue;
622 break;
623 case 'l':
624 RtsFlags.DebugFlags.linker = rtsTrue;
625 break;
626 case 'a':
627 RtsFlags.DebugFlags.apply = rtsTrue;
628 break;
629 case 'm':
630 RtsFlags.DebugFlags.stm = rtsTrue;
631 break;
632 case 'z':
633 RtsFlags.DebugFlags.squeeze = rtsTrue;
634 break;
635 case 'c':
636 RtsFlags.DebugFlags.hpc = rtsTrue;
637 break;
638 default:
639 bad_option( rts_argv[arg] );
640 }
641 }
642 break;
643 }
644 #endif
645
646 case 'K':
647 RtsFlags.GcFlags.maxStkSize =
648 decode(rts_argv[arg]+2) / sizeof(W_);
649
650 if (RtsFlags.GcFlags.maxStkSize == 0)
651 bad_option( rts_argv[arg] );
652 break;
653
654 case 'k':
655 RtsFlags.GcFlags.initialStkSize =
656 decode(rts_argv[arg]+2) / sizeof(W_);
657
658 if (RtsFlags.GcFlags.initialStkSize == 0)
659 bad_option( rts_argv[arg] );
660 break;
661
662 case 'M':
663 RtsFlags.GcFlags.maxHeapSize =
664 decode(rts_argv[arg]+2) / BLOCK_SIZE;
665 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
666
667 if (RtsFlags.GcFlags.maxHeapSize <= 0) {
668 bad_option(rts_argv[arg]);
669 }
670 break;
671
672 case 'm':
673 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
674
675 if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
676 RtsFlags.GcFlags.pcFreeHeap > 100)
677 bad_option( rts_argv[arg] );
678 break;
679
680 case 'G':
681 RtsFlags.GcFlags.generations = decode(rts_argv[arg]+2);
682 if (RtsFlags.GcFlags.generations < 1) {
683 bad_option(rts_argv[arg]);
684 }
685 break;
686
687 case 'T':
688 RtsFlags.GcFlags.steps = decode(rts_argv[arg]+2);
689 if (RtsFlags.GcFlags.steps < 1) {
690 bad_option(rts_argv[arg]);
691 }
692 break;
693
694 case 'H':
695 RtsFlags.GcFlags.heapSizeSuggestion =
696 decode(rts_argv[arg]+2) / BLOCK_SIZE;
697 break;
698
699 #ifdef RTS_GTK_FRONTPANEL
700 case 'f':
701 RtsFlags.GcFlags.frontpanel = rtsTrue;
702 break;
703 #endif
704
705 case 'I': /* idle GC delay */
706 if (rts_argv[arg][2] == '\0') {
707 /* use default */
708 } else {
709 I_ cst; /* tmp */
710
711 /* Convert to millisecs */
712 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
713 RtsFlags.GcFlags.idleGCDelayTime = cst;
714 }
715 break;
716
717 case 'S':
718 RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
719 goto stats;
720
721 case 's':
722 RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
723 goto stats;
724
725 case 't':
726 RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
727 goto stats;
728
729 stats:
730 {
731 int r;
732 r = open_stats_file(arg, *argc, argv,
733 *rts_argc, rts_argv, NULL,
734 &RtsFlags.GcFlags.statsFile);
735 if (r == -1) { error = rtsTrue; }
736 }
737 break;
738
739 case 'Z':
740 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
741 break;
742
743 /* =========== PROFILING ========================== */
744
745 case 'l':
746 #ifdef EVENTLOG
747 RtsFlags.EventLogFlags.doEventLogging = rtsTrue;
748 #else
749 errorBelch("not built for: -eventlog");
750 #endif
751 break;
752
753 case 'P': /* detailed cost centre profiling (time/alloc) */
754 case 'p': /* cost centre profiling (time/alloc) */
755 PROFILING_BUILD_ONLY(
756 switch (rts_argv[arg][2]) {
757 case 'x':
758 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
759 break;
760 case 'a':
761 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
762 break;
763 default:
764 if (rts_argv[arg][1] == 'P') {
765 RtsFlags.CcFlags.doCostCentres =
766 COST_CENTRES_VERBOSE;
767 } else {
768 RtsFlags.CcFlags.doCostCentres =
769 COST_CENTRES_SUMMARY;
770 }
771 break;
772 }
773 ) break;
774
775 case 'R':
776 PROFILING_BUILD_ONLY(
777 RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
778 ) break;
779 case 'L':
780 PROFILING_BUILD_ONLY(
781 RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
782 if(RtsFlags.ProfFlags.ccsLength <= 0) {
783 bad_option(rts_argv[arg]);
784 }
785 ) break;
786 case 'h': /* serial heap profile */
787 #if !defined(PROFILING)
788 switch (rts_argv[arg][2]) {
789 case '\0':
790 case 'T':
791 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
792 break;
793 default:
794 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
795 error = rtsTrue;
796 }
797 #else
798 PROFILING_BUILD_ONLY(
799 switch (rts_argv[arg][2]) {
800 case '\0':
801 case 'C':
802 case 'c':
803 case 'M':
804 case 'm':
805 case 'D':
806 case 'd':
807 case 'Y':
808 case 'y':
809 case 'R':
810 case 'r':
811 case 'B':
812 case 'b':
813 if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
814 {
815 char *left = strchr(rts_argv[arg], '{');
816 char *right = strrchr(rts_argv[arg], '}');
817
818 // curly braces are optional, for
819 // backwards compat.
820 if (left)
821 left = left+1;
822 else
823 left = rts_argv[arg] + 3;
824
825 if (!right)
826 right = rts_argv[arg] + strlen(rts_argv[arg]);
827
828 *right = '\0';
829
830 switch (rts_argv[arg][2]) {
831 case 'c': // cost centre label select
832 RtsFlags.ProfFlags.ccSelector = left;
833 break;
834 case 'C':
835 RtsFlags.ProfFlags.ccsSelector = left;
836 break;
837 case 'M':
838 case 'm': // cost centre module select
839 RtsFlags.ProfFlags.modSelector = left;
840 break;
841 case 'D':
842 case 'd': // closure descr select
843 RtsFlags.ProfFlags.descrSelector = left;
844 break;
845 case 'Y':
846 case 'y': // closure type select
847 RtsFlags.ProfFlags.typeSelector = left;
848 break;
849 case 'R':
850 case 'r': // retainer select
851 RtsFlags.ProfFlags.retainerSelector = left;
852 break;
853 case 'B':
854 case 'b': // biography select
855 RtsFlags.ProfFlags.bioSelector = left;
856 break;
857 }
858 }
859 break;
860 }
861
862 if (RtsFlags.ProfFlags.doHeapProfile != 0) {
863 errorBelch("multiple heap profile options");
864 error = rtsTrue;
865 break;
866 }
867
868 switch (rts_argv[arg][2]) {
869 case '\0':
870 case 'C':
871 case 'c':
872 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
873 break;
874 case 'M':
875 case 'm':
876 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
877 break;
878 case 'D':
879 case 'd':
880 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
881 break;
882 case 'Y':
883 case 'y':
884 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
885 break;
886 case 'R':
887 case 'r':
888 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
889 break;
890 case 'B':
891 case 'b':
892 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
893 break;
894 }
895 break;
896
897 default:
898 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
899 error = rtsTrue;
900 }
901 )
902 #endif /* PROFILING */
903 break;
904
905 case 'i': /* heap sample interval */
906 if (rts_argv[arg][2] == '\0') {
907 /* use default */
908 } else {
909 I_ cst; /* tmp */
910
911 /* Convert to milliseconds */
912 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
913 RtsFlags.ProfFlags.profileInterval = cst;
914 }
915 break;
916
917 /* =========== CONCURRENT ========================= */
918 case 'C': /* context switch interval */
919 if (rts_argv[arg][2] == '\0')
920 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
921 else {
922 I_ cst; /* tmp */
923
924 /* Convert to milliseconds */
925 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
926 RtsFlags.ConcFlags.ctxtSwitchTime = cst;
927 }
928 break;
929
930 case 'V': /* master tick interval */
931 if (rts_argv[arg][2] == '\0') {
932 // turns off ticks completely
933 RtsFlags.MiscFlags.tickInterval = 0;
934 } else {
935 I_ cst; /* tmp */
936
937 /* Convert to milliseconds */
938 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
939 RtsFlags.MiscFlags.tickInterval = cst;
940 }
941 break;
942
943 #if defined(THREADED_RTS) && !defined(NOSMP)
944 case 'N':
945 THREADED_BUILD_ONLY(
946 if (rts_argv[arg][2] == '\0') {
947 #if defined(PROFILING)
948 RtsFlags.ParFlags.nNodes = 1;
949 #else
950 RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
951 #endif
952 } else {
953 RtsFlags.ParFlags.nNodes
954 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
955 if (RtsFlags.ParFlags.nNodes <= 0) {
956 errorBelch("bad value for -N");
957 error = rtsTrue;
958 }
959 #if defined(PROFILING)
960 if (RtsFlags.ParFlags.nNodes > 1) {
961 errorBelch("bad option %s: only -N1 is supported with profiling", rts_argv[arg]);
962 error = rtsTrue;
963 }
964 #endif
965 }
966 ) break;
967
968 case 'g':
969 THREADED_BUILD_ONLY(
970 switch (rts_argv[arg][2]) {
971 case '1':
972 // backwards compat only
973 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
974 break;
975 default:
976 errorBelch("unknown RTS option: %s",rts_argv[arg]);
977 error = rtsTrue;
978 break;
979 }
980 ) break;
981
982 case 'q':
983 switch (rts_argv[arg][2]) {
984 case '\0':
985 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
986 error = rtsTrue;
987 break;
988 case '1':
989 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
990 break;
991 case 'g':
992 if (rts_argv[arg][3] != '\0') {
993 RtsFlags.ParFlags.parGcGen
994 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
995 } else {
996 errorBelch("bad value for -qg");
997 error = rtsTrue;
998 }
999 break;
1000 case 'b':
1001 RtsFlags.ParFlags.parGcLoadBalancing = rtsFalse;
1002 break;
1003 case 'a':
1004 RtsFlags.ParFlags.setAffinity = rtsTrue;
1005 break;
1006 case 'm':
1007 RtsFlags.ParFlags.migrate = rtsFalse;
1008 break;
1009 case 'w':
1010 RtsFlags.ParFlags.wakeupMigrate = rtsTrue;
1011 break;
1012 default:
1013 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1014 error = rtsTrue;
1015 break;
1016 }
1017 break;
1018 #endif
1019 /* =========== PARALLEL =========================== */
1020 case 'e':
1021 THREADED_BUILD_ONLY(
1022 if (rts_argv[arg][2] != '\0') {
1023 RtsFlags.ParFlags.maxLocalSparks
1024 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1025 if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1026 errorBelch("bad value for -e");
1027 error = rtsTrue;
1028 }
1029 }
1030 ) break;
1031
1032 /* =========== TICKY ============================== */
1033
1034 case 'r': /* Basic profiling stats */
1035 TICKY_BUILD_ONLY(
1036
1037 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1038
1039 {
1040 int r;
1041 r = open_stats_file(arg, *argc, argv,
1042 *rts_argc, rts_argv, TICKY_FILENAME_FMT,
1043 &RtsFlags.TickyFlags.tickyFile);
1044 if (r == -1) { error = rtsTrue; }
1045 }
1046 ) break;
1047
1048 /* =========== TRACING ---------=================== */
1049
1050 case 'v':
1051 switch(rts_argv[arg][2]) {
1052 case '\0':
1053 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1054 error = rtsTrue;
1055 break;
1056 case 't':
1057 RtsFlags.DebugFlags.timestamp = rtsTrue;
1058 break;
1059 case 's':
1060 case 'g':
1061 // ignored for backwards-compat
1062 break;
1063 default:
1064 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1065 error = rtsTrue;
1066 break;
1067 }
1068 break;
1069
1070 /* =========== EXTENDED OPTIONS =================== */
1071
1072 case 'x': /* Extend the argument space */
1073 switch(rts_argv[arg][2]) {
1074 case '\0':
1075 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1076 error = rtsTrue;
1077 break;
1078
1079 case 'b': /* heapBase in hex; undocumented */
1080 if (rts_argv[arg][3] != '\0') {
1081 RtsFlags.GcFlags.heapBase
1082 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1083 } else {
1084 errorBelch("-xb: requires argument");
1085 error = rtsTrue;
1086 }
1087 break;
1088
1089 #if defined(x86_64_HOST_ARCH)
1090 case 'm': /* linkerMemBase */
1091 if (rts_argv[arg][3] != '\0') {
1092 RtsFlags.MiscFlags.linkerMemBase
1093 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1094 if (RtsFlags.MiscFlags.linkerMemBase > 0x80000000) {
1095 errorBelch("-xm: value must be <80000000");
1096 error = rtsTrue;
1097 }
1098 } else {
1099 RtsFlags.MiscFlags.linkerMemBase = 0;
1100 }
1101 break;
1102 #endif
1103
1104 case 'c': /* Debugging tool: show current cost centre on an exception */
1105 PROFILING_BUILD_ONLY(
1106 RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1107 );
1108 break;
1109
1110 case 't': /* Include memory used by TSOs in a heap profile */
1111 PROFILING_BUILD_ONLY(
1112 RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1113 );
1114 break;
1115
1116 /* The option prefix '-xx' is reserved for future extension. KSW 1999-11. */
1117
1118 default:
1119 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1120 error = rtsTrue;
1121 break;
1122 }
1123 break; /* defensive programming */
1124
1125 /* =========== OH DEAR ============================ */
1126 default:
1127 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1128 error = rtsTrue;
1129 break;
1130 }
1131 }
1132 }
1133
1134 if (RtsFlags.MiscFlags.tickInterval < 0) {
1135 RtsFlags.MiscFlags.tickInterval = 50;
1136 }
1137
1138 // If the master timer is disabled, turn off the other timers.
1139 if (RtsFlags.MiscFlags.tickInterval == 0) {
1140 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1141 RtsFlags.GcFlags.idleGCDelayTime = 0;
1142 RtsFlags.ProfFlags.profileInterval = 0;
1143 }
1144
1145 // Determine what tick interval we should use for the RTS timer
1146 // by taking the shortest of the various intervals that we need to
1147 // monitor.
1148 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1149 RtsFlags.MiscFlags.tickInterval =
1150 stg_min(RtsFlags.ConcFlags.ctxtSwitchTime,
1151 RtsFlags.MiscFlags.tickInterval);
1152 }
1153
1154 if (RtsFlags.GcFlags.idleGCDelayTime > 0) {
1155 RtsFlags.MiscFlags.tickInterval =
1156 stg_min(RtsFlags.GcFlags.idleGCDelayTime,
1157 RtsFlags.MiscFlags.tickInterval);
1158 }
1159
1160 if (RtsFlags.ProfFlags.profileInterval > 0) {
1161 RtsFlags.MiscFlags.tickInterval =
1162 stg_min(RtsFlags.ProfFlags.profileInterval,
1163 RtsFlags.MiscFlags.tickInterval);
1164 }
1165
1166 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1167 RtsFlags.ConcFlags.ctxtSwitchTicks =
1168 RtsFlags.ConcFlags.ctxtSwitchTime /
1169 RtsFlags.MiscFlags.tickInterval;
1170 } else {
1171 RtsFlags.ConcFlags.ctxtSwitchTicks = 0;
1172 }
1173
1174 if (RtsFlags.ProfFlags.profileInterval > 0) {
1175 RtsFlags.ProfFlags.profileIntervalTicks =
1176 RtsFlags.ProfFlags.profileInterval /
1177 RtsFlags.MiscFlags.tickInterval;
1178 } else {
1179 RtsFlags.ProfFlags.profileIntervalTicks = 0;
1180 }
1181
1182 if (error) {
1183 const char **p;
1184
1185 fflush(stdout);
1186 for (p = usage_text; *p; p++)
1187 errorBelch("%s", *p);
1188 stg_exit(EXIT_FAILURE);
1189 }
1190 }
1191
1192
1193 static void
1194 stats_fprintf(FILE *f, char *s, ...)
1195 {
1196 va_list ap;
1197 va_start(ap,s);
1198 if (f == NULL) {
1199 vdebugBelch(s, ap);
1200 } else {
1201 vfprintf(f, s, ap);
1202 }
1203 va_end(ap);
1204 }
1205
1206 static int /* return -1 on error */
1207 open_stats_file (
1208 I_ arg,
1209 int argc, char *argv[],
1210 int rts_argc, char *rts_argv[],
1211 const char *FILENAME_FMT,
1212 FILE **file_ret)
1213 {
1214 FILE *f = NULL;
1215
1216 if (strequal(rts_argv[arg]+2, "stderr")
1217 || (FILENAME_FMT == NULL && rts_argv[arg][2] == '\0')) {
1218 f = NULL; /* NULL means use debugBelch */
1219 } else {
1220 if (rts_argv[arg][2] != '\0') { /* stats file specified */
1221 f = fopen(rts_argv[arg]+2,"w");
1222 } else {
1223 char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
1224 sprintf(stats_filename, FILENAME_FMT, argv[0]);
1225 f = fopen(stats_filename,"w");
1226 }
1227 if (f == NULL) {
1228 errorBelch("Can't open stats file %s\n", rts_argv[arg]+2);
1229 return -1;
1230 }
1231 }
1232 *file_ret = f;
1233
1234 {
1235 /* Write argv and rtsv into start of stats file */
1236 int count;
1237 for(count = 0; count < argc; count++) {
1238 stats_fprintf(f, "%s ", argv[count]);
1239 }
1240 stats_fprintf(f, "+RTS ");
1241 for(count = 0; count < rts_argc; count++)
1242 stats_fprintf(f, "%s ", rts_argv[count]);
1243 stats_fprintf(f, "\n");
1244 }
1245 return 0;
1246 }
1247
1248
1249
1250 static I_
1251 decode(const char *s)
1252 {
1253 I_ c;
1254 StgDouble m;
1255
1256 if (!*s)
1257 return 0;
1258
1259 m = atof(s);
1260 c = s[strlen(s)-1];
1261
1262 if (c == 'g' || c == 'G')
1263 m *= 1000*1000*1000; /* UNchecked! */
1264 else if (c == 'm' || c == 'M')
1265 m *= 1000*1000; /* We do not use powers of 2 (1024) */
1266 else if (c == 'k' || c == 'K') /* to avoid possible bad effects on */
1267 m *= 1000; /* a direct-mapped cache. */
1268 else if (c == 'w' || c == 'W')
1269 m *= sizeof(W_);
1270
1271 return (I_)m;
1272 }
1273
1274 static void GNU_ATTRIBUTE(__noreturn__)
1275 bad_option(const char *s)
1276 {
1277 errorBelch("bad RTS option: %s", s);
1278 stg_exit(EXIT_FAILURE);
1279 }
1280
1281 /* -----------------------------------------------------------------------------
1282 Getting/Setting the program's arguments.
1283
1284 These are used by System.Environment, and parts of the RTS.
1285 -------------------------------------------------------------------------- */
1286
1287 void
1288 setProgName(char *argv[])
1289 {
1290 /* Remove directory from argv[0] -- default files in current directory */
1291 #if !defined(mingw32_HOST_OS)
1292 char *last_slash;
1293 if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
1294 prog_name = last_slash+1;
1295 } else {
1296 prog_name = argv[0];
1297 }
1298 #else
1299 char* last_slash = argv[0] + (strlen(argv[0]) - 1);
1300 while ( last_slash > argv[0] ) {
1301 if ( *last_slash == '/' || *last_slash == '\\' ) {
1302 prog_name = last_slash+1;
1303 return;
1304 }
1305 last_slash--;
1306 }
1307 prog_name = argv[0];
1308 #endif
1309 }
1310
1311 void
1312 getProgArgv(int *argc, char **argv[])
1313 {
1314 if (argc) { *argc = prog_argc; }
1315 if (argv) { *argv = prog_argv; }
1316 }
1317
1318 void
1319 setProgArgv(int argc, char *argv[])
1320 {
1321 /* Usually this is done by startupHaskell, so we don't need to call this.
1322 However, sometimes Hugs wants to change the arguments which Haskell
1323 getArgs >>= ... will be fed. So you can do that by calling here
1324 _after_ calling startupHaskell.
1325 */
1326 prog_argc = argc;
1327 prog_argv = argv;
1328 setProgName(prog_argv);
1329 }
1330
1331 /* These functions record and recall the full arguments, including the
1332 +RTS ... -RTS options. The reason for adding them was so that the
1333 ghc-inplace program can pass /all/ the arguments on to the real ghc. */
1334 void
1335 getFullProgArgv(int *argc, char **argv[])
1336 {
1337 if (argc) { *argc = full_prog_argc; }
1338 if (argv) { *argv = full_prog_argv; }
1339 }
1340
1341 void
1342 setFullProgArgv(int argc, char *argv[])
1343 {
1344 int i;
1345 full_prog_argc = argc;
1346 full_prog_argv = stgCallocBytes(argc + 1, sizeof (char *),
1347 "setFullProgArgv 1");
1348 for (i = 0; i < argc; i++) {
1349 full_prog_argv[i] = stgMallocBytes(strlen(argv[i]) + 1,
1350 "setFullProgArgv 2");
1351 strcpy(full_prog_argv[i], argv[i]);
1352 }
1353 full_prog_argv[argc] = NULL;
1354 }
1355