2016-02-24 13:48:16 +00:00
|
|
|
|
#include "archive.hh"
|
2016-02-25 16:43:19 +00:00
|
|
|
|
#include "binary-cache-store.hh"
|
2016-02-24 13:48:16 +00:00
|
|
|
|
#include "compression.hh"
|
|
|
|
|
#include "derivations.hh"
|
2016-02-25 16:43:19 +00:00
|
|
|
|
#include "fs-accessor.hh"
|
2016-02-24 13:48:16 +00:00
|
|
|
|
#include "globals.hh"
|
|
|
|
|
#include "nar-info.hh"
|
2016-02-25 16:43:19 +00:00
|
|
|
|
#include "sync.hh"
|
2016-09-02 18:24:34 +00:00
|
|
|
|
#include "remote-fs-accessor.hh"
|
2016-04-20 13:27:48 +00:00
|
|
|
|
#include "nar-info-disk-cache.hh"
|
2016-11-09 17:57:22 +00:00
|
|
|
|
#include "nar-accessor.hh"
|
2019-09-04 17:24:35 +00:00
|
|
|
|
#include "thread-pool.hh"
|
2024-03-10 06:36:47 +00:00
|
|
|
|
#include "signals.hh"
|
2016-02-24 13:48:16 +00:00
|
|
|
|
|
|
|
|
|
#include <chrono>
|
2016-09-16 16:54:14 +00:00
|
|
|
|
#include <future>
|
2019-09-04 17:24:35 +00:00
|
|
|
|
#include <regex>
|
2020-07-13 18:07:19 +00:00
|
|
|
|
#include <fstream>
|
Remove 100s of CPU time (10%) from build times (1465s -> 1302s)
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.
```
Change-Id: I27f0a2d566db17832cd9be935f12efe7f95b92d0
2024-05-30 04:12:34 +00:00
|
|
|
|
#include <sstream>
|
2019-09-04 17:24:35 +00:00
|
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
2016-09-16 16:54:14 +00:00
|
|
|
|
|
2016-02-24 13:48:16 +00:00
|
|
|
|
namespace nix {
|
|
|
|
|
|
2016-06-01 12:49:12 +00:00
|
|
|
|
BinaryCacheStore::BinaryCacheStore(const Params & params)
|
2020-09-10 08:55:51 +00:00
|
|
|
|
: BinaryCacheStoreConfig(params)
|
|
|
|
|
, Store(params)
|
2016-02-24 13:48:16 +00:00
|
|
|
|
{
|
2016-04-29 14:28:57 +00:00
|
|
|
|
if (secretKeyFile != "")
|
2016-02-24 13:48:16 +00:00
|
|
|
|
secretKey = std::unique_ptr<SecretKey>(new SecretKey(readFile(secretKeyFile)));
|
2016-02-24 15:52:28 +00:00
|
|
|
|
|
|
|
|
|
StringSink sink;
|
|
|
|
|
sink << narVersionMagic1;
|
2022-01-17 21:20:05 +00:00
|
|
|
|
narMagic = sink.s;
|
2016-02-24 13:48:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryCacheStore::init()
|
|
|
|
|
{
|
|
|
|
|
std::string cacheInfoFile = "nix-cache-info";
|
2016-05-30 11:33:05 +00:00
|
|
|
|
|
2024-05-03 19:46:07 +00:00
|
|
|
|
auto cacheInfo = getFileContents(cacheInfoFile);
|
2016-05-30 11:33:05 +00:00
|
|
|
|
if (!cacheInfo) {
|
2017-03-14 14:26:01 +00:00
|
|
|
|
upsertFile(cacheInfoFile, "StoreDir: " + storeDir + "\n", "text/x-nix-cache-info");
|
2016-05-30 11:33:05 +00:00
|
|
|
|
} else {
|
|
|
|
|
for (auto & line : tokenizeString<Strings>(*cacheInfo, "\n")) {
|
2020-04-21 23:07:07 +00:00
|
|
|
|
size_t colon= line.find(':');
|
|
|
|
|
if (colon ==std::string::npos) continue;
|
2016-05-30 11:33:05 +00:00
|
|
|
|
auto name = line.substr(0, colon);
|
|
|
|
|
auto value = trim(line.substr(colon + 1, std::string::npos));
|
|
|
|
|
if (name == "StoreDir") {
|
2016-06-01 12:49:12 +00:00
|
|
|
|
if (value != storeDir)
|
2020-04-21 23:07:07 +00:00
|
|
|
|
throw Error("binary cache '%s' is for Nix stores with prefix '%s', not '%s'",
|
|
|
|
|
getUri(), value, storeDir);
|
2016-05-30 11:33:05 +00:00
|
|
|
|
} else if (name == "WantMassQuery") {
|
2021-09-22 12:15:35 +00:00
|
|
|
|
wantMassQuery.setDefault(value == "1");
|
2016-05-30 11:33:05 +00:00
|
|
|
|
} else if (name == "Priority") {
|
2021-09-22 12:15:35 +00:00
|
|
|
|
priority.setDefault(std::stoi(value));
|
2016-05-30 11:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-24 13:48:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 18:58:02 +00:00
|
|
|
|
void BinaryCacheStore::upsertFile(const std::string & path,
|
2020-07-13 18:07:19 +00:00
|
|
|
|
std::string && data,
|
2020-07-10 18:58:02 +00:00
|
|
|
|
const std::string & mimeType)
|
|
|
|
|
{
|
2020-07-13 18:07:19 +00:00
|
|
|
|
upsertFile(path, std::make_shared<std::stringstream>(std::move(data)), mimeType);
|
2020-07-10 18:58:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 22:22:21 +00:00
|
|
|
|
box_ptr<Source> BinaryCacheStore::getFile(const std::string & path)
|
2016-09-16 16:54:14 +00:00
|
|
|
|
{
|
2024-05-10 22:22:21 +00:00
|
|
|
|
return make_box_ptr<GeneratorSource>([](std::string data) -> Generator<Bytes> {
|
|
|
|
|
co_yield std::span{data.data(), data.size()};
|
|
|
|
|
}(std::move(*getFileContents(path))));
|
2018-03-27 21:12:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 19:46:07 +00:00
|
|
|
|
std::optional<std::string> BinaryCacheStore::getFileContents(const std::string & path)
|
2018-03-27 21:12:31 +00:00
|
|
|
|
{
|
2019-07-10 17:46:15 +00:00
|
|
|
|
StringSink sink;
|
|
|
|
|
try {
|
2024-05-10 22:22:21 +00:00
|
|
|
|
return getFile(path)->drain();
|
2019-07-10 17:46:15 +00:00
|
|
|
|
} catch (NoSuchBinaryCacheFile &) {
|
2022-01-17 21:20:05 +00:00
|
|
|
|
return std::nullopt;
|
2019-07-10 17:46:15 +00:00
|
|
|
|
}
|
2022-01-17 21:20:05 +00:00
|
|
|
|
return std::move(sink.s);
|
2016-09-16 16:54:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
std::string BinaryCacheStore::narInfoFileFor(const StorePath & storePath)
|
2016-02-24 13:48:16 +00:00
|
|
|
|
{
|
2020-06-16 12:16:39 +00:00
|
|
|
|
return std::string(storePath.hashPart()) + ".narinfo";
|
2016-02-24 13:48:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 17:44:05 +00:00
|
|
|
|
void BinaryCacheStore::writeNarInfo(ref<NarInfo> narInfo)
|
|
|
|
|
{
|
|
|
|
|
auto narInfoFile = narInfoFileFor(narInfo->path);
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
upsertFile(narInfoFile, narInfo->to_string(*this), "text/x-nix-narinfo");
|
2017-11-14 17:44:05 +00:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto state_(state.lock());
|
2021-10-14 11:28:22 +00:00
|
|
|
|
state_->pathInfoCache.upsert(
|
|
|
|
|
std::string(narInfo->path.to_string()),
|
|
|
|
|
PathInfoCacheValue { .value = std::shared_ptr<NarInfo>(narInfo) });
|
2017-11-14 17:44:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (diskCache)
|
2021-10-14 11:28:22 +00:00
|
|
|
|
diskCache->upsertNarInfo(getUri(), std::string(narInfo->path.hashPart()), std::shared_ptr<NarInfo>(narInfo));
|
2017-11-14 17:44:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 18:58:02 +00:00
|
|
|
|
AutoCloseFD openFile(const Path & path)
|
|
|
|
|
{
|
2024-03-18 13:52:04 +00:00
|
|
|
|
AutoCloseFD fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)};
|
2020-07-10 18:58:02 +00:00
|
|
|
|
if (!fd)
|
|
|
|
|
throw SysError("opening file '%1%'", path);
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 14:40:41 +00:00
|
|
|
|
ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
|
2020-08-15 16:41:28 +00:00
|
|
|
|
Source & narSource, RepairFlag repair, CheckSigsFlag checkSigs,
|
|
|
|
|
std::function<ValidPathInfo(HashResult)> mkInfo)
|
2016-02-24 13:48:16 +00:00
|
|
|
|
{
|
2020-07-10 18:58:02 +00:00
|
|
|
|
auto [fdTemp, fnTemp] = createTempFile();
|
|
|
|
|
|
2020-08-03 16:33:39 +00:00
|
|
|
|
AutoDelete autoDelete(fnTemp);
|
|
|
|
|
|
2020-07-10 18:58:02 +00:00
|
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
|
|
|
|
|
2020-07-13 15:30:42 +00:00
|
|
|
|
/* Read the NAR simultaneously into a CompressionSink+FileSink (to
|
|
|
|
|
write the compressed NAR to disk), into a HashSink (to get the
|
|
|
|
|
NAR hash), and into a NarAccessor (to get the NAR listing). */
|
2020-08-15 16:41:28 +00:00
|
|
|
|
HashSink fileHashSink { htSHA256 };
|
2020-07-13 15:30:42 +00:00
|
|
|
|
std::shared_ptr<FSAccessor> narAccessor;
|
2020-08-15 16:41:28 +00:00
|
|
|
|
HashSink narHashSink { htSHA256 };
|
2020-07-10 18:58:02 +00:00
|
|
|
|
{
|
|
|
|
|
FdSink fileSink(fdTemp.get());
|
2020-08-15 16:41:28 +00:00
|
|
|
|
TeeSink teeSinkCompressed { fileSink, fileHashSink };
|
2021-10-12 06:14:36 +00:00
|
|
|
|
auto compressionSink = makeCompressionSink(compression, teeSinkCompressed, parallelCompression, compressionLevel);
|
2020-08-15 16:41:28 +00:00
|
|
|
|
TeeSink teeSinkUncompressed { *compressionSink, narHashSink };
|
|
|
|
|
TeeSource teeSource { narSource, teeSinkUncompressed };
|
2020-07-13 15:30:42 +00:00
|
|
|
|
narAccessor = makeNarAccessor(teeSource);
|
2020-07-10 18:58:02 +00:00
|
|
|
|
compressionSink->finish();
|
2020-08-04 12:50:43 +00:00
|
|
|
|
fileSink.flush();
|
2020-07-10 18:58:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
|
2020-08-15 16:41:28 +00:00
|
|
|
|
auto info = mkInfo(narHashSink.finish());
|
2020-07-10 18:58:02 +00:00
|
|
|
|
auto narInfo = make_ref<NarInfo>(info);
|
|
|
|
|
narInfo->compression = compression;
|
|
|
|
|
auto [fileHash, fileSize] = fileHashSink.finish();
|
|
|
|
|
narInfo->fileHash = fileHash;
|
|
|
|
|
narInfo->fileSize = fileSize;
|
2020-07-15 21:19:56 +00:00
|
|
|
|
narInfo->url = "nar/" + narInfo->fileHash->to_string(Base32, false) + ".nar"
|
2020-07-10 18:58:02 +00:00
|
|
|
|
+ (compression == "xz" ? ".xz" :
|
|
|
|
|
compression == "bzip2" ? ".bz2" :
|
2021-04-09 21:13:08 +00:00
|
|
|
|
compression == "zstd" ? ".zst" :
|
|
|
|
|
compression == "lzip" ? ".lzip" :
|
|
|
|
|
compression == "lz4" ? ".lz4" :
|
2020-07-10 18:58:02 +00:00
|
|
|
|
compression == "br" ? ".br" :
|
|
|
|
|
"");
|
2020-05-29 20:19:48 +00:00
|
|
|
|
|
2020-07-10 18:58:02 +00:00
|
|
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
|
|
|
|
|
printMsg(lvlTalkative, "copying path '%1%' (%2% bytes, compressed %3$.1f%% in %4% ms) to binary cache",
|
|
|
|
|
printStorePath(narInfo->path), info.narSize,
|
|
|
|
|
((1.0 - (double) fileSize / info.narSize) * 100.0),
|
|
|
|
|
duration);
|
2016-05-04 11:36:54 +00:00
|
|
|
|
|
2016-04-22 10:15:06 +00:00
|
|
|
|
/* Verify that all references are valid. This may do some .narinfo
|
2023-01-14 21:38:43 +00:00
|
|
|
|
reads, but typically they'll already be cached. */
|
|
|
|
|
for (auto & ref : info.references)
|
2016-04-22 10:15:06 +00:00
|
|
|
|
try {
|
2023-01-14 21:38:43 +00:00
|
|
|
|
if (ref != info.path)
|
|
|
|
|
queryPathInfo(ref);
|
2016-04-22 10:15:06 +00:00
|
|
|
|
} catch (InvalidPath &) {
|
2024-05-18 10:16:32 +00:00
|
|
|
|
throw Error("cannot add '%s' to the binary cache because the reference '%s' does not exist",
|
2019-12-05 18:11:09 +00:00
|
|
|
|
printStorePath(info.path), printStorePath(ref));
|
2016-04-22 10:15:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-21 14:50:28 +00:00
|
|
|
|
/* Optionally write a JSON file containing a listing of the
|
|
|
|
|
contents of the NAR. */
|
|
|
|
|
if (writeNARListing) {
|
2022-11-16 15:49:49 +00:00
|
|
|
|
nlohmann::json j = {
|
|
|
|
|
{"version", 1},
|
|
|
|
|
{"root", listNar(ref<FSAccessor>(narAccessor), "", true)},
|
|
|
|
|
};
|
2016-10-21 14:50:28 +00:00
|
|
|
|
|
2022-11-16 15:49:49 +00:00
|
|
|
|
upsertFile(std::string(info.path.hashPart()) + ".ls", j.dump(), "application/json");
|
2016-10-21 14:50:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 17:24:35 +00:00
|
|
|
|
/* Optionally maintain an index of DWARF debug info files
|
|
|
|
|
consisting of JSON files named 'debuginfo/<build-id>' that
|
|
|
|
|
specify the NAR file and member containing the debug info. */
|
|
|
|
|
if (writeDebugInfo) {
|
|
|
|
|
|
|
|
|
|
std::string buildIdDir = "/lib/debug/.build-id";
|
|
|
|
|
|
|
|
|
|
if (narAccessor->stat(buildIdDir).type == FSAccessor::tDirectory) {
|
|
|
|
|
|
|
|
|
|
ThreadPool threadPool(25);
|
|
|
|
|
|
|
|
|
|
auto doFile = [&](std::string member, std::string key, std::string target) {
|
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
|
|
nlohmann::json json;
|
|
|
|
|
json["archive"] = target;
|
|
|
|
|
json["member"] = member;
|
|
|
|
|
|
|
|
|
|
// FIXME: or should we overwrite? The previous link may point
|
|
|
|
|
// to a GC'ed file, so overwriting might be useful...
|
|
|
|
|
if (fileExists(key)) return;
|
|
|
|
|
|
|
|
|
|
printMsg(lvlTalkative, "creating debuginfo link from '%s' to '%s'", key, target);
|
|
|
|
|
|
|
|
|
|
upsertFile(key, json.dump(), "application/json");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::regex regex1("^[0-9a-f]{2}$");
|
|
|
|
|
std::regex regex2("^[0-9a-f]{38}\\.debug$");
|
|
|
|
|
|
|
|
|
|
for (auto & s1 : narAccessor->readDirectory(buildIdDir)) {
|
|
|
|
|
auto dir = buildIdDir + "/" + s1;
|
|
|
|
|
|
|
|
|
|
if (narAccessor->stat(dir).type != FSAccessor::tDirectory
|
|
|
|
|
|| !std::regex_match(s1, regex1))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (auto & s2 : narAccessor->readDirectory(dir)) {
|
|
|
|
|
auto debugPath = dir + "/" + s2;
|
|
|
|
|
|
|
|
|
|
if (narAccessor->stat(debugPath).type != FSAccessor::tRegular
|
|
|
|
|
|| !std::regex_match(s2, regex2))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
auto buildId = s1 + s2;
|
|
|
|
|
|
|
|
|
|
std::string key = "debuginfo/" + buildId;
|
|
|
|
|
std::string target = "../" + narInfo->url;
|
|
|
|
|
|
|
|
|
|
threadPool.enqueue(std::bind(doFile, std::string(debugPath, 1), key, target));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
threadPool.process();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Atomically write the NAR file. */
|
2016-05-04 11:36:54 +00:00
|
|
|
|
if (repair || !fileExists(narInfo->url)) {
|
2016-02-24 13:48:16 +00:00
|
|
|
|
stats.narWrite++;
|
2020-07-13 18:07:19 +00:00
|
|
|
|
upsertFile(narInfo->url,
|
2020-08-04 14:00:59 +00:00
|
|
|
|
std::make_shared<std::fstream>(fnTemp, std::ios_base::in | std::ios_base::binary),
|
2020-07-13 18:07:19 +00:00
|
|
|
|
"application/x-nix-nar");
|
2016-02-24 13:48:16 +00:00
|
|
|
|
} else
|
|
|
|
|
stats.narWriteAverted++;
|
|
|
|
|
|
2020-07-10 18:58:02 +00:00
|
|
|
|
stats.narWriteBytes += info.narSize;
|
|
|
|
|
stats.narWriteCompressedBytes += fileSize;
|
2016-02-24 13:48:16 +00:00
|
|
|
|
stats.narWriteCompressionTimeMs += duration;
|
|
|
|
|
|
|
|
|
|
/* Atomically write the NAR info file.*/
|
2019-12-05 18:11:09 +00:00
|
|
|
|
if (secretKey) narInfo->sign(*this, *secretKey);
|
2016-02-24 13:48:16 +00:00
|
|
|
|
|
2017-11-14 17:44:05 +00:00
|
|
|
|
writeNarInfo(narInfo);
|
2016-04-20 13:27:48 +00:00
|
|
|
|
|
2016-02-24 13:48:16 +00:00
|
|
|
|
stats.narInfoWrite++;
|
2020-08-15 16:41:28 +00:00
|
|
|
|
|
2020-09-23 14:40:41 +00:00
|
|
|
|
return narInfo;
|
2020-08-15 16:41:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource,
|
|
|
|
|
RepairFlag repair, CheckSigsFlag checkSigs)
|
|
|
|
|
{
|
|
|
|
|
if (!repair && isValidPath(info.path)) {
|
|
|
|
|
// FIXME: copyNAR -> null sink
|
|
|
|
|
narSource.drain();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 15:32:58 +00:00
|
|
|
|
addToStoreCommon(narSource, repair, checkSigs, {[&](HashResult nar) {
|
2020-08-15 16:41:28 +00:00
|
|
|
|
/* FIXME reinstate these, once we can correctly do hash modulo sink as
|
2020-09-23 14:36:55 +00:00
|
|
|
|
needed. We need to throw here in case we uploaded a corrupted store path. */
|
2020-08-15 16:41:28 +00:00
|
|
|
|
// assert(info.narHash == nar.first);
|
|
|
|
|
// assert(info.narSize == nar.second);
|
|
|
|
|
return info;
|
|
|
|
|
}});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 14:08:06 +00:00
|
|
|
|
StorePath BinaryCacheStore::addToStoreFromDump(Source & dump, std::string_view name,
|
2021-11-09 09:24:49 +00:00
|
|
|
|
FileIngestionMethod method, HashType hashAlgo, RepairFlag repair, const StorePathSet & references)
|
2020-08-15 16:41:28 +00:00
|
|
|
|
{
|
|
|
|
|
if (method != FileIngestionMethod::Recursive || hashAlgo != htSHA256)
|
|
|
|
|
unsupported("addToStoreFromDump");
|
|
|
|
|
return addToStoreCommon(dump, repair, CheckSigs, [&](HashResult nar) {
|
|
|
|
|
ValidPathInfo info {
|
2020-10-07 13:52:20 +00:00
|
|
|
|
*this,
|
2023-01-23 17:58:11 +00:00
|
|
|
|
name,
|
|
|
|
|
FixedOutputInfo {
|
2023-07-05 22:53:44 +00:00
|
|
|
|
.method = method,
|
|
|
|
|
.hash = nar.first,
|
2023-02-28 16:57:20 +00:00
|
|
|
|
.references = {
|
2023-01-23 17:58:11 +00:00
|
|
|
|
.others = references,
|
2023-04-17 13:15:11 +00:00
|
|
|
|
// caller is not capable of creating a self-reference, because this is content-addressed without modulus
|
2023-01-23 17:58:11 +00:00
|
|
|
|
.self = false,
|
2020-10-07 13:52:20 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
2020-08-15 16:41:28 +00:00
|
|
|
|
nar.first,
|
|
|
|
|
};
|
|
|
|
|
info.narSize = nar.second;
|
|
|
|
|
return info;
|
2020-09-23 14:40:41 +00:00
|
|
|
|
})->path;
|
2016-02-24 13:48:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
bool BinaryCacheStore::isValidPathUncached(const StorePath & storePath)
|
2016-02-24 13:48:16 +00:00
|
|
|
|
{
|
2016-02-25 16:43:19 +00:00
|
|
|
|
// FIXME: this only checks whether a .narinfo with a matching hash
|
2016-11-25 23:37:43 +00:00
|
|
|
|
// part exists. So ‘f4kb...-foo’ matches ‘f4kb...-bar’, even
|
2016-02-25 16:43:19 +00:00
|
|
|
|
// though they shouldn't. Not easily fixed.
|
2016-02-24 13:48:16 +00:00
|
|
|
|
return fileExists(narInfoFileFor(storePath));
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 15:48:09 +00:00
|
|
|
|
std::optional<StorePath> BinaryCacheStore::queryPathFromHashPart(const std::string & hashPart)
|
|
|
|
|
{
|
|
|
|
|
auto pseudoPath = StorePath(hashPart + "-" + MissingName);
|
|
|
|
|
try {
|
|
|
|
|
auto info = queryPathInfo(pseudoPath);
|
|
|
|
|
return info->path;
|
|
|
|
|
} catch (InvalidPath &) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 15:58:27 +00:00
|
|
|
|
WireFormatGenerator BinaryCacheStore::narFromPath(const StorePath & storePath)
|
2016-02-24 13:48:16 +00:00
|
|
|
|
{
|
2016-04-19 16:50:15 +00:00
|
|
|
|
auto info = queryPathInfo(storePath).cast<const NarInfo>();
|
2016-02-24 13:48:16 +00:00
|
|
|
|
|
2018-08-06 13:40:29 +00:00
|
|
|
|
try {
|
2024-05-10 22:22:21 +00:00
|
|
|
|
auto file = getFile(info->url);
|
2024-05-16 15:58:27 +00:00
|
|
|
|
return [](auto info, auto file, auto & stats) -> WireFormatGenerator {
|
2024-07-21 09:28:23 +00:00
|
|
|
|
constexpr size_t buflen = 65536;
|
|
|
|
|
auto buf = std::make_unique<char []>(buflen);
|
2024-05-16 15:58:27 +00:00
|
|
|
|
size_t total = 0;
|
|
|
|
|
auto decompressor = makeDecompressionSource(info->compression, *file);
|
|
|
|
|
try {
|
|
|
|
|
while (true) {
|
2024-07-21 09:28:23 +00:00
|
|
|
|
const auto len = decompressor->read(buf.get(), buflen);
|
2024-05-16 15:58:27 +00:00
|
|
|
|
co_yield std::span{buf.get(), len};
|
|
|
|
|
total += len;
|
|
|
|
|
}
|
|
|
|
|
} catch (EndOfFile &) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats.narRead++;
|
|
|
|
|
//stats.narReadCompressedBytes += nar->size(); // FIXME
|
|
|
|
|
stats.narReadBytes += total;
|
|
|
|
|
}(std::move(info), std::move(file), stats);
|
2018-08-06 13:40:29 +00:00
|
|
|
|
} catch (NoSuchBinaryCacheFile & e) {
|
2022-12-07 11:58:58 +00:00
|
|
|
|
throw SubstituteGone(std::move(e.info()));
|
2018-08-06 13:40:29 +00:00
|
|
|
|
}
|
2016-03-21 16:55:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 19:24:36 +00:00
|
|
|
|
std::shared_ptr<const ValidPathInfo> BinaryCacheStore::queryPathInfoUncached(const StorePath & storePath)
|
2016-02-24 13:48:16 +00:00
|
|
|
|
{
|
2017-08-31 13:25:58 +00:00
|
|
|
|
auto uri = getUri();
|
2019-12-05 18:11:09 +00:00
|
|
|
|
auto storePathS = printStorePath(storePath);
|
2017-08-31 13:25:58 +00:00
|
|
|
|
auto act = std::make_shared<Activity>(*logger, lvlTalkative, actQueryPathInfo,
|
2019-12-05 18:11:09 +00:00
|
|
|
|
fmt("querying info about '%s' on '%s'", storePathS, uri), Logger::Fields{storePathS, uri});
|
2017-08-31 13:25:58 +00:00
|
|
|
|
PushActivity pact(act->id);
|
|
|
|
|
|
2016-04-19 16:50:15 +00:00
|
|
|
|
auto narInfoFile = narInfoFileFor(storePath);
|
|
|
|
|
|
2024-05-03 19:46:07 +00:00
|
|
|
|
auto data = getFileContents(narInfoFile);
|
2016-04-19 16:50:15 +00:00
|
|
|
|
|
2024-05-18 05:38:33 +00:00
|
|
|
|
if (!data) return nullptr;
|
2016-04-19 16:50:15 +00:00
|
|
|
|
|
2024-04-27 19:24:36 +00:00
|
|
|
|
stats.narInfoRead++;
|
2017-08-31 13:25:58 +00:00
|
|
|
|
|
2024-04-27 19:24:36 +00:00
|
|
|
|
return std::make_shared<NarInfo>(*this, *data, narInfoFile);
|
2016-02-24 13:48:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
|
StorePath BinaryCacheStore::addToStore(
|
|
|
|
|
std::string_view name,
|
|
|
|
|
const Path & srcPath,
|
|
|
|
|
FileIngestionMethod method,
|
|
|
|
|
HashType hashAlgo,
|
|
|
|
|
PathFilter & filter,
|
|
|
|
|
RepairFlag repair,
|
|
|
|
|
const StorePathSet & references)
|
2016-02-24 15:52:28 +00:00
|
|
|
|
{
|
2020-08-15 16:41:28 +00:00
|
|
|
|
/* FIXME: Make BinaryCacheStore::addToStoreCommon support
|
|
|
|
|
non-recursive+sha256 so we can just use the default
|
|
|
|
|
implementation of this method in terms of addToStoreFromDump. */
|
2016-02-24 15:52:28 +00:00
|
|
|
|
|
2020-09-26 04:56:29 +00:00
|
|
|
|
HashSink sink { hashAlgo };
|
2020-05-27 18:04:20 +00:00
|
|
|
|
if (method == FileIngestionMethod::Recursive) {
|
2024-03-22 23:47:16 +00:00
|
|
|
|
sink << dumpPath(srcPath, filter);
|
2016-02-24 15:52:28 +00:00
|
|
|
|
} else {
|
2024-03-23 23:44:57 +00:00
|
|
|
|
sink << readFileSource(srcPath);
|
2016-02-24 15:52:28 +00:00
|
|
|
|
}
|
2020-09-26 04:56:29 +00:00
|
|
|
|
auto h = sink.finish().first;
|
2016-02-24 15:52:28 +00:00
|
|
|
|
|
2024-03-22 23:47:16 +00:00
|
|
|
|
auto source = GeneratorSource{dumpPath(srcPath, filter)};
|
|
|
|
|
return addToStoreCommon(source, repair, CheckSigs, [&](HashResult nar) {
|
2020-09-26 04:56:29 +00:00
|
|
|
|
ValidPathInfo info {
|
2020-10-07 13:52:20 +00:00
|
|
|
|
*this,
|
2023-01-23 17:58:11 +00:00
|
|
|
|
name,
|
|
|
|
|
FixedOutputInfo {
|
2023-07-05 22:53:44 +00:00
|
|
|
|
.method = method,
|
|
|
|
|
.hash = h,
|
2023-02-28 16:57:20 +00:00
|
|
|
|
.references = {
|
2023-01-23 17:58:11 +00:00
|
|
|
|
.others = references,
|
2023-04-17 13:15:11 +00:00
|
|
|
|
// caller is not capable of creating a self-reference, because this is content-addressed without modulus
|
2023-01-23 17:58:11 +00:00
|
|
|
|
.self = false,
|
2020-10-07 13:52:20 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
2020-09-26 04:56:29 +00:00
|
|
|
|
nar.first,
|
|
|
|
|
};
|
|
|
|
|
info.narSize = nar.second;
|
|
|
|
|
return info;
|
|
|
|
|
})->path;
|
2016-02-24 15:52:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
|
StorePath BinaryCacheStore::addTextToStore(
|
|
|
|
|
std::string_view name,
|
|
|
|
|
std::string_view s,
|
|
|
|
|
const StorePathSet & references,
|
|
|
|
|
RepairFlag repair)
|
2016-02-24 15:52:28 +00:00
|
|
|
|
{
|
2020-09-26 03:21:36 +00:00
|
|
|
|
auto textHash = hashString(htSHA256, s);
|
2023-03-30 20:29:13 +00:00
|
|
|
|
auto path = makeTextPath(name, TextInfo { { textHash }, references });
|
2020-08-15 16:41:28 +00:00
|
|
|
|
|
|
|
|
|
if (!repair && isValidPath(path))
|
|
|
|
|
return path;
|
|
|
|
|
|
2020-10-20 13:03:54 +00:00
|
|
|
|
StringSink sink;
|
2024-03-22 23:47:16 +00:00
|
|
|
|
sink << dumpString(s);
|
2022-01-17 21:20:05 +00:00
|
|
|
|
StringSource source(sink.s);
|
2020-08-15 16:41:28 +00:00
|
|
|
|
return addToStoreCommon(source, repair, CheckSigs, [&](HashResult nar) {
|
2020-10-07 13:52:20 +00:00
|
|
|
|
ValidPathInfo info {
|
|
|
|
|
*this,
|
2023-01-23 17:58:11 +00:00
|
|
|
|
std::string { name },
|
|
|
|
|
TextInfo {
|
2023-07-05 22:53:44 +00:00
|
|
|
|
.hash = textHash,
|
|
|
|
|
.references = references,
|
2020-10-07 13:52:20 +00:00
|
|
|
|
},
|
|
|
|
|
nar.first,
|
|
|
|
|
};
|
2020-08-15 16:41:28 +00:00
|
|
|
|
info.narSize = nar.second;
|
|
|
|
|
return info;
|
2020-09-23 14:40:41 +00:00
|
|
|
|
})->path;
|
2016-02-24 15:52:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 21:44:12 +00:00
|
|
|
|
std::shared_ptr<const Realisation> BinaryCacheStore::queryRealisationUncached(const DrvOutput & id)
|
2020-10-08 15:36:51 +00:00
|
|
|
|
{
|
|
|
|
|
auto outputInfoFilePath = realisationsPrefix + "/" + id.to_string() + ".doi";
|
|
|
|
|
|
2024-05-03 19:46:07 +00:00
|
|
|
|
auto data = getFileContents(outputInfoFilePath);
|
2024-04-27 21:44:12 +00:00
|
|
|
|
if (!data) return {};
|
|
|
|
|
|
|
|
|
|
auto realisation = Realisation::fromJSON(
|
|
|
|
|
nlohmann::json::parse(*data), outputInfoFilePath);
|
|
|
|
|
return std::make_shared<const Realisation>(realisation);
|
2020-10-08 15:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryCacheStore::registerDrvOutput(const Realisation& info) {
|
2021-05-06 14:45:09 +00:00
|
|
|
|
if (diskCache)
|
|
|
|
|
diskCache->upsertRealisation(getUri(), info);
|
2020-10-08 15:36:51 +00:00
|
|
|
|
auto filePath = realisationsPrefix + "/" + info.id.to_string() + ".doi";
|
2020-12-15 08:34:45 +00:00
|
|
|
|
upsertFile(filePath, info.toJSON().dump(), "application/json");
|
2020-10-08 15:36:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 16:43:19 +00:00
|
|
|
|
ref<FSAccessor> BinaryCacheStore::getFSAccessor()
|
|
|
|
|
{
|
2017-10-17 19:15:33 +00:00
|
|
|
|
return make_ref<RemoteFSAccessor>(ref<Store>(shared_from_this()), localNarCache);
|
2016-02-25 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
void BinaryCacheStore::addSignatures(const StorePath & storePath, const StringSet & sigs)
|
2017-11-14 17:44:05 +00:00
|
|
|
|
{
|
|
|
|
|
/* Note: this is inherently racy since there is no locking on
|
|
|
|
|
binary caches. In particular, with S3 this unreliable, even
|
|
|
|
|
when addSignatures() is called sequentially on a path, because
|
|
|
|
|
S3 might return an outdated cached version. */
|
|
|
|
|
|
|
|
|
|
auto narInfo = make_ref<NarInfo>((NarInfo &) *queryPathInfo(storePath));
|
|
|
|
|
|
|
|
|
|
narInfo->sigs.insert(sigs.begin(), sigs.end());
|
|
|
|
|
|
|
|
|
|
writeNarInfo(narInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:58:54 +00:00
|
|
|
|
std::optional<std::string> BinaryCacheStore::getBuildLogExact(const StorePath & path)
|
2017-03-13 13:07:58 +00:00
|
|
|
|
{
|
2022-12-15 20:58:54 +00:00
|
|
|
|
auto logPath = "log/" + std::string(baseNameOf(printStorePath(path)));
|
2017-03-13 13:07:58 +00:00
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
|
debug("fetching build log from binary cache '%s/%s'", getUri(), logPath);
|
2017-03-13 13:07:58 +00:00
|
|
|
|
|
2024-05-03 19:46:07 +00:00
|
|
|
|
return getFileContents(logPath);
|
2017-03-13 13:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 15:14:01 +00:00
|
|
|
|
void BinaryCacheStore::addBuildLog(const StorePath & drvPath, std::string_view log)
|
|
|
|
|
{
|
|
|
|
|
assert(drvPath.isDerivation());
|
|
|
|
|
|
|
|
|
|
upsertFile(
|
|
|
|
|
"log/" + std::string(drvPath.to_string()),
|
|
|
|
|
(std::string) log, // FIXME: don't copy
|
|
|
|
|
"text/plain; charset=utf-8");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 13:48:16 +00:00
|
|
|
|
}
|