posix_fadvise源码分析

posix_fadvise是linux上对文件进行预取的系统调用,其中第四个参数int advice为预取的方式,主要有以下几种:

POSIX_FADV_NORMAL                         无特别建议                                      重置预读大小为默认值
POSIX_FADV_SEQUENTIAL                将要进行顺序操作                           设预读大小为默认值的2 倍
POSIX_FADV_RANDOM                        将要进行随机操作                           将预读大小清零(禁止预读)
POSIX_FADV_NOREUSE                     指定的数据将只访问一次              (暂无动作)
POSIX_FADV_WILLNEED                    指定的数据即将被访问                    立即预读数据到page cache
POSIX_FADV_DONTNEED                  指定的数据近期不会被访问            立即从page cache 中丢弃数据

其中,POSIX_FADV_NORMAL、POSIX_FADV_SEQUENTIAL、POSIX_FADV_RANDOM  是用来调整预取窗口的大小,POSIX_FADV_WILLNEED则可以将指定范围的磁盘文件读入到page cache中,POSIX_FADV_DONTNEED则将指定的磁盘文件中数据从page cache中换出。

在kernel源码的mm/fadvise.c中,如下所示:

SYSCALL_DEFINE(fadvise64_64)(int fd, loff_t offset, loff_t len, int advice)
{
    struct file *file = fget(fd);
    struct address_space *mapping;
    struct backing_dev_info *bdi;
    loff_t endbyte;            /* inclusive */
    pgoff_t start_index;
    pgoff_t end_index;
    unsigned long nrpages;
    int ret = 0;

/* 判断由文件描述符获得的file指针是否有效 */
    if (!file)
        return -EBADF;

/* 判断是否是管道 */
    if (S_ISFIFO(file->f_path.dentry->d_inode->i_mode)) {
        ret = -ESPIPE;
        goto out;
    }


    mapping = file->f_mapping;
    if (!mapping || len < 0) {
        ret = -EINVAL;
        goto out;
    }

    if (mapping->a_ops->get_xip_mem) {
        switch (advice) {
        case POSIX_FADV_NORMAL:
        case POSIX_FADV_RANDOM:
        case POSIX_FADV_SEQUENTIAL:
        case POSIX_FADV_WILLNEED:
        case POSIX_FADV_NOREUSE:
        case POSIX_FADV_DONTNEED:
            /* no bad return value, but ignore advice */
            break;
        default:
            ret = -EINVAL;
        }
        goto out;
    }

    /* Careful about overflows. Len == 0 means "as much as possible" */
    endbyte = offset + len;
    if (!len || endbyte < len)
        endbyte = -1;
    else
        endbyte--;        /* inclusive */

    bdi = mapping->backing_dev_info;

    switch (advice) {
    case POSIX_FADV_NORMAL:

       /* 将预取窗口调整为设备默认的大小 */
        file->f_ra.ra_pages = bdi->ra_pages;
        spin_lock(&file->f_lock);
        file->f_mode &= ~FMODE_RANDOM;
        spin_unlock(&file->f_lock);
        break;
    case POSIX_FADV_RANDOM:

        /* 将文件的访问模式设为random即不做预取 */
        spin_lock(&file->f_lock);
        file->f_mode |= FMODE_RANDOM;
        spin_unlock(&file->f_lock);
        break;
    case POSIX_FADV_SEQUENTIAL:

        /* 将预取窗口设为默认值的2倍 */
        file->f_ra.ra_pages = bdi->ra_pages * 2;
        spin_lock(&file->f_lock);
        file->f_mode &= ~FMODE_RANDOM;
        spin_unlock(&file->f_lock);
        break;
    case POSIX_FADV_WILLNEED:

        /* 强制读取文件指定范围内的数据 */

        if (!mapping->a_ops->readpage) {
            ret = -EINVAL;
            break;
        }

        /* First and last PARTIAL page! */

        /* 获得起始页、结束页的编号 */
        start_index = offset >> PAGE_CACHE_SHIFT;
        end_index = endbyte >> PAGE_CACHE_SHIFT;

        /* Careful about overflow on the "+1" */

        /* 计算需要读取的页数,并+1向上取整 */

        nrpages = end_index - start_index + 1;
        if (!nrpages)
            nrpages = ~0UL;

       /* 强制读取指定范围的数据 */       

        ret = force_page_cache_readahead(mapping, file,
                start_index,
                nrpages);
        if (ret > 0)
            ret = 0;
        break;
    case POSIX_FADV_NOREUSE:

        /* 没有实现,no use 没用 */

        break;
    case POSIX_FADV_DONTNEED:

        /* 将指定范围内的数据从page cache中换出 */

        if (!bdi_write_congested(mapping->backing_dev_info))
            filemap_flush(mapping);

        /* First and last FULL page! */

        /* 计算数据的起始页、结束页的编号 */

        start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
        end_index = (endbyte >> PAGE_CACHE_SHIFT);

        if (end_index >= start_index)

           /* 将指定页从page cache中换出*/

            invalidate_mapping_pages(mapping, start_index,
                        end_index);
        break;
    default:
        ret = -EINVAL;
    }
out:
    fput(file);
    return ret;
}


[root@VM-24-11-opencloudos nginx-1.24.0]# ./configure --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --with-openssl=/www/server/nginx/src/openssl --with-pcre=/usr/local/src/pcre-8.43 --w ith-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stu b_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --wi th-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_modul e --with-http_realip_module --with-http_mp4_module --add-module=/www/server/nginx/src/ngx_http_substitutio ns_filter_module-master --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-http_dav_module --add-module= /www/server/nginx/src/nginx-dav-ext-module --add-module=/usr/local/fastdfs-nginx-module/src checking for OS + Linux 6.6.47-12.oc9.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 12.3.1 20230912 (OpenCloudOS 12.3.1.3-1) (Tencent Compiler 12.3.1.3) checking for gcc -pipe switch ... found checking for --with-ld-opt="-Wl,-E" ... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found checking for gcc variadic macros ... found checking for gcc builtin 64 bit byteswap ... found checking for unistd.h ... found checking for inttypes.h ... found checking for limits.h ... found checking for sys/filio.h ... not found checking for sys/param.h ... found checking for sys/mount.h ... found checking for sys/statvfs.h ... found checking for crypt.h ... found checking for Linux specific features checking for epoll ... found checking for EPOLLRDHUP ... found checking for EPOLLEXCLUSIVE ... found checking for eventfd() ... found checking for O_PATH ... found checking for sendfile() ... found checking for sendfile64() ... found checking for sys/prctl.h ... found checking for prctl(PR_SET_DUMPABLE) ... found checking for prctl(PR_SET_KEEPCAPS) ... found checking for capabilities ... found checking for crypt_r() ... found checking for sys/vfs.h ... found checking for UDP_SEGMENT ... found checking for nobody group ... found checking for poll() ... found checking for /dev/poll ... not found checking for kqueue ... not found checking for crypt() ... not found checking for crypt() in libcrypt ... found checking for F_READAHEAD ... not found checking for posix_fadvise() ... found checking for O_DIRECT ... found checking for F_NOCACHE ... not found checking for directio() ... not found checking for statfs() ... found checking for statvfs() ... found checking for dlopen() ... found checking for sched_yield() ... found checking for sched_setaffinity() ... found checking for SO_SETFIB ... not found checking for SO_REUSEPORT ... found checking for SO_ACCEPTFILTER ... not found checking for SO_BINDANY ... not found checking for IP_TRANSPARENT ... found checking for IP_BINDANY ... not found checking for IP_BIND_ADDRESS_NO_PORT ... found checking for IP_RECVDSTADDR ... not found checking for IP_SENDSRCADDR ... not found checking for IP_PKTINFO ... found checking for IPV6_RECVPKTINFO ... found checking for TCP_DEFER_ACCEPT ... found checking for TCP_KEEPIDLE ... found checking for TCP_FASTOPEN ... found checking for TCP_INFO ... found checking for accept4() ... found checking for int size ... 4 bytes checking for long size ... 8 bytes checking for long long size ... 8 bytes checking for void * size ... 8 bytes checking for uint32_t ... found checking for uint64_t ... found checking for sig_atomic_t ... found checking for sig_atomic_t size ... 4 bytes checking for socklen_t ... found checking for in_addr_t ... found checking for in_port_t ... found checking for rlim_t ... found checking for uintptr_t ... uintptr_t found checking for system byte ordering ... little endian checking for size_t size ... 8 bytes checking for off_t size ... 8 bytes checking for time_t size ... 8 bytes checking for AF_INET6 ... found checking for setproctitle() ... not found checking for pread() ... found checking for pwrite() ... found checking for pwritev() ... found checking for strerrordesc_np() ... found checking for localtime_r() ... found checking for clock_gettime(CLOCK_MONOTONIC) ... found checking for posix_memalign() ... found checking for memalign() ... found checking for mmap(MAP_ANON|MAP_SHARED) ... found checking for mmap("/dev/zero", MAP_SHARED) ... found checking for System V shared memory ... found checking for POSIX semaphores ... found checking for struct msghdr.msg_control ... found checking for ioctl(FIONBIO) ... found checking for ioctl(FIONREAD) ... found checking for struct tm.tm_gmtoff ... found checking for struct dirent.d_namlen ... not found checking for struct dirent.d_type ... found checking for sysconf(_SC_NPROCESSORS_ONLN) ... found checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found checking for openat(), fstatat() ... found checking for getaddrinfo() ... found configuring additional modules adding module in /www/server/nginx/src/ngx_devel_kit + ngx_devel_kit was configured adding module in /www/server/nginx/src/lua_nginx_module checking for LuaJIT library in /usr/local/lib and /usr/local/include/luajit-2.1 (specified by the LUAJIT_LIB and LUAJIT_INC env, with -ldl) ... found checking for LuaJIT 2.x ... found checking for Lua language 5.1 ... found checking for LuaJIT has FFI ... found checking for export symbols by default (-E) ... found checking for export symbols by default (--export-all-symbols) ... not found checking for SO_PASSCRED ... found checking for SA_RESTART ... found checking for malloc_trim ... found checking for pipe2 ... found checking for signalfd ... found checking for execvpe ... found + ngx_http_lua_module was configured adding module in /www/server/nginx/src/ngx_cache_purge + ngx_http_cache_purge_module was configured adding module in /www/server/nginx/src/ngx_http_substitutions_filter_module-master + ngx_http_subs_filter_module was configured adding module in /www/server/nginx/src/nginx-dav-ext-module + ngx_http_dav_ext_module was configured adding module in /usr/local/fastdfs-nginx-module/src + ngx_http_fastdfs_module was configured checking for zlib library ... found checking for libxslt ... found checking for libexslt ... found checking for GD library ... found checking for GD WebP support ... found creating objs/Makefile Configuration summary + using PCRE library: /usr/local/src/pcre-8.43 + using OpenSSL library: /www/server/nginx/src/openssl + using system zlib library nginx path prefix: "/www/server/nginx" nginx binary file: "/www/server/nginx/sbin/nginx" nginx modules path: "/www/server/nginx/modules" nginx configuration prefix: "/www/server/nginx/conf" nginx configuration file: "/www/server/nginx/conf/nginx.conf" nginx pid file: "/www/server/nginx/logs/nginx.pid" nginx error log file: "/www/server/nginx/logs/error.log" nginx http access log file: "/www/server/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" ./configure: warning: the "--with-ipv6" option is deprecated [root@VM-24-11-opencloudos nginx-1.24.0]# make clean rm -rf Makefile objs [root@VM-24-11-opencloudos nginx-1.24.0]# make -j$(nproc) make: *** No targets specified and no makefile found. Stop.
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值