Skip to content

Commit

Permalink
Merge pull request #9841 from obsidiansystems/float-speed-factor
Browse files Browse the repository at this point in the history
Convert `Machine::speedFactor` from a non-neg int to a non-neg float
  • Loading branch information
thufschmitt authored Jan 24, 2024
2 parents 1c260fa + 1e24db6 commit 69d0ae2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/libstore/machines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ Machine::Machine(decltype(storeUri) storeUri,
systemTypes(systemTypes),
sshKey(sshKey),
maxJobs(maxJobs),
speedFactor(std::max(1U, speedFactor)),
speedFactor(speedFactor == 0.0f ? 1.0f : std::move(speedFactor)),
supportedFeatures(supportedFeatures),
mandatoryFeatures(mandatoryFeatures),
sshPublicHostKey(sshPublicHostKey)
{}
{
if (speedFactor < 0.0)
throw UsageError("speed factor must be >= 0");
}

bool Machine::systemSupported(const std::string & system) const
{
Expand Down Expand Up @@ -135,6 +138,14 @@ static Machine parseBuilderLine(const std::string & line)
return result.value();
};

auto parseFloatField = [&](size_t fieldIndex) {
const auto result = string2Int<float>(tokens[fieldIndex]);
if (!result) {
throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'float'", fieldIndex, line);
}
return result.value();
};

auto ensureBase64 = [&](size_t fieldIndex) {
const auto & str = tokens[fieldIndex];
try {
Expand All @@ -153,7 +164,7 @@ static Machine parseBuilderLine(const std::string & line)
isSet(1) ? tokenizeString<std::set<std::string>>(tokens[1], ",") : std::set<std::string>{settings.thisSystem},
isSet(2) ? tokens[2] : "",
isSet(3) ? parseUnsignedIntField(3) : 1U,
isSet(4) ? parseUnsignedIntField(4) : 1U,
isSet(4) ? parseFloatField(4) : 1.0f,
isSet(5) ? tokenizeString<std::set<std::string>>(tokens[5], ",") : std::set<std::string>{},
isSet(6) ? tokenizeString<std::set<std::string>>(tokens[6], ",") : std::set<std::string>{},
isSet(7) ? ensureBase64(7) : ""
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/machines.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Machine {
const std::set<std::string> systemTypes;
const std::string sshKey;
const unsigned int maxJobs;
const unsigned int speedFactor;
const float speedFactor;
const std::set<std::string> supportedFeatures;
const std::set<std::string> mandatoryFeatures;
const std::string sshPublicHostKey;
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/libstore/machines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using testing::SizeIs;

using nix::absPath;
using nix::FormatError;
using nix::UsageError;
using nix::getMachines;
using nix::Machine;
using nix::Machines;
Expand Down Expand Up @@ -133,7 +134,7 @@ TEST(machines, getMachinesWithIncorrectFormat) {
settings.builders = "[email protected] - - 8 three";
EXPECT_THROW(getMachines(), FormatError);
settings.builders = "[email protected] - - 8 -3";
EXPECT_THROW(getMachines(), FormatError);
EXPECT_THROW(getMachines(), UsageError);
settings.builders = "[email protected] - - 8 3 - - BAD_BASE64";
EXPECT_THROW(getMachines(), FormatError);
}
Expand Down

0 comments on commit 69d0ae2

Please sign in to comment.