From 97a4b4dbf628a627e3c2d5689be89265f56074df Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Fri, 17 Dec 2010 18:04:45 +0100 Subject: [PATCH 1/1] Define process::print_loads_p() to print loads by using neighp[] instead of neigh[]. In order to achieve this cleanly, define neighbor::print(). The logging category is (by default) the default for the current compilation unit. I had to torture xbt/log to attain my goal... --- .gitignore | 1 + Makefile | 19 +++++++++--------- communicator.cpp | 7 ++++--- cost_func.cpp | 4 ++-- deployment.cpp | 7 ++++--- hostdata.cpp | 7 ++++--- loba_fairstrategy.cpp | 10 +++------- loba_simple.cpp | 4 ++-- main.cpp | 15 ++++++++------- misc.h | 6 ++++++ neighbor.cpp | 18 +++++++++++++++-- neighbor.h | 7 +++++++ options.cpp | 7 ++++--- process.cpp | 45 ++++++++++++++++++++++++++----------------- process.h | 19 ++++++++++++------ 15 files changed, 111 insertions(+), 65 deletions(-) diff --git a/.gitignore b/.gitignore index 06c5254..4995caf 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ core vgcore.* localversion +misc_autogen.h loba simple_async simgrid-dev diff --git a/Makefile b/Makefile index 2ab9e7b..c5202cf 100644 --- a/Makefile +++ b/Makefile @@ -28,9 +28,6 @@ MAKEDEPEND.FLAGS = $(CPPFLAGS) -MM -MF $@ $< MAKEDEPEND.C = $(CC) $(MAKEDEPEND.FLAGS) MAKEDEPEND.CXX = $(CXX) $(MAKEDEPEND.FLAGS) -LOCALVERSION := localversion -SETLOCALVERSION := ./setlocalversion - SRC.loba := main.cpp \ communicator.cpp \ cost_func.cpp \ @@ -51,15 +48,14 @@ DEP := $(SRC:%.cpp=%.d) TARGETS := loba simple_async -$(shell $(SETLOCALVERSION)) - .PHONY: all clean realclean all: $(TARGETS) clean: $(RM) core core.[0-9]* vgcore.[0-9]* - $(RM) $(LOCALVERSION) + $(RM) misc_autogen.h + $(RM) localversion $(RM) $(OBJ) $(RM) $(DEP) $(RM) $(TARGETS) @@ -70,12 +66,17 @@ realclean: clean %.d: %.c ; $(MAKEDEPEND.C) %.d: %.cpp ; $(MAKEDEPEND.CXX) +misc_autogen.h: $(SIMGRID_INSTALL_DIR)/include/xbt/log.h + grep -C1 '# *define *CLOG' $< \ + | sed 's/CLOG/XCLOG/;s/_XBT_LOGV\(([^)]*)\)/(*\1)/' > $@ + version.o: $(patsubst %.cpp,%.o,$(filter-out version.cpp, $(SRC.loba))) ifneq ($(MAKECMDGOALS),clean) -ifneq ($(MAKECMDGOALS),realclean) --include $(DEP) -endif + ifneq ($(MAKECMDGOALS),realclean) + $(shell ./setlocalversion) + -include $(DEP) + endif endif .SECONDEXPANSION: diff --git a/communicator.cpp b/communicator.cpp index e6653eb..054c98a 100644 --- a/communicator.cpp +++ b/communicator.cpp @@ -1,14 +1,15 @@ -#include "communicator.h" - #include #include #include #include #include + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm); + #include "misc.h" #include "options.h" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm); +#include "communicator.h" std::string message::to_string() { diff --git a/cost_func.cpp b/cost_func.cpp index d102fd9..7311199 100644 --- a/cost_func.cpp +++ b/cost_func.cpp @@ -1,11 +1,11 @@ -#include "cost_func.h" - #include #include #include #include #include +#include "cost_func.h" + cost_func::cost_func(const char* param) { int len = strlen(param); diff --git a/deployment.cpp b/deployment.cpp index a28d8d9..29de927 100644 --- a/deployment.cpp +++ b/deployment.cpp @@ -1,14 +1,15 @@ -#include "deployment.h" - #include #include #include #include #include + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(depl); + #include "hostdata.h" #include "options.h" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(depl); +#include "deployment.h" void MY_launch_application() { diff --git a/hostdata.cpp b/hostdata.cpp index 3ac7b0b..8a86eb9 100644 --- a/hostdata.cpp +++ b/hostdata.cpp @@ -1,14 +1,15 @@ -#include "hostdata.h" - #include #include #include #include #include + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main); + #include "misc.h" #include "options.h" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main); +#include "hostdata.h" std::vector hostdata::hosts; diff --git a/loba_fairstrategy.cpp b/loba_fairstrategy.cpp index 81de9e1..eb05b49 100644 --- a/loba_fairstrategy.cpp +++ b/loba_fairstrategy.cpp @@ -1,9 +1,10 @@ -#include "loba_fairstrategy.h" #include #include XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba); +#include "loba_fairstrategy.h" + /* simple version: * load balance with a least-loaded neighbor, * without breaking the ping-pong condition @@ -25,12 +26,7 @@ double loba_fairstrategy::load_balance(double my_load) std::sort(pneigh.begin(),pneigh.end(),compare()); - //print_loads(); - - INFO0("list voisins"); - for(unsigned i=0;i< pneigh.size() ; ++i) { - INFO2("%s %f ", pneigh[i]->get_name(), pneigh[i]->get_load()); - } + print_loads_p(); double sum_sent=0; bool found=true; diff --git a/loba_simple.cpp b/loba_simple.cpp index d44a1de..d79ead5 100644 --- a/loba_simple.cpp +++ b/loba_simple.cpp @@ -1,9 +1,9 @@ -#include "loba_simple.h" - #include XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba); +#include "loba_simple.h" + /* simple version: * load balance with a least-loaded neighbor, * without breaking the ping-pong condition diff --git a/main.cpp b/main.cpp index 3ee3ae1..fe584f7 100644 --- a/main.cpp +++ b/main.cpp @@ -3,13 +3,6 @@ #include #include #include -#include "deployment.h" -#include "hostdata.h" -#include "misc.h" -#include "options.h" -#include "process.h" -#include "timer.h" -#include "version.h" // Creates log categories XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages"); @@ -21,6 +14,14 @@ XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer"); XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main); +#include "deployment.h" +#include "hostdata.h" +#include "misc.h" +#include "options.h" +#include "process.h" +#include "timer.h" +#include "version.h" + // Failure exit status enum { EXIT_NO_FAILURE = 0x00, // no error diff --git a/misc.h b/misc.h index 2ec625b..974f666 100644 --- a/misc.h +++ b/misc.h @@ -11,6 +11,12 @@ #define LOG_ISENABLED(priority) \ (_XBT_LOG_ISENABLEDV((*_XBT_LOGV(default)), (priority))) +/* Defines XCLOGn(...) which behave like CLOGn(...), except that the + * given category is not passed through _XBT_LOGV before use. + * (auto-generated, like its name says...) + */ +#include "misc_autogen.h" + /* Returns c-string "s" if n > 1, empty string "" otherwise. */ #define ESSE(n) ((n) > 1 ? misc::str_esse : misc::str_nil) namespace misc { diff --git a/neighbor.cpp b/neighbor.cpp index 3340f8f..c45fc8f 100644 --- a/neighbor.cpp +++ b/neighbor.cpp @@ -1,8 +1,12 @@ -#include "neighbor.h" - #include #include +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc); // needed to compile neighbor.h + +#include "misc.h" + +#include "neighbor.h" + neighbor::neighbor(const char* hostname) : host((hostdata* )MSG_host_get_data(MSG_get_host_by_name(hostname))) , load(std::numeric_limits::infinity()) @@ -14,3 +18,13 @@ neighbor::neighbor(const char* hostname) neighbor::~neighbor() { } + +void neighbor::print(bool verbose, + e_xbt_log_priority_t logp, xbt_log_category_t cat) const +{ + if (verbose) + XCLOG4(cat, logp, "%s: load = %g ; debt = %g ; to_send = %g", + get_name(), get_load(), get_debt(), get_to_send()); + else + XCLOG2(cat, logp, "%s: load = %g", get_name(), get_load()); +} diff --git a/neighbor.h b/neighbor.h index 58f6c1c..ba50f2e 100644 --- a/neighbor.h +++ b/neighbor.h @@ -2,6 +2,7 @@ #define NEIGHBOR_H #include +#include #include "hostdata.h" class neighbor { @@ -27,6 +28,12 @@ public: void set_to_send(double amount) { to_send = amount; } void add_to_send(double amount) { to_send += amount; } + // Prints its name and load on given category, with given + // priority. If verbose is true, prints debt and to_send too. + void print(bool verbose = false, + e_xbt_log_priority_t logp = xbt_log_priority_info, + xbt_log_category_t cat = _XBT_LOGV(default)) const; + private: const hostdata* host; // pointer to this neighbor's hostdata diff --git a/options.cpp b/options.cpp index e413470..a8178a9 100644 --- a/options.cpp +++ b/options.cpp @@ -1,14 +1,15 @@ -#include "options.h" - #include #include #include #include // getopt #include + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main); + #include "loba_simple.h" #include "loba_fairstrategy.h" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main); +#include "options.h" namespace opt { diff --git a/process.cpp b/process.cpp index 1556f3c..214b9d0 100644 --- a/process.cpp +++ b/process.cpp @@ -1,5 +1,3 @@ -#include "process.h" - #include #include #include @@ -8,10 +6,13 @@ #include #include #include + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc); + #include "misc.h" #include "options.h" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc); +#include "process.h" double process::total_load_init = 0.0; double process::total_load_exit = 0.0; @@ -55,7 +56,7 @@ process::process(int argc, char* argv[]) oss << neigh.back().get_name(); } LOG1(logp, "Got %s.", oss.str().c_str()); - print_loads(logp); + print_loads(true, logp); } process::~process() @@ -82,7 +83,7 @@ int process::run() INFO2("(%u) current load: %g", iter, load); } - print_loads(xbt_log_priority_debug); + print_loads(true, xbt_log_priority_debug); if (opt::bookkeeping) expected_load -= load_balance(expected_load); @@ -281,23 +282,31 @@ void process::finalize() comm.flush(true); } -void process::print_loads(e_xbt_log_priority_t logp) +#define print_loads_generic(vec, verbose, logp, cat) \ + if (_XBT_LOG_ISENABLEDV((*cat), logp)) { \ + using namespace std::tr1; \ + using namespace std::tr1::placeholders; \ + XCLOG0(cat, logp, "Neighbor loads:"); \ + std::for_each(vec.begin(), vec.end(), \ + bind(&neighbor::print, _1, verbose, logp, cat)); \ + } else ((void)0) + +void process::print_loads(bool verbose, + e_xbt_log_priority_t logp, + xbt_log_category_t cat) const { - if (!LOG_ISENABLED(logp)) - return; + print_loads_generic(neigh, verbose, logp, cat); +} - std::ostringstream oss; - if (neigh.empty()) { - oss << "no neighbor!"; - } else { - std::transform(neigh.begin(), neigh.end() - 1, - std::ostream_iterator(oss, ", "), - std::tr1::mem_fn(&neighbor::get_load)); - oss << neigh.back().get_load(); - } - LOG1(logp, "Neighbor loads: %s", oss.str().c_str()); +void process::print_loads_p(bool verbose, + e_xbt_log_priority_t logp, + xbt_log_category_t cat) const +{ + print_loads_generic(pneigh, verbose, logp, cat); } +#undef print_loads_generic + // Local variables: // mode: c++ // End: diff --git a/process.h b/process.h index df0cfe5..68dcd9b 100644 --- a/process.h +++ b/process.h @@ -13,6 +13,7 @@ # define MAP_TEMPLATE std::map #endif #include +#include #include "communicator.h" #include "neighbor.h" @@ -35,12 +36,20 @@ protected: // Returns the sum of "to_send" for all neighbors. double sum_of_to_send() const; - // Print with given priority what we know about our neighbors' loads - void print_loads(e_xbt_log_priority_t logp = xbt_log_priority_info); + + // Calls neighbor::print(verbose, logp, cat) for each member of neigh. + void print_loads(bool verbose = false, + e_xbt_log_priority_t logp = xbt_log_priority_info, + xbt_log_category_t cat = _XBT_LOGV(default)) const; + + // Calls neighbor::print(verbose, logp, cat) for each member of pneigh. + void print_loads_p(bool verbose = false, + e_xbt_log_priority_t logp = xbt_log_priority_info, + xbt_log_category_t cat = _XBT_LOGV(default)) const; private: - static double total_load_init; - static double total_load_exit; + static double total_load_init; // sum of neighbor loads at init + static double total_load_exit; // sum of neighbor loads at exit typedef MAP_TEMPLATE rev_neigh_type; neigh_type neigh; // list of neighbors (do not alter @@ -84,8 +93,6 @@ private: // all of them to answer. void finalize1(neighbor& nb); void finalize(); - - }; #endif // !PROCESS_H -- 2.39.5