owned this note
owned this note
Published
Linked with GitHub
gnuplot
===
以下以作業1,phonebook實踐
## 理解gnuplot
檔案名稱:.xxx.gp檔
(通常我們會在phonebook資料夾裏面再建一個script資料夾,然後我們再裏面存放runtime.gp檔
以runtime.gp為例)
```clike=
#註解行
reset //重新設定
set ylabel 'time(sec)'
set style fill solid
set title 'perfomance comparison' //設定圖形標題為'內文'
set term png enhanced font 'Verdana,10' //設定圖片類型
set output 'runtime.png' //存檔名稱
plot [:][:0.150]'output.txt' using 2:xtic(1) with histogram title 'original', \
//擷取output.txt內容來繪圖,且y軸範圍0-0.15,繪製成 histogram 且名稱 original
'' using ($0-0.06):($2+0.001):2 with labels title ' ', \
//''因為同樣是來自output.txt資料;$2,$3代表第2欄第3欄
'' using 3:xtic(1) with histogram title 'optimized' , \
'' using ($0+0.3):($3+0.0015):3 with labels title ' '
```

e.g
output.txt data:
```
append() 0.048240 0.040298 0.057908
findName() 0.006495 0.002938 0.000001
```
```clike=
reset
set ylabel 'time(sec)'
set style fill solid
set title 'perfomance comparison'
set term png enhanced font 'Verdana,10'
set output 'runtime.png'
plot [:][:0.100]'output.txt' using 2:xtic(1) with histogram title 'original', \
'' using ($0-0.06):($2+0.001):2 with labels title ' ', \
'' using 3:xtic(1) with histogram title 'optimized' , \
'' using 4:xtic(1) with histogram title 'hash' , \
'' using ($0+0.3):($3+0.0015):3 with labels title ' ', \
'' using ($0+0.4):($4+0.0015):4 with labels title ' '
```

e.g
```clike=
reset
set ylabel 'time(sec)'
set style fill solid
set key center top
set title 'perfomance comparison'
set term png enhanced font 'Verdana,10'
set output 'runtime.png'
plot [:][:0.210]'output.txt' using 2:xtic(1) with histogram title 'original', \
'' using 3:xtic(1) with histogram title 'optimized' , \
'' using 4:xtic(1) with histogram title 'hashfunction' , \
'' using 5:xtic(1) with histogram title 'trie' , \
'' using 6:xtic(1) with histogram title 'rbtree' , \
'' using ($0-0.200):(0.110):2 with labels title ' ' textcolor lt 1, \
'' using ($0-0.200):(0.120):3 with labels title ' ' textcolor lt 2, \
'' using ($0-0.200):(0.130):4 with labels title ' ' textcolor lt 3, \
'' using ($0-0.200):(0.140):5 with labels title ' ' textcolor lt 4, \
'' using ($0-0.200):(0.150):6 with labels title ' ' textcolor lt 5
```

## 使用 makefile 實踐
打開 phonebook 的 makefile
```
cache-test: $(EXEC)
perf stat --repeat 100 \
-e cache-misses,cache-references,instructions,cycles \
./phonebook_orig
perf stat --repeat 100 \
-e cache-misses,cache-references,instructions,cycles \
./phonebook_opt
output.txt: cache-test calculate
./calculate
plot: output.txt
gnuplot scripts/runtime.gp
```
發現 output.txt 來自 output.txt,而output.txt 來自 cache-test所以之前如果已經有編譯過了
那就會產生output.txt只用下繪圖指令,不用再重新編譯
只需要下以下指令即可畫圖
`$gnuplot scripts/runtime.gp`(不要用$make plot,這樣會重新再產生一次output.txt耗費時間)
## 增加hash.c hash.h
## 修改calculate.c
## 修改main.c
## 修改runtime.gp
## 修改makefile
phonebook原始版本是兩個圖

makefile檔
```
CC ?= gcc
CFLAGS_common ?= -Wall -std=gnu99
CFLAGS_orig = -O0
CFLAGS_opt = -O0 //CFLAGS_hash = -O0
EXEC = phonebook_orig phonebook_opt //EXEC = 加入phonebook_hash
GIT_HOOKS := .git/hooks/pre-commit
.PHONY: all
all: $(GIT_HOOKS) $(EXEC)
$(GIT_HOOKS):
@scripts/install-git-hooks
@echo
SRCS_common = main.c
phonebook_orig: $(SRCS_common) phonebook_orig.c phonebook_orig.h
$(CC) $(CFLAGS_common) $(CFLAGS_orig) \
-DIMPL="\"$@.h\"" -o $@ \
$(SRCS_common) $@.c
phonebook_opt: $(SRCS_common) phonebook_opt.c phonebook_opt.h
$(CC) $(CFLAGS_common) $(CFLAGS_opt) \
-DIMPL="\"$@.h\"" -o $@ \
$(SRCS_common) $@.c
//phonebook_hash: $(SRCS_common) phonebook_hash.c phonebook_hash.h
$(CC) $(CFLAGS_common) $(CFLAGS_hsah) \
-DIMPL="\"$@.h\"" -o $@ \
$(SRCS_common) $@.c
run: $(EXEC)
echo 3 | sudo tee /proc/sys/vm/drop_caches
watch -d -t "./phonebook_orig && echo 3 | sudo tee /proc/sys/vm/drop_caches"
cache-test: $(EXEC)
perf stat --repeat 100 \
-e cache-misses,cache-references,instructions,cycles \
./phonebook_orig
perf stat --repeat 100 \
-e cache-misses,cache-references,instructions,cycles \
./phonebook_opt
// perf stat --repeat 100 \
-e cache-misses,cache-references,instructions,cycles \
./phonebook_hash
output.txt: cache-test calculate
./calculate
plot: output.txt
gnuplot scripts/runtime.gp
calculate: calculate.c
$(CC) $(CFLAGS_common) $^ -o $@
.PHONY: clean
clean:
$(RM) $(EXEC) *.o perf.* \
calculate orig.txt opt.txt output.txt runtime.png
```
reference
[gnuplot 語法解說和示範](https://hackmd.io/s/Skwp-alOg#gnuplot-%E8%AA%9E%E6%B3%95%E8%A7%A3%E8%AA%AA%E5%92%8C%E7%A4%BA%E7%AF%84)