# 2018q3 Homework3 (dict)
contributed by < [`butastur-rtos`](https://github.com/butastur-rtos) >
###### tags: `homework3` `dict` `tst` `sysprog2018`
## [作業要求](https://hackmd.io/s/B1Bq5Jat7#)
* 學習用 gnuplot 繪圖
* make plot
* prefix search
* 學習 Ternary Search Tree
* 理解 Bloom Filter
* 學習及探討實際整合案例: Phonebook
* 設計實驗
* CPY 實作機制
* REF 實作機制
* 充分量化並以現代處理器架構的行為解讀 CPY 和 REF
### 學習用 gnuplot 繪圖
### make plot
暫時不知道要在 make plot 寫什麼,先加一行到 Makefile 裡
```shell
plot: $(TESTS)
```
### 學習 TST (Ternary Search Tree)
* 學習 [Trie](https://en.wikipedia.org/wiki/Trie)
* What is TST?
* 探討 TST 回應的統計分佈
* 以 perf 分析 TST 程式碼的執行時期行為並且充分解讀
* 以 ternary search tree 作為 auto-complete 和 prefix search 的實作機制
#### 學習 [Trie](https://en.wikipedia.org/wiki/Trie)
:::info
Trie 就是 prefix tree
:::
[Trie 視覺化](https://www.cs.usfca.edu/~galles/visualization/Trie.html)
例如增加 ipad
'i' 'p' 'a' 'd'
```graphviz
digraph Trie {
node [fontname="Arial"];
root [shape=circle,style=filled,color=green];
}
```
```graphviz
digraph Trie {
node [fontname="Arial"];
root [shape=circle,style=filled];
i [shape=circle,style=filled,color=green];
root -> i;
}
```
```graphviz
digraph Trie {
node [fontname="Arial"];
root [shape=circle,style=filled];
p[shape=circle,style=filled];
i [shape=circle,style=filled];
null[color=white,style=filled,fontcolor=white];
root -> i;
i -> null[color=white];
p[shape=circle,style=filled,color=green];
i -> p;
}
```
```graphviz
digraph Trie {
node [fontname="Arial"];
root [shape=circle,style=filled];
a[shape=circle,style=filled,color=green];
p[shape=circle,style=filled];
i [shape=circle,style=filled];
null[color=white,style=filled,fontcolor=white];
nulla[color=white,style=filled,fontcolor=white];
root -> i;
i -> null[color=white];
i -> p;
p -> nulla[color=white];
p -> a;
}
```
```graphviz
digraph Trie {
node [fontname="Arial"];
root [shape=circle,style=filled];
d[shape=circle,style=filled,color=green];
a[shape=circle,style=filled];
p[shape=circle,style=filled];
i [shape=circle,style=filled];
null[color=white,style=filled,fontcolor=white];
nulla[color=white,style=filled,fontcolor=white];
nulld[color=white,style=filled,fontcolor=white];
root -> i;
i -> null[color=white];
i -> p;
p -> nulla[color=white];
p -> a;
a -> d;
a -> nulld[color=white];
}
```
增加 ipod
'i' 'p' 'o' 'd'
```graphviz
digraph Trie {
root [shape=circle,style=filled];
node [fontname="Arial"];
d[shape=circle,style=filled];
a[shape=circle,style=filled];
p[shape=circle,style=filled];
i [shape=circle,style=filled,color=green];
null[color=white,style=filled,fontcolor=white];
nulla[color=white,style=filled,fontcolor=white];
nulld[color=white,style=filled,fontcolor=white];
root -> i;
i -> null[color=white];
i -> p;
p -> nulla[color=white];
p -> a;
a -> d;
a -> nulld[color=white];
}
```
```graphviz
digraph Trie {
root [shape=circle,style=filled];
node [fontname="Arial"];
d[shape=circle,style=filled];
a[shape=circle,style=filled];
p[shape=circle,style=filled,color=green];
i [shape=circle,style=filled];
null[color=white,style=filled,fontcolor=white];
nulla[color=white,style=filled,fontcolor=white];
nulld[color=white,style=filled,fontcolor=white];
root -> i;
i -> null[color=white];
i -> p;
p -> nulla[color=white];
p -> a;
a -> d;
a -> nulld[color=white];
}
```
```graphviz
digraph Trie {
root [shape=circle,style=filled];
node [fontname="Arial"];
d[shape=circle,style=filled];
a[shape=circle,style=filled];
p[shape=circle,style=filled];
i [shape=circle,style=filled];
null[color=white,style=filled,fontcolor=white];
o[shape=circle,style=filled,color=green];
nulld[color=white,style=filled,fontcolor=white];
root -> i;
i -> null[color=white];
i -> p -> o;
p -> a;
a -> d;
a -> nulld[color=white];
}
```
```graphviz
digraph Trie {
root [shape=circle,style=filled];
node [fontname="Arial"];
d1[shape=circle,style=filled,color=green,label=d];
d[shape=circle,style=filled];
a[shape=circle,style=filled];
p[shape=circle,style=filled];
i [shape=circle,style=filled];
null[color=white,style=filled,fontcolor=white];
o[shape=circle,style=filled];
nulld[color=white,style=filled,fontcolor=white];
nulld1[color=white,style=filled,fontcolor=white];
root -> i;
i -> null[color=white];
i -> p -> o;
p -> a;
a -> d;
a -> nulld[color=white];
o -> nulld1[color=white];
o -> d1;
}
```
舉例示範 Trie 在記憶體上的 cost
#### What is TST?
* TST 和 Trie 有什麼差別?
TST 是一種 Trie,而 Trie 就是 prefix tree
```graphviz
digraph TrieAndTST {
Trie[shape=rect,fontname="Arial"];
root [shape=circle,style=filled];
node [fontname="Arial"];
d0[shape=circle,style=filled,label=d];
d[shape=circle,style=filled];
a[shape=circle,style=filled];
p[shape=circle,style=filled];
i [shape=circle,style=filled];
null[color=white,style=filled,fontcolor=white];
o[shape=circle,style=filled];
nulld[color=white,style=filled,fontcolor=white];
nulld1[color=white,style=filled,fontcolor=white];
root -> i;
i -> null[color=white];
i -> p -> o;
p -> a;
a -> d;
a -> nulld[color=white];
o -> nulld1[color=white];
o -> d0;
TST[shape=rect]
i1 [shape=circle,style=filled,label=i];
p1 [shape=circle,style=filled,label=p];
a1 [shape=circle,style=filled,label=a];
d1 [shape=circle,style=filled,label=d];
d2 [shape=circle,style=filled,label=d];
o1 [shape=circle,style=filled,label=o];
null1[shape=circle,style=filled,label=null];
null2[shape=circle,style=filled,label=null];
i1 -> p1 -> a1 ->d1 -> null1;
a1 -> o1 -> d2 -> null2;
}
```
#### 探討 TST 回應的統計分佈
* 什麼是統計分佈?
#### 以 perf 分析 TST 程式碼的執行時期行為並且充分解讀
* [prefix search](https://hackmd.io/s/Bki0g_P3Z#)
* [source](https://github.com/sysprog21/prefix-search)
#### 以 ternary search tree 作為 auto-complete 和 prefix search 的實作機制
* auto-complete
* [prefix search](https://hackmd.io/s/Bki0g_P3Z#)
### 理解 Bloom Filter
* FP (false positive)
* FN (false negative)
* 一個 n bits 的 table
* 說明 Bloom Filter
## 實驗環境
CPU: Intel(R) Core(TM) i3-7130U CPU @ 2.70GHz
Memory: 4 GB
```shell
$ free -m
total used free shared buff/cache available
Mem: 3822 1366 1601 112 853 2110
置換: 2047 0 2047
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 142
model name : Intel(R) Core(TM) i3-7130U CPU @ 2.70GHz
stepping : 9
microcode : 0x8e
cpu MHz : 500.409
cache size : 3072 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm arat pln pts hwp hwp_notify hwp_act_window hwp_epp flush_l1d
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips : 5424.00
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 142
model name : Intel(R) Core(TM) i3-7130U CPU @ 2.70GHz
stepping : 9
microcode : 0x8e
cpu MHz : 500.036
cache size : 3072 KB
physical id : 0
siblings : 4
core id : 1
cpu cores : 2
apicid : 2
initial apicid : 2
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm arat pln pts hwp hwp_notify hwp_act_window hwp_epp flush_l1d
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips : 5424.00
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 2
vendor_id : GenuineIntel
cpu family : 6
model : 142
model name : Intel(R) Core(TM) i3-7130U CPU @ 2.70GHz
stepping : 9
microcode : 0x8e
cpu MHz : 500.009
cache size : 3072 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 2
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm arat pln pts hwp hwp_notify hwp_act_window hwp_epp flush_l1d
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips : 5424.00
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
processor : 3
vendor_id : GenuineIntel
cpu family : 6
model : 142
model name : Intel(R) Core(TM) i3-7130U CPU @ 2.70GHz
stepping : 9
microcode : 0x8e
cpu MHz : 500.025
cache size : 3072 KB
physical id : 0
siblings : 4
core id : 1
cpu cores : 2
apicid : 3
initial apicid : 3
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm arat pln pts hwp hwp_notify hwp_act_window hwp_epp flush_l1d
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips : 5424.00
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
```