2017-05-02 14:17:37 +03:00
|
|
|
#include "machines.hh"
|
|
|
|
#include "globals.hh"
|
2020-08-12 19:32:36 +03:00
|
|
|
#include "store-api.hh"
|
2017-05-02 14:17:37 +03:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-01-23 22:36:03 +02:00
|
|
|
Machine::Machine(
|
|
|
|
const std::string & storeUri,
|
2017-05-02 14:17:37 +03:00
|
|
|
decltype(systemTypes) systemTypes,
|
|
|
|
decltype(sshKey) sshKey,
|
|
|
|
decltype(maxJobs) maxJobs,
|
|
|
|
decltype(speedFactor) speedFactor,
|
|
|
|
decltype(supportedFeatures) supportedFeatures,
|
2017-05-02 15:36:59 +03:00
|
|
|
decltype(mandatoryFeatures) mandatoryFeatures,
|
|
|
|
decltype(sshPublicHostKey) sshPublicHostKey) :
|
2024-01-23 22:36:03 +02:00
|
|
|
storeUri(StoreReference::parse(
|
2021-06-24 00:07:55 +03:00
|
|
|
// Backwards compatibility: if the URI is schemeless, is not a path,
|
|
|
|
// and is not one of the special store connection words, prepend
|
|
|
|
// ssh://.
|
2017-10-24 16:16:18 +03:00
|
|
|
storeUri.find("://") != std::string::npos
|
2021-06-24 00:07:55 +03:00
|
|
|
|| storeUri.find("/") != std::string::npos
|
|
|
|
|| storeUri == "auto"
|
|
|
|
|| storeUri == "daemon"
|
|
|
|
|| storeUri == "local"
|
|
|
|
|| hasPrefix(storeUri, "auto?")
|
|
|
|
|| hasPrefix(storeUri, "daemon?")
|
|
|
|
|| hasPrefix(storeUri, "local?")
|
|
|
|
|| hasPrefix(storeUri, "?")
|
2017-05-02 14:17:37 +03:00
|
|
|
? storeUri
|
2024-01-23 22:36:03 +02:00
|
|
|
: "ssh://" + storeUri)),
|
2017-05-02 14:17:37 +03:00
|
|
|
systemTypes(systemTypes),
|
|
|
|
sshKey(sshKey),
|
|
|
|
maxJobs(maxJobs),
|
2024-01-24 08:03:07 +02:00
|
|
|
speedFactor(speedFactor == 0.0f ? 1.0f : std::move(speedFactor)),
|
2017-05-02 14:17:37 +03:00
|
|
|
supportedFeatures(supportedFeatures),
|
2017-05-02 15:36:59 +03:00
|
|
|
mandatoryFeatures(mandatoryFeatures),
|
|
|
|
sshPublicHostKey(sshPublicHostKey)
|
2024-01-24 08:03:07 +02:00
|
|
|
{
|
|
|
|
if (speedFactor < 0.0)
|
|
|
|
throw UsageError("speed factor must be >= 0");
|
|
|
|
}
|
2017-05-02 14:17:37 +03:00
|
|
|
|
2024-01-23 19:52:54 +02:00
|
|
|
bool Machine::systemSupported(const std::string & system) const
|
|
|
|
{
|
|
|
|
return system == "builtin" || (systemTypes.count(system) > 0);
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
bool Machine::allSupported(const std::set<std::string> & features) const
|
2021-10-29 15:45:13 +03:00
|
|
|
{
|
2017-05-02 14:17:37 +03:00
|
|
|
return std::all_of(features.begin(), features.end(),
|
2022-02-25 17:00:00 +02:00
|
|
|
[&](const std::string & feature) {
|
2017-05-02 14:17:37 +03:00
|
|
|
return supportedFeatures.count(feature) ||
|
|
|
|
mandatoryFeatures.count(feature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
bool Machine::mandatoryMet(const std::set<std::string> & features) const
|
2021-10-29 15:45:13 +03:00
|
|
|
{
|
2017-05-02 14:17:37 +03:00
|
|
|
return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(),
|
2022-02-25 17:00:00 +02:00
|
|
|
[&](const std::string & feature) {
|
2017-05-02 14:17:37 +03:00
|
|
|
return features.count(feature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:36:03 +02:00
|
|
|
StoreReference Machine::completeStoreReference() const
|
2021-10-29 15:45:13 +03:00
|
|
|
{
|
2024-01-23 22:36:03 +02:00
|
|
|
auto storeUri = this->storeUri;
|
|
|
|
|
|
|
|
auto * generic = std::get_if<StoreReference::Specified>(&storeUri.variant);
|
|
|
|
|
|
|
|
if (generic && generic->scheme == "ssh") {
|
|
|
|
storeUri.params["max-connections"] = "1";
|
|
|
|
storeUri.params["log-fd"] = "4";
|
2021-02-25 03:52:22 +02:00
|
|
|
}
|
|
|
|
|
2024-01-23 22:36:03 +02:00
|
|
|
if (generic && (generic->scheme == "ssh" || generic->scheme == "ssh-ng")) {
|
2020-08-12 19:32:36 +03:00
|
|
|
if (sshKey != "")
|
2024-01-23 22:36:03 +02:00
|
|
|
storeUri.params["ssh-key"] = sshKey;
|
2021-02-25 03:52:22 +02:00
|
|
|
if (sshPublicHostKey != "")
|
2024-01-23 22:36:03 +02:00
|
|
|
storeUri.params["base64-ssh-public-host-key"] = sshPublicHostKey;
|
2020-08-12 19:32:36 +03:00
|
|
|
}
|
2021-02-25 03:52:22 +02:00
|
|
|
|
2020-08-12 19:32:36 +03:00
|
|
|
{
|
2024-01-23 22:36:03 +02:00
|
|
|
auto & fs = storeUri.params["system-features"];
|
2020-08-12 19:32:36 +03:00
|
|
|
auto append = [&](auto feats) {
|
|
|
|
for (auto & f : feats) {
|
|
|
|
if (fs.size() > 0) fs += ' ';
|
|
|
|
fs += f;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
append(supportedFeatures);
|
|
|
|
append(mandatoryFeatures);
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:36:03 +02:00
|
|
|
return storeUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref<Store> Machine::openStore() const
|
|
|
|
{
|
|
|
|
return nix::openStore(completeStoreReference());
|
2020-08-12 19:32:36 +03:00
|
|
|
}
|
|
|
|
|
2021-10-29 15:45:13 +03:00
|
|
|
static std::vector<std::string> expandBuilderLines(const std::string & builders)
|
|
|
|
{
|
2021-10-12 15:18:44 +03:00
|
|
|
std::vector<std::string> result;
|
2022-02-25 17:00:00 +02:00
|
|
|
for (auto line : tokenizeString<std::vector<std::string>>(builders, "\n;")) {
|
2017-10-24 11:52:34 +03:00
|
|
|
trim(line);
|
2017-05-02 14:17:37 +03:00
|
|
|
line.erase(std::find(line.begin(), line.end(), '#'), line.end());
|
|
|
|
if (line.empty()) continue;
|
2017-10-24 11:52:34 +03:00
|
|
|
|
|
|
|
if (line[0] == '@') {
|
2021-10-12 15:18:44 +03:00
|
|
|
const std::string path = trim(std::string(line, 1));
|
|
|
|
std::string text;
|
2017-10-24 11:52:34 +03:00
|
|
|
try {
|
2021-10-12 15:18:44 +03:00
|
|
|
text = readFile(path);
|
2017-10-24 11:52:34 +03:00
|
|
|
} catch (const SysError & e) {
|
|
|
|
if (e.errNo != ENOENT)
|
|
|
|
throw;
|
2021-10-12 15:18:44 +03:00
|
|
|
debug("cannot find machines file '%s'", path);
|
2017-10-24 11:52:34 +03:00
|
|
|
}
|
2021-10-12 15:18:44 +03:00
|
|
|
|
|
|
|
const auto lines = expandBuilderLines(text);
|
|
|
|
result.insert(end(result), begin(lines), end(lines));
|
2017-10-24 11:52:34 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-12 15:18:44 +03:00
|
|
|
result.emplace_back(line);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-05-02 15:36:59 +03:00
|
|
|
|
2024-05-23 06:12:23 +03:00
|
|
|
static Machine parseBuilderLine(const std::set<std::string> & defaultSystems, const std::string & line)
|
2021-10-29 15:45:13 +03:00
|
|
|
{
|
2022-02-25 17:00:00 +02:00
|
|
|
const auto tokens = tokenizeString<std::vector<std::string>>(line);
|
2021-10-12 15:18:44 +03:00
|
|
|
|
|
|
|
auto isSet = [&](size_t fieldIndex) {
|
|
|
|
return tokens.size() > fieldIndex && tokens[fieldIndex] != "" && tokens[fieldIndex] != "-";
|
|
|
|
};
|
|
|
|
|
|
|
|
auto parseUnsignedIntField = [&](size_t fieldIndex) {
|
|
|
|
const auto result = string2Int<unsigned int>(tokens[fieldIndex]);
|
|
|
|
if (!result) {
|
|
|
|
throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'unsigned int'", fieldIndex, line);
|
|
|
|
}
|
|
|
|
return result.value();
|
|
|
|
};
|
|
|
|
|
2024-01-24 08:03:07 +02:00
|
|
|
auto parseFloatField = [&](size_t fieldIndex) {
|
Remove 100s of CPU time (10%) from build times (1465s -> 1302s)
Result's from Mic92's framework 13th Gen Intel Core i7-1360P:
Before: 3595.92s user 183.01s system 1360% cpu 4:37.74 total
After: 3486.07s user 168.93s system 1354% cpu 4:29.79 total
I saw that boost/lexical_cast was costing about 100s in CPU time on our
compiles. We can fix this trivially by doing explicit template
instantiation in exactly one place and eliminating all other includes of
it, which is a code improvement anyway by hiding the boost.
Before:
```
lix/lix2 » ClangBuildAnalyzer --analyze buildtimeold.bin
Analyzing build trace from 'buildtimeold.bin'...
**** Time summary:
Compilation (551 times):
Parsing (frontend): 1465.3 s
Codegen & opts (backend): 1110.9 s
<snip>
**** Expensive headers:
178153 ms: ../src/libcmd/installable-value.hh (included 52 times, avg 3426 ms), included via:
40x: command.hh
5x: command-installable-value.hh
3x: installable-flake.hh
2x: <direct include>
2x: installable-attr-path.hh
176217 ms: ../src/libutil/error.hh (included 246 times, avg 716 ms), included via:
36x: command.hh installable-value.hh installables.hh derived-path.hh config.hh experimental-features.hh
12x: globals.hh config.hh experimental-features.hh
11x: file-system.hh file-descriptor.hh
6x: serialise.hh strings.hh
6x: <direct include>
6x: archive.hh serialise.hh strings.hh
...
173243 ms: ../src/libstore/store-api.hh (included 152 times, avg 1139 ms), included via:
55x: <direct include>
39x: command.hh installable-value.hh installables.hh
7x: libexpr.hh
4x: local-store.hh
4x: command-installable-value.hh installable-value.hh installables.hh
3x: binary-cache-store.hh
...
170482 ms: ../src/libutil/serialise.hh (included 201 times, avg 848 ms), included via:
37x: command.hh installable-value.hh installables.hh built-path.hh realisation.hh hash.hh
14x: store-api.hh nar-info.hh hash.hh
11x: <direct include>
7x: primops.hh eval.hh attr-set.hh nixexpr.hh value.hh source-path.hh archive.hh
7x: libexpr.hh value.hh source-path.hh archive.hh
6x: fetchers.hh hash.hh
...
169397 ms: ../src/libcmd/installables.hh (included 53 times, avg 3196 ms), included via:
40x: command.hh installable-value.hh
5x: command-installable-value.hh installable-value.hh
3x: installable-flake.hh installable-value.hh
2x: <direct include>
1x: installable-derived-path.hh
1x: installable-value.hh
...
159740 ms: ../src/libutil/strings.hh (included 221 times, avg 722 ms), included via:
37x: command.hh installable-value.hh installables.hh built-path.hh realisation.hh hash.hh serialise.hh
19x: <direct include>
14x: store-api.hh nar-info.hh hash.hh serialise.hh
11x: serialise.hh
7x: primops.hh eval.hh attr-set.hh nixexpr.hh value.hh source-path.hh archive.hh serialise.hh
7x: libexpr.hh value.hh source-path.hh archive.hh serialise.hh
...
156796 ms: ../src/libcmd/command.hh (included 51 times, avg 3074 ms), included via:
42x: <direct include>
7x: command-installable-value.hh
2x: installable-attr-path.hh
150392 ms: ../src/libutil/types.hh (included 251 times, avg 599 ms), included via:
36x: command.hh installable-value.hh installables.hh path.hh
11x: file-system.hh
10x: globals.hh
6x: fetchers.hh
6x: serialise.hh strings.hh error.hh
5x: archive.hh
...
133101 ms: /nix/store/644b90j1vms44nr18yw3520pzkrg4dd1-boost-1.81.0-dev/include/boost/lexical_cast.hpp (included 226 times, avg 588 ms), included via
:
37x: command.hh installable-value.hh installables.hh built-path.hh realisation.hh hash.hh serialise.hh strings.hh
19x: file-system.hh
11x: store-api.hh nar-info.hh hash.hh serialise.hh strings.hh
7x: primops.hh eval.hh attr-set.hh nixexpr.hh value.hh source-path.hh archive.hh serialise.hh strings.hh
7x: libexpr.hh value.hh source-path.hh archive.hh serialise.hh strings.hh
6x: eval.hh attr-set.hh nixexpr.hh value.hh source-path.hh archive.hh serialise.hh strings.hh
...
132887 ms: /nix/store/h2abv2l8irqj942i5rq9wbrj42kbsh5y-gcc-12.3.0/include/c++/12.3.0/memory (included 262 times, avg 507 ms), included via:
36x: command.hh installable-value.hh installables.hh path.hh types.hh ref.hh
16x: gtest.h
11x: file-system.hh types.hh ref.hh
10x: globals.hh types.hh ref.hh
10x: json.hpp
6x: serialise.hh
...
done in 0.6s.
```
After:
```
lix/lix2 » maintainers/buildtime_report.sh build
Processing all files and saving to '/home/jade/lix/lix2/maintainers/../buildtime.bin'...
done in 0.6s. Run 'ClangBuildAnalyzer --analyze /home/jade/lix/lix2/maintainers/../buildtime.bin' to analyze it.
Analyzing build trace from '/home/jade/lix/lix2/maintainers/../buildtime.bin'...
**** Time summary:
Compilation (551 times):
Parsing (frontend): 1302.1 s
Codegen & opts (backend): 956.3 s
<snip>
**** Expensive headers:
178145 ms: ../src/libutil/error.hh (included 246 times, avg 724 ms), included via:
36x: command.hh installable-value.hh installables.hh derived-path.hh config.hh experimental-features.hh
12x: globals.hh config.hh experimental-features.hh
11x: file-system.hh file-descriptor.hh
6x: <direct include>
6x: serialise.hh strings.hh
6x: fetchers.hh hash.hh serialise.hh strings.hh
...
154043 ms: ../src/libcmd/installable-value.hh (included 52 times, avg 2962 ms), included via:
40x: command.hh
5x: command-installable-value.hh
3x: installable-flake.hh
2x: <direct include>
2x: installable-attr-path.hh
153593 ms: ../src/libstore/store-api.hh (included 152 times, avg 1010 ms), included via:
55x: <direct include>
39x: command.hh installable-value.hh installables.hh
7x: libexpr.hh
4x: local-store.hh
4x: command-installable-value.hh installable-value.hh installables.hh
3x: binary-cache-store.hh
...
149948 ms: ../src/libutil/types.hh (included 251 times, avg 597 ms), included via:
36x: command.hh installable-value.hh installables.hh path.hh
11x: file-system.hh
10x: globals.hh
6x: fetchers.hh
6x: serialise.hh strings.hh error.hh
5x: archive.hh
...
144560 ms: ../src/libcmd/installables.hh (included 53 times, avg 2727 ms), included via:
40x: command.hh installable-value.hh
5x: command-installable-value.hh installable-value.hh
3x: installable-flake.hh installable-value.hh
2x: <direct include>
1x: installable-value.hh
1x: installable-derived-path.hh
...
136585 ms: ../src/libcmd/command.hh (included 51 times, avg 2678 ms), included via:
42x: <direct include>
7x: command-installable-value.hh
2x: installable-attr-path.hh
133394 ms: /nix/store/h2abv2l8irqj942i5rq9wbrj42kbsh5y-gcc-12.3.0/include/c++/12.3.0/memory (included 262 times, avg 509 ms), included via:
36x: command.hh installable-value.hh installables.hh path.hh types.hh ref.hh
16x: gtest.h
11x: file-system.hh types.hh ref.hh
10x: globals.hh types.hh ref.hh
10x: json.hpp
6x: serialise.hh
...
89315 ms: ../src/libstore/derived-path.hh (included 178 times, avg 501 ms), included via:
37x: command.hh installable-value.hh installables.hh
25x: store-api.hh realisation.hh
7x: primops.hh eval.hh attr-set.hh nixexpr.hh value.hh context.hh
6x: eval.hh attr-set.hh nixexpr.hh value.hh context.hh
6x: libexpr.hh value.hh context.hh
6x: shared.hh
...
87347 ms: /nix/store/h2abv2l8irqj942i5rq9wbrj42kbsh5y-gcc-12.3.0/include/c++/12.3.0/ostream (included 273 times, avg 319 ms), included via:
35x: command.hh installable-value.hh installables.hh path.hh types.hh ref.hh memory unique_ptr.h
12x: regex sstream istream
10x: file-system.hh types.hh ref.hh memory unique_ptr.h
10x: gtest.h memory unique_ptr.h
10x: globals.hh types.hh ref.hh memory unique_ptr.h
6x: fetchers.hh types.hh ref.hh memory unique_ptr.h
...
85249 ms: ../src/libutil/config.hh (included 213 times, avg 400 ms), included via:
37x: command.hh installable-value.hh installables.hh derived-path.hh
20x: globals.hh
20x: logging.hh
16x: store-api.hh logging.hh
6x: <direct include>
6x: eval.hh attr-set.hh nixexpr.hh value.hh context.hh derived-path.hh
...
done in 0.5s.
```
Adapated from https://git.lix.systems/lix-project/lix/commit/18aa3e1d570b4ecbb9962376e5fba5757dad8da9
2024-05-30 07:12:34 +03:00
|
|
|
const auto result = string2Float<float>(tokens[fieldIndex]);
|
2024-01-24 08:03:07 +02:00
|
|
|
if (!result) {
|
|
|
|
throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'float'", fieldIndex, line);
|
|
|
|
}
|
|
|
|
return result.value();
|
|
|
|
};
|
|
|
|
|
2021-10-12 15:18:44 +03:00
|
|
|
auto ensureBase64 = [&](size_t fieldIndex) {
|
2021-10-29 15:45:13 +03:00
|
|
|
const auto & str = tokens[fieldIndex];
|
2021-10-12 15:18:44 +03:00
|
|
|
try {
|
|
|
|
base64Decode(str);
|
2021-10-29 15:45:13 +03:00
|
|
|
} catch (const Error & e) {
|
2021-10-12 15:18:44 +03:00
|
|
|
throw FormatError("bad machine specification: a column #%lu in a row: '%s' is not valid base64 string: %s", fieldIndex, line, e.what());
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2021-10-29 15:45:13 +03:00
|
|
|
if (!isSet(0))
|
|
|
|
throw FormatError("bad machine specification: store URL was not found at the first column of a row: '%s'", line);
|
|
|
|
|
2024-05-23 18:28:25 +03:00
|
|
|
// TODO use designated initializers, once C++ supports those with
|
|
|
|
// custom constructors.
|
2021-10-29 15:45:13 +03:00
|
|
|
return {
|
2024-05-23 18:28:25 +03:00
|
|
|
// `storeUri`
|
2021-10-29 15:45:13 +03:00
|
|
|
tokens[0],
|
2024-05-23 18:28:25 +03:00
|
|
|
// `systemTypes`
|
2024-05-23 06:12:23 +03:00
|
|
|
isSet(1) ? tokenizeString<std::set<std::string>>(tokens[1], ",") : defaultSystems,
|
2024-05-23 18:28:25 +03:00
|
|
|
// `sshKey`
|
2021-10-29 15:45:13 +03:00
|
|
|
isSet(2) ? tokens[2] : "",
|
2024-05-23 18:28:25 +03:00
|
|
|
// `maxJobs`
|
2021-10-29 15:45:13 +03:00
|
|
|
isSet(3) ? parseUnsignedIntField(3) : 1U,
|
2024-05-23 18:28:25 +03:00
|
|
|
// `speedFactor`
|
2024-01-24 08:03:07 +02:00
|
|
|
isSet(4) ? parseFloatField(4) : 1.0f,
|
2024-05-23 18:28:25 +03:00
|
|
|
// `supportedFeatures`
|
2022-02-25 17:00:00 +02:00
|
|
|
isSet(5) ? tokenizeString<std::set<std::string>>(tokens[5], ",") : std::set<std::string>{},
|
2024-05-23 18:28:25 +03:00
|
|
|
// `mandatoryFeatures`
|
2022-02-25 17:00:00 +02:00
|
|
|
isSet(6) ? tokenizeString<std::set<std::string>>(tokens[6], ",") : std::set<std::string>{},
|
2024-05-23 18:28:25 +03:00
|
|
|
// `sshPublicHostKey`
|
2021-10-29 15:45:13 +03:00
|
|
|
isSet(7) ? ensureBase64(7) : ""
|
|
|
|
};
|
2021-10-12 15:18:44 +03:00
|
|
|
}
|
|
|
|
|
2024-05-23 06:12:23 +03:00
|
|
|
static Machines parseBuilderLines(const std::set<std::string> & defaultSystems, const std::vector<std::string> & builders)
|
2022-02-25 17:00:00 +02:00
|
|
|
{
|
2021-10-12 15:18:44 +03:00
|
|
|
Machines result;
|
2024-05-23 06:12:23 +03:00
|
|
|
std::transform(
|
|
|
|
builders.begin(), builders.end(), std::back_inserter(result),
|
|
|
|
[&](auto && line) { return parseBuilderLine(defaultSystems, line); });
|
2021-10-12 15:18:44 +03:00
|
|
|
return result;
|
2017-05-02 14:17:37 +03:00
|
|
|
}
|
|
|
|
|
2024-05-23 06:12:23 +03:00
|
|
|
Machines Machine::parseConfig(const std::set<std::string> & defaultSystems, const std::string & s)
|
|
|
|
{
|
|
|
|
const auto builderLines = expandBuilderLines(s);
|
|
|
|
return parseBuilderLines(defaultSystems, builderLines);
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:44:10 +03:00
|
|
|
Machines getMachines()
|
|
|
|
{
|
2024-05-23 06:12:23 +03:00
|
|
|
return Machine::parseConfig({settings.thisSystem}, settings.builders);
|
2017-05-02 14:44:10 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 14:17:37 +03:00
|
|
|
}
|