@@ -34,6 +34,54 @@ JL_DLLEXPORT const char *jl_get_default_sysimg_path(void)
3434 return & system_image_path [1 ];
3535}
3636
37+ /* This function is also used by gc-stock.c to parse the
38+ * JULIA_HEAP_SIZE_HINT environment variable. */
39+ uint64_t parse_heap_size_hint (const char * optarg , const char * option_name )
40+ {
41+ long double value = 0.0 ;
42+ char unit [4 ] = {0 };
43+ int nparsed = sscanf (optarg , "%Lf%3s" , & value , unit );
44+ if (nparsed == 0 || strlen (unit ) > 2 || (strlen (unit ) == 2 && ascii_tolower (unit [1 ]) != 'b' )) {
45+ jl_errorf ("julia: invalid argument to %s (%s)" , option_name , optarg );
46+ }
47+ uint64_t multiplier = 1ull ;
48+ switch (ascii_tolower (unit [0 ])) {
49+ case '\0' :
50+ case 'b' :
51+ break ;
52+ case 'k' :
53+ multiplier <<= 10 ;
54+ break ;
55+ case 'm' :
56+ multiplier <<= 20 ;
57+ break ;
58+ case 'g' :
59+ multiplier <<= 30 ;
60+ break ;
61+ case 't' :
62+ multiplier <<= 40 ;
63+ break ;
64+ case '%' :
65+ if (value > 100 )
66+ jl_errorf ("julia: invalid percentage specified in %s" , option_name );
67+ uint64_t mem = uv_get_total_memory ();
68+ uint64_t cmem = uv_get_constrained_memory ();
69+ if (cmem > 0 && cmem < mem )
70+ mem = cmem ;
71+ multiplier = mem /100 ;
72+ break ;
73+ default :
74+ jl_errorf ("julia: invalid argument to %s (%s)" , option_name , optarg );
75+ break ;
76+ }
77+ long double sz = value * multiplier ;
78+ if (isnan (sz ) || sz < 0 ) {
79+ jl_errorf ("julia: invalid argument to %s (%s)" , option_name , optarg );
80+ }
81+ const long double limit = ldexpl (1.0 , 64 ); // UINT64_MAX + 1
82+ return sz < limit ? (uint64_t )sz : UINT64_MAX ;
83+ }
84+
3785static int jl_options_initialized = 0 ;
3886
3987JL_DLLEXPORT void jl_init_options (void )
@@ -231,10 +279,11 @@ static const char opts[] =
231279 " current environment and fallbacks to the latest\n"
232280 " compatible BugReporting.jl if not. For more\n"
233281 " information, see --bug-report=help.\n\n"
234- " --heap-size-hint=<size> Forces garbage collection if memory usage is higher\n"
282+ " --heap-size-hint=<size>[<unit>] Forces garbage collection if memory usage is higher\n"
235283 " than the given value. The value may be specified as a\n"
236- " number of bytes, optionally in units of KB, MB, GB,\n"
237- " or TB, or as a percentage of physical memory with %.\n\n"
284+ " number of bytes, optionally in units of: B, K (kibibytes),\n"
285+ " M (mebibytes), G (gibibytes), T (tebibytes), or % (percentage\n"
286+ " of physical memory).\n\n"
238287;
239288
240289static const char opts_hidden [] =
@@ -880,52 +929,10 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
880929 jl_options .strip_ir = 1 ;
881930 break ;
882931 case opt_heap_size_hint :
883- if (optarg != NULL ) {
884- long double value = 0.0 ;
885- char unit [4 ] = {0 };
886- int nparsed = sscanf (optarg , "%Lf%3s" , & value , unit );
887- if (nparsed == 0 || strlen (unit ) > 2 || (strlen (unit ) == 2 && ascii_tolower (unit [1 ]) != 'b' )) {
888- jl_errorf ("julia: invalid argument to --heap-size-hint (%s)" , optarg );
889- }
890- uint64_t multiplier = 1ull ;
891- switch (ascii_tolower (unit [0 ])) {
892- case '\0' :
893- case 'b' :
894- break ;
895- case 'k' :
896- multiplier <<= 10 ;
897- break ;
898- case 'm' :
899- multiplier <<= 20 ;
900- break ;
901- case 'g' :
902- multiplier <<= 30 ;
903- break ;
904- case 't' :
905- multiplier <<= 40 ;
906- break ;
907- case '%' :
908- if (value > 100 )
909- jl_errorf ("julia: invalid percentage specified in --heap-size-hint" );
910- uint64_t mem = uv_get_total_memory ();
911- uint64_t cmem = uv_get_constrained_memory ();
912- if (cmem > 0 && cmem < mem )
913- mem = cmem ;
914- multiplier = mem /100 ;
915- break ;
916- default :
917- jl_errorf ("julia: invalid argument to --heap-size-hint (%s)" , optarg );
918- break ;
919- }
920- long double sz = value * multiplier ;
921- if (isnan (sz ) || sz < 0 ) {
922- jl_errorf ("julia: invalid argument to --heap-size-hint (%s)" , optarg );
923- }
924- const long double limit = ldexpl (1.0 , 64 ); // UINT64_MAX + 1
925- jl_options .heap_size_hint = sz < limit ? (uint64_t )sz : UINT64_MAX ;
926- }
932+ if (optarg != NULL )
933+ jl_options .heap_size_hint = parse_heap_size_hint (optarg , "--heap-size-hint=<size>[<unit>]" );
927934 if (jl_options .heap_size_hint == 0 )
928- jl_errorf ("julia: invalid memory size specified in --heap-size-hint" );
935+ jl_errorf ("julia: invalid memory size specified in --heap-size-hint=<size>[<unit>] " );
929936
930937 break ;
931938 case opt_gc_threads :
0 commit comments