contributed by <nekoneko
>
sys2016
nekoneko
homework
ChenYi
git push
$ git remote add origin git@github.com:your_account/your_repository.git
其實以前都有設定過,只是這次重灌,重新設定之外,做點紀錄
astyle --style=kr --indent=spaces=4 --indent-switches --suffix=none *.[ch]
照著Linux 效能分析工具: Perf
資料的perf_top_example.c
來執行perf top,但是沒有跑出預期結果
$ sudo sh -c " echo -1 > /proc/sys/kernel/perf_event_paranoid"
$ sudo sh -c " echo 0 > /proc/sys/kernel/kptr_restrict"
g++ -c perf_top_example.c
g++ perf_top_example.o -o example
./example
sudo perf top -p $pid
sudo perf top
顯示也還是如上圖,但是下-e
參數的話,是會有資訊的。應該是文件沒看完,看熟文件後,來想辦法找出一點頭緒cheng hung lin
cycles
event參考文件中,提到預設的perfomance event為cycles,然而從上圖來看,預設卻是cycles:pp
。接著執行perf list
來查閱有cycles關鍵字的event。
實際上,在我的筆電上是沒有cycles:pp
的event。所以改成以下就可看到我們要的cycles
event
$ sudo perf top -e cycles -p $pid
「程式輸出的文字訊息」請勿使用圖片表示,請改由文字貼上課程助教
好的,之後不會再使用圖片,謝謝提醒cheng hung lin
可以用perf --stdio
來作為文字輸出 louielu
for (int i = 0; i < max; i++) if (<condition>) sum++;
之後有時間再回來看cheng hung lin
複習白算盤 4e 5.2 章節
書中提到cache miss對於效能的影響
The processing of a cache miss create a pipeline stall as opposed to an interrupt, which would require saving the state of registers
cahe miss on write
The modified block is written to the lower level of the hierarchy when it is replaced.
)$ echo 1 | sudo tee /proc/sys/vm/drop_caches
$ sudo perf stat -r 10 -e cache-misses,cache-references,L1-dcache-load-misses,L1-dcache-store-misses,L1-dcache-prefetch-misses,L1-icache-load-misses,instructions,cycles ./phonebook_orig
觀察的事件有
結果
size of entry : 136 bytes
execution time of append() : 0.085346 sec
execution time of findName() : 0.011937 sec
Performance counter stats for './phonebook_orig' (10 runs):
988,062 cache-misses # 93.309 % of all cache refs ( +- 0.87% ) (61.95%)
1,058,908 cache-references ( +- 0.63% ) (61.94%)
2,644,837 L1-dcache-load-misses ( +- 0.56% ) (61.94%)
877,813 L1-dcache-store-misses ( +- 1.34% ) (50.10%)
1,294,117 L1-dcache-prefetch-misses ( +- 1.38% ) (52.38%)
526,219 L1-icache-load-misses ( +- 5.38% ) (54.38%)
260,342,568 instructions # 1.00 insns per cycle ( +- 0.36% ) (65.70%)
260,840,687 cycles ( +- 0.61% ) (63.41%)
0.129547167 seconds time elapsed ( +- 2.20% )
$ perf record -F 12500 -e cache-misses ./phonebook_orig && perf report
0.05% phonebook_orig phonebook_orig [.] append
append在cache miss的測試上,並沒有每次都有紀錄。cheng hung lin
原始struct大小為136Byte,findName 和 append需要只有last name的資訊。
/* original version */
typedef struct __PHONE_BOOK_ENTRY {
char lastName[MAX_LAST_NAME_SIZE];
char firstName[16];
char email[16];
char phone[10];
char cell[10];
char addr1[16];
char addr2[16];
char city[16];
char state[2];
char zip[5];
struct __PHONE_BOOK_ENTRY *pNext;
} entry;
#define OPT 1
typedef struct __DETAIL_ENTRY {
char firstName[16];
char email[16];
char phone[10];
char cell[10];
char addr1[16];
char addr2[16];
char city[16];
char state[2];
char zip[5];
} detailEntry;
typedef struct __PHONE_BOOK_ENTRY {
char lastName[MAX_LAST_NAME_SIZE];
detailEntry *detail;
/*
char firstName[16];
char email[16];
char phone[10];
char cell[10];
char addr1[16];
char addr2[16];
char city[16];
char state[2];
char zip[5];
*/
struct __PHONE_BOOK_ENTRY *pNext;
} entry;
#define OPT 1
註解拿掉,回到main.c裡可以看到#if defined(OPT)
output = fopen("opt.txt", "a");
#else
defined
說明
perf stat 結果
size of entry : 32 bytes
execution time of append() : 0.103262 sec
execution time of findName() : 0.016726 sec
Performance counter stats for './phonebook_opt' (10 runs):
1,669,477 cache-misses # 94.412 % of all cache refs ( +- 0.44% ) (62.14%)
1,768,295 cache-references ( +- 0.45% ) (62.22%)
2,823,014 L1-dcache-load-misses ( +- 0.47% ) (62.07%)
1,058,923 L1-dcache-store-misses ( +- 0.87% ) (50.66%)
1,672,812 L1-dcache-prefetch-misses ( +- 2.14% ) (53.15%)
559,604 L1-icache-load-misses ( +- 7.74% ) (52.70%)
330,247,777 instructions # 1.00 insns per cycle ( +- 0.63% ) (64.64%)
328,716,232 cycles ( +- 0.41% ) (62.74%)
0.161549694 seconds time elapsed ( +- 1.50% )
如同參考文件寫的,append的時候還有malloc detailEntry的話,cache miss不降反生,從93.309%升到94.412 %,但是這裡可以看出來findName的cache miss比原先版本的下降,從10.3%到3.03%。
那94.412%的整體cache miss是給detail配置記憶體空間造成的,但要從何才能觀察到呢?cheng hung lin
原本編譯參數自己有加上-g
,忘記刪除,所以上面數據重新更改為去除-g
flagscheng hung linMon, Sep 26, 2016 8:27 PM
detail pointer不配置記憶體而先初始成NULL
size of entry : 32 bytes
execution time of append() : 0.072660 sec
execution time of findName() : 0.004983 sec
Performance counter stats for './phonebook_opt' (10 runs):
349,355 cache-misses # 78.215 % of all cache refs ( +- 0.77% ) (59.17%)
446,659 cache-references ( +- 1.63% ) (59.07%)
954,744 L1-dcache-load-misses ( +- 3.86% ) (60.24%)
295,731 L1-dcache-store-misses ( +- 3.02% ) (53.95%)
1,506,670 L1-dcache-prefetch-misses ( +- 5.08% ) (57.94%)
272,066 L1-icache-load-misses ( +- 4.59% ) (55.98%)
261,005,283 instructions # 1.41 insns per cycle ( +- 0.98% ) (66.21%)
185,111,487 cycles ( +- 0.69% ) (61.76%)
0.102715691 seconds time elapsed ( +- 12.42% )
hash
當初資料結構沒學好,從原本的教科書(Fundamentals of data structures in c)和網路資料看起。
實做
參考了Yuron大大和ayueh0822大大的共筆,初步了解hashing的過程,分別是 建立hash table、hash function把key轉成buckect address、將資料存進bucket address。因為在插入資料到hash table時,有可能會有overflow和collision的狀況,從ayueh0822大大共筆的圖來看,每個buckect 的slot用linked list來存可以避免這樣個狀況。
overflow和collision其實還是沒有很懂,等實做完後再來查資料。
overflow and collision
hash table structure
#define TWO_POWER_NUM 8
#define MAX_HASH_TABLE_SIZE 1 << TWO_POWER_NUM
typedef struct __HASH_SLOT {
entry *data;
struct __HASH_SLOT *pNext;
} slot_unit;
typedef struct __HASH_BUCKET {
slot_unit *slot;
} hash_bucket;
hash_bucket hashTable[MAX_HASH_TABLE_SIZE];
再精簡一些
#define TWO_POWER_NUM 8
#define MAX_HASH_TABLE_SIZE 1 << TWO_POWER_NUM
typedef struct __HASH_SLOT {
entry *data;
struct __HASH_SLOT *pNext;
} slot_unit;
slot_unit hashTable[MAX_HASH_TABLE_SIZE];
unsigned int stringToInt(char *key)
{
int number = 0;
while (*key)
number += *key++;
return number;
}
unsigned int hashFunction(unsigned int key)
{
return key & ((1<<TWO_POWER_NUM)-1);
}
[ ]key & ((1<<TWO_POWER_NUM)-1)
是否作到預期結果
[ ]是否有比key % 256
來的快
findName
entry *findName(char lastname[], entry *pHead)
{
unsigned int hashPos;
slot_unit *slot;
hashPos = hashFunction(stringToInt(lastname));
slot = hashTable[hashPos];
while (slot!=NULL) {
if (strcasecmp(lastname, slot->data->lastName) == 0)
return slot->data;
slot = slot->pNext;
}
return NULL;
}
/* allocate memory for the new entry and put lastName */
e->pNext = (entry *) malloc(sizeof(entry));
e->detail = NULL;
e = e->pNext;
strcpy(e->lastName, lastName);
e->pNext = NULL;
unsigned int hashPos;
slot_unit *newSlot;
hashPos = hashFunction(stringToInt(lastName));
newSlot = (slot_unit *) malloc(sizeof(slot_unit));
newSlot->pNext = hashTable[hashPos];
newSlot->data = e;
hashTable[hashPos] = newSlot;
#if defined(HASH)
unsigned int __i;
for (__i=0;__i < MAX_HASH_TABLE_SIZE;++__i)
hashTable[__i] = NULL;
#endif
這個版本的程式碼,無法跑出結果,應該是卡在某個loop中,來debugcheng hung linTue, Sep 27, 2016 3:09 AM
stringToInt
函式第五行number += *key;
,改成*key++
cheng hung linTue, Sep 27, 2016 3:28 AM
使用hash table的資料結構,代表說原本的linked list可以不需要用到了,目前的版本保留原本的linked list是多餘的,等bug解完再修cheng hung lin
size of entry : 32 bytes
execution time of append() : 0.103418 sec
execution time of findName() : 0.000000 sec
Performance counter stats for './phonebook_hash' (10 runs):
484,838 cache-misses # 89.380 % of all cache refs ( +- 1.35% ) (59.20%)
542,444 cache-references ( +- 1.22% ) (63.18%)
781,892 L1-dcache-load-misses ( +- 2.35% ) (65.95%)
491,304 L1-dcache-store-misses ( +- 0.47% ) (55.71%)
68,136 L1-dcache-prefetch-misses ( +- 12.38% ) (53.77%)
380,728 L1-icache-load-misses ( +- 5.78% ) (50.07%)
304,166,446 instructions # 1.36 insns per cycle ( +- 0.37% ) (61.18%)
224,161,591 cycles ( +- 0.74% ) (57.94%)
0.111725998 seconds time elapsed ( +- 2.45% )
Samples: 1K of event 'cache-misses', Event count (approx.): 515758
Overhead Command Shared Object Symbol ▒
64.32% phonebook_hash [kernel.kallsyms] [k] clear_page ▒
8.63% phonebook_hash [kernel.kallsyms] [k] pte_lockptr.isra.13 ▒
3.73% phonebook_hash [kernel.kallsyms] [k] try_charge ▒
3.48% phonebook_hash [kernel.kallsyms] [k] copy_user_generic_string ▒
2.74% phonebook_hash [kernel.kallsyms] [k] unmap_page_range ▒
2.62% phonebook_hash [kernel.kallsyms] [k] __rmqueue.isra.79 ▒
2.48% phonebook_hash [kernel.kallsyms] [k] get_page_from_freelist ▒
1.74% phonebook_hash [kernel.kallsyms] [k] get_mem_cgroup_from_mm ▒
1.57% phonebook_hash [kernel.kallsyms] [k] free_pcppages_bulk ◆
1.29% phonebook_hash [kernel.kallsyms] [k] handle_mm_fault ▒
0.98% phonebook_hash [kernel.kallsyms] [k] __alloc_pages_nodemask ▒
0.94% phonebook_hash [kernel.kallsyms] [k] mem_cgroup_try_charge ▒
64.32%, 8.63%都為紅字,其餘為綠字cheng hung lin
size of entry : 32 bytes
execution time of append() : 0.109029 sec
execution time of findName() : 0.000000 sec
Performance counter stats for './phonebook_hash' (10 runs):
877,411 cache-misses # 87.872 % of all cache refs ( +- 1.44% ) (61.75%)
998,513 cache-references ( +- 1.43% ) (63.25%)
1,406,025 L1-dcache-load-misses ( +- 1.37% ) (64.22%)
536,177 L1-dcache-store-misses ( +- 1.04% ) (52.47%)
71,249 L1-dcache-prefetch-misses ( +- 22.30% ) (51.38%)
603,052 L1-icache-load-misses ( +- 4.61% ) (49.54%)
321,729,410 instructions # 0.93 insns per cycle ( +- 2.92% ) (62.35%)
347,147,596 cycles ( +- 0.76% ) (60.77%)
0.169511003 seconds time elapsed ( +- 0.80% )
entry *append(char lastName[], entry *e)
{
/* allocate memory for the new entry and put lastName */
unsigned int hashPos;
hashPos = hashFunction(stringToInt(lastName));
e = (entry *) malloc(sizeof(entry));
e->pNext = hashTable[hashPos];
strcpy(e->lastName, lastName);
e->detail = NULL;
hashTable[hashPos] = e;
return e;
}
size of entry : 32 bytes
execution time of append() : 0.087568 sec
execution time of findName() : 0.000000 sec
Performance counter stats for './phonebook_hash' (10 runs):
692,774 cache-misses # 87.907 % of all cache refs ( +- 0.90% ) (59.08%)
788,076 cache-references ( +- 1.63% ) (61.45%)
1,110,403 L1-dcache-load-misses ( +- 1.20% ) (63.80%)
300,690 L1-dcache-store-misses ( +- 2.65% ) (54.17%)
79,456 L1-dcache-prefetch-misses ( +- 4.96% ) (53.95%)
448,250 L1-icache-load-misses ( +- 5.02% ) (51.45%)
295,086,356 instructions # 0.98 insns per cycle ( +- 1.58% ) (62.71%)
299,696,349 cycles ( +- 0.56% ) (59.94%)
0.148717215 seconds time elapsed ( +- 3.20% )
unsigned int hash(hash_table *hashtable , char *str)
{
unsigned int hash_value = 0;
while (*str)
hash_value = (hash_value << 5) - hash_value + (*str++);
return (hash_value % MAX_HASH_TABLE_SIZE);
}
size of entry : 32 bytes
execution time of append() : 0.093066 sec
execution time of findName() : 0.000010 sec
Performance counter stats for './phonebook_hash' (10 runs):
695,569 cache-misses # 61.825 % of all cache refs ( +- 0.40% ) (60.64%)
1,125,064 cache-references ( +- 0.35% ) (60.66%)
1,524,817 L1-dcache-load-misses ( +- 0.77% ) (61.47%)
321,656 L1-dcache-store-misses ( +- 0.73% ) (52.48%)
48,764 L1-dcache-prefetch-misses ( +- 1.28% ) (54.86%)
567,795 L1-icache-load-misses ( +- 3.91% ) (53.19%)
281,766,203 instructions # 0.89 insns per cycle ( +- 1.17% ) (64.34%)
316,135,640 cycles ( +- 0.63% ) (61.96%)
0.159598584 seconds time elapsed ( +- 3.83% )
48.91% phonebook_hash1 phonebook_hash1 [.] main
30.85% phonebook_hash1 [kernel.kallsyms] [k] clear_page
3.48% phonebook_hash1 [kernel.kallsyms] [k] pte_lockptr.isra.13
2.60% phonebook_hash1 [kernel.kallsyms] [k] copy_user_generic_string
1.65% phonebook_hash1 [kernel.kallsyms] [k] try_charge
1.49% phonebook_hash1 phonebook_hash1 [.] append
1.37% phonebook_hash1 [kernel.kallsyms] [k] unmap_page_range
1.30% phonebook_hash1 [kernel.kallsyms] [k] get_page_from_freelist
1.17% phonebook_hash1 [kernel.kallsyms] [k] __rmqueue.isra.79
0.91% phonebook_hash1 [kernel.kallsyms] [k] get_mem_cgroup_from_mm
0.80% phonebook_hash1 [kernel.kallsyms] [k] mem_cgroup_try_charge
0.75% phonebook_hash1 [kernel.kallsyms] [k] free_pcppages_bulk
0.51% phonebook_hash1 [kernel.kallsyms] [k] handle_mm_fault
cache miss rate 下降,但是cache miss沒有減少,反而是cache-references增加
為了測試slot
,phonebook_hash,phonebook_hash1,都會在讀取整個hash table一遍,試著將以下程式碼註解
#if 0
FILE *__fp;
entry *__entry;
unsigned int __count;
__fp = fopen("hash_slots.txt", "w");
for (__i=0; __i<MAX_HASH_TABLE_SIZE; ++__i) {
__count = 0;
__entry = hashTable[__i];
while (__entry) {
++__count;
__entry = __entry->pNext;
}
fprintf(__fp, "%d %d\n", __i, __count);
}
fclose(__fp);
#endif
size of entry : 32 bytes
execution time of append() : 0.087941 sec
execution time of findName() : 0.000000 sec
Performance counter stats for './phonebook_hash1' (10 runs):
337,303 cache-misses # 53.906 % of all cache refs ( +- 0.37% ) (64.80%)
625,727 cache-references ( +- 1.56% ) (64.38%)
912,984 L1-dcache-load-misses ( +- 1.76% ) (65.78%)
313,882 L1-dcache-store-misses ( +- 0.70% ) (69.79%)
62,026 L1-dcache-prefetch-misses ( +- 5.81% ) (70.86%)
290,789 L1-icache-load-misses ( +- 6.16% ) (67.31%)
0.105764890 seconds time elapsed ( +- 13.96% )
延伸閱讀: Intel Ivy Bridge Cache Replacement Policy
這可見 Intel i3, i5, i7 之間 cache 設計的落差 jserv
Generating Perfect Hash Functions
Perfect Hash Function Generator