一个人如何在Mac OS X上做原料IO? (即相当于对Linux的O_DIRECT标志) [英] How does one do Raw IO on Mac OS X? (ie. equivalent to Linux's O_DIRECT flag)

查看:192
本文介绍了一个人如何在Mac OS X上做原料IO? (即相当于对Linux的O_DIRECT标志)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Google的一个方式做原料(有时也称为直接)I / O在Mac OS。原始I / O打开操作系统页面缓存来给应用程序到磁盘更直接地访问。因为一些我使用的filestructure都没有有效的使用LRU页面置换,这非常有用。这是实现我们所需要的页面置换算法相当直截了当,但首先我们需要关闭OS X默认缓冲。我们已经打开文件时使用O_DIRECT标记linux下已经做到了这一点。有谁知道如何在Mac OS关闭页面缓冲?

I having been googling for a way to do raw (sometimes called direct) i/o under mac os. Raw i/o turns of the operating system page cache to give the application more direct access to the disk. This is useful because some of the filestructure I am using are not efficient using LRU page replacement. It is fairly straight forward to implement the page replacement algorithms we need but first we need to turn off os x default buffering. We have already done this under linux using the O_DIRECT flag when opening files. Does anyone know how to turn off page buffering under mac os?

干杯
蒂姆·

推荐答案

通过手册页一些阅读后,我终于找到了理想的答案。原来,MAC OS实际上有非常类似的机制O_DIRECT,但它是不是通过公开的功能是通过的fcntl。具体来说有一个名为F_NOCACHE一个选项,它允许你打开缓存或关闭特定的文件描述符,这正是我想要的。见<一href=\"http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man2/fcntl.2.html\">http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man2/fcntl.2.html为全面运行的其他东西,你可以用的fcntl Mac版,它的确切用途的说明做下来。我希望这个答案可以帮助别人了。

After some more reading through the man pages I finally found the ideal answer. It turns out mac os actually has very similar mechanism to O_DIRECT, however it is not through the open function it is through fcntl. Specifically there is an option called F_NOCACHE which allows you to turn the cache on or off for a particular file descriptor which is exactly what I wanted. See http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man2/fcntl.2.html for the full run down of the other things you can do with the mac version of fcntl, and explanation of its exact use. I hope this answer will help someone else out.

<一个href=\"http://lists.apple.com/archives/filesystem-dev/2007/Sep/msg00010.html\">http://lists.apple.com/archives/filesystem-dev/2007/Sep/msg00010.html是一个很好的线索,解释F_NOCACHE标志根据您的Mac OS版本号的行为。

http://lists.apple.com/archives/filesystem-dev/2007/Sep/msg00010.html Is a good thread that explains how the F_NOCACHE flag behaves depending on your mac os version number.

最后code(围棋):

Final Code (in go):

    r1, r2, err := syscall.Syscall(syscall.SYS_FCNTL, uintptr(self.file.Fd()), syscall.F_NOCACHE, 1)
    if err != 0 {
        fmt.Printf("Syscall to SYS_FCNTL failed\n\tr1=%v, r2=%v, err=%v\n", r1, r2, err)
        self.Close()
        return false
    }

这篇关于一个人如何在Mac OS X上做原料IO? (即相当于对Linux的O_DIRECT标志)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆