Ethan Cheng
    • 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
    • Make a copy
    • 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 Make a copy 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
    # Operating System Project 2 Report ## 設計 `master.c / slave.c`: 我們參考了 [這裡](https://github.com/paraka/mmap-kernel-transfer-data/blob/master/mmap-example.c) 和 [這裡](https://linux-kernel-labs.github.io/refs/heads/master/labs/memory_mapping.html) ,在 master 和 slave 的 device driver 裡新增了 mmap 的 code。如此一來,我們可以利用 mmap 將 kernel 的記憶體映設至 user program 的 memory。之後,再將我們從 disk load 進來的檔案搬到 memory 對到 kernel 裡的那塊,達到資料的移動。 另外,我們在其中遇到的困難之一是每次僅能讀少量 byte 就發現要不到記憶體做 mapping 了,經過研究之後才想到必須要在 mmap 完後進行 `munmap` 才能釋出記憶體,如此一來,將傳輸能量從少量的 bytes 提升到 1GB 之多。 至於 `slave.c`,大致和 `master.c` 相同,只是身分從要寫資料進去的角色,變成讀資料的接收者。 `master_device.c / slave_device.c`: 這兩個檔案所使用的程式碼非常相似,因此將兩個檔案一起描述,並著重在`master_device.c`,但`slave_device.c`大同小異。為了支援`master.c`裡的 mmap,我們在 master device 被 open 時就用 `kmalloc` 來要一塊作為 mmap 用途的 buffer。另外一個重要的因素是memory需要對齊才能正常執行,因此我們allocate memory時,都使用page size的倍數。 而在這部分,我們一開始觀察`dmesg`時會發現slave會不斷讀取到重複且不完整的片段。我們的解決方式是在`kreceive`的函式呼叫裡加上`MSG_WAITALL`的參數。這個參數會使得socket等待所有單次資料都傳輸完畢之後才會進行後續的操作,而不會在master的資料還沒傳送完畢之前就斷線。 ```Testing``` 為了測試不同page size/fcntl buffer size/mmap size與效能的關係時時,我們常常遇到程式crash的情況。經過觀察後我們發現sample code的 buffer 是開在 process memory 的 stack 區塊,所以當 buffer 一大,就會發生 stackoverflow的狀況。解決的方法是把變數放在 static 或是 global 區塊。 和 spec 比較不同的是,為了增加測試的效率,我們在 `user_program/master.c` 和 `user_program/slave.c` 裡面,我們使用 stdin 的方式來讀進參數,而不是透過 command line argument 來讀。 ## Bonus 我們發現在 ## Demo 在 demo 的部分,我們對 3 種不同的 input 進行了測試,前 2 種爲助教提供的 sample input,分別代表多個小檔和單一大檔,而第 3 種爲我們自己設計的「多個大檔」(5個100MB大小的檔案),我們認爲多個大檔比起單一大檔更能公平地去衡量不同傳輸方法的差異,避免因爲系統狀況的不穩定而產生有 bias 的結果。 下圖為 master device 與 slave device 的 page descriptor ![](https://i.imgur.com/pJrgliO.png) ## 比較 read/write 和 mmap+memcpy 以下圖表為master與slave皆使用mmap與read/write,在不同file size時的表現: ![](https://i.imgur.com/StThdQN.png) 其中橫軸的單位為bytes。 從圖片中我們可以觀察到,mmap的表現普遍優於read/write,而且時間的差距隨著檔案的大小變大也更加明顯,應證了理論上mmap較優的表現。 ---- 下圖為使用mmap時,使用不同的mmap size(即單次傳輸的檔案大小)的表現。測試用的檔案大小為400MB。 ![](https://i.imgur.com/Jv4PbdE.png) 其中橫軸的單位為page size的倍數,例如20即代表mmap size = $20 \times$ page size。 可以觀察到隨著使用的mmap size增加,所需的時間也較少。原因是單次的傳輸量越高,所需要呼叫的mmap次數也降低,而單次傳輸量即使較高,在記憶體空間足夠的情況下,速度仍然是快速的。 ---- 下圖為master與slave分別使用不同的IO方式得出的時間表。格式$(X+Y)$代表master使用$X$,slave使用$Y$。 ![](https://i.imgur.com/GNho8cV.png) 其中橫軸的單位為bytes,縱軸的單位ms。 圖中可以觀察到當兩邊都使用mmap時會得到最好的表現。而當兩方都使用fcntl時,在file size較大會有最差的表現。 另外我們認為slave的部分對整體表現的影響是比較大的。從圖中紅線與綠線可以觀察到,同樣是一方使用fcntl,一方使用mmap,但由slave使用mmap時表現會較優。 ## sample code 所做之修改 1. 裡面有很多的 `ioctl` 寫成了 `ioclt` 2. 以下這行 code 的 0.0001 應該要改成 0.001 ```cpp=72 trans_time = (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)*0.0001; ``` 3. 以下這行 code 的 sizeof(msg) 應該要改成 count,這樣的程式邏輯才是 read 函數的程式邏輯。 ```cpp=198 len = krecv(sockfd_cli, msg, sizeof(msg), 0); ``` 4. 另外,最後在計算 transmission time 的時候,計算 byte 時應該不需要除以 8,會變成計算傳輸的 word 數。 ## 分工表 |學號 姓名|工作|分工占比| |-|-|-| |B07902028 林鶴哲|協助coding,並進行許多測試與debug,以及report的撰寫|16.66%| |B07902064 蔡銘軒|修改原程式碼的錯誤,執行測試,寫report與製圖|16.66%| |B07902075 林楷恩|撰寫基礎程式碼,寫報告|16.7%| |B07902084 鄭益昀|看 sample code,整體開發上的幫助,看一些 documentation 與對 mmap 的 debug|16.66%| |B07902123 蔡奇夆|看 sample code,幫忙 kmalloc、kfree、receive_msg 的 debug,寫報告|16.66%| |B07902136 楊子平|看 sample code,幫忙 mmap 的 debug、測試執行|16.66%| ## 參考資料 * device driver 內的 mmap 實作: https://linux-kernel-labs.github.io/refs/heads/master/labs/memory_mapping.html https://github.com/paraka/mmap-kernel-transfer-data/blob/master/mmap-example.c `vma->vm_flags |= VM_LOCKED;` 來自於 B07902133。

    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