# 即時 Linux 核心原理和實務 Copyright (C) 2018 [Jim Huang (黃敬群)](http://wiki.csie.ncku.edu.tw/User/jserv) <jserv.tw@gmail.com> ==[提問單](https://docs.google.com/document/d/1oOKT5C417SnIXVKjhmWDKzH1fPiWOc7e9htJ4WbALFc/edit?usp=sharing)== / ==教材網址: https://goo.gl/V1bGsD== ![](https://i.imgur.com/m3VczLX.jpg) ## 簡介 「工業 4.0」反映了 real-time system (即時系統) 的需求,而自動控制工業大廠如 Siemens 和 KUKA 均投入研發資源在 GNU/Linux 為基礎的機器人設計,但尚未有圓滿地解決現有需求的單一方案,原因是 GNU/Linux 過於複雜而且得考慮到開發社群的多樣需求。 ## 1 分鐘看完數位馬達 [數位馬達](https://www.youtube.com/watch?v=AyQ1YeUxXeg) ![](https://i.imgur.com/ZnnBEg8.png) 一台機械手臂要價 120 萬貴嗎?其實很便宜 * 機械手臂的組裝速度,是人類的 3 倍 * 人類一天工作 8 小時,機械手臂可以 24 小時 * 人類一周休息 2 天,機械手臂工作 7 天 * 人類一年放假 130 天左右,機械手臂工作 365 天 整體算起來,機械手臂是人類的 12 倍產能 * 正常人一年的薪水最低算 30 萬就好,這樣換算起來,手臂的價格只要 1/3 * 10 隻手臂可以抵上百人,更省了管理問題 (上百人的公司,又要工會、福委會、育嬰室有的沒的) 所以建造機器人大軍絕對正確,千萬不要跟機器人競爭,時代在轉變,工作型態一定要轉變。 [[source](https://www.facebook.com/photo.php?fbid=10154122588853898&set=a.116227113897.99543.634428897)] * 機械手臂結構大致有: * Cartesian: PPP; * Cylindrical: RPP; * Spherical: RRP; * Articulated: RRR; * SCARA(Selective Compliant Articulated Robot for Assembly): RRP。 (P: prismatic ;R: revolute) * 機械手的部份,又有Normal;Sliding;Approach等三軸。 ## Real-time Linux Real-time Linux 不僅可拿來控制馬達,事實上應用範疇相當廣,早在 1997 年,Linux 被改造為 hard real-time 系統時,其中一個目的就是為了打造火箭控制系統 (請見 [RTLinux](https://en.wikipedia.org/wiki/RTLinux), 研究人員來自美國新墨西哥大學 [NMT]),經過這近 20 年的演化,產生了相當多不同的組合。 搭配的影片 * https://www.youtube.com/watch?v=IfP6J9Bxt2w * Hard Real-Time 還應用在交通系統, Google 自走車。 * 2015 年 Linux Foundation 宣佈 [Advance Real-Time Linux Project](https://www.marketwatch.com/press-release/the-linux-foundation-announces-project-to-advance-real-time-linux-2015-10-05?siteid=nbkh),目的在加強 Linux 在 Real-Time 方面的處理能力。 * 裡面出現了 Altera, ARM, Intel , IBM, TI 等公司,但有趣的是連 Google 都出現在這個計畫中,顯示這個時代很大部份是由軟體公司去參與甚至主導工業控制相關設備的製造 ## POSIX * [POSIX – 25 Years of Open Standard APIs](http://rtcmagazine.com/articles/view/103514) ![](https://i.imgur.com/cdkiqmb.jpg) * 用圖表說明了 PSE51, PSE52, PSE53, PSE54,以及用於即時處理應用的 IEEE 1003.13-2003 (POSIX.13) Profiles * 1986 年,IEEE 指定了一個委員會制定了一個開放作業系統的標準,稱為 POSIX (Portable Operating Systems Interface),最後加上個 "X" 是因為本質上是 UNIX 的標準 [API standards for Open Systems](http://www.opengroup.org/austin/papers/wp-apis.txt) * IEEE POSIX Standards for Real-time * The PASC Real-time System Services Working Group (SSWG-RT) has developed a series of standards that amend IEEE Std 1003.1-1990 and a profile standard (IEEE Std 1003.13-1998). * The Real-time amendments to IEEE Std 1003.1-1990 are as follows: * IEEE Std 1003.1b-1993 Realtime Extension * IEEE Std 1003.1c-1995 Threads * IEEE Std 1003.1d-1999 Additional Realtime Extensions * IEEE Std 1003.1j-2000 Advanced Realtime Extensions * IEEE Std 1003.1q-2000 Tracing * Note that RTLinux from FSMLabs turns the 1003.13 hierarchy upsidedown, with the smaller PSE51/52 realtime threads in control, and the full-figured Linux system (similar in functionality to PSE54) as just another thread under control of the realtime threads. * This is the opposite of what the PASC SSWG-RT had imagined when 1003.13-1998 was written, but it nonetheless works. * [POSIX module on top of Xenomai](https://git.xenomai.org/xenomai-2.6.git/tree/doc/txt/pse51-skin.txt) ## RTOS 概念 簡報檔案: [rtos-concept](https://www.dropbox.com/s/kary26fgomtn913/rtos-concept.pdf?dl=0) - [ ] Multi-core scheduling * Soft real-time industry practice * Multicore-aware scheduling (e.g. EDF variants) can provoke task migration * Task can migrate from one processor to free processor at runtime * [LITMUS^RT^](http://www.litmus-rt.org/): Linux patch for multicore-aware real-time scheduler policy * Pros: load balancing helps to meet deadlines * Cons: migration overhead may mitigate benefit (processor’s cache pollution); debugging overload scenario are complex to handle * Hard real-time industry practice: * Static priority-based scheduling (e.g. original RM) per core – Forbid migration, fixed allocation of tasks per core - [ ] RTOS Latency RTOS shall provide bounded execution on external/internal events – Interrupt latency: time between an external hardware event happens and the software interrupt handler is called. This latency includes device handler’s execution time – Scheduler latency: time between a software event and the process execution. This latency includes scheduler’s execution time ## Latency 變化 * 簡報檔案: [rt-linux-latency](https://www.dropbox.com/s/rcecglt7e3mybx7/rt-linux-latency.pdf?dl=0) - [ ] Fully Preemptible kernel * Preemption almost everywhere * Spinlocks turn into mutexes * No hard interrupt context * Preemptible critical section * Preemptible interrupt handlers * Preemptible “interrupt disable” code sequence • Priority inheritance * Deferred operation * Latency reduction * 簡報檔案: [preemption-model](https://www.dropbox.com/s/tvzkvzqz6eooo14/preemption-model.pdf?dl=0) ## Linux 網路相關的應用案例 * 簡報檔案: [rt-usecase](https://www.dropbox.com/s/tkb2ttg9dql8i1j/rt-usecase.pdf?dl=0) Changing The Priority Model ![](https://i.imgur.com/2bmMBow.png) ## Making Linux do Hard Real-time * 簡報檔案: [making-linux-do-hard-realtime](https://www.slideshare.net/jserv/making-linux-do-hard-realtime) ## SMP * 簡報檔案: [smp](https://www.dropbox.com/s/qj3xx62am1fmxya/smp.pdf?dl=0)