linux2023: ahbji
− 1 :ST-Tree 運作原理分析
ST-tree 主要參考 AVL tree 的平衡操作,并引入了 rbtree 的結構特點,相對於 AVL tree ,這一特點有助於對代碼運作原理的解讀。
ST-tree 的結構定義
tree 節點由 st_node
結構體定義,結合了 AVL tree 和 rbtree 的特點:
- parent、left 、right 指針來自 rbtree
- hint 來自 AVL tree ,根據注釋内容可知,hint 記錄的是一種近似值,不像 AVL tree 那樣需要準確記錄當前節點的 depth 。
st_*(struct st_node *n)
系列巨集和函式定義了獲取當前 st_node 的親代、左右子代、前驅和後繼的指針
root 節點和 ST-tree 的插入操作
st_root
結構體可以用來找到 ST-tree 的 root 節點
st_root
在 treeint_init
中被初始化,在 treeint_insert
處理第一個節點時將之作爲 root 節點引用到 st_root
st_insert
負責將新節點 n 插入到 p 所在的位置,方向由 d 決定,插入后需要對 tree 進行更新
更新處理方法涉及到當前節點的選裝和 hint 更新,然後遞歸更新 parent。
和 AVL tree 不同的是:
- ST tree 是先旋轉再更新當前節點的 hint 。
- 這裏通過判斷是否無法更新當前平衡因子來決定是否繼續遞歸走訪 parent。
- 與之相對,AVL tree 是先更新 height 再旋轉,旋轉時會對當前節點及其 child 的 height 再做更新,另外,無論是左傾還是右傾處理,都有著更複雜的旋轉選擇。這為 ST tree 出現 worst case 埋下了伏筆。
子樹的旋轉采用了 AVL tree 的處理方式,需要注意的是,在實作中,左傾時右旋,右傾時左旋:
遺留不解之處
如何迅速找到 worst case ?
ST-tree 的節點移除操作
使用繁體中文書寫,留意資訊科技詞彙翻譯
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
jserv
ST-tree 的節點移除操作的設計思路來自 AVL tree ,根據注釋内容得知其詳情:
- The process of deletion in this tree structure is relatively more intricate…:
這部分注釋介紹了刪除操作在這棵樹結構中相對複雜。刪除操作和其他 BST 中的刪除操作類似。當然要刪除一個節點時,會有不同情況需要考慮。
- When removing a node, if the node to be deleted has a right child, the deletion process entails replacing the node to be removed with the first node encountered in the right subtree…:會在新插入節點的右子樹上繼續更新操作
如果要删除的節點有一個右子節點,刪除操作會將要刪除的節點替換爲後繼節點。這個替換過程涉及到節點的旋轉操作,以便在刪除后保持書的平衡。隨後,會在新插入節點的右子樹上繼續更新操作。
- Similarly, if the node to be deleted does not have a right child, the replacement process involves utilizing the first node found in the left subtree…:
類似地,如果要刪除的節點沒有右子節點,則會在左子樹中找到前驅節點。隨後会對新插入節點的左子樹做更新處理。
- In scenarios where the node to be deleted has no children (neither left nor right), it can be directly removed from the tree, and an update operation is invoked on the parent node of the deleted node.:
要刪除的節點沒有子節點(既没有左子節點也没有右子節點)時,可以直接從樹中刪除該節點,並在被刪除節點的親代節點上呼叫更新操作。
用前驅和後繼節點替換當前子樹的 root ,實作用的是 BST tree 的標準做法,詳情如注釋中的圖示:
需要注意的是,因爲 st_update 的缺陷,刪除時也會因爲 hint 更新不及時導致出現 worst case 的情況。
− 2 :指出上述程式碼可改進之處,特別跟 AVL tree 和 red-black tree 相比,並予以實作
關於和 AVL tree 和 rbtree 的對比,以及對於 ST tree 的 worst case 論述,fourcolor 的 linux 2023 Quiz01 作業做了很好的總結。
改進思路是全面使用 AVL tree 的方式來做旋轉和 hint 更新。因爲時間關係,實作尚未完成。
- 1 align_up 運作原理分析
題目要求,align_up
函式需要返回對齊到 alignment 的地址。
根據題目中樣例代碼中的注釋和 alignment & (alignment - 1)
的理解,我需要用加、減運算和 bitwise 操作實作儅 alignment 為 2 的冪時 sz 對齊到 alignment 的地址。
總結樣例輸出,可知:
- 儅 sz 能被 alignment 整除時,直接返回當前 sz
- 儅 sz 不能被 alignment 整除時,返回
sz - (sz % alignment) + alignment
。
因爲不能使用條件判斷,所以只能推導出公式 (sz + mask) & ~mask
來解決,其中 mask = alignment - 1
公式的推導有些運氣成分:我只是寫下這些内容,觀察了幾個小時,用排除法找到了這個公式
代碼如下:
關於數學推導過程,StevenChen8759 的作業太精彩了
- 2 align_up 在 Linux 核心原始程式碼找出類似 align_up 的程式碼,並舉例說明其用法
内核中明確使用這種方式處理地址對其的是 Network drive 文檔 qlge.rst ,其中 Dump kernel data structures in drgn 章節提到了相同的地址對其方式:
這種方式也可以處理符号位,避免將值設置爲負數,例如 powerpc kernel 的 vecemu.c
- 1 qsort-mt 運作原理分析
qsort-mt 使用的是基於 fork-join 模型的多執行緒快速排序方法:
- main 執行緒負責初始化 thread pool ,並從 pool 中取出第一個 qsort 執行緒對指定序列排序
- 第一個 qsort 執行緒找到 pivot 並分割好子序列后,則在 pool 中取出處於 idle 狀態 qsort 執行緒,以遞歸的方式對子序列進行快速排序。
- 所有子序列都排序完成後,則在第一個 qsort 執行緒中將所有 qsort 執行緒設置為 terminate 狀態,然後返回到 main 執行緒打印程序運行時間以及分別在 user space 和 kernel space 使用的 cpu 時間數。
遺留問題
爲何在 do-while 回圈中處理 pthread_xxx call 錯誤?
- 2 以 Thread Sanitizer 找出上述程式碼的 data race 並著手修正
使用 TSan (ThreadSanitizer) 編譯,可在執行時檢查是否有 data race 發生。
目前 Ubuntu 只在 22.04 以上版本支持 Tsan,低於 22.04 版本的 Ubuntu 在使用 TSan 編譯會報錯 cannot open libtsan_preinit.o: No such file or directory
分析上述 log ,可以得出以下信息:
- Thread T1 和 T2 分別是由 main Thread 創建的 qsort 執行緒
- T1 需要從 pool 中取出 T2 對子序列執行 qsort,因而修改 T2 的 st 狀態
然而此時,T2 正在
mtx_st
critical section 中輪詢其 st 狀態
可以看出,在 allocate_thread 函式中修改 pool[i].st 時并未在 mtx_st
critical section 中,這是導致 data race 的原因。
修正很簡單,只需要在 mtx_st
crtical section 中修改 pool[i].st 狀態便得到解決:
繼續探討現有實作的效能疑慮,為何平行程度不高?
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
jserv