Skip to content

Commit

Permalink
os
Browse files Browse the repository at this point in the history
- buffer
- dma (zero copy)
  • Loading branch information
xy-241 committed Dec 1, 2024
1 parent 030e872 commit 413f132
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 2 additions & 3 deletions content/OS/IO/Buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ 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:
---
## 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
Expand Down
18 changes: 12 additions & 6 deletions content/OS/IO/Direct Memory Access (DMA).md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
---
Expand All @@ -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)
- [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)

0 comments on commit 413f132

Please sign in to comment.