295 : model_(model), opt_(
options), sn_(
lqn::lqn_finalize(model)) {
298 "SolverLQNS requires the lqns and lqsim commands on the system path.\n"
299 "Obtain them from their authors at: http://www.sce.carleton.ca/rads/lqns/\n"
300 "LINE ships no LQNS binary and does not redistribute one.\n\n"
301 "Alternatively, point LINE at a host that already runs LQNS by setting\n"
302 "LqnsOptions::remote and LqnsOptions::remote_url.");
303 const std::string m = opt_.method.empty() ? std::string(
"default") : opt_.method;
305 if (std::find(valid.begin(), valid.end(), m) == valid.end())
307 "' is not a method of this solver; it takes default, lqns, srvn, "
308 "exactmva, srvn.exactmva, sim, lqsim and lqns.default");
310 multiserver_pragma(opt_.multiserver);
328 std::vector<std::string> m;
330 if (name !=
"qns" && name.rfind(
"qns.", 0) != 0) {
339 return method ==
"sim" || method ==
"lqsim";
350 const char* names[] = {
"Sink",
"Source",
"Queue",
351 "Coxian",
"Erlang",
"Exp",
352 "HyperExp",
"Buffer",
"Server",
353 "JobSink",
"RandomSource",
"ServiceTunnel",
354 "SchedStrategy_PS",
"SchedStrategy_FCFS",
"ClosedClass"};
355 return std::vector<std::string>(names, names +
sizeof(names) /
sizeof(names[0]));
377 const std::clock_t t0 = std::clock();
378 const std::string dir = detail::make_temp_dir(
"lqns");
379 const std::string stem = dir +
"/model";
380 const std::string modelfile = stem +
".lqnx";
381 const std::string resultfile = stem +
".lqxo";
386 for (std::size_t i = 0; i < rep.
dropped.size(); ++i)
387 std::fprintf(stderr,
"SolverLQNS: %s\n", rep.
dropped[i].c_str());
389 if (opt_.remote) run_remote(modelfile, resultfile);
390 else run_local(modelfile, resultfile);
393 parse_lqxo(resultfile);
395 if (!opt_.keep) detail::remove_temp_dir(dir);
398 if (!opt_.keep) detail::remove_temp_dir(dir);
399 else if (opt_.verbose) std::fprintf(stderr,
"SolverLQNS: files kept in %s\n", dir.c_str());
400 runtime_ = double(std::clock() - t0) / double(CLOCKS_PER_SEC);
412 const std::size_t n = sn_.nidx;
415 auto fill = [&](std::vector<T>& v, std::vector<bool>& d,
const std::vector<double>& src) {
417 d.assign(n + 1,
false);
418 for (std::size_t i = 1; i <= n; ++i) {
419 if (std::isnan(src[i]))
continue;
430 for (std::size_t i = 1; i <= n; ++i)
456 void run_analyzer_once() {
461 static std::string multiserver_pragma(
const std::string& policy) {
462 if (policy.empty() || policy ==
"none")
return std::string();
463 if (policy ==
"default")
return "-Pmultiserver=rolia";
464 if (policy ==
"conway" || policy ==
"rolia" || policy ==
"zhou" || policy ==
"suri" ||
465 policy ==
"reiser" || policy ==
"schmidt")
466 return "-Pmultiserver=" + policy;
468 "' is not a multiserver policy of lqns; it takes conway, rolia, zhou, "
469 "suri, reiser, schmidt and default");
486 std::vector<std::string> build_argv(
const std::string& modelfile)
const {
488 std::vector<std::string> a;
489 a.push_back(sim ?
"lqsim" :
"lqns");
497 const std::string ms = multiserver_pragma(opt_.multiserver);
498 if (!ms.empty()) a.push_back(ms);
500 if (opt_.method ==
"srvn" || opt_.method ==
"srvn.exactmva") a.push_back(
"-Playering=srvn");
501 if (opt_.method ==
"exactmva" || opt_.method ==
"srvn.exactmva") a.push_back(
"-Pmva=exact-mva");
502 if (sim && opt_.samples > 0) {
504 std::snprintf(buf,
sizeof(buf),
"%.0f", opt_.samples);
511 if (opt_.method !=
"lqns.default") a.push_back(
"-Pstop-on-message-loss=false");
513 a.push_back(modelfile);
517 void run_local(
const std::string& modelfile,
const std::string& resultfile)
const {
518 const std::vector<std::string> argv = build_argv(modelfile);
521 for (std::size_t i = 0; i < argv.size(); ++i) cmd += (i ?
" " :
"") + argv[i];
522 std::fprintf(stderr,
"SolverLQNS command: %s\n", cmd.c_str());
525 const util::ProcResult r =
529 throw ExternalError(ErrorKind{
"LQNS"},
"SolverLQNS: " + argv[0] +
" did not finish within " +
530 std::to_string(opt_.timeout_seconds) +
"s and was killed");
532 throw UnsupportedError(ErrorKind{
"LQNS"},
"SolverLQNS: could not run '" + argv[0] +
533 "'; LINE ships no LQNS binary, so it must be installed and on "
535 if (opt_.verbose && !r.out.empty()) std::fprintf(stderr,
"%s", r.out.c_str());
538 if (r.exitCode != 0 || ::stat(resultfile.c_str(), &st) != 0) {
539 std::string out = r.out;
540 while (!out.empty() && (out.back() ==
'\n' || out.back() ==
' ')) out.pop_back();
541 throw ExternalError(ErrorKind{
"LQNS"},
"SolverLQNS: the LQNS binary (exit status " + std::to_string(r.exitCode) +
542 ") produced no results for this model. Its output was:\n" + out);
553 void run_remote(
const std::string& modelfile,
const std::string& resultfile)
const {
556 std::ifstream
in(modelfile.c_str());
557 if (!in)
throw ExternalError(ErrorKind{
"LQNS"},
"SolverLQNS: cannot read the model it just wrote: " + modelfile);
558 content.assign(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
564 while (!base.empty() && base[base.size() - 1] ==
'/') base.erase(base.size() - 1);
565 const std::string url = base + (sim ?
"/api/v1/solve/lqsim" :
"/api/v1/solve/lqns");
568 req[
"model"][
"content"] = content;
569 req[
"model"][
"base64"] =
false;
570 req[
"options"][
"include_raw_output"] =
true;
572 req[
"options"][
"blocks"] = 30;
573 if (opt_.samples > 0) req[
"options"][
"run_time"] = opt_.samples;
575 std::string ms = opt_.multiserver.empty() || opt_.multiserver ==
"default"
576 ? std::string(
"rolia")
578 req[
"options"][
"pragmas"][
"multiserver"] = ms;
579 req[
"options"][
"pragmas"][
"stop_on_message_loss"] =
false;
580 if (opt_.method ==
"srvn" || opt_.method ==
"srvn.exactmva")
581 req[
"options"][
"pragmas"][
"layering"] =
"srvn";
582 if (opt_.method ==
"exactmva" || opt_.method ==
"srvn.exactmva")
583 req[
"options"][
"pragmas"][
"mva"] =
"exact";
589 const int timeout_ms = timeout_s > 0 && timeout_s < 2000000 ? timeout_s * 1000 : 0;
593 }
catch (
const http::HttpError&) {
597 if (resp.status < 200 || resp.status >= 300)
598 throw ExternalError(ErrorKind{
"LQNS"},
"SolverLQNS: remote LQNS at " + url +
" returned HTTP " +
599 std::to_string(resp.status) +
": " + resp.body);
602 out = nlohmann::json::parse(resp.body);
603 }
catch (
const std::exception& e) {
604 throw ExternalError(ErrorKind{
"LQNS"}, std::string(
"SolverLQNS: remote LQNS returned a body that is not JSON: ") +
607 const std::string status = out.value(
"status", std::string());
608 if (status ==
"error" || status ==
"failed")
609 throw ExternalError(ErrorKind{
"LQNS"},
"SolverLQNS: remote solver returned an error: " +
610 out.value(
"error", std::string(
"unknown error")));
612 if (out.contains(
"raw_output") && out[
"raw_output"].is_object())
613 lqxo = out[
"raw_output"].value(
"lqxo", std::string());
615 throw ExternalError(ErrorKind{
"LQNS"},
"SolverLQNS: remote solver did not return an LQXO document");
616 std::ofstream f(resultfile.c_str());
617 if (!f)
throw ExternalError(ErrorKind{
"LQNS"},
"SolverLQNS: cannot write the remote result to " + resultfile);
635 std::size_t index_of(
const std::string& name,
lang::LqnElement kind)
const {
636 const std::map<std::pair<int, std::string>, std::size_t>::const_iterator it =
637 byname_.find(std::make_pair(
static_cast<int>(kind), name));
638 return it == byname_.end() ? 0 : it->second;
641 std::size_t call_index_of(
const std::string& name)
const {
642 const std::map<std::string, std::size_t>::const_iterator it = bycallname_.find(name);
643 return it == bycallname_.end() ? 0 : it->second;
646 void init_name_index() {
647 if (!byname_.empty())
return;
649 for (std::size_t i = 1; i <= sn_.nidx; ++i)
650 byname_.insert(std::make_pair(
651 std::make_pair(
static_cast<int>(sn_.type[i]), sn_.names[i]), i));
652 for (std::size_t c = 1; c <= sn_.ncalls; ++c)
653 bycallname_.insert(std::make_pair(sn_.callnames[c], c));
663 void parse_lqxo(
const std::string& resultfile) {
665 const std::size_t n = sn_.nidx;
666 const double nan = detail::nan_value();
668 raw_.util.assign(n + 1, nan);
669 raw_.phase1util.assign(n + 1, nan);
670 raw_.phase2util.assign(n + 1, nan);
671 raw_.phase1svct.assign(n + 1, nan);
672 raw_.phase2svct.assign(n + 1, nan);
673 raw_.tput.assign(n + 1, nan);
674 raw_.procwaiting.assign(n + 1, nan);
675 raw_.procutil.assign(n + 1, nan);
676 raw_.residvisit.assign(n + 1, nan);
677 raw_.residt.assign(n + 1, nan);
678 raw_.callwaiting.assign(sn_.ncalls + 1, nan);
680 std::unique_ptr<xml::Element> doc;
683 }
catch (
const Error&) {
685 "SolverLQNS: no readable result at " + resultfile +
686 "; the binary ran but wrote no .lqxo, which is what it does when it rejects the "
687 "model (run with verbose to see its diagnostics)");
690 for (
const xml::Element* sp : doc->by_tag(
"solver-params")) {
691 const std::vector<const xml::Element*> g = sp->by_tag(
"result-general");
692 if (!g.empty()) raw_.iterations =
static_cast<int>(detail::attr_num(g[0],
"iterations"));
695 for (
const xml::Element* pe : doc->by_tag(
"processor")) {
697 const std::vector<const xml::Element*> pr = pe->by_tag(
"result-processor");
698 if (pidx && !pr.empty()) raw_.procutil[pidx] = detail::attr_num(pr[0],
"utilization");
700 for (
const xml::Element* te : pe->by_tag(
"task")) {
702 const std::vector<const xml::Element*> tr = te->by_tag(
"result-task");
703 double task_tput = nan;
705 task_tput = detail::attr_num(tr[0],
"throughput");
707 raw_.util[tidx] = detail::attr_num(tr[0],
"utilization");
708 raw_.phase1util[tidx] = detail::attr_num(tr[0],
"phase1-utilization");
709 raw_.phase2util[tidx] = detail::attr_num(tr[0],
"phase2-utilization");
710 raw_.tput[tidx] = task_tput;
711 raw_.procutil[tidx] = detail::attr_num(tr[0],
"proc-utilization");
715 for (
const xml::Element* ee : te->by_tag(
"entry")) {
717 const std::vector<const xml::Element*> er = ee->by_tag(
"result-entry");
718 double entry_tput = nan;
720 entry_tput = detail::attr_num(er[0],
"throughput");
722 raw_.util[eidx] = detail::attr_num(er[0],
"utilization");
723 raw_.phase1util[eidx] = detail::attr_num(er[0],
"phase1-utilization");
724 raw_.phase2util[eidx] = detail::attr_num(er[0],
"phase2-utilization");
725 raw_.phase1svct[eidx] = detail::attr_num(er[0],
"phase1-service-time");
726 raw_.phase2svct[eidx] = detail::attr_num(er[0],
"phase2-service-time");
727 raw_.tput[eidx] = entry_tput;
728 raw_.procutil[eidx] = detail::attr_num(er[0],
"proc-utilization");
733 const std::vector<const xml::Element*> epa =
734 ee->by_tag(
"entry-phase-activities");
735 if (epa.empty())
continue;
736 for (
const xml::Element* ae : epa[0]->by_tag(
"activity")) {
737 read_activity(ae, entry_tput,
true);
741 const std::vector<const xml::Element*> tal = te->by_tag(
"task-activities");
742 if (tal.empty())
continue;
743 for (
const xml::Element* ae : tal[0]->by_tag(
"activity")) {
747 if (ae->parent != tal[0])
continue;
748 read_activity(ae, task_tput,
false);
753 aggregate_entry_procutil();
754 aggregate_entry_svct();
769 void compute_residt() {
770 const std::size_t n = sn_.nidx;
771 for (std::size_t a = 1; a <= sn_.nacts; ++a) {
772 const std::size_t aidx = sn_.ashift + a;
773 if (aidx > n || aidx >= sn_.parent.size())
continue;
774 const std::size_t tidx = sn_.parent[aidx];
775 if (tidx == 0 || tidx > n)
continue;
776 const double xa = raw_.tput[aidx];
777 const double xt = raw_.tput[tidx];
778 const double rv = raw_.residvisit[aidx];
779 if (std::isnan(rv) || std::isnan(xa) || std::isnan(xt))
continue;
782 for (std::size_t t = 1; t <= sn_.ntasks; ++t) {
783 const std::size_t tidx = sn_.tshift + t;
786 bool complete =
true;
787 for (std::size_t a = 1; a <= sn_.nacts; ++a) {
788 const std::size_t aidx = sn_.ashift + a;
789 if (aidx >= sn_.parent.size() || sn_.parent[aidx] != tidx)
continue;
791 if (std::isnan(raw_.residt[aidx])) {
795 sum += raw_.residt[aidx];
797 if (any && complete) raw_.residt[tidx] =
sum;
824 void aggregate_entry_svct() {
825 for (std::size_t e = 1; e <= sn_.nentries; ++e) {
826 const std::size_t eidx = sn_.eshift + e;
827 if (eidx >= sn_.actsof.size() || eidx >= raw_.phase1svct.size())
continue;
828 if (!std::isnan(raw_.phase1svct[eidx]))
continue;
829 const std::vector<std::size_t>& acts = sn_.actsof[eidx];
830 if (acts.empty())
continue;
832 for (std::size_t aidx : acts) {
833 if (aidx >= raw_.phase1svct.size() || std::isnan(raw_.phase1svct[aidx]) ||
834 raw_.phase1svct[aidx] != 0.0) {
839 if (all_zero) raw_.phase1svct[eidx] = 0.0;
856 void aggregate_entry_procutil() {
857 for (std::size_t e = 1; e <= sn_.nentries; ++e) {
858 const std::size_t eidx = sn_.eshift + e;
859 if (eidx >= sn_.actsof.size())
continue;
860 const std::vector<std::size_t>& acts = sn_.actsof[eidx];
861 if (acts.empty())
continue;
863 bool complete =
true;
864 for (std::size_t aidx : acts) {
865 if (aidx >= raw_.procutil.size() || std::isnan(raw_.procutil[aidx])) {
869 sum += raw_.procutil[aidx];
871 if (complete) raw_.procutil[eidx] =
sum;
884 void read_activity(
const xml::Element* ae,
double owner_tput,
bool phase_form) {
885 const std::string aname = ae->attr(
"name");
887 const std::vector<const xml::Element*> ar = ae->by_tag(
"result-activity");
888 if (aidx && !ar.empty()) {
889 const xml::Element* r = ar[0];
890 raw_.util[aidx] = detail::attr_num(r,
"utilization");
891 raw_.phase1svct[aidx] = detail::attr_num(r,
"service-time");
892 raw_.procwaiting[aidx] = detail::attr_num(r,
"proc-waiting");
899 const double pw = raw_.procwaiting[aidx];
900 const double st = raw_.phase1svct[aidx];
901 raw_.residvisit[aidx] = (pw < 0 || (!std::isnan(st) && pw > st))
902 ? detail::nan_value()
903 : detail::attr_num(ae,
"host-demand-mean") + pw;
904 const double t = detail::attr_num(r,
"throughput");
908 raw_.tput[aidx] = std::isnan(t) && phase_form ? owner_tput : t;
909 const double pu = detail::attr_num(r,
"proc-utilization");
910 if (!std::isnan(pu)) {
911 raw_.procutil[aidx] = pu;
912 }
else if (phase_form) {
913 const double hd = detail::attr_num(ae,
"host-demand-mean");
914 if (!std::isnan(owner_tput) && !std::isnan(hd)) raw_.procutil[aidx] = owner_tput * hd;
918 read_calls(ae, aname,
"synch-call",
"=>");
919 read_calls(ae, aname,
"asynch-call",
"->");
922 void read_calls(
const xml::Element* ae,
const std::string& aname,
const char* tag,
924 for (
const xml::Element* ce : ae->by_tag(tag)) {
925 const std::size_t cidx = call_index_of(aname + arrow + ce->attr(
"dest"));
926 const std::vector<const xml::Element*> cr = ce->by_tag(
"result-call");
927 if (cidx && !cr.empty()) raw_.callwaiting[cidx] = detail::attr_num(cr[0],
"waiting");
931 lqn::LqnModel<T> model_;
933 lqn::LqnStruct<T> sn_;
935 std::map<std::pair<int, std::string>, std::size_t> byname_;
936 std::map<std::string, std::size_t> bycallname_;
937 double runtime_ = 0.0;
938 bool solved_ =
false;