fileio_asyncio.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /*
  11. * FileIO AsyncIO exposes read/write IO pools that allow doing IO asynchronously.
  12. * Current implementation relies on having one thread that reads and one that
  13. * writes.
  14. * Each IO pool supports up to `MAX_IO_JOBS` that can be enqueued for work, but
  15. * are performed serially by the appropriate worker thread.
  16. * Most systems exposes better primitives to perform asynchronous IO, such as
  17. * io_uring on newer linux systems. The API is built in such a way that in the
  18. * future we could replace the threads with better solutions when available.
  19. */
  20. #ifndef ZSTD_FILEIO_ASYNCIO_H
  21. #define ZSTD_FILEIO_ASYNCIO_H
  22. #include "../lib/common/mem.h" /* U32, U64 */
  23. #include "fileio_types.h"
  24. #include "platform.h"
  25. #include "util.h"
  26. #include "../lib/common/pool.h"
  27. #include "../lib/common/threading.h"
  28. #define MAX_IO_JOBS (10)
  29. typedef struct {
  30. /* These struct fields should be set only on creation and not changed afterwards */
  31. POOL_ctx* threadPool;
  32. int threadPoolActive;
  33. int totalIoJobs;
  34. const FIO_prefs_t* prefs;
  35. POOL_function poolFunction;
  36. /* Controls the file we currently write to, make changes only by using provided utility functions */
  37. FILE* file;
  38. /* The jobs and availableJobsCount fields are accessed by both the main and worker threads and should
  39. * only be mutated after locking the mutex */
  40. ZSTD_pthread_mutex_t ioJobsMutex;
  41. void* availableJobs[MAX_IO_JOBS];
  42. int availableJobsCount;
  43. size_t jobBufferSize;
  44. } IOPoolCtx_t;
  45. typedef struct {
  46. IOPoolCtx_t base;
  47. /* State regarding the currently read file */
  48. int reachedEof;
  49. U64 nextReadOffset;
  50. U64 waitingOnOffset;
  51. /* We may hold an IOJob object as needed if we actively expose its buffer. */
  52. void *currentJobHeld;
  53. /* Coalesce buffer is used to join two buffers in case where we need to read more bytes than left in
  54. * the first of them. Shouldn't be accessed from outside ot utility functions. */
  55. U8 *coalesceBuffer;
  56. /* Read buffer can be used by consumer code, take care when copying this pointer aside as it might
  57. * change when consuming / refilling buffer. */
  58. U8 *srcBuffer;
  59. size_t srcBufferLoaded;
  60. /* We need to know what tasks completed so we can use their buffers when their time comes.
  61. * Should only be accessed after locking base.ioJobsMutex . */
  62. void* completedJobs[MAX_IO_JOBS];
  63. int completedJobsCount;
  64. ZSTD_pthread_cond_t jobCompletedCond;
  65. } ReadPoolCtx_t;
  66. typedef struct {
  67. IOPoolCtx_t base;
  68. unsigned storedSkips;
  69. } WritePoolCtx_t;
  70. typedef struct {
  71. /* These fields are automatically set and shouldn't be changed by non WritePool code. */
  72. void *ctx;
  73. FILE* file;
  74. void *buffer;
  75. size_t bufferSize;
  76. /* This field should be changed before a job is queued for execution and should contain the number
  77. * of bytes to write from the buffer. */
  78. size_t usedBufferSize;
  79. U64 offset;
  80. } IOJob_t;
  81. /* AIO_supported:
  82. * Returns 1 if AsyncIO is supported on the system, 0 otherwise. */
  83. int AIO_supported(void);
  84. /* AIO_WritePool_releaseIoJob:
  85. * Releases an acquired job back to the pool. Doesn't execute the job. */
  86. void AIO_WritePool_releaseIoJob(IOJob_t *job);
  87. /* AIO_WritePool_acquireJob:
  88. * Returns an available write job to be used for a future write. */
  89. IOJob_t* AIO_WritePool_acquireJob(WritePoolCtx_t *ctx);
  90. /* AIO_WritePool_enqueueAndReacquireWriteJob:
  91. * Enqueues a write job for execution and acquires a new one.
  92. * After execution `job`'s pointed value would change to the newly acquired job.
  93. * Make sure to set `usedBufferSize` to the wanted length before call.
  94. * The queued job shouldn't be used directly after queueing it. */
  95. void AIO_WritePool_enqueueAndReacquireWriteJob(IOJob_t **job);
  96. /* AIO_WritePool_sparseWriteEnd:
  97. * Ends sparse writes to the current file.
  98. * Blocks on completion of all current write jobs before executing. */
  99. void AIO_WritePool_sparseWriteEnd(WritePoolCtx_t *ctx);
  100. /* AIO_WritePool_setFile:
  101. * Sets the destination file for future writes in the pool.
  102. * Requires completion of all queues write jobs and release of all otherwise acquired jobs.
  103. * Also requires ending of sparse write if a previous file was used in sparse mode. */
  104. void AIO_WritePool_setFile(WritePoolCtx_t *ctx, FILE* file);
  105. /* AIO_WritePool_getFile:
  106. * Returns the file the writePool is currently set to write to. */
  107. FILE* AIO_WritePool_getFile(const WritePoolCtx_t* ctx);
  108. /* AIO_WritePool_closeFile:
  109. * Ends sparse write and closes the writePool's current file and sets the file to NULL.
  110. * Requires completion of all queues write jobs and release of all otherwise acquired jobs. */
  111. int AIO_WritePool_closeFile(WritePoolCtx_t *ctx);
  112. /* AIO_WritePool_create:
  113. * Allocates and sets and a new write pool including its included jobs.
  114. * bufferSize should be set to the maximal buffer we want to write to at a time. */
  115. WritePoolCtx_t* AIO_WritePool_create(const FIO_prefs_t* prefs, size_t bufferSize);
  116. /* AIO_WritePool_free:
  117. * Frees and releases a writePool and its resources. Closes destination file. */
  118. void AIO_WritePool_free(WritePoolCtx_t* ctx);
  119. /* AIO_WritePool_setAsync:
  120. * Allows (de)activating async mode, to be used when the expected overhead
  121. * of asyncio costs more than the expected gains. */
  122. void AIO_WritePool_setAsync(WritePoolCtx_t* ctx, int async);
  123. /* AIO_ReadPool_create:
  124. * Allocates and sets and a new readPool including its included jobs.
  125. * bufferSize should be set to the maximal buffer we want to read at a time, will also be used
  126. * as our basic read size. */
  127. ReadPoolCtx_t* AIO_ReadPool_create(const FIO_prefs_t* prefs, size_t bufferSize);
  128. /* AIO_ReadPool_free:
  129. * Frees and releases a readPool and its resources. Closes source file. */
  130. void AIO_ReadPool_free(ReadPoolCtx_t* ctx);
  131. /* AIO_ReadPool_setAsync:
  132. * Allows (de)activating async mode, to be used when the expected overhead
  133. * of asyncio costs more than the expected gains. */
  134. void AIO_ReadPool_setAsync(ReadPoolCtx_t* ctx, int async);
  135. /* AIO_ReadPool_consumeBytes:
  136. * Consumes byes from srcBuffer's beginning and updates srcBufferLoaded accordingly. */
  137. void AIO_ReadPool_consumeBytes(ReadPoolCtx_t *ctx, size_t n);
  138. /* AIO_ReadPool_fillBuffer:
  139. * Makes sure buffer has at least n bytes loaded (as long as n is not bigger than the initialized bufferSize).
  140. * Returns if srcBuffer has at least n bytes loaded or if we've reached the end of the file.
  141. * Return value is the number of bytes added to the buffer.
  142. * Note that srcBuffer might have up to 2 times bufferSize bytes. */
  143. size_t AIO_ReadPool_fillBuffer(ReadPoolCtx_t *ctx, size_t n);
  144. /* AIO_ReadPool_consumeAndRefill:
  145. * Consumes the current buffer and refills it with bufferSize bytes. */
  146. size_t AIO_ReadPool_consumeAndRefill(ReadPoolCtx_t *ctx);
  147. /* AIO_ReadPool_setFile:
  148. * Sets the source file for future read in the pool. Initiates reading immediately if file is not NULL.
  149. * Waits for all current enqueued tasks to complete if a previous file was set. */
  150. void AIO_ReadPool_setFile(ReadPoolCtx_t *ctx, FILE* file);
  151. /* AIO_ReadPool_getFile:
  152. * Returns the current file set for the read pool. */
  153. FILE* AIO_ReadPool_getFile(const ReadPoolCtx_t *ctx);
  154. /* AIO_ReadPool_closeFile:
  155. * Closes the current set file. Waits for all current enqueued tasks to complete and resets state. */
  156. int AIO_ReadPool_closeFile(ReadPoolCtx_t *ctx);
  157. #endif /* ZSTD_FILEIO_ASYNCIO_H */