在Linux系统编程中,IO流(Input/Output Streams)是一个非常重要的概念。高级IO流是基于基本IO操作(如read、write等)之上的扩展,提供了更强大的功能和更高效的操作方式。本文将深入探讨Linux中的高级IO流,重点介绍其原理和使用方法,并提供相应的C++代码示例。
在 Linux 平台上进行开发,IO 操作是一个非常重要的领域,掌握 IO 操作不仅能够提升应用程序的性能,还能够提高系统资源的利用效率。那么,如何才能算得上精通 IO 呢?本文将从几个方面进行详细探讨,包括文件 IO、网络 IO 以及高级 IO 技术。
NVMe SSD具有高性能、低时延等优点,是目前存储行业的研究热点之一,但在光鲜的性能下也同样存在一些没有广为人知的问题,而这些问题其实对于一个生产系统而言至关重要,例如:
前段时间我在实现 rust-kernel-riscv (使用 Rust 无栈协程进行上下文切换的操作系统内核) 时, 跟进了一些 Linux Kernel 的特性, 其中印象最深的就是 io_uring. io_uring 作为最新的高性能异步 I/O 框架, 支持普通文件与网络套接字的异步读写, 解决了传统 AIO 的许多问题. 在 Linux 通过隔离内核页表来应对 Meltdown 攻击后, 系统调用的开销是不可忽略的, 而 io_uring 通过映射一段在用户态与内核态共享的内存区域, 显著减少系统调用的次数, 缓解了刷新缓存开销. 关于 io_uring 的使用方法可以参考迟先生的博客: io_uring 的接口与实现.
此文来自于AndresFreund,PG社区资深开发,探讨IO对于PG方面的问题。此翻译和文字来自于视频,因为部分英文听的比较费劲,所以可能有失误的地方,尽请见谅。
本文转载自https://0xffffff.org/2017/05/01/41-linux-io/
在 Linux 系统中,传统的访问方式是通过 write() 和 read() 两个系统调用实现的,通过 read() 函数读取文件到到缓存区中,然后通过 write() 方法把缓存中的数据输出到网络端口。
POSIX AIO 是在用户控件模拟异步 IO 的功能,不需要内核支持,而 linux AIO 则是 linux 内核原声支持的异步 IO 调用,行为更加低级。
本文从操作系统的角度来解释BIO,NIO,AIO的概念,含义和背后的那些事。本文主要分为3篇。 第一篇 讲解BIO和NIO以及IO多路复用 第二篇 讲解磁盘IO和AIO 第三篇 讲解在这些机制上的一些应用的实现方式,比如nginx,nodejs,Java NIO等 磁盘IO 磁盘IO,简单来说就是读取硬盘一类设备的IO。这类设备包括传统的磁盘、SSD、闪存、CD等。操作系统将其统一抽象为”块设备“。所以磁盘IO又可以叫做”块IO“。这些设备上的数据一般用文件系统来组织,所以又可以成为”文件IO“。本文统
磁盘IO,简单来说就是读取硬盘一类设备的IO。这类设备包括传统的磁盘、SSD、闪存、CD等。操作系统将其统一抽象为”块设备“。所以磁盘IO又可以叫做”块IO“。这些设备上的数据一般用文件系统来组织,所以又可以成为”文件IO“。本文统一用”磁盘IO“这个术语。
如果你觉得这些问题都很简单,都能很明确的回答上来。那么很遗憾这篇文章不是为你准备的,你可以关掉网页去做其他更有意义的事情了。如果你觉得无法明确的回答这些问题,那么就耐心地读完这篇文章,相信不会浪费你的时间。受限于个人时间和文章篇幅,部分议题如果我不能给出更好的解释或者已有专业和严谨的资料,就只会给出相关的参考文献的链接,请读者自行参阅。
在这篇博客中,我们将探讨Linux底层的几种IO(输入/输出)方式,为鸿蒙开发者提供一个清晰的理解。本文将详细介绍阻塞IO、非阻塞IO、I/O多路复用、信号驱动IO及异步IO等概念,旨在帮助开发者优化鸿蒙应用性能。关键词:鸿蒙OS、Linux、IO模型、阻塞非阻塞、IO多路复用、性能优化。
我们都知道unix世界里、一切皆文件、而文件是什么呢?文件就是一串二进制流而已、不管socket、还是FIFO、管道、终端、对我们来说、一切都是文件、一切都是流、在信息交换的过程中、我们都是对这些流进行数据的收发操作、简称为I/O操作(input and output)、往流中读出数据、系统调用read、写入数据、系统调用write、不过话说回来了、计算机里有这么多的流、我怎么知道要操作哪个流呢?做到这个的就是文件描述符、即通常所说的fd(file descriptor)、一个fd就是一个整数、所以对这个整数的操作、就是对这个文件(流)的操作、我们创建一个socket、通过系统调用会返回一个文件描述符、那么剩下对socket的操作就会转化为对这个描述符的操作、不能不说这又是一种分层和抽象的思想、
IO子系统一般是linux系统中最慢的部分。一个原因是它距离CPU的距离,另一个原因是它的物理结构。访问磁盘的时间与访问内存的时间是7天与7分钟的区别。linux kernel要尽量减少磁盘IO。 1.Reading and Writing Data linux内核以page为单位访问磁盘IO,一般为4K。 查看页大小:/usr/bin/time -v date Page size (bytes): 4096 2.Major and Minor Page Faul
去年秋天,我正在开发一个库,创建一套安全的API,实现在一个 io-uring 实例的基础上执行 future。虽然最后发布了一个叫 iou 的 liburing 的 binding 库,但其与 future 集成的 ostkreuz 库最终未能发布。我不知道将来是否会继续这项工作,但是有些人已经开始开发目标类似的库了,因此我想就我在 io-uring 和 Rust 的 future 模型上的学习情况做一些笔记。这篇文章假定你对 io-uring API 有一定了解。这个文档(https://kernel.dk/io_uring.pdf)提供了关于 io-uring 的高级概述。
最近在项目开发中,由cs开发的exe的程序,需要自动升级,该exe程序放在linux下,自动升级时检测不到该exe程序的版本号信息,但是我们客户端的exe程序需要获取服务器上新程序的版本号信息。最后由我用java实现linux上exe文件的版本号读取功能。下面是详细代码:
在Linux系统中,我们经常用free命令来查看系统内存的使用状态。在一个RHEL6的系统上,free命令的显示内容大概是这样一个状态:
不同版本的操作系统的 buffer_head 代表的大小可能不一样,但是都是内存和硬盘交换数据的基本单元。
BIO, NIO, AIO,本身的描述都是在Java语言的基础上的。 而描述IO,我们需要从三个层面:
tc(Traffic Control) 是linux系统中常用的来控制传输速率、模拟网络延时丢包等场景的工具,tc命令有三个主要的概念,是qdisc、class和filter,qdisc又分为classless qdisc和classful qdisc,在控制传输速度的方面大致有两种用法
👋 你好,我是 Lorin 洛林,一位 Java 后端技术开发者!座右铭:Technology has the power to make the world a better place.
这篇文章读不懂的没关系,可以先收藏一下。笔者准备介绍完epoll和NIO等知识点,然后写一篇Java网络IO模型的介绍,这样可以使Java网络IO的知识体系更加地完整和严谨。初学者也可以等看完IO模型介绍的博客之后,再回头看这些博客,会更加有收获。
结合这个图,想想Linux系统编程里用到的Buffered IO、mmap(2)、Direct IO,这些机制怎么和Linux IO栈联系起来呢?上面的图有点复杂,我画一幅简图,把这些机制所在的位置添加进去:
以交友平台用户中心的user表为例,单表数据规模达到千万级别时,你可能会发现使用用户筛选功能查询用户变得非常非常慢,明明查询命中了索引,但是,部分查询还是很慢,这时候,我们就需要考虑拆分这张user表了。
接下来我们会学习一个 Netty 系列教程,Netty 系列由「架构与原理」,「源码」,「架构」三部分组成,今天我们先来看看第一部分:Netty 架构与原理初探,大纲如下:
由盘片,磁头组成,数据存在盘片的环形磁道上,读写时,磁头移动,定位到数据的磁道,进行数据读写
零拷贝(Zero-Copy)是一个大家耳熟能详的概念,那么,具体有哪些框架会使用到零拷贝呢?在思考这个问题之前,让我们先一起探寻一下零拷贝机制的底层原理。
最近工作中遇到某个服务器应用程序 UDP 丢包,在排查过程中查阅了很多资料,我在排查过程中基本都是通过使用 tcpdump 在出现问题的各个环节上进行抓包、分析在那个环节出现问题、针对性去排查解决问题,对症下药,最后终究能够解决问题。但是这种情况大多是因为服务本身的问题,如果是环境问题、操作系统、甚至硬件的问题,可能从服务本身出发不能解决问题,但是这篇文章另辟蹊径,从外部环境分析可能丢包的原因,看完之后,很受用,部分章节对原文有所修改,下面分享出来供更多人参考。
最近工作中遇到某个服务器应用程序 UDP 丢包,在排查过程中查阅了很多资料,总结出来这篇文章,供更多人参考。
最近越来越认为,在讲解技术相关问题时,大白话固然很重要,通俗易懂,让人有想读下去的欲望。但几乎所有的事,都有两面性,在看到其带来好处时,不妨想想是否也引入了不好的地方。
NIO是Jdk中非常重要的一个组成部分,基于它的Netty开源框架可以很方便的开发高性能、高可靠性的网络服务器和客户端程序。本文将就其核心基础类型Channel, Buffer, Selector进行
摘要 关于epoll的问题很早就像写文章讲讲自己的看法,但是由于ffrpc一直没有完工,所以也就拖下来了。Epoll主要在服务器编程中使用,本文主要探讨服务器程序中epoll的使用技巧。Epoll一般和异步io结合使用,故本文讨论基于以下应用场合: 主要讨论服务器程序中epoll的使用,主要涉及tcp socket的相关api。 Tcp socket 为异步模式,包括socket的异步读写,以及监听的异步操作。 本文不会过多讨论API的细节,而是专注流程与设计。 Epoll 的io模型 Epol
bs_is_seq_randIfthisoptionisset,fiowillusethenormalread,writeblocksizesettingsassequential,randominstead.AnyrandomreadorwritewillusetheWRITEblocksizesettings,andanysequentialreadorwritewillusetheREADblocksizesetting.zero_buffersIfthisoptionisgiven,fiowillinittheIObufferstoallzeroes.Thedefaultistofillthemwithrandomdata.refill_buffersIfthisoptionisgiven,fiowillrefilltheIObuffersoneverysubmit.Thedefaultistoonlyfillitatinittimeandreusethatdata.Onlymakessenseifzero_buffersisn't specified, naturally. If data verification is enabled, refill_buffers is also automatically enabled. scramble_buffers=bool If refill_buffers is too costly and the target is using data deduplication, then setting this option will slightly modify the IO buffer contents to defeat normal de-dupe attempts. This is not enough to defeat more clever block compression attempts, but it will stop naive dedupe of blocks. Default: true. buffer_compress_percentage=int If this is set, then fio will attempt to provide IO buffer content (on WRITEs) that compress to the specified level. Fio does this by providing a mix of random data and a fixed pattern. The fixed pattern is either zeroes, or the pattern specified by buffer_pattern. If the pattern option is used, it might skew the compression ratio slightly. Note that this is per block size unit, for file/disk wide compression level that matches this setting, you'llalsowanttosetrefill_buffers.buffer_compress_chunk=intSeebuffer_compress_percentage.Thissettingallowsfiotomanagehowbigtherangesofrandomdataandzeroeddatais.Withoutthisset,fiowillprovidebuffer_compress_percentageofblocksizerandomdata,followedbytheremainingzeroed.Withthissettosomechunksizesmallerthantheblocksize,fiocanalternaterandomandzeroeddatathroughouttheIObuffer.buffer_pattern=strIfset,fiowillfilltheiobufferswiththispattern.Ifnotset,thecontentsofiobuffersisdefinedbytheotheroptionsrelatedtobuffercontents.Thesettingcanbeanypatternofbytes,andcanbeprefixedwith0xforhexvalues.Itmayalsobeastring,wherethestringmustthenbewrappedwit
1、字符设备驱动: 当我们的应用层读写(read()/write())字符设备驱动时,是按字节/字符来读写数据的,期间没有任何缓存区,因为数据量小,不能随机读取数据,例如:按键、LED、鼠标、键盘等 2、块设备: 块设备是i/o设备中的一类, 当我们的应用层对该设备读写时,是按扇区大小来读写数据的,若读写的数据小于扇区的大小,就会需要缓存区, 可以随机读写设备的任意位置处的数据,例如 普通文件(.txt,.c等),硬盘,U盘,SD卡。 3、块设备结构: 段(Segments):由若干个块组成。是Linux内存管理机制中一个内存页或者内存页的一部分。 块 (Blocks): 由Linux制定对内核或文件系统等数据处理的基本单位。通常由1个或多个扇区组成。(对Linux操作系统而言) 扇区(Sectors):块设备的基本单位。通常在512字节到32768字节之间,默认512字节 应用程序进行文件的读写,通过文件系统将文件的读写转换为块设备驱动操作硬件。
操作系统很早就开始使用内存映射文件(Memory Mapped File)来作为进程间的共享存储区,这是一种非常高效的进程通讯手段。.NET 4.0新增加了一个System.IO. MemoryMappedFiles命名空间,其中添加了几个类和相应的枚举类型,从而使我们可以很方便地创建内存映射文件。Mono 3.2也有这个类来操作Linux下的内存映射文件,《MemoryMappedFile 在 Mono in Linux 的开发笔记》详细的介绍了Mono和.NET 4的实现区别,为了让代码能够在Linux
前文我们介绍了如何使用 Node Exporter 监控 Linux 主机的 CPU 使用率,接下来我们来介绍如何监控 Linux 的磁盘空间、磁盘 IO、网络 IO 等方面。
java中最最让人激动的部分就是IO和NIO了。IO的全称是input output,是java程序跟外部世界交流的桥梁,IO指的是java.io包中的所有类,他们是从java1.0开始就存在的。NIO叫做new IO,是在java1.4中引入的新一代IO。
执行 ./test.sh 0,观察out.txt文件大小变化(程序不停的向out.txt文件写数据):
Kafka之所以那么快,其中一个很大的原因就是零拷贝(Zero-copy)技术,零拷贝不是kafka的专利,而是操作系统的升级,又比如Netty,也用到了零拷贝。下面我就画图讲解零拷贝,如果对你有帮助请点个赞支持。
今天发现突然有一台主机无缘无故死机了,于是翻看了/var/log/message日志,发现提示: echo 0 > /proc/sys/kernel/hung_task_timeout_secs;
这里我们使用术语“缓冲”(一般为buffer)来表示对数据写的暂存,使用术语“缓存”(一般为cache)来表示对数据读的暂存。顾名思义,由于底层存储设备和内存之间速率的差异,缓冲是用来暂“缓”对底层存储设备IO的“冲”击。缓存主要是在内存中暂“存”从磁盘读到的数据,以便接下来对这些数据的访问不用再次访问慢速的底层存储设备。
在Windows下资源管理器查看内存使用的情况,如果使用率达到80%以上,再运行大程序就能感觉到系统不流畅了,因为在内存紧缺的情况下使用交换分区,频繁地从磁盘上换入换出页会极大地影响系统的性能。而当我
tar是“tape archive”(磁带存档)的简称,它出现在还没有软盘驱动器、硬盘和 光盘驱动器的计算机早期阶段,随着时间的推移, tar命令逐渐变为一个将很多文件进行存档的工具,目前许多用于Linux操作系统的程序就是打包为tar档案文件的形式。 在Linux里面,tar一般和其他没有文件管理的压缩算法文件结合使用,用tar打包整个文件目录结构成一个文件,再用gz,bzip等压缩算法压缩成一次。也是Linux常见的压缩归档的处理方法
在 Linux 系统中,我们经常用 free 命令来查看系统内存的使用状态。在个 RHEL6 的系统上,free 命令的显示内容大概是这样一个状态: 这里的默认显示单位是 kb,我的服务器是 128
领取专属 10元无门槛券
手把手带您无忧上云