grant Lin
    • 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
    # 2019q1 Homework7 (ringbuffer) contributed by < `grant7163` > ###### tags: `sysprog2019_q1` ## 作業要求 依據 [F11: ringbuffer](https://hackmd.io/s/SkYLI9CiN) * 完成 [第 11 週測驗題 (上)](https://hackmd.io/s/HypUB7HjV) 和所有延伸題目 * 在 Linux 核心原始程式碼指出類似的 ring buffer 實作,設計 Linux 核心模組的實驗,需要探討對應的原理 * 需要涵蓋 kernel API 同步機制的運用 * 執行時期的分析 (提示: 可善用 eBPF) ## 測驗 1 首先判斷 cirbuf 是否已滿,若還沒滿則將 data 複製到 cirbuf tail 中並更新 tail 的位置,最後在檢查 tail 是否有超出 cirbuf 的記憶體區段,若超出 cirbuf 的記憶體區段則將 tail 取餘數(tail % cirbuf size)。 ```clike= static inline int cirbuf_offer(cirbuf_t *cb, const unsigned char *data, const int size) { /* prevent buffer from getting completely full or over commited */ if (cirbuf_unusedspace(cb) <= size) return 0; int written = cirbuf_unusedspace(cb); written = size < written ? size : written; memcpy(cb->data + cb->tail, data, written); cb->tail += written; MM1 = if (cb->size < cb->tail) cb->tail %= cb->size; return written; } ``` 首先判斷 cirbuf 是否為空,若不為空則將 cirbuf head 中的資料取出並更新 head 的位置。 ```clike= static inline unsigned char *cirbuf_peek(const cirbuf_t *cb) { if (cirbuf_is_empty(cb)) return NULL; MM2 = return cb->data + cb->head; } ``` ps : 藍色圈圈為 head, 橘色圈圈為 tail ![](https://i.imgur.com/8jpDGgN.png) 查閱 linux man page 了解相關說明。 `$ man mkstemp` > The function generates a unique temporary filename from template, creates and opens the file, and returns an open file descriptor for the file. > ==The last six characters of template must be "XXXXXX" and these are replaced with a string that makes the filename unique.== Since it will be modified, ==template must not be a string constant, but should be declared as a character array.== `$ man 2 unlink` > unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available for reuse. > If the name was the last link to a file but any processes still have the file open, the file will remain in existence until the last file descriptor referring to it is closed. > If the name referred to a symbolic link, the link is removed. `$ man 2 ftruncate` > If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0'). > If the size changed, then the st_ctime and st_mtime fields (respectively, time of last status change and time of last modification; see inode(7)) for the file are updated, and the set-user-ID and set-group-ID mode bits may be cleared. ```clike= static void create_buffer_mirror(cirbuf_t *cb) { char path[] = "/tmp/cirbuf-XXXXXX"; int fd = mkstemp(path); unlink(path); ftruncate(fd, cb->size); /* FIXME: validate if mkstemp, unlink, ftruncate failed */ /* create the array of data */ cb->data = mmap(NULL, cb->size << 1, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); /* FIXME: validate if cb->data != MAP_FAILED */ void *address = mmap(cb->data, cb->size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd, 0); /* FIXME: validate if address == cb->data */ address = mmap(cb->data + cb->size, cb->size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd, 0); /* FIXME: validate if address == cb->data + cb->size */ close(fd); } ``` ## 延伸問題 - [ ] 解析 test suite 運作原理,嘗試強化現有 cirbuf 的例外處理機制,並且實作於 unit test 內部 #### test suite 運作原理 在 cirbuf 目錄下輸入 make 後會自動執行測試程式並將訊息顯示在終端機上,可以看到會額外產生一個 main.c,不難發現這就是剛剛在執行的測試程式。 ```shell $ make ... ./driver running TestCirbuf_set_size_with_init running TestCirbuf_is_empty_after_init running TestCirbuf_is_not_empty_after_offer running TestCirbuf_is_empty_after_poll_release running TestCirbuf_spaceused_is_zero_after_poll_release running TestCirbuf_cant_offer_if_not_enough_space running TestCirbuf_cant_offer_if_buffer_will_be_completely_full running TestCirbuf_offer_and_poll running TestCirbuf_cant_poll_nonexistant running TestCirbuf_cant_poll_twice_when_released running TestCirbuf_independant_of_each_other running TestCirbuf_independant_of_each_other_with_no_polling OK (12 tests) ``` 從 makefile 中觀察 main.c 是透過一個 shell script 的腳本(`gentest.sh`) 產生的。 ```shell main.c: bash tests/gentest.sh tests/test-*.c > $@ driver: main.c tests/test-cirbuf.c tests/unit-test.c main.c $(CC) $(CFLAGS) -o $@ $^ ./$@ ``` 將檔案指定為 command line 的第一個 arguments(在此為 test-cirbuf.c),接著使用 sed 命令編輯文字。 先將 test-cirbuf.c 中所有開頭為 void Test 的字串列出來,接著將 ( 之後的字串替換為 `(CuTest*);` 並將字串開頭替換為 extern。 * `sed -e 's/^void //'` : 將 void 取代為 空白 參考 [3.3 The s Command](http://www.gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command) * `*` : 匹配前面的(左鄰)字元有 0 個以上 * `.` : 匹配任何字元 * `^` : 表達式的開頭 * `$` : 表達式的結尾 參考 [5.3 Overview of basic regular expression syntax](http://www.gnu.org/software/sed/manual/sed.html#BRE-syntax) ```shell FILES=$1 ... cat $FILES | grep '^void Test' | sed -e 's/(.*$//' \ -e 's/$/(CuTest*);/' \ -e 's/^/extern /' ... cat $FILES | grep '^void Test' | sed -e 's/^void //' \ -e 's/(.*$//' \ -e 's/^/ SUITE_ADD_TEST(suite, /' \ -e 's/$/);/' ... ``` 在 main.c 中,建立一個 suite 來管理整個測試資料,透過 SUITE_ADD_TEST 這樣的 macro 將要測試的函式加入到 suite 中的 list。 ```c #define SUITE_ADD_TEST(SUITE, TEST) CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST)) typedef struct { int count; CuTest *list[MAX_TEST_CASES]; int failCount; } CuSuite; ... void RunAllTests(void) { CuString *output = CuStringNew(); CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, TestCirbuf_set_size_with_init); SUITE_ADD_TEST(suite, TestCirbuf_is_empty_after_init); SUITE_ADD_TEST(suite, TestCirbuf_is_not_empty_after_offer); ... CuSuiteRun(suite); CuSuiteDetails(suite, output); printf("%s\n", CuStringC(output)); } int main() { RunAllTests(); return 0; } ``` 以 TestCirbuf_set_size_with_init() 函式為例 : 建立一個 CuTest 的變數並將 TestCirbuf_set_size_with_init 指派給該變數,後續就可以透過這個變數來呼叫該函式(callback function)。 ```clike= struct CuTestInternal { const char *name; TestFunction function; int failed; int ran; const char *message; jmp_buf *jumpBuf; }; typedef struct CuTestInternal CuTest; void CuTestInit(CuTest *t, const char *name, TestFunction function) { t->name = CuStrCopy(name); t->failed = 0; t->ran = 0; t->message = NULL; t->function = function; t->jumpBuf = NULL; } CuTest *CuTestNew(const char *name, TestFunction function) { CuTest *tc = CU_ALLOC(CuTest); CuTestInit(tc, name, function); return tc; } ``` 接著從 testSuite 中一一執行先前所加入的 testCase。 ```clike= void CuSuiteRun(CuSuite *testSuite) { for (int i = 0; i < testSuite->count; ++i) { CuTest *testCase = testSuite->list[i]; CuTestRun(testCase); if (testCase->failed) { testSuite->failCount += 1; } } } ``` 在第 16 行的 `tc->function` 實際是執行先前指派的`TestCirbuf_set_size_with_init` 。 ```clike= void TestCirbuf_set_size_with_init(CuTest *tc) { cirbuf_t cb; cirbuf_new(&cb, 65536u); CuAssertTrue(tc, 65536u == cirbuf_size(&cb)); } void CuTestRun(CuTest *tc) { printf(" running %s\n", tc->name); jmp_buf buf; tc->jumpBuf = &buf; if (setjmp(buf) == 0) { tc->ran = 1; (tc->function)(tc); } tc->jumpBuf = 0; } ``` 故意先將 `CuAssertTrue(tc, 65536u == cirbuf_size(&cb));` 的大小改為不相等,使其方便觀察 CuAssertTrue 函式的整個作用。 ```shell $ make ... running TestCirbuf_cant_poll_twice_when_released running TestCirbuf_independant_of_each_other running TestCirbuf_independant_of_each_other_with_no_polling There was 1 failure: 1) TestCirbuf_set_size_with_init: tests/test-cirbuf.c:13: assert failed !!!FAILURES!!! Runs: 12 Passes: 11 Fails: 1 ``` 當 cond 為 1 時就直接 return 結束一個測試項目,否的話則紀錄錯誤資訊。 ```clike= #define CuAssertTrue(tc, cond) \ CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond)) void CuAssert_Line(CuTest *tc, const char *file, int line, const char *message, int condition) { if (condition) return; CuFail_Line(tc, file, line, NULL, message); } ``` 將 `assert failed`, `__FILE__`, `__LINE__` 加入到該測試項目的 tc->message ,接著使用 longjmp 回到當初呼叫 setjmp() 的地方。 `$ man memmove` > The memmove() function copies n bytes from memory area src to memory area dest. ==The memory areas may overlap==: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest. ```clike= void CuFail_Line(CuTest *tc, const char *file, int line, const char *message2, const char *message) { CuString string; CuStringInit(&string); if (message2) { CuStringAppend(&string, message2); CuStringAppend(&string, ": "); } CuStringAppend(&string, message); CuFailInternal(tc, file, line, &string); } static void CuFailInternal(CuTest *tc, const char *file, int line, CuString *string) { char buf[HUGE_STRING_LEN]; sprintf(buf, "%s:%d: ", file, line); CuStringInsert(string, buf, 0); tc->failed = 1; tc->message = string->buffer; if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0); } ``` 最後藉由 CuStringAppendFormat() 函式顯示統計的結果。 ```c void CuSuiteDetails(CuSuite *testSuite, CuString *details) { ... for (int i = 0, failCount = 0; i < testSuite->count; ++i) { CuTest *testCase = testSuite->list[i]; if (testCase->failed) { failCount++; CuStringAppendFormat(details, "%d) %s: %s\n", failCount, testCase->name, testCase->message); } } CuStringAppend(details, "\n!!!FAILURES!!!\n"); CuStringAppendFormat(details, "Runs: %d ", testSuite->count); CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount); CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount); } ``` #### 強化 cirbuf 程式碼 在 unit-test.c 中針對當有配置記憶體空間失敗時直接回傳 NULL 並印出錯誤訊息,接著不要讓該測試項目加入到測試 list 中。 ```clike= void CuSuiteAdd(CuSuite *testSuite, CuTest *testCase) { assert(testSuite->count < MAX_TEST_CASES); if(testCase == NULL) return; testSuite->list[testSuite->count] = testCase; testSuite->count++; } CuTest *CuTestNew(const char *name, TestFunction function) { int ret = 0; CuTest *tc = CU_ALLOC(CuTest); if(tc == NULL) { printf("CuTestNew malloc fail \n"); return tc; } ret = CuTestInit(tc, name, function); if(ret < 0) { free(tc); tc = NULL; printf("CuTestInit malloc fail \n"); } return tc; } ``` - [ ] 儘管已有 12 個 test case,但涵蓋層面仍不夠廣泛,請指出現有實作的缺陷並著手改善; (提示: 數值範圍及多個 PAGE_SIZE 的空間) - [ ] 學習 [Using black magic to make a fast circular buffer](https://lo.calho.st/quick-hacks/employing-black-magic-in-the-linux-page-table/),指出 fast circular buffer 實作的技巧,並分析 cirbuf 的效能,並逐步量化及改善效率; ### mmap ``` void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); ``` `$ man 2 mmap` >mmap() creates a new mapping in the virtual address space of the calling process. * addr : The starting address for the new mapping is specified in addr * length : 設定 mapping 的記憶體空間大小(必須大於 0) * prot : 設定 mapping 的操作權限(不能與 open 函式設定的權現有衝突) * PROT_EXEC : Pages may be executed * PROT_READ : Pages may be read * PROT_WRITE : Pages may be written * PROT_NONE : Pages may not be accessed * flags : 設定 mapping 到記憶體空間的功能 * MAP_SHARED : Share this mapping * MAP_PRIVATE : Create a private copy-on-write mapping. Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file. * fd : * offset : mapping 開始的位置(原先 mapping 啟始的位置加偏移量) - [ ] 向 OP-TEE 的 [Benchmark framework](https://optee.readthedocs.io/debug/benchmark.html) 取鏡,學習效能評比框架的開發;

    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