Skip to content

Commit

Permalink
also expose neededBuilds and neededSubstitutes in json
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Dec 10, 2024
1 parent 8e82a79 commit 4779abb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
55 changes: 49 additions & 6 deletions src/drv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,67 @@
#include <string>
#include <utility>
#include <vector>
#include <algorithm>

#include "drv.hh"
#include "eval-args.hh"

namespace {

auto queryCacheStatus(nix::Store &store,
std::map<std::string, std::optional<std::string>>
&outputs) -> Drv::CacheStatus {
std::map<std::string,
std::optional<std::string>> &outputs,
std::vector<std::string> &neededBuilds,
std::vector<std::string> &neededSubstitutes,
std::vector<std::string> &unknownPaths) -> Drv::CacheStatus {
uint64_t downloadSize = 0;
uint64_t narSize = 0;
nix::StorePathSet willBuild;
nix::StorePathSet willSubstitute;
nix::StorePathSet unknown;

std::vector<nix::StorePathWithOutputs> paths;
for (auto const &[key, val] : outputs) {
if (val) {
paths.push_back(followLinksToStorePathWithOutputs(store, *val));
}
}
nix::StorePathSet willBuild;
nix::StorePathSet willSubstitute;
nix::StorePathSet unknown;

store.queryMissing(toDerivedPaths(paths), willBuild, willSubstitute,
unknown, downloadSize, narSize);

if (!willBuild.empty()) {
// TODO: can we expose the topological sort order as a graph?
auto sorted = store.topoSortPaths(willBuild);
reverse(sorted.begin(), sorted.end());
for (auto &i : sorted) {
neededBuilds.push_back(store.printStorePath(i));
}
}
if (!willSubstitute.empty()) {
std::vector<const nix::StorePath *> willSubstituteSorted = {};
std::for_each(willSubstitute.begin(), willSubstitute.end(),
[&](const nix::StorePath &p) {
willSubstituteSorted.push_back(&p);
});
std::sort(willSubstituteSorted.begin(), willSubstituteSorted.end(),
[](const nix::StorePath *lhs, const nix::StorePath *rhs) {
if (lhs->name() == rhs->name()) {
return lhs->to_string() < rhs->to_string();
}
return lhs->name() < rhs->name();
});
for (const auto *p : willSubstituteSorted) {
neededSubstitutes.push_back(store.printStorePath(*p));
}
}

if (!unknown.empty()) {
for (const auto &i : unknown) {
unknownPaths.push_back(store.printStorePath(i));
}
}

if (willBuild.empty() && unknown.empty()) {
if (willSubstitute.empty()) {
// cacheStatus is Local if:
Expand Down Expand Up @@ -153,7 +190,9 @@ Drv::Drv(std::string &attrPath, nix::EvalState &state,
meta = meta_;
}
if (args.checkCacheStatus) {
cacheStatus = queryCacheStatus(*localStore, outputs);
// TODO: is this a bottleneck, where we should batch these queries?
cacheStatus = queryCacheStatus(*localStore, outputs, neededBuilds,
neededSubstitutes, unknownPaths);
} else {
cacheStatus = Drv::CacheStatus::Unknown;
}
Expand Down Expand Up @@ -204,5 +243,9 @@ void to_json(nlohmann::json &json, const Drv &drv) {
json["cacheStatus"] = "notBuilt";
break;
}
json["neededBuilds"] = drv.neededBuilds;
json["neededSubstitutes"] = drv.neededSubstitutes;
// TODO: is it useful to include "unknown" paths at all?
// json["unknown"] = drv.unknownPaths;
}
}
15 changes: 13 additions & 2 deletions src/drv.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,25 @@ struct Drv {
std::string system;
std::string drvPath;

std::map<std::string, std::optional<std::string>> outputs;

// TODO: make this optional or remove?
std::map<std::string, std::set<std::string>> inputDrvs;

// TODO: can we lazily allocate these?
std::vector<std::string> neededBuilds;
std::vector<std::string> neededSubstitutes;
std::vector<std::string> unknownPaths;

// TODO: we might not need to store this as it can be computed from the
// above
enum class CacheStatus : uint8_t {
Local,
Cached,
NotBuilt,
Unknown
} cacheStatus;
std::map<std::string, std::optional<std::string>> outputs;
std::map<std::string, std::set<std::string>> inputDrvs;

std::optional<nlohmann::json> meta;
std::optional<Constituents> constituents;
};
Expand Down

0 comments on commit 4779abb

Please sign in to comment.