Class IgniteNativeIoLib


  • public class IgniteNativeIoLib
    extends Object
    Native IO library based on *nix C library, enabled for Linux, kernel version >= 2.4.10.

    Uses JNA library (https://github.com/java-native-access/jna) to access native calls.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_OPEN_MODE
      Default access mask for newly created files.
      static int E_INVAL
      Invalid argument.
      static int O_CREAT
      File shall be created.
      static int O_DIRECT
      Try to minimize cache effects of the I/O to and from this file.
      static int O_RDONLY
      Open for reading only.
      static int O_RDWR
      Open for reading and writing.
      static int O_SYNC
      Write operations on the file will complete according to the requirements of synchronized I/O file integrity completion.
      static int O_TRUNC
      If the file exists and is a regular file length shall be truncated to 0.
      static int O_WRONLY
      Open for writing only.
      static int POSIX_FADV_DONTNEED
      The specified data will not be accessed in the near future.
      static int S_IRGRP
      Flag for newly created files: group has read permission.
      static int S_IROTH
      Flag for newly created files: others have read permission.
      static int S_IRUSR
      Flag for newly created files: user has read permission.
      static int S_IWUSR
      Flag for newly created files: user has write permission.
      static int SEEK_CUR
      Seek option: change file position to offset
      static int SEEK_SET
      Seek option: set file offset to offset
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int close​(int fd)
      See "man 2 close".
      static int fdatasync​(int fd)
      Synchronize a file's in-core state with storage device.
      static void free​(com.sun.jna.Pointer ptr)
      Frees the memory space pointed to by ptr, which must have been returned by a previous call to native allocation methods.
      static int fsync​(int fd)
      Synchronize a file's in-core state with storage device.
      static int ftruncate​(int fd, long len)
      Causes regular file referenced by fd to be truncated to a size of precisely length bytes.
      static int getDirectIOBlockSize​(String storageDir, IgniteLogger log)
      Determines FS and OS block size.
      static int getpagesize()
      The function getpagesize() returns the number of bytes in a memory page, where "page" is a fixed-length block, the unit for memory allocation and file mapping
      static boolean isJnaAvailable()  
      static long lseek​(int fd, long off, int whence)
      Repositions the file offset of the open file description associated with the file descriptor fd to the argument offset according to the directive whence
      static int open​(String pathname, int flags, int mode)
      Open a file.
      static com.sun.jna.NativeLong pathconf​(String path, int name)
      Return path (FS) configuration parameter value.
      static int posix_fadvise​(int fd, long off, long len, int flag)
      Allows to announce an intention to access file data in a specific pattern in the future, thus allowing the kernel to perform appropriate optimizations.
      static int posix_memalign​(com.sun.jna.ptr.PointerByReference memptr, com.sun.jna.NativeLong alignment, com.sun.jna.NativeLong size)
      Allocates size bytes and places the address of the allocated memory in memptr.
      static com.sun.jna.NativeLong pread​(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt, com.sun.jna.NativeLong off)
      Reads up to cnt bytes from file descriptor fd at offset off (from the start of the file) into the buffer starting at buf.
      static com.sun.jna.NativeLong pwrite​(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt, com.sun.jna.NativeLong off)
      Writes up to cnt bytes to the buffer starting at buf to the file descriptor fd at offset offset.
      static com.sun.jna.NativeLong read​(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt)
      Reads up to cnt bytes from file descriptor fd into the buffer starting at buf.
      static String strerror​(int errnum)
      Function returns a string that describes the error code passed in the argument errnum.
      static com.sun.jna.NativeLong write​(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt)
      Writes up to cnt bytes to the buffer starting at buf to the file descriptor fd.
    • Field Detail

      • O_CREAT

        public static final int O_CREAT
        File shall be created. If the file exists, this flag has no effect.
        See Also:
        Constant Field Values
      • O_TRUNC

        public static final int O_TRUNC
        If the file exists and is a regular file length shall be truncated to 0.
        See Also:
        Constant Field Values
      • O_DIRECT

        public static final int O_DIRECT
        Try to minimize cache effects of the I/O to and from this file.
        See Also:
        Constant Field Values
      • O_SYNC

        public static final int O_SYNC
        Write operations on the file will complete according to the requirements of synchronized I/O file integrity completion. By the time write(2) (or similar) returns, the output data and associated file metadata have been transferred to the underlying hardware.
        See Also:
        Constant Field Values
      • POSIX_FADV_DONTNEED

        public static final int POSIX_FADV_DONTNEED
        The specified data will not be accessed in the near future. See fadvise.h and "man 2 posix_fadvise".
        See Also:
        Constant Field Values
      • S_IRUSR

        public static final int S_IRUSR
        Flag for newly created files: user has read permission.
        See Also:
        Constant Field Values
      • S_IWUSR

        public static final int S_IWUSR
        Flag for newly created files: user has write permission.
        See Also:
        Constant Field Values
      • S_IRGRP

        public static final int S_IRGRP
        Flag for newly created files: group has read permission.
        See Also:
        Constant Field Values
      • S_IROTH

        public static final int S_IROTH
        Flag for newly created files: others have read permission.
        See Also:
        Constant Field Values
      • DEFAULT_OPEN_MODE

        public static final int DEFAULT_OPEN_MODE
        Default access mask for newly created files.
        See Also:
        Constant Field Values
      • SEEK_SET

        public static final int SEEK_SET
        Seek option: set file offset to offset
        See Also:
        Constant Field Values
      • SEEK_CUR

        public static final int SEEK_CUR
        Seek option: change file position to offset
        See Also:
        Constant Field Values
    • Constructor Detail

      • IgniteNativeIoLib

        public IgniteNativeIoLib()
    • Method Detail

      • getDirectIOBlockSize

        public static int getDirectIOBlockSize​(String storageDir,
                                               IgniteLogger log)
        Determines FS and OS block size. Returns file system block size for use with storageDir see "man 3 posix_memalign"
        Parameters:
        storageDir - storage path, base path to check (FS) configuration parameters.
        log - Logger.
        Returns:
        • FS block size to be used in Direct IO and memory alignments.
        • or -1 Operating System is not applicable for enabling Direct IO.
        • and -1 if failed to determine block size.
        • and -1 if JNA is not available or init failed.
      • isJnaAvailable

        public static boolean isJnaAvailable()
        Returns:
        Flag indicating JNA library available and initialized. Always false for non linux systems.
      • open

        public static int open​(String pathname,
                               int flags,
                               int mode)
        Open a file. See "man 3 open".
        Parameters:
        pathname - pathname naming the file.
        flags - flag/open options. Flags are constructed by a bitwise-inclusive OR of flags.
        mode - create file mode creation mask.
        Returns:
        file descriptor.
      • close

        public static int close​(int fd)
        See "man 2 close".
        Parameters:
        fd - The file descriptor of the file to close.
        Returns:
        0 on success, -1 on error.
      • pwrite

        public static com.sun.jna.NativeLong pwrite​(int fd,
                                                    com.sun.jna.Pointer buf,
                                                    com.sun.jna.NativeLong cnt,
                                                    com.sun.jna.NativeLong off)
        Writes up to cnt bytes to the buffer starting at buf to the file descriptor fd at offset offset. The file offset is not changed. See "man 2 pwrite".
        Parameters:
        fd - file descriptor.
        buf - pointer to buffer with data.
        cnt - bytes to write.
        off - position in file to write data.
        Returns:
        the number of bytes written. Note that is not an error for a successful call to transfer fewer bytes than requested.
      • write

        public static com.sun.jna.NativeLong write​(int fd,
                                                   com.sun.jna.Pointer buf,
                                                   com.sun.jna.NativeLong cnt)
        Writes up to cnt bytes to the buffer starting at buf to the file descriptor fd. The file offset is changed. See "man 2 write".
        Parameters:
        fd - file descriptor.
        buf - pointer to buffer with data.
        cnt - bytes to write.
        Returns:
        the number of bytes written. Note that is not an error for a successful call to transfer fewer bytes than requested.
      • pread

        public static com.sun.jna.NativeLong pread​(int fd,
                                                   com.sun.jna.Pointer buf,
                                                   com.sun.jna.NativeLong cnt,
                                                   com.sun.jna.NativeLong off)
        Reads up to cnt bytes from file descriptor fd at offset off (from the start of the file) into the buffer starting at buf. The file offset is not changed. See "man 2 pread".
        Parameters:
        fd - file descriptor.
        buf - pointer to buffer to place the data.
        cnt - bytes to read.
        Returns:
        On success, the number of bytes read is returned (zero indicates end of file), on error, -1 is returned, and errno is set appropriately.
      • read

        public static com.sun.jna.NativeLong read​(int fd,
                                                  com.sun.jna.Pointer buf,
                                                  com.sun.jna.NativeLong cnt)
        Reads up to cnt bytes from file descriptor fd into the buffer starting at buf. The file offset is changed. See "man 2 read".
        Parameters:
        fd - file descriptor.
        buf - pointer to buffer to place the data.
        cnt - bytes to read.
        Returns:
        On success, the number of bytes read is returned (zero indicates end of file), on error, -1 is returned, and errno is set appropriately.
      • fsync

        public static int fsync​(int fd)
        Synchronize a file's in-core state with storage device. See "man 2 fsync".
        Parameters:
        fd - file descriptor.
        Returns:
        On success return zero. On error, -1 is returned, and errno is set appropriately.
      • fdatasync

        public static int fdatasync​(int fd)
        Synchronize a file's in-core state with storage device. See "man 2 fsync". Similar to fsync(int), but does not flush modified metadata unless that metadata is needed in order to allow a subsequent data retrieval to be correctly handled.
        Parameters:
        fd - file descriptor.
        Returns:
        On success return zero. On error, -1 is returned, and errno is set appropriately.
      • posix_memalign

        public static int posix_memalign​(com.sun.jna.ptr.PointerByReference memptr,
                                         com.sun.jna.NativeLong alignment,
                                         com.sun.jna.NativeLong size)
        Allocates size bytes and places the address of the allocated memory in memptr. The address of the allocated memory will be a multiple of alignment. See "man 3 posix_memalign".
        Parameters:
        memptr - out memory pointer.
        alignment - memory alignment, must be a power of two and a multiple of sizeof(void *).
        size - size of buffer.
        Returns:
        returns zero on success, or one of the error values.
      • strerror

        public static String strerror​(int errnum)
        Function returns a string that describes the error code passed in the argument errnum. See "man 3 strerror".
        Parameters:
        errnum - error code.
        Returns:
        displayable error information.
      • pathconf

        public static com.sun.jna.NativeLong pathconf​(String path,
                                                      int name)
        Return path (FS) configuration parameter value.
        Helps to determine alignment restrictions, for example, on buffers used for direct block device I/O.
        POSIX specifies the pathconf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed.
        Parameters:
        path - base path to check settings.
        name - variable name to query.
      • getpagesize

        public static int getpagesize()
        The function getpagesize() returns the number of bytes in a memory page, where "page" is a fixed-length block, the unit for memory allocation and file mapping
      • posix_fadvise

        public static int posix_fadvise​(int fd,
                                        long off,
                                        long len,
                                        int flag)
        Allows to announce an intention to access file data in a specific pattern in the future, thus allowing the kernel to perform appropriate optimizations. The advice applies to a (not necessarily existent) region starting at off and extending for len bytes (or until the end of the file if len is 0) within the file referred to by fd. See "man 2 posix_fadvise".
        Parameters:
        fd - file descriptor.
        off - region start.
        len - region end.
        flag - advice (option) to apply.
        Returns:
        On success, zero is returned. On error, an error number is returned.
      • ftruncate

        public static int ftruncate​(int fd,
                                    long len)
        Causes regular file referenced by fd to be truncated to a size of precisely length bytes. If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0'). The file offset is not changed.
        Parameters:
        fd - file descriptor.
        len - required length.
        Returns:
        On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
      • lseek

        public static long lseek​(int fd,
                                 long off,
                                 int whence)
        Repositions the file offset of the open file description associated with the file descriptor fd to the argument offset according to the directive whence
        Parameters:
        fd - file descriptor.
        off - required position offset.
        whence - position base.
        Returns:
        On error, the value -1 is returned and errno is set to indicate the error.