text_mode.rst 5.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 40 41 42 43 44 45 46 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 74 75 76 77 78 79 80 81 82 83 84 85 86 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 129 130 131 132 133 134 135 136 137
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::

        configuration = Configuration(argv[0])

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)

Finally, a scheduler is also required::

            configuration.scheduler_info.set_name("examples/RM.py",
                    configuration.cur_dir)

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

        def main(argv):
            if len(argv) == 1:
                # Configuration load from a file.
                configuration = Configuration(argv[0])
            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:
                configuration.scheduler_info.set_name("examples/RM.py",
                        configuration.cur_dir)

            # 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)


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))