sysprog
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Linux 核心專題: kHTTPd 改進 > 執行人: williamlin0518 > [專題解說影片](https://www.youtube.com/watch?v=Lo_5C3lfhzo&t=97s) ## 任務簡介 改進 kHTTPd 的並行處理能力,予以量化並有效管理連線。 ## TODO: 依據[第七次作業](https://hackmd.io/@sysprog/linux2024-ktcp) 開發 kHTTPd > 著重 concurrency, workqueue, CMWQ > 充分予以量化分析 ## [Linux 核心設計: RCU 同步機制](https://hackmd.io/@sysprog/linux-rcu) a synchronization mechanism handle read-mostly situations ### Key Concepts of RCU: * Reading without Locks * Grace Period * Deferred Updates -------------------------------------------------- ## Workqueues in the Linux Kernel ### 1. Goals of cmwq Make improve on original wq both concurrency and resource usage #### Multi-threaded wq had one worker thread per CPU * Single MT wq would have N worker threads, N is CPUs in the system. * resource wastage #### Single-threaded wq had one worker thread system-wide * limited concurrency ### 2. CMWQ Design #### Unified Worker Pools * Shared pool of worker threads for all workqueues. #### Separation of High Priority and Normal Tasks * separate pools for normal and high-priority tasks ## 透過 eBPF 觀察作業系統行為 -------------------------------------------------- ## 解釋 drop-tcp-socket 核心模組運作原理 -------------------------------------------------- ## [kecho](https://github.com/sysprog21/kecho) Trace the steps from a connection is initiated to when it is processed: ### **Request Handling Flow** #### 1. Listening for Connections: 1. `kecho_init_module` is called. Sets up the listening socket and starts the echo server daemon thread. 2. create and set up a socket that listens on a specific port for incoming connections. #### 2. Accepting Connections: 1. The `echo_server_daemon` function runs in a loop, handling the server logic. It uses `kernel_accept` to accept incoming connections on the listening socket. #### 3. Handling Connections (Work Item Execution): 1. Inside `create_work`, a `struct kecho` instance is initialized and a work structure (`struct work_struct`) is prepared. This is enqueued into `kecho_wq`. 2. `echo_server_worker` is the function assigned to this work structure. When the workqueue decides to execute this task, `echo_server_worker` processes the data from the connected socket ```sql +-------------------+ | echo_server_daemon| | (Kernel Thread) | +-------------------+ | | listens for connections V +----------------------------+ | Workqueue (kecho_wq) | | - Manages multiple works | | submitted for processing| \\ +----------------------------+ \\ || \\ || \\ V V +--------------------------+ +--------------------------+ | Worker 1 (struct kecho) | | Worker 2 (struct kecho) | | - Handles specific | | - Handles specific | | connection A | | connection B | | - Contains: | | - Contains: | | * struct work_struct | | * struct work_struct | | * struct socket* | | * struct socket* | | * Other connection | | * Other connection | | specific data | | specific data | +--------------------------+ +--------------------------+ ``` ### Analysis Mechanism of kecho ---------------- ## 在 khttpd 引入 CMWQ ``` william@william-System-Product-Name:~/linux2024/khttpd$ ./htstress -n 100000 -c 1 -t 4 http://localhost:8081/ 0 requests 10000 requests 20000 requests 30000 requests 40000 requests 50000 requests 60000 requests 70000 requests 80000 requests 90000 requests requests: 100000 good requests: 100000 [100%] bad requests: 0 [0%] socket errors: 0 [0%] seconds: 1.614 requests/sec: 61959.059 ``` -------------------------------------- 1. 宣告 `daemon` 全域變數,用來存放目前服務是否已停止和 worker 的 linked list ```c struct http_service daemon = {.is_stopped = false, .worker = LIST_HEAD_INIT(daemon.worker)}; ``` 2. 參考kecho ,建立代表 service 和 worker 的結構。 ```c struct http_service { bool is_stopped; struct list_head worker; }; struct khttpd { struct socket *sock; struct list_head list; struct work_struct http_work; }; ``` 3. daemon 函式中對應的 kthread_create 修改為以 create_work 建立新的任務,並利用 queue_work 將新增的任務加入 workqueue 中等待執行。 ```c static struct work_struct *create_work(struct socket *sk) { struct http_work *work; if (!(work = kmalloc(sizeof(struct http_work), GFP_KERNEL))) return NULL; work->socket = sk; INIT_WORK(&work->khttpd_work, http_server_worker_CMWQ); list_add(&work->node, &daemon_list.head); return &work->khttpd_work; } ``` 5. error module not insert correctly ## 分析效能表現和提出改進方案 ##

    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