From 413f1324e0b87b54800e80e3ae0c7489e970c63a Mon Sep 17 00:00:00 2001 From: Xinyang YU <47915643+xy-241@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:07:50 +0800 Subject: [PATCH] os - buffer - dma (zero copy) --- content/OS/IO/Buffer.md | 5 ++--- content/OS/IO/Direct Memory Access (DMA).md | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/content/OS/IO/Buffer.md b/content/OS/IO/Buffer.md index 6c4fcadfa..312d21ff6 100644 --- a/content/OS/IO/Buffer.md +++ b/content/OS/IO/Buffer.md @@ -6,7 +6,7 @@ Author Profile: tags: - OS Creation Date: 2024-04-16, 12:48 -Last Date: 2024-04-16T13:13:47+08:00 +Last Date: 2024-12-01T14:47:58+08:00 References: draft: description: @@ -14,10 +14,9 @@ description: ## Abstract --- - A buffer serves as a **temporary storage area** that **consolidates multiple pieces of data** before they are read from or written to an [[OS/IO/IO Device|IO Device]]/[[Socket]] by the [[Kernel]] -- Reducing the negative impact of [[System Call (系统调用)]] overhead and smoothen the data flow >[!success] Better performance -> This consolidation helps optimize data transfer by **minimizing the overhead** associated with individual data transfers which requires System Call, and can also help **smooth out disparities** in data transfer rates between different components of the system. +> This consolidation helps optimise data transfer by **minimising the overhead** associated with individual data transfers, which require [[System Call (系统调用)|system calls]], and also helps **smooth out disparities** in data transfer rates between different system components. >[!example] > IO Device Buffering diff --git a/content/OS/IO/Direct Memory Access (DMA).md b/content/OS/IO/Direct Memory Access (DMA).md index 3a7e5c2c9..ff34da9f2 100644 --- a/content/OS/IO/Direct Memory Access (DMA).md +++ b/content/OS/IO/Direct Memory Access (DMA).md @@ -6,7 +6,7 @@ Author Profile: tags: - OS Creation Date: 2023-08-27T14:44:16+08:00 -Last Date: 2024-12-01T14:27:24+08:00 +Last Date: 2024-12-01T15:07:33+08:00 References: description: DMA enables direct data transfer between memory and device controllers, avoiding CPU intervention and busy waiting. Zero copy minimises memory copies using system calls like sendfile(2), freeing the CPU for other tasks. --- @@ -20,15 +20,21 @@ description: DMA enables direct data transfer between memory and device controll ![[zero_copy_kafka.svg|500]] -- Zero copy means [[CPU]] does not perform the task of **copying data from one memory area to another** or in which **unnecessary data copies are avoided** -- The above diagram shows data is copied directly from the OS buffer to the NIC Buffer via [[System Call (系统调用)]] like [sendfile(2)](https://man7.org/linux/man-pages/man2/sendfile.2.html). This avoids copying data from the OS buffer to the Kafka buffer which is in the [[User Space]], and avoids making another system call to copy the data from Kafka buffer to the socket buffer, then eventually the NIC buffer +- Zero copy means [[CPU]] does not perform the task of **copying data from one memory area to another** with the help of [[Direct Memory Access (DMA)|DMA]] or in which **unnecessary data copies are avoided** ->[!success] CPU is free! -> The direct copying of data is handled by [[Direct Memory Access (DMA)]], so the CPU isn't involved and is free to work on other tasks! +>[!success] Benefits of zero copy +> The CPU is **consistently involved** in copying data between the **OS buffer** in [[Kernel Space|kernel space]] and the **Kafka buffer** in user space, and vice versa. Expensive [[Context Switch|context switching]] is also involved. +> +> Using system calls like [`sendfile(2)`](https://man7.org/linux/man-pages/man2/sendfile.2.html), data is copied directly from the OS buffer to the NIC buffer. +> +> Unlike `read` and `write`, which require transferring data to and from user space, copying with `sendfile` occurs entirely within kernel space. The actual data transfer is offloaded to the [[Direct Memory Access (DMA)|DMA]], freeing the CPU for other computational tasks. +> +> `sendfile` is particularly useful when the application in user space does not need to process the data, and the data is ready to be sent out via the NIC (Network Interface Card). ## References --- -- [System Design: Why is Kafka fast? - YouTube](https://youtu.be/UNUz1-msbOM?si=2nC4zt0WOb1CgR6P) \ No newline at end of file +- [System Design: Why is Kafka fast? - YouTube](https://youtu.be/UNUz1-msbOM?si=2nC4zt0WOb1CgR6P) +- [sendfile(2) - Linux manual page](https://man7.org/linux/man-pages/man2/sendfile.2.html) \ No newline at end of file