Esempio n. 1
0
    def initialize_kronos_exp(self):
        """Initialize Kronos exp

        :return: None
        """
        self.check_kronos_loaded()
        num_tracers = self.get_number_of_tracers()
        logging.info(
            f"Initializing Kronos experiment with {num_tracers} nodes")
        ret = kronos_functions.initializeExp(num_tracers)
        if ret < 0:
            logging.error("Kronos initialization failed. Exiting ...")
            sys.exit(defines.EXIT_FAILURE)
Esempio n. 2
0
    def __init__(self,
                 number_dilated_nodes,
                 n_insns_per_round=1000000,
                 rel_cpu_speed=1.0,
                 is_virtual=False,
                 physical_system_sim_driver=None):
        """Performs some initialization and also optionally initializes Kronos.

        Args:
            is_virtual: If True Kronos is initialized
            physical_system_driver: An object which implements PhysicalSystemSim
                abstract class defined in contib/physical_system_sim.py. If it
                is None, then it denotes that this co-simulation has no attached
                physical simulator.
            number_dilated_nodes: Ignored unless is_virtual is True. Denotes
                number of nodes under Kronos control. Note that each PLC's CPU
                counts as a separate node. So if there are two PLCs each with
                2 CPUs, then there are 4 dilated_nodes in total.
            rel_cpu_speed: Ignored unless is_virtual is True. Denotes the relative
                cpu speed / (equivalent to TDF). In Kronos it represents the number
                of instructions that can be executed in 1ns of virtual time.
            n_insns_per_round: Number of instructions to execute per round.
                When divided by the rel_cpu_speed, it denotes amount of time
                the co-simulation would advance in one-round. 
                
            Note:
            -----
            For the PLCs, which use Application-driven virtual time advancement mechanism (APP-VT),
            the rel_cpu_speed and n_insns_per_round indivdually have no-effect.
            They are only used to calculate the step size of each round of the PLC.
            But if there are other nodes/process in the emulation (besides PLCs
            for e.g HMIs, modbus_comm_module) which use Instruction-driven 
            virtual time advancement (INS-VT), then these two quantities become relevant.
        """

        self.is_virtual = is_virtual
        self.num_tracers = number_dilated_nodes
        self.n_progressed_rounds = 0
        self.timestep_per_round_secs =\
             (float(n_insns_per_round)/rel_cpu_speed)/1000000000.0
        self.total_time_elapsed = 0.0
        assert number_dilated_nodes > 0
        if self.is_virtual == True:
            print "Initializing Kronos ..."
            if kf.initializeExp(1) < 0:
                print "Kronos initialization failed ! Make sure you are running\
                    the dilated kernel and kronos module is loaded !"

                sys.exit(0)

        self.physical_system_sim_driver = physical_system_sim_driver
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 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(
        '--run_in_one_tracer',
        dest='run_in_one_tracer',
        help='True/False runs all commands in one tracer if True',
        default="False")

    parser.add_argument('--rel_cpu_speed', dest='rel_cpu_speed',
                        help='relative cpu speed', type=float, \
                        default=1.5)

    parser.add_argument('--num_insns_per_round',
                        dest='num_insns_per_round',
                        help='Number of insns per round',
                        type=int,
                        default=10000)

    parser.add_argument('--num_progress_rounds',
                        dest='num_progress_rounds',
                        help='Number of rounds to run',
                        type=int,
                        default=10000)
    parser.add_argument('--num_times',
                        dest='num_times',
                        help='Number of times to re-run experiment',
                        type=int,
                        default=1)
    parser.add_argument('--output_dir',
                        dest='output_dir',
                        type=str,
                        default='/tmp')
    args = parser.parse_args()

    count = 1
    while count <= args.num_times:

        log_fds = []
        tracer_pids = []
        cmds_to_run = []

        if args.run_in_one_tracer == "True":
            fd = os.open("/tmp/tracer_log.txt", os.O_RDWR | os.O_CREAT)
            log_fds.append(fd)
            num_tracers = 1
        else:
            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 xrange(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 xrange(0, len(cmds_to_run)) ]
            num_tracers = len(cmds_to_run)

        print "Initializing Kronos !"
        if kf.initializeExp(1) < 0:
            print "Kronos initialization failed ! Make sure you are running\
                the dilated kernel and kronos module is loaded !"

            sys.exit(0)

        print "Starting all commands to run !"
        if args.run_in_one_tracer == "True":
            start_all_cmds_in_one_tracer(args.cmds_to_run_file, args.rel_cpu_speed,\
                args.num_insns_per_round, log_fds[0])
        else:
            for i in xrange(0, len(cmds_to_run)):
                print "Starting tracer for cmd: %s" % (cmds_to_run[i])
                start_new_dilated_process(cmds_to_run[i], args.rel_cpu_speed, \
                    args.num_insns_per_round, log_fds[i])

        print "Synchronizing anf freezing tracers ..."
        while kf.synchronizeAndFreeze(num_tracers) <= 0:
            print "Kronos >> Synchronize and Freeze failed. Retrying in 1 sec"
            time.sleep(1)

        print "Starting Synchronized Experiment !"
        kf.startExp()

        if args.num_progress_rounds > 0:
            print "Running for %d rounds ... " % (args.num_progress_rounds)

            num_finised_rounds = 0
            step_size = min(100, args.num_progress_rounds)
            start_time = float(time.time())
            kf.progress_n_rounds(args.num_progress_rounds)
            #while num_finised_rounds < args.num_progress_rounds:
            #    kf.progress_n_rounds(step_size)
            #    num_finised_rounds += step_size
            #    print "Ran %d rounds ..." %(num_finised_rounds)
        elapsed_time = float(time.time()) - start_time
        total_elapsed_virtual_time = float(
            args.num_progress_rounds * args.num_insns_per_round) / 1000000000.0
        print "Stopping Synchronized Experiment !"
        kf.stopExp()
        with open('/tmp/exp_stats.txt', 'w') as f:
            f.write('overhead ratio: %f\n' %
                    (elapsed_time / total_elapsed_virtual_time))
            f.write('total_elapsed_virtual_time: %f\n' %
                    (total_elapsed_virtual_time))
            f.write('elapsed_time: %f\n' % (elapsed_time))

        for fd in log_fds:
            os.close(fd)

        os.system('mkdir -p %s/run_%d' % (args.output_dir, count))
        os.system('cp /tmp/tracer* %s/run_%d' % (args.output_dir, count))
        os.system('cp /tmp/exp_stats.txt %s/run_%d' % (args.output_dir, count))
        print "Finished ! Logs of each ith tracer can be found in %s/run_%d" % (
            args.output_dir, count)
        count += 1