williamchangTW
    • 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
      • Invitee
      • No invitee
    • Publish Note

      Publish Note

      Everyone on the web can find and read all notes of this public team.
      Once published, notes can be searched and viewed by anyone online.
      See published notes
      Please check the box to agree to the Community Guidelines.
    • 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
    • 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 Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync 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
Invitee
No invitee
Publish Note

Publish Note

Everyone on the web can find and read all notes of this public team.
Once published, notes can be searched and viewed by anyone online.
See published notes
Please check the box to agree to the Community Guidelines.
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
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 2017 q3 Homework1 (作業區) ==contributed by<`williamchangTW`>== ###### tags: `Class_Project`, `Jserv` ### Reviewed by `zhanyangch` * 針對 orig 與 opt 的比較不應只有時間差異,應探討造成此差異的原因,利用 perf 等工具可以協助分析。 * 對 cache 的介紹可以不夠詳盡,可以參考老師提供的 [Computer Architecture](https://hackmd.io/s/H1sZHv4R) [(NOTE)](https://hackmd.io/s/rkloHgHcx) 的 memory 章節。 ### Reviewed by `tina0405` * 縮減 structure 裡的大小後,也可以考慮在其他方面去優化,像資料結構或記憶體的分析方面 * 利用 perf 工具去分析現況 [perf stat 輸出解讀](https://zhengheng.me/2015/11/12/perf-stat/) ## Phonebook ### 建置環境所遇到的問題 - 在這我在使用 perf top 的時候遇到一個問題,花了點時間解決,就是一開始想把權限全打開,預設值是 3 ,我預先執行一段 command line : `gedit /proc/sys/kernel/perf_event_paranoid` 打開後想直接更換為 -1 ,可是出現 `you do not have the permissions necessary to save the file.Please...` 這一段使我不能更改這個檔案,我試過 `sudo su` 及 `vim` 和 `gksudo` 等方法去試著更改其中的值,最後我使用 [sudo sh -c 'echo -1 > /proc/sys/kernel/perf_event_paranoid'](https://superuser.com/questions/980632/run-perf-without-root-rights) ,最後是因為覺得跟 kernel 檔案的關係比較大才找到解決辦法。 - 最後做 `cat /proc/sys/kernel/perf_event_paranoid` 的值確定為 -1 。 ### 還沒有優化前資料 - 再來當我輸入 `perf top` 如下顯示: ![](https://i.imgur.com/lu9u1z9.jpg) - 再來是一開始的 `make plot`:(opt code copy from orig) ![](https://i.imgur.com/Vb8by81.png) - main.c define: ~~~c= #ifdef OPT #define OUT_FILE "opt.txt" #else #define OUT_FILE "orig.txt" #endif ~~~ - 這裡是一種類似於 if ... else 判斷式的定義,依照 #ifdef(+條件定義) 及 #else(+條件定義) #endif結束判斷式。 - 這裡可以看到當有 OPT 這個 headerfile 就定義 **OUT_FILE 生成 "opt.txt" 反之生成 "orig.txt"**。 - 再來看 phonebook_opt.c code,我想從 orig去改 code 所以複製了一份去試試, trace 完後發現一個地方會忘了把 malloc free 。 ~~~c= #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include "phonebook_orig.h" /* original version */ entry *findName(char lastName[], entry *pHead) { while (pHead != NULL) { if (strcasecmp(lastName, pHead->lastName) == 0) return pHead; pHead = pHead->pNext; } return NULL; } entry *append(char lastName[], entry *e) { /* allocate memory for the new entry and put lastName */ e->pNext = (entry *) malloc(sizeof(entry)); e = e->pNext; strcpy(e->lastName, lastName); e->pNext = NULL; return e; } ~~~ - 更改完後 ~~~c= entry *append(char lastName[], entry *e) { e->pNext = (entry *) malloc(sizeof(entry)); e = e->pNext; strcpy(e->lastName, lastName); e->pNext = NULL; free(e->pNext); return e; } ~~~ - 再來我朝 **findName go down** 開始,一開始想說在 while loop 中每次都要做**判斷式**,想減少他的 branch times ,因為 branch 是影響效能比重最重的一個 stage,可是想不出怎麼提出到外面解決,後來想說**減少單次判斷所需等待時間著手**。 - 先看 OPT 這個 headerfile ,於是找出 phonebook_opt.h ~~~c= #ifndef _PHONEBOOK_H #define _PHONEBOOK_H #define MAX_LAST_NAME_SIZE 16 /* TODO: After modifying the original version, uncomment the following * line to set OPT properly */ // #define OPT 1 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; entry *findName(char lastName[], entry *pHead); entry *append(char lastName[], entry *e); #endif ~~~ - 上述**這段冗長的資料要花一點時間去做**,每一次都要花這麼多時間,我決定除了 lastName 留下外其他放到一個新的結構內做需要的時候在搜尋它。其餘不變。 ~~~c= typedef struct __PHONE_BOOK_ENTRY { char lastName[MAX_LAST_NAME_SIZE]; struct __PHONE_BOOK_DET *pDet; struct __PHONE_BOOK_ENTRY *pNext; }entry; typedef struct __PHONE_BOOK_DET { 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]; } det; ~~~ ![](https://i.imgur.com/XqusFqh.png) - 很明顯的 find() 時間變短了,而 append() 小幅下降。 - 把註解掉的 OPT 打開。 ~~~c= #define OPT 1 ~~~ ### cache 詳閱 - 首先是為何要有 cache ,從下面這張圖可以明顯地知道,為了填補 cpu ~ main memory 之間的差異,進而需要階層式的 table 來填補速度上的差異和 reduce cpu idle time。 ![](https://i.imgur.com/qHvuFu9.png) - cache 被設計成一個 hash table 內存數組 index address,簡單概述的說 fully associative 不可能實現,因為這樣需要大量的尋找就損失原本的意義。 - direced mapped 也不好用,前幾個區塊常被用到,後面幾個區塊幾乎不用,浪費空間,使用率可能會很差。 - N-way associative & (LRU or Random) implemented. - 把最近常用到的資料放在越靠近 CPU 的 memory 作儲存,這樣在抓取資料時 responds time 較短較有效率。

Import from clipboard

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 is not available.
Upgrade
All
  • All
  • Team
No template found.

Create custom 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

How to use Slide mode

API Docs

Edit in VSCode

Install browser extension

Get in Touch

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
Upgrade to Prime Plan

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

No updates to save
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

      Upgrade

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Upgrade

      Danger Zone

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

      Syncing

      Push failed

      Push successfully