# 2021q3 Homework3 (vpoll) contributed by < `GundamBox` > ###### tags: `linux2021` ## 開發環境 - CPU - Intel i3-4160 (2C4T) - Distro - Ubuntu 20.04-LTS - Kernel - 5.8.0-63-generic ## 開發之前 - [作業連結][linux2021-summer-homework3] ## 題目 ### 1. 解釋上述程式碼運作原理,並探討對應的 memory order 議題 #### 建立與移除裝置 - 建立裝置: `static int __init vpoll_init(void)` - `alloc_chrdev_region(&major, 0, 1, NAME)` - `class_create(THIS_MODULE, NAME)` - `device_create(vpoll_class, NULL, major, NULL, NAME)` - `cdev_add(&vpoll_cdev, major, 1)` - 移除裝置: `static void __exit vpoll_exit(void)` - `device_destroy(vpoll_class, major)` - `cdev_del(&vpoll_cdev)` - `class_destroy(vpoll_class)` - `unregister_chrdev_region(major, 1)` - 資源的配置與移除要成對 #### wait_queue 與 polling > Everything is a file descriptor - `vpoll_open` - 開啟檔案(裝置)的時候建立 `wait_queue` - `vpoll_release` - 關閉檔案(裝置)的時候移除 `wait_queue` - `vpoll_poll` - `poll_wait` 註冊 wait_queue 到 kernel,由 kernel 管理 polling - `vpoll_ioctl` 改變 - `wake_up_locked_poll` #### user.c - parent process `epoll_wait` 會等待一段時間並回傳 event 數量 - 數量為 -1 => 錯誤發生 - 數量為 0 => 時間內沒事件發生,輸出逾時 - 數量大於 0,事件發生,並透過 `ioctl` 發出 `VPOLL_IO_DELEVENTS` 命令給 module,若接收到的事件包含 `EPOLLHUP` 就停止迴圈 - child process 暫停一段時間後透過 `ioctl` 發出命令給 module #### wake_up_interruptible_poll? wake_up_locked_poll? 原本不懂為什麼,直到看 [RoyWFHuang 同學的答案][RoyWFHuang-homework3]後才想通問題 在 [wait.c](https://github.com/torvalds/linux/blob/bcf876870b95592b52519ed4aafcf9d95999bc9c/kernel/sched/wait.c#L122) 的實作中,會嘗試 lock,但之前 module 已經 lock,所以第二次的 lock 就會失敗 #### memory order 議題 :::info 施工中 目前只看完 jserv 老師給的 [memory-barriers][memory-barriers] 文件,不知道從哪下手探討 ::: ### 2. 修改 vpoll 核心模組,實作效能評比的特別模式,從而分析 epoll 效能 > 參閱 [Epoll Kernel Performance Improvements][Epoll Kernel Performance Improvements] 和 [linux-ipc-benchmarks][linux-ipc-benchmarks] :::info 施工中 ::: ## 參考資料 1. [struct file_operations][struct-file-operations] 2. [ioctl][ioctl] 3. [memory-barriers][memory-barriers] 4. [RoyWFHuang - 2021q3 Homework3 (vpoll)][RoyWFHuang-homework3] [linux2021-summer-homework3]: https://hackmd.io/@sysprog/linux2021-summer-quiz4 [Epoll Kernel Performance Improvements]: https://events19.linuxfoundation.org/wp-content/uploads/2018/07/dbueso-oss-japan19.pdf [linux-ipc-benchmarks]: https://github.com/kamalmarhubi/linux-ipc-benchmarks [struct-file-operations]: https://www.kernel.org/doc/html/v5.10/filesystems/vfs.html#struct-file-operations [ioctl]: https://www.kernel.org/doc/html/v5.10/driver-api/ioctl.html [memory-barriers]: https://www.kernel.org/doc/Documentation/memory-barriers.txt [RoyWFHuang-homework3]: https://hackmd.io/@royhuang/2021linux_vpoll#%E8%A7%A3%E9%87%8B%E4%B8%8A%E8%BF%B0%E7%A8%8B%E5%BC%8F%E7%A2%BC%E9%81%8B%E4%BD%9C%E5%8E%9F%E7%90%86%EF%BC%8C%E4%B8%A6%E6%8E%A2%E8%A8%8E%E5%B0%8D%E6%87%89%E7%9A%84-memory-order-%E8%AD%B0%E9%A1%8C