1{
2 "metadata": {
3  "kernelspec": {
4   "display_name": "Python 2",
5   "language": "python",
6   "name": "python2"
7  },
8  "language_info": {
9   "codemirror_mode": {
10    "name": "ipython",
11    "version": 2
12   },
13   "file_extension": ".py",
14   "mimetype": "text/x-python",
15   "name": "python",
16   "nbconvert_exporter": "python",
17   "pygments_lexer": "ipython2",
18   "version": "2.7.9"
19  },
20  "name": ""
21 },
22 "nbformat": 3,
23 "nbformat_minor": 0,
24 "worksheets": [
25  {
26   "cells": [
27    {
28     "cell_type": "heading",
29     "level": 1,
30     "metadata": {},
31     "source": [
32      "Setup"
33     ]
34    },
35    {
36     "cell_type": "code",
37     "collapsed": false,
38     "input": [
39      "from trappy.stats.Topology import Topology\n",
40      "from bart.sched.SchedMultiAssert import SchedMultiAssert\n",
41      "from bart.sched.SchedAssert import SchedAssert\n",
42      "import trappy\n",
43      "import os\n",
44      "import operator\n",
45      "import json\n",
46      "\n",
47      "#Define a CPU Topology (for multi-cluster systems)\n",
48      "BIG = [1, 2]\n",
49      "LITTLE = [0, 3, 4, 5]\n",
50      "CLUSTERS = [BIG, LITTLE]\n",
51      "topology = Topology(clusters=CLUSTERS)\n",
52      "\n",
53      "BASE_PATH = \"/Users/kapileshwarsingh/AnalysisRawData/LPC/sched_deadline/\"\n",
54      "\n",
55      "THRESHOLD = 10.0\n",
56      "def between_threshold(a, b):\n",
57      "    return abs(((a - b) * 100.0) / b) < THRESHOLD"
58     ],
59     "language": "python",
60     "metadata": {},
61     "outputs": [],
62     "prompt_number": 3
63    },
64    {
65     "cell_type": "heading",
66     "level": 1,
67     "metadata": {},
68     "source": [
69      "Periodic Yield"
70     ]
71    },
72    {
73     "cell_type": "markdown",
74     "metadata": {},
75     "source": [
76      "The thread periodic_yeild is woken up at 30ms intervals where it calls sched_yield and relinquishes its time-slice.\n",
77      "The expectation is that the task will have a duty cycle < 1% and a period of 30ms.\n",
78      "\n",
79      "There are two threads, and the rank=1 conveys that the condition is true for one of the threads with the name \"periodic_yeild\"\n"
80     ]
81    },
82    {
83     "cell_type": "code",
84     "collapsed": false,
85     "input": [
86      "TRACE_FILE = os.path.join(BASE_PATH, \"yield\")\n",
87      "ftrace = trappy.FTrace(TRACE_FILE, \"cpuhog\")\n",
88      "\n",
89      "# Assert Period\n",
90      "s = SchedMultiAssert(ftrace, topology, execnames=\"periodic_yield\")\n",
91      "if s.assertPeriod(30, between_threshold, rank=1):\n",
92      "    print \"PASS: Period\"\n",
93      "    print json.dumps(s.getPeriod(), indent=3)\n",
94      "\n",
95      "print \"\"\n",
96      "    \n",
97      "# Assert DutyCycle    \n",
98      "if s.assertDutyCycle(1, operator.lt, window=(0,4), rank=2):\n",
99      "    print \"PASS: DutyCycle\"\n",
100      "    print json.dumps(s.getDutyCycle(window=(0,4)), indent=3)"
101     ],
102     "language": "python",
103     "metadata": {},
104     "outputs": [
105      {
106       "output_type": "stream",
107       "stream": "stdout",
108       "text": [
109        "PASS: Period\n",
110        "{\n",
111        "   \"1844\": {\n",
112        "      \"period\": 1.0085000000401578, \n",
113        "      \"task_name\": \"periodic_yield\"\n",
114        "   }, \n",
115        "   \"1845\": {\n",
116        "      \"period\": 29.822017857142669, \n",
117        "      \"task_name\": \"periodic_yield\"\n",
118        "   }\n",
119        "}\n",
120        "\n",
121        "PASS: DutyCycle\n",
122        "{\n",
123        "   \"1844\": {\n",
124        "      \"task_name\": \"periodic_yield\", \n",
125        "      \"dutycycle\": 0.074749999998857675\n",
126        "   }, \n",
127        "   \"1845\": {\n",
128        "      \"task_name\": \"periodic_yield\", \n",
129        "      \"dutycycle\": 0.03862499999343072\n",
130        "   }\n",
131        "}\n"
132       ]
133      }
134     ],
135     "prompt_number": 10
136    },
137    {
138     "cell_type": "heading",
139     "level": 1,
140     "metadata": {},
141     "source": [
142      "CPU Hog"
143     ]
144    },
145    {
146     "cell_type": "markdown",
147     "metadata": {},
148     "source": [
149      "The reservation of a CPU hogging task is set to 10ms for every 100ms. The assertion ensures a duty cycle of 10%"
150     ]
151    },
152    {
153     "cell_type": "code",
154     "collapsed": false,
155     "input": [
156      "TRACE_FILE = os.path.join(BASE_PATH, \"cpuhog\")\n",
157      "ftrace = trappy.FTrace(TRACE_FILE, \"cpuhog\")\n",
158      "s = SchedMultiAssert(ftrace, topology, execnames=\"cpuhog\")\n",
159      "s.plot().view()\n",
160      "\n",
161      "# Assert DutyCycle\n",
162      "if s.assertDutyCycle(10, between_threshold, window=(0, 5), rank=1):\n",
163      "    print \"PASS: DutyCycle\"\n",
164      "    print json.dumps(s.getDutyCycle(window=(0, 5)), indent=3)"
165     ],
166     "language": "python",
167     "metadata": {},
168     "outputs": [
169      {
170       "html": [
171        "<style>\n",
172        "/*\n",
173        "\n",
174        " *    Copyright 2015-2015 ARM Limited\n",
175        "\n",
176        " *\n",
177        "\n",
178        " * Licensed under the Apache License, Version 2.0 (the \"License\");\n",
179        "\n",
180        " * you may not use this file except in compliance with the License.\n",
181        "\n",
182        " * You may obtain a copy of the License at\n",
183        "\n",
184        " *\n",
185        "\n",
186        " *     http://www.apache.org/licenses/LICENSE-2.0\n",
187        "\n",
188        " *\n",
189        "\n",
190        " * Unless required by applicable law or agreed to in writing, software\n",
191        "\n",
192        " * distributed under the License is distributed on an \"AS IS\" BASIS,\n",
193        "\n",
194        " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
195        "\n",
196        " * See the License for the specific language governing permissions and\n",
197        "\n",
198        " * limitations under the License.\n",
199        "\n",
200        " */\n",
201        "\n",
202        "\n",
203        "\n",
204        ".d3-tip {\n",
205        "\n",
206        "  line-height: 1;\n",
207        "\n",
208        "  padding: 12px;\n",
209        "\n",
210        "  background: rgba(0, 0, 0, 0.6);\n",
211        "\n",
212        "  color: #fff;\n",
213        "\n",
214        "  border-radius: 2px;\n",
215        "\n",
216        "  position: absolute !important;\n",
217        "\n",
218        "  z-index: 99999;\n",
219        "\n",
220        "}\n",
221        "\n",
222        "\n",
223        "\n",
224        ".d3-tip:after {\n",
225        "\n",
226        "  box-sizing: border-box;\n",
227        "\n",
228        "  pointer-events: none;\n",
229        "\n",
230        "  display: inline;\n",
231        "\n",
232        "  font-size: 10px;\n",
233        "\n",
234        "  width: 100%;\n",
235        "\n",
236        "  line-height: 1;\n",
237        "\n",
238        "  color: rgba(0, 0, 0, 0.6);\n",
239        "\n",
240        "  content: \"\\25BC\";\n",
241        "\n",
242        "  position: absolute !important;\n",
243        "\n",
244        "  z-index: 99999;\n",
245        "\n",
246        "  text-align: center;\n",
247        "\n",
248        "}\n",
249        "\n",
250        "\n",
251        "\n",
252        ".d3-tip.n:after {\n",
253        "\n",
254        "  margin: -1px 0 0 0;\n",
255        "\n",
256        "  top: 100%;\n",
257        "\n",
258        "  left: 0;\n",
259        "\n",
260        "}\n",
261        "\n",
262        "\n",
263        "\n",
264        ".chart {\n",
265        "\n",
266        "  shape-rendering: crispEdges;\n",
267        "\n",
268        "}\n",
269        "\n",
270        "\n",
271        "\n",
272        ".mini text {\n",
273        "\n",
274        "  font: 9px sans-serif;\n",
275        "\n",
276        "}\n",
277        "\n",
278        "\n",
279        "\n",
280        ".main text {\n",
281        "\n",
282        "  font: 12px sans-serif;\n",
283        "\n",
284        "}\n",
285        "\n",
286        "\n",
287        "\n",
288        ".axis line, .axis path {\n",
289        "\n",
290        "  stroke: black;\n",
291        "\n",
292        "}\n",
293        "\n",
294        "\n",
295        "\n",
296        ".miniItem {\n",
297        "\n",
298        "  stroke-width: 8;\n",
299        "\n",
300        "}\n",
301        "\n",
302        "\n",
303        "\n",
304        ".brush .extent {\n",
305        "\n",
306        "\n",
307        "\n",
308        "  stroke: #000;\n",
309        "\n",
310        "  fill-opacity: .125;\n",
311        "\n",
312        "  shape-rendering: crispEdges;\n",
313        "\n",
314        "}\n",
315        "\n",
316        "</style>\n",
317        "<div id=\"fig_41c7653cedde4765ae1f166e75c4fb08\" class=\"eventplot\">\n",
318        "        <script>\n",
319        "            var req = require.config( {\n",
320        "\n",
321        "                paths: {\n",
322        "\n",
323        "                    \"EventPlot\": \"https://rawgit.com/sinkap/7f89de3e558856b81f10/raw/46144f8f8c5da670c54f826f0c634762107afc66/EventPlot\",\n",
324        "                    \"d3-tip\": \"http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3\",\n",
325        "                    \"d3\": \"http://d3js.org/d3.v3.min\"\n",
326        "                },\n",
327        "                shim: {\n",
328        "                    \"d3-tip\": [\"d3\"],\n",
329        "                    \"EventPlot\": {\n",
330        "\n",
331        "                        \"deps\": [\"d3-tip\", \"d3\" ],\n",
332        "                        \"exports\":  \"EventPlot\"\n",
333        "                    }\n",
334        "                }\n",
335        "            });\n",
336        "            req([\"require\", \"EventPlot\"], function() {\n",
337        "               EventPlot.generate('fig_41c7653cedde4765ae1f166e75c4fb08', 'https://rawgit.com/sinkap/e9bc2394cf322f4dad0d/raw/014fae226c847a467fba541fbc390e18acea127b/fig_41c7653cedde4765ae1f166e75c4fb08.json');\n",
338        "            });\n",
339        "        </script>\n",
340        "        </div>"
341       ],
342       "metadata": {},
343       "output_type": "display_data",
344       "text": [
345        "<IPython.core.display.HTML object>"
346       ]
347      },
348      {
349       "output_type": "stream",
350       "stream": "stdout",
351       "text": [
352        "PASS: DutyCycle\n",
353        "{\n",
354        "   \"1852\": {\n",
355        "      \"task_name\": \"cpuhog\", \n",
356        "      \"dutycycle\": 10.050119999991693\n",
357        "   }\n",
358        "}\n"
359       ]
360      }
361     ],
362     "prompt_number": 11
363    },
364    {
365     "cell_type": "heading",
366     "level": 1,
367     "metadata": {},
368     "source": [
369      "Changing Reservations"
370     ]
371    },
372    {
373     "cell_type": "markdown",
374     "metadata": {},
375     "source": [
376      "A CPU hogging task has reservations set in the increasing order starting from 10%  followed by a 2s period of normal execution"
377     ]
378    },
379    {
380     "cell_type": "code",
381     "collapsed": false,
382     "input": [
383      "TRACE_FILE = os.path.join(BASE_PATH, \"cancel_dl_timer\")\n",
384      "ftrace = trappy.FTrace(TRACE_FILE, \"cpuhog\")\n",
385      "s = SchedAssert(ftrace, topology, execname=\"cpuhog\")\n",
386      "s.plot().view()\n",
387      "\n",
388      "NUM_PHASES = 10\n",
389      "PHASE_DURATION = 2\n",
390      "start = s.getStartTime()\n",
391      "DUTY_CYCLE_FACTOR = 10\n",
392      "\n",
393      "\n",
394      "for phase in range(NUM_PHASES + 1):\n",
395      "    window = (start + (phase * PHASE_DURATION),\n",
396      "              start + ((phase + 1) * PHASE_DURATION))\n",
397      "    \n",
398      "    if phase % 2 == 0:\n",
399      "        DUTY_CYCLE = (phase + 2) * DUTY_CYCLE_FACTOR / 2\n",
400      "    else:\n",
401      "        DUTY_CYCLE = 100\n",
402      "\n",
403      "\n",
404      "    print \"WINDOW -> [{:.2f}, {:.2f}]\".format(window[0],\n",
405      "                                            window[1])\n",
406      "    \n",
407      "    \n",
408      "    \n",
409      "    if s.assertDutyCycle(DUTY_CYCLE, between_threshold, window=window):\n",
410      "        print \"PASS: Expected={} Actual={:.2f} THRESHOLD={}\".format(DUTY_CYCLE,\n",
411      "                                                                s.getDutyCycle(window=window),\n",
412      "                                                                THRESHOLD)\n",
413      "    else:\n",
414      "        print \"FAIL: Expected={} Actual={:.2f} THRESHOLD={}\".format(DUTY_CYCLE,\n",
415      "                                                                s.getDutyCycle(window=window),\n",
416      "                                                                THRESHOLD)\n",
417      "    \n",
418      "    print \"\""
419     ],
420     "language": "python",
421     "metadata": {},
422     "outputs": [
423      {
424       "html": [
425        "<style>\n",
426        "/*\n",
427        "\n",
428        " *    Copyright 2015-2015 ARM Limited\n",
429        "\n",
430        " *\n",
431        "\n",
432        " * Licensed under the Apache License, Version 2.0 (the \"License\");\n",
433        "\n",
434        " * you may not use this file except in compliance with the License.\n",
435        "\n",
436        " * You may obtain a copy of the License at\n",
437        "\n",
438        " *\n",
439        "\n",
440        " *     http://www.apache.org/licenses/LICENSE-2.0\n",
441        "\n",
442        " *\n",
443        "\n",
444        " * Unless required by applicable law or agreed to in writing, software\n",
445        "\n",
446        " * distributed under the License is distributed on an \"AS IS\" BASIS,\n",
447        "\n",
448        " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
449        "\n",
450        " * See the License for the specific language governing permissions and\n",
451        "\n",
452        " * limitations under the License.\n",
453        "\n",
454        " */\n",
455        "\n",
456        "\n",
457        "\n",
458        ".d3-tip {\n",
459        "\n",
460        "  line-height: 1;\n",
461        "\n",
462        "  padding: 12px;\n",
463        "\n",
464        "  background: rgba(0, 0, 0, 0.6);\n",
465        "\n",
466        "  color: #fff;\n",
467        "\n",
468        "  border-radius: 2px;\n",
469        "\n",
470        "  position: absolute !important;\n",
471        "\n",
472        "  z-index: 99999;\n",
473        "\n",
474        "}\n",
475        "\n",
476        "\n",
477        "\n",
478        ".d3-tip:after {\n",
479        "\n",
480        "  box-sizing: border-box;\n",
481        "\n",
482        "  pointer-events: none;\n",
483        "\n",
484        "  display: inline;\n",
485        "\n",
486        "  font-size: 10px;\n",
487        "\n",
488        "  width: 100%;\n",
489        "\n",
490        "  line-height: 1;\n",
491        "\n",
492        "  color: rgba(0, 0, 0, 0.6);\n",
493        "\n",
494        "  content: \"\\25BC\";\n",
495        "\n",
496        "  position: absolute !important;\n",
497        "\n",
498        "  z-index: 99999;\n",
499        "\n",
500        "  text-align: center;\n",
501        "\n",
502        "}\n",
503        "\n",
504        "\n",
505        "\n",
506        ".d3-tip.n:after {\n",
507        "\n",
508        "  margin: -1px 0 0 0;\n",
509        "\n",
510        "  top: 100%;\n",
511        "\n",
512        "  left: 0;\n",
513        "\n",
514        "}\n",
515        "\n",
516        "\n",
517        "\n",
518        ".chart {\n",
519        "\n",
520        "  shape-rendering: crispEdges;\n",
521        "\n",
522        "}\n",
523        "\n",
524        "\n",
525        "\n",
526        ".mini text {\n",
527        "\n",
528        "  font: 9px sans-serif;\n",
529        "\n",
530        "}\n",
531        "\n",
532        "\n",
533        "\n",
534        ".main text {\n",
535        "\n",
536        "  font: 12px sans-serif;\n",
537        "\n",
538        "}\n",
539        "\n",
540        "\n",
541        "\n",
542        ".axis line, .axis path {\n",
543        "\n",
544        "  stroke: black;\n",
545        "\n",
546        "}\n",
547        "\n",
548        "\n",
549        "\n",
550        ".miniItem {\n",
551        "\n",
552        "  stroke-width: 8;\n",
553        "\n",
554        "}\n",
555        "\n",
556        "\n",
557        "\n",
558        ".brush .extent {\n",
559        "\n",
560        "\n",
561        "\n",
562        "  stroke: #000;\n",
563        "\n",
564        "  fill-opacity: .125;\n",
565        "\n",
566        "  shape-rendering: crispEdges;\n",
567        "\n",
568        "}\n",
569        "\n",
570        "</style>\n",
571        "<div id=\"fig_421afa8cc8234df49030c900b680220b\" class=\"eventplot\">\n",
572        "        <script>\n",
573        "            var req = require.config( {\n",
574        "\n",
575        "                paths: {\n",
576        "\n",
577        "                    \"EventPlot\": \"https://rawgit.com/sinkap/7f89de3e558856b81f10/raw/46144f8f8c5da670c54f826f0c634762107afc66/EventPlot\",\n",
578        "                    \"d3-tip\": \"http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3\",\n",
579        "                    \"d3\": \"http://d3js.org/d3.v3.min\"\n",
580        "                },\n",
581        "                shim: {\n",
582        "                    \"d3-tip\": [\"d3\"],\n",
583        "                    \"EventPlot\": {\n",
584        "\n",
585        "                        \"deps\": [\"d3-tip\", \"d3\" ],\n",
586        "                        \"exports\":  \"EventPlot\"\n",
587        "                    }\n",
588        "                }\n",
589        "            });\n",
590        "            req([\"require\", \"EventPlot\"], function() {\n",
591        "               EventPlot.generate('fig_421afa8cc8234df49030c900b680220b', 'https://rawgit.com/sinkap/a207675f6483aa0b9342/raw/825717935112f36fe996b77093c0c71d3871fee4/fig_421afa8cc8234df49030c900b680220b.json');\n",
592        "            });\n",
593        "        </script>\n",
594        "        </div>"
595       ],
596       "metadata": {},
597       "output_type": "display_data",
598       "text": [
599        "<IPython.core.display.HTML object>"
600       ]
601      },
602      {
603       "output_type": "stream",
604       "stream": "stdout",
605       "text": [
606        "WINDOW -> [0.00, 2.00]\n",
607        "PASS: Expected=10 Actual=10.38 THRESHOLD=10.0\n",
608        "\n",
609        "WINDOW -> [2.00, 4.00]\n",
610        "PASS: Expected=100 Actual=99.60 THRESHOLD=10.0\n",
611        "\n",
612        "WINDOW -> [4.00, 6.00]\n",
613        "PASS: Expected=20 Actual=21.06 THRESHOLD=10.0\n",
614        "\n",
615        "WINDOW -> [6.00, 8.00]\n",
616        "PASS: Expected=100 Actual=95.69 THRESHOLD=10.0\n",
617        "\n",
618        "WINDOW -> [8.00, 10.00]\n",
619        "PASS: Expected=30 Actual=31.78 THRESHOLD=10.0\n",
620        "\n",
621        "WINDOW -> [10.00, 12.00]\n",
622        "PASS: Expected=100 Actual=98.23 THRESHOLD=10.0\n",
623        "\n",
624        "WINDOW -> [12.00, 14.00]\n",
625        "PASS: Expected=40 Actual=40.74 THRESHOLD=10.0\n",
626        "\n",
627        "WINDOW -> [14.00, 16.00]\n",
628        "PASS: Expected=100 Actual=97.58 THRESHOLD=10.0\n",
629        "\n",
630        "WINDOW -> [16.00, 18.00]\n",
631        "PASS: Expected=50 Actual=52.51 THRESHOLD=10.0\n",
632        "\n",
633        "WINDOW -> [18.00, 20.00]\n",
634        "PASS: Expected=100 Actual=96.38 THRESHOLD=10.0\n",
635        "\n",
636        "WINDOW -> [20.00, 22.00]\n",
637        "PASS: Expected=60 Actual=60.71 THRESHOLD=10.0\n",
638        "\n"
639       ]
640      }
641     ],
642     "prompt_number": 4
643    }
644   ],
645   "metadata": {}
646  }
647 ]
648}