/
RtsFlags.c
2198 lines (1960 loc) · 73.7 KB
/
RtsFlags.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -----------------------------------------------------------------------------
*
* (c) The AQUA Project, Glasgow University, 1994-1997
* (c) The GHC Team, 1998-2006
*
* Functions for parsing the argument list.
*
* ---------------------------------------------------------------------------*/
#include "PosixSource.h"
#include "Rts.h"
#include "RtsUtils.h"
#include "Profiling.h"
#include "RtsFlags.h"
#include "sm/OSMem.h"
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
// Flag Structure
RTS_FLAGS RtsFlags;
/*
* Split argument lists
*/
int prog_argc = 0; /* an "int" so as to match normal "argc" */
char **prog_argv = NULL;
int full_prog_argc = 0; /* an "int" so as to match normal "argc" */
char **full_prog_argv = NULL;
char *prog_name = NULL; /* 'basename' of prog_argv[0] */
int rts_argc = 0; /* ditto */
char **rts_argv = NULL;
int rts_argv_size = 0;
#if defined(mingw32_HOST_OS)
// On Windows, we want to use GetCommandLineW rather than argc/argv,
// but we need to mutate the command line arguments for withProgName and
// friends. The System.Environment module achieves that using this bit of
// shared state:
int win32_prog_argc = 0;
wchar_t **win32_prog_argv = NULL;
#endif
/*
* constants, used later
*/
#define RTS 1
#define PGM 0
#if defined(PARALLEL_RTS)
static void process_par_option(int arg, rtsBool *error);
#if defined(DEBUG)
static char *par_debug_opts_strs[] = {
"DEBUG (-qDv, -qD1): verbose; be generally verbose with parallel related stuff.\n",
"DEBUG (-qDc, -qD2): mpcomm; low level messages.\n",
"DEBUG (-qDp, -qD4): pack; packing code.\n",
"DEBUG (-qDq, -qD8): packet; verbose packing code.\n",
"DEBUG (-qDP, -qD16): processes; process management code.\n",
"DEBUG (-qDo, -qD32): ports; port management code (more verbose).\n"
};
/* one character codes for the available debug options */
static char par_debug_opts_flags[] = {
'v', 'c', 'p', 'q', 'P', 'o'
};
static void set_par_debug_options(nat n);
static void help_par_debug_options(nat n);
#endif
#endif
/* -----------------------------------------------------------------------------
Static function decls
-------------------------------------------------------------------------- */
static void procRtsOpts (HsBool is_hs_main, int rts_argc0, RtsOptsEnabledEnum enabled);
static void normaliseRtsOpts (void);
static void initStatsFile (FILE *f);
static int openStatsFile (char *filename, const char *FILENAME_FMT,
FILE **file_ret);
static StgWord64 decodeSize (const char *flag, nat offset,
StgWord64 min, StgWord64 max);
static void bad_option (const char *s);
#ifdef TRACING
static void read_trace_flags(char *arg);
#endif
static void errorUsage (void) GNU_ATTRIBUTE(__noreturn__);
static char * copyArg (char *arg);
static char ** copyArgv (int argc, char *argv[]);
static void freeArgv (int argc, char *argv[]);
static void errorRtsOptsDisabled(HsBool is_hs_main, const char *s);
/* -----------------------------------------------------------------------------
* Command-line option parsing routines.
* ---------------------------------------------------------------------------*/
void initRtsFlagsDefaults(void)
{
StgWord64 maxStkSize = 8 * getPhysicalMemorySize() / 10;
// if getPhysicalMemorySize fails just move along with an 8MB limit
if (maxStkSize == 0)
maxStkSize = 8 * 1024 * 1024;
RtsFlags.GcFlags.statsFile = NULL;
RtsFlags.GcFlags.giveStats = NO_GC_STATS;
RtsFlags.GcFlags.maxStkSize = maxStkSize / sizeof(W_);
RtsFlags.GcFlags.initialStkSize = 1024 / sizeof(W_);
RtsFlags.GcFlags.stkChunkSize = (32 * 1024) / sizeof(W_);
RtsFlags.GcFlags.stkChunkBufferSize = (1 * 1024) / sizeof(W_);
RtsFlags.GcFlags.minAllocAreaSize = (512 * 1024) / BLOCK_SIZE;
RtsFlags.GcFlags.minOldGenSize = (1024 * 1024) / BLOCK_SIZE;
RtsFlags.GcFlags.maxHeapSize = 0; /* off by default */
RtsFlags.GcFlags.heapSizeSuggestion = 0; /* none */
RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsFalse;
RtsFlags.GcFlags.pcFreeHeap = 3; /* 3% */
RtsFlags.GcFlags.oldGenFactor = 2;
RtsFlags.GcFlags.generations = 2;
RtsFlags.GcFlags.squeezeUpdFrames = rtsTrue;
RtsFlags.GcFlags.compact = rtsFalse;
RtsFlags.GcFlags.compactThreshold = 30.0;
RtsFlags.GcFlags.sweep = rtsFalse;
RtsFlags.GcFlags.idleGCDelayTime = USToTime(300000); // 300ms
#ifdef THREADED_RTS
RtsFlags.GcFlags.doIdleGC = rtsTrue;
#else
RtsFlags.GcFlags.doIdleGC = rtsFalse;
#endif
#if osf3_HOST_OS
/* ToDo: Perhaps by adjusting this value we can make linking without
* -static work (i.e., not generate a core-dumping executable)? */
# if SIZEOF_VOID_P == 8
RtsFlags.GcFlags.heapBase = 0x180000000L;
# else
# error I have no idea where to begin the heap on a non-64-bit osf3 machine.
# endif
#else
RtsFlags.GcFlags.heapBase = 0; /* means don't care */
#endif
#ifdef DEBUG
RtsFlags.DebugFlags.scheduler = rtsFalse;
RtsFlags.DebugFlags.interpreter = rtsFalse;
RtsFlags.DebugFlags.weak = rtsFalse;
RtsFlags.DebugFlags.gccafs = rtsFalse;
RtsFlags.DebugFlags.gc = rtsFalse;
RtsFlags.DebugFlags.block_alloc = rtsFalse;
RtsFlags.DebugFlags.sanity = rtsFalse;
RtsFlags.DebugFlags.stable = rtsFalse;
RtsFlags.DebugFlags.stm = rtsFalse;
RtsFlags.DebugFlags.prof = rtsFalse;
RtsFlags.DebugFlags.apply = rtsFalse;
RtsFlags.DebugFlags.linker = rtsFalse;
RtsFlags.DebugFlags.squeeze = rtsFalse;
RtsFlags.DebugFlags.hpc = rtsFalse;
RtsFlags.DebugFlags.sparks = rtsFalse;
#endif
#if defined(PROFILING)
RtsFlags.CcFlags.doCostCentres = 0;
#endif /* PROFILING */
RtsFlags.ProfFlags.doHeapProfile = rtsFalse;
RtsFlags.ProfFlags. heapProfileInterval = USToTime(100000); // 100ms
#ifdef PROFILING
RtsFlags.ProfFlags.includeTSOs = rtsFalse;
RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
RtsFlags.ProfFlags.maxRetainerSetSize = 8;
RtsFlags.ProfFlags.ccsLength = 25;
RtsFlags.ProfFlags.modSelector = NULL;
RtsFlags.ProfFlags.descrSelector = NULL;
RtsFlags.ProfFlags.typeSelector = NULL;
RtsFlags.ProfFlags.ccSelector = NULL;
RtsFlags.ProfFlags.ccsSelector = NULL;
RtsFlags.ProfFlags.retainerSelector = NULL;
RtsFlags.ProfFlags.bioSelector = NULL;
#endif
#ifdef TRACING
RtsFlags.TraceFlags.tracing = TRACE_NONE;
RtsFlags.TraceFlags.timestamp = rtsFalse;
RtsFlags.TraceFlags.scheduler = rtsFalse;
RtsFlags.TraceFlags.gc = rtsFalse;
RtsFlags.TraceFlags.sparks_sampled= rtsFalse;
RtsFlags.TraceFlags.sparks_full = rtsFalse;
RtsFlags.TraceFlags.user = rtsFalse;
#endif
#ifdef PROFILING
// When profiling we want a lot more ticks
RtsFlags.MiscFlags.tickInterval = USToTime(1000); // 1ms
#else
RtsFlags.MiscFlags.tickInterval = DEFAULT_TICK_INTERVAL;
#endif
RtsFlags.ConcFlags.ctxtSwitchTime = USToTime(20000); // 20ms
RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
RtsFlags.MiscFlags.machineReadable = rtsFalse;
RtsFlags.MiscFlags.linkerMemBase = 0;
#ifdef THREADED_RTS
RtsFlags.ParFlags.nNodes = 1;
RtsFlags.ParFlags.migrate = rtsTrue;
RtsFlags.ParFlags.parGcEnabled = 1;
RtsFlags.ParFlags.parGcGen = 0;
RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
RtsFlags.ParFlags.parGcLoadBalancingGen = 1;
RtsFlags.ParFlags.parGcNoSyncWithIdle = 0;
RtsFlags.ParFlags.setAffinity = 0;
#endif
/* this flag is always active, to support serialisation in seq.rts */
RtsFlags.ParFlags.packBufferSize = 10485760; // 10MB, avoid trouble
#ifdef PARALLEL_RTS
RtsFlags.ParFlags.sendBufferSize = 20; /* MD should be tested */
RtsFlags.ParFlags.placement = 0; /* default: RR placement,
including local PE*/
#endif /* PARALLEL_RTS */
#if defined(THREADED_RTS)
RtsFlags.ParFlags.maxLocalSparks = 4096;
#endif /* THREADED_RTS */
#ifdef TICKY_TICKY
RtsFlags.TickyFlags.showTickyStats = rtsFalse;
RtsFlags.TickyFlags.tickyFile = NULL;
#endif
#ifdef USE_PAPI
/* By default no special measurements taken */
RtsFlags.PapiFlags.eventType = 0;
RtsFlags.PapiFlags.numUserEvents = 0;
#endif
}
static const char *
usage_text[] = {
"",
"Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
"",
" +RTS Indicates run time system options follow",
" -RTS Indicates program arguments follow",
" --RTS Indicates that ALL subsequent arguments will be given to the",
" program (including any of these RTS flags)",
"",
"The following run time system options are available:",
"",
" -? Prints this message and exits; the program is not executed",
" --info Print information about the RTS used by this program",
"",
" -K<size> Sets the maximum stack size (default: 80% of the heap)",
" Egs: -K32k -K512k -K8M",
" -ki<size> Sets the initial thread stack size (default 1k) Egs: -ki4k -ki2m",
" -kc<size> Sets the stack chunk size (default 32k)",
" -kb<size> Sets the stack chunk buffer size (default 1k)",
"",
" -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
" -M<size> Sets the maximum heap size (default unlimited) Egs: -M256k -M1G",
" -H<size> Sets the minimum heap size (default 0M) Egs: -H24m -H1G",
" -m<n> Minimum % of heap which must be available (default 3%)",
" -G<n> Number of generations (default: 2)",
" -c<n> Use in-place compaction instead of copying in the oldest generation",
" when live data is at least <n>% of the maximum heap size set with",
" -M (default: 30%)",
" -c Use in-place compaction for all oldest generation collections",
" (the default is to use copying)",
" -w Use mark-region for the oldest generation (experimental)",
#if defined(THREADED_RTS)
" -I<sec> Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
#endif
"",
" -T Collect GC statistics (useful for in-program statistics access)",
" -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
" -s[<file>] Summary GC statistics (if <file> omitted, uses stderr)",
" -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
"",
"",
" -Z Don't squeeze out update frames on stack overflow",
" -B Sound the bell at the start of each garbage collection",
#if defined(PROFILING)
"",
" -p Time/allocation profile (output file <program>.prof)",
" -P More detailed Time/Allocation profile",
" -Pa Give information about *all* cost centres",
# if defined(PROFILING)
"",
" -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
" break-down: c = cost centre stack (default)",
" m = module",
" d = closure description",
" y = type description",
" r = retainer",
" b = biography (LAG,DRAG,VOID,USE)",
" A subset of closures may be selected thusly:",
" -hc<cc>,... specific cost centre(s) (top of stack only)",
" -hC<cc>,... specific cost centre(s) (anywhere in stack)",
" -hm<mod>... all cost centres from the specified modules(s)",
" -hd<des>,... closures with specified closure descriptions",
" -hy<typ>... closures with specified type descriptions",
" -hr<cc>... closures with specified retainers",
" -hb<bio>... closures with specified biographies (lag,drag,void,use)",
"",
" -R<size> Set the maximum retainer set size (default: 8)",
"",
" -L<chars> Maximum length of a cost-centre stack in a heap profile",
" (default: 25)",
"",
" -xt Include threads (TSOs) in a heap profile",
"",
" -xc Show current cost centre stack on raising an exception",
# endif
#endif /* PROFILING or PAR */
#ifdef TRACING
"",
" -l[flags] Log events in binary format to the file <program>.eventlog",
# ifdef DEBUG
" -v[flags] Log events to stderr",
# endif
" where [flags] can contain:",
" s scheduler events",
" g GC and heap events",
" p par spark events (sampled)",
" f par spark events (full detail)",
" u user events (emitted from Haskell code)",
" a all event classes above",
# ifdef DEBUG
" t add time stamps (only useful with -v)",
# endif
" -x disable an event class, for any flag above",
" the initial enabled event classes are 'sgpu'",
#endif
#if !defined(PROFILING)
"",
" -h Heap residency profile (output file <program>.hp)",
#endif
" -i<sec> Time between heap profile samples (seconds, default: 0.1)",
"",
#if defined(TICKY_TICKY)
" -r<file> Produce ticky-ticky statistics (with -rstderr for stderr)",
"",
#endif
#ifdef PARALLEL_RTS
" -N<n> Use <n> (virtual) processors in parallel "
// -qp<n> is still understood, but not encouraged.
#if defined(USE_PVM)
"(PVM default: all available nodes)",
/* NB: the -N<n> is implemented by the driver script!! */
#elif defined(USE_MPI)
"(MPI default: 1)",
/* NB: the -N<n> is implemented by the driver script!! */
#else // normally, the default should be as in THREADED_RTS
"(default: 1)",
#endif
#endif
" -C<secs> Context-switch interval in seconds.",
" 0 or no argument means switch as often as possible.",
" Default: 0.02 sec.",
" -V<secs> Master tick interval in seconds (0 == disable timer).",
" This sets the resolution for -C and the heap profile timer -i,",
" and is the frequence of time profile samples.",
#ifdef PROFILING
" Default: 0.001 sec.",
#else
" Default: 0.01 sec.",
#endif
"",
#if defined(DEBUG)
" -Ds DEBUG: scheduler",
" -Di DEBUG: interpreter",
" -Dw DEBUG: weak",
" -DG DEBUG: gccafs",
" -Dg DEBUG: gc",
" -Db DEBUG: block",
" -DS DEBUG: sanity",
" -Dt DEBUG: stable",
" -Dp DEBUG: prof",
" -Da DEBUG: apply",
" -Dl DEBUG: linker",
" -Dm DEBUG: stm",
" -Dz DEBUG: stack squeezing",
" -Dc DEBUG: program coverage",
" -Dr DEBUG: sparks",
"",
" NOTE: DEBUG events are sent to stderr by default; add -l to create a",
" binary event log file instead.",
# if defined(PARALLEL_RTS)
"",
" -qW wait for debugger to attach (in all spawned processes)",
" -qD* DEBUG (parallel), see below",
# endif
"",
#endif /* DEBUG */
#if defined(THREADED_RTS) && !defined(NOSMP)
" -N[<n>] Use <n> processors (default: 1, -N alone determines",
" the number of processors to use automatically)",
" -qg[<n>] Use parallel GC only for generations >= <n>",
" (default: 0, -qg alone turns off parallel GC)",
" -qb[<n>] Use load-balancing in the parallel GC only for generations >= <n>",
" (default: 1, -qb alone turns off load-balancing)",
" -qa Use the OS to set thread affinity (experimental)",
" -qm Don't automatically migrate threads between CPUs",
" -qi<n> If a processor has been idle for the last <n> GCs, do not",
" wake it up for a non-load-balancing parallel GC.",
" (0 disables, default: 0)",
#endif
" --install-signal-handlers=<yes|no>",
" Install signal handlers (default: yes)",
#if defined(PARALLEL_RTS)
" -qQ<size> Set pack-buffer size (default: 1MB)",
" -qq<n> Set MPI-send-buffer size to <n> * pack-buffer (default: 20)",
" -qremote Avoid placing child processes on the same PE",
" -qrnd Enable random process placement (i.e. not round-robin)",
/*
" -qP Activate parallel profiling (Eden)",
" -qPh include GC statistics in trace file (implies -qP)",
" -qPm include message traffic in trace file (implies -qP)",
*/
#endif
#if defined(THREADED_RTS)
" -e<n> Maximum number of outstanding local sparks (default: 4096)",
#endif
#if defined(x86_64_HOST_ARCH)
" -xm Base address to mmap memory in the GHCi linker",
" (hex; must be <80000000)",
#endif
#if defined(USE_PAPI)
" -aX CPU performance counter measurements using PAPI",
" (use with the -s<file> option). X is one of:",
"",
/* " y - cycles", */
" 1 - level 1 cache misses",
" 2 - level 2 cache misses",
" b - branch mispredictions",
" s - stalled cycles",
" e - cache miss and branch misprediction events",
" +PAPI_EVENT - collect papi preset event PAPI_EVENT",
" #NATIVE_EVENT - collect native event NATIVE_EVENT (in hex)",
#endif
"",
"RTS options may also be specified using the GHCRTS environment variable.",
"",
"Other RTS options may be available for programs compiled a different way.",
"The GHC User's Guide has full details.",
"",
0
};
STATIC_INLINE rtsBool
strequal(const char *a, const char * b)
{
return(strcmp(a, b) == 0);
}
// We can't predict up front how much space we'll need for rts_argv,
// because it involves parsing ghc_rts_opts and GHCRTS, so we
// expand it on demand.
static void appendRtsArg (char *arg)
{
if (rts_argc == rts_argv_size) {
rts_argv_size *= 2;
rts_argv = stgReallocBytes(rts_argv, rts_argv_size * sizeof (char *),
"RtsFlags.c:appendRtsArg");
}
rts_argv[rts_argc++] = arg;
}
static void splitRtsFlags(const char *s)
{
const char *c1, *c2;
char *t;
c1 = s;
do {
while (isspace(*c1)) { c1++; };
c2 = c1;
while (!isspace(*c2) && *c2 != '\0') { c2++; };
if (c1 == c2) { break; }
t = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
strncpy(t, c1, c2-c1);
t[c2-c1] = '\0';
appendRtsArg(t);
c1 = c2;
} while (*c1 != '\0');
}
static void
errorRtsOptsDisabled(HsBool is_hs_main, const char *s) {
char *advice;
if (is_hs_main) {
advice = "Link with -rtsopts to enable them.";
} else {
advice = "Use hs_init_with_rtsopts() to enable them.";
}
errorBelch(s, advice);
}
/* -----------------------------------------------------------------------------
Parse the command line arguments, collecting options for the RTS.
On return:
- argv[] is *modified*, any RTS options have been stripped out
- *argc contains the new count of arguments in argv[]
- rts_argv[] (global) contains a copy of the collected RTS args
- rts_argc (global) contains the count of args in rts_argv
- prog_argv[] (global) contains a copy of the non-RTS args (== argv)
- prog_argc (global) contains the count of args in prog_argv
- prog_name (global) contains the basename of prog_argv[0]
-------------------------------------------------------------------------- */
void setupRtsFlags (int *argc, char *argv[],
RtsOptsEnabledEnum rtsOptsEnabled,
const char *ghc_rts_opts,
HsBool is_hs_main)
{
nat mode;
nat total_arg;
nat arg, rts_argc0;
setProgName (argv);
total_arg = *argc;
arg = 1;
if (*argc > 1) { *argc = 1; };
rts_argc = 0;
rts_argv_size = total_arg + 1;
rts_argv = stgMallocBytes(rts_argv_size * sizeof (char *), "setupRtsFlags");
rts_argc0 = rts_argc;
// process arguments from the -with-rtsopts compile-time flag first
// (arguments from the GHCRTS environment variable and the command
// line override these).
{
if (ghc_rts_opts != NULL) {
splitRtsFlags(ghc_rts_opts);
// opts from ghc_rts_opts are always enabled:
procRtsOpts(is_hs_main, rts_argc0, RtsOptsAll);
rts_argc0 = rts_argc;
}
}
// process arguments from the GHCRTS environment variable next
// (arguments from the command line override these).
{
char *ghc_rts = getenv("GHCRTS");
if (ghc_rts != NULL) {
if (rtsOptsEnabled == RtsOptsNone) {
errorRtsOptsDisabled(is_hs_main, "Warning: Ignoring GHCRTS variable as RTS options are disabled.\n %s");
// We don't actually exit, just warn
} else {
splitRtsFlags(ghc_rts);
procRtsOpts(is_hs_main, rts_argc0, rtsOptsEnabled);
rts_argc0 = rts_argc;
}
}
}
// Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
// argv[0] must be PGM argument -- leave in argv
for (mode = PGM; arg < total_arg; arg++) {
// The '--RTS' argument disables all future +RTS ... -RTS processing.
if (strequal("--RTS", argv[arg])) {
arg++;
break;
}
// The '--' argument is passed through to the program, but
// disables all further +RTS ... -RTS processing.
else if (strequal("--", argv[arg])) {
break;
}
else if (strequal("+RTS", argv[arg])) {
mode = RTS;
}
else if (strequal("-RTS", argv[arg])) {
mode = PGM;
}
else if (mode == RTS) {
appendRtsArg(copyArg(argv[arg]));
}
else {
argv[(*argc)++] = argv[arg];
}
}
// process remaining program arguments
for (; arg < total_arg; arg++) {
argv[(*argc)++] = argv[arg];
}
argv[*argc] = (char *) 0;
procRtsOpts(is_hs_main, rts_argc0, rtsOptsEnabled);
appendRtsArg((char *)0);
rts_argc--; // appendRtsArg will have bumped it for the NULL (#7227)
normaliseRtsOpts();
setProgArgv(*argc, argv);
if (RtsFlags.GcFlags.statsFile != NULL) {
initStatsFile (RtsFlags.GcFlags.statsFile);
}
if (RtsFlags.TickyFlags.tickyFile != NULL) {
initStatsFile (RtsFlags.GcFlags.statsFile);
}
}
/* -----------------------------------------------------------------------------
* procRtsOpts: Process rts_argv between rts_argc0 and rts_argc.
* -------------------------------------------------------------------------- */
#if defined(HAVE_UNISTD_H) && defined(HAVE_SYS_TYPES_H) && !defined(mingw32_HOST_OS)
static void checkSuid(HsBool is_hs_main, RtsOptsEnabledEnum enabled)
{
if (enabled == RtsOptsSafeOnly) {
/* This doesn't cover linux/posix capabilities like CAP_DAC_OVERRIDE,
we'd have to link with -lcap for that. */
if ((getuid() != geteuid()) || (getgid() != getegid())) {
errorRtsOptsDisabled(is_hs_main, "RTS options are disabled for setuid binaries. %s");
stg_exit(EXIT_FAILURE);
}
}
}
#else
static void checkSuid(HsBool is_hs_main STG_UNUSED, RtsOptsEnabledEnum enabled STG_UNUSED)
{
}
#endif
static void checkUnsafe(HsBool is_hs_main, RtsOptsEnabledEnum enabled)
{
if (enabled == RtsOptsSafeOnly) {
errorRtsOptsDisabled(is_hs_main, "Most RTS options are disabled. %s");
stg_exit(EXIT_FAILURE);
}
}
static void procRtsOpts (HsBool is_hs_main, int rts_argc0, RtsOptsEnabledEnum rtsOptsEnabled)
{
rtsBool error = rtsFalse;
int arg;
if (!(rts_argc0 < rts_argc)) return;
if (rtsOptsEnabled == RtsOptsNone) {
errorRtsOptsDisabled(is_hs_main, "RTS options are disabled. %s");
stg_exit(EXIT_FAILURE);
}
checkSuid(is_hs_main, rtsOptsEnabled);
// Process RTS (rts_argv) part: mainly to determine statsfile
for (arg = rts_argc0; arg < rts_argc; arg++) {
/* We handle RtsOptsSafeOnly mode by declaring each option as
either OPTION_SAFE or OPTION_UNSAFE. To make sure we cover
every branch we use an option_checked flag which is reset
at the start each iteration and checked at the end. */
rtsBool option_checked = rtsFalse;
#define OPTION_SAFE option_checked = rtsTrue;
#define OPTION_UNSAFE checkUnsafe(is_hs_main, rtsOptsEnabled); option_checked = rtsTrue;
if (rts_argv[arg][0] != '-') {
fflush(stdout);
errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
error = rtsTrue;
} else {
switch(rts_argv[arg][1]) {
/* process: general args, then PROFILING-only ones, then
CONCURRENT-only, TICKY-only (same order as defined in
RtsFlags.lh); within those groups, mostly in
case-insensitive alphabetical order. Final group is
x*, which allows for more options.
*/
#ifdef TICKY_TICKY
# define TICKY_BUILD_ONLY(x) x
#else
# define TICKY_BUILD_ONLY(x) \
errorBelch("the flag %s requires the program to be built with -ticky", rts_argv[arg]); \
error = rtsTrue;
#endif
#ifdef PROFILING
# define PROFILING_BUILD_ONLY(x) x
#else
# define PROFILING_BUILD_ONLY(x) \
errorBelch("the flag %s requires the program to be built with -prof", rts_argv[arg]); \
error = rtsTrue;
#endif
#ifdef TRACING
# define TRACING_BUILD_ONLY(x) x
#else
# define TRACING_BUILD_ONLY(x) \
errorBelch("the flag %s requires the program to be built with -eventlog or -debug", rts_argv[arg]); \
error = rtsTrue;
#endif
#ifdef PARALLEL_RTS
# define PARALLEL_RTS_BUILD_ONLY(x) x
# define THREADED_OR_PARALLEL_BUILD_ONLY(x) x
#else
# define THREADED_OR_PARALLEL_BUILD_ONLY(x) THREADED_BUILD_ONLY(x)
# define PARALLEL_RTS_BUILD_ONLY(x) \
errorBelch("the flag %s requires a parallel build", rts_argv[arg]); \
error = rtsTrue;
#endif
#ifdef THREADED_RTS
# define THREADED_BUILD_ONLY(x) x
#else
# define THREADED_BUILD_ONLY(x) \
errorBelch("the flag %s requires the program to be built with -threaded", rts_argv[arg]); \
error = rtsTrue;
#endif
#ifdef DEBUG
# define DEBUG_BUILD_ONLY(x) x
#else
# define DEBUG_BUILD_ONLY(x) \
errorBelch("the flag %s requires the program to be built with -debug", rts_argv[arg]); \
error = rtsTrue;
#endif
/* =========== GENERAL ========================== */
case '?':
OPTION_SAFE;
error = rtsTrue;
break;
/* This isn't going to allow us to keep related options
together as we add more --* flags. We really need a
proper options parser. */
case '-':
if (strequal("install-signal-handlers=yes",
&rts_argv[arg][2])) {
OPTION_UNSAFE;
RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
}
else if (strequal("install-signal-handlers=no",
&rts_argv[arg][2])) {
OPTION_UNSAFE;
RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
}
else if (strequal("machine-readable",
&rts_argv[arg][2])) {
OPTION_UNSAFE;
RtsFlags.MiscFlags.machineReadable = rtsTrue;
}
else if (strequal("info",
&rts_argv[arg][2])) {
OPTION_SAFE;
printRtsInfo();
stg_exit(0);
}
else {
OPTION_SAFE;
errorBelch("unknown RTS option: %s",rts_argv[arg]);
error = rtsTrue;
}
break;
case 'A':
OPTION_UNSAFE;
RtsFlags.GcFlags.minAllocAreaSize
= decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_INT_MAX)
/ BLOCK_SIZE;
break;
#ifdef USE_PAPI
case 'a':
OPTION_UNSAFE;
switch(rts_argv[arg][2]) {
case '1':
RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
break;
case '2':
RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
break;
case 'b':
RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
break;
case 's':
RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
break;
case 'e':
RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
break;
case '+':
case '#':
if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
errorBelch("maximum number of PAPI events reached");
stg_exit(EXIT_FAILURE);
}
nat eventNum = RtsFlags.PapiFlags.numUserEvents++;
char kind = rts_argv[arg][2];
nat eventKind = kind == '+' ? PAPI_PRESET_EVENT_KIND : PAPI_NATIVE_EVENT_KIND;
RtsFlags.PapiFlags.userEvents[eventNum] = rts_argv[arg] + 3;
RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
RtsFlags.PapiFlags.userEventsKind[eventNum] = eventKind;
break;
default:
bad_option( rts_argv[arg] );
}
break;
#endif
case 'B':
OPTION_UNSAFE;
RtsFlags.GcFlags.ringBell = rtsTrue;
break;
case 'c':
OPTION_UNSAFE;
if (rts_argv[arg][2] != '\0') {
RtsFlags.GcFlags.compactThreshold =
atof(rts_argv[arg]+2);
} else {
RtsFlags.GcFlags.compact = rtsTrue;
}
break;
case 'w':
OPTION_UNSAFE;
RtsFlags.GcFlags.sweep = rtsTrue;
break;
case 'F':
OPTION_UNSAFE;
RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
if (RtsFlags.GcFlags.oldGenFactor < 0)
bad_option( rts_argv[arg] );
break;
case 'D':
OPTION_SAFE;
DEBUG_BUILD_ONLY(
{
char *c;
for (c = rts_argv[arg] + 2; *c != '\0'; c++) {
switch (*c) {
case 's':
RtsFlags.DebugFlags.scheduler = rtsTrue;
break;
case 'i':
RtsFlags.DebugFlags.interpreter = rtsTrue;
break;
case 'w':
RtsFlags.DebugFlags.weak = rtsTrue;
break;
case 'G':
RtsFlags.DebugFlags.gccafs = rtsTrue;
break;
case 'g':
RtsFlags.DebugFlags.gc = rtsTrue;
break;
case 'b':
RtsFlags.DebugFlags.block_alloc = rtsTrue;
break;
case 'S':
RtsFlags.DebugFlags.sanity = rtsTrue;
break;
case 't':
RtsFlags.DebugFlags.stable = rtsTrue;
break;
case 'p':
RtsFlags.DebugFlags.prof = rtsTrue;
break;
case 'l':
RtsFlags.DebugFlags.linker = rtsTrue;
break;
case 'a':
RtsFlags.DebugFlags.apply = rtsTrue;
break;
case 'm':
RtsFlags.DebugFlags.stm = rtsTrue;
break;
case 'z':
RtsFlags.DebugFlags.squeeze = rtsTrue;
break;
case 'c':
RtsFlags.DebugFlags.hpc = rtsTrue;
break;
case 'r':
RtsFlags.DebugFlags.sparks = rtsTrue;
break;
default:
bad_option( rts_argv[arg] );
}
}
// -Dx also turns on -v. Use -l to direct trace
// events to the .eventlog file instead.
RtsFlags.TraceFlags.tracing = TRACE_STDERR;
})
break;
case 'K':
OPTION_UNSAFE;
RtsFlags.GcFlags.maxStkSize =
decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
break;
case 'k':
OPTION_UNSAFE;
switch(rts_argv[arg][2]) {
case 'c':
RtsFlags.GcFlags.stkChunkSize =
decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
break;
case 'b':
RtsFlags.GcFlags.stkChunkBufferSize =
decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
break;
case 'i':
RtsFlags.GcFlags.initialStkSize =
decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
break;
default:
RtsFlags.GcFlags.initialStkSize =
decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
break;
}
break;
case 'M':
OPTION_UNSAFE;
RtsFlags.GcFlags.maxHeapSize =
decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE;
/* user give size in *bytes* but "maxHeapSize" is in *blocks* */
break;
case 'm':
OPTION_UNSAFE;
RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
RtsFlags.GcFlags.pcFreeHeap > 100)
bad_option( rts_argv[arg] );
break;
case 'G':
OPTION_UNSAFE;
RtsFlags.GcFlags.generations =
decodeSize(rts_argv[arg], 2, 1, HS_INT_MAX);
break;
case 'H':
OPTION_UNSAFE;
if (rts_argv[arg][2] == '\0') {
RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsTrue;
} else {
RtsFlags.GcFlags.heapSizeSuggestion =
(nat)(decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE);
}
break;