2009-03-05 14:59:43 +00:00
|
|
|
|
#include <map>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2018-08-10 20:25:43 +02:00
|
|
|
|
#define GC_LINUX_THREADS 1
|
2011-01-14 12:53:54 +00:00
|
|
|
|
#include <gc/gc_allocator.h>
|
|
|
|
|
|
2009-03-05 14:59:43 +00:00
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "eval.hh"
|
2012-10-04 15:24:25 -04:00
|
|
|
|
#include "eval-inline.hh"
|
2009-03-05 15:41:43 +00:00
|
|
|
|
#include "util.hh"
|
2016-10-06 15:05:05 +02:00
|
|
|
|
#include "json.hh"
|
2009-03-06 15:16:29 +00:00
|
|
|
|
#include "get-drvs.hh"
|
2014-09-12 14:30:01 +02:00
|
|
|
|
#include "globals.hh"
|
2017-10-26 13:10:14 +02:00
|
|
|
|
#include "common-eval-args.hh"
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
2018-05-16 14:14:53 +02:00
|
|
|
|
#include "hydra-config.hh"
|
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
2009-03-05 14:59:43 +00:00
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
|
|
2009-03-15 11:56:11 +00:00
|
|
|
|
static Path gcRootsDir;
|
|
|
|
|
|
|
|
|
|
|
2014-09-30 00:20:54 +02:00
|
|
|
|
static void findJobs(EvalState & state, JSONObject & top,
|
2017-10-26 13:10:14 +02:00
|
|
|
|
Bindings & autoArgs, Value & v, const string & attrPath);
|
2009-03-06 16:55:19 +00:00
|
|
|
|
|
2009-03-07 14:06:10 +00:00
|
|
|
|
|
2014-01-12 17:37:56 +01:00
|
|
|
|
static string queryMetaStrings(EvalState & state, DrvInfo & drv, const string & name)
|
2009-07-07 13:20:00 +00:00
|
|
|
|
{
|
2015-10-08 11:56:30 +02:00
|
|
|
|
Strings res;
|
|
|
|
|
std::function<void(Value & v)> rec;
|
|
|
|
|
|
|
|
|
|
rec = [&](Value & v) {
|
|
|
|
|
state.forceValue(v);
|
|
|
|
|
if (v.type == tString)
|
|
|
|
|
res.push_back(v.string.s);
|
|
|
|
|
else if (v.isList())
|
|
|
|
|
for (unsigned int n = 0; n < v.listSize(); ++n)
|
|
|
|
|
rec(*v.listElems()[n]);
|
|
|
|
|
else if (v.type == tAttrs) {
|
|
|
|
|
auto a = v.attrs->find(state.symbols.create("shortName"));
|
|
|
|
|
if (a != v.attrs->end())
|
|
|
|
|
res.push_back(state.forceString(*a->value));
|
2013-11-04 22:50:32 +01:00
|
|
|
|
}
|
2015-10-08 11:56:30 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Value * v = drv.queryMeta(name);
|
|
|
|
|
if (v) rec(*v);
|
|
|
|
|
|
|
|
|
|
return concatStringsSep(", ", res);
|
2013-11-04 22:50:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
static std::string lastAttrPath;
|
|
|
|
|
static bool comma = false;
|
|
|
|
|
static size_t maxHeapSize;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct BailOut { };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool lte(const std::string & s1, const std::string & s2)
|
|
|
|
|
{
|
|
|
|
|
size_t p1 = 0, p2 = 0;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
if (p1 == s1.size()) return p2 == s2.size();
|
|
|
|
|
if (p2 == s2.size()) return true;
|
|
|
|
|
|
|
|
|
|
auto d1 = s1.find('.', p1);
|
|
|
|
|
auto d2 = s2.find('.', p2);
|
|
|
|
|
|
|
|
|
|
auto c = s1.compare(p1, d1 - p1, s2, p2, d2 - p2);
|
|
|
|
|
|
|
|
|
|
if (c < 0) return true;
|
|
|
|
|
if (c > 0) return false;
|
|
|
|
|
|
|
|
|
|
p1 = d1 == std::string::npos ? s1.size() : d1 + 1;
|
|
|
|
|
p2 = d2 == std::string::npos ? s2.size() : d2 + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-30 00:20:54 +02:00
|
|
|
|
static void findJobsWrapped(EvalState & state, JSONObject & top,
|
2017-10-26 13:10:14 +02:00
|
|
|
|
Bindings & autoArgs, Value & vIn, const string & attrPath)
|
2009-03-05 15:41:43 +00:00
|
|
|
|
{
|
2018-06-05 12:18:55 +02:00
|
|
|
|
if (lastAttrPath != "" && lte(attrPath, lastAttrPath)) return;
|
|
|
|
|
|
2009-03-06 16:55:19 +00:00
|
|
|
|
debug(format("at path `%1%'") % attrPath);
|
2010-11-19 11:01:31 +00:00
|
|
|
|
|
2012-10-04 14:31:47 -04:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
2017-10-26 13:10:14 +02:00
|
|
|
|
Value v;
|
|
|
|
|
state.autoCallFunction(autoArgs, vIn, v);
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
2010-05-18 09:57:37 +00:00
|
|
|
|
if (v.type == tAttrs) {
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
2017-07-21 13:09:08 +02:00
|
|
|
|
auto drv = getDerivation(state, v, false);
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2017-07-21 13:09:08 +02:00
|
|
|
|
if (drv) {
|
2009-03-15 11:56:11 +00:00
|
|
|
|
Path drvPath;
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
2017-07-21 13:09:08 +02:00
|
|
|
|
DrvInfo::Outputs outputs = drv->queryOutputs();
|
2013-02-13 16:49:28 +00:00
|
|
|
|
|
2017-07-21 13:09:08 +02:00
|
|
|
|
if (drv->querySystem() == "unknown")
|
2014-09-22 16:53:40 +02:00
|
|
|
|
throw EvalError("derivation must have a ‘system’ attribute");
|
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
if (comma) { std::cout << ","; comma = false; }
|
|
|
|
|
|
2015-03-18 22:03:55 +01:00
|
|
|
|
{
|
2016-10-06 15:05:05 +02:00
|
|
|
|
auto res = top.object(attrPath);
|
2017-07-21 13:09:08 +02:00
|
|
|
|
res.attr("nixName", drv->queryName());
|
|
|
|
|
res.attr("system", drv->querySystem());
|
|
|
|
|
res.attr("drvPath", drvPath = drv->queryDrvPath());
|
|
|
|
|
res.attr("description", drv->queryMetaString("description"));
|
|
|
|
|
res.attr("license", queryMetaStrings(state, *drv, "license"));
|
|
|
|
|
res.attr("homepage", drv->queryMetaString("homepage"));
|
|
|
|
|
res.attr("maintainers", queryMetaStrings(state, *drv, "maintainers"));
|
|
|
|
|
res.attr("schedulingPriority", drv->queryMetaInt("schedulingPriority", 100));
|
|
|
|
|
res.attr("timeout", drv->queryMetaInt("timeout", 36000));
|
|
|
|
|
res.attr("maxSilent", drv->queryMetaInt("maxSilent", 7200));
|
|
|
|
|
res.attr("isChannel", drv->queryMetaBool("isHydraChannel", false));
|
2010-05-26 08:03:59 +00:00
|
|
|
|
|
2013-08-15 02:33:10 +02:00
|
|
|
|
/* If this is an aggregate, then get its constituents. */
|
2013-08-14 01:59:29 +02:00
|
|
|
|
Bindings::iterator a = v.attrs->find(state.symbols.create("_hydraAggregate"));
|
2016-10-06 15:05:05 +02:00
|
|
|
|
if (a != v.attrs->end() && state.forceBool(*a->value, *a->pos)) {
|
2013-08-15 02:33:10 +02:00
|
|
|
|
Bindings::iterator a = v.attrs->find(state.symbols.create("constituents"));
|
2013-08-14 01:59:29 +02:00
|
|
|
|
if (a == v.attrs->end())
|
2013-08-15 02:33:10 +02:00
|
|
|
|
throw EvalError("derivation must have a ‘constituents’ attribute");
|
2013-08-14 01:59:29 +02:00
|
|
|
|
PathSet context;
|
2014-04-08 17:08:09 +02:00
|
|
|
|
state.coerceToString(*a->pos, *a->value, context, true, false);
|
2013-08-14 01:59:29 +02:00
|
|
|
|
PathSet drvs;
|
2015-07-31 01:45:16 +02:00
|
|
|
|
for (auto & i : context)
|
|
|
|
|
if (i.at(0) == '!') {
|
|
|
|
|
size_t index = i.find("!", 1);
|
|
|
|
|
drvs.insert(string(i, index + 1));
|
2013-08-14 01:59:29 +02:00
|
|
|
|
}
|
2014-09-30 00:20:54 +02:00
|
|
|
|
res.attr("constituents", concatStringsSep(" ", drvs));
|
2013-08-14 01:59:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-15 11:56:11 +00:00
|
|
|
|
/* Register the derivation as a GC root. !!! This
|
|
|
|
|
registers roots for jobs that we may have already
|
|
|
|
|
done. */
|
2016-10-06 15:05:05 +02:00
|
|
|
|
auto localStore = state.store.dynamic_pointer_cast<LocalFSStore>();
|
|
|
|
|
if (gcRootsDir != "" && localStore) {
|
2010-06-01 11:20:05 +00:00
|
|
|
|
Path root = gcRootsDir + "/" + baseNameOf(drvPath);
|
2016-10-06 15:05:05 +02:00
|
|
|
|
if (!pathExists(root)) localStore->addPermRoot(drvPath, root, false);
|
2010-06-01 11:20:05 +00:00
|
|
|
|
}
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2016-10-06 15:05:05 +02:00
|
|
|
|
auto res2 = res.object("outputs");
|
2014-09-30 00:20:54 +02:00
|
|
|
|
for (auto & j : outputs)
|
|
|
|
|
res2.attr(j.first, j.second);
|
2015-03-18 22:03:55 +01:00
|
|
|
|
|
|
|
|
|
}
|
2018-06-05 12:18:55 +02:00
|
|
|
|
|
|
|
|
|
GC_prof_stats_s gc;
|
|
|
|
|
GC_get_prof_stats(&gc, sizeof(gc));
|
|
|
|
|
|
|
|
|
|
if (gc.heapsize_full > maxHeapSize) {
|
|
|
|
|
printInfo("restarting hydra-eval-jobs after job '%s' because heap size is at %d bytes", attrPath, gc.heapsize_full);
|
|
|
|
|
lastAttrPath = attrPath;
|
|
|
|
|
throw BailOut();
|
|
|
|
|
}
|
2009-03-05 15:41:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
2012-10-04 14:31:47 -04:00
|
|
|
|
if (!state.isDerivation(v)) {
|
2018-06-05 12:18:55 +02:00
|
|
|
|
for (auto & i : v.attrs->lexicographicOrder()) {
|
|
|
|
|
std::string name(i->name);
|
|
|
|
|
|
|
|
|
|
/* Skip jobs with dots in the name. */
|
|
|
|
|
if (name.find('.') != std::string::npos) {
|
|
|
|
|
printError("skipping job with illegal name '%s'", name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findJobs(state, top, autoArgs, *i->value,
|
|
|
|
|
(attrPath.empty() ? "" : attrPath + ".") + name);
|
|
|
|
|
}
|
2012-10-04 14:31:47 -04:00
|
|
|
|
}
|
2009-03-05 15:41:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-16 11:14:22 +01:00
|
|
|
|
else if (v.type == tNull) {
|
2012-02-16 11:03:22 +01:00
|
|
|
|
// allow null values, meaning 'do nothing'
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 16:55:19 +00:00
|
|
|
|
else
|
2010-05-18 09:57:37 +00:00
|
|
|
|
throw TypeError(format("unsupported value: %1%") % v);
|
2009-03-07 14:06:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-30 00:20:54 +02:00
|
|
|
|
static void findJobs(EvalState & state, JSONObject & top,
|
2017-10-26 13:10:14 +02:00
|
|
|
|
Bindings & autoArgs, Value & v, const string & attrPath)
|
2009-03-07 14:06:10 +00:00
|
|
|
|
{
|
|
|
|
|
try {
|
2017-10-26 13:10:14 +02:00
|
|
|
|
findJobsWrapped(state, top, autoArgs, v, attrPath);
|
2009-10-08 12:29:20 +00:00
|
|
|
|
} catch (EvalError & e) {
|
2018-06-05 12:18:55 +02:00
|
|
|
|
if (comma) { std::cout << ","; comma = false; }
|
2016-10-06 15:05:05 +02:00
|
|
|
|
auto res = top.object(attrPath);
|
2018-08-01 17:06:32 +02:00
|
|
|
|
res.attr("error", filterANSIEscapes(e.msg(), true));
|
2009-03-07 14:06:10 +00:00
|
|
|
|
}
|
2009-03-05 15:41:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-08-13 16:24:26 +02:00
|
|
|
|
int main(int argc, char * * argv)
|
2009-03-05 14:59:43 +00:00
|
|
|
|
{
|
2018-06-05 12:18:55 +02:00
|
|
|
|
assert(lte("abc", "def"));
|
|
|
|
|
assert(lte("abc", "def.foo"));
|
|
|
|
|
assert(!lte("def", "abc"));
|
|
|
|
|
assert(lte("nixpkgs.hello", "nixpkgs"));
|
|
|
|
|
assert(lte("nixpkgs.hello", "nixpkgs.hellooo"));
|
|
|
|
|
assert(lte("gitAndTools.git-annex.x86_64-darwin", "gitAndTools.git-annex.x86_64-linux"));
|
|
|
|
|
assert(lte("gitAndTools.git-annex.x86_64-linux", "gitAndTools.git-annex-remote-b2.aarch64-linux"));
|
|
|
|
|
|
2012-05-23 14:44:10 -04:00
|
|
|
|
/* Prevent undeclared dependencies in the evaluation via
|
|
|
|
|
$NIX_PATH. */
|
|
|
|
|
unsetenv("NIX_PATH");
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2014-08-13 16:24:26 +02:00
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
2018-05-16 14:14:53 +02:00
|
|
|
|
|
|
|
|
|
auto config = std::make_unique<::Config>();
|
|
|
|
|
|
|
|
|
|
auto initialHeapSize = config->getStrOption("evaluator_initial_heap_size", "");
|
|
|
|
|
if (initialHeapSize != "")
|
|
|
|
|
setenv("GC_INITIAL_HEAP_SIZE", initialHeapSize.c_str(), 1);
|
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
maxHeapSize = config->getIntOption("evaluator_max_heap_size", 1UL << 30);
|
|
|
|
|
|
2014-08-13 16:24:26 +02:00
|
|
|
|
initNix();
|
2015-03-19 20:16:38 +01:00
|
|
|
|
initGC();
|
2014-08-13 16:24:26 +02:00
|
|
|
|
|
|
|
|
|
Path releaseExpr;
|
2017-10-26 13:10:14 +02:00
|
|
|
|
|
|
|
|
|
struct MyArgs : LegacyArgs, MixEvalArgs
|
|
|
|
|
{
|
|
|
|
|
using LegacyArgs::LegacyArgs;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyArgs myArgs(baseNameOf(argv[0]), [&](Strings::iterator & arg, const Strings::iterator & end) {
|
|
|
|
|
if (*arg == "--gc-roots-dir")
|
2014-08-13 16:24:26 +02:00
|
|
|
|
gcRootsDir = getArg(*arg, arg, end);
|
2014-09-12 14:30:01 +02:00
|
|
|
|
else if (*arg == "--dry-run")
|
|
|
|
|
settings.readOnlyMode = true;
|
2014-08-13 16:24:26 +02:00
|
|
|
|
else if (*arg != "" && arg->at(0) == '-')
|
|
|
|
|
return false;
|
2010-05-18 09:57:37 +00:00
|
|
|
|
else
|
2017-11-28 16:51:00 +01:00
|
|
|
|
releaseExpr = *arg;
|
2014-08-13 16:24:26 +02:00
|
|
|
|
return true;
|
|
|
|
|
});
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
2017-10-26 13:10:14 +02:00
|
|
|
|
myArgs.parseCmdline(argvToStrings(argc, argv));
|
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
JSONObject json(std::cout, true);
|
|
|
|
|
std::cout.flush();
|
2017-03-20 12:57:05 -04:00
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
do {
|
2015-04-14 15:16:00 +02:00
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
Pipe pipe;
|
|
|
|
|
pipe.create();
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
ProcessOptions options;
|
|
|
|
|
options.allowVfork = false;
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2018-08-10 20:25:43 +02:00
|
|
|
|
GC_atfork_prepare();
|
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
auto pid = startProcess([&]() {
|
|
|
|
|
pipe.readSide = -1;
|
2017-10-26 13:10:14 +02:00
|
|
|
|
|
2018-08-10 20:25:43 +02:00
|
|
|
|
GC_atfork_child();
|
|
|
|
|
GC_start_mark_threads();
|
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
if (lastAttrPath != "") debug("resuming from '%s'", lastAttrPath);
|
2009-03-05 15:41:43 +00:00
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
/* FIXME: The build hook in conjunction with import-from-derivation is causing "unexpected EOF" during eval */
|
|
|
|
|
settings.builders = "";
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
/* Prevent access to paths outside of the Nix search path and
|
|
|
|
|
to the environment. */
|
2018-07-31 10:23:41 +02:00
|
|
|
|
evalSettings.restrictEval = true;
|
2018-06-05 12:18:55 +02:00
|
|
|
|
|
|
|
|
|
if (releaseExpr == "") throw UsageError("no expression specified");
|
|
|
|
|
|
|
|
|
|
if (gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
|
|
|
|
|
|
|
|
|
|
EvalState state(myArgs.searchPath, openStore());
|
|
|
|
|
|
|
|
|
|
Bindings & autoArgs = *myArgs.getAutoArgs(state);
|
|
|
|
|
|
|
|
|
|
Value v;
|
|
|
|
|
state.evalFile(lookupFileArg(state, releaseExpr), v);
|
|
|
|
|
|
|
|
|
|
comma = lastAttrPath != "";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
findJobs(state, json, autoArgs, v, "");
|
|
|
|
|
lastAttrPath = "";
|
|
|
|
|
} catch (BailOut &) { }
|
|
|
|
|
|
|
|
|
|
writeFull(pipe.writeSide.get(), lastAttrPath);
|
|
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
}, options);
|
|
|
|
|
|
2018-08-10 20:25:43 +02:00
|
|
|
|
GC_atfork_parent();
|
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
pipe.writeSide = -1;
|
|
|
|
|
|
|
|
|
|
int status;
|
|
|
|
|
while (true) {
|
|
|
|
|
checkInterrupt();
|
|
|
|
|
if (waitpid(pid, &status, 0) == pid) break;
|
|
|
|
|
if (errno != EINTR) continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
|
throw Exit(WIFEXITED(status) ? WEXITSTATUS(status) : 99);
|
2009-03-05 14:59:43 +00:00
|
|
|
|
|
2018-08-08 11:26:26 +02:00
|
|
|
|
maxHeapSize += 64 * 1024 * 1024;
|
2018-08-07 11:47:53 +02:00
|
|
|
|
|
2018-06-05 12:18:55 +02:00
|
|
|
|
lastAttrPath = drainFD(pipe.readSide.get());
|
|
|
|
|
} while (lastAttrPath != "");
|
2014-08-13 16:24:26 +02:00
|
|
|
|
});
|
|
|
|
|
}
|