日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

It's all about buffers: zero-copy, mmap and Java NIO

發(fā)布時間:2024/1/17 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 It's all about buffers: zero-copy, mmap and Java NIO 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

?

Sep 10, 2016?in?OS

There are use cases where data need to be read from source to a sink without modification. In code this might look quite simple: for example in Java, you may read data from one?InputStream?chunk by chunk into a small buffer (typically 8KB), and feed them into the?OutputStream, or even better, you could create a?PipedInputStream, which is basically just a util that maintains that buffer for you. However, if low latency is crucial to your software, this might be quite expensive from the OS perspective and I shall explain.

What happens under the hood

Well, here’s what happens when the above code is used:

  • JVM sends read() syscall.
  • OS context switches to kernel mode and reads data into the input socket buffer.
  • OS kernel then copies data into user buffer, and context switches back to user mode. read() returns.
  • JVM processes code logic and sends write() syscall.
  • OS context switches to kernel mode and copies data from user buffer to output socket buffer.
  • OS returns to user mode and logic in JVM continues.
  • ?

    This would be fine if latency and throughput aren’t your service’s concern or bottleneck, but it would be annoying if you do care, say for a static asset server. There are 4 context switches and 2 unnecessary copies for the above example.

    OS-level zero copy for the rescue

    Clearly in this use case, the copy from/to user space memory is totally unnecessary because we didn’t do anything other than dumping data to a different socket. Zero copy can thus be used here to save the 2 extra copies. The actual implementation doesn’t really have a standard and is up to the OS how to achieve that. Typically *nix systems will offer?sendfile(). Its man page can be found?here. Some say some operating systems have broken versions of that with one of them being OSX?link. Honestly with such low-level feature, I wouldn’t trust Apple’s BSD-like system so never tested there.

    With that, the diagram would be like this:

    You may say OS still has to make a copy of the data in kernel memory space. Yes but from OS’s perspective this is already zero-copy because there’s no data copied from kernel space to user space. The reason why kernel needs to make a copy is because general hardware DMA access expects consecutive memory space (and hence the buffer). However this is avoidable if the hardware supports scatter-n-gather:

    A lot of web servers do support zero-copy such as Tomcat and Apache. For example apache’s related doc can be found?here?but by default it’s off.

    Note: Java’s NIO offers this through?transferTo?(doc).

    mmap

    The problem with the above zero-copy approach is that because there’s no user mode actually involved, code cannot do anything other than piping the stream. However, there’s a more expensive yet more useful approach - mmap, short for memory-map.

    Mmap allows code to map file to kernel memory and access that directly as if it were in the application user space, thus avoiding the unnecessary copy. As a tradeoff, that will still involve 4 context switches. But since OS maps certain chunk of file into memory, you get all benefits from OS virtual memory management - hot content can be intelligently cached efficiently, and all data are page-aligned thus no buffer copying is needed to write stuff back.

    However, nothing comes for free - while mmap does avoid that extra copy, it doesn’t guarantee the code will always be faster - depending on the OS implementation, there may be quite a bit of setup and teardown overhead (since it needs to find the space and maintain it in the TLB and make sure to flush it after unmapping) and page fault gets much more expensive since kernel now needs to read from hardware (like disk) to update the memory space and TLB. Hence, if performance is this critical, benchmark is always needed as abusing mmap() may yield worse performance than simply doing the copy.

    The corresponding class in Java is?MappedByteBuffer?from NIO package. It’s actually a variation of?DirectByteBuffer?though there’s no direct relationship between classes. The actual usage is out of scope of this post.

    NIO DirectByteBuffer

    Java NIO introduces?ByteBuffer?which represents the buffer area used for channels. There are 3 main implementations of?ByteBuffer:

  • HeapByteBuffer

    This is used when?ByteBuffer.allocate()?is called. It’s called heap because it’s maintained in JVM’s heap space and hence you get all benefits like GC support and caching optimization. However, it’s not page aligned, which means if you need to talk to native code through JNI, JVM would have to make a copy to the aligned buffer space.

  • DirectByteBuffer

    Used when?ByteBuffer.allocateDirect()?is called. JVM will allocate memory space outside the heap space using?malloc(). Because it’s not managed by JVM, your memory space is page-aligned and not subject to GC, which makes it perfect candidate for working with native code (e.g. when writing OpenGL stuff). However, you are then “deteriorated” to C programmer as you’ll have to allocate and deallocate memory yourself to prevent memory leak.

  • MappedByteBuffer

    Used when?FileChannel.map()?is called. Similar to?DirectByteBuffer?this is also outside of JVM heap. It essentially functions as a wrapper around OS mmap() system call in order for code to directly manipulate mapped physical memory data.

  • Conclusion

    sendfile()?and?mmap()?offer efficient, low-latency low-level solutions to data manipulation across sockets. Again, no code should assume these are silver bullets as real world scenarios may be complex and it might not be worth the effort to switch code to them if this is not the true bottleneck. For software engineering to get the most ROI, in most cases, it’s better to “make it right” and then “make it fast”. Without the guardrails offered by JVM, it’s easy to make software much more vulnerable to crashing (I literally mean crashing, not exceptions) when it comes to complicated logic.

    Quick Reference

    Efficient data transfer through zero copy?- It also covers sendfile() performance comparison.

    Getting started with new I/O (NIO)

    轉載于:https://my.oschina.net/fuckmylife0/blog/1554915

    總結

    以上是生活随笔為你收集整理的It's all about buffers: zero-copy, mmap and Java NIO的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 三级特黄| 亚洲福利视频一区二区三区 | 少妇无套内谢免费视频 | 国产精品久久久久久无人区 | 精品欧美一区二区三区在线观看 | 国产午夜精品一区二区三区欧美 | 波多野结衣一区二区三区高清 | 欧美一区亚洲二区 | 91免费观看网站 | 热玖玖| 无码人妻一区二区三区精品视频 | 国产精品自产拍高潮在线观看 | av大帝在线 | 少妇精品无码一区二区免费视频 | 九九热国产视频 | 一区二区三区高清在线 | 亚洲一区在线观看视频 | 国产一级黄色片子 | 国产免费一区二区三区四区五区 | 亚洲色图一区二区三区 | 黄片毛片在线免费观看 | 国产精品羞羞答答在线 | 国产欧美综合一区二区三区 | 欧美成人一区二区三区片免费 | 日韩不卡 | 精品国产乱码久久久 | 欧美美女性高潮 | 放荡的少妇2欧美版 | 久青草免费视频 | 亚洲无色 | 天堂中文字幕免费一区 | 国产精品美女在线 | 国产美女极度色诱视频www | 蜜桃免费av | 亚洲综合精品国产一区二区三区 | 日韩精品一区二区三 | 久青草影视 | 亚洲精品777 | 中文字幕日韩专区 | 亚洲欧美日韩中文在线 | 亚洲黄色视屏 | 亚洲高清在线免费观看 | 麻豆欧美| 亚洲免费观看视频 | 亚洲成年人免费观看 | 精品一区二区三区不卡 | av片一区二区 | 亚洲成人av片 | 中文字幕资源网 | 国产一区精品在线 | 另类ts人妖一区二区三区 | 一本一道av | 午夜av电影在线观看 | 亚洲美女毛片 | 免费观看黄色一级视频 | 午夜天堂影院 | 一级特黄aaa大片 | 欧洲国产视频 | 亚洲自拍中文字幕 | 久久在线 | 中国一级黄色大片 | 国产精品乱 | 日韩爽片 | www成人| 午夜免费在线观看 | 国产在线极品 | 男女男精品视频网站 | av中文字幕一区 | 性激烈视频在线观看 | 亚洲黄片一区二区 | 一级毛片黄色 | 国产中文欧美日韩在线 | 国产一区二区三区麻豆 | 爱av导航 | 先锋影音av资源网 | av丁香| 99re伊人| 草莓视频18免费观看 | 国产资源网站 | 51成人做爰www免费看网站 | 进去里在线观看 | 熊猫电影yy8y全部免费观看 | 精品国产伦一区二区三 | 亚洲区小说区图片区 | 欧美高清性xxxxhd | 91看片看淫黄大片 | 人妻丰满熟妇av无码久久洗澡 | 精品一二三四区 | 国精品无码一区二区三区 | 国产91欧美 | 蜜臀av一区二区三区有限公司 | 久久久久五月 | 精品综合久久久 | 黄色激情网站 | 亚洲v欧美v另类v综合v日韩v | 国产午夜免费视频 | 日韩人妻精品无码一区二区三区 | 九九九亚洲 | 欧美日韩一级黄色片 |