utils/replay-simulation.cc
changeset 197 8346ab3528bf
parent 150 663120712cd9
child 201 b7635c5d5bdf
equal deleted inserted replaced
196:9d243651d00c 197:8346ab3528bf
    18  *
    18  *
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    20  */
    20  */
    21 
    21 
    22 #include "ns3/simulator.h"
    22 #include "ns3/simulator.h"
    23 #include "ns3/event.h"
    23 #include "ns3/nstime.h"
    24 #include "ns3/event.tcc"
    24 #include "ns3/system-wall-clock-ms.h"
    25 #include "ns3/wall-clock-ms.h"
       
    26 #include <vector>
    25 #include <vector>
    27 #include <deque>
    26 #include <deque>
    28 #include <fstream>
    27 #include <fstream>
    29 #include <iostream>
    28 #include <iostream>
    30 
    29 
    31 using namespace ns3;
    30 using namespace ns3;
    32 
    31 
    33 class LogReader {
    32 class LogReader {
    34 public:
    33 public:
    35   void readFrom_filename (char const *filename);
    34   void ReadFromFilename (char const *filename);
    36   void run (void);
    35   void Run (void);
    37   void printStats (void);
    36   void PrintStats (void);
    38 private:
    37 private:
    39   struct Command {
    38   struct Command {
    40       enum {
    39       enum {
    41           REMOVE,
    40           REMOVE,
    42           INSERT,
    41           INSERT,
    58               // time at which the event is supposed to expire
    57               // time at which the event is supposed to expire
    59               uint64_t m_evUs;
    58               uint64_t m_evUs;
    60           } insertRemove;
    59           } insertRemove;
    61       };
    60       };
    62   };
    61   };
    63   void executeLogCommands (uint32_t uid);
    62   void ExecuteLogCommands (uint32_t uid);
    64 
    63 
    65   typedef std::deque<struct Command> Commands;
    64   typedef std::deque<struct Command> Commands;
    66   typedef std::deque<struct Command>::iterator CommandsI;
    65   typedef std::deque<struct Command>::iterator CommandsI;
    67   typedef std::deque<Event > RemoveEvents;
    66   typedef std::deque<EventId> RemoveEvents;
    68   
    67   
    69 
    68 
    70   Commands m_commands;
    69   Commands m_commands;
    71   CommandsI m_command;
    70   CommandsI m_command;
    72   RemoveEvents m_removeEvents;
    71   RemoveEvents m_removeEvents;
    75 
    74 
    76 typedef std::vector<std::pair<uint32_t, uint32_t> > Removes;
    75 typedef std::vector<std::pair<uint32_t, uint32_t> > Removes;
    77 typedef std::vector<std::pair<uint32_t, uint32_t> >::iterator RemovesI;
    76 typedef std::vector<std::pair<uint32_t, uint32_t> >::iterator RemovesI;
    78 
    77 
    79 void
    78 void
    80 LogReader::ReadFrom_filename (char const *filename)
    79 LogReader::ReadFromFilename (char const *filename)
    81 {
    80 {
    82   std::ifstream log;
    81   std::ifstream log;
    83   std::cout << "read log..." << std::endl;
    82   std::cout << "read log..." << std::endl;
    84   Removes removes;
    83   Removes removes;
    85   log.open (filename);
    84   log.open (filename);
    86   while (!log.eof ()) {
    85   while (!log.eof ()) 
       
    86     {
    87       std::string type;
    87       std::string type;
    88       log >> type;
    88       log >> type;
    89       if (type == "i") {
    89       if (type == "i") 
       
    90         {
    90           uint32_t nowUid, evUid;
    91           uint32_t nowUid, evUid;
    91           uint64_t nowUs, evUs;
    92           uint64_t nowUs, evUs;
    92           log >> nowUid >> nowUs >> evUid >> evUs;
    93           log >> nowUid >> nowUs >> evUid >> evUs;
    93           struct Command cmd;
    94           struct Command cmd;
    94           cmd.m_type = Command::INSERT;
    95           cmd.m_type = Command::INSERT;
    95           cmd.m_uid = nowUid;
    96           cmd.m_uid = nowUid;
    96           cmd.insert.m_evUs = evUs;
    97           cmd.insert.m_evUs = evUs;
    97           m_commands.push_back (cmd);
    98           m_commands.push_back (cmd);
    98       } else if (type == "r") {
    99         } 
       
   100       else if (type == "r") 
       
   101         {
    99           uint32_t nowUid, evUid;
   102           uint32_t nowUid, evUid;
   100           uint64_t nowUs, evUs;
   103           uint64_t nowUs, evUs;
   101           log >> nowUid >> nowUs >> evUid >> evUs;
   104           log >> nowUid >> nowUs >> evUid >> evUs;
   102           struct Command cmd;
   105           struct Command cmd;
   103           cmd.m_type = Command::REMOVE;
   106           cmd.m_type = Command::REMOVE;
   104           cmd.m_uid = nowUid;
   107           cmd.m_uid = nowUid;
   105           m_commands.push_back (cmd);
   108           m_commands.push_back (cmd);
   106           removes.push_back (std::Make_pair (nowUid, evUid));
   109           removes.push_back (std::make_pair (nowUid, evUid));
   107       } else if (type == "il") {
   110         } 
       
   111       else if (type == "il") 
       
   112         {
   108           uint32_t nowUid, evUid;
   113           uint32_t nowUid, evUid;
   109           uint64_t nowUs, evUs;
   114           uint64_t nowUs, evUs;
   110           log >> nowUid >> nowUs >> evUid >> evUs;
   115           log >> nowUid >> nowUs >> evUid >> evUs;
   111           struct Command cmd;
   116           struct Command cmd;
   112           cmd.m_type = Command::INSERT_LATER;
   117           cmd.m_type = Command::INSERT_LATER;
   113           cmd.m_uid = nowUid;
   118           cmd.m_uid = nowUid;
   114           m_commands.push_back (cmd);
   119           m_commands.push_back (cmd);
   115       }
   120         }
   116   }
   121     }
   117   log.close ();
   122   log.close ();
   118 
   123 
   119   std::cout << "gather insert removes..." << std::endl;
   124   std::cout << "gather insert removes..." << std::endl;
   120   for (CommandsI i = m_commands.begin (); i != m_commands.end (); i++) {
   125   for (CommandsI i = m_commands.begin (); i != m_commands.end (); i++) 
   121       if (i->m_type == Command::INSERT) {
   126     {
   122           for (RemovesI j = removes.begin (); j != removes.end (); j++) {
   127       if (i->m_type == Command::INSERT) 
   123               if (j->second == i->m_uid) {
   128         {
       
   129           for (RemovesI j = removes.begin (); j != removes.end (); j++) 
       
   130             {
       
   131               if (j->second == i->m_uid) 
       
   132                 {
   124                   // this insert will be removed later.
   133                   // this insert will be removed later.
   125                   uint64_t us = i->insert.m_evUs;
   134                   uint64_t us = i->insert.m_evUs;
   126                   uint32_t uid = i->m_uid;
   135                   uint32_t uid = i->m_uid;
   127                   i->m_type = Command::INSERT_REMOVE;
   136                   i->m_type = Command::INSERT_REMOVE;
   128                   i->m_uid = uid;
   137                   i->m_uid = uid;
   129                   i->insertRemove.m_evUs = us;
   138                   i->insertRemove.m_evUs = us;
   130                   i->insertRemove.m_evLoc = j->first;
   139                   i->insertRemove.m_evLoc = j->first;
   131                   break;
   140                   break;
   132               }
   141                 }
   133           }
   142             }
   134       }
   143         }
   135   }
   144     }
   136   std::cout << "calculate remove locations..." << std::endl;
   145   std::cout << "calculate remove locations..." << std::endl;
   137   // calculate the final insert/remove location.
   146   // calculate the final insert/remove location.
   138   for (CommandsI i = m_commands.begin (); i != m_commands.end (); i++) {
   147   for (CommandsI i = m_commands.begin (); i != m_commands.end (); i++) 
   139       if (i->m_type == Command::INSERT_REMOVE) {
   148     {
       
   149       if (i->m_type == Command::INSERT_REMOVE) 
       
   150         {
   140           uint32_t loc = 0;
   151           uint32_t loc = 0;
   141           for (CommandsI tmp = i; tmp != m_commands.end (); tmp++) {
   152           for (CommandsI tmp = i; tmp != m_commands.end (); tmp++) 
       
   153             {
   142               if (tmp->m_type == Command::REMOVE &&
   154               if (tmp->m_type == Command::REMOVE &&
   143                   tmp->m_uid == i->insertRemove.m_evLoc) {
   155                   tmp->m_uid == i->insertRemove.m_evLoc) 
       
   156                 {
   144                   i->insertRemove.m_evLoc = loc;
   157                   i->insertRemove.m_evLoc = loc;
   145                   break;
   158                   break;
   146               }
   159                 }
   147               loc++;
   160               loc++;
   148           }
   161             }
   149       }
   162         }
   150   }
   163     }
   151 }
   164 }
   152 void
   165 void
   153 LogReader::ExecuteLogCommands (uint32_t uid)
   166 LogReader::ExecuteLogCommands (uint32_t uid)
   154 {
   167 {
   155   if (m_command == m_commands.end ()) {
   168   if (m_command == m_commands.end ()) 
       
   169     {
   156       return;
   170       return;
   157   }
   171     }
   158   //std::cout << "one event, uid=" <<m_uid<< std::endl;
   172   //std::cout << "one event, uid=" <<m_uid<< std::endl;
   159   struct Command cmd = *m_command;
   173   struct Command cmd = *m_command;
   160   //std::cout << "cmd uid=" <<cmd.m_uid<< std::endl;
   174   //std::cout << "cmd uid=" <<cmd.m_uid<< std::endl;
   161   while (cmd.m_uid == uid) {
   175   while (cmd.m_uid == uid) 
       
   176     {
   162       m_command++;
   177       m_command++;
   163       switch (cmd.m_type) {
   178       switch (cmd.m_type) {
   164       case Command::INSERT:
   179       case Command::INSERT:
   165           //std::Cout << "exec insert now=" << Simulator::nowUs ()
   180         //std::Cout << "exec insert now=" << Simulator::nowUs ()
   166           //<< ", time=" << cmd.insert.m_evUs << std::endl;
   181         //<< ", time=" << cmd.insert.m_evUs << std::endl;
   167           Simulator::ScheduleAbsUs (cmd.insert.m_evUs, 
   182         Simulator::Schedule (MicroSeconds (cmd.insert.m_evUs) - Now (), 
   168                        makeEvent (&LogReader::executeLogCommands, this, m_uid));
   183                              &LogReader::ExecuteLogCommands, this, m_uid);
   169           m_uid++;
   184         m_uid++;
   170           break;
   185         break;
   171       case Command::INSERT_LATER:
   186       case Command::INSERT_LATER:
   172           //std::cout << "exec insert later" << std::endl;
   187           //std::cout << "exec insert later" << std::endl;
   173           Simulator::ScheduleNow (makeEvent (&LogReader::executeLogCommands, this, m_uid));
   188           Simulator::ScheduleNow (&LogReader::ExecuteLogCommands, this, m_uid);
   174           m_uid++;
   189           m_uid++;
   175           break;
   190           break;
   176       case Command::REMOVE: {
   191       case Command::REMOVE: 
       
   192         {
   177           //std::cout << "exec remove" << std::endl;
   193           //std::cout << "exec remove" << std::endl;
   178           Event ev = m_removeEvents.front ();
   194           EventId id = m_removeEvents.front ();
   179           m_removeEvents.pop_front ();
   195           m_removeEvents.pop_front ();
   180           Simulator::Remove (ev);
   196           Simulator::Remove (id);
   181       } break;
   197         } break;
   182       case Command::INSERT_REMOVE: {
   198       case Command::INSERT_REMOVE: 
       
   199         {
   183           //std::cout << "exec insert remove" << std::endl;
   200           //std::cout << "exec insert remove" << std::endl;
   184           Event ev = makeEvent (&LogReader::executeLogCommands, this, m_uid);
   201           EventId id = Simulator::Schedule (MicroSeconds (cmd.insertRemove.m_evUs) - Now (),
   185           Simulator::ScheduleAbsUs (cmd.insertRemove.m_evUs, ev);
   202                                             &LogReader::ExecuteLogCommands, this, m_uid);
   186           m_removeEvents[cmd.insertRemove.m_evLoc] = ev;
   203           m_removeEvents[cmd.insertRemove.m_evLoc] = id;
   187           m_uid++;
   204           m_uid++;
   188       } break;
   205         } break;
   189       }
   206       }
   190       cmd = *m_command;
   207       cmd = *m_command;
   191   }
   208   }
   192 }
   209 }
   193 
   210 
   194 void
   211 void
   195 LogReader::PrintStats (void)
   212 LogReader::PrintStats (void)
   196 {
   213 {
   197   uint32_t nInserts = 0;
   214   uint32_t nInserts = 0;
   198   uint32_t nRemoves = 0;
   215   uint32_t nRemoves = 0;
   199   for (CommandsI i = m_commands.begin (); i != m_commands.end (); i++) {
   216   for (CommandsI i = m_commands.begin (); i != m_commands.end (); i++) 
       
   217     {
   200       switch (i->m_type) {
   218       switch (i->m_type) {
   201       case Command::INSERT:
   219       case Command::INSERT:
   202           nInserts++;
   220           nInserts++;
   203           break;
   221           break;
   204       case Command::INSERT_LATER:
   222       case Command::INSERT_LATER:
   209           break;
   227           break;
   210       case Command::REMOVE:
   228       case Command::REMOVE:
   211           nRemoves++;
   229           nRemoves++;
   212           break;
   230           break;
   213       }
   231       }
   214   }
   232     }
   215   std::cout << "inserts="<<nInserts<<", removes="<<nRemoves<<std::endl;
   233   std::cout << "inserts="<<nInserts<<", removes="<<nRemoves<<std::endl;
   216   std::cout << "run simulation..."<<std::endl;
   234   std::cout << "run simulation..."<<std::endl;
   217 }
   235 }
   218 
   236 
   219 void
   237 void
   220 LogReader::Run (void)
   238 LogReader::Run (void)
   221 {
   239 {
   222   m_uid = 0;
   240   m_uid = 0;
   223   WallClockMs time;
   241   SystemWallClockMs time;
   224   time.start ();
   242   time.Start ();
   225   m_command = m_commands.begin ();
   243   m_command = m_commands.begin ();
   226   executeLogCommands (m_uid);
   244   ExecuteLogCommands (m_uid);
   227   Simulator::Run ();
   245   Simulator::Run ();
   228   unsigned long long delta = time.end ();
   246   unsigned long long delta = time.End ();
   229   double delay = ((double)delta)/1000;
   247   double delay = ((double)delta)/1000;
   230   std::cout << "runtime="<<delay<<"s"<<std::endl;
   248   std::cout << "runtime="<<delay<<"s"<<std::endl;
   231 }
   249 }
   232 
   250 
   233 
   251 
   234 int main (int argc, char *argv[])
   252 int main (int argc, char *argv[])
   235 {
   253 {
   236   char const *input = 0;
   254   char const *input = 0;
   237   uint32_t n = 1;
   255   uint32_t n = 1;
   238   while (argc > 0) {
   256   while (argc > 0) 
   239       if (strcmp ("--list", argv[0]) == 0) {
   257     {
       
   258       if (strcmp ("--list", argv[0]) == 0) 
       
   259         {
   240           Simulator::SetLinkedList ();
   260           Simulator::SetLinkedList ();
   241       } else if (strcmp ("--heap", argv[0]) == 0) {
   261         } 
       
   262       else if (strcmp ("--heap", argv[0]) == 0) 
       
   263         {
   242           Simulator::SetBinaryHeap ();
   264           Simulator::SetBinaryHeap ();
   243       } else if (strcmp ("--map", argv[0]) == 0) {
   265         } 
       
   266       else if (strcmp ("--map", argv[0]) == 0) 
       
   267         {
   244           Simulator::SetStdMap ();
   268           Simulator::SetStdMap ();
   245       } else if (strncmp ("--n=", argv[0], strlen("--n=")) == 0) {
   269         } 
       
   270       else if (strncmp ("--n=", argv[0], strlen("--n=")) == 0) 
       
   271         {
   246           n = atoi (argv[0]+strlen ("--n="));
   272           n = atoi (argv[0]+strlen ("--n="));
   247       } else if (strncmp ("--input=", argv[0],strlen ("--input=")) == 0) {
   273         } 
       
   274       else if (strncmp ("--input=", argv[0],strlen ("--input=")) == 0) 
       
   275         {
   248           input = argv[0] + strlen ("--input=");
   276           input = argv[0] + strlen ("--input=");
   249       } else if (strncmp ("--log=", argv[0],strlen ("--log=")) == 0) {
   277         } 
       
   278       else if (strncmp ("--log=", argv[0],strlen ("--log=")) == 0) 
       
   279         {
   250           char const *filename = argv[0] + strlen ("--log=");
   280           char const *filename = argv[0] + strlen ("--log=");
   251           Simulator::EnableLogTo (filename);
   281           Simulator::EnableLogTo (filename);
   252       }
   282         }
   253       argc--;
   283       argc--;
   254       argv++;
   284       argv++;
   255   }
   285     }
   256   if (input == 0) {
   286   if (input == 0) 
       
   287     {
   257       std::cerr << "need --input=[filename] option" << std::endl;
   288       std::cerr << "need --input=[filename] option" << std::endl;
   258       return 1;
   289       return 1;
   259   }
   290     }
   260   LogReader log;
   291   LogReader log;
   261   log.readFrom_filename (input);
   292   log.ReadFromFilename (input);
   262   for (uint32_t i = 0; i < n; i++) {
   293   for (uint32_t i = 0; i < n; i++) 
   263       log.run ();
   294     {
   264   }
   295       log.Run ();
   265 }
   296     }
       
   297 }