Skip to content

Commit

Permalink
Merge pull request dlang#1098 from MartinNowak/gcGrowth
Browse files Browse the repository at this point in the history
new heap growth strategy
  • Loading branch information
rainers committed Feb 12, 2015
2 parents db4b476 + 76a1bb7 commit b7bbc48
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 104 deletions.
208 changes: 154 additions & 54 deletions src/gc/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import core.stdc.ctype;
import core.stdc.string;
import core.vararg;

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

extern extern(C) __gshared bool rt_envvars_enabled;
Expand All @@ -30,8 +31,11 @@ struct Config
size_t minPoolSize = 1; // initial and minimum pool size (MB)
size_t maxPoolSize = 64; // maximum pool size (MB)
size_t incPoolSize = 3; // pool size increment (MB)
float heapSizeFactor = 2.0; // heap size to used memory ratio

bool initialize() @nogc
@nogc nothrow:

bool initialize()
{
import core.internal.traits : externDFunc;

Expand All @@ -50,7 +54,7 @@ struct Config
return s is null;
}

void help() @nogc nothrow
void help()
{
string s = "GC options are specified as white space separated assignments:
disable:0|1 - start disabled (%d)
Expand All @@ -62,70 +66,166 @@ struct Config
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)
heapSizeFactor:N - targeted heap size to used memory ratio (%f)
";
printf(s.ptr, disable, profile, cast(long)initReserve,
cast(long)minPoolSize, cast(long)maxPoolSize, cast(long)incPoolSize);
printf(s.ptr, disable, profile, cast(long)initReserve, cast(long)minPoolSize,
cast(long)maxPoolSize, cast(long)incPoolSize, heapSizeFactor);
}

bool parseOptions(const(char)[] opt) @nogc nothrow
bool parseOptions(const(char)[] opt)
{
size_t p = 0;
while(p < opt.length)
opt = skip!isspace(opt);
while (opt.length)
{
while (p < opt.length && isspace(opt[p]))
p++;
if (p >= opt.length)
break;
auto q = p;
while (q < opt.length && opt[q] != ':' && opt[q] != '=' && !isspace(opt[q]))
q++;

auto s = opt[p .. q];
if(s == "help")
auto tail = find!(c => c == ':' || c == '=')(opt);
auto name = opt[0 .. $ - tail.length];
if (tail.length <= 1)
return optError("Missing argument for", name);
tail = tail[1 .. $];

switch (name)
{
help();
p = q;
}
else if (q < opt.length)
case "help": help(); break;

foreach (field; __traits(allMembers, Config))
{
auto r = q + 1;
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")
profile = v != 0;
else if(s == "precise")
precise = v != 0;
else if(s == "concurrent")
concurrent = v != 0;
else if(s == "initReserve")
initReserve = v;
else if(s == "minPoolSize")
minPoolSize = v;
else if(s == "maxPoolSize")
maxPoolSize = v;
else if(s == "incPoolSize")
incPoolSize = v;
else
static if (!is(typeof(__traits(getMember, this, field)) == function))
{
printf("Unknown GC option \"%.*s\"\n", cast(int) s.length, s.ptr);
return false;
case field:
if (!parse(name, tail, __traits(getMember, this, field)))
return false;
break;
}
p = r;
}
else
{
printf("Incomplete GC option \"%.*s\"\n", cast(int) s.length, s.ptr);
return false;
break;

default:
return optError("Unknown", name);
}
opt = skip!isspace(tail);
}
return true;
}
}

private:

bool optError(in char[] msg, in char[] name)
{
version (unittest) if (inUnittest) return false;

fprintf(stderr, "%.*s GC option '%.*s'.\n",
cast(int)msg.length, msg.ptr,
cast(int)name.length, name.ptr);
return false;
}

inout(char)[] skip(alias pred)(inout(char)[] str)
{
return find!(c => !pred(c))(str);
}

inout(char)[] find(alias pred)(inout(char)[] str)
{
foreach (i; 0 .. str.length)
if (pred(str[i])) return str[i .. $];
return null;
}

bool parse(T:size_t)(const(char)[] optname, ref const(char)[] str, ref T res)
in { assert(str.length); }
body
{
size_t i, v;
for (; i < str.length && isdigit(str[i]); ++i)
v = 10 * v + str[i] - '0';

if (!i)
return parseError("a number", optname, str);
str = str[i .. $];
res = v;
return true;
}

bool parse(T:bool)(const(char)[] optname, ref const(char)[] str, ref T res)
in { assert(str.length); }
body
{
if (str[0] == '1' || str[0] == 'y' || str[0] == 'Y')
res = true;
else if (str[0] == '0' || str[0] == 'n' || str[0] == 'N')
res = false;
else
return parseError("'0/n/N' or '1/y/Y'", optname, str);
str = str[1 .. $];
return true;
}

bool parse(T:float)(const(char)[] optname, ref const(char)[] str, ref T res)
in { assert(str.length); }
body
{
// % uint f %n \0
char[1 + 10 + 1 + 2 + 1] fmt=void;
// specify max-width
immutable n = snprintf(fmt.ptr, fmt.length, "%%%uf%%n", cast(uint)str.length);
assert(n > 4 && n < fmt.length);

int nscanned;
if (sscanf(str.ptr, fmt.ptr, &res, &nscanned) < 1)
return parseError("a float", optname, str);
str = str[nscanned .. $];
return true;
}

bool parseError(in char[] exp, in char[] opt, in char[] got)
{
version (unittest) if (inUnittest) return false;

fprintf(stderr, "Expecting %.*s as argument for GC option '%.*s', got '%.*s' instead.\n",
cast(int)exp.length, exp.ptr,
cast(int)opt.length, opt.ptr,
cast(int)got.length, got.ptr);
return false;
}

size_t min(size_t a, size_t b) { return a <= b ? a : b; }

version (unittest) __gshared bool inUnittest;

unittest
{
inUnittest = true;
scope (exit) inUnittest = false;

Config conf;
assert(!conf.parseOptions("profile"));
assert(!conf.parseOptions("profile:"));
assert(!conf.parseOptions("profile:5"));
assert(conf.parseOptions("profile:y") && conf.profile);
assert(conf.parseOptions("profile:n") && !conf.profile);
assert(conf.parseOptions("profile:Y") && conf.profile);
assert(conf.parseOptions("profile:N") && !conf.profile);
assert(conf.parseOptions("profile:1") && conf.profile);
assert(conf.parseOptions("profile:0") && !conf.profile);

assert(conf.parseOptions("profile=y") && conf.profile);
assert(conf.parseOptions("profile=n") && !conf.profile);

assert(conf.parseOptions("profile:1 minPoolSize:16"));
assert(conf.profile);
assert(conf.minPoolSize == 16);

assert(conf.parseOptions("heapSizeFactor:3.1"));
assert(conf.heapSizeFactor == 3.1f);
assert(conf.parseOptions("heapSizeFactor:3.1234567890 profile:0"));
assert(conf.heapSizeFactor > 3.123f);
assert(!conf.profile);
assert(!conf.parseOptions("heapSizeFactor:3.0.2.5"));
assert(conf.parseOptions("heapSizeFactor:2"));
assert(conf.heapSizeFactor == 2.0f);

assert(!conf.parseOptions("initReserve:foo"));
assert(!conf.parseOptions("initReserve:y"));
assert(!conf.parseOptions("initReserve:20.5"));
}
Loading

0 comments on commit b7bbc48

Please sign in to comment.