Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
better than static casting, use a container
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Thu, 16 Mar 2017 16:48:11 +0000 (17:48 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Thu, 16 Mar 2017 16:48:11 +0000 (17:48 +0100)
include/xbt/replay.hpp
src/msg/msg_actions.cpp
src/xbt/xbt_replay.cpp

index b7986e3..a317f34 100644 (file)
 #include "xbt/dict.h"
 #ifdef __cplusplus
 #include <fstream>
+#include <queue>
+#include <unordered_map>
 
 namespace simgrid {
 namespace xbt {
 /* To split the file if a unique one is given (specific variable for the other case live in runner()) */
+typedef std::vector<std::string> ReplayAction;
+static std::unordered_map<std::string, std::queue<ReplayAction*>*> action_queues;
+
 XBT_PUBLIC_DATA(std::ifstream*) action_fs;
 XBT_PUBLIC(bool) replay_is_active();
 XBT_PUBLIC(void) replay_init();
@@ -28,7 +33,6 @@ XBT_PUBLIC(int) replay_runner(int argc, char* argv[]);
 SG_BEGIN_DECL()
 
 typedef void (*action_fun)(const char* const* args);
-XBT_PUBLIC_DATA(xbt_dict_t) xbt_action_queues;
 XBT_PUBLIC(void) xbt_replay_action_register(const char* action_name, action_fun function);
 
 SG_END_DECL()
index 567209f..916aa87 100644 (file)
@@ -31,22 +31,18 @@ void MSG_action_exit()
  */
 msg_error_t MSG_action_trace_run(char *path)
 {
-  msg_error_t res;
-  char *name;
-  xbt_dynar_t todo;
-  xbt_dict_cursor_t cursor;
-
   if (path) {
     simgrid::xbt::action_fs = new std::ifstream(path, std::ifstream::in);
   }
-  res = MSG_main();
 
-  if (!xbt_dict_is_empty(xbt_action_queues)) {
+  msg_error_t res = MSG_main();
+
+  if (!simgrid::xbt::action_queues.empty()) {
     XBT_WARN("Not all actions got consumed. If the simulation ended successfully (without deadlock),"
              " you may want to add new processes to your deployment file.");
 
-    xbt_dict_foreach(xbt_action_queues, cursor, name, todo) {
-      XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
+    for (auto actions_of : simgrid::xbt::action_queues) {
+      XBT_WARN("Still %zu actions for %s", actions_of.second->size(), actions_of.first.c_str());
     }
   }
 
@@ -55,9 +51,6 @@ msg_error_t MSG_action_trace_run(char *path)
     simgrid::xbt::action_fs = nullptr;
   }
 
-  xbt_dict_free(&xbt_action_queues);
-  xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
-
   return res;
 }
 
index f2c6ae2..40b5cee 100644 (file)
 #include <boost/algorithm/string.hpp>
 #include <ctype.h>
 #include <errno.h>
-#include <fstream>
-#include <queue>
-#include <unordered_map>
 #include <wchar.h>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
 
-xbt_dict_t xbt_action_queues = nullptr;
 bool is_replay_active        = false;
 
 namespace simgrid {
@@ -29,7 +25,6 @@ namespace xbt {
 
 std::ifstream* action_fs = nullptr;
 std::unordered_map<std::string, action_fun> action_funs;
-typedef std::vector<std::string> ReplayAction;
 
 static void read_and_trim_line(std::ifstream* fs, std::string* line)
 {
@@ -78,15 +73,12 @@ bool ReplayReader::get(ReplayAction* action)
 void replay_init()
 {
   if (!is_replay_active) {
-    xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
     is_replay_active  = true;
   }
 }
 
 void replay_exit()
 {
-  xbt_dict_free(&xbt_action_queues);
-  xbt_action_queues = nullptr;
 }
 
 bool replay_is_active()
@@ -98,8 +90,9 @@ static ReplayAction* get_action(char* name)
 {
   ReplayAction* action;
 
-  std::queue<ReplayAction*>* myqueue =
-      static_cast<std::queue<ReplayAction*>*>(xbt_dict_get_or_null(xbt_action_queues, name));
+  std::queue<ReplayAction*>* myqueue = nullptr;
+  if (action_queues.find(std::string(name)) != action_queues.end())
+    myqueue = action_queues.at(std::string(name));
   if (myqueue == nullptr || myqueue->empty()) { // Nothing stored for me. Read the file further
     if (action_fs == nullptr) {                 // File closed now. There's nothing more to read. I'm out of here
       goto todo_done;
@@ -119,11 +112,12 @@ static ReplayAction* get_action(char* name)
           return action;
         } else {
           // Else, I have to store it for the relevant colleague
-          std::queue<ReplayAction*>* otherqueue =
-              static_cast<std::queue<ReplayAction*>*>(xbt_dict_get_or_null(xbt_action_queues, evtname.c_str()));
+          std::queue<ReplayAction*>* otherqueue = nullptr;
+          if (action_queues.find(evtname) != action_queues.end())
+            otherqueue = action_queues.at(evtname);
           if (otherqueue == nullptr) { // Damn. Create the queue of that guy
             otherqueue = new std::queue<ReplayAction*>();
-            xbt_dict_set(xbt_action_queues, evtname.c_str(), otherqueue, nullptr);
+            action_queues.insert({evtname, otherqueue});
           }
           otherqueue->push(action);
         }
@@ -141,7 +135,7 @@ static ReplayAction* get_action(char* name)
 todo_done:
   if (myqueue != nullptr) {
     delete myqueue;
-    xbt_dict_remove(xbt_action_queues, name);
+    action_queues.erase(std::string(name));
   }
   return nullptr;
 }