Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
move drt_ variables to rt.config
Browse files Browse the repository at this point in the history
add function drtConfigOption to retreive an option
show current settings in GC help message
  • Loading branch information
rainers committed Nov 4, 2014
1 parent 7d12df1 commit 33f5980
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 51 deletions.
1 change: 1 addition & 0 deletions mak/MANIFEST
Expand Up @@ -191,6 +191,7 @@ MANIFEST=\
src\rt\bss_section.c \
src\rt\cast_.d \
src\rt\cmath2.d \
src\rt\config.d \
src\rt\cover.d \
src\rt\critical_.d \
src\rt\deh.d \
Expand Down
1 change: 1 addition & 0 deletions mak/SRCS
Expand Up @@ -95,6 +95,7 @@ SRCS=\
src\rt\arrayshort.d \
src\rt\cast_.d \
src\rt\cmath2.d \
src\rt\config.d \
src\rt\cover.d \
src\rt\critical_.d \
src\rt\deh.d \
Expand Down
76 changes: 25 additions & 51 deletions src/gc/config.d
Expand Up @@ -7,28 +7,6 @@

module gc.config;

// The default way to configure the GC is by passing command line argument --DRT-gcopt to
// the executable, use --DRT-gcopt=help for a list of available options.
//
// Configuration via the command line can be disabled by declaring a variable for the
// linker to pick up before using it's defult from the runtime:
//
// extern(C) __gshared bool drt_cmdline_enabled = false;
//
// Likewise, declare a boolean drt_envvars_enabled to enable configuration via the
// environment variable DRT_GCOPT:
//
// extern(C) __gshared bool drt_envvars_enabled = true;
//
// Setting default configuration properties in the executable can be done by specifying an
// array of options named drt_args:
//
// extern(C) __gshared string[] drt_args = [ "gcopt=precise=1 profile=1"];
//
// Evaluation order of options is drt_args, then environment variables, then command
// line arguments, i.e. if command line arguments are not disabled, they can override
// options specified through the environment or embedded in the executable.

import core.stdc.stdlib;
import core.stdc.stdio;
import core.stdc.ctype;
Expand All @@ -37,19 +15,9 @@ import core.vararg;

extern extern(C) string[] rt_args();

// put each variable in its own COMDAT by making them template instances
template drt_envvars_enabled()
{
pragma(mangle,"drt_envvars_enabled") extern(C) __gshared bool drt_envvars_enabled = false;
}
template drt_cmdline_enabled()
{
pragma(mangle,"drt_cmdline_enabled") extern(C) __gshared bool drt_cmdline_enabled = true;
}
template drt_args()
{
pragma(mangle,"drt_args") extern(C) __gshared string[] drt_args = [];
}
extern extern(C) __gshared bool drt_envvars_enabled;
extern extern(C) __gshared bool drt_cmdline_enabled;
extern extern(C) __gshared string[] drt_args;

struct Config
{
Expand All @@ -65,20 +33,20 @@ struct Config

bool initialize(...) // avoid inlining
{
foreach (a; drt_args!())
foreach (a; drt_args)
{
if(a.length >= 6 && a[0..6] == "gcopt=")
if (!parseOptions(a[6 .. $]))
return false;
}
if(drt_envvars_enabled!())
if(drt_envvars_enabled)
{
auto p = getenv("DRT_GCOPT");
if (p)
if (!parseOptions(p[0 .. strlen(p)]))
return false;
}
if(drt_cmdline_enabled!())
if(drt_cmdline_enabled)
{
foreach (a; rt_args)
{
Expand All @@ -90,19 +58,21 @@ struct Config
return true;
}

string help() @nogc
void help() @nogc
{
return "GC options are specified as white space separated assignments:
disable=0|1 - start disabled
profile=0|1 - enable profiling with summary when terminating program
precise=0|1 - enable precise scanning (not implemented yet)
concurrent=0|1 - enable concurrent collection (not implemented yet)
string s = "GC options are specified as white space separated assignments:
disable:0|1 - start disabled (%d)
profile:0|1 - enable profiling with summary when terminating program (%d)
precise:0|1 - enable precise scanning (not implemented yet)
concurrent:0|1 - enable concurrent collection (not implemented yet)
initReserve=N - initial memory to reserve (MB), default 0
minPoolSize=N - initial and minimum pool size (MB), default 1
maxPoolSize=N - maximum pool size (MB), default 64
incPoolSize=N - pool size increment (MB), defaut 3
initReserve:N - initial memory to reserve in MB (%lld)
minPoolSize:N - initial and minimum pool size in MB (%lld)
maxPoolSize:N - maximum pool size in MB (%lld)
incPoolSize:N - pool size increment MB (%lld)
";
printf(s.ptr, disable, profile, cast(long)initReserve,
cast(long)minPoolSize, cast(long)maxPoolSize, cast(long)incPoolSize);
}

bool parseOptions(const(char)[] opt) @nogc
Expand All @@ -115,13 +85,13 @@ struct Config
if (p >= opt.length)
break;
auto q = p;
while (q < opt.length && opt[q] != '=' && !isspace(opt[q]))
while (q < opt.length && opt[q] != ':' && opt[q] != '=' && !isspace(opt[q]))
q++;

auto s = opt[p .. q];
if(s == "help")
{
printf("%s", help().ptr);
help();
p = q;
}
else if (q < opt.length)
Expand All @@ -130,7 +100,11 @@ struct Config
size_t v = 0;
for ( ; r < opt.length && isdigit(opt[r]); r++)
v = v * 10 + opt[r] - '0';

if(r == q + 1)
{
printf("numeric argument expected for GC option \"%.*s\"\n", cast(int) s.length, s.ptr);
return false;
}
if(s == "disable")
disable = v != 0;
else if(s == "profile")
Expand Down
103 changes: 103 additions & 0 deletions src/rt/config.d
@@ -0,0 +1,103 @@
/**
* Configuration options for druntime
*
* Copyright: Copyright Digital Mars 2014.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Rainer Schuetze
* Source: $(DRUNTIMESRC src/rt/_config.d)
*/

module rt.config;

// The default way to configure the runtime is by passing command line arguments
// starting with "--DRT-" and followed by the option name, e.g. "--DRT-gcopt" to
// configure the GC.
// Command line options starting with "--DRT-" are filtered out before calling main,
// so the program will not see them. They are still available via rt_args().
//
// Configuration via the command line can be disabled by declaring a variable for the
// linker to pick up before using it's default from the runtime:
//
// extern(C) __gshared bool drt_cmdline_enabled = false;
//
// Likewise, declare a boolean drt_envvars_enabled to enable configuration via the
// environment variable "DRT_" followed by the option name, e.g. "DRT_GCOPT":
//
// extern(C) __gshared bool drt_envvars_enabled = true;
//
// Setting default configuration properties in the executable can be done by specifying an
// array of options named drt_args:
//
// extern(C) __gshared string[] drt_args = [ "gcopt=precise:1 profile:1"];
//
// Evaluation order of options is drt_args, then environment variables, then command
// line arguments, i.e. if command line arguments are not disabled, they can override
// options specified through the environment or embedded in the executable.


// put each variable in its own COMDAT by making them template instances
template drt_envvars_enabled()
{
pragma(mangle,"drt_envvars_enabled") extern(C) __gshared bool drt_envvars_enabled = false;
}
template drt_cmdline_enabled()
{
pragma(mangle,"drt_cmdline_enabled") extern(C) __gshared bool drt_cmdline_enabled = true;
}
template drt_args()
{
pragma(mangle,"drt_args") extern(C) __gshared string[] drt_args = [];
}

import core.stdc.ctype : toupper;
import core.stdc.stdlib : getenv;
import core.stdc.string : strlen;

extern extern(C) string[] rt_args() @nogc nothrow;

/**
* get a druntme config option using standard configuration options
* opt name of the option to retreive
*
* returns the options' value if
* - set on the command line as "--DRT-<opt>=value" (drt_cmdline_enabled enabled)
* - the environment variable "DRT_<OPT>" is set (drt_envvars_enabled enabled)
* - drt_args[] contains an entry "<opt>=value"
* - null otherwise
*/
extern(C) string drtConfigOption(string opt) @nogc nothrow
{
if(drt_cmdline_enabled!())
{
foreach (a; rt_args)
{
if (a.length >= opt.length + 7 && a[0..6] == "--DRT-" &&
a[6 .. 6 + opt.length] == opt && a[6 + opt.length] == '=')
return a[7 + opt.length .. $];
}
}
if(drt_envvars_enabled!())
{
if (opt.length >= 32)
assert(0);

char[40] var;
var[0 .. 4] = "DRT_";
foreach (i, c; opt)
var[4 + i] = cast(char) toupper(c);
var[4 + opt.length] = 0;

auto p = getenv(var.ptr);
if (p)
return cast(string) p[0 .. strlen(p)];
}
foreach (a; drt_args!())
{
if(a.length > opt.length && a[0..opt.length] == opt && a[opt.length] == '=')
return a[opt.length + 1 .. $];
}
return null;
}

0 comments on commit 33f5980

Please sign in to comment.