Model.py 4.43 KB
Newer Older
1 2
# coding=utf-8

3 4
# from simpy.Simulation import Simulation
from simpy import Environment
5 6 7 8 9 10
from simso.core.Processor import Processor
from simso.core.Task import Task
from simso.core.Timer import Timer
from simso.core.etm import execution_time_models
from simso.core.Logger import Logger
from simso.core.results import Results
11
from simso.core.Fault import FaultFactory
12 13


14
class Model(Environment):
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    """
    Main class for the simulation. It instantiate the various components
    required by the simulation and run it.
    """

    def __init__(self, configuration, callback=None):
        """
        Args:
            - `callback`: A callback can be specified. This function will be \
                called to report the advance of the simulation (useful for a \
                progression bar).
            - `configuration`: The :class:`configuration \
                <simso.configuration.Configuration>` of the simulation.

        Methods:
        """
31
        Environment.__init__(self)
32 33 34
        self._logger = Logger(self)
        task_info_list = configuration.task_info_list
        proc_info_list = configuration.proc_info_list
35
        fault_info_list = configuration.fault_info_list
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        self._cycles_per_ms = configuration.cycles_per_ms
        self.scheduler = configuration.scheduler_info.instantiate(self)

        try:
            self._etm = execution_time_models[configuration.etm](
                self, len(proc_info_list)
            )
        except KeyError:
            print("Unknowned Execution Time Model.", configuration.etm)

        self._task_list = []
        for task_info in task_info_list:
            self._task_list.append(Task(self, task_info))

        # Init the processor class. This will in particular reinit the
        # identifiers to 0.
        Processor.init()
53

54 55 56 57 58 59 60 61 62 63
        # Initialization of the caches
        for cache in configuration.caches_list:
            cache.init()

        self._processors = []
        for proc_info in proc_info_list:
            proc = Processor(self, proc_info)
            proc.caches = proc_info.caches
            self._processors.append(proc)

64

65 66
        self._fault_list = []
        for fault_info in fault_info_list:
67
            self._fault_list.append(FaultFactory(self, fault_info))
68

69 70 71 72 73 74 75 76 77 78 79 80 81
        # XXX: too specific.
        self.penalty_preemption = configuration.penalty_preemption
        self.penalty_migration = configuration.penalty_migration

        self._etm.init()

        self._duration = configuration.duration
        self.progress = Timer(self, Model._on_tick, (self,),
                              self.duration // 20 + 1, one_shot=False,
                              in_ms=False)
        self._callback = callback
        self.scheduler.task_list = self._task_list
        self.scheduler.processors = self._processors
82
        self.scheduler.configuration = configuration
83 84 85
        self.results = None

    def now_ms(self):
86
        return float(self.now) / self._cycles_per_ms
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

    @property
    def logs(self):
        """
        All the logs from the :class:`Logger <simso.core.Logger.Logger>`.
        """
        return self._logger.logs

    @property
    def logger(self):
        return self._logger

    @property
    def cycles_per_ms(self):
        """
        Number of cycles per milliseconds. A cycle is the internal unit used
        by SimSo. However, the tasks are defined using milliseconds.
        """
        return self._cycles_per_ms

    @property
    def etm(self):
        """
        Execution Time Model
        """
        return self._etm

    @property
    def processors(self):
        """
        List of all the processors.
        """
        return self._processors

    @property
    def task_list(self):
        """
        List of all the tasks.
        """
        return self._task_list

    @property
129 130 131 132 133 134 135
    def fault_list(self):
        """
        List of all faults
        """
        return self._fault_list

    @property
136 137 138 139 140 141 142 143
    def duration(self):
        """
        Duration of the simulation.
        """
        return self._duration

    def _on_tick(self):
        if self._callback:
144
            self._callback(self.now)
145 146 147

    def run_model(self):
        """ Execute the simulation."""
148
        # self.initialize()
149 150 151 152
        self.scheduler.init()
        self.progress.start()

        try:
153
            self.run(until=self._duration)
154 155 156
        finally:
            self._etm.update()

157
            if self.now > 0:
158 159
                self.results = Results(self)
                self.results.end()