From 46f388300472cee4f2a8c80d275ee95ca9098510 Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 13:09:14 -0700 Subject: [PATCH 01/14] qmanager: add std::shared_ptr support Add better memory management into qmanager using a smart pointer (std::shared_ptr). Queue policy factory class: Return a shared_ptr object from the queue factory method. qmanager: Integrate the factory class change into the qmanager module code. Use shared_ptr to manage the main context for qmanager (qmanager_ctx_t) as well. Ctx will be freed right before module_main exists. Note that this didn't go all the way to convert the qmanager_new and qmanager_destroy into a constructor and destructor of qmanager_ctx_t keeping the code style consistent with other C-based modules. --- qmanager/modules/qmanager.cpp | 58 +++++++-------- qmanager/policies/queue_policy_factory.hpp | 5 +- .../policies/queue_policy_factory_impl.hpp | 72 +++++++++---------- 3 files changed, 68 insertions(+), 67 deletions(-) diff --git a/qmanager/modules/qmanager.cpp b/qmanager/modules/qmanager.cpp index 573afe7dd..c0b5c7ebf 100644 --- a/qmanager/modules/qmanager.cpp +++ b/qmanager/modules/qmanager.cpp @@ -55,7 +55,7 @@ struct qmanager_ctx_t { flux_t *h; qmanager_args_t args; schedutil_t *schedutil; - queue_policy_base_t *queue; + std::shared_ptr queue; }; @@ -65,7 +65,7 @@ struct qmanager_ctx_t { * * ******************************************************************************/ -static int post_sched_loop (qmanager_ctx_t *ctx) +static int post_sched_loop (std::shared_ptr &ctx) { int rc = -1; std::shared_ptr job = nullptr; @@ -106,7 +106,8 @@ extern "C" int jobmanager_hello_cb (flux_t *h, { int rc = -1; - qmanager_ctx_t *ctx = (qmanager_ctx_t *)arg; + std::shared_ptr ctx = nullptr; + ctx = *(static_cast *>(arg)); std::shared_ptr running_job = std::make_shared (job_state_kind_t:: RUNNING, id, uid, prio, ts, R); @@ -124,7 +125,8 @@ extern "C" int jobmanager_hello_cb (flux_t *h, extern "C" void jobmanager_alloc_cb (flux_t *h, const flux_msg_t *msg, const char *jobspec, void *arg) { - qmanager_ctx_t *ctx = (qmanager_ctx_t *)arg; + std::shared_ptr ctx = nullptr; + ctx = *(static_cast *>(arg)); std::shared_ptr job = std::make_shared (); if (schedutil_alloc_request_decode (msg, &job->id, &job->priority, @@ -150,7 +152,8 @@ extern "C" void jobmanager_free_cb (flux_t *h, const flux_msg_t *msg, const char *R, void *arg) { flux_jobid_t id; - qmanager_ctx_t *ctx = (qmanager_ctx_t *)arg; + std::shared_ptr ctx = nullptr; + ctx = *(static_cast *>(arg)); if (schedutil_free_request_decode (msg, &id) < 0) { flux_log_error (h, "%s: schedutil_free_request_decode", __FUNCTION__); @@ -176,19 +179,21 @@ extern "C" void jobmanager_free_cb (flux_t *h, const flux_msg_t *msg, } static void jobmanager_exception_cb (flux_t *h, flux_jobid_t id, - const char *t, int s, void *a) + const char *type, int severity, void *arg) { std::shared_ptr job; - qmanager_ctx_t *ctx = (qmanager_ctx_t *)a; + std::shared_ptr ctx = nullptr; + ctx = *(static_cast *>(arg)); - if (s > 0 || (job = ctx->queue->lookup (id)) == nullptr + if (severity > 0 || (job = ctx->queue->lookup (id)) == nullptr || !job->is_pending ()) return; if (ctx->queue->remove (id) < 0) { flux_log_error (h, "%s: remove job (%jd)", __FUNCTION__, (intmax_t)id); return; } - std::string note = std::string ("alloc aborted due to exception type=") + t; + std::string note = std::string ("alloc aborted due to exception type=") + + type; if (schedutil_alloc_respond_denied (ctx->schedutil, job->msg, note.c_str ()) < 0) { @@ -198,7 +203,8 @@ static void jobmanager_exception_cb (flux_t *h, flux_jobid_t id, flux_log (h, LOG_DEBUG, "%s (id=%jd)", note.c_str (), (intmax_t)id); } -static int process_args (qmanager_ctx_t *ctx, int argc, char **argv) +static int process_args (std::shared_ptr &ctx, + int argc, char **argv) { int rc = 0; qmanager_args_t &args = ctx->args; @@ -233,7 +239,7 @@ static void set_default_args (qmanager_args_t &args) args.policy_params = ""; } -static int handshake_jobmanager (qmanager_ctx_t *ctx) +static int handshake_jobmanager (std::shared_ptr &ctx) { int rc = -1; int queue_depth = 0; /* Not implemented in job-manager */ @@ -243,11 +249,11 @@ static int handshake_jobmanager (qmanager_ctx_t *ctx) jobmanager_alloc_cb, jobmanager_free_cb, jobmanager_exception_cb, - ctx))) { + &ctx))) { flux_log_error (ctx->h, "%s: schedutil_create", __FUNCTION__); goto out; } - if (schedutil_hello (ctx->schedutil, jobmanager_hello_cb, ctx) < 0) { + if (schedutil_hello (ctx->schedutil, jobmanager_hello_cb, &ctx) < 0) { flux_log_error (ctx->h, "%s: schedutil_hello", __FUNCTION__); goto out; } @@ -260,7 +266,7 @@ static int handshake_jobmanager (qmanager_ctx_t *ctx) return rc; } -static int enforce_queue_policy (qmanager_ctx_t *ctx) +static int enforce_queue_policy (std::shared_ptr &ctx) { int rc = -1; ctx->queue = create_queue_policy (ctx->args.queue_policy, "module"); @@ -291,23 +297,22 @@ static int enforce_queue_policy (qmanager_ctx_t *ctx) return rc; } -static qmanager_ctx_t *qmanager_new (flux_t *h) +static std::shared_ptr qmanager_new (flux_t *h) { - qmanager_ctx_t *ctx = NULL; - - if (!(ctx = new (std::nothrow) qmanager_ctx_t ())) { + std::shared_ptr ctx = nullptr; + try { + ctx = std::make_shared (); + ctx->h = h; + ctx->schedutil = NULL; + set_default_args (ctx->args); + } catch (std::bad_alloc &e) { errno = ENOMEM; - goto out; } - ctx->h = h; - ctx->schedutil = NULL; - set_default_args (ctx->args); -out: return ctx; } -static void qmanager_destroy (qmanager_ctx_t *ctx) +static void qmanager_destroy (std::shared_ptr &ctx) { if (ctx) { int saved_errno = errno; @@ -316,10 +321,7 @@ static void qmanager_destroy (qmanager_ctx_t *ctx) flux_respond_error (ctx->h, job->msg, ENOSYS, "unloading"); while ((job = ctx->queue->complete_pop ()) != nullptr) flux_respond_error (ctx->h, job->msg, ENOSYS, "unloading"); - delete ctx->queue; - ctx->queue = NULL; schedutil_destroy (ctx->schedutil); - delete (ctx); errno = saved_errno; } } @@ -335,7 +337,7 @@ extern "C" int mod_main (flux_t *h, int argc, char **argv) { int rc = -1; try { - qmanager_ctx_t *ctx = NULL; + std::shared_ptr ctx = nullptr; if (!(ctx = qmanager_new (h))) { flux_log_error (h, "%s: qmanager_new", __FUNCTION__); return rc; diff --git a/qmanager/policies/queue_policy_factory.hpp b/qmanager/policies/queue_policy_factory.hpp index 98777f3a2..d131a5443 100644 --- a/qmanager/policies/queue_policy_factory.hpp +++ b/qmanager/policies/queue_policy_factory.hpp @@ -25,13 +25,16 @@ #ifndef QUEUE_POLICY_FACTORY_HPP #define QUEUE_POLICY_FACTORY_HPP +#include #include namespace Flux { namespace queue_manager { bool known_queue_policy (const std::string &policy); -queue_policy_base_t *create_queue_policy (const std::string &policy); +std::shared_ptr create_queue_policy ( + const std::string &policy, + const std::string &reapi); } // namespace Flux::queue_manager } // namespace Flux diff --git a/qmanager/policies/queue_policy_factory_impl.hpp b/qmanager/policies/queue_policy_factory_impl.hpp index 3439e1057..777a09e4c 100644 --- a/qmanager/policies/queue_policy_factory_impl.hpp +++ b/qmanager/policies/queue_policy_factory_impl.hpp @@ -58,50 +58,46 @@ bool known_queue_policy (const std::string &policy) return rc; } -queue_policy_base_t *create_queue_policy (const std::string &policy, - const std::string &reapi) +std::shared_ptr create_queue_policy ( + const std::string &policy, + const std::string &reapi) { - queue_policy_base_t *p = NULL; - if (policy == "fcfs") { - if (reapi == "module") { - p = (queue_policy_base_t *) - new (std::nothrow)queue_policy_fcfs_t (); - } - else if (reapi == "cli") { - p = (queue_policy_base_t *) - new (std::nothrow)queue_policy_fcfs_t (); - } - } - else if (policy == "easy") { - if (reapi == "module") { - p = (queue_policy_base_t *) - new (std::nothrow)queue_policy_easy_t (); - } - else if (reapi == "cli") { - p = (queue_policy_base_t *) - new (std::nothrow)queue_policy_easy_t (); - } - } - else if (policy == "hybrid") { - if (reapi == "module") { - p = (queue_policy_base_t *) - new (std::nothrow)queue_policy_hybrid_t (); + std::shared_ptr p = nullptr; + + try { + if (policy == "fcfs") { + if (reapi == "module") + p = std::make_shared> (); + else if (reapi == "cli") + p = std::make_shared> (); } - else if (reapi == "cli") { - p = (queue_policy_base_t *) - new (std::nothrow)queue_policy_hybrid_t (); + else if (policy == "easy") { + if (reapi == "module") + p = std::make_shared> (); + else if (reapi == "cli") + p = std::make_shared> (); } - } - else if (policy == "conservative") { - if (reapi == "module") { - p = (queue_policy_base_t *) new (std::nothrow) - queue_policy_conservative_t (); + else if (policy == "hybrid") { + if (reapi == "module") + p = std::make_shared> (); + else if (reapi == "cli") + p = std::make_shared> (); } - else if (reapi == "cli") { - p = (queue_policy_base_t *) new (std::nothrow) - queue_policy_conservative_t (); + else if (policy == "conservative") { + if (reapi == "module") { + p = std::make_shared> (); + } + else if (reapi == "cli") { + p = std::make_shared> (); + } } + } catch (std::bad_alloc &e) { + errno = ENOMEM; + p = nullptr; } + return p; } From 1c045d29437ceba45b09019c3fb3c0e449d0f7d5 Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 13:16:50 -0700 Subject: [PATCH 02/14] jobspec: add const correctness to constructor Change it because the constructor shouldn't modify the input jobspec string. --- resource/libjobspec/jobspec.cpp | 2 +- resource/libjobspec/jobspec.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resource/libjobspec/jobspec.cpp b/resource/libjobspec/jobspec.cpp index a2bf816e8..9db5dc472 100644 --- a/resource/libjobspec/jobspec.cpp +++ b/resource/libjobspec/jobspec.cpp @@ -418,7 +418,7 @@ catch (YAML::Exception& e) { throw parse_error(e.what()); } -Jobspec::Jobspec(std::string &s) +Jobspec::Jobspec(const std::string &s) try : Jobspec {YAML::Load (s)} { diff --git a/resource/libjobspec/jobspec.hpp b/resource/libjobspec/jobspec.hpp index fa5afe68b..025dfb908 100644 --- a/resource/libjobspec/jobspec.hpp +++ b/resource/libjobspec/jobspec.hpp @@ -131,7 +131,7 @@ class Jobspec { Jobspec() = default; Jobspec(const YAML::Node&); Jobspec(std::istream &is); - Jobspec(std::string &s); + Jobspec(const std::string &s); }; std::ostream& operator<<(std::ostream& s, Jobspec const& js); From 791007672a0642ebd2aa863668f47c06cdbfd12b Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 13:27:20 -0700 Subject: [PATCH 03/14] reader: Add virtual destructors for reader classes Add virtual destructors as any class that derives from an abstract class should have them for a correct resource release sequence. While there is no harm done here as these reader classes currently have no resources to free, it is a good practice in case they are extended to include resource acquisition in the future. --- resource/readers/resource_reader_base.cpp | 5 +++++ resource/readers/resource_reader_base.hpp | 3 +++ resource/readers/resource_reader_grug.cpp | 5 +++++ resource/readers/resource_reader_grug.hpp | 2 ++ resource/readers/resource_reader_hwloc.cpp | 5 +++++ resource/readers/resource_reader_hwloc.hpp | 3 +++ resource/readers/resource_reader_jgf.cpp | 5 +++++ resource/readers/resource_reader_jgf.hpp | 3 +++ 8 files changed, 31 insertions(+) diff --git a/resource/readers/resource_reader_base.cpp b/resource/readers/resource_reader_base.cpp index 85dc32dcf..b38c2090c 100644 --- a/resource/readers/resource_reader_base.cpp +++ b/resource/readers/resource_reader_base.cpp @@ -54,6 +54,11 @@ bool resource_reader_base_t::in_whitelist (const std::string &resource) * * ********************************************************************************/ +resource_reader_base_t::~resource_reader_base_t () +{ + +} + int resource_reader_base_t::set_whitelist (const std::string &csl) { if (csl == "") diff --git a/resource/readers/resource_reader_base.hpp b/resource/readers/resource_reader_base.hpp index e134d2399..556cc600a 100644 --- a/resource/readers/resource_reader_base.hpp +++ b/resource/readers/resource_reader_base.hpp @@ -39,6 +39,9 @@ namespace resource_model { */ class resource_reader_base_t { public: + + virtual ~resource_reader_base_t (); + /*! Unpack str into a resource graph. * * \param g resource graph diff --git a/resource/readers/resource_reader_grug.cpp b/resource/readers/resource_reader_grug.cpp index 359e91a37..229bb6420 100644 --- a/resource/readers/resource_reader_grug.cpp +++ b/resource/readers/resource_reader_grug.cpp @@ -421,6 +421,11 @@ int dfs_emitter_t::get_rank () * * ********************************************************************************/ +resource_reader_grug_t::~resource_reader_grug_t () +{ + +} + int resource_reader_grug_t::unpack (resource_graph_t &g, resource_graph_metadata_t &m, const std::string &str, int rank) diff --git a/resource/readers/resource_reader_grug.hpp b/resource/readers/resource_reader_grug.hpp index 4e3fea1a4..06170c8f1 100644 --- a/resource/readers/resource_reader_grug.hpp +++ b/resource/readers/resource_reader_grug.hpp @@ -38,6 +38,8 @@ namespace resource_model { */ class resource_reader_grug_t : public resource_reader_base_t { public: + virtual ~resource_reader_grug_t (); + /*! Unpack str into a resource graph. * * \param g resource graph diff --git a/resource/readers/resource_reader_hwloc.cpp b/resource/readers/resource_reader_hwloc.cpp index f47b3029d..f9e8b7ce2 100644 --- a/resource/readers/resource_reader_hwloc.cpp +++ b/resource/readers/resource_reader_hwloc.cpp @@ -336,6 +336,11 @@ int resource_reader_hwloc_t::unpack_internal (resource_graph_t &g, * * ********************************************************************************/ +resource_reader_hwloc_t::~resource_reader_hwloc_t () +{ + +} + int resource_reader_hwloc_t::unpack (resource_graph_t &g, resource_graph_metadata_t &m, const std::string &str, int rank) diff --git a/resource/readers/resource_reader_hwloc.hpp b/resource/readers/resource_reader_hwloc.hpp index 59bb66177..ceadb6471 100644 --- a/resource/readers/resource_reader_hwloc.hpp +++ b/resource/readers/resource_reader_hwloc.hpp @@ -37,6 +37,9 @@ namespace resource_model { */ class resource_reader_hwloc_t : public resource_reader_base_t { public: + + virtual ~resource_reader_hwloc_t (); + /*! Unpack str into a resource graph. * * \param g resource graph diff --git a/resource/readers/resource_reader_jgf.cpp b/resource/readers/resource_reader_jgf.cpp index ef583f69e..47d8a3806 100644 --- a/resource/readers/resource_reader_jgf.cpp +++ b/resource/readers/resource_reader_jgf.cpp @@ -279,6 +279,11 @@ int resource_reader_jgf_t::unpack_edges (resource_graph_t &g, * * ********************************************************************************/ +resource_reader_jgf_t::~resource_reader_jgf_t () +{ + +} + int resource_reader_jgf_t::unpack (resource_graph_t &g, resource_graph_metadata_t &m, const std::string &str, int rank) diff --git a/resource/readers/resource_reader_jgf.hpp b/resource/readers/resource_reader_jgf.hpp index b07b6bba8..745ead90b 100644 --- a/resource/readers/resource_reader_jgf.hpp +++ b/resource/readers/resource_reader_jgf.hpp @@ -40,6 +40,9 @@ namespace resource_model { */ class resource_reader_jgf_t : public resource_reader_base_t { public: + + virtual ~resource_reader_jgf_t (); + /*! Unpack str into a resource graph. * * \param g resource graph From cd3a611a21a70ce2892a08f690611777c8cd0565 Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 13:35:31 -0700 Subject: [PATCH 04/14] resource: style changes Enforce 80 character per line limit. Add deeper indentations for conditional statements for readability. --- resource/modules/resource_match.cpp | 14 ++++++++------ resource/traversers/dfu_impl.cpp | 3 ++- resource/utilities/command.cpp | 8 +++++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/resource/modules/resource_match.cpp b/resource/modules/resource_match.cpp index 3b04c1765..3cd6bdfb4 100644 --- a/resource/modules/resource_match.cpp +++ b/resource/modules/resource_match.cpp @@ -120,8 +120,10 @@ static const struct flux_msg_handler_spec htab[] = { { FLUX_MSGTYPE_REQUEST, "resource.info", info_request_cb, 0}, { FLUX_MSGTYPE_REQUEST, "resource.stat", stat_request_cb, 0}, { FLUX_MSGTYPE_REQUEST, "resource.next_jobid", next_jobid_request_cb, 0}, - { FLUX_MSGTYPE_REQUEST, "resource.set_property", set_property_request_cb, 0}, - { FLUX_MSGTYPE_REQUEST, "resource.get_property", get_property_request_cb, 0}, + { FLUX_MSGTYPE_REQUEST, "resource.set_property", set_property_request_cb, + 0}, + { FLUX_MSGTYPE_REQUEST, "resource.get_property", get_property_request_cb, + 0}, FLUX_MSGHANDLER_TABLE_END }; @@ -880,8 +882,8 @@ static void set_property_request_cb (flux_t *h, flux_msg_handler_t *w, vtx_t v; if (flux_request_unpack (msg, NULL, "{s:s s:s}", - "sp_resource_path", &rp, - "sp_keyval", &kv) < 0) + "sp_resource_path", &rp, + "sp_keyval", &kv) < 0) goto error; resource_path = rp; @@ -941,8 +943,8 @@ static void get_property_request_cb (flux_t *h, flux_msg_handler_t *w, string resp_value = ""; if (flux_request_unpack (msg, NULL, "{s:s s:s}", - "gp_resource_path", &rp, - "gp_key", &gp_key) < 0) + "gp_resource_path", &rp, + "gp_key", &gp_key) < 0) goto error; resource_path = rp; diff --git a/resource/traversers/dfu_impl.cpp b/resource/traversers/dfu_impl.cpp index 8b8776ad0..7a441168d 100644 --- a/resource/traversers/dfu_impl.cpp +++ b/resource/traversers/dfu_impl.cpp @@ -1008,7 +1008,8 @@ int dfu_impl_t::prime_pruning_filter (const subsystem_t &s, vtx_t u, for (auto &aggr : dfv) accum_if (s, aggr.first, aggr.second, to_parent); - if (m_match->get_my_pruning_types (s, (*m_graph)[u].type, out_prune_types)) { + if (m_match->get_my_pruning_types (s, + (*m_graph)[u].type, out_prune_types)) { for (auto &type : out_prune_types) { types.push_back (type.c_str ()); if (dfv.find (type) != dfv.end ()) diff --git a/resource/utilities/command.cpp b/resource/utilities/command.cpp index b6bd3a697..2cc80d9a1 100644 --- a/resource/utilities/command.cpp +++ b/resource/utilities/command.cpp @@ -53,10 +53,12 @@ command_t commands[] = { { "set-property", "p", cmd_set_property, "Add a property to a resource: " "resource-query> set-property resource PROPERTY=VALUE" }, { "get-property", "g", cmd_get_property, "Get all properties of a resource: " -"resource-query> get-property resource" }, /* May need to be modified to get-property resource PROPERTY*/ +"resource-query> get-property resource" }, { "list", "l", cmd_list, "List all jobs: resource-query> list" }, - { "info", "i", cmd_info, "Print info on a jobid: resource-query> info jobid" }, - { "stat", "s", cmd_stat, "Print overall stats: resource-query> stat jobid" }, + { "info", "i", cmd_info, +"Print info on a jobid: resource-query> info jobid" }, + { "stat", "s", cmd_stat, + "Print overall stats: resource-query> stat jobid" }, { "cat", "a", cmd_cat, "Print jobspec file: resource-query> cat jobspec" }, { "help", "h", cmd_help, "Print help message: resource-query> help" }, { "quit", "q", cmd_quit, "Quit the session: resource-query> quit" }, From 87707f075aace9dbd2be3f511f27e3b12ca7eb2b Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 13:55:34 -0700 Subject: [PATCH 05/14] writer: use shared_ptr for better memory management --- resource/writers/match_writers.cpp | 4 +--- resource/writers/match_writers.hpp | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/resource/writers/match_writers.cpp b/resource/writers/match_writers.cpp index e75064841..0c8519475 100644 --- a/resource/writers/match_writers.cpp +++ b/resource/writers/match_writers.cpp @@ -195,9 +195,7 @@ rlite_match_writers_t::rlite_match_writers_t() { m_reducer["core"] = set (); m_reducer["gpu"] = set (); - // Note: GCC 4.8 doesn't support move constructor for stringstream - // we use a pointer type although it is not ideal. - m_gatherer["node"] = new stringstream (); + m_gatherer["node"] = std::make_shared (); } rlite_match_writers_t::~rlite_match_writers_t () diff --git a/resource/writers/match_writers.hpp b/resource/writers/match_writers.hpp index 73f4b2da6..1c72bc460 100644 --- a/resource/writers/match_writers.hpp +++ b/resource/writers/match_writers.hpp @@ -25,6 +25,7 @@ #ifndef MATCH_WRITERS_HPP #define MATCH_WRITERS_HPP +#include #include #include #include @@ -108,7 +109,7 @@ class rlite_match_writers_t : public match_writers_t private: bool m_reducer_set (); std::map> m_reducer; - std::map m_gatherer; + std::map> m_gatherer; std::stringstream m_out; }; From ded19b820e75ffd4412a2782441cd433616dd9bc Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 19:06:07 -0700 Subject: [PATCH 06/14] resource: integrate shared_ptr for direct dependencies Add smart pointer support for all of the components within the resource infrastructure which are used by both resource matching module and resource-query code. Provide better memory management. In particular, modify the traverser code to use shared pointers on a few key member variables: filtered graph, metadata and match policy callback members. The traverser does not solely "own" these objects (in fact they are being passed from outside), and this means using raw pointers requires a careful manual coordination between the owners of these objects. Sharing the ownership of these objects using std::shared_ptr should be safer and far more elegant. Include the following specific changes. (Note that the change span many files because of dependence of the requisite changes.) In the converted factor classes (DFU match policy factory class and match writer policy factory class), modify their create methods to return a shared_ptr object and add catch clauses to catch bad_alloc exceptions. Add a catch-all catch clause at the end of mod_main of the resource match module as std::make_shared essentially doesn't support nothrow, and this means a bad_alloc exception can bubble up to the broker code level leading to a broker crash. Change the roots member of the resource metadata class to be a std::shared_ptr type so that this can also be the "shared" with the traverser more easily as well. Modify resource match module and resource-query to use shared_ptr objects for their resource contexts instead of raw pointers. Adjust their initialization and finalize routines as well as how a context is pushed and popped with the broker through an interface like flux_aux_get (). --- resource/modules/resource_match.cpp | 241 ++++++++++-------- .../policies/dfu_match_policy_factory.cpp | 19 +- .../policies/dfu_match_policy_factory.hpp | 3 +- resource/readers/resource_reader_grug.cpp | 6 +- resource/readers/resource_reader_hwloc.cpp | 4 +- resource/readers/resource_reader_jgf.cpp | 2 +- resource/store/resource_graph_store.cpp | 7 +- resource/store/resource_graph_store.hpp | 4 +- resource/traversers/dfu.cpp | 32 ++- resource/traversers/dfu.hpp | 27 +- resource/traversers/dfu_impl.cpp | 36 +-- resource/traversers/dfu_impl.hpp | 41 +-- resource/utilities/command.cpp | 44 ++-- resource/utilities/command.hpp | 44 ++-- resource/utilities/resource-query.cpp | 142 +++++------ resource/writers/match_writers.cpp | 56 ++-- resource/writers/match_writers.hpp | 2 +- 17 files changed, 387 insertions(+), 323 deletions(-) diff --git a/resource/modules/resource_match.cpp b/resource/modules/resource_match.cpp index 3cd6bdfb4..4a0481293 100644 --- a/resource/modules/resource_match.cpp +++ b/resource/modules/resource_match.cpp @@ -72,20 +72,25 @@ struct match_perf_t { }; struct resource_ctx_t { + ~resource_ctx_t (); flux_t *h; /* Flux handle */ flux_msg_handler_t **handlers; /* Message handlers */ resource_args_t args; /* Module load options */ - dfu_match_cb_t *matcher; /* Match callback object */ - dfu_traverser_t *traverser; /* Graph traverser object */ + std::shared_ptr matcher; /* Match callback object */ + std::shared_ptr traverser; /* Graph traverser object */ resource_graph_db_t db; /* Resource graph data store */ - f_resource_graph_t *fgraph; /* Graph filtered by subsystems to use */ - match_writers_t *writers; /* Vertex/Edge writers for a match */ + std::shared_ptr fgraph; /* Filtered graph */ + std::shared_ptr writers; /* Vertex/Edge writers */ match_perf_t perf; /* Match performance stats */ - map jobs; /* Jobs table */ + map> jobs; /* Jobs table */ map allocations; /* Allocation table */ map reservations; /* Reservation table */ }; +resource_ctx_t::~resource_ctx_t () +{ + flux_msg_handler_delvec (handlers); +} /****************************************************************************** * * @@ -140,26 +145,6 @@ static double get_elapse_time (timeval &st, timeval &et) * * ******************************************************************************/ -static void freectx (void *arg) -{ - resource_ctx_t *ctx = (resource_ctx_t *)arg; - if (ctx) { - flux_msg_handler_delvec (ctx->handlers); - delete ctx->matcher; - delete ctx->traverser; - delete ctx->fgraph; - for (auto &kv : ctx->jobs) { - delete kv.second; /* job_info_t* type */ - ctx->jobs.erase (kv.first); - } - delete ctx->writers; - ctx->jobs.clear (); - ctx->allocations.clear (); - ctx->reservations.clear (); - delete ctx; - } -} - static void set_default_args (resource_args_t &args) { args.load_file = ""; @@ -172,12 +157,18 @@ static void set_default_args (resource_args_t &args) args.reserve_vtx_vec = 0; } -static resource_ctx_t *getctx (flux_t *h) +static std::shared_ptr getctx (flux_t *h) { - resource_ctx_t *ctx = (resource_ctx_t *)flux_aux_get (h, "resource"); + void *d = NULL; + std::shared_ptr ctx = nullptr; + + if ( (d = flux_aux_get (h, "resource")) != NULL) + ctx = *(static_cast *>(d)); if (!ctx) { - ctx = new (nothrow)resource_ctx_t; - if (!ctx) { + try { + ctx = std::make_shared (); + ctx->traverser = std::make_shared (); + } catch (std::bad_alloc &e) { errno = ENOMEM; goto done; } @@ -189,22 +180,17 @@ static resource_ctx_t *getctx (flux_t *h) ctx->perf.min = DBL_MAX; ctx->perf.max = 0.0f; ctx->perf.accum = 0.0f; - ctx->matcher = NULL; /* Cannot be allocated at this point */ - ctx->traverser = new (nothrow)dfu_traverser_t (); - if (!ctx->traverser) { - errno = ENOMEM; - goto done; - } - ctx->fgraph = NULL; /* Cannot be allocated at this point */ - ctx->writers = NULL; /* Cannot be allocated at this point */ - flux_aux_set (h, "resource", ctx, freectx); + ctx->matcher = nullptr; /* Cannot be allocated at this point */ + ctx->fgraph = nullptr; /* Cannot be allocated at this point */ + ctx->writers = nullptr; /* Cannot be allocated at this point */ } done: return ctx; } -static int process_args (resource_ctx_t *ctx, int argc, char **argv) +static int process_args (std::shared_ptr &ctx, + int argc, char **argv) { int rc = 0; resource_args_t &args = ctx->args; @@ -275,14 +261,15 @@ static int process_args (resource_ctx_t *ctx, int argc, char **argv) return rc; } -static resource_ctx_t *init_module (flux_t *h, int argc, char **argv) +static std::shared_ptr init_module (flux_t *h, + int argc, char **argv) { - resource_ctx_t *ctx = NULL; + std::shared_ptr ctx = nullptr; uint32_t rank = 1; if (!(ctx = getctx (h))) { flux_log (h, LOG_ERR, "can't allocate the context"); - goto error; + return nullptr; } if (flux_get_rank (h, &rank) < 0) { flux_log (h, LOG_ERR, "can't determine rank"); @@ -300,8 +287,7 @@ static resource_ctx_t *init_module (flux_t *h, int argc, char **argv) return ctx; error: - freectx (ctx); - return NULL; + return nullptr; } @@ -347,7 +333,7 @@ static json_t *get_string_blocking (flux_t *h, const char *key) return NULL; } -static int populate_resource_db_file (resource_ctx_t *ctx, +static int populate_resource_db_file (std::shared_ptr &ctx, std::shared_ptr rd) { int rc = -1; @@ -372,7 +358,7 @@ static int populate_resource_db_file (resource_ctx_t *ctx, return rc; } -static int populate_resource_db_kvs (resource_ctx_t *ctx, +static int populate_resource_db_kvs (std::shared_ptr &ctx, std::shared_ptr rd) { int n = -1; @@ -405,11 +391,11 @@ static int populate_resource_db_kvs (resource_ctx_t *ctx, goto done; } Jput (o); - if (db.metadata.roots.find ("containment") == db.metadata.roots.end ()) { flux_log (ctx->h, LOG_ERR, "cluster vertex is unavailable"); + if (db.metadata.roots->find ("containment") == db.metadata.roots->end ()) { goto done; } - v = db.metadata.roots["containment"]; + v = db.metadata.roots->at ("containment"); // For the rest of the ranks -- general case for (rank=1; rank < size; rank++) { @@ -432,7 +418,7 @@ static int populate_resource_db_kvs (resource_ctx_t *ctx, return rc; } -static int populate_resource_db (resource_ctx_t *ctx) +static int populate_resource_db (std::shared_ptr &ctx) { int rc = -1; double elapse; @@ -475,7 +461,7 @@ static int populate_resource_db (resource_ctx_t *ctx) return rc; } -static int select_subsystems (resource_ctx_t *ctx) +static int select_subsystems (std::shared_ptr &ctx) { /* * Format of match_subsystems @@ -514,44 +500,58 @@ static int select_subsystems (resource_ctx_t *ctx) return rc; } -static int init_resource_graph (resource_ctx_t *ctx) +static std::shared_ptr create_filtered_graph ( + std::shared_ptr< + resource_ctx_t> &ctx) +{ + std::shared_ptr fg = nullptr; + resource_graph_t &g = ctx->db.resource_graph; + + try { + // Set vertex and edge maps + vtx_infra_map_t vmap = get (&resource_pool_t::idata, g); + edg_infra_map_t emap = get (&resource_relation_t::idata, g); + + // Set vertex and edge filters based on subsystems to use + const multi_subsystemsS &filter = ctx->matcher->subsystemsS (); + subsystem_selector_t vtxsel (vmap, filter); + subsystem_selector_t edgsel (emap, filter); + fg = std::make_shared (g, edgsel, vtxsel); + } catch (std::bad_alloc &e) { + errno = ENOMEM; + fg = nullptr; + } + + return fg; +} + +static int init_resource_graph (std::shared_ptr &ctx) { int rc = 0; // Select the appropriate matcher based on CLI policy. - ctx->matcher = create_match_cb (ctx->args.match_policy); + if ( !(ctx->matcher = create_match_cb (ctx->args.match_policy))) { + flux_log (ctx->h, LOG_ERR, "%s: can't create match callback", + __FUNCTION__); + return -1; - if ((rc = populate_resource_db (ctx)) != 0) { flux_log (ctx->h, LOG_ERR, "can't populate graph resource database"); + } + if ( (rc = populate_resource_db (ctx)) != 0) { return rc; } - if ((rc = select_subsystems (ctx)) != 0) { flux_log (ctx->h, LOG_ERR, "error processing subsystems %s", ctx->args.match_subsystems.c_str ()); + if ( (rc = select_subsystems (ctx)) != 0) { return rc; } - - resource_graph_t &g = ctx->db.resource_graph; - - // Set vertex and edge maps - vtx_infra_map_t vmap = get (&resource_pool_t::idata, g); - edg_infra_map_t emap = get (&resource_relation_t::idata, g); - - // Set vertex and edge filters based on subsystems to use - const multi_subsystemsS &filter = ctx->matcher->subsystemsS (); - subsystem_selector_t vtxsel (vmap, filter); - subsystem_selector_t edgsel (emap, filter); - - // Create a filtered graph based on the filters - if (!(ctx->fgraph = new (nothrow)f_resource_graph_t (g, edgsel, vtxsel))) { - errno = ENOMEM; + if ( !(ctx->fgraph = create_filtered_graph (ctx))) return -1; - } // Create a writers object for matched vertices and edges match_format_t format = match_writers_factory_t:: get_writers_type (ctx->args.match_format); - if (!(ctx->writers = match_writers_factory_t::create (format))) + if ( !(ctx->writers = match_writers_factory_t::create (format))) return -1; if (ctx->args.prune_filters != "" @@ -564,8 +564,8 @@ static int init_resource_graph (resource_ctx_t *ctx) // Initialize the DFU traverser if (ctx->traverser->initialize (ctx->fgraph, - &(ctx->db.metadata.roots), ctx->matcher) < 0) { flux_log (ctx->h, LOG_ERR, "traverser initialization"); + ctx->db.metadata.roots, ctx->matcher) < 0) { return -1; } @@ -579,7 +579,8 @@ static int init_resource_graph (resource_ctx_t *ctx) * * ******************************************************************************/ -static void update_match_perf (resource_ctx_t *ctx, double elapse) +static void update_match_perf (std::shared_ptr &ctx, + double elapse) { ctx->perf.njobs++; ctx->perf.min = (ctx->perf.min > elapse)? elapse : ctx->perf.min; @@ -592,9 +593,10 @@ static inline string get_status_string (int64_t now, int64_t at) return (at == now)? "ALLOCATED" : "RESERVED"; } -static int track_schedule_info (resource_ctx_t *ctx, int64_t id, int64_t now, - int64_t at, string &jspec, stringstream &R, - double elapse) +static int track_schedule_info (std::shared_ptr &ctx, + int64_t id, int64_t now, int64_t at, + const std::string &jspec, + std::stringstream &R, double elapse) { job_lifecycle_t state = job_lifecycle_t::INIT; @@ -604,8 +606,10 @@ static int track_schedule_info (resource_ctx_t *ctx, int64_t id, int64_t now, } state = (at == now)? job_lifecycle_t::ALLOCATED : job_lifecycle_t::RESERVED; - if (!(ctx->jobs[id] = new ((nothrow))job_info_t (id, state, at, "", jspec, - R.str (), elapse))) { + try { + ctx->jobs[id] = std::make_shared (id, state, at, "", + jspec, R.str (), elapse); + } catch (std::bad_alloc &e) { errno = ENOMEM; return -1; } @@ -618,8 +622,8 @@ static int track_schedule_info (resource_ctx_t *ctx, int64_t id, int64_t now, return 0; } -static int run (resource_ctx_t *ctx, int64_t jobid, - const char *cmd, string jstr, int64_t *at) +static int run (std::shared_ptr &ctx, int64_t jobid, + const char *cmd, const std::string &jstr, int64_t *at) { int rc = 0; Flux::Jobspec::Jobspec j {jstr}; @@ -636,9 +640,9 @@ static int run (resource_ctx_t *ctx, int64_t jobid, return rc; } -static int run_match (resource_ctx_t *ctx, int64_t jobid, const char *cmd, - string jstr, int64_t *now, int64_t *at, double *ov, - stringstream &o) +static int run_match (std::shared_ptr &ctx, int64_t jobid, + const char *cmd, const std::string &jstr, int64_t *now, + int64_t *at, double *ov, stringstream &o) { int rc = 0; double elapse = 0.0f; @@ -675,12 +679,14 @@ static int run_match (resource_ctx_t *ctx, int64_t jobid, const char *cmd, return rc; } -static inline bool is_existent_jobid (const resource_ctx_t *ctx, uint64_t jobid) +static inline bool is_existent_jobid ( + const std::shared_ptr &ctx, + uint64_t jobid) { return (ctx->jobs.find (jobid) != ctx->jobs.end ())? true : false; } -static int run_remove (resource_ctx_t *ctx, int64_t jobid) +static int run_remove (std::shared_ptr &ctx, int64_t jobid) { int rc = -1; dfu_traverser_t &tr = *(ctx->traverser); @@ -694,7 +700,7 @@ static int run_remove (resource_ctx_t *ctx, int64_t jobid) // removed multiple times by the upper queuing layer // as part of providing advanced queueing policies // (e.g., conservative backfill). - job_info_t *info = ctx->jobs[jobid]; + std::shared_ptr info = ctx->jobs[jobid]; info->state = job_lifecycle_t::ERROR; } goto out; @@ -719,7 +725,7 @@ static void match_request_cb (flux_t *h, flux_msg_handler_t *w, const char *js_str = NULL; stringstream R; - resource_ctx_t *ctx = getctx ((flux_t *)arg); + std::shared_ptr ctx = getctx ((flux_t *)arg); if (flux_request_unpack (msg, NULL, "{s:s s:I s:s}", "cmd", &cmd, "jobid", &jobid, "jobspec", &js_str) < 0) goto error; @@ -754,7 +760,7 @@ static void match_request_cb (flux_t *h, flux_msg_handler_t *w, static void cancel_request_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { - resource_ctx_t *ctx = getctx ((flux_t *)arg); + std::shared_ptr ctx = getctx ((flux_t *)arg); int64_t jobid = -1; if (flux_request_unpack (msg, NULL, "{s:I}", "jobid", &jobid) < 0) @@ -787,9 +793,9 @@ static void cancel_request_cb (flux_t *h, flux_msg_handler_t *w, static void info_request_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { - resource_ctx_t *ctx = getctx ((flux_t *)arg); + std::shared_ptr ctx = getctx ((flux_t *)arg); int64_t jobid = -1; - job_info_t *info = NULL; + std::shared_ptr info = NULL; string status = ""; if (flux_request_unpack (msg, NULL, "{s:I}", "jobid", &jobid) < 0) @@ -819,7 +825,7 @@ static void info_request_cb (flux_t *h, flux_msg_handler_t *w, static void stat_request_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { - resource_ctx_t *ctx = getctx ((flux_t *)arg); + std::shared_ptr ctx = getctx ((flux_t *)arg); double avg = 0.0f; double min = 0.0f; @@ -838,7 +844,8 @@ static void stat_request_cb (flux_t *h, flux_msg_handler_t *w, flux_log_error (h, "%s", __FUNCTION__); } -static inline int64_t next_jobid (const std::map &m) +static inline int64_t next_jobid (const std::map> &m) { int64_t jobid = -1; if (m.empty ()) @@ -852,7 +859,7 @@ static inline int64_t next_jobid (const std::map &m) static void next_jobid_request_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { - resource_ctx_t *ctx = getctx ((flux_t *)arg); + std::shared_ptr ctx = getctx ((flux_t *)arg); int64_t jobid = -1; if ((jobid = next_jobid (ctx->jobs)) < 0) { @@ -876,7 +883,7 @@ static void set_property_request_cb (flux_t *h, flux_msg_handler_t *w, string resource_path = "", keyval = ""; string property_key = "", property_value = ""; size_t pos; - resource_ctx_t *ctx = getctx ((flux_t *)arg); + std::shared_ptr ctx = getctx ((flux_t *)arg); std::map::const_iterator it; std::pair::iterator, bool> ret; vtx_t v; @@ -932,11 +939,11 @@ static void set_property_request_cb (flux_t *h, flux_msg_handler_t *w, } static void get_property_request_cb (flux_t *h, flux_msg_handler_t *w, - const flux_msg_t *msg, void *arg) + const flux_msg_t *msg, void *arg) { const char *rp = NULL, *gp_key = NULL; string resource_path = "", property_key = ""; - resource_ctx_t *ctx = getctx ((flux_t *)arg); + std::shared_ptr ctx = getctx ((flux_t *)arg); std::map::const_iterator it; std::map::const_iterator p_it; vtx_t v; @@ -994,24 +1001,36 @@ static void get_property_request_cb (flux_t *h, flux_msg_handler_t *w, extern "C" int mod_main (flux_t *h, int argc, char **argv) { int rc = -1; - resource_ctx_t *ctx = NULL; - uint32_t rank = 1; + try { + std::shared_ptr ctx = nullptr; + uint32_t rank = 1; - if (!(ctx = init_module (h, argc, argv))) { - flux_log (h, LOG_ERR, "can't initialize resource module"); - goto done; - } - flux_log (h, LOG_DEBUG, "resource module starting..."); + if ( !(ctx = init_module (h, argc, argv))) { + flux_log (h, LOG_ERR, "%s: can't initialize resource module", + __FUNCTION__); + goto done; + } + // Because mod_main is always active, the following is safe. + flux_aux_set (h, "resource", &ctx, NULL); + flux_log (h, LOG_DEBUG, "%s: resource module starting", __FUNCTION__); + + if ( (rc = init_resource_graph (ctx)) != 0) { + flux_log (h, LOG_ERR, + "%s: can't initialize resource graph database", + __FUNCTION__); + goto done; + } + flux_log (h, LOG_DEBUG, "%s: resource graph database loaded", + __FUNCTION__); - if ((rc = init_resource_graph (ctx)) != 0) { - flux_log (h, LOG_ERR, "can't initialize resource graph database"); - goto done; + if (( rc = flux_reactor_run (flux_get_reactor (h), 0)) < 0) { + flux_log (h, LOG_ERR, "%s: flux_reactor_run: %s", + __FUNCTION__, strerror (errno)); + goto done; + } } - flux_log (h, LOG_INFO, "resource graph database loaded"); - - if ((rc = flux_reactor_run (flux_get_reactor (h), 0)) < 0) { - flux_log (h, LOG_ERR, "flux_reactor_run: %s", strerror (errno)); - goto done; + catch (std::exception &e) { + flux_log_error (h, "%s: %s", __FUNCTION__, e.what ()); } done: diff --git a/resource/policies/dfu_match_policy_factory.cpp b/resource/policies/dfu_match_policy_factory.cpp index b84c21f97..1ffcecaed 100644 --- a/resource/policies/dfu_match_policy_factory.cpp +++ b/resource/policies/dfu_match_policy_factory.cpp @@ -38,17 +38,24 @@ bool known_match_policy (const std::string &policy) return rc; } -dfu_match_cb_t *create_match_cb (const std::string &policy) +std::shared_ptr create_match_cb (const std::string &policy) { - dfu_match_cb_t *matcher = NULL; + std::shared_ptr matcher = nullptr; + + try { if (policy == HIGH_ID_FIRST) - matcher = (dfu_match_cb_t *)new high_first_t (); + matcher = std::make_shared (); else if (policy == LOW_ID_FIRST) - matcher = (dfu_match_cb_t *)new low_first_t (); + matcher = std::make_shared (); else if (policy == LOCALITY_AWARE) - matcher = (dfu_match_cb_t *)new greater_interval_first_t (); + matcher = std::make_shared (); else if (policy == VAR_AWARE) - matcher = (dfu_match_cb_t *)new var_aware_t (); + matcher = std::make_shared (); + } catch (std::bad_alloc &e) { + errno = ENOMEM; + matcher = nullptr; + } + return matcher; } diff --git a/resource/policies/dfu_match_policy_factory.hpp b/resource/policies/dfu_match_policy_factory.hpp index c34d47c47..5595fcee4 100644 --- a/resource/policies/dfu_match_policy_factory.hpp +++ b/resource/policies/dfu_match_policy_factory.hpp @@ -26,6 +26,7 @@ #define DFU_MATCH_POLICY_FACTORY_HPP #include +#include #include "resource/policies/base/dfu_match_cb.hpp" #include "resource/policies/dfu_match_high_id_first.hpp" #include "resource/policies/dfu_match_low_id_first.hpp" @@ -45,7 +46,7 @@ bool known_match_policy (const std::string &policy); /*! Factory method for creating a matching callback * object, representing a matching policy. */ -dfu_match_cb_t *create_match_cb (const std::string &policy); +std::shared_ptr create_match_cb (const std::string &policy); } // resource_model } // Flux diff --git a/resource/readers/resource_reader_grug.cpp b/resource/readers/resource_reader_grug.cpp index 229bb6420..21a244880 100644 --- a/resource/readers/resource_reader_grug.cpp +++ b/resource/readers/resource_reader_grug.cpp @@ -188,8 +188,8 @@ vtx_t dfs_emitter_t::emit_vertex (ggv_t u, gge_t e, const gg_t &recipe, resource_graph_t &g = *m_g_p; resource_graph_metadata_t &m = *m_gm_p; if (src_v == boost::graph_traits::null_vertex ()) - if (m.roots.find (recipe[u].subsystem) != m.roots.end ()) - return m.roots[recipe[u].subsystem]; + if (m.roots->find (recipe[u].subsystem) != m.roots->end ()) + return (*(m.roots))[recipe[u].subsystem]; vtx_t v = add_vertex (g);; string pref = ""; @@ -198,7 +198,7 @@ vtx_t dfs_emitter_t::emit_vertex (ggv_t u, gge_t e, const gg_t &recipe, if (src_v == boost::graph_traits::null_vertex ()) { // ROOT vertex of graph - m.roots[recipe[u].subsystem] = v; + (*(m.roots))[recipe[u].subsystem] = v; id = 0; } else { id = gen_id (e, recipe, i, sz, j); diff --git a/resource/readers/resource_reader_hwloc.cpp b/resource/readers/resource_reader_hwloc.cpp index f9e8b7ce2..74a9f3fb0 100644 --- a/resource/readers/resource_reader_hwloc.cpp +++ b/resource/readers/resource_reader_hwloc.cpp @@ -67,7 +67,7 @@ vtx_t resource_reader_hwloc_t::create_cluster_vertex ( graph_traits:: null_vertex (), 0, subsys, "cluster", "cluster", 1); - m.roots[subsys] = v; + (*(m.roots))[subsys] = v; return v; } @@ -267,7 +267,7 @@ void resource_reader_hwloc_t::walk_hwloc (resource_graph_t &g, // Create edge between parent/child if (parent == boost::graph_traits::null_vertex ()) { // is root - m.roots[subsys] = v; + (*(m.roots))[subsys] = v; } else { std::string relation = "contains"; std::string rev_relation = "in"; diff --git a/resource/readers/resource_reader_jgf.cpp b/resource/readers/resource_reader_jgf.cpp index 47d8a3806..cc202b766 100644 --- a/resource/readers/resource_reader_jgf.cpp +++ b/resource/readers/resource_reader_jgf.cpp @@ -153,7 +153,7 @@ int resource_reader_jgf_t::add_vtx (resource_graph_t &g, g[v].idata.member_of[kv.first] = "*"; m.by_path[kv.second] = v; if (std::count(kv.second.begin (), kv.second.end (), '/') == 1) - m.roots[kv.first] = v; + (*(m.roots))[kv.first] = v; } m.by_type[g[v].type].push_back (v); m.by_name[g[v].name].push_back (v); diff --git a/resource/store/resource_graph_store.cpp b/resource/store/resource_graph_store.cpp index 41b1b0775..51de7d7b0 100644 --- a/resource/store/resource_graph_store.cpp +++ b/resource/store/resource_graph_store.cpp @@ -28,9 +28,14 @@ using namespace Flux; using namespace Flux::resource_model; +resource_graph_metadata_t::resource_graph_metadata_t() +{ + roots = std::make_shared> (); +} + bool resource_graph_db_t::known_subsystem (const std::string &s) { - return (metadata.roots.find (s) != metadata.roots.end ())? true : false; + return (metadata.roots->find (s) != metadata.roots->end ())? true : false; } int resource_graph_db_t::load (const std::string &str, diff --git a/resource/store/resource_graph_store.hpp b/resource/store/resource_graph_store.hpp index 738cf5d13..4ff3c31aa 100644 --- a/resource/store/resource_graph_store.hpp +++ b/resource/store/resource_graph_store.hpp @@ -38,7 +38,9 @@ class resource_reader_base_t; * Adjacency_list graph, roots of this graph and various indexing. */ struct resource_graph_metadata_t { - std::map roots; + resource_graph_metadata_t (); + + std::shared_ptr> roots; std::map> by_type; std::map> by_name; std::map by_path; diff --git a/resource/traversers/dfu.cpp b/resource/traversers/dfu.cpp index cb0efeeaa..080a05288 100644 --- a/resource/traversers/dfu.cpp +++ b/resource/traversers/dfu.cpp @@ -120,8 +120,10 @@ dfu_traverser_t::dfu_traverser_t () } -dfu_traverser_t::dfu_traverser_t (f_resource_graph_t *g, dfu_match_cb_t *m, - map *roots) +dfu_traverser_t::dfu_traverser_t (std::shared_ptr g, + std::shared_ptr m, + std::shared_ptr> roots) : detail::dfu_impl_t (g, m, roots) { @@ -144,17 +146,20 @@ dfu_traverser_t::~dfu_traverser_t () } -const f_resource_graph_t *dfu_traverser_t::get_graph () const +const std::shared_ptr dfu_traverser_t:: + get_graph () const { return detail::dfu_impl_t::get_graph (); } -const map *dfu_traverser_t::get_roots () const +const std::shared_ptr> dfu_traverser_t::get_roots () const { return detail::dfu_impl_t::get_roots (); } -const dfu_match_cb_t *dfu_traverser_t::get_match_cb () const +const std::shared_ptr dfu_traverser_t:: + get_match_cb () const { return detail::dfu_impl_t::get_match_cb (); } @@ -164,17 +169,18 @@ const string &dfu_traverser_t::err_message () const return detail::dfu_impl_t::err_message (); } -void dfu_traverser_t::set_graph (f_resource_graph_t *g) +void dfu_traverser_t::set_graph (std::shared_ptr g) { detail::dfu_impl_t::set_graph (g); } -void dfu_traverser_t::set_roots (map *roots) +void dfu_traverser_t::set_roots (std::shared_ptr> roots) { detail::dfu_impl_t::set_roots (roots); } -void dfu_traverser_t::set_match_cb (dfu_match_cb_t *m) +void dfu_traverser_t::set_match_cb (std::shared_ptr m) { detail::dfu_impl_t::set_match_cb (m); } @@ -207,9 +213,10 @@ int dfu_traverser_t::initialize () return rc; } -int dfu_traverser_t::initialize (f_resource_graph_t *g, - map *roots, - dfu_match_cb_t *m) +int dfu_traverser_t::initialize (std::shared_ptr g, + std::shared_ptr> roots, + std::shared_ptr m) { set_graph (g); set_roots (roots); @@ -217,7 +224,8 @@ int dfu_traverser_t::initialize (f_resource_graph_t *g, return initialize (); } -int dfu_traverser_t::run (Jobspec::Jobspec &jobspec, match_writers_t *writers, +int dfu_traverser_t::run (Jobspec::Jobspec &jobspec, + std::shared_ptr writers, match_op_t op, int64_t jobid, int64_t *at) { const subsystem_t &dom = get_match_cb ()->dom_subsystem (); diff --git a/resource/traversers/dfu.hpp b/resource/traversers/dfu.hpp index d08f7298e..4b6bbb8ce 100644 --- a/resource/traversers/dfu.hpp +++ b/resource/traversers/dfu.hpp @@ -41,20 +41,23 @@ class dfu_traverser_t : protected detail::dfu_impl_t { public: dfu_traverser_t (); - dfu_traverser_t (f_resource_graph_t *g, - dfu_match_cb_t *m, std::map *roots); + dfu_traverser_t (std::shared_ptr g, + std::shared_ptr m, + std::shared_ptr> roots); dfu_traverser_t (const dfu_traverser_t &o); + dfu_traverser_t (dfu_traverser_t &&o) = default; dfu_traverser_t &operator= (const dfu_traverser_t &o); + dfu_traverser_t &operator= (dfu_traverser_t &&o) = default; ~dfu_traverser_t (); - const f_resource_graph_t *get_graph () const; - const std::map *get_roots () const; - const dfu_match_cb_t *get_match_cb () const; + const std::shared_ptr get_graph () const; + const std::shared_ptr> get_roots () const; + const std::shared_ptr get_match_cb () const; const std::string &err_message () const; - void set_graph (f_resource_graph_t *g); - void set_roots (std::map *roots); - void set_match_cb (dfu_match_cb_t *m); + void set_graph (std::shared_ptr g); + void set_roots (std::shared_ptr> roots); + void set_match_cb (std::shared_ptr m); void clear_err_message (); /*! Prime the resource graph with subtree plans. Assume that resource graph, @@ -90,8 +93,9 @@ class dfu_traverser_t : protected detail::dfu_impl_t * ENOTSUP: roots does not contain a subsystem * the match callback uses. */ - int initialize (f_resource_graph_t *g, std::map *roots, - dfu_match_cb_t *m); + int initialize (std::shared_ptr g, + std::shared_ptr> roots, + std::shared_ptr m); /*! Begin a graph traversal for the jobspec and either allocate or * reserve the resources in the resource graph. Best-matching resources @@ -114,7 +118,8 @@ class dfu_traverser_t : protected detail::dfu_impl_t * ENODEV: unsatifiable jobspec becuase no * resources/devices can satisfy the request. */ - int run (Jobspec::Jobspec &jobspec, match_writers_t *writers, + int run (Jobspec::Jobspec &jobspec, + std::shared_ptr writers, match_op_t op, int64_t id, int64_t *at); /*! Remove the allocation/reservation referred to by jobid and update diff --git a/resource/traversers/dfu_impl.cpp b/resource/traversers/dfu_impl.cpp index 7a441168d..699ea8ceb 100644 --- a/resource/traversers/dfu_impl.cpp +++ b/resource/traversers/dfu_impl.cpp @@ -640,14 +640,14 @@ int dfu_impl_t::enforce (const subsystem_t &subsystem, scoring_api_t &dfu) return rc; } -int dfu_impl_t::emit_vtx (vtx_t u, match_writers_t *w, unsigned needs, - bool exclusive) +int dfu_impl_t::emit_vtx (vtx_t u, std::shared_ptr w, + unsigned needs, bool exclusive) { w->emit_vtx (level (), (*m_graph), u, needs, exclusive); return 0; } -int dfu_impl_t::emit_edg (edg_t e, match_writers_t *w) +int dfu_impl_t::emit_edg (edg_t e, std::shared_ptr w) { w->emit_edg (level (), (*m_graph), e); return 0; @@ -683,7 +683,7 @@ int dfu_impl_t::upd_plan (vtx_t u, const subsystem_t &s, unsigned int needs, return (span == -1)? -1 : 0; } -int dfu_impl_t::upd_sched (vtx_t u, match_writers_t *writers, +int dfu_impl_t::upd_sched (vtx_t u, std::shared_ptr writers, const subsystem_t &s, unsigned int needs, bool excl, int n, const jobmeta_t &meta, map &dfu, @@ -735,9 +735,9 @@ int dfu_impl_t::upd_upv (vtx_t u, const subsystem_t &subsystem, return 0; } -int dfu_impl_t::upd_dfv (vtx_t u, match_writers_t *writers, unsigned int needs, - bool excl, const jobmeta_t &meta, - map &to_parent) +int dfu_impl_t::upd_dfv (vtx_t u, std::shared_ptr writers, + unsigned int needs, bool excl, const jobmeta_t &meta, + std::map &to_parent) { int n_plans = 0; map dfu; @@ -914,8 +914,9 @@ dfu_impl_t::dfu_impl_t () } -dfu_impl_t::dfu_impl_t (f_resource_graph_t *g, dfu_match_cb_t *m, - map *roots) +dfu_impl_t::dfu_impl_t (std::shared_ptr g, + std::shared_ptr m, + std::shared_ptr> roots) : m_roots (roots), m_graph (g), m_match (m) { @@ -949,17 +950,18 @@ dfu_impl_t::~dfu_impl_t () } -const f_resource_graph_t *dfu_impl_t::get_graph () const +const std::shared_ptr dfu_impl_t::get_graph () const { return m_graph; } -const map *dfu_impl_t::get_roots () const +const std::shared_ptr> dfu_impl_t::get_roots () const { return m_roots; } -const dfu_match_cb_t *dfu_impl_t::get_match_cb () const +const std::shared_ptr dfu_impl_t::get_match_cb () const { return m_match; } @@ -969,17 +971,17 @@ const string &dfu_impl_t::err_message () const return m_err_msg; } -void dfu_impl_t::set_graph (f_resource_graph_t *g) +void dfu_impl_t::set_graph (std::shared_ptr g) { m_graph = g; } -void dfu_impl_t::set_roots (map *roots) +void dfu_impl_t::set_roots (std::shared_ptr> roots) { m_roots = roots; } -void dfu_impl_t::set_match_cb (dfu_match_cb_t *m) +void dfu_impl_t::set_match_cb (std::shared_ptr m) { m_match = m; } @@ -1072,8 +1074,8 @@ int dfu_impl_t::select (Jobspec::Jobspec &j, vtx_t root, jobmeta_t &meta, return rc; } -int dfu_impl_t::update (vtx_t root, match_writers_t *writers, jobmeta_t &meta, - unsigned int needs, bool exclusive) +int dfu_impl_t::update (vtx_t root, std::shared_ptr writers, + jobmeta_t &meta, unsigned int needs, bool exclusive) { map dfu; m_color.reset (); diff --git a/resource/traversers/dfu_impl.hpp b/resource/traversers/dfu_impl.hpp index 01d6a15a3..58ae4be51 100644 --- a/resource/traversers/dfu_impl.hpp +++ b/resource/traversers/dfu_impl.hpp @@ -29,6 +29,7 @@ #include #include #include +#include #include "resource/libjobspec/jobspec.hpp" #include "resource/config/system_defaults.hpp" #include "resource/schema/resource_data.hpp" @@ -51,7 +52,6 @@ enum class match_kind_t { RESOURCE_MATCH, struct jobmeta_t { bool allocate = true; - bool never_matched = true; int64_t jobid = -1; int64_t at = -1; uint64_t duration = SYSTEM_DEFAULT_DURATION; // will need config ultimately @@ -74,21 +74,24 @@ struct jobmeta_t { class dfu_impl_t { public: dfu_impl_t (); - dfu_impl_t (f_resource_graph_t *g, dfu_match_cb_t *m, - std::map *roots); + dfu_impl_t (std::shared_ptr g, + std::shared_ptr m, + std::shared_ptr> roots); dfu_impl_t (const dfu_impl_t &o); + dfu_impl_t (dfu_impl_t &&o) = default; dfu_impl_t &operator= (const dfu_impl_t &o); + dfu_impl_t &operator= (dfu_impl_t &&o) = default; ~dfu_impl_t (); //! Accessors - const f_resource_graph_t *get_graph () const; - const std::map *get_roots () const; - const dfu_match_cb_t *get_match_cb () const; + const std::shared_ptr get_graph () const; + const std::shared_ptr> get_roots () const; + const std::shared_ptr get_match_cb () const; const std::string &err_message () const; - void set_graph (f_resource_graph_t *g); - void set_roots (std::map *roots); - void set_match_cb (dfu_match_cb_t *m); + void set_graph (std::shared_ptr g); + void set_roots (std::shared_ptr> roots); + void set_match_cb (std::shared_ptr m); void clear_err_message (); /*! Exclusive request? Return true if a resource in resources vector @@ -187,7 +190,7 @@ class dfu_impl_t { * \return 0 on success; -1 on error -- call err_message () * for detail. */ - int update (vtx_t root, match_writers_t *writers, + int update (vtx_t root, std::shared_ptr writers, jobmeta_t &meta, unsigned int needs, bool excl); /*! Update to make the resource state ready for the next selection. @@ -273,15 +276,15 @@ class dfu_impl_t { bool *excl, scoring_api_t &to_parent); // Emit matched resource set - int emit_vtx (vtx_t u, match_writers_t *w, unsigned int needs, - bool exclusive); - int emit_edg (edg_t e, match_writers_t *w); + int emit_vtx (vtx_t u, std::shared_ptr w, + unsigned int needs, bool exclusive); + int emit_edg (edg_t e, std::shared_ptr w); // Update resource graph data store int upd_plan (vtx_t u, const subsystem_t &s, unsigned int needs, bool excl, const jobmeta_t &meta, int &n_p, std::map &to_parent); - int upd_sched (vtx_t u, match_writers_t *writers, + int upd_sched (vtx_t u, std::shared_ptr writers, const subsystem_t &subsystem, unsigned int needs, bool excl, int n, const jobmeta_t &meta, std::map &dfu, @@ -289,8 +292,8 @@ class dfu_impl_t { int upd_upv (vtx_t u, const subsystem_t &subsystem, unsigned int needs, bool excl, const jobmeta_t &meta, std::map &to_parent); - int upd_dfv (vtx_t u, match_writers_t *writers, unsigned int needs, - bool excl, const jobmeta_t &meta, + int upd_dfv (vtx_t u, std::shared_ptr writers, + unsigned int needs, bool excl, const jobmeta_t &meta, std::map &to_parent); // Remove allocation or reservations @@ -310,9 +313,9 @@ class dfu_impl_t { color_t m_color; uint64_t m_best_k_cnt = 0; unsigned int m_trav_level = 0; - std::map *m_roots = NULL; - f_resource_graph_t *m_graph = NULL; - dfu_match_cb_t *m_match = NULL; + std::shared_ptr> m_roots = nullptr; + std::shared_ptr m_graph = nullptr; + std::shared_ptr m_match = nullptr; std::string m_err_msg = ""; }; // the end of class dfu_impl_t diff --git a/resource/utilities/command.cpp b/resource/utilities/command.cpp index 2cc80d9a1..71cbfe665 100644 --- a/resource/utilities/command.cpp +++ b/resource/utilities/command.cpp @@ -65,12 +65,12 @@ command_t commands[] = { { "NA", "NA", (cmd_func_f *)NULL, "NA" } }; -static int do_remove (resource_context_t *ctx, int64_t jobid) +static int do_remove (std::shared_ptr &ctx, int64_t jobid) { int rc = -1; if ((rc = ctx->traverser->remove ((int64_t)jobid)) == 0) { if (ctx->jobs.find (jobid) != ctx->jobs.end ()) { - job_info_t *info = ctx->jobs[jobid]; + std::shared_ptr info = ctx->jobs[jobid]; info->state = job_lifecycle_t::CANCELLED; } } else { @@ -80,9 +80,9 @@ static int do_remove (resource_context_t *ctx, int64_t jobid) return rc; } -static void print_schedule_info (resource_context_t *ctx, ostream &out, - uint64_t jobid, string &jobspec_fn, - bool matched, int64_t at, +static void print_schedule_info (std::shared_ptr &ctx, + ostream &out, uint64_t jobid, + string &jobspec_fn, bool matched, int64_t at, double elapse, bool sat) { if (matched) { @@ -98,8 +98,9 @@ static void print_schedule_info (resource_context_t *ctx, ostream &out, out << "INFO:" << " =============================" << endl; st = (at == 0)? job_lifecycle_t::ALLOCATED : job_lifecycle_t::RESERVED; - ctx->jobs[jobid] = new job_info_t (jobid, st, - at, jobspec_fn, "", elapse); + ctx->jobs[jobid] = std::make_shared (jobid, st, at, + jobspec_fn, + "", elapse); if (at == 0) ctx->allocations[jobid] = jobid; else @@ -117,7 +118,8 @@ static void print_schedule_info (resource_context_t *ctx, ostream &out, ctx->jobid_counter++; } -static void update_match_perf (resource_context_t *ctx, double elapse) +static void update_match_perf (std::shared_ptr &ctx, + double elapse) { ctx->perf.min = (ctx->perf.min > elapse)? elapse : ctx->perf.min; ctx->perf.max = (ctx->perf.max < elapse)? elapse : ctx->perf.max; @@ -131,7 +133,7 @@ double get_elapse_time (timeval &st, timeval &et) return ts2 - ts1; } -int cmd_match (resource_context_t *ctx, vector &args) +int cmd_match (std::shared_ptr &ctx, vector &args) { if (args.size () != 3) { cerr << "ERROR: malformed command" << endl; @@ -197,7 +199,7 @@ int cmd_match (resource_context_t *ctx, vector &args) return 0; } -int cmd_cancel (resource_context_t *ctx, vector &args) +int cmd_cancel (std::shared_ptr &ctx, vector &args) { if (args.size () != 2) { cerr << "ERROR: malformed command" << endl; @@ -228,7 +230,8 @@ int cmd_cancel (resource_context_t *ctx, vector &args) return 0; } -int cmd_set_property (resource_context_t *ctx, std::vector &args) +int cmd_set_property (std::shared_ptr &ctx, + std::vector &args) { if (args.size () != 3) { cerr << "ERROR: malformed command" << endl; @@ -275,7 +278,8 @@ int cmd_set_property (resource_context_t *ctx, std::vector &args) return 0; } -int cmd_get_property (resource_context_t *ctx, std::vector &args) +int cmd_get_property (std::shared_ptr &ctx, + std::vector &args) { if (args.size () != 2) { cerr << "ERROR: malformed command" << endl; @@ -308,10 +312,10 @@ int cmd_get_property (resource_context_t *ctx, std::vector &args) return 0; } -int cmd_list (resource_context_t *ctx, vector &args) +int cmd_list (std::shared_ptr &ctx, vector &args) { for (auto &kv: ctx->jobs) { - job_info_t *info = kv.second; + std::shared_ptr info = kv.second; string mode; get_jobstate_str (info->state, mode); cout << "INFO: " << info->jobid << ", " << mode << ", " @@ -321,7 +325,7 @@ int cmd_list (resource_context_t *ctx, vector &args) return 0; } -int cmd_info (resource_context_t *ctx, vector &args) +int cmd_info (std::shared_ptr &ctx, vector &args) { if (args.size () != 2) { cerr << "ERROR: malformed command" << endl; @@ -333,7 +337,7 @@ int cmd_info (resource_context_t *ctx, vector &args) return 0; } string mode; - job_info_t *info = ctx->jobs[jobid]; + std::shared_ptr info = ctx->jobs[jobid]; get_jobstate_str (info->state, mode); cout << "INFO: " << info->jobid << ", " << mode << ", " << info->scheduled_at << ", " << info->jobspec_fn << ", " @@ -341,7 +345,7 @@ int cmd_info (resource_context_t *ctx, vector &args) return 0; } -int cmd_stat (resource_context_t *ctx, vector &args) +int cmd_stat (std::shared_ptr &ctx, vector &args) { if (args.size () != 1) { cerr << "ERROR: malformed command" << endl; @@ -361,7 +365,7 @@ int cmd_stat (resource_context_t *ctx, vector &args) return 0; } -int cmd_cat (resource_context_t *ctx, vector &args) +int cmd_cat (std::shared_ptr &ctx, vector &args) { string &jspec_filename = args[1]; ifstream jspec_in; @@ -374,7 +378,7 @@ int cmd_cat (resource_context_t *ctx, vector &args) return 0; } -int cmd_help (resource_context_t *ctx, vector &args) +int cmd_help (std::shared_ptr &ctx, vector &args) { bool multi = true; bool found = false; @@ -398,7 +402,7 @@ int cmd_help (resource_context_t *ctx, vector &args) return 0; } -int cmd_quit (resource_context_t *ctx, vector &args) +int cmd_quit (std::shared_ptr &ctx, vector &args) { return -1; } diff --git a/resource/utilities/command.hpp b/resource/utilities/command.hpp index b3bbbbdc9..326836433 100644 --- a/resource/utilities/command.hpp +++ b/resource/utilities/command.hpp @@ -30,6 +30,7 @@ #include "resource/readers/resource_reader_factory.hpp" #include "resource/traversers/dfu.hpp" #include "resource/jobinfo/jobinfo.hpp" +#include #include #include #include @@ -65,30 +66,41 @@ struct match_perf_t { struct resource_context_t { test_params_t params; /* Parameters for resource-query */ uint64_t jobid_counter; /* Hold the current jobid value */ - dfu_match_cb_t *matcher; /* Match callback object */ - dfu_traverser_t *traverser; /* Graph traverser object */ + std::shared_ptr matcher; /* Match callback object */ + std::shared_ptr traverser; /* Graph traverser object */ resource_graph_db_t db; /* Resource graph data store */ - f_resource_graph_t *fgraph; /* Graph filtered by subsystems to use */ - match_writers_t *writers; /* Vertex/Edge writers for a match */ + std::shared_ptr fgraph; /* Filtered graph */ + std::shared_ptr writers; /* Vertex/Edge writers */ match_perf_t perf; /* Match performance stats */ - std::map jobs; /* Jobs table */ + std::map> jobs; /* Jobs table */ std::map allocations; /* Allocation table */ std::map reservations; /* Reservation table */ }; -typedef int cmd_func_f (resource_context_t *, std::vector &); +typedef int cmd_func_f (std::shared_ptr &, + std::vector &); cmd_func_f *find_cmd (const std::string &cmd_str); -int cmd_match (resource_context_t *ctx, std::vector &args); -int cmd_cancel (resource_context_t *ctx, std::vector &args); -int cmd_set_property (resource_context_t *ctx, std::vector &args); -int cmd_get_property (resource_context_t *ctx, std::vector &args); -int cmd_list (resource_context_t *ctx, std::vector &args); -int cmd_info (resource_context_t *ctx, std::vector &args); -int cmd_stat (resource_context_t *ctx, std::vector &args); -int cmd_cat (resource_context_t *ctx, std::vector &args); -int cmd_quit (resource_context_t *ctx, std::vector &args); -int cmd_help (resource_context_t *ctx, std::vector &args); +int cmd_match (std::shared_ptr &ctx, + std::vector &args); +int cmd_cancel (std::shared_ptr &ctx, + std::vector &args); +int cmd_set_property (std::shared_ptr &ctx, + std::vector &args); +int cmd_get_property (std::shared_ptr &ctx, + std::vector &args); +int cmd_list (std::shared_ptr &ctx, + std::vector &args); +int cmd_info (std::shared_ptr &ctx, + std::vector &args); +int cmd_stat (std::shared_ptr &ctx, + std::vector &args); +int cmd_cat (std::shared_ptr &ctx, + std::vector &args); +int cmd_quit (std::shared_ptr &ctx, + std::vector &args); +int cmd_help (std::shared_ptr &ctx, + std::vector &args); double get_elapse_time (timeval &st, timeval &et); } // namespace resource_model diff --git a/resource/utilities/resource-query.cpp b/resource/utilities/resource-query.cpp index c3e2e2024..b5985197e 100644 --- a/resource/utilities/resource-query.cpp +++ b/resource/utilities/resource-query.cpp @@ -31,15 +31,13 @@ #include #include #include +#include #include #include #include #include "resource/utilities/command.hpp" #include "resource/store/resource_graph_store.hpp" -#include "resource/policies/dfu_match_high_id_first.hpp" -#include "resource/policies/dfu_match_low_id_first.hpp" -#include "resource/policies/dfu_match_locality.hpp" -#include "resource/policies/dfu_match_var_aware.hpp" +#include "resource/policies/dfu_match_policy_factory.hpp" extern "C" { #if HAVE_CONFIG_H @@ -193,22 +191,7 @@ static void usage (int code) exit (code); } -static dfu_match_cb_t *create_match_cb (const string &policy) -{ - dfu_match_cb_t *matcher = NULL; - if (policy == "high") - matcher = (dfu_match_cb_t *)new high_first_t (); - else if (policy == "low") - matcher = (dfu_match_cb_t *)new low_first_t (); - else if (policy == "locality") - matcher = (dfu_match_cb_t *)new greater_interval_first_t (); - else if (policy == "variation") - matcher = (dfu_match_cb_t *)new var_aware_t (); - - return matcher; -} - -static void set_default_params (resource_context_t *ctx) +static void set_default_params (std::shared_ptr &ctx) { ctx->params.load_file = "conf/default"; ctx->params.load_format = "grug"; @@ -254,15 +237,17 @@ static int graph_format_to_ext (emit_format_t format, string &st) return rc; } -static int subsystem_exist (resource_context_t *ctx, string n) +static int subsystem_exist (std::shared_ptr &ctx, + std::string n) { int rc = 0; - if (ctx->db.metadata.roots.find (n) == ctx->db.metadata.roots.end ()) + if (ctx->db.metadata.roots->find (n) == ctx->db.metadata.roots->end ()) rc = -1; return rc; } -static int set_subsystems_use (resource_context_t *ctx, string n) +static int set_subsystems_use (std::shared_ptr &ctx, + std::string n) { int rc = 0; ctx->matcher->set_matcher_name (n); @@ -403,7 +388,7 @@ static void write_to_graphml (f_resource_graph_t &fg, fstream &o) write_graphml (o, fg, dp, true); } -static void write_to_graph (resource_context_t *ctx) +static void write_to_graph (std::shared_ptr &ctx) { fstream o; string fn, mn; @@ -431,21 +416,7 @@ static void write_to_graph (resource_context_t *ctx) o.close (); } -static void destory_resource_ctx (resource_context_t *ctx) -{ - delete ctx->matcher; - delete ctx->traverser; - delete ctx->fgraph; - delete ctx->writers; - for (auto &kv : ctx->jobs) - delete kv.second; /* job_info_t* type */ - ctx->jobs.clear (); - ctx->allocations.clear (); - ctx->reservations.clear (); - delete ctx; -} - -static void control_loop (resource_context_t *ctx) +static void control_loop (std::shared_ptr &ctx) { cmd_func_f *cmd = NULL; while (1) { @@ -472,7 +443,7 @@ static void control_loop (resource_context_t *ctx) } } -static int populate_resource_db (resource_context_t *ctx) +static int populate_resource_db (std::shared_ptr &ctx) { int rc = -1; double elapse; @@ -522,7 +493,30 @@ static int populate_resource_db (resource_context_t *ctx) return rc; } -static int init_resource_graph (resource_context_t *ctx) +static std::shared_ptr create_filtered_graph ( + std::shared_ptr &ctx) +{ + std::shared_ptr fg = nullptr; + + resource_graph_t &g = ctx->db.resource_graph; + vtx_infra_map_t vmap = get (&resource_pool_t::idata, g); + edg_infra_map_t emap = get (&resource_relation_t::idata, g); + const multi_subsystemsS &filter = ctx->matcher->subsystemsS (); + subsystem_selector_t vtxsel (vmap, filter); + subsystem_selector_t edgsel (emap, filter); + + try { + fg = std::make_shared (g, edgsel, vtxsel); + } catch (std::bad_alloc &e) { + errno = ENOMEM; + cerr << "ERROR: out of memory allocating f_resource_graph_t" << endl; + fg = nullptr; + } + + return fg; +} + +static int init_resource_graph (std::shared_ptr &ctx) { int rc = 0; @@ -538,17 +532,9 @@ static int init_resource_graph (resource_context_t *ctx) cerr << "ERROR: Not all subsystems found" << endl; return rc; } - - vtx_infra_map_t vmap = get (&resource_pool_t::idata, g); - edg_infra_map_t emap = get (&resource_relation_t::idata, g); - const multi_subsystemsS &filter = ctx->matcher->subsystemsS (); - subsystem_selector_t vtxsel (vmap, filter); - subsystem_selector_t edgsel (emap, filter); - - if (!(ctx->fgraph = new (nothrow)f_resource_graph_t (g, edgsel, vtxsel))) { - cerr << "ERROR: out of memory allocating f_resource_graph_t" << endl; + if ( !(ctx->fgraph = create_filtered_graph (ctx))) return -1; - } + ctx->jobid_counter = 1; if (ctx->params.prune_filters != "" && ctx->matcher->set_pruning_types_w_spec (ctx->matcher->dom_subsystem (), @@ -564,18 +550,23 @@ static int init_resource_graph (resource_context_t *ctx) | std::ofstream::badbit); ctx->params.r_out.open (ctx->params.r_fname); } - if ( !(ctx->traverser = new (nothrow)dfu_traverser_t ())) { + + try { + ctx->traverser = std::make_shared (); + } catch (std::bad_alloc &e) { + errno = ENOMEM; cerr << "ERROR: out of memory allocating traverser" << endl; return -1; } - if ( (rc = ctx->traverser->initialize (ctx->fgraph, &(ctx->db.metadata.roots), + + if ( (rc = ctx->traverser->initialize (ctx->fgraph, ctx->db.metadata.roots, ctx->matcher)) != 0) { cerr << "ERROR: initializing traverser" << endl; return -1; } match_format_t format = match_writers_factory_t:: get_writers_type (ctx->params.match_format); - if (!(ctx->writers = match_writers_factory_t::create (format))) { + if ( !(ctx->writers = match_writers_factory_t::create (format))) { cerr << "ERROR: out of memory allocating traverser" << endl; return -1; } @@ -583,7 +574,8 @@ static int init_resource_graph (resource_context_t *ctx) return rc; } -static void process_args (resource_context_t *ctx, int argc, char *argv[]) +static void process_args (std::shared_ptr &ctx, + int argc, char *argv[]) { int rc = 0; int ch = 0; @@ -674,48 +666,50 @@ static void process_args (resource_context_t *ctx, int argc, char *argv[]) usage (1); } -static int init_resource_query (resource_context_t **ctx, int c, char *v[]) +static std::shared_ptr init_resource_query (int c, + char *v[]) { - int rc = 0; + std::shared_ptr ctx = nullptr; - if (!(*ctx = new (nothrow)resource_context_t ())) { + try { + ctx = std::make_shared (); + } catch (std::bad_alloc &e) { cerr << "ERROR: out of memory allocating resource context" << endl; errno = ENOMEM; - rc = -1; goto done; } - set_default_params (*ctx); - process_args (*ctx, c, v); - (*ctx)->perf.min = DBL_MAX; - (*ctx)->perf.max = 0.0f; - (*ctx)->perf.accum = 0.0f; - if ( !((*ctx)->matcher = create_match_cb ((*ctx)->params.matcher_policy))) { + + set_default_params (ctx); + process_args (ctx, c, v); + ctx->perf.min = DBL_MAX; + ctx->perf.max = 0.0f; + ctx->perf.accum = 0.0f; + if ( !(ctx->matcher = create_match_cb (ctx->params.matcher_policy))) { cerr << "ERROR: unknown match policy " << endl; - cerr << "ERROR: " << (*ctx)->params.matcher_policy << endl; - rc = -1; + cerr << "ERROR: " << ctx->params.matcher_policy << endl; + ctx = nullptr; } - if (init_resource_graph (*ctx) != 0) { + if (init_resource_graph (ctx) != 0) { cerr << "ERROR: resource graph initialization" << endl; - rc = -1; + ctx = nullptr; } done: - return rc; + return ctx; } -static void fini_resource_query (resource_context_t *ctx) +static void fini_resource_query (std::shared_ptr &ctx) { if (ctx->params.r_fname != "") ctx->params.r_out.close (); if (ctx->params.o_fname != "") write_to_graph (ctx); - destory_resource_ctx (ctx); } int main (int argc, char *argv[]) { - resource_context_t *ctx = NULL; - if (init_resource_query (&ctx, argc, argv) != 0) { + std::shared_ptr ctx = nullptr; + if ( !(ctx = init_resource_query (argc, argv))) { cerr << "ERROR: resource query initialization" << endl; return EXIT_FAILURE; } diff --git a/resource/writers/match_writers.cpp b/resource/writers/match_writers.cpp index 0c8519475..bfc18dc73 100644 --- a/resource/writers/match_writers.cpp +++ b/resource/writers/match_writers.cpp @@ -200,8 +200,7 @@ rlite_match_writers_t::rlite_match_writers_t() rlite_match_writers_t::~rlite_match_writers_t () { - for (auto kv : m_gatherer) - delete kv.second; + } void rlite_match_writers_t::reset () @@ -386,34 +385,37 @@ void pretty_sim_match_writers_t::emit_vtx (const string &prefix, * * ****************************************************************************/ -match_writers_t *match_writers_factory_t::create (match_format_t f) +std::shared_ptr match_writers_factory_t:: + create (match_format_t f) { - match_writers_t *w = NULL; - - switch (f) { - case match_format_t::SIMPLE: - w = new (nothrow)sim_match_writers_t (); - break; - case match_format_t::JGF: - w = new (nothrow)jgf_match_writers_t (); - break; - case match_format_t::RLITE: - w = new (nothrow)rlite_match_writers_t (); - break; - case match_format_t::RV1_NOSCHED: - w = new (nothrow)rv1_nosched_match_writers_t (); - break; - case match_format_t::PRETTY_SIMPLE: - w = new (nothrow)pretty_sim_match_writers_t (); - break; - case match_format_t::RV1: - default: - w = new (nothrow)rv1_match_writers_t (); - break; - } + std::shared_ptr w = nullptr; - if (!w) + try { + switch (f) { + case match_format_t::SIMPLE: + w = std::make_shared (); + break; + case match_format_t::JGF: + w = std::make_shared (); + break; + case match_format_t::RLITE: + w = std::make_shared (); + break; + case match_format_t::RV1_NOSCHED: + w = std::make_shared (); + break; + case match_format_t::PRETTY_SIMPLE: + w = std::make_shared (); + break; + case match_format_t::RV1: + default: + w = std::make_shared (); + break; + } + } catch (std::bad_alloc &e) { errno = ENOMEM; + w = nullptr; + } return w; } diff --git a/resource/writers/match_writers.hpp b/resource/writers/match_writers.hpp index 1c72bc460..6bcc906e8 100644 --- a/resource/writers/match_writers.hpp +++ b/resource/writers/match_writers.hpp @@ -167,7 +167,7 @@ class pretty_sim_match_writers_t : public match_writers_t class match_writers_factory_t { public: static match_format_t get_writers_type (const std::string &n); - static match_writers_t *create (match_format_t f); + static std::shared_ptr create (match_format_t f); }; bool known_match_format (const std::string &format); From da03f24e72c1d12bc8637a5a1aa5c29e255288f0 Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 19:11:27 -0700 Subject: [PATCH 07/14] resource: add function name for logging messages For better debugging. --- resource/modules/resource_match.cpp | 123 +++++++++++++++++----------- 1 file changed, 76 insertions(+), 47 deletions(-) diff --git a/resource/modules/resource_match.cpp b/resource/modules/resource_match.cpp index 4a0481293..ca28d5a7d 100644 --- a/resource/modules/resource_match.cpp +++ b/resource/modules/resource_match.cpp @@ -204,7 +204,8 @@ static int process_args (std::shared_ptr &ctx, args.load_format = strstr (argv[i], "=") + 1; if (!known_resource_reader (args.load_format)) { flux_log (ctx->h, LOG_ERR, - "unknown resource reader (%s)! Use default (%s).", + "%s: unknown resource reader (%s)! use default (%s).", + __FUNCTION__, args.load_format.c_str (), dflt.c_str ()); args.load_format = dflt; } @@ -220,7 +221,8 @@ static int process_args (std::shared_ptr &ctx, args.match_policy = strstr (argv[i], "=") + 1; if (!known_match_policy (args.match_policy)) { flux_log (ctx->h, LOG_ERR, - "Unknown match policy (%s)! Use default (%s).", + "%s: unknown match policy (%s)! use default (%s).", + __FUNCTION__, args.match_policy.c_str (), dflt.c_str ()); args.match_policy = dflt; } @@ -238,8 +240,9 @@ static int process_args (std::shared_ptr &ctx, if (!known_match_format (args.match_format)) { args.match_format = dflt; flux_log (ctx->h, LOG_ERR, - "Unknown match format (%s)! Use default (%s).", - args.match_format.c_str (), dflt.c_str ()); + "%s: unknown match format (%s)! use default (%s).", + __FUNCTION__, + args.match_format.c_str (), dflt.c_str ()); args.match_format = dflt; } } else if (!strncmp ("reserve-vtx-vec=", @@ -247,14 +250,15 @@ static int process_args (std::shared_ptr &ctx, args.reserve_vtx_vec = atoi (strstr (argv[i], "=") + 1); if ( args.reserve_vtx_vec <= 0 || args.reserve_vtx_vec > 2000000) { flux_log (ctx->h, LOG_ERR, - "out of range specified for reserve-vtx-vec (%d)", - args.reserve_vtx_vec); + "%s: out of range specified for reserve-vtx-vec (%d)", + __FUNCTION__, args.reserve_vtx_vec); args.reserve_vtx_vec = 0; } } else { rc = -1; errno = EINVAL; - flux_log (ctx->h, LOG_ERR, "Unknown option `%s'", argv[i]); + flux_log (ctx->h, LOG_ERR, "%s: unknown option `%s'", + __FUNCTION__, argv[i]); } } @@ -268,20 +272,24 @@ static std::shared_ptr init_module (flux_t *h, uint32_t rank = 1; if (!(ctx = getctx (h))) { - flux_log (h, LOG_ERR, "can't allocate the context"); + flux_log (h, LOG_ERR, "%s: can't allocate the context", + __FUNCTION__); return nullptr; } if (flux_get_rank (h, &rank) < 0) { - flux_log (h, LOG_ERR, "can't determine rank"); + flux_log (h, LOG_ERR, "%s: can't determine rank", + __FUNCTION__); goto error; } if (rank) { - flux_log (h, LOG_ERR, "resource module must only run on rank 0"); + flux_log (h, LOG_ERR, "%s: resource module must only run on rank 0", + __FUNCTION__); goto error; } process_args (ctx, argc, argv); if (flux_msg_handler_addvec (h, htab, (void *)h, &ctx->handlers) < 0) { - flux_log_error (h, "error registering resource event handler"); + flux_log_error (h, "%s: error registering resource event handler", + __FUNCTION__); goto error; } return ctx; @@ -343,13 +351,15 @@ static int populate_resource_db_file (std::shared_ptr &ctx, in_file.open (ctx->args.load_file.c_str (), std::ifstream::in); if (!in_file.good ()) { errno = EIO; - flux_log (ctx->h, LOG_ERR, "opening %s", ctx->args.load_file.c_str ()); + flux_log (ctx->h, LOG_ERR, "%s: opening %s", + __FUNCTION__, ctx->args.load_file.c_str ()); goto done; } buffer << in_file.rdbuf (); in_file.close (); if ( (rc = ctx->db.load (buffer.str (), rd)) < 0) { - flux_log (ctx->h, LOG_ERR, "reader: %s", rd->err_message ().c_str ()); + flux_log (ctx->h, LOG_ERR, "%s: reader: %s", + __FUNCTION__, rd->err_message ().c_str ()); goto done; } rc = 0; @@ -387,12 +397,14 @@ static int populate_resource_db_kvs (std::shared_ptr &ctx, o = get_string_blocking (h, k); hwloc_xml = json_string_value (o); if ( (rc = ctx->db.load (hwloc_xml, rd, rank)) < 0) { - flux_log (ctx->h, LOG_ERR, "reader: %s", rd->err_message ().c_str ()); + flux_log (ctx->h, LOG_ERR, "%s: reader: %s", + __FUNCTION__, rd->err_message ().c_str ()); goto done; } Jput (o); - flux_log (ctx->h, LOG_ERR, "cluster vertex is unavailable"); if (db.metadata.roots->find ("containment") == db.metadata.roots->end ()) { + flux_log (ctx->h, LOG_ERR, "%s: cluster vertex is unavailable", + __FUNCTION__); goto done; } v = db.metadata.roots->at ("containment"); @@ -407,7 +419,8 @@ static int populate_resource_db_kvs (std::shared_ptr &ctx, o = get_string_blocking (h, k); hwloc_xml = json_string_value (o); if ( (rc = ctx->db.load (hwloc_xml, rd, v, rank)) < 0) { - flux_log (ctx->h, LOG_ERR, "reader: %s", rd->err_message ().c_str ()); + flux_log (ctx->h, LOG_ERR, "%s: reader: %s", + __FUNCTION__, rd->err_message ().c_str ()); goto done; } Jput (o); @@ -428,30 +441,36 @@ static int populate_resource_db (std::shared_ptr &ctx) if (ctx->args.reserve_vtx_vec != 0) ctx->db.resource_graph.m_vertices.reserve (ctx->args.reserve_vtx_vec); if ( (rd = create_resource_reader (ctx->args.load_format)) == nullptr) { - flux_log (ctx->h, LOG_ERR, "Can't create load reader"); + flux_log (ctx->h, LOG_ERR, "%s: can't create load reader", + __FUNCTION__); goto done; } if (ctx->args.load_whitelist != "") { if (rd->set_whitelist (ctx->args.load_whitelist) < 0) - flux_log (ctx->h, LOG_ERR, "setting whitelist"); + flux_log (ctx->h, LOG_ERR, "%s: setting whitelist", __FUNCTION__); if (!rd->is_whitelist_supported ()) - flux_log (ctx->h, LOG_WARNING, "whitelist unsupported"); + flux_log (ctx->h, LOG_WARNING, "%s: whitelist unsupported", + __FUNCTION__); } gettimeofday (&st, NULL); if (ctx->args.load_file != "") { if (populate_resource_db_file (ctx, rd) < 0) { - flux_log (ctx->h, LOG_ERR, "error loading resources from file"); + flux_log (ctx->h, LOG_ERR, "%s: error loading resources from file", + __FUNCTION__); goto done; } - flux_log (ctx->h, LOG_INFO, - "loaded resources from %s", ctx->args.load_file.c_str ()); + flux_log (ctx->h, LOG_INFO, "%s: loaded resources from %s", + __FUNCTION__, ctx->args.load_file.c_str ()); } else { if (populate_resource_db_kvs (ctx, rd) < 0) { - flux_log (ctx->h, LOG_ERR, "loading resources from the KVS"); + flux_log (ctx->h, LOG_ERR, "%s: loading resources from the KVS", + __FUNCTION__); goto done; } - flux_log (ctx->h, LOG_INFO, "loaded resources from hwloc in the KVS"); + flux_log (ctx->h, LOG_INFO, + "%s: loaded resources from hwloc in the KVS", + __FUNCTION__); } gettimeofday (&et, NULL); ctx->perf.load = get_elapse_time (st, et); @@ -535,14 +554,16 @@ static int init_resource_graph (std::shared_ptr &ctx) __FUNCTION__); return -1; - flux_log (ctx->h, LOG_ERR, "can't populate graph resource database"); } if ( (rc = populate_resource_db (ctx)) != 0) { + flux_log (ctx->h, LOG_ERR, + "%s: can't populate graph resource database", + __FUNCTION__); return rc; } - flux_log (ctx->h, LOG_ERR, "error processing subsystems %s", - ctx->args.match_subsystems.c_str ()); if ( (rc = select_subsystems (ctx)) != 0) { + flux_log (ctx->h, LOG_ERR, "%s: error processing subsystems %s", + __FUNCTION__, ctx->args.match_subsystems.c_str ()); return rc; } if ( !(ctx->fgraph = create_filtered_graph (ctx))) @@ -557,15 +578,16 @@ static int init_resource_graph (std::shared_ptr &ctx) if (ctx->args.prune_filters != "" && ctx->matcher->set_pruning_types_w_spec (ctx->matcher->dom_subsystem (), ctx->args.prune_filters) < 0) { - flux_log (ctx->h, LOG_ERR, "error setting pruning types with: %s", - ctx->args.prune_filters.c_str ()); + flux_log (ctx->h, LOG_ERR, "%s: error setting pruning types with: %s", + __FUNCTION__, ctx->args.prune_filters.c_str ()); return -1; } // Initialize the DFU traverser if (ctx->traverser->initialize (ctx->fgraph, - flux_log (ctx->h, LOG_ERR, "traverser initialization"); ctx->db.metadata.roots, ctx->matcher) < 0) { + flux_log (ctx->h, LOG_ERR, "%s: traverser initialization", + __FUNCTION__); return -1; } @@ -656,7 +678,7 @@ static int run_match (std::shared_ptr &ctx, int64_t jobid, && strcmp ("allocate_orelse_reserve", cmd) != 0 && strcmp ("allocate_with_satisfiability", cmd) != 0) { errno = EINVAL; - flux_log_error (ctx->h, "unknown cmd: %s", cmd); + flux_log_error (ctx->h, "%s: unknown cmd: %s", __FUNCTION__, cmd); goto done; } @@ -671,7 +693,8 @@ static int run_match (std::shared_ptr &ctx, int64_t jobid, if ((rc = track_schedule_info (ctx, jobid, *now, *at, jstr, o, *ov)) != 0) { errno = EINVAL; - flux_log_error (ctx->h, "can't add job info (id=%jd)", (intmax_t)jobid); + flux_log_error (ctx->h, "%s: can't add job info (id=%jd)", + __FUNCTION__, (intmax_t)jobid); goto done; } @@ -731,13 +754,15 @@ static void match_request_cb (flux_t *h, flux_msg_handler_t *w, goto error; if (is_existent_jobid (ctx, jobid)) { errno = EINVAL; - flux_log_error (h, "existent job (%jd).", (intmax_t)jobid); + flux_log_error (h, "%s: existent job (%jd).", + __FUNCTION__, (intmax_t)jobid); goto error; } if (run_match (ctx, jobid, cmd, js_str, &now, &at, &ov, R) < 0) { if (errno != EBUSY && errno != ENODEV) - flux_log_error (ctx->h, "match failed due to match error (id=%jd)", - (intmax_t)jobid); + flux_log_error (ctx->h, + "%s: match failed due to match error (id=%jd)", + __FUNCTION__, (intmax_t)jobid); goto error; } @@ -771,13 +796,14 @@ static void cancel_request_cb (flux_t *h, flux_msg_handler_t *w, ctx->reservations.erase (jobid); else { errno = ENOENT; - flux_log (h, LOG_DEBUG, "nonexistent job (id=%jd)", (intmax_t)jobid); + flux_log (h, LOG_DEBUG, "%s: nonexistent job (id=%jd)", + __FUNCTION__, (intmax_t)jobid); goto error; } if (run_remove (ctx, jobid) < 0) { - flux_log_error (h, "remove fails due to match error (id=%jd)", - (intmax_t)jobid); + flux_log_error (h, "%s: remove fails due to match error (id=%jd)", + __FUNCTION__, (intmax_t)jobid); goto error; } if (flux_respond_pack (h, msg, "{}") < 0) @@ -802,7 +828,8 @@ static void info_request_cb (flux_t *h, flux_msg_handler_t *w, goto error; if (!is_existent_jobid (ctx, jobid)) { errno = ENOENT; - flux_log (h, LOG_DEBUG, "nonexistent job (id=%jd)", (intmax_t)jobid); + flux_log (h, LOG_DEBUG, "%s: nonexistent job (id=%jd)", + __FUNCTION__, (intmax_t)jobid); goto error; } @@ -900,8 +927,9 @@ static void set_property_request_cb (flux_t *h, flux_msg_handler_t *w, if (pos == 0 || (pos == keyval.size () - 1) || pos == string::npos) { errno = EINVAL; - flux_log_error (h, - "Incorrect format.\nUse set-property PROPERTY=VALUE"); + flux_log_error (h, "%s: Incorrect format.", __FUNCTION__); + flux_log_error (h, "%s: Use set-property PROPERTY=VALUE", + __FUNCTION__); goto error; } @@ -912,8 +940,8 @@ static void set_property_request_cb (flux_t *h, flux_msg_handler_t *w, if (it == ctx->db.metadata.by_path.end ()) { errno = ENOENT; - flux_log_error (h, "Couldn't find %s in resource graph.", - resource_path.c_str ()); + flux_log_error (h, "%s: Couldn't find %s in resource graph.", + __FUNCTION__, resource_path.c_str ()); goto error; } @@ -961,8 +989,8 @@ static void get_property_request_cb (flux_t *h, flux_msg_handler_t *w, if (it == ctx->db.metadata.by_path.end ()) { errno = ENOENT; - flux_log_error (h, "Couldn't find %s in resource graph.", - resource_path.c_str ()); + flux_log_error (h, "%s: Couldn't find %s in resource graph.", + __FUNCTION__, resource_path.c_str ()); goto error; } @@ -977,8 +1005,9 @@ static void get_property_request_cb (flux_t *h, flux_msg_handler_t *w, if (resp_value.empty ()) { errno = ENOENT; - flux_log_error (h,"Property %s was not found for resource %s.", - property_key.c_str (), resource_path.c_str ()); + flux_log_error (h, "%s: Property %s was not found for resource %s.", + __FUNCTION__, property_key.c_str (), + resource_path.c_str ()); goto error; } From 492975a084ebe1dc38c74a58e9c1f8ba43d23659 Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 19:25:55 -0700 Subject: [PATCH 08/14] traverser: add std:: prefix to access data types etc For better namespace safety, remove "using namespace std" and instead explicitly use std:: prefix for all of the std symbols as being used in the traverser code. --- resource/traversers/dfu.cpp | 11 ++-- resource/traversers/dfu_impl.cpp | 97 ++++++++++++++++---------------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/resource/traversers/dfu.cpp b/resource/traversers/dfu.cpp index 080a05288..3a5a38320 100644 --- a/resource/traversers/dfu.cpp +++ b/resource/traversers/dfu.cpp @@ -33,7 +33,6 @@ extern "C" { #endif } -using namespace std; using namespace Flux::resource_model; using namespace Flux::resource_model::detail; using namespace Flux::Jobspec; @@ -47,12 +46,12 @@ using namespace Flux::Jobspec; int dfu_traverser_t::schedule (Jobspec::Jobspec &jobspec, detail::jobmeta_t &meta, bool x, match_op_t op, vtx_t root, unsigned int *needs, - std::unordered_map &dfv) + std::unordered_map &dfv) { int t = 0; int rc = -1; size_t len = 0; - vector agg; + std::vector agg; uint64_t duration = 0; int saved_errno = errno; planner_multi_t *p = NULL; @@ -164,7 +163,7 @@ const std::shared_ptr dfu_traverser_t:: return detail::dfu_impl_t::get_match_cb (); } -const string &dfu_traverser_t::err_message () const +const std::string &dfu_traverser_t::err_message () const { return detail::dfu_impl_t::err_message (); } @@ -200,7 +199,7 @@ int dfu_traverser_t::initialize () } for (auto &subsystem : get_match_cb ()->subsystems ()) { - map from_dfv; + std::map from_dfv; if (get_roots ()->find (subsystem) == get_roots ()->end ()) { errno = ENOTSUP; rc = -1; @@ -241,7 +240,7 @@ int dfu_traverser_t::run (Jobspec::Jobspec &jobspec, unsigned int needs = 0; vtx_t root = get_roots ()->at(dom); bool x = detail::dfu_impl_t::exclusivity (jobspec.resources, root); - std::unordered_map dfv; + std::unordered_map dfv; detail::dfu_impl_t::prime_jobspec (jobspec.resources, dfv); meta.build (jobspec, true, jobid, *at); if ( (rc = schedule (jobspec, meta, x, op, root, &needs, dfv)) == 0) { diff --git a/resource/traversers/dfu_impl.cpp b/resource/traversers/dfu_impl.cpp index 699ea8ceb..cc9866cea 100644 --- a/resource/traversers/dfu_impl.cpp +++ b/resource/traversers/dfu_impl.cpp @@ -30,7 +30,6 @@ extern "C" { #endif } -using namespace std; using namespace Flux::Jobspec; using namespace Flux::resource_model; using namespace Flux::resource_model::detail; @@ -71,7 +70,7 @@ bool dfu_impl_t::stop_explore (edg_t e, const subsystem_t &subsystem) const || m_color.is_black ((*m_graph)[u].idata.colors[subsystem])); } -bool dfu_impl_t::exclusivity (const vector &resources, +bool dfu_impl_t::exclusivity (const std::vector &resources, vtx_t u) { // If one of the resources matches with the visiting vertex, u @@ -170,7 +169,7 @@ int dfu_impl_t::by_subplan (const jobmeta_t &meta, const std::string &s, vtx_t u size_t len = 0; int64_t at = meta.at; uint64_t d = meta.duration; - vector aggs; + std::vector aggs; int saved_errno = errno; planner_multi_t *p = (*m_graph)[u].idata.subplans[s]; @@ -220,8 +219,8 @@ int dfu_impl_t::prune (const jobmeta_t &meta, bool exclusive, return rc; } -planner_multi_t *dfu_impl_t::subtree_plan (vtx_t u, vector &av, - vector &tp) +planner_multi_t *dfu_impl_t::subtree_plan (vtx_t u, std::vector &av, + std::vector &tp) { size_t len = av.size (); int64_t base_time = planner_base_time ((*m_graph)[u].schedule.plans); @@ -229,7 +228,7 @@ planner_multi_t *dfu_impl_t::subtree_plan (vtx_t u, vector &av, return planner_multi_new (base_time, duration, &av[0], &tp[0], len); } -void dfu_impl_t::match (vtx_t u, const vector &resources, +void dfu_impl_t::match (vtx_t u, const std::vector &resources, const Resource **slot_resource, const Resource **match_resource) { @@ -273,9 +272,9 @@ bool dfu_impl_t::slot_match (vtx_t u, const Resource *slot_resources) return slot_match; } -const vector &dfu_impl_t::test (vtx_t u, - const vector &resources, - bool &pristine, match_kind_t &spec) +const std::vector &dfu_impl_t::test (vtx_t u, + const std::vector &resources, + bool &pristine, match_kind_t &spec) { /* Note on the purpose of pristine: we differentiate two similar but * distinct cases with this parameter. @@ -292,7 +291,7 @@ const vector &dfu_impl_t::test (vtx_t u, * pristine is used to detect this case. */ bool slot = true; - const vector *ret = &resources; + const std::vector *ret = &resources; const Resource *slot_resources = NULL; const Resource *match_resources = NULL; match (u, resources, &slot_resources, &match_resources); @@ -314,8 +313,9 @@ const vector &dfu_impl_t::test (vtx_t u, /* Accumulate counts into accum[type] if the type is one of the pruning * filter type. */ -int dfu_impl_t::accum_if (const subsystem_t &subsystem, const string &type, - unsigned int counts, map &accum) +int dfu_impl_t::accum_if (const subsystem_t &subsystem, const std::string &type, + unsigned int counts, + std::map &accum) { int rc = -1; if (m_match->is_pruning_type (subsystem, type)) { @@ -329,9 +329,9 @@ int dfu_impl_t::accum_if (const subsystem_t &subsystem, const string &type, } /* Same as above except that accum is unorder_map */ -int dfu_impl_t::accum_if (const subsystem_t &subsystem, const string &type, +int dfu_impl_t::accum_if (const subsystem_t &subsystem, const std::string &type, unsigned int counts, - std::unordered_map &accum) + std::unordered_map &accum) { int rc = -1; if (m_match->is_pruning_type (subsystem, type)) { @@ -345,7 +345,7 @@ int dfu_impl_t::accum_if (const subsystem_t &subsystem, const string &type, } int dfu_impl_t::prime_exp (const subsystem_t &subsystem, vtx_t u, - map &dfv) + std::map &dfv) { int rc = 0; f_out_edg_iterator_t ei, ei_end; @@ -361,7 +361,7 @@ int dfu_impl_t::prime_exp (const subsystem_t &subsystem, vtx_t u, int dfu_impl_t::explore (const jobmeta_t &meta, vtx_t u, const subsystem_t &subsystem, - const vector &resources, bool pristine, + const std::vector &resources, bool pristine, bool *excl, visit_t direction, scoring_api_t &dfu) { int rc = -1; @@ -396,7 +396,7 @@ int dfu_impl_t::explore (const jobmeta_t &meta, vtx_t u, } int dfu_impl_t::aux_upv (const jobmeta_t &meta, vtx_t u, const subsystem_t &aux, - const vector &resources, bool pristine, + const std::vector &resources, bool pristine, bool *excl, scoring_api_t &to_parent) { int rc = -1; @@ -431,7 +431,7 @@ int dfu_impl_t::aux_upv (const jobmeta_t &meta, vtx_t u, const subsystem_t &aux, } int dfu_impl_t::dom_exp (const jobmeta_t &meta, vtx_t u, - const vector &resources, bool pristine, + const std::vector &resources, bool pristine, bool *excl, scoring_api_t &dfu) { int rc = -1; @@ -447,7 +447,7 @@ int dfu_impl_t::dom_exp (const jobmeta_t &meta, vtx_t u, return rc; } -int dfu_impl_t::cnt_slot (const vector &slot_shape, +int dfu_impl_t::cnt_slot (const std::vector &slot_shape, scoring_api_t &dfu_slot) { unsigned int qc = 0; @@ -469,7 +469,7 @@ int dfu_impl_t::cnt_slot (const vector &slot_shape, } int dfu_impl_t::dom_slot (const jobmeta_t &meta, vtx_t u, - const vector &slot_shape, bool pristine, + const std::vector &slot_shape, bool pristine, bool *excl, scoring_api_t &dfu) { int rc; @@ -506,14 +506,14 @@ int dfu_impl_t::dom_slot (const jobmeta_t &meta, vtx_t u, edg_group.score = score; edg_group.count = 1; edg_group.exclusive = 1; - dfu.add (dom, string ("slot"), edg_group); + dfu.add (dom, std::string ("slot"), edg_group); } done: return (qual_num_slots)? 0 : -1; } int dfu_impl_t::dom_dfv (const jobmeta_t &meta, vtx_t u, - const vector &resources, bool pristine, + const std::vector &resources, bool pristine, bool *excl, scoring_api_t &to_parent) { int rc = -1; @@ -525,8 +525,8 @@ int dfu_impl_t::dom_dfv (const jobmeta_t &meta, vtx_t u, bool check_pres = pristine; scoring_api_t dfu; planner_t *p = NULL; - const string &dom = m_match->dom_subsystem (); - const vector &next = test (u, resources, check_pres, sm); + const std::string &dom = m_match->dom_subsystem (); + const std::vector &next = test (u, resources, check_pres, sm); if (sm == match_kind_t::NONE_MATCH) goto done; @@ -561,7 +561,7 @@ int dfu_impl_t::dom_dfv (const jobmeta_t &meta, vtx_t u, return rc; } -int dfu_impl_t::resolve (vtx_t root, vector &resources, +int dfu_impl_t::resolve (vtx_t root, std::vector &resources, scoring_api_t &dfu, bool excl, unsigned int *needs) { int rc = -1; @@ -583,7 +583,7 @@ int dfu_impl_t::resolve (vtx_t root, vector &resources, // resolve remaining unconstrained resource types for (auto &subsystem : m_match->subsystems ()) { - vector types; + std::vector types; dfu.resrc_types (subsystem, types); for (auto &type : types) { if (dfu.qualified_count (subsystem, type) == 0) @@ -618,7 +618,7 @@ int dfu_impl_t::enforce (const subsystem_t &subsystem, scoring_api_t &dfu) { int rc = 0; try { - vector resource_types; + std::vector resource_types; dfu.resrc_types (subsystem, resource_types); for (auto &t : resource_types) { int best_i = dfu.best_i (subsystem, t); @@ -633,7 +633,7 @@ int dfu_impl_t::enforce (const subsystem_t &subsystem, scoring_api_t &dfu) } } } - } catch (const out_of_range &exception) { + } catch (const std::out_of_range &exception) { errno = ERANGE; rc = -1; } @@ -655,7 +655,7 @@ int dfu_impl_t::emit_edg (edg_t e, std::shared_ptr w) int dfu_impl_t::upd_plan (vtx_t u, const subsystem_t &s, unsigned int needs, bool excl, const jobmeta_t &meta, int &n, - map &to_parent) + std::map &to_parent) { int64_t span = 0; if (!excl) { @@ -686,8 +686,8 @@ int dfu_impl_t::upd_plan (vtx_t u, const subsystem_t &s, unsigned int needs, int dfu_impl_t::upd_sched (vtx_t u, std::shared_ptr writers, const subsystem_t &s, unsigned int needs, bool excl, int n, const jobmeta_t &meta, - map &dfu, - map &to_parent) + std::map &dfu, + std::map &to_parent) { if (upd_plan (u, s, needs, excl, meta, n, to_parent) == -1) goto done; @@ -705,7 +705,7 @@ int dfu_impl_t::upd_sched (vtx_t u, std::shared_ptr writers, // Update subtree plan planner_multi_t *subtree_plan = (*m_graph)[u].idata.subplans[s]; if (subtree_plan) { - vector aggregate; + std::vector aggregate; count_relevant_types (subtree_plan, dfu, aggregate); span = planner_multi_add_span (subtree_plan, meta.at, meta.duration, &(aggregate[0]), aggregate.size ()); @@ -729,7 +729,7 @@ int dfu_impl_t::upd_sched (vtx_t u, std::shared_ptr writers, int dfu_impl_t::upd_upv (vtx_t u, const subsystem_t &subsystem, unsigned int needs, bool excl, const jobmeta_t &meta, - map &to_parent) + std::map &to_parent) { //NYI: update resources on the UPV direction return 0; @@ -737,11 +737,11 @@ int dfu_impl_t::upd_upv (vtx_t u, const subsystem_t &subsystem, int dfu_impl_t::upd_dfv (vtx_t u, std::shared_ptr writers, unsigned int needs, bool excl, const jobmeta_t &meta, - std::map &to_parent) + std::map &to_parent) { int n_plans = 0; - map dfu; - const string &dom = m_match->dom_subsystem (); + std::map dfu; + const std::string &dom = m_match->dom_subsystem (); f_out_edg_iterator_t ei, ei_end; m_trav_level++; @@ -845,7 +845,7 @@ int dfu_impl_t::rem_x_checker (vtx_t u, int64_t jobid) } int dfu_impl_t::rem_subtree_plan (vtx_t u, int64_t jobid, - const string &subsystem) + const std::string &subsystem) { int rc = 0; int span = -1; @@ -873,7 +873,7 @@ int dfu_impl_t::rem_subtree_plan (vtx_t u, int64_t jobid, int dfu_impl_t::rem_dfv (vtx_t u, int64_t jobid) { int rc = 0; - const string &dom = m_match->dom_subsystem (); + const std::string &dom = m_match->dom_subsystem (); auto &tags = (*m_graph)[u].schedule.tags; f_out_edg_iterator_t ei, ei_end; @@ -966,7 +966,7 @@ const std::shared_ptr dfu_impl_t::get_match_cb () const return m_match; } -const string &dfu_impl_t::err_message () const +const std::string &dfu_impl_t::err_message () const { return m_err_msg; } @@ -992,14 +992,14 @@ void dfu_impl_t::clear_err_message () } int dfu_impl_t::prime_pruning_filter (const subsystem_t &s, vtx_t u, - map &to_parent) + std::map &to_parent) { int rc = -1; int saved_errno = errno; - vector avail; - vector types; - map dfv; - string type = (*m_graph)[u].type; + std::vector avail; + std::vector types; + std::map dfv; + std::string type = (*m_graph)[u].type; std::vector out_prune_types; (*m_graph)[u].idata.colors[s] = m_color.gray (); @@ -1038,8 +1038,9 @@ int dfu_impl_t::prime_pruning_filter (const subsystem_t &s, vtx_t u, return rc; } -void dfu_impl_t::prime_jobspec (vector &resources, - std::unordered_map &to_parent) +void dfu_impl_t::prime_jobspec (std::vector &resources, + std::unordered_map &to_parent) { const subsystem_t &subsystem = m_match->dom_subsystem (); for (auto &resource : resources) { @@ -1060,7 +1061,7 @@ int dfu_impl_t::select (Jobspec::Jobspec &j, vtx_t root, jobmeta_t &meta, int rc = -1; scoring_api_t dfu; bool x_in = excl; - const string &dom = m_match->dom_subsystem (); + const std::string &dom = m_match->dom_subsystem (); tick (); rc = dom_dfv (meta, root, j.resources, true, &x_in, dfu); @@ -1077,7 +1078,7 @@ int dfu_impl_t::select (Jobspec::Jobspec &j, vtx_t root, jobmeta_t &meta, int dfu_impl_t::update (vtx_t root, std::shared_ptr writers, jobmeta_t &meta, unsigned int needs, bool exclusive) { - map dfu; + std::map dfu; m_color.reset (); return (upd_dfv (root, writers, needs, exclusive, meta, dfu) > 0)? 0 : -1; } From 00ae5e01295dacd7016eae4e98f868f8c9edd70a Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 21:14:08 -0700 Subject: [PATCH 09/14] resource: use fully qualified std symbols For better namespace safety, remove "using namespace std" and instead explicitly use std:: prefix for all of the std symbols as being used in resource_match.cpp --- resource/modules/resource_match.cpp | 62 ++++++++++++++--------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/resource/modules/resource_match.cpp b/resource/modules/resource_match.cpp index ca28d5a7d..8b10ba056 100644 --- a/resource/modules/resource_match.cpp +++ b/resource/modules/resource_match.cpp @@ -43,7 +43,6 @@ extern "C" { #include "resource/jobinfo/jobinfo.hpp" #include "resource/policies/dfu_match_policy_factory.hpp" -using namespace std; using namespace Flux::resource_model; /****************************************************************************** @@ -53,13 +52,13 @@ using namespace Flux::resource_model; ******************************************************************************/ struct resource_args_t { - string load_file; /* load file name */ - string load_format; /* load reader format */ - string load_whitelist; /* load resource whitelist */ - string match_subsystems; - string match_policy; - string prune_filters; - string match_format; + std::string load_file; /* load file name */ + std::string load_format; /* load reader format */ + std::string load_whitelist; /* load resource whitelist */ + std::string match_subsystems; + std::string match_policy; + std::string prune_filters; + std::string match_format; int reserve_vtx_vec; /* Allow for reserving vertex vector size */ }; @@ -82,9 +81,9 @@ struct resource_ctx_t { std::shared_ptr fgraph; /* Filtered graph */ std::shared_ptr writers; /* Vertex/Edge writers */ match_perf_t perf; /* Match performance stats */ - map> jobs; /* Jobs table */ - map allocations; /* Allocation table */ - map reservations; /* Reservation table */ + std::map> jobs; /* Jobs table */ + std::map allocations; /* Allocation table */ + std::map reservations; /* Reservation table */ }; resource_ctx_t::~resource_ctx_t () @@ -194,7 +193,7 @@ static int process_args (std::shared_ptr &ctx, { int rc = 0; resource_args_t &args = ctx->args; - string dflt = ""; + std::string dflt = ""; for (int i = 0; i < argc; i++) { if (!strncmp ("load-file=", argv[i], sizeof ("load-file"))) { @@ -345,7 +344,7 @@ static int populate_resource_db_file (std::shared_ptr &ctx, std::shared_ptr rd) { int rc = -1; - ifstream in_file; + std::ifstream in_file; std::stringstream buffer{}; in_file.open (ctx->args.load_file.c_str (), std::ifstream::in); @@ -487,9 +486,9 @@ static int select_subsystems (std::shared_ptr &ctx) * subsystem1[:relation1[:relation2...]],subsystem2[... */ int rc = 0; - stringstream ss (ctx->args.match_subsystems); + std::stringstream ss (ctx->args.match_subsystems); subsystem_t subsystem; - string token; + std::string token; while (getline (ss, token, ',')) { size_t found = token.find_first_of (":"); @@ -508,8 +507,9 @@ static int select_subsystems (std::shared_ptr &ctx) errno = EINVAL; goto done; } - stringstream relations (token.substr (found+1, std::string::npos)); - string relation; + std::stringstream relations (token.substr (found+1, + std::string::npos)); + std::string relation; while (getline (relations, relation, ':')) ctx->matcher->add_subsystem (subsystem, relation); } @@ -610,7 +610,7 @@ static void update_match_perf (std::shared_ptr &ctx, ctx->perf.accum += elapse; } -static inline string get_status_string (int64_t now, int64_t at) +static inline std::string get_status_string (int64_t now, int64_t at) { return (at == now)? "ALLOCATED" : "RESERVED"; } @@ -651,12 +651,12 @@ static int run (std::shared_ptr &ctx, int64_t jobid, Flux::Jobspec::Jobspec j {jstr}; dfu_traverser_t &tr = *(ctx->traverser); - if (string ("allocate") == cmd) + if (std::string ("allocate") == cmd) rc = tr.run (j, ctx->writers, match_op_t::MATCH_ALLOCATE, jobid, at); - else if (string ("allocate_with_satisfiability") == cmd) + else if (std::string ("allocate_with_satisfiability") == cmd) rc = tr.run (j, ctx->writers, match_op_t:: MATCH_ALLOCATE_W_SATISFIABILITY, jobid, at); - else if (string ("allocate_orelse_reserve") == cmd) + else if (std::string ("allocate_orelse_reserve") == cmd) rc = tr.run (j, ctx->writers, match_op_t::MATCH_ALLOCATE_ORELSE_RESERVE, jobid, at); return rc; @@ -664,7 +664,7 @@ static int run (std::shared_ptr &ctx, int64_t jobid, static int run_match (std::shared_ptr &ctx, int64_t jobid, const char *cmd, const std::string &jstr, int64_t *now, - int64_t *at, double *ov, stringstream &o) + int64_t *at, double *ov, std::stringstream &o) { int rc = 0; double elapse = 0.0f; @@ -743,10 +743,10 @@ static void match_request_cb (flux_t *h, flux_msg_handler_t *w, int64_t now = 0; int64_t jobid = -1; double ov = 0.0f; - string status = ""; + std::string status = ""; const char *cmd = NULL; const char *js_str = NULL; - stringstream R; + std::stringstream R; std::shared_ptr ctx = getctx ((flux_t *)arg); if (flux_request_unpack (msg, NULL, "{s:s s:I s:s}", "cmd", &cmd, @@ -822,7 +822,7 @@ static void info_request_cb (flux_t *h, flux_msg_handler_t *w, std::shared_ptr ctx = getctx ((flux_t *)arg); int64_t jobid = -1; std::shared_ptr info = NULL; - string status = ""; + std::string status = ""; if (flux_request_unpack (msg, NULL, "{s:I}", "jobid", &jobid) < 0) goto error; @@ -907,12 +907,12 @@ static void set_property_request_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { const char *rp = NULL, *kv = NULL; - string resource_path = "", keyval = ""; - string property_key = "", property_value = ""; + std::string resource_path = "", keyval = ""; + std::string property_key = "", property_value = ""; size_t pos; std::shared_ptr ctx = getctx ((flux_t *)arg); std::map::const_iterator it; - std::pair::iterator, bool> ret; + std::pair::iterator, bool> ret; vtx_t v; if (flux_request_unpack (msg, NULL, "{s:s s:s}", @@ -925,7 +925,7 @@ static void set_property_request_cb (flux_t *h, flux_msg_handler_t *w, pos = keyval.find ('='); - if (pos == 0 || (pos == keyval.size () - 1) || pos == string::npos) { + if (pos == 0 || (pos == keyval.size () - 1) || pos == std::string::npos) { errno = EINVAL; flux_log_error (h, "%s: Incorrect format.", __FUNCTION__); flux_log_error (h, "%s: Use set-property PROPERTY=VALUE", @@ -970,12 +970,12 @@ static void get_property_request_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { const char *rp = NULL, *gp_key = NULL; - string resource_path = "", property_key = ""; + std::string resource_path = "", property_key = ""; std::shared_ptr ctx = getctx ((flux_t *)arg); std::map::const_iterator it; std::map::const_iterator p_it; vtx_t v; - string resp_value = ""; + std::string resp_value = ""; if (flux_request_unpack (msg, NULL, "{s:s s:s}", "gp_resource_path", &rp, From a3292edf9d2ac71038f6161018715ef8e66fd189 Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 21:41:54 -0700 Subject: [PATCH 10/14] reader: convert to use fully qualified std symbols Remove "using namespace std" and explicitly use the "std::" prefix for all of the std symbols being used in reader codes. --- resource/readers/resource_reader_base.cpp | 1 - resource/readers/resource_reader_grug.cpp | 36 +++++++++++------------ resource/readers/resource_reader_jgf.cpp | 1 - resource/readers/resource_spec_grug.cpp | 15 +++++----- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/resource/readers/resource_reader_base.cpp b/resource/readers/resource_reader_base.cpp index b38c2090c..212d48c47 100644 --- a/resource/readers/resource_reader_base.cpp +++ b/resource/readers/resource_reader_base.cpp @@ -31,7 +31,6 @@ extern "C" { #endif } -using namespace std; using namespace Flux::resource_model; diff --git a/resource/readers/resource_reader_grug.cpp b/resource/readers/resource_reader_grug.cpp index 21a244880..42385c1e9 100644 --- a/resource/readers/resource_reader_grug.cpp +++ b/resource/readers/resource_reader_grug.cpp @@ -38,7 +38,6 @@ extern "C" { #endif } -using namespace std; using namespace Flux::resource_model; /*! Note that this class must be copy-constructible @@ -56,7 +55,7 @@ class dfs_emitter_t : public boost::default_dfs_visitor { void tree_edge (gge_t e, const gg_t &recipe); void finish_vertex (ggv_t u, const gg_t &recipe); - const string &err_message () const; + const std::string &err_message () const; void set_rank (int rank); int get_rank (); @@ -72,13 +71,13 @@ class dfs_emitter_t : public boost::default_dfs_visitor { int gen_id (gge_t e, const gg_t &recipe, int i, int sz, int j); - map> m_gen_src_vtx; - deque m_hier_scales; + std::map> m_gen_src_vtx; + std::deque m_hier_scales; resource_graph_t *m_g_p = NULL; resource_graph_metadata_t *m_gm_p = NULL; resource_gen_spec_t *m_gspec_p = NULL; int m_rank = -1; - string m_err_msg = ""; + std::string m_err_msg = ""; }; @@ -88,7 +87,8 @@ class dfs_emitter_t : public boost::default_dfs_visitor { * * ********************************************************************************/ -int dfs_emitter_t::path_prefix (const string &path, int uplevel, string &prefix) +int dfs_emitter_t::path_prefix (const std::string &path, + int uplevel, std::string &prefix) { size_t pos = 0; unsigned int occurrence = 0; @@ -97,12 +97,12 @@ int dfs_emitter_t::path_prefix (const string &path, int uplevel, string &prefix) return -1; while (occurrence != (num_slashes - uplevel + 1)) { pos = path.find ("/", pos); - if (pos == string::npos) + if (pos == std::string::npos) break; pos += 1; occurrence++; } - string new_prefix = path.substr (0, pos); + std::string new_prefix = path.substr (0, pos); if (new_prefix.back () != '/') new_prefix.push_back ('/'); prefix = std::move (new_prefix); @@ -133,7 +133,7 @@ int dfs_emitter_t::gen_id (gge_t e, const gg_t &recipe, int i, int sz, int j) if (scope > (int) m_hier_scales.size ()) scope = m_hier_scales.size (); j_dim_wrap = 1; - deque::const_iterator iter; + std::deque::const_iterator iter; for (h = 0; h < scope; ++h) j_dim_wrap *= m_hier_scales[h]; @@ -192,8 +192,8 @@ vtx_t dfs_emitter_t::emit_vertex (ggv_t u, gge_t e, const gg_t &recipe, return (*(m.roots))[recipe[u].subsystem]; vtx_t v = add_vertex (g);; - string pref = ""; - string ssys = recipe[u].subsystem; + std::string pref = ""; + std::string ssys = recipe[u].subsystem; int id = 0; if (src_v == boost::graph_traits::null_vertex ()) { @@ -205,7 +205,7 @@ vtx_t dfs_emitter_t::emit_vertex (ggv_t u, gge_t e, const gg_t &recipe, pref = g[src_v].paths[ssys]; } - string istr = (id != -1)? to_string (id) : ""; + std::string istr = (id != -1)? std::to_string (id) : ""; g[v].type = recipe[u].type; g[v].basename = recipe[u].basename; g[v].size = recipe[u].size; @@ -286,10 +286,10 @@ void dfs_emitter_t::tree_edge (gge_t e, const gg_t &recipe) vtx_t src_vtx, tgt_vtx; ggv_t src_ggv = source (e, recipe); ggv_t tgt_ggv = target (e, recipe); - vector::iterator src_it, tgt_it; + std::vector::iterator src_it, tgt_it; resource_graph_t &g = *m_g_p; resource_graph_metadata_t &m = *m_gm_p; - string in; + std::string in; int i = 0, j = 0;; if (recipe[src_ggv].root) { @@ -301,7 +301,7 @@ void dfs_emitter_t::tree_edge (gge_t e, const gg_t &recipe) } } - m_gen_src_vtx[tgt_ggv] = vector(); + m_gen_src_vtx[tgt_ggv] = std::vector(); switch (m_gspec_p->to_gen_method_t (recipe[e].gen_method)) { case MULTIPLY: @@ -347,7 +347,7 @@ void dfs_emitter_t::tree_edge (gge_t e, const gg_t &recipe) src_vtx = *src_it; for (tgt_it = m.by_type[recipe[tgt_ggv].type].begin (); tgt_it != m.by_type[recipe[tgt_ggv].type].end (); tgt_it++) { - string comp_pth1, comp_pth2; + std::string comp_pth1, comp_pth2; tgt_vtx = (*tgt_it); path_prefix (g[tgt_vtx].paths[in], recipe[e].as_tgt_uplvl, comp_pth1); @@ -391,7 +391,7 @@ void dfs_emitter_t::finish_vertex (ggv_t u, const gg_t &recipe) * * \return error message */ -const string &dfs_emitter_t::err_message () const +const std::string &dfs_emitter_t::err_message () const { return m_err_msg; } @@ -432,7 +432,7 @@ int resource_reader_grug_t::unpack (resource_graph_t &g, { int rc = 0; - istringstream in; + std::istringstream in; in.str (str); if (m_gspec.read_graphml (in) != 0) { diff --git a/resource/readers/resource_reader_jgf.cpp b/resource/readers/resource_reader_jgf.cpp index cc202b766..eebc6c04f 100644 --- a/resource/readers/resource_reader_jgf.cpp +++ b/resource/readers/resource_reader_jgf.cpp @@ -35,7 +35,6 @@ extern "C" { #endif } -using namespace std; using namespace Flux; using namespace Flux::resource_model; diff --git a/resource/readers/resource_spec_grug.cpp b/resource/readers/resource_spec_grug.cpp index 072ee3045..f27425952 100644 --- a/resource/readers/resource_spec_grug.cpp +++ b/resource/readers/resource_spec_grug.cpp @@ -35,12 +35,11 @@ extern "C" { #endif } -using namespace std; using namespace Flux::resource_model; struct str2enum_t { - string str; + std::string str; int e; }; @@ -135,7 +134,8 @@ const gg_t &resource_gen_spec_t::gen_graph () * \param s gen_method string from graphml spec * \return 0 on success; -1 otherwise */ -const gen_meth_t resource_gen_spec_t::to_gen_method_t (const std::string &s) const +const gen_meth_t resource_gen_spec_t::to_gen_method_t ( + const std::string &s) const { int i; for (i=0; str2genmeth[i].str != ""; ++i) @@ -149,10 +149,10 @@ const gen_meth_t resource_gen_spec_t::to_gen_method_t (const std::string &s) con * \param ifn resource generator recipe file in graphml * \return 0 on success; -1 otherwise */ -int resource_gen_spec_t::read_graphml (const string &ifn) +int resource_gen_spec_t::read_graphml (const std::string &ifn) { int rc = 0; - ifstream in_file (ifn.c_str ()); + std::ifstream in_file (ifn.c_str ()); if (!in_file.good ()) return -1; rc = read_graphml (in_file); @@ -183,10 +183,10 @@ int resource_gen_spec_t::read_graphml (std::istream &in) * \param simple if false, output will have more detailed info * \return 0 on success; -1 otherwise */ -int resource_gen_spec_t::write_graphviz (const string &ofn, bool simple) +int resource_gen_spec_t::write_graphviz (const std::string &ofn, bool simple) { int rc = 0; - fstream out_file (ofn, fstream::out); + std::fstream out_file (ofn, std::fstream::out); try { vtx_basename_map_t v_bn_map = get (&resource_pool_gen_t::basename, g); vtx_size_map_t v_sz_map = get (&resource_pool_gen_t::size, g); @@ -209,7 +209,6 @@ int resource_gen_spec_t::write_graphviz (const string &ofn, bool simple) boost::write_graphviz (out_file, g, vwr, ewr); } } catch (boost::graph_exception &e) { - cerr << e.what () << endl; rc = -1; } out_file.close (); From 058aaa1b575fb7ff7a13fa82c1b8ee05fe1e61b9 Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 21:49:13 -0700 Subject: [PATCH 11/14] writer: use "std::" prefix for std symbols Explicitly use std:: prefix for all of the std symbols as being used in match_writers.cpp. --- resource/writers/match_writers.cpp | 86 +++++++++++++++--------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/resource/writers/match_writers.cpp b/resource/writers/match_writers.cpp index bfc18dc73..5688f681a 100644 --- a/resource/writers/match_writers.cpp +++ b/resource/writers/match_writers.cpp @@ -31,7 +31,6 @@ namespace Flux { namespace resource_model { -using namespace std; /**************************************************************************** * * @@ -39,7 +38,8 @@ using namespace std; * * ****************************************************************************/ -void match_writers_t::compress (stringstream &o, const set &ids) +void match_writers_t::compress (std::stringstream &o, + const std::set &ids) { int64_t base = INT64_MIN; int64_t runlen = 0; @@ -49,7 +49,7 @@ void match_writers_t::compress (stringstream &o, const set &ids) runlen++; } else { if (runlen != 0) - o << "-" + to_string (base + runlen) + ","; + o << "-" + std::to_string (base + runlen) + ","; else if (base != INT64_MIN) o << ","; o << id; @@ -68,7 +68,7 @@ void match_writers_t::compress (stringstream &o, const set &ids) * * ****************************************************************************/ -void sim_match_writers_t::emit (stringstream &out) +void sim_match_writers_t::emit (std::stringstream &out) { out << m_out.str (); } @@ -79,12 +79,13 @@ void sim_match_writers_t::reset () m_out.clear (); } -void sim_match_writers_t::emit_vtx (const string &prefix, +void sim_match_writers_t::emit_vtx (const std::string &prefix, const f_resource_graph_t &g, const vtx_t &u, unsigned int needs, bool exclusive) { - string mode = (exclusive)? "x" : "s"; - m_out << prefix << g[u].name << "[" << needs << ":" << mode << "]" << endl; + std::string mode = (exclusive)? "x" : "s"; + m_out << prefix << g[u].name << "[" << needs << ":" << mode << "]" + << std::endl; } @@ -94,7 +95,7 @@ void sim_match_writers_t::emit_vtx (const string &prefix, * * ****************************************************************************/ -void jgf_match_writers_t::emit (stringstream &out, bool newline) +void jgf_match_writers_t::emit (std::stringstream &out, bool newline) { size_t vout_size = m_vout.str ().size (); size_t eout_size = m_eout.str ().size (); @@ -106,11 +107,11 @@ void jgf_match_writers_t::emit (stringstream &out, bool newline) out << m_eout.str ().substr (0, eout_size - 1); out << "]}}"; if (newline) - out << endl; + out << std::endl; } } -void jgf_match_writers_t::emit (stringstream &out) +void jgf_match_writers_t::emit (std::stringstream &out) { emit (out, true); } @@ -123,11 +124,11 @@ void jgf_match_writers_t::reset () m_eout.clear (); } -void jgf_match_writers_t::emit_vtx (const string &prefix, +void jgf_match_writers_t::emit_vtx (const std::string &prefix, const f_resource_graph_t &g, const vtx_t &u, unsigned int needs, bool exclusive) { - string x = (exclusive)? "true" : "false"; + std::string x = (exclusive)? "true" : "false"; m_vout << "{"; m_vout << "\"id\":\"" << g[u].uniq_id << "\","; m_vout << "\"metadata\":{"; @@ -141,7 +142,7 @@ void jgf_match_writers_t::emit_vtx (const string &prefix, m_vout << "\"unit\":\"" << g[u].unit << "\","; m_vout << "\"size\":" << needs; if (!g[u].properties.empty ()) { - stringstream props; + std::stringstream props; m_vout << ", "; m_vout << "\"properties\":{"; for (auto &kv : g[u].properties) @@ -150,7 +151,7 @@ void jgf_match_writers_t::emit_vtx (const string &prefix, m_vout << "}"; } if (!g[u].paths.empty ()) { - stringstream paths; + std::stringstream paths; m_vout << ", "; m_vout << "\"paths\":{"; for (auto &kv : g[u].paths) @@ -163,7 +164,7 @@ void jgf_match_writers_t::emit_vtx (const string &prefix, m_vout << "},"; } -void jgf_match_writers_t::emit_edg (const string &prefix, +void jgf_match_writers_t::emit_edg (const std::string &prefix, const f_resource_graph_t &g, const edg_t &e) { m_eout << "{"; @@ -172,7 +173,7 @@ void jgf_match_writers_t::emit_edg (const string &prefix, m_eout << "\"metadata\":{"; if (!g[e].name.empty ()) { - stringstream names; + std::stringstream names; m_eout << "\"name\":{"; for (auto &kv : g[e].name) names << "\"" << kv.first << "\":\"" << kv.second << "\","; @@ -193,8 +194,8 @@ void jgf_match_writers_t::emit_edg (const string &prefix, rlite_match_writers_t::rlite_match_writers_t() { - m_reducer["core"] = set (); - m_reducer["gpu"] = set (); + m_reducer["core"] = std::set (); + m_reducer["gpu"] = std::set (); m_gatherer["node"] = std::make_shared (); } @@ -209,17 +210,17 @@ void rlite_match_writers_t::reset () m_out.clear (); } -void rlite_match_writers_t::emit (stringstream &out, bool newline) +void rlite_match_writers_t::emit (std::stringstream &out, bool newline) { size_t size = m_out.str ().size (); if (size > 1) { out << "{\"R_lite\":[" << m_out.str ().substr (0, size - 1) << "]}"; if (newline) - out << endl; + out << std::endl; } } -void rlite_match_writers_t::emit (stringstream &out) +void rlite_match_writers_t::emit (std::stringstream &out) { emit (out, true); } @@ -236,7 +237,7 @@ bool rlite_match_writers_t::m_reducer_set () return set; } -void rlite_match_writers_t::emit_vtx (const string &prefix, +void rlite_match_writers_t::emit_vtx (const std::string &prefix, const f_resource_graph_t &g, const vtx_t &u, unsigned int needs, @@ -246,7 +247,7 @@ void rlite_match_writers_t::emit_vtx (const string &prefix, m_reducer[g[u].type].insert (g[u].id); } else if (m_gatherer.find (g[u].type) != m_gatherer.end ()) { if (m_reducer_set ()) { - stringstream &gout = *(m_gatherer[g[u].type]); + std::stringstream &gout = *(m_gatherer[g[u].type]); gout << "{"; gout << "\"rank\":\"" << g[u].rank << "\","; gout << "\"node\":\"" << g[u].name << "\","; @@ -280,24 +281,24 @@ void rv1_match_writers_t::reset () jgf.reset (); } -void rv1_match_writers_t::emit (stringstream &out) +void rv1_match_writers_t::emit (std::stringstream &out) { - string ver = "\"version\":1"; - string exec_key = "\"execution\":"; - string sched_key = "\"scheduling\":"; + std::string ver = "\"version\":1"; + std::string exec_key = "\"execution\":"; + std::string sched_key = "\"scheduling\":"; size_t base = out.str ().size (); out << "{" << ver; out << "," << exec_key; rlite.emit (out, false); out << "," << sched_key; jgf.emit (out, false); - out << "}" << endl; + out << "}" << std::endl; if (out.str ().size () <= (base + ver.size () + exec_key.size () + sched_key.size () + 5)) out.str (out.str ().substr (0, base)); } -void rv1_match_writers_t::emit_vtx (const string &prefix, +void rv1_match_writers_t::emit_vtx (const std::string &prefix, const f_resource_graph_t &g, const vtx_t &u, unsigned int needs, @@ -307,7 +308,7 @@ void rv1_match_writers_t::emit_vtx (const string &prefix, jgf.emit_vtx (prefix, g, u, needs, exclusive); } -void rv1_match_writers_t::emit_edg (const string &prefix, +void rv1_match_writers_t::emit_edg (const std::string &prefix, const f_resource_graph_t &g, const edg_t &e) { @@ -327,20 +328,20 @@ void rv1_nosched_match_writers_t::reset () rlite.reset (); } -void rv1_nosched_match_writers_t::emit (stringstream &out) +void rv1_nosched_match_writers_t::emit (std::stringstream &out) { - string ver = "\"version\":1"; - string exec_key = "\"execution\":"; + std::string ver = "\"version\":1"; + std::string exec_key = "\"execution\":"; size_t base = out.str ().size (); out << "{" << ver; out << "," << exec_key; rlite.emit (out, false); - out << "}" << endl; + out << "}" << std::endl; if (out.str ().size () <= (base + ver.size () + exec_key.size () + 4)) out.str (out.str ().substr (0, base)); } -void rv1_nosched_match_writers_t::emit_vtx (const string &prefix, +void rv1_nosched_match_writers_t::emit_vtx (const std::string &prefix, const f_resource_graph_t &g, const vtx_t &u, unsigned int needs, @@ -356,7 +357,7 @@ void rv1_nosched_match_writers_t::emit_vtx (const string &prefix, * * ****************************************************************************/ -void pretty_sim_match_writers_t::emit (stringstream &out) +void pretty_sim_match_writers_t::emit (std::stringstream &out) { for (auto &s: m_out) out << s; @@ -367,14 +368,15 @@ void pretty_sim_match_writers_t::reset () m_out.clear (); } -void pretty_sim_match_writers_t::emit_vtx (const string &prefix, +void pretty_sim_match_writers_t::emit_vtx (const std::string &prefix, const f_resource_graph_t &g, const vtx_t &u, unsigned int needs, bool exclusive) { - stringstream out; - string mode = (exclusive)? "exclusive" : "shared"; - out << prefix << g[u].name << "[" << needs << ":" << mode << "]" << endl; + std::stringstream out; + std::string mode = (exclusive)? "exclusive" : "shared"; + out << prefix << g[u].name << "[" << needs << ":" << mode << "]" + << std::endl; m_out.push_front (out.str ()); } @@ -420,7 +422,7 @@ std::shared_ptr match_writers_factory_t:: return w; } -match_format_t match_writers_factory_t::get_writers_type (const string &n) +match_format_t match_writers_factory_t::get_writers_type (const std::string &n) { match_format_t format = match_format_t::SIMPLE; if (n == "jgf") @@ -436,7 +438,7 @@ match_format_t match_writers_factory_t::get_writers_type (const string &n) return format; } -bool known_match_format (const string &format) +bool known_match_format (const std::string &format) { return (format == "simple" || format == "jgf" From 96e9cf03498125c54723258cd40e61c8d293ca6a Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 22:26:56 -0700 Subject: [PATCH 12/14] query: use fully qualified path for std symbols Remove "using namespace std" and instead explicitly use std:: prefix for std symbols in resource-query.cpp and command.cpp. --- resource/utilities/command.cpp | 192 ++++++++++++++------------ resource/utilities/resource-query.cpp | 141 ++++++++++--------- 2 files changed, 182 insertions(+), 151 deletions(-) diff --git a/resource/utilities/command.cpp b/resource/utilities/command.cpp index 71cbfe665..caae53210 100644 --- a/resource/utilities/command.cpp +++ b/resource/utilities/command.cpp @@ -34,14 +34,13 @@ extern "C" { namespace Flux { namespace resource_model { -using namespace std; using namespace Flux::Jobspec; struct command_t { - string name; - string abbr; + std::string name; + std::string abbr; cmd_func_f *cmd; - string note; + std::string note; }; command_t commands[] = { @@ -74,29 +73,30 @@ static int do_remove (std::shared_ptr &ctx, int64_t jobid) info->state = job_lifecycle_t::CANCELLED; } } else { - cout << ctx->traverser->err_message (); + std::cout << ctx->traverser->err_message (); ctx->traverser->clear_err_message (); } return rc; } static void print_schedule_info (std::shared_ptr &ctx, - ostream &out, uint64_t jobid, - string &jobspec_fn, bool matched, int64_t at, - double elapse, bool sat) + std::ostream &out, uint64_t jobid, + std::string &jobspec_fn, bool matched, + int64_t at, double elapse, bool sat) { if (matched) { job_lifecycle_t st; - string mode = (at == 0)? "ALLOCATED" : "RESERVED"; - string scheduled_at = (at == 0)? "Now" : to_string (at); - out << "INFO:" << " =============================" << endl; - out << "INFO:" << " JOBID=" << jobid << endl; - out << "INFO:" << " RESOURCES=" << mode << endl; - out << "INFO:" << " SCHEDULED AT=" << scheduled_at << endl; + std::string mode = (at == 0)? "ALLOCATED" : "RESERVED"; + std::string scheduled_at = (at == 0)? "Now" : std::to_string (at); + out << "INFO:" << " =============================" << std::endl; + out << "INFO:" << " JOBID=" << jobid << std::endl; + out << "INFO:" << " RESOURCES=" << mode << std::endl; + out << "INFO:" << " SCHEDULED AT=" << scheduled_at << std::endl; if (ctx->params.elapse_time) - out << "INFO:" << " ELAPSE=" << to_string (elapse) << endl; + std::cout << "INFO:" << " ELAPSE=" << std::to_string (elapse) + << std::endl; - out << "INFO:" << " =============================" << endl; + out << "INFO:" << " =============================" << std::endl; st = (at == 0)? job_lifecycle_t::ALLOCATED : job_lifecycle_t::RESERVED; ctx->jobs[jobid] = std::make_shared (jobid, st, at, jobspec_fn, @@ -106,14 +106,15 @@ static void print_schedule_info (std::shared_ptr &ctx, else ctx->reservations[jobid] = jobid; } else { - out << "INFO:" << " =============================" << endl; - out << "INFO: " << "No matching resources found" << endl; + out << "INFO:" << " =============================" << std::endl; + out << "INFO: " << "No matching resources found" << std::endl; if (!sat) - out << "INFO: " << "Unsatisfiable request" << endl; - out << "INFO:" << " JOBID=" << jobid << endl; + out << "INFO: " << "Unsatisfiable request" << std::endl; + out << "INFO:" << " JOBID=" << jobid << std::endl; if (ctx->params.elapse_time) - out << "INFO:" << " ELAPSE=" << to_string (elapse) << endl; - out << "INFO:" << " =============================" << endl; + out << "INFO:" << " ELAPSE=" << std::to_string (elapse) + << std::endl; + out << "INFO:" << " =============================" << std::endl; } ctx->jobid_counter++; } @@ -133,16 +134,17 @@ double get_elapse_time (timeval &st, timeval &et) return ts2 - ts1; } -int cmd_match (std::shared_ptr &ctx, vector &args) +int cmd_match (std::shared_ptr &ctx, + std::vector &args) { if (args.size () != 3) { - cerr << "ERROR: malformed command" << endl; + std::cerr << "ERROR: malformed command" << std::endl; return 0; } - string subcmd = args[1]; + std::string subcmd = args[1]; if (!(subcmd == "allocate" || subcmd == "allocate_orelse_reserve" || subcmd == "allocate_with_satisfiability")) { - cerr << "ERROR: unknown subcmd " << args[1] << endl; + std::cerr << "ERROR: unknown subcmd " << args[1] << std::endl; return 0; } @@ -151,12 +153,12 @@ int cmd_match (std::shared_ptr &ctx, vector &args) bool sat = true; int64_t at = 0; int64_t jobid = ctx->jobid_counter; - string &jobspec_fn = args[2]; - ifstream jobspec_in; + std::string &jobspec_fn = args[2]; + std::ifstream jobspec_in; jobspec_in.exceptions (std::ifstream::failbit | std::ifstream::badbit); jobspec_in.open (jobspec_fn); Flux::Jobspec::Jobspec job {jobspec_in}; - stringstream o; + std::stringstream o; double elapse = 0.0f; struct timeval st, et; @@ -179,7 +181,8 @@ int cmd_match (std::shared_ptr &ctx, vector &args) sat = false; ctx->writers->emit (o); - ostream &out = (ctx->params.r_fname != "")? ctx->params.r_out : cout; + std::ostream &out = (ctx->params.r_fname != "")? ctx->params.r_out + : std::cout; out << o.str (); gettimeofday (&et, NULL); @@ -190,24 +193,26 @@ int cmd_match (std::shared_ptr &ctx, vector &args) jobspec_fn, rc == 0, at, elapse, sat); jobspec_in.close (); - } catch (ifstream::failure &e) { - cerr << "ERROR: Exception occurs for input file I/O" << e.what () << endl; + } catch (std::ifstream::failure &e) { + std::cerr << "ERROR: Exception occurs for input file I/O" + << e.what () << std::endl; } catch (parse_error &e) { - cerr << "ERROR: Jobspec error for " << ctx->jobid_counter <<": " - << e.what () << endl; + std::cerr << "ERROR: Jobspec error for " << ctx->jobid_counter <<": " + << e.what () << std::endl; } return 0; } -int cmd_cancel (std::shared_ptr &ctx, vector &args) +int cmd_cancel (std::shared_ptr &ctx, + std::vector &args) { if (args.size () != 2) { - cerr << "ERROR: malformed command" << endl; + std::cerr << "ERROR: malformed command" << std::endl; return 0; } int rc = -1; - string jobid_str = args[1]; + std::string jobid_str = args[1]; uint64_t jobid = (uint64_t)std::strtoll (jobid_str.c_str (), NULL, 10); if (ctx->allocations.find (jobid) != ctx->allocations.end ()) { @@ -217,13 +222,14 @@ int cmd_cancel (std::shared_ptr &ctx, vector &args) if ( (rc = do_remove (ctx, jobid)) == 0) ctx->reservations.erase (jobid); } else { - cerr << "ERROR: nonexistent job " << jobid << endl; + std::cerr << "ERROR: nonexistent job " << jobid << std::endl; goto done; } if (rc != 0) { - cerr << "ERROR: error encountered while removing job " << jobid << endl; - cerr << "ERROR: " << strerror (errno) << endl; + std::cerr << "ERROR: error encountered while removing job " + << jobid << std::endl; + std::cerr << "ERROR: " << strerror (errno) << std::endl; } done: @@ -234,19 +240,20 @@ int cmd_set_property (std::shared_ptr &ctx, std::vector &args) { if (args.size () != 3) { - cerr << "ERROR: malformed command" << endl; + std::cerr << "ERROR: malformed command" << std::endl; return 0; } - string resource_path = args[1]; - string property_key, property_value; - ostream &out = (ctx->params.r_fname != "") ? ctx->params.r_out : cout; + std::string resource_path = args[1]; + std::string property_key, property_value; + std::ostream &out = (ctx->params.r_fname != "") ? ctx->params.r_out + : std::cout; size_t pos = args[2].find ('='); - if (pos == 0 || (pos == args[2].size () - 1) || pos == string::npos) { - out << "Incorrect input format. " << endl + if (pos == 0 || (pos == args[2].size () - 1) || pos == std::string::npos) { + out << "Incorrect input format. " << std::endl << "Please use `set-property PROPERTY=VALUE`." - << endl; + << std::endl; return 0; } else { @@ -259,7 +266,7 @@ int cmd_set_property (std::shared_ptr &ctx, if (it == ctx->db.metadata.by_path.end ()) { out << "Couldn't find path " << resource_path - << " in resource graph." << endl; + << " in resource graph." << std::endl; } else { vtx_t v = it->second; @@ -272,8 +279,9 @@ int cmd_set_property (std::shared_ptr &ctx, != ctx->db.resource_graph[v].properties.end ()) { ctx->db.resource_graph[v].properties.erase (property_key); } - ctx->db.resource_graph[v].properties.insert - (pair (property_key,property_value)); + ctx->db.resource_graph[v].properties.insert ( + std::pair (property_key, + property_value)); } return 0; } @@ -282,73 +290,77 @@ int cmd_get_property (std::shared_ptr &ctx, std::vector &args) { if (args.size () != 2) { - cerr << "ERROR: malformed command" << endl; + std::cerr << "ERROR: malformed command" << std::endl; return 0; } - string resource_path = args[1]; - ostream &out = (ctx->params.r_fname != "") ? ctx->params.r_out : cout; + std::string resource_path = args[1]; + std::ostream &out = (ctx->params.r_fname != "") ? ctx->params.r_out + : std::cout; std::map::const_iterator it = ctx->db.metadata.by_path.find (resource_path); if (it == ctx->db.metadata.by_path.end ()) { out << "Could not find path " << resource_path - << " in resource graph." << endl; + << " in resource graph." << std::endl; } else { vtx_t v = it->second; if (ctx->db.resource_graph[v].properties.size () == 0) { out << "No properties were found for " << resource_path - << ". " << endl; + << ". " << std::endl; } else { std::map::const_iterator p_it; for (p_it = ctx->db.resource_graph[v].properties.begin (); p_it != ctx->db.resource_graph[v].properties.end (); p_it++) - out << p_it->first << "=" << p_it->second << endl; + out << p_it->first << "=" << p_it->second << std::endl; } } return 0; } -int cmd_list (std::shared_ptr &ctx, vector &args) +int cmd_list (std::shared_ptr &ctx, + std::vector &args) { for (auto &kv: ctx->jobs) { std::shared_ptr info = kv.second; - string mode; + std::string mode; get_jobstate_str (info->state, mode); - cout << "INFO: " << info->jobid << ", " << mode << ", " - << info->scheduled_at << ", " << info->jobspec_fn << ", " - << info->overhead << endl; + std::cout << "INFO: " << info->jobid << ", " << mode << ", " + << info->scheduled_at << ", " << info->jobspec_fn << ", " + << info->overhead << std::endl; } return 0; } -int cmd_info (std::shared_ptr &ctx, vector &args) +int cmd_info (std::shared_ptr &ctx, + std::vector &args) { if (args.size () != 2) { - cerr << "ERROR: malformed command" << endl; + std::cerr << "ERROR: malformed command" << std::endl; return 0; } uint64_t jobid = (uint64_t)std::atoll(args[1].c_str ()); if (ctx->jobs.find (jobid) == ctx->jobs.end ()) { - cout << "ERROR: jobid doesn't exist: " << args[1] << endl; + std::cout << "ERROR: jobid doesn't exist: " << args[1] << std::endl; return 0; } - string mode; + std::string mode; std::shared_ptr info = ctx->jobs[jobid]; get_jobstate_str (info->state, mode); - cout << "INFO: " << info->jobid << ", " << mode << ", " - << info->scheduled_at << ", " << info->jobspec_fn << ", " - << info->overhead << endl; + std::cout << "INFO: " << info->jobid << ", " << mode << ", " + << info->scheduled_at << ", " << info->jobspec_fn << ", " + << info->overhead << std::endl; return 0; } -int cmd_stat (std::shared_ptr &ctx, vector &args) +int cmd_stat (std::shared_ptr &ctx, + std::vector &args) { if (args.size () != 1) { - cerr << "ERROR: malformed command" << endl; + std::cerr << "ERROR: malformed command" << std::endl; return 0; } double avg = 0.0f; @@ -358,31 +370,35 @@ int cmd_stat (std::shared_ptr &ctx, vector &args) avg = ctx->perf.accum / (double)ctx->jobs.size (); min = ctx->perf.min; } - cout << "INFO: Num. of Jobs Matched: " << ctx->jobs.size () << endl; - cout << "INFO: Min. Match Time: " << min << endl; - cout << "INFO: Max. Match Time: " << ctx->perf.max << endl; - cout << "INFO: Avg. Match Time: " << avg << endl; + std::cout << "INFO: Num. of Jobs Matched: " << ctx->jobs.size () + << std::endl; + std::cout << "INFO: Min. Match Time: " << min << std::endl; + std::cout << "INFO: Max. Match Time: " << ctx->perf.max << std::endl; + std::cout << "INFO: Avg. Match Time: " << avg << std::endl; return 0; } -int cmd_cat (std::shared_ptr &ctx, vector &args) +int cmd_cat (std::shared_ptr &ctx, + std::vector &args) { - string &jspec_filename = args[1]; - ifstream jspec_in; + std::string &jspec_filename = args[1]; + std::ifstream jspec_in; jspec_in.open (jspec_filename); - string line; + std::string line; while (getline (jspec_in, line)) - cout << line << endl; - cout << "INFO: " << "Jobspec in " << jspec_filename << endl; + std::cout << line << std::endl; + std::cout << "INFO: " << "Jobspec in " << jspec_filename + << std::endl; jspec_in.close (); return 0; } -int cmd_help (std::shared_ptr &ctx, vector &args) +int cmd_help (std::shared_ptr &ctx, + std::vector &args) { bool multi = true; bool found = false; - string cmd = "unknown"; + std::string cmd = "unknown"; if (args.size () == 2) { multi = false; @@ -391,23 +407,25 @@ int cmd_help (std::shared_ptr &ctx, vector &args) for (int i = 0; commands[i].name != "NA"; ++i) { if (multi || cmd == commands[i].name || cmd == commands[i].abbr) { - cout << "INFO: " << commands[i].name << " (" << commands[i].abbr - << ")" << " -- " << commands[i].note << endl; + std::cout << "INFO: " << commands[i].name << " (" + << commands[i].abbr << ")" << " -- " + << commands[i].note << std::endl; found = true; } } if (!multi && !found) - cout << "ERROR: unknown command: " << cmd << endl; + std::cout << "ERROR: unknown command: " << cmd << std::endl; return 0; } -int cmd_quit (std::shared_ptr &ctx, vector &args) +int cmd_quit (std::shared_ptr &ctx, + std::vector &args) { return -1; } -cmd_func_f *find_cmd (const string &cmd_str) +cmd_func_f *find_cmd (const std::string &cmd_str) { for (int i = 0; commands[i].name != "NA"; ++i) { if (cmd_str == commands[i].name) diff --git a/resource/utilities/resource-query.cpp b/resource/utilities/resource-query.cpp index b5985197e..1e3b7035b 100644 --- a/resource/utilities/resource-query.cpp +++ b/resource/utilities/resource-query.cpp @@ -45,7 +45,6 @@ extern "C" { #endif } -using namespace std; using namespace Flux::resource_model; #define OPTIONS "L:f:W:S:P:F:g:o:p:t:r:edh" @@ -69,7 +68,7 @@ static const struct option longopts[] = { static void usage (int code) { - cerr << + std::cerr << "usage: resource-query [OPTIONS...]\n" "\n" "Command-line utility that takes in an HPC resource request written in\n" @@ -209,19 +208,19 @@ static void set_default_params (std::shared_ptr &ctx) ctx->params.disable_prompt = false; } -static int string_to_graph_format (string st, emit_format_t &format) +static int string_to_graph_format (std::string st, emit_format_t &format) { int rc = 0; - if (boost::iequals (st, string ("dot"))) + if (boost::iequals (st, std::string ("dot"))) format = emit_format_t::GRAPHVIZ_DOT; - else if (boost::iequals (st, string ("graphml"))) + else if (boost::iequals (st, std::string ("graphml"))) format = emit_format_t::GRAPH_ML; else rc = -1; return rc; } -static int graph_format_to_ext (emit_format_t format, string &st) +static int graph_format_to_ext (emit_format_t format, std::string &st) { int rc = 0; switch (format) { @@ -252,59 +251,59 @@ static int set_subsystems_use (std::shared_ptr &ctx, int rc = 0; ctx->matcher->set_matcher_name (n); dfu_match_cb_t &matcher = *(ctx->matcher); - const string &matcher_type = matcher.matcher_name (); + const std::string &matcher_type = matcher.matcher_name (); - if (boost::iequals (matcher_type, string ("CA"))) { + if (boost::iequals (matcher_type, std::string ("CA"))) { if ( (rc = subsystem_exist (ctx, "containment")) == 0) matcher.add_subsystem ("containment", "*"); - } else if (boost::iequals (matcher_type, string ("IBA"))) { + } else if (boost::iequals (matcher_type, std::string ("IBA"))) { if ( (rc = subsystem_exist (ctx, "ibnet")) == 0) matcher.add_subsystem ("ibnet", "*"); - } else if (boost::iequals (matcher_type, string ("IBBA"))) { + } else if (boost::iequals (matcher_type, std::string ("IBBA"))) { if ( (rc = subsystem_exist (ctx, "ibnetbw")) == 0) matcher.add_subsystem ("ibnetbw", "*"); - } else if (boost::iequals (matcher_type, string ("PFS1BA"))) { + } else if (boost::iequals (matcher_type, std::string ("PFS1BA"))) { if ( (rc = subsystem_exist (ctx, "pfs1bw")) == 0) matcher.add_subsystem ("pfs1bw", "*"); - } else if (boost::iequals (matcher_type, string ("PA"))) { + } else if (boost::iequals (matcher_type, std::string ("PA"))) { if ( (rc = subsystem_exist (ctx, "power")) == 0) matcher.add_subsystem ("power", "*"); - } else if (boost::iequals (matcher_type, string ("C+PFS1BA"))) { + } else if (boost::iequals (matcher_type, std::string ("C+PFS1BA"))) { if ( (rc = subsystem_exist (ctx, "containment")) == 0) matcher.add_subsystem ("containment", "contains"); if ( !rc && (rc = subsystem_exist (ctx, "pfs1bw")) == 0) matcher.add_subsystem ("pfs1bw", "*"); - } else if (boost::iequals (matcher_type, string ("C+IBA"))) { + } else if (boost::iequals (matcher_type, std::string ("C+IBA"))) { if ( (rc = subsystem_exist (ctx, "containment")) == 0) matcher.add_subsystem ("containment", "contains"); if ( !rc && (rc = subsystem_exist (ctx, "ibnet")) == 0) matcher.add_subsystem ("ibnet", "connected_up"); - } else if (boost::iequals (matcher_type, string ("C+PA"))) { + } else if (boost::iequals (matcher_type, std::string ("C+PA"))) { if ( (rc = subsystem_exist (ctx, "containment")) == 0) matcher.add_subsystem ("containment", "*"); if ( !rc && (rc = subsystem_exist (ctx, "power")) == 0) matcher.add_subsystem ("power", "draws_from"); - } else if (boost::iequals (matcher_type, string ("IB+IBBA"))) { + } else if (boost::iequals (matcher_type, std::string ("IB+IBBA"))) { if ( (rc = subsystem_exist (ctx, "ibnet")) == 0) matcher.add_subsystem ("ibnet", "connected_down"); if ( !rc && (rc = subsystem_exist (ctx, "ibnetbw")) == 0) matcher.add_subsystem ("ibnetbw", "*"); - } else if (boost::iequals (matcher_type, string ("C+P+IBA"))) { + } else if (boost::iequals (matcher_type, std::string ("C+P+IBA"))) { if ( (rc = subsystem_exist (ctx, "containment")) == 0) matcher.add_subsystem ("containment", "contains"); if ( (rc = subsystem_exist (ctx, "power")) == 0) matcher.add_subsystem ("power", "draws_from"); if ( !rc && (rc = subsystem_exist (ctx, "ibnet")) == 0) matcher.add_subsystem ("ibnet", "connected_up"); - } else if (boost::iequals (matcher_type, string ("V+PFS1BA"))) { + } else if (boost::iequals (matcher_type, std::string ("V+PFS1BA"))) { if ( (rc = subsystem_exist (ctx, "virtual1")) == 0) matcher.add_subsystem ("virtual1", "*"); if ( !rc && (rc = subsystem_exist (ctx, "pfs1bw")) == 0) matcher.add_subsystem ("pfs1bw", "*"); - } else if (boost::iequals (matcher_type, string ("VA"))) { + } else if (boost::iequals (matcher_type, std::string ("VA"))) { if ( (rc = subsystem_exist (ctx, "virtual1")) == 0) matcher.add_subsystem ("virtual1", "*"); - } else if (boost::iequals (matcher_type, string ("ALL"))) { + } else if (boost::iequals (matcher_type, std::string ("ALL"))) { if ( (rc = subsystem_exist (ctx, "containment")) == 0) matcher.add_subsystem ("containment", "*"); if ( !rc && (rc = subsystem_exist (ctx, "ibnet")) == 0) @@ -322,7 +321,7 @@ static int set_subsystems_use (std::shared_ptr &ctx, } static void write_to_graphviz (f_resource_graph_t &fg, subsystem_t ss, - fstream &o) + std::fstream &o) { f_res_name_map_t vmap = get (&resource_pool_t::name, fg); f_edg_infra_map_t emap = get (&resource_relation_t::idata, fg); @@ -331,9 +330,10 @@ static void write_to_graphviz (f_resource_graph_t &fg, subsystem_t ss, write_graphviz (o, fg, vwr, ewr); } -static void flatten (f_resource_graph_t &fg, map &paths, - map &subsystems, - map &esubsystems) +static void flatten (f_resource_graph_t &fg, + std::map &paths, + std::map &subsystems, + std::map &esubsystems) { f_vtx_iterator_t vi, v_end; f_edg_iterator_t ei, e_end; @@ -359,15 +359,19 @@ static void flatten (f_resource_graph_t &fg, map &paths, } } -static void write_to_graphml (f_resource_graph_t &fg, fstream &o) +static void write_to_graphml (f_resource_graph_t &fg, std::fstream &o) { boost::dynamic_properties dp; - map esubsystems; - map subsystems, properties, paths; - boost::associative_property_map> subsystems_map (subsystems); - boost::associative_property_map> esubsystems_map (esubsystems); - boost::associative_property_map> props_map (properties); - boost::associative_property_map> paths_map (paths); + std::map esubsystems; + std::map subsystems, properties, paths; + boost::associative_property_map< + std::map> subsystems_map (subsystems); + boost::associative_property_map< + std::map> esubsystems_map (esubsystems); + boost::associative_property_map< + std::map> props_map (properties); + boost::associative_property_map< + std::map> paths_map (paths); flatten (fg, paths, subsystems, esubsystems); @@ -390,13 +394,14 @@ static void write_to_graphml (f_resource_graph_t &fg, fstream &o) static void write_to_graph (std::shared_ptr &ctx) { - fstream o; - string fn, mn; + std::fstream o; + std::string fn, mn; mn = ctx->matcher->matcher_name (); fn = ctx->params.o_fname + "." + ctx->params.o_fext; - cout << "INFO: Write the target graph of the matcher..." << endl; - o.open (fn, fstream::out); + std::cout << "INFO: Write the target graph of the matcher..." + << std::endl; + o.open (fn, std::fstream::out); switch (ctx->params.o_format) { case emit_format_t::GRAPHVIZ_DOT: @@ -406,11 +411,11 @@ static void write_to_graph (std::shared_ptr &ctx) write_to_graphml (*(ctx->fgraph), o); break; default: - cout << "ERROR: Unknown graph format" << endl; + std::cout << "ERROR: Unknown graph format" << std::endl; break; } if (o.bad ()) { - cerr << "ERROR: Failure encountered in writing" << endl; + std::cerr << "ERROR: Failure encountered in writing" << std::endl; o.clear (); } o.close (); @@ -424,18 +429,19 @@ static void control_loop (std::shared_ptr &ctx) : readline ("resource-query> "); if (line == NULL) continue; - else if(*line) + else if (*line) add_history (line); - vector tokens; - istringstream iss (line); - copy(istream_iterator(iss), istream_iterator(), + std::vector tokens; + std::istringstream iss (line); + std::copy (std::istream_iterator (iss), + std::istream_iterator (), back_inserter (tokens)); free(line); if (tokens.empty ()) continue; - string &cmd_str = tokens[0]; + std::string &cmd_str = tokens[0]; if (!(cmd = find_cmd (cmd_str))) continue; if (cmd (ctx, tokens) != 0) @@ -447,7 +453,7 @@ static int populate_resource_db (std::shared_ptr &ctx) { int rc = -1; double elapse; - ifstream in_file; + std::ifstream in_file; struct timeval st, et; std::stringstream buffer{}; std::shared_ptr rd; @@ -509,7 +515,8 @@ static std::shared_ptr create_filtered_graph ( fg = std::make_shared (g, edgsel, vtxsel); } catch (std::bad_alloc &e) { errno = ENOMEM; - cerr << "ERROR: out of memory allocating f_resource_graph_t" << endl; + std::cerr << "ERROR: out of memory allocating f_resource_graph_t" + << std::endl; fg = nullptr; } @@ -521,15 +528,17 @@ static int init_resource_graph (std::shared_ptr &ctx) int rc = 0; if ( (rc = populate_resource_db (ctx)) != 0) { - cerr << "ERROR: can't populate graph resource database" << endl; + std::cerr << "ERROR: can't populate graph resource database" + << std::endl; return rc; } resource_graph_t &g = ctx->db.resource_graph; // Configure the matcher and its subsystem selector - cout << "INFO: Loading a matcher: " << ctx->params.matcher_name << endl; + std::cout << "INFO: Loading a matcher: " << ctx->params.matcher_name + << std::endl; if ( (rc = set_subsystems_use (ctx, ctx->params.matcher_name)) != 0) { - cerr << "ERROR: Not all subsystems found" << endl; + std::cerr << "ERROR: Not all subsystems found" << std::endl; return rc; } if ( !(ctx->fgraph = create_filtered_graph (ctx))) @@ -540,8 +549,9 @@ static int init_resource_graph (std::shared_ptr &ctx) && ctx->matcher->set_pruning_types_w_spec (ctx->matcher->dom_subsystem (), ctx->params.prune_filters) < 0) { - cerr << "ERROR: setting pruning filters with ctx->params.prune_filters: " - << ctx->params.prune_filters << endl; + std::cerr + << "ERROR: setting pruning filters with ctx->params.prune_filters: " + << ctx->params.prune_filters << std::endl; return -1; } @@ -555,19 +565,20 @@ static int init_resource_graph (std::shared_ptr &ctx) ctx->traverser = std::make_shared (); } catch (std::bad_alloc &e) { errno = ENOMEM; - cerr << "ERROR: out of memory allocating traverser" << endl; + std::cerr << "ERROR: out of memory allocating traverser" << std::endl; return -1; } if ( (rc = ctx->traverser->initialize (ctx->fgraph, ctx->db.metadata.roots, ctx->matcher)) != 0) { - cerr << "ERROR: initializing traverser" << endl; + std::cerr << "ERROR: initializing traverser" << std::endl; return -1; } match_format_t format = match_writers_factory_t:: get_writers_type (ctx->params.match_format); if ( !(ctx->writers = match_writers_factory_t::create (format))) { - cerr << "ERROR: out of memory allocating traverser" << endl; + std::cerr << "ERROR: out of memory allocating traverser" + << std::endl; return -1; } @@ -593,7 +604,7 @@ static void process_args (std::shared_ptr &ctx, ctx->params.load_format = optarg; if (!known_resource_reader (ctx->params.load_format)) { std::cerr << "[ERROR] unknown format for --load-format: "; - std::cerr << optarg << endl; + std::cerr << optarg << std::endl; usage (1); } break; @@ -613,16 +624,16 @@ static void process_args (std::shared_ptr &ctx, case 'F': /* --match-format */ ctx->params.match_format = optarg; if (!known_match_format (ctx->params.match_format)) { - cerr << "[ERROR] unknown format for --match-format: "; - cerr << optarg << endl; + std::cerr << "[ERROR] unknown format for --match-format: "; + std::cerr << optarg << std::endl; usage (1); } break; case 'g': /* --graph-format */ rc = string_to_graph_format (optarg, ctx->params.o_format); if ( rc != 0) { - cerr << "[ERROR] unknown format for --graph-format: "; - cerr << optarg << endl; + std::cerr << "[ERROR] unknown format for --graph-format: "; + std::cerr << optarg << std::endl; usage (1); } graph_format_to_ext (ctx->params.o_format, ctx->params.o_fext); @@ -646,8 +657,9 @@ static void process_args (std::shared_ptr &ctx, if ( (ctx->params.reserve_vtx_vec < 0) || (ctx->params.reserve_vtx_vec > 2000000)) { ctx->params.reserve_vtx_vec = 0; - cerr << "WARN: out of range specified for --reserve-vtx-vec: "; - cerr << optarg << endl; + std::cerr + << "WARN: out of range specified for --reserve-vtx-vec: "; + std::cerr << optarg << std::endl; } break; case 'e': /* --elapse-time */ @@ -674,7 +686,8 @@ static std::shared_ptr init_resource_query (int c, try { ctx = std::make_shared (); } catch (std::bad_alloc &e) { - cerr << "ERROR: out of memory allocating resource context" << endl; + std::cerr << "ERROR: out of memory allocating resource context" + << std::endl; errno = ENOMEM; goto done; } @@ -685,12 +698,12 @@ static std::shared_ptr init_resource_query (int c, ctx->perf.max = 0.0f; ctx->perf.accum = 0.0f; if ( !(ctx->matcher = create_match_cb (ctx->params.matcher_policy))) { - cerr << "ERROR: unknown match policy " << endl; - cerr << "ERROR: " << ctx->params.matcher_policy << endl; + std::cerr << "ERROR: unknown match policy " << std::endl; + std::cerr << "ERROR: " << ctx->params.matcher_policy << std::endl; ctx = nullptr; } if (init_resource_graph (ctx) != 0) { - cerr << "ERROR: resource graph initialization" << endl; + std::cerr << "ERROR: resource graph initialization" << std::endl; ctx = nullptr; } @@ -710,7 +723,7 @@ int main (int argc, char *argv[]) { std::shared_ptr ctx = nullptr; if ( !(ctx = init_resource_query (argc, argv))) { - cerr << "ERROR: resource query initialization" << endl; + std::cerr << "ERROR: resource query initialization" << std::endl; return EXIT_FAILURE; } From 1d6fc8b97cae331e2ef0e84d18448341f0dc73d0 Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 22:30:12 -0700 Subject: [PATCH 13/14] grug2dot: use std:: prefix --- resource/utilities/grug2dot.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/resource/utilities/grug2dot.cpp b/resource/utilities/grug2dot.cpp index d845ffb4e..4df72c89d 100644 --- a/resource/utilities/grug2dot.cpp +++ b/resource/utilities/grug2dot.cpp @@ -32,7 +32,6 @@ extern "C" { #endif } -using namespace std; using namespace Flux::resource_model; #define OPTIONS "hm" @@ -44,7 +43,7 @@ static const struct option longopts[] = { void usage (int code) { - cerr << + std::cerr << "Usage: grug2dot .graphml\n" " Convert a resource-graph generator spec (.graphml)\n" " to AT&T GraphViz format (.dot). The output\n" @@ -83,15 +82,15 @@ int main (int argc, char *argv[]) usage (1); resource_gen_spec_t gspec; - string fn (argv[optind]); + std::string fn (argv[optind]); boost::filesystem::path path = fn; - string base = path.stem ().string (); + std::string base = path.stem ().string (); if (gspec.read_graphml (fn) != 0) { - cerr << "Error in reading " << fn << endl; + std::cerr << "Error in reading " << fn << std::endl; rc = -1; } else if (gspec.write_graphviz (base + ".dot", simple) != 0) { - cerr << "Error in writing " << base + ".dot" << endl; + std::cerr << "Error in writing " << base + ".dot" << std::endl; rc = -1; } From fd9207a845a2615bc2a7a7aea86c110c21f3119f Mon Sep 17 00:00:00 2001 From: "Dong H. Ahn" Date: Mon, 14 Oct 2019 22:38:30 -0700 Subject: [PATCH 14/14] jobspec: add full std namespace support --- resource/libjobspec/jobspec.cpp | 108 ++++++++++++++++---------------- 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/resource/libjobspec/jobspec.cpp b/resource/libjobspec/jobspec.cpp index 9db5dc472..a22b50d09 100644 --- a/resource/libjobspec/jobspec.cpp +++ b/resource/libjobspec/jobspec.cpp @@ -34,7 +34,6 @@ extern "C" { #endif } -using namespace std; using namespace Flux::Jobspec; parse_error::parse_error(const char *msg) @@ -130,7 +129,7 @@ void parse_yaml_count (Resource& res, const YAML::Node &cnode) } namespace { -vector parse_yaml_resources (const YAML::Node &resources); +std::vector parse_yaml_resources (const YAML::Node &resources); } Resource::Resource (const YAML::Node &resnode) @@ -148,7 +147,7 @@ Resource::Resource (const YAML::Node &resnode) throw parse_error (resnode["type"], "Value of \"type\" must be a scalar"); } - type = resnode["type"].as(); + type = resnode["type"].as(); field_count++; if (!resnode["count"]) { @@ -163,7 +162,7 @@ Resource::Resource (const YAML::Node &resnode) "Value of \"unit\" must be a scalar"); } field_count++; - unit = resnode["unit"].as(); + unit = resnode["unit"].as(); } if (resnode["exclusive"]) { if (!resnode["exclusive"].IsScalar()) { @@ -171,7 +170,7 @@ Resource::Resource (const YAML::Node &resnode) "Value of \"exclusive\" must be a scalar"); } field_count++; - string val = resnode["exclusive"].as(); + std::string val = resnode["exclusive"].as(); if (val == "false") { exclusive = tristate_t::FALSE; } else if (val == "true") { @@ -193,7 +192,7 @@ Resource::Resource (const YAML::Node &resnode) "Value of \"label\" must be a scalar"); } field_count++; - label = resnode["label"].as(); + label = resnode["label"].as(); } else if (type == "slot") { throw parse_error (resnode, "All slots must be labeled"); } @@ -204,7 +203,7 @@ Resource::Resource (const YAML::Node &resnode) "Value of \"id\" must be a scalar"); } field_count++; - id = resnode["id"].as(); + id = resnode["id"].as(); } if (field_count != resnode.size()) { @@ -228,9 +227,9 @@ Task::Task (const YAML::Node &tasknode) throw parse_error (tasknode, "Key \"command\" missing from task"); } if (tasknode["command"].IsSequence()) { - command = tasknode["command"].as>(); + command = tasknode["command"].as>(); } else if (tasknode["command"].IsScalar()) { - command.push_back(tasknode["command"].as()); + command.push_back(tasknode["command"].as()); } else { throw parse_error (tasknode["command"], "\"command\" value must be a scalar or a sequence"); @@ -244,7 +243,7 @@ Task::Task (const YAML::Node &tasknode) throw parse_error (tasknode["slot"], "Value of task \"slot\" must be a YAML scalar"); } - slot = tasknode["slot"].as(); + slot = tasknode["slot"].as(); /* Import count mapping */ if (tasknode["count"]) { @@ -254,7 +253,8 @@ Task::Task (const YAML::Node &tasknode) "\"count\" in task is not a mapping"); } for (auto&& entry : count_node) { - count[entry.first.as()] = entry.second.as(); + count[entry.first.as()] + = entry.second.as(); } } @@ -264,7 +264,7 @@ Task::Task (const YAML::Node &tasknode) throw parse_error (tasknode["distribution"], "Value of task \"distribution\" must be a YAML scalar"); } - distribution = tasknode["distribution"].as(); + distribution = tasknode["distribution"].as(); } /* Import attributes mapping if it is present */ @@ -274,7 +274,8 @@ Task::Task (const YAML::Node &tasknode) throw parse_error (attrs, "\"attributes\" in task is not a mapping"); } for (auto&& attr : attrs) { - attributes[attr.first.as()] = attr.second.as(); + attributes[attr.first.as()] + = attr.second.as(); } } @@ -285,9 +286,9 @@ Task::Task (const YAML::Node &tasknode) } namespace { -vector parse_yaml_tasks (const YAML::Node &tasks) +std::vector parse_yaml_tasks (const YAML::Node &tasks) { - vector taskvec; + std::vector taskvec; /* "tasks" must be a sequence */ if (!tasks.IsSequence()) { @@ -303,9 +304,9 @@ vector parse_yaml_tasks (const YAML::Node &tasks) } namespace { -vector parse_yaml_resources (const YAML::Node &resources) +std::vector parse_yaml_resources (const YAML::Node &resources) { - vector resvec; + std::vector resvec; /* "resources" must be a sequence */ if (!resources.IsSequence()) { @@ -329,25 +330,25 @@ Attributes parse_yaml_attributes (const YAML::Node &attrs) throw parse_error (attrs, "\"attributes\" is not a map"); } for (auto&& kv : attrs) { - if (kv.first.as() == "user") { + if (kv.first.as() == "user") { a.user = kv.second; } - else if (kv.first.as() == "system") { + else if (kv.first.as() == "system") { for (auto&& s : kv.second) { - if (s.first.as() == "duration") { + if (s.first.as() == "duration") { a.system.duration = s.second.as(); } - else if (s.first.as() == "cwd") { - a.system.cwd = s.second.as(); + else if (s.first.as() == "cwd") { + a.system.cwd = s.second.as(); } - else if (s.first.as() == "environment") { + else if (s.first.as() == "environment") { for (auto&& e : s.second) { - a.system.environment[e.first.as()] - = e.second.as(); + a.system.environment[e.first.as()] + = e.second.as(); } } else { - a.system.optional[s.first.as()] = s.second; + a.system.optional[s.first.as()] = s.second; } } } @@ -476,24 +477,25 @@ class IndentingOStreambuf : public std::streambuf std::ostream& Flux::Jobspec::operator<<(std::ostream& s, Jobspec const& jobspec) { - s << "version: " << jobspec.version << endl; - s << "resources: " << endl; + s << "version: " << jobspec.version << std::endl; + s << "resources: " << std::endl; for (auto&& resource : jobspec.resources) { IndentingOStreambuf indent (s); s << resource; } - s << "tasks: " << endl; + s << "tasks: " << std::endl; for (auto&& task : jobspec.tasks) { IndentingOStreambuf indent (s); s << task; } - s << "attributes:" << endl; - s << " " << "system:" << endl; - s << " " << "duration: " << jobspec.attributes.system.duration << endl; - s << " " << "cwd: " << jobspec.attributes.system.cwd << endl; - s << " " << "environment:" << endl; + s << "attributes:" << std::endl; + s << " " << "system:" << std::endl; + s << " " << "duration: " << jobspec.attributes.system.duration + << std::endl; + s << " " << "cwd: " << jobspec.attributes.system.cwd << std::endl; + s << " " << "environment:" << std::endl; for (auto&& e : jobspec.attributes.system.environment) { - s << " " << e.first << ": " << e.second << endl; + s << " " << e.first << ": " << e.second << std::endl; } return s; @@ -502,24 +504,24 @@ std::ostream& Flux::Jobspec::operator<<(std::ostream& s, Jobspec const& jobspec) std::ostream& Flux::Jobspec::operator<<(std::ostream& s, Resource const& resource) { - s << "- type: " << resource.type << endl; - s << " count:" << endl; - s << " min: " << resource.count.min << endl; - s << " max: " << resource.count.max << endl; - s << " operator: " << resource.count.oper << endl; - s << " operand: " << resource.count.operand << endl; + s << "- type: " << resource.type << std::endl; + s << " count:" << std::endl; + s << " min: " << resource.count.min << std::endl; + s << " max: " << resource.count.max << std::endl; + s << " operator: " << resource.count.oper << std::endl; + s << " operand: " << resource.count.operand << std::endl; if (resource.unit.size() > 0) - s << " unit: " << resource.unit << endl; + s << " unit: " << resource.unit << std::endl; if (resource.label.size() > 0) - s << " label: " << resource.label << endl; + s << " label: " << resource.label << std::endl; if (resource.id.size() > 0) - s << " id: " << resource.id << endl; + s << " id: " << resource.id << std::endl; if (resource.exclusive == tristate_t::TRUE) - s << " exclusive: true" << endl; + s << " exclusive: true" << std::endl; else if (resource.exclusive == tristate_t::FALSE) - s << " exclusive: false" << endl; + s << " exclusive: false" << std::endl; if (resource.with.size() > 0) { - s << " with:" << endl; + s << " with:" << std::endl; IndentingOStreambuf indent (s, 4); for (auto&& child_resource : resource.with) { s << child_resource; @@ -541,19 +543,19 @@ std::ostream& Flux::Jobspec::operator<<(std::ostream& s, first = false; s << "\"" << field << "\""; } - s << " ]" << endl; - s << "slot: " << task.slot << endl; + s << " ]" << std::endl; + s << "slot: " << task.slot << std::endl; if (task.count.size() > 0) { - s << "count:" << endl; + s << "count:" << std::endl; IndentingOStreambuf indent (s); for (auto&& c : task.count) { - s << c.first << ": " << c.second << endl; + s << c.first << ": " << c.second << std::endl; } } if (task.distribution.size() > 0) - s << "distribution: " << task.distribution << endl; + s << "distribution: " << task.distribution << std::endl; if (task.attributes.size() > 0) { - s << "attributes:" << endl; + s << "attributes:" << std::endl; IndentingOStreambuf indent (s); for (auto&& attr : task.attributes) { s << attr.first << ": " << attr.second;