Class LibaioContext<Callback extends SubmitInfo>
java.lang.Object
org.apache.activemq.artemis.nativo.jlibaio.LibaioContext<Callback>
- All Implemented Interfaces:
Closeable,AutoCloseable
This class is used as an aggregator for the
It holds native data, and it will share a libaio queue that can be used by multiple files.
You need to use the poll methods to read the result of write and read submissions.
You also need to use the special buffer created by
A Single controller can server multiple files. There's no need to create one controller per file.
Interesting reading for this.
LibaioFile.
It holds native data, and it will share a libaio queue that can be used by multiple files.
You need to use the poll methods to read the result of write and read submissions.
You also need to use the special buffer created by
LibaioFile as you need special alignments
when dealing with O_DIRECT files.
A Single controller can server multiple files. There's no need to create one controller per file.
Interesting reading for this.
-
Constructor Summary
ConstructorsConstructorDescriptionLibaioContext(int queueSize, boolean useSemaphore, boolean useFdatasync) The queue size here will use resources defined on the kernel parameter fs.aio-max-nr . -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()This is used to close the libaio queues and cleanup the native data used.static voidclose(int fd) static voidfreeBuffer(ByteBuffer buffer) This will call posix free to release the inner buffer allocated atnewAlignedBuffer(int, int).static intgetBlockSize(File path) static intgetBlockSize(String path) static longThis is used to validate leaks on tests.static booleanThe system may choose to set this if a failing condition happened inside the code.static booleanisLoaded()static booleanlock(int fd) voidmemsetBuffer(ByteBuffer buffer) It will reset all the positions on the buffer to 0, using memset.static voidmemsetBuffer(ByteBuffer buffer, int size) static ByteBuffernewAlignedBuffer(int size, int alignment) Buffers for O_DIRECT need to use posix_memalign.static intit will return a file descriptor.static LibaioFileopenControlFile(String file, boolean direct) It will open a file disassociated with any sort of factory.It will open a file.It will open a file.voidpoll()It will start polling and will keep doing until the context is closed.intIt will poll the libaio queue for results.static voidThis is used on tests validating for leaks.static voidsetForceSyscall(boolean value) voidsubmitRead(int fd, long position, int size, ByteBuffer bufferWrite, Callback callback) voidsubmitWrite(int fd, long position, int size, ByteBuffer bufferWrite, Callback callback)
-
Constructor Details
-
LibaioContext
public LibaioContext(int queueSize, boolean useSemaphore, boolean useFdatasync) The queue size here will use resources defined on the kernel parameter fs.aio-max-nr .- Parameters:
queueSize- the size to be initialize on libaio io_queue_init which can't be higher than /proc/sys/fs/aio-max-nr.useSemaphore- should block on a semaphore avoiding using more submits than what's available.useFdatasync- should use fdatasync before calling callbacks.
-
-
Method Details
-
isLoaded
public static boolean isLoaded() -
setForceSyscall
public static void setForceSyscall(boolean value) -
isForceSyscall
public static boolean isForceSyscall()The system may choose to set this if a failing condition happened inside the code. -
getTotalMaxIO
public static long getTotalMaxIO()This is used to validate leaks on tests.- Returns:
- the number of allocated aio, to be used on test checks.
-
memsetBuffer
It will reset all the positions on the buffer to 0, using memset.- Parameters:
buffer- a native buffer. s
-
resetMaxAIO
public static void resetMaxAIO()This is used on tests validating for leaks. -
submitWrite
public void submitWrite(int fd, long position, int size, ByteBuffer bufferWrite, Callback callback) throws IOException - Parameters:
fd- the file descriptorposition- the write positionsize- number of bytes to usebufferWrite- the native buffercallback- a callback- Throws:
IOException- in case of error
-
submitRead
public void submitRead(int fd, long position, int size, ByteBuffer bufferWrite, Callback callback) throws IOException - Throws:
IOException
-
close
public void close()This is used to close the libaio queues and cleanup the native data used.
It is unsafe to close the controller while you have pending writes or files open as this could cause core dumps or VM crashes.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
openFile
It will open a file. If you set the direct flag = false then you won't need to use the special buffer. Notice: This will create an empty file if the file doesn't already exist.- Parameters:
file- the file to be open.direct- will set ODIRECT.- Returns:
- It will return a LibaioFile instance.
- Throws:
IOException- in case of error.
-
openFile
It will open a file. If you set the direct flag = false then you won't need to use the special buffer. Notice: This will create an empty file if the file doesn't already exist.- Parameters:
file- the file to be open.direct- should use O_DIRECT when opening the file.- Returns:
- a new open file.
- Throws:
IOException- in case of error.
-
openControlFile
It will open a file disassociated with any sort of factory. This is useful when you won't use reading / writing through libaio like locking files.- Parameters:
file- a file namedirect- will use O_DIRECT- Returns:
- a new file
- Throws:
IOException- in case of error.
-
poll
It will poll the libaio queue for results. It should block until min is reached Results are placed on the callback.
This shouldn't be called concurrently. You should provide your own synchronization if you need more than one Thread polling for any reason.
Notice that the native layer will invokeSubmitInfo.onError(int, String)in case of failures, but it won't call done method for you.- Parameters:
callbacks- area to receive the callbacks passed on submission.The size of this callback has to be greater than the parameter max.min- the minimum number of elements to receive. It will block until this is achieved.max- The maximum number of elements to receive.- Returns:
- Number of callbacks returned.
- See Also:
-
poll
public void poll()It will start polling and will keep doing until the context is closed. This will call callbacks onSubmitInfo.onError(int, String)andSubmitInfo.done(). In case of error, bothSubmitInfo.onError(int, String)andSubmitInfo.done()are called. -
open
it will return a file descriptor.- Parameters:
path- the file name.direct- translates as O_DIRECT On open- Returns:
- a fd from open C call.
-
close
public static void close(int fd) -
newAlignedBuffer
Buffers for O_DIRECT need to use posix_memalign.
Documented atLibaioFile.newBuffer(int).- Parameters:
size- needs to be % alignmentalignment- the alignment used at the dispositive- Returns:
- a new native buffer used with posix_memalign
-
freeBuffer
This will call posix free to release the inner buffer allocated atnewAlignedBuffer(int, int).- Parameters:
buffer- a native buffer allocated withnewAlignedBuffer(int, int).
-
lock
public static boolean lock(int fd) -
memsetBuffer
-
getBlockSize
-
getBlockSize
-