text_mode.html 22.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Using SimSo in script mode &mdash; SimSo documentation</title>
    
    <link rel="stylesheet" href="_static/nature.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
17
        VERSION:     '0.8',
18 19 20 21 22 23 24 25 26 27 28 29
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="SimSo documentation" href="index.html" />
    <link rel="next" title="Main modules" href="modules.html" />
    <link rel="prev" title="How to write a scheduling policy" href="write_scheduler.html" /> 
  </head>
PE Hladik committed
30 31
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
32 33 34 35 36 37 38 39 40 41 42 43 44 45
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="modules.html" title="Main modules"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="write_scheduler.html" title="How to write a scheduling policy"
             accesskey="P">previous</a> |</li>
PE Hladik committed
46
        <li class="nav-item nav-item-0"><a href="index.html">SimSo documentation</a> &raquo;</li> 
47 48 49 50 51
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
PE Hladik committed
52
          <div class="body" role="main">
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
            
  <div class="section" id="using-simso-in-script-mode">
<h1><a class="toc-backref" href="#id1">Using SimSo in script mode</a><a class="headerlink" href="#using-simso-in-script-mode" title="Permalink to this headline"></a></h1>
<p>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.</p>
<div class="contents topic" id="table-of-contents">
<p class="topic-title first">Table of Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#using-simso-in-script-mode" id="id1">Using SimSo in script mode</a><ul>
<li><a class="reference internal" href="#loading-a-configuration-using-a-simulation-file" id="id2">Loading a configuration using a simulation file</a></li>
<li><a class="reference internal" href="#creating-a-configuration-from-scratch" id="id3">Creating a configuration from scratch</a></li>
<li><a class="reference internal" href="#creating-the-model" id="id4">Creating the Model</a></li>
<li><a class="reference internal" href="#first-example" id="id5">First Example</a></li>
<li><a class="reference internal" href="#more-details" id="id6">More details</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="loading-a-configuration-using-a-simulation-file">
<h2><a class="toc-backref" href="#id2">Loading a configuration using a simulation file</a><a class="headerlink" href="#loading-a-configuration-using-a-simulation-file" title="Permalink to this headline"></a></h2>
PE Hladik committed
72
<p>A <a class="reference internal" href="modules.html#module-simso.configuration.Configuration" title="simso.configuration.Configuration"><code class="xref py py-class docutils literal"><span class="pre">Configuration</span></code></a> can be initialized with a file passed to its constructor:</p>
73
<div class="highlight-python"><div class="highlight"><pre><span class="n">configuration</span> <span class="o">=</span> <span class="n">Configuration</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
74 75
</pre></div>
</div>
PE Hladik committed
76
<p>The configuration could also be partial and completed by the script. Finally, the configuration can be checked for correctness using the <code class="xref py py-meth docutils literal"><span class="pre">check_all</span></code> method:</p>
77 78 79 80 81 82 83
<div class="highlight-python"><div class="highlight"><pre><span class="n">configuration</span><span class="o">.</span><span class="n">check_all</span><span class="p">()</span>
</pre></div>
</div>
<p>This method will raise an exception if something is not correct.</p>
</div>
<div class="section" id="creating-a-configuration-from-scratch">
<h2><a class="toc-backref" href="#id3">Creating a configuration from scratch</a><a class="headerlink" href="#creating-a-configuration-from-scratch" title="Permalink to this headline"></a></h2>
PE Hladik committed
84
<p>It is also possible to create a new configuration from an empty configuration. This is done by instantiating a <a class="reference internal" href="modules.html#module-simso.configuration.Configuration" title="simso.configuration.Configuration"><code class="xref py py-class docutils literal"><span class="pre">Configuration</span></code></a> object without argument. Then, its attributes can be changed:</p>
85 86 87 88 89 90 91 92 93 94 95 96 97 98
<div class="highlight-python"><div class="highlight"><pre><span class="n">configuration</span> <span class="o">=</span> <span class="n">Configuration</span><span class="p">()</span>

<span class="n">configuration</span><span class="o">.</span><span class="n">duration</span> <span class="o">=</span> <span class="mi">100</span> <span class="o">*</span> <span class="n">configuration</span><span class="o">.</span><span class="n">cycles_per_ms</span>
</pre></div>
</div>
<p>It is also possible to add tasks:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">configuration</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;T1&quot;</span><span class="p">,</span> <span class="n">identifier</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span>
                       <span class="n">activation_date</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wcet</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">deadline</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span>
</pre></div>
</div>
<p>And of course processors:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">configuration</span><span class="o">.</span><span class="n">add_processor</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;CPU 1&quot;</span><span class="p">,</span> <span class="n">identifier</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
99 100 101 102 103 104
<p>Finally, a scheduler is also required. For that, it&#8217;s possible to use a custom scheduler:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">configuration</span><span class="o">.</span><span class="n">scheduler_info</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="s">&quot;examples/RM.py&quot;</span>
</pre></div>
</div>
<p>Or one of the schedulers embedded with SimSo:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">configuration</span><span class="o">.</span><span class="n">scheduler_info</span><span class="o">.</span><span class="n">clas</span> <span class="o">=</span> <span class="s">&quot;simso.schedulers.RM&quot;</span>
105 106 107 108 109
</pre></div>
</div>
</div>
<div class="section" id="creating-the-model">
<h2><a class="toc-backref" href="#id4">Creating the Model</a><a class="headerlink" href="#creating-the-model" title="Permalink to this headline"></a></h2>
PE Hladik committed
110
<p>A <a class="reference internal" href="modules.html#module-simso.configuration.Configuration" title="simso.configuration.Configuration"><code class="xref py py-class docutils literal"><span class="pre">configuration</span></code></a> is an object grouping every characteristics of the system (tasks, processors, schedulers, etc). Such a configuration can be passed to the <a class="reference internal" href="modules.html#simso.core.Model.Model" title="simso.core.Model.Model"><code class="xref py py-class docutils literal"><span class="pre">Model</span></code></a> constructor in order to create the simulation:</p>
111 112 113
<div class="highlight-python"><div class="highlight"><pre><span class="n">model</span> <span class="o">=</span> <span class="n">Model</span><span class="p">(</span><span class="n">configuration</span><span class="p">)</span>
</pre></div>
</div>
PE Hladik committed
114
<p>And the simulation can be run with the <a class="reference internal" href="modules.html#simso.core.Model.Model.run_model" title="simso.core.Model.Model.run_model"><code class="xref py py-meth docutils literal"><span class="pre">run_model</span></code></a> method:</p>
115 116 117
<div class="highlight-python"><div class="highlight"><pre><span class="n">model</span><span class="o">.</span><span class="n">run_model</span><span class="p">()</span>
</pre></div>
</div>
PE Hladik committed
118
<p>Some basic logs can be get through the <a class="reference internal" href="modules.html#simso.core.Model.Model.logs" title="simso.core.Model.Model.logs"><code class="xref py py-meth docutils literal"><span class="pre">logs</span></code></a> attribute:</p>
119 120 121 122 123 124 125 126 127 128 129 130
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">log</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">logs</span><span class="p">:</span>
    <span class="k">print</span><span class="p">(</span><span class="n">log</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="first-example">
<h2><a class="toc-backref" href="#id5">First Example</a><a class="headerlink" href="#first-example" title="Permalink to this headline"></a></h2>
<p>The following script simulate a system loading from a simulation file or configured from scratch:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">simso.core</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="kn">from</span> <span class="nn">simso.configuration</span> <span class="kn">import</span> <span class="n">Configuration</span>

131

132
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">argv</span><span class="p">):</span>
133
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">argv</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
134
        <span class="c"># Configuration load from a file.</span>
135
        <span class="n">configuration</span> <span class="o">=</span> <span class="n">Configuration</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Manual configuration:</span>
        <span class="n">configuration</span> <span class="o">=</span> <span class="n">Configuration</span><span class="p">()</span>

        <span class="n">configuration</span><span class="o">.</span><span class="n">duration</span> <span class="o">=</span> <span class="mi">420</span> <span class="o">*</span> <span class="n">configuration</span><span class="o">.</span><span class="n">cycles_per_ms</span>

        <span class="c"># Add tasks:</span>
        <span class="n">configuration</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;T1&quot;</span><span class="p">,</span> <span class="n">identifier</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span>
                               <span class="n">activation_date</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wcet</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">deadline</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span>
        <span class="n">configuration</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;T2&quot;</span><span class="p">,</span> <span class="n">identifier</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span>
                               <span class="n">activation_date</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wcet</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">deadline</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
        <span class="n">configuration</span><span class="o">.</span><span class="n">add_task</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;T3&quot;</span><span class="p">,</span> <span class="n">identifier</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span>
                               <span class="n">activation_date</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">wcet</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">deadline</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>

        <span class="c"># Add a processor:</span>
        <span class="n">configuration</span><span class="o">.</span><span class="n">add_processor</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;CPU 1&quot;</span><span class="p">,</span> <span class="n">identifier</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

        <span class="c"># Add a scheduler:</span>
154 155
        <span class="c">#configuration.scheduler_info.filename = &quot;examples/RM.py&quot;</span>
        <span class="n">configuration</span><span class="o">.</span><span class="n">scheduler_info</span><span class="o">.</span><span class="n">clas</span> <span class="o">=</span> <span class="s">&quot;simso.schedulers.RM&quot;</span>
156 157 158 159 160 161 162 163 164 165 166 167 168

    <span class="c"># Check the config before trying to run it.</span>
    <span class="n">configuration</span><span class="o">.</span><span class="n">check_all</span><span class="p">()</span>

    <span class="c"># Init a model from the configuration.</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Model</span><span class="p">(</span><span class="n">configuration</span><span class="p">)</span>

    <span class="c"># Execute the simulation.</span>
    <span class="n">model</span><span class="o">.</span><span class="n">run_model</span><span class="p">()</span>

    <span class="c"># Print logs.</span>
    <span class="k">for</span> <span class="n">log</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">logs</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="n">log</span><span class="p">)</span>
169 170

<span class="n">main</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span>
171 172 173 174 175
</pre></div>
</div>
</div>
<div class="section" id="more-details">
<h2><a class="toc-backref" href="#id6">More details</a><a class="headerlink" href="#more-details" title="Permalink to this headline"></a></h2>
PE Hladik committed
176
<p>It is possible to get more information from the tasks using <a class="reference internal" href="modules.html#simso.core.results.Results" title="simso.core.results.Results"><code class="xref py py-class docutils literal"><span class="pre">Results</span></code></a> class. For example we could get the computation time of the jobs:</p>
177 178 179 180 181 182 183
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">task</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">results</span><span class="o">.</span><span class="n">tasks</span><span class="p">:</span>
    <span class="k">print</span><span class="p">(</span><span class="n">task</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s">&quot;:&quot;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">task</span><span class="o">.</span><span class="n">jobs</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> </span><span class="si">%.3f</span><span class="s"> ms&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">job</span><span class="o">.</span><span class="n">computation_time</span><span class="p">))</span>
</pre></div>
</div>
<p>Or the number of preemptions per task:</p>
PE Hladik committed
184 185
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">task</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">results</span><span class="o">.</span><span class="n">tasks</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">task</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">task</span><span class="o">.</span><span class="n">preemption_count</span><span class="p">))</span>
186 187
</pre></div>
</div>
PE Hladik committed
188
<p>You can get all the metrics provided in the <a class="reference internal" href="modules.html#simso.core.results.TaskR" title="simso.core.results.TaskR"><code class="xref py py-class docutils literal"><span class="pre">TaskR</span></code></a> and <a class="reference internal" href="modules.html#simso.core.results.JobR" title="simso.core.results.JobR"><code class="xref py py-class docutils literal"><span class="pre">JobR</span></code></a> objects. Read the documentation of these classes to know exactly what is directly accessible.</p>
189 190 191 192 193 194
<p>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:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">cxt</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">processor</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">processors</span><span class="p">:</span>
    <span class="n">prev</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">for</span> <span class="n">evt</span> <span class="ow">in</span> <span class="n">processor</span><span class="o">.</span><span class="n">monitor</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">evt</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">event</span> <span class="o">==</span> <span class="n">ProcEvent</span><span class="o">.</span><span class="n">RUN</span><span class="p">:</span>
PE Hladik committed
195
            <span class="k">if</span> <span class="n">prev</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">prev</span> <span class="o">!=</span> <span class="n">evt</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">task</span><span class="p">:</span>
196
                <span class="n">cxt</span> <span class="o">+=</span> <span class="mi">1</span>
PE Hladik committed
197
            <span class="n">prev</span> <span class="o">=</span> <span class="n">evt</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">task</span>
198 199 200 201 202 203 204 205 206 207 208 209

<span class="k">print</span><span class="p">(</span><span class="s">&quot;Number of context switches (without counting the OS): &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cxt</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>


          </div>
      </div>
      <div class="clearer"></div>
    </div>
PE Hladik committed
210
    <div class="related" role="navigation" aria-label="related navigation">
211 212 213 214 215 216 217 218 219 220 221 222 223 224
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="modules.html" title="Main modules"
             >next</a> |</li>
        <li class="right" >
          <a href="write_scheduler.html" title="How to write a scheduling policy"
             >previous</a> |</li>
PE Hladik committed
225
        <li class="nav-item nav-item-0"><a href="index.html">SimSo documentation</a> &raquo;</li> 
226 227
      </ul>
    </div>
PE Hladik committed
228
    <div class="footer" role="contentinfo">
229 230 231
    </div>
  </body>
</html>