主講人: jserv / 課程討論區: 2019 年系統軟體課程
返回「Linux 核心設計」課程進度表
Robert Love 在 Linux Kernel Development 一書論及浮點運算:
No (Easy) Use of Floating Point
When using floating-point instructions kernel normally catches a trap and then initiates the transition from integer to floating point mode. Unlike user-space, the kernel does not have the luxury of seamless support for floating point because it cannot easily trap itself. Using a floating point inside the kernel requires manually saving and restoring the floating point registers. Except in the rare cases, no floating-point operations are in the kernel.
Rusty Russell 在 Unreliable Guide To Hacking The Linux Kernel 則說:
The FPU context is not saved; even in user context the FPU state probably won't correspond with the current process: you would mess with some user process' FPU state. If you really want to do this, you would have to explicitly save/restore the full FPU state (and avoid context switches). It is generally a bad idea; use fixed point arithmetic first.
( )
表示順序範例:
MathEX 是成功大學師生開發的數學表示式求值工具,接受一組數學表達式,輸出這段數學表達式所對應的答案。
取得 MathEX
原始程式碼並測試:
預期可見以下測試輸出:
MathEX
基本的使用方式:
上面的程式碼為範例,利用 expr_create()
將字串 x = 40, add(2, x)
輸入,再使用 expr_eval()
來產生輸出結果
這個工具提供了 4 個 API:
struct expr *expr_create(const char *s, size_t len, struct expr_var_list *vars, struct expr_func *funcs)
:將輸入的字串數學表達式編譯成 struct expr
型態,並且回傳float expr_eval(struct expr *e)
:計算 struct expr
的結果並回傳void expr_destroy(struct expr *e, struct expr_var_list *vars)
:清除記憶體,vars
可以為 NULL (不清除 vars
的記憶體)struct expr_var *expr_var(struct expr_var_list *vars, const char *s, size_t len)
:在給定的變數清單 vars
中回傳對應名稱 s
的變數,如果清單中不存在該變數則會產生一個並回傳這類的數學表達式分析工具可以被應用於工程計算機類的軟體,如:insect 是個支援物理單位的工程計算機,其中程式碼 insect/src/Insect/Parser.purs 就是計算機的數學表達式的解析器
MathEx
程式碼透過 expression.h
的 macro 以及 struct 可以找到用來表示 expression 的 struct:
從上面的定義可以看到 struct expr
中,param.op.args
裡面其實包著 struct expr
的 pointer,在後續的程式中可以發現這個 pointer 會指向 1 或 2 個 struct expr
,因此得知 struct expr
是一個 tree 的結構
接著可以透過 expression.c
中的 expr_eval()
中得知如何計算 expression 結果:
從上面可以知道,它透過遞迴呼叫,計算目前節點的左子節點跟右子節點的結果,最後根據自己的 operator 將左子節點跟右子節點的結果作運算
利用 test-bench.c
來查看結果:
calc.c
檔案中,和 fibdrv 一樣有 character device,但註冊用的 kernel API 不同 (register_chrdev
vs. alloc_chrdev_region
),能否解釋其差異和適用場合呢?scripts/test.sh
檔案中,有一道命令為 sudo chmod 0666
,這個作用為何?對於我們測試有何幫助?能否對 fibdrv 建立的 /dev/fibonacci
device file 也套用類似修改呢?另外,請解釋 device file 在核心及使用者層級的功能calc.c
檔案中,用到 copy_to_user
這個 kernel API,其作用為何?本例使用該 API 做了什麼事?若是資料量增長,是否會有效能的嚴重衝擊呢?MathEx
進行效能分析?能否找出相似的 math expression 來分析比較呢?
MathEx
原始程式碼的 expression.[ch]
裡頭 vec
相關的程式碼,主要做什麼事?有沒有發現類似 list 使用到的技巧呢?
mathex/test-unit.c
的測試項目MathEx
一類的 math expression 在真實世界有哪些應用?甚至,是否在 Linux 核心就存在類似的程式碼?
v4.15+
呢?MathEx
整合到 calc.c
中 (作為 LKM 的形式),過程中需要一併完成以下:
編輯 Homework2 作業區共筆,將你的觀察、上述要求的解說、應用場合探討,以及各式效能改善過程,善用 gnuplot 製圖,紀錄於新建立的共筆
Mar 19, 2019 (含) 中午之前
越早在 GitHub 上有動態、越早接受 code review,評分越高