text_mode.rst 5.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
Using SimSo in script mode
==========================

SimSo can be used as a library in order to automatize wide experimentations and have a maximum of flexibility on the analysis of the results. In this tutorial, a few examples are provided.

.. contents:: Table of Contents


Loading a configuration using a simulation file
-----------------------------------------------

A :class:`Configuration <simso.configuration.Configuration>` can be initialized with a file passed to its constructor::

14
        configuration = Configuration(argv[1])
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

The configuration could also be partial and completed by the script. Finally, the configuration can be checked for correctness using the :meth:`check_all <simso.configuration.Configuration.check_all>` method::

        configuration.check_all()

This method will raise an exception if something is not correct.

Creating a configuration from scratch
-------------------------------------

It is also possible to create a new configuration from an empty configuration. This is done by instantiating a :class:`Configuration <simso.configuration.Configuration>` object without argument. Then, its attributes can be changed::

            configuration = Configuration()
            
            configuration.duration = 100 * configuration.cycles_per_ms

It is also possible to add tasks::

            configuration.add_task(name="T1", identifier=1, period=7,
                                   activation_date=0, wcet=3, deadline=7)

And of course processors::

            configuration.add_processor(name="CPU 1", identifier=1)

40 41 42 43 44 45 46
Finally, a scheduler is also required. For that, it's possible to use a custom scheduler::

            configuration.scheduler_info.filename = "examples/RM.py"

Or one of the schedulers embedded with SimSo::

            configuration.scheduler_info.clas = "simso.schedulers.RM"
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73


Creating the Model
------------------

A :class:`configuration <simso.configuration.Configuration>` is an object grouping every characteristics of the system (tasks, processors, schedulers, etc). Such a configuration can be passed to the :class:`Model <simso.core.Model.Model>` constructor in order to create the simulation::

        model = Model(configuration)

And the simulation can be run with the :meth:`run_model <simso.core.Model.Model.run_model>` method::

        model.run_model()

Some basic logs can be get through the :meth:`logs <simso.core.Model.Model.logs>` attribute::

        for log in model.logs:
            print(log)

First Example
-------------

The following script simulate a system loading from a simulation file or configured from scratch::

        import sys
        from simso.core import Model
        from simso.configuration import Configuration

74

75
        def main(argv):
76
            if len(argv) == 2:
77
                # Configuration load from a file.
78
                configuration = Configuration(argv[1])
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            else:
                # Manual configuration:
                configuration = Configuration()
                
                configuration.duration = 420 * configuration.cycles_per_ms

                # Add tasks:
                configuration.add_task(name="T1", identifier=1, period=7,
                                       activation_date=0, wcet=3, deadline=7)
                configuration.add_task(name="T2", identifier=2, period=12,
                                       activation_date=0, wcet=3, deadline=12)
                configuration.add_task(name="T3", identifier=3, period=20,
                                       activation_date=0, wcet=5, deadline=20)

                # Add a processor:
                configuration.add_processor(name="CPU 1", identifier=1)

                # Add a scheduler:
97 98
                #configuration.scheduler_info.filename = "examples/RM.py"
                configuration.scheduler_info.clas = "simso.schedulers.RM"
99 100 101 102 103 104 105 106 107 108 109 110 111 112

            # Check the config before trying to run it.
            configuration.check_all()

            # Init a model from the configuration.
            model = Model(configuration)

            # Execute the simulation.
            model.run_model()

            # Print logs.
            for log in model.logs:
                print(log)

113 114
        main(sys.argv)

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

More details
------------

It is possible to get more information from the tasks using :class:`Results <simso.core.results.Results>` class. For example we could get the computation time of the jobs::

            for task in model.results.tasks:
                print(task.name + ":")
                for job in task.jobs:
                    print("%s %.3f ms" % (job.name, job.computation_time))

Or the number of preemptions per task::

            for task in model.results.task_list:
                print("%s %d" % (task.name, sum([job.preemption_count for job in task.jobs])))

You can get all the metrics provided in the :class:`TaskR <simso.core.results.TaskR>` and :class:`JobR <simso.core.results.JobR>` objects. Read the documentation of these classes to know exactly what is directly accessible.

It is also possible to get the monitor object from each processors. This is a very detail history of the system. For example, you can count the number of context switches, where a context switch is something that happen when the previous task running on the same processor is different::

        cxt = 0
        for processor in model.processors:
            prev = None
            for evt in processor.monitor:
                if evt[1].event == ProcEvent.RUN:
                    if prev is not None and prev != evt[1].args.identifier:
                        cxt += 1
                    prev = evt[1].args.identifier

        print("Number of context switches (without counting the OS): " + str(cxt))