分享到plurk 分享到twitter 分享到facebook

版本 5b0b0885d47b31ee95c6d1022baa2b29d638a4d2

embedded/Lab40

Changes from 5b0b0885d47b31ee95c6d1022baa2b29d638a4d2 to current

---
title: Lab40: Scheduling (2015q1 Week #4)
toc: no
...

預期目標
------------
- 分析 `FreeRTOS</embedded/freertos>`_ 排程行為
- 學習 ARM Cortex-M3 架構並且理解 `SysTick<http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0179b/ar01s02s08.html>`_
- 分析 [FreeRTOS](/embedded/freertos) 排程行為
- 學習 ARM Cortex-M3 架構並且理解 [SysTick](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0179b/ar01s02s08.html)
- 設計出一個具體而微的 FreeRTOS 視覺化機制

Grasp
-------
- Understanding and validating the timing behavior of real-time systems is not trivial.
- Many real-time operating systems and their development environments do not provide tracing support, and provide only limited visualization, measurements and analysis tools.
- `Grasp<http://www.win.tue.nl/san/grasp/>`_ is a tool for tracing, visualizing and measuring the behavior of real-time systems. It provides a simple plugin infrastructure for extending Grasp with custom visualization and measurement methods.
- PDF: `Grasp: Tracing, Visualizing and Measuring the Behavior of Real-Time Systems<http://www.win.tue.nl/~mholende/publications/Holenderski2010.pdf>`_
![](/embedded/Lab40/grasp-n-os.png)

- mutex 視覺化輸出
* [Grasp](http://www.win.tue.nl/san/grasp/) 是個能夠追蹤、視覺化,以及測量即時系統中執行時期行為的工具,很容易擴充。
  - 使用案例: mutex 視覺化輸出

.. image:: /embedded/Lab12/mutex.png
![](/embedded/Lab12/mutex.png)

- RTOS scheduling 視覺化輸出
  - 使用案例: RTOS scheduling 視覺化輸出

.. image:: /embedded/Lab12/hsf.png
![](/embedded/Lab12/hsf.png)

Grasp 安裝
----------------
* 抓取預先編譯的程式碼 (x86/linux): 假設和 `Lab-39</embedded/Lab39>`_ 的工作目錄一樣是 ``workspace``
* 抓取預先編譯的程式碼 (x86/linux): 假設和 [Lab-39](/embedded/Lab39) 的工作目錄一樣是 ``workspace``

.. code-block:: prettyprint

    sudo apt-get install zip
    wget http://www.win.tue.nl/san/grasp/grasp_linux.zip
    unzip grasp_linux.zip

* 如果你的系統是 x86_64,需要額外安裝以下套件才能執行 Grasp
* 如果你的系統是 x86_64,需要額外安裝 i386 版本的 ``libxft2`` 和 ``libxss1`` 套件才能執行 Grasp,方法如下:

.. code-block:: prettyprint

    sudo apt-get install libxft2:i386
    sudo apt-get install libxss1:i386

    sudo dpkg --add-architecture i386
    sudo apt-get update 
    sudo apt-get install libxft2:i386 libxss1:i386

* 執行 Grasp、點選 ``examples`` 目錄並且挑選副檔名為 ``.grasp`` 的檔案,之後觀察視覺化輸出

.. image:: /embedded/Lab40/grasp.png
![](/embedded/Lab40/grasp.png)

取得 Visualizer 並測試
------------------------------
* 假設 `Lab-39</embedded/Lab39>`_ 的 ``freertos-lite`` 和 ``qemu_stm32`` 都放在 ``workspace`` 目錄
* Visualizer 需要 GDB,所以需要事先準備好

.. code-block:: prettyprint

    sudo apt-get install gdb-arm-none-eabi

* 假設 [Lab-39](/embedded/Lab39) 的 ``freertos-basic`` 和 ``qemu_stm32`` 都放在 ``workspace`` 目錄
  - ``cd worksapce``

* 取得原始程式碼並且執行

.. code-block:: prettyprint

    git clone git://github.com:embedded2015/visualizer.git || git clone https://github.com/embedded2015/visualizer.git
    cd visualizer
    make qemuauto

*  可以見到 idle task 和相關圖例
* 運作原理
  - 讀取 SysTick 中 current & reload 的數值,用來計算更精確的時間單位
  - 讀取 [SysTick](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0179b/ar01s02s08.html) 中 current & reload 的數值,用來計算更精確的時間單位
  - 增加追蹤執行狀態的 break point,藉以了解程式目前運作的位置
    + 增加程式 context switch 的 Hook
    + 將程式在 idle 時的 function 也列入追蹤

檢查清單
--------------
* 是否理解 ARM Cortex-M 的 interrupt 處理機制?
  - 檔案 [main.c](https://github.com/embedded2015/visualizer/blob/master/main.c) 的 ``USART2_IRQHandler``
  - 檔案 [startup_stm32f10x_md.s](https://github.com/embedded2015/freertos-basic/blob/master/freertos/libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/gcc_ride7/startup_stm32f10x_md.s) 的 ``g_pfnVectors``

* 是否清楚 system register 的作用?
  - 檔案 [main.c](https://github.com/embedded2015/visualizer/blob/master/main.c) 中,位址開頭為 0xe0 的常數或巨集: ``NVIC_INTERRUPTx_PRIORITY``, ``get_reload()`` / ``get_current()`` / ``get_time()`` (與 SysTick 相關)

參考作業要求
------------------
* 改寫 ``main.c`` 並且引入兩個週期性 Task,使其得以使用 CPU 資源,並且讓 grasp 予以視覺化
* 試著用程式碼自動計算出 context switch 的時間成本

挑戰題
----------
* 說明為何視覺化系統得以運作,以及目前實做的限制與改進方案
* 自動測試 [USART](/embedded/USART) 的中斷處理 (對應於 ``USART2_IRQHandler``),並且透過 grasp 視覺化

繳交作業
------------
- 截止日期:
   * Mar 28, 2015 (含) 之前

- 更新開發過程到共筆網站: `Lab 40 / 作業共筆<https://embedded2015.hackpad.com/2015q3-Week-4-Lab40>`_,需要標注自己的 ID
- 更新開發過程到共筆網站: [Lab 40 / 作業共筆](https://embedded2015.hackpad.com/2015q3-Week-4-Lab40),需要標注自己的 ID
    * 將符合作業提案的程式碼,提交到自行 fork 的 repository: https://github.com/embedded2015/visualizer
    * 注意: 要記得 fork `visualizer<https://github.com/embedded2015/visualizer>`_
    * 注意: 要記得 fork [visualizer](https://github.com/embedded2015/visualizer)

參考資料
---------------
* `RTOS Visualization</embedded/Sol12>`_
* `RTOS context switch</embedded/Sol8>`_
* `Context switching time<https://www.segger.com/embos-context-switching-time.html>`_
* [Grasp: Tracing, Visualizing and Measuring the Behavior of Real-Time Systems](http://www.win.tue.nl/~mholende/publications/Holenderski2010.pdf)
* [RTOS Visualization](/embedded/Sol12)
* [RTOS context switch](/embedded/Sol8)
* [Context switching time](https://www.segger.com/embos-context-switching-time.html)
* Cortex M3 SysTick Explained
  - [Part I](http://www.zembedded.com/cortex-m3-sys-tick-explained-part-i/), [Part II](http://www.zembedded.com/cortex-m3-systick-explained-part-ii/)