Wei-Yu Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 2016q3 Homework1 (phonebook) contributed by <`aweimeow`> ###### tags: `sysprog21` `aweimeow` ### Reviewed by `HuangWenChen` * 這裡只有使用一個 hash function 作效能分析,可以嘗試使用不同 hash function 去做各個 hash function 效能比較 * 可以利用字串壓縮法將名子長度縮小 * github 尚未更新 ### 作業環境 * OS: Ubuntu 14.04.4 LTS * CPU: Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz * Memory: 8G * Cache: * L1d cache: 32KB * L1i cache: 32KB * L2 cache: 256KB * L3 cache: 3072KB ### 安裝前置作業 這邊看到 [@louielu](https://hackmd.io/CYJgZgHA7AnADAFgLQGMCmaCGSEGYCsyMARjPkgIwhS4gT4VwogJA===#) 用了 `lstopo` 這個工具覺得很酷所以也想安裝一下,這邊紀錄一下安裝的流程: #### Install lstopo ```bash wget https://www.open-mpi.org/software/hwloc/v1.11/downloads/hwloc-1.11.4.tar.gz tar zxvf hwloc-1.11.4.tar.gz cd hwloc-1.11.4 cat README # 不過他的 README 沒有說明要怎麼安裝 Orz ./configure make sudo make install ``` BUT ... ! :::danger libhwloc.so.5: cannot open shared object file: No such file or directory ::: 後來找了一下發現 `libhwloc.so.5` 在 `/usr/local/lib` 裡面,所以我手動修改了 `/usr/local/lib`,再執行 `ldconfig` 輸出: ``` Machine (7879MB) Package L#0 + L3 L#0 (3072KB) L2 L#0 (256KB) + L1d L#0 (32KB) + L1i L#0 (32KB) + Core L#0 PU L#0 (P#0) PU L#1 (P#1) L2 L#1 (256KB) + L1d L#1 (32KB) + L1i L#1 (32KB) + Core L#1 PU L#2 (P#2) PU L#3 (P#3) HostBridge L#0 PCIBridge PCI 10de:1341 PCI 8086:0416 GPU L#0 "card0" GPU L#1 "renderD128" GPU L#2 "controlD64" PCIBridge PCI 10ec:8168 Net L#3 "eth0" PCIBridge PCI 168c:0036 Net L#4 "wlan0" PCI 8086:8c03 Block(Removable Media Device) L#5 "sr0" Block(Disk) L#6 "sda" ``` #### Install perf 根據 [wiki](http://wiki.csie.ncku.edu.tw/embedded/perf-tutorial) 上面的說明,我的操作如下,過程沒有碰到任何問題: ``` $ sudo apt-get install linux-tools-common $ sudo apt-get install linux-tools-4.2.0-27-generic linux-cloud-tools-4.2.0-27-generic ``` perf stat 存取局域性結果差異: ``` $ perf stat --repeat 5 -e cache-misses,cache-references,instructions,cycles ./test Performance counter stats for './test' (5 runs): 2,614,830 cache-misses # 2.525 % of all cache refs 100,415,393 cache-references 2,006,482,671 instructions # 0.81 insns per cycle 2,409,393,663 cycles 0.803709786 seconds time elapsed ( +- 14.38% ) ``` ``` $ perf stat --repeat 5 -e cache-misses,cache-references,instructions,cycles ./test Performance counter stats for './test' (5 runs): 217,059 cache-misses # 63.407 % of all cache refs 347,854 cache-references 2,005,971,913 instructions # 1.93 insns per cycle 1,032,907,799 cycles 0.334975609 seconds time elapsed ( +- 0.97% ) ``` #### Install gnu plot ``` $ apt-get install gnuplot ``` #### Install astyle ``` $ apt-get install astyle ``` ### 進入主題: phonebook 在我的電腦上,未修改的程式碼的 cache-miss 為 81.418% ``` Performance counter stats for './phonebook_orig' (100 runs): 1,154,821 cache-misses # 81.418 % of all cache refs 1,389,198 cache-references 263,656,721 instructions # 1.06 insns per cycle 240,380,961 cycles 0.081547701 seconds time elapsed ( +- 0.82% ) ``` 以下先試試使用改變資料結構來改善效能 #### 改變資料結構です 我們先從 `phonebook_opt.h` 來開始下手,讓我們先思考一個問題:在要查找電話簿時, key 應該是什麼?想當然是名字,在翻找電話簿時,找到名字後,跟隨在其後的地址、信箱、電話號碼 ... 才會需要被看到。 因此,來修改一下程式碼,將 `firstName`, `email`, `phone`, ... 這些資料先藏起來放到另一個資料結構中,我把它取名為 `detail`,搜尋到時可以存取這一筆 entry 裡面的細節(信箱、電話、...) ```c typedef struct __PHONE_BOOK_ENTRY_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]; } detail; typedef struct __PHONE_BOOK_ENTRY { char lastName[MAX_LAST_NAME_SIZE]; struct detail *detail; struct __PHONE_BOOK_ENTRY *pNext; } entry; ``` 這樣子改善之後,我們的 `cache-miss` 會下降多少呢? ``` Performance counter stats for './phonebook_opt' (100 runs): 158,618 cache-misses # 38.030 % of all cache refs 437,759 cache-references 245,272,988 instructions # 1.52 insns per cycle 163,283,836 cycles 0.053033708 seconds time elapsed ( +- 1.06% ) ``` 從輸出當中我們可以觀察到 `cache-miss` 從 ==81.418%== 下降到 ==38.030%== 下圖是這個階段執行完的 function 時間比較: ![compare for modify data structure](http://imgur.com/BwQlPmx.png) 可以從圖中發現,時間也有明顯的下降。 #### 使用 Hash Function Hash Function 選用 [djb2](http://stackoverflow.com/questions/7666509/hash-function-for-string) > 不過真的很久沒有碰過 C 語言了,改改 header file 還可以,要重拾三年前的 C 語言讓我碰到滿滿的挫折(或者說以前根本沒好好學 C) ##### 撰寫時碰到的問題們(以下會以我的理解盡力解釋問題,如果有誤請務必指正我感謝 QQ): * Pointer 的觀念 ~~略懂略懂~~ 完全不懂 QQ >> 無法用 C 語言開發出 C compiler & UNIX 等級的軟體,就是不懂 C 語言,沒有「略懂」這回事。 [name=jserv] ``` entry *people; strcpy(people->lastName, lastName); ``` 然後就 `Segment Fault` 了,後來查到[問題](http://stackoverflow.com/questions/6447744/segmentation-fault-around-strcpy)才發現原來我這樣子作是不對的。 在這邊都是指標,指標儲存的是一個記憶體位址,所以我直接把 `lastName` copy 到 `people->lastName` 就相當於:以 `lastName` 為 'AAAA' 來說,就是把 0x41414141 寫到 `people->lastName`,所以這個 pointer 就會指到這個奇怪的地址去,所以就錯了。 *

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    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.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully