Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tillt/9041279 to your computer and use it in GitHub Desktop.
Save tillt/9041279 to your computer and use it in GitHub Desktop.
// Create a fresh set of resources only for memory.
// You may want to include all other resources in this set as well.... YMMV!
Resources rangedResources;
// Memory resource.
if (!resources.mem().isSome()) {
// No memory specified so probe OS or resort to DEFAULT_MEM.
Bytes mem;
Try<os::Memory> mem_ = os::memory();
if (mem_.isError()) {
LOG(WARNING) << "Failed to auto-detect the size of main memory: '"
<< mem_.error()
<< "' ; defaulting to DEFAULT_MEM";
mem = DEFAULT_MEM;
} else {
mem = mem_.get().total;
// Leave 1 GB free if we have more than 1 GB, otherwise, use all!
// TODO(benh): Have better default scheme (e.g., % of mem not greater
// than 1 GB?)
if (mem > Gigabytes(1)) {
mem = mem - Gigabytes(1);
}
}
rangedResources += Resources::parse(
"mem",
stringify(mem.megabytes()),
flags.default_role).get();
} else {
// Fetch the user supplied memory value from our collection.
Bytes mem = resource.mem.get();
// Do a range check on the user supplied values.
if (mem < SOME_VALUE_YOU_FIND_GOOD_ENOUGH_FOR_A_MINIMUM) {
LOG(INFO) << "Given memory value is too low, adapting towards minimum.";
mem = SOME_VALUE_YOU_FIND_GOOD_ENOUGH_FOR_A_MINIMUM;
} else if (mem > SOME_VALUE_YOU_FIND_GOOD_ENOUGH_FOR_A_MAXIMUM) {
LOG(INFO) << "Given memory value is too high, adapting towards maximum.";
mem = SOME_VALUE_YOU_FIND_GOOD_ENOUGH_FOR_A_MAXIMUM;
}
// Write possibly adapted resource memory entry back to our resources collection.
rangedResources += Resources::parse(
"mem",
stringify(mem.megabytes()),
flags.default_role).get();
}
// You now have a set of range checked resource (rangedResources) for the resource "mem".
// YEEEHAAAW!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment