or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
[Linux Kernel Class] Homework 1
- 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 →解釋上述程式碼運作原理
S-Tree 繼承了 AVL Tree 的特性,透過 Insert & Rotation 來平衡樹高,此目的可以保證在 Search Node 的時間複雜度維持在 O(log n),但因為 AVL Tree 存在著過度 Balance 的問題導致在做 Insert or Delete Node 的時候會帶來較大的開銷,此時設計者加入 red-black tree 的特性,也就是不會過度執行 Balance 進而可以平衡掉 AVL Tree 執行 Balance 帶來的開銷。
st_update()
時候會檢查 balance 如果左右子樹的樹高差距大於 1 則會進行st_rotate_right()
orst_rotate_left()
- 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 →指出上述程式碼可改進之處,特別跟 AVL tree 和 red-black tree 相比,並予以實作
如果在 Insert Node 發生 rotation 時,rotation 的 node 在 update 完恰好 hint 與 previous hint 相同,便不會繼續往 parent node 更新。這會讓 Tree 變成左傾樹或是右傾樹。
因為 S-Tree 只會在
n->hint == 0
orn->hint != prev_hint
條件下才會往 Parent Node 進行更新,所以在這情況下可能會變成 Link List 狀態。Search 的時間複雜度為O(n)
。- 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 →設計效能評比程式,探討上述程式碼和 red-black tree 效能落差
- 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 →說明上述程式碼的運作原理
分為兩個 Case:
alignment is power of two -
sz + mask
如果sz
不會被mask
整除,則sz + mask
會進位。此時& ~mask
可以保留aligment
的最高位元以後的所有位元,而達到 align_up。alignment isn't power of two -
如果
alignment
不為 2 的冪次方,可以透過((sz + mask) / alignment) * alignment)
來達到align_up
的效果。- 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 →在 Linux 核心原始程式碼找出類似 align_up 的程式碼,並舉例說明其用法
檢查資料結構大小是否與指定的
Size
對齊。kvm/include/test_util.h
kvm/lib/kvm_util.c
- 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 →解釋上述程式碼運作原理
參考原本的 Quick Sort 演算法,會利用 Divide and Conquer 的性質來進行排序,。
這邊利用 POSIX Thread 的概念將 Divide 出來的 Task 由 Sub-Thread 來進行。
該如何管理每個 Thread 的狀態?
首先必須將 Alloc 好的 Thread 利用
pthread_cond_wait()
讓其進入休眠狀態。當 Quick Sort Divide 出 Sub-Task 後則可以透過
pthread_cond_signal()
喚醒該 Thread 來工作。- 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 →以 Thread Sanitizer 找出上述程式碼的 data race 並著手修正
使用 ThreadSanitizer (aka TSan) 來檢查 Race Condition。
編譯完後執行可以發現
ThreadSanitizer
檢查到有 Race Condition 產生。這邊我們從這段訊息找到 Race Condition 發生的位置
0x7b4000000000
,同時也可以知道這塊為動態記憶體配置heap block of size 256
c.pool
資料結構分別存在pthread_mutex_t mtx_st
與pthread_cond_t cond_st
,來做 Data Protect。下面分別顯示出兩個 Thread 發生 Race Condition 的時間點。
Thread 1 -
由此程式碼所示,在讀取某個
struct qsort
需要透過pthread_mutex_lock(&qs->mtx_st)
來保證在同一時間點只有一個 Thread 在 Critical Section。Thread 2 -
這邊可以看到 Tread 2 修改了
struct qsort
裡的某個變數c->pool[i].st = ts_work;
但是沒有做 mutex_lock,因此在這邊發生了 Read & Write Race Condition。Solution -
這邊需要將
c->pool[i].st = ts_work;
放至mutex_lock
保護之後,以確保只有一個 Thread 進入到 Critical Section。- 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 →研讀 專題: lib/sort.c,提出上述程式碼效能改進之規劃並予以實作
參考 Heapsort 使用 Multi-Threads 方式 heapsort-mt.c

下圖表示一般的 Heapsort 在 Elements = 1000000 的效能表現。
下圖表示 Multi-Threads 在 Heapsort 在 Elements = 1000000 的效能表現。
需要再改進優化。