def progress_for(self, time_step_secs): """Run the entire co/simulation-emulation for specified time. Args: time_step_secs (float): Time to advance by. Returns: None Raises: None """ if self.is_virtual and self.n_progressed_rounds == 0: print("Starting Synchronized Experiment ...") if self.is_virtual: n_rounds = float(time_step_secs) / self.timestep_per_round_secs if n_rounds <= 0: n_rounds = 1 kf.progressBy(self.n_insns_per_round, int(n_rounds)) self.n_progressed_rounds += int(n_rounds) else: pass #time.sleep(time_step_secs) self.total_time_elapsed += float(time_step_secs) if self.physical_system_sim_driver is not None: start_time = float(time.time()) self.physical_system_sim_driver.progress(float(time_step_secs)) end_time = float(time.time()) if not self.is_virtual: left_over = time_step_secs - (end_time - start_time) if left_over > 0: time.sleep(left_over)
def wait_for_loaded_pcap_msg(self): """Waits for required pcaps to be loaded by all replay-drivers :return: None """ logging.info( "########################################################################" ) logging.info( "\nMelody >> Waiting for pcaps to be loaded by all replay drivers ... " ) n_warmup_rounds = 0 outstanding_hosts = self.nodes_involved_in_replay while True: if len(outstanding_hosts) == 0: break for host in outstanding_hosts: dummy_id, msg = self.shared_buf_array.read( f"{str(host)}-replay-main-cmd-channel-buffer") if msg == defines.LOADED_CMD: logging.info("\nMelody >> Got a PCAP-Loaded message from " f"replay driver for node: {host}") outstanding_hosts.remove(host) if self.enable_kronos == 1: kronos_functions.progressBy(self.timeslice, 1) n_warmup_rounds += 1 if n_warmup_rounds % 100 == 0: stdout.write( "\rNumber of rounds ran until all replay pcaps were loaded: %d" % n_warmup_rounds) stdout.flush() else: time.sleep(0.1) if self.enable_kronos == 1: logging.info( f"\nMelody >> All pcaps loaded in " f"{float(n_warmup_rounds*self.timeslice)/float(defines.SEC)} " "seconds (virtual time)") logging.info("\nMelody >> All replay drivers ready to proceed ...\n")
def main(): parser = argparse.ArgumentParser() parser.add_argument('--cmds_to_run_file', dest='cmds_to_run_file', help='path to file containing commands to run', \ type=str, default='cmds_to_run_file.txt') parser.add_argument('--num_insns_per_round', dest='num_insns_per_round', help='Number of insns per round', type=int, default=1000000) parser.add_argument('--num_progress_rounds', dest='num_progress_rounds', help='Number of rounds to run', type=int, default=2000) parser.add_argument('--rel_cpu_speed', dest='rel_cpu_speed', help='relative cpu speed', type=float, \ default=1.0) args = parser.parse_args() log_fds = [] tracer_pids = [] cmds_to_run = [] if not os.path.isfile(args.cmds_to_run_file): print("Commands file path is incorrect !") sys.exit(0) fd1 = open(args.cmds_to_run_file, "r") cmds_to_run = [x.strip() for x in fd1.readlines()] fd1.close() for i in range(0, len(cmds_to_run)): with open("/tmp/tracer_log%d.txt" % (i), "w") as f: pass log_fds = [ os.open("/tmp/tracer_log%d.txt" %(i), os.O_RDWR | os.O_CREAT ) \ for i in range(0, len(cmds_to_run)) ] num_tracers = len(cmds_to_run) input('Press any key to continue !') for i in range(0, num_tracers): with open("/tmp/tracer_log%d.txt" % (i), "w") as f: pass log_fds = [ os.open("/tmp/tracer_log%d.txt" %(i), os.O_RDWR | os.O_CREAT ) \ for i in range(0, num_tracers) ] print("Initializing VT Module !") if kf.initializeExp(num_tracers) < 0: print("VT module initialization failed ! Make sure you are running " "the dilated kernel and kronos module is loaded !") sys.exit(0) input('Press any key to continue !') print("Starting all commands to run !") for i in range(0, num_tracers): print("Starting tracer: %d" % (i + 1)) start_new_dilated_process(num_tracers, cmds_to_run[i], log_fds[i], args.rel_cpu_speed) print("Synchronizing anf freezing tracers ...") while kf.synchronizeAndFreeze() <= 0: print("VT Module >> Synchronize and Freeze failed. Retrying in 1 sec") time.sleep(1) input('Press any key to continue !') print("Starting Synchronized Experiment !") start_time = float(time.time()) if args.num_progress_rounds > 0: print("Running for %d rounds ... " % (args.num_progress_rounds)) num_finised_rounds = 0 step_size = min(10, args.num_progress_rounds) while num_finised_rounds < args.num_progress_rounds: kf.progressBy(args.num_insns_per_round, step_size) num_finised_rounds += step_size #input("Press Enter to continue...") print("Ran %d rounds ..." % (num_finised_rounds), " elapsed time ...", float(time.time()) - start_time) elapsed_time = float(time.time()) - start_time print("Total time elapsed (secs) = ", elapsed_time) input("Press Enter to continue...") print("Stopping Synchronized Experiment !") kf.stopExp() for fd in log_fds: os.close(fd) print( "Finished ! Logs of each ith tracer can be found in /tmp/tracer_logi.txt" )
def run_for(self, run_time_ns, sync=True): """Runs the cyber emulation for the specified time in nano seconds. The cyber emulation gets frozen after running for the specified duration iff it is under kronos control. :param run_time_ns: Batch run time in nano seconds :type run_time_ns: int :param sync: If True, it syncs with power simulator after running for the required time :type sync: bool :return: None """ if not self.started: logging.info( "########################################################################" ) logging.info("") logging.info( " Starting Experiment. Total Run time (secs) = %d" % self.run_time) logging.info("") logging.info( "########################################################################" ) self.trigger_all_processes(defines.START_CMD) self.started = True if run_time_ns > 0: if run_time_ns < self.timeslice: run_time_ns = self.timeslice # progress for 20ms if self.timeslice > 20 * defines.NS_PER_MS: n_rounds = 1 else: n_rounds = int(10 * defines.NS_PER_MS / self.timeslice) n_total_rounds = int(run_time_ns / self.timeslice) n_rounds_progressed = 0 while True: if self.enable_kronos == 1: kronos_functions.progressBy(self.timeslice, n_rounds) n_rounds_progressed += n_rounds stdout.write( "\rRunning for %f ms. Number of Rounds Progressed: %d/%d" % (float(run_time_ns) / float(defines.MS), n_rounds_progressed, n_total_rounds)) stdout.flush() if n_rounds_progressed >= n_total_rounds: break else: time.sleep(float(run_time_ns) / float(defines.NS_PER_SEC)) break if self.enable_kronos == 1: if sync: stdout.write( "\r................... Syncing with power simulator ..................." ) stdout.flush() self.sync_with_power_simulator() stdout.write("\n") self.total_elapsed_virtual_time += \ float(run_time_ns)/float(defines.SEC) elif sync: stdout.write( "\r................... Syncing with power simulator ...................\n" ) stdout.flush() self.sync_with_power_simulator()