default.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #define _FASTWRITER_DEFAULT_C
  2. #define _GNU_SOURCE
  3. #define _XOPEN_SOURCE 600
  4. #define _POSIX_C_SOURCE 200112L
  5. #define _LARGEFILE64_SOURCE
  6. #include "config.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13. #include <pthread.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <sys/time.h>
  17. #include <fcntl.h>
  18. #ifdef HAVE_LINUX_FALLOC_H
  19. # include <linux/falloc.h>
  20. #endif /* HAVE_LINUX_FALLOC_H */
  21. #ifndef DISABLE_XFS_REALTIME
  22. # include <xfs/xfs.h>
  23. #endif /* !DISABLE_XFS_REALTIME */
  24. #include "fastwriter.h"
  25. #include "private.h"
  26. #include "sysinfo.h"
  27. #include "default.h"
  28. #define SYNC_MODE
  29. #define HAVE_FALLOCATE
  30. #define EXT4_WRITEBLOCK 4194304
  31. #define EXT4_PREALLOCATE 1073741824
  32. #define OCFS_WRITEBLOCK 262144
  33. #define AIO_QUEUE_LENGTH 4
  34. #define AIO_BUFFERS 8
  35. #ifndef DISABLE_AIO
  36. # include <libaio.h>
  37. # if AIO_QUEUE_LENGTH > AIO_BUFFERS
  38. # error "AIO_QUEUE_LENGTH > AIO_BUFFERS"
  39. # endif
  40. #endif /* DISABLE_AIO */
  41. #ifndef DISABLE_AIO
  42. typedef struct {
  43. size_t offset;
  44. size_t size;
  45. int ios;
  46. int ready; /**< 0 - unused, 1 - processing, 2 - done */
  47. } fastwriter_data_t;
  48. #endif /* !DISABLE_AIO */
  49. typedef struct {
  50. int fd;
  51. int sync_mode; /**< Open with O_DIRECT flag to avoid caches */
  52. int aio_mode; /**< Use kernel AIO (libaio.h) */
  53. size_t prior_size; /**< original size of file */
  54. size_t preallocated; /**< preallocated bytes */
  55. size_t wr_block; /**< minimal block of data to write */
  56. size_t pa_block; /**< preallocation setp */
  57. #ifndef DISABLE_AIO
  58. io_context_t aio;
  59. int ios_ready_n;
  60. int ios_ready[AIO_QUEUE_LENGTH];
  61. struct iocb ios[AIO_QUEUE_LENGTH];
  62. int data_head, data_tail;
  63. fastwriter_data_t data[AIO_BUFFERS];
  64. int ios_status[AIO_QUEUE_LENGTH];
  65. size_t sched; /**< how far we ahead of currently writted head */
  66. size_t fd_offset; /**< current file offset */
  67. int page_size;
  68. #endif /* !DISABLE_AIO */
  69. } fastwriter_default_t;
  70. int fastwriter_default_open(fastwriter_t *fw, const char *name, fastwriter_flags_t flags) {
  71. int err;
  72. char fs[16];
  73. #ifndef DISABLE_XFS_REALTIME
  74. struct fsxattr attr;
  75. #endif /* !DISABLE_XFS_REALTIME */
  76. int open_flags = (O_CREAT|O_WRONLY|O_NOATIME|O_LARGEFILE);
  77. int open_mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  78. fastwriter_default_t *ctx;
  79. err = fastwriter_get_file_fs(name, sizeof(fs) - 1, fs);
  80. if (err) return err;
  81. ctx = (fastwriter_default_t*)malloc(sizeof(fastwriter_default_t));
  82. if (!ctx) return ENOMEM;
  83. memset(ctx, 0, sizeof(fastwriter_default_t));
  84. fw->ctx = ctx;
  85. #ifdef SYNC_MODE
  86. ctx->sync_mode = 1;
  87. #endif /* SYNC_MODE */
  88. ctx->prior_size = 0;
  89. if (!strcmp(fs, "raw")) {
  90. ctx->wr_block = EXT4_WRITEBLOCK;
  91. ctx->pa_block = 0;
  92. ctx->prior_size = (size_t)-1;
  93. open_flags &= ~(O_CREAT|O_NOATIME|O_LARGEFILE);
  94. } else if (!strcmp(fs, "ext4")) {
  95. ctx->wr_block = EXT4_WRITEBLOCK;
  96. ctx->pa_block = EXT4_PREALLOCATE;
  97. } else if (!strcmp(fs, "btrfs")) {
  98. ctx->wr_block = EXT4_WRITEBLOCK;
  99. ctx->pa_block = EXT4_PREALLOCATE;
  100. } else if (!strcmp(fs, "xfs")) {
  101. ctx->wr_block = EXT4_WRITEBLOCK;
  102. ctx->pa_block = EXT4_PREALLOCATE;
  103. } else if (!strcmp(fs, "ocfs2")) {
  104. #ifndef DISABLE_AIO
  105. ctx->aio_mode = 1;
  106. ctx->sync_mode = 0;
  107. ctx->wr_block = OCFS_WRITEBLOCK;
  108. #else /* !DISABLE_AIO */
  109. ctx->wr_block = EXT4_WRITEBLOCK;
  110. #endif /* !DISABLE_AIO */
  111. ctx->pa_block = EXT4_PREALLOCATE;
  112. } else {
  113. ctx->wr_block = EXT4_WRITEBLOCK;
  114. ctx->pa_block = 0;
  115. }
  116. if (ctx->sync_mode) {
  117. open_flags |= O_DIRECT;
  118. }
  119. if (flags&FASTWRITER_FLAGS_OVERWRITE)
  120. open_flags |= O_TRUNC;
  121. ctx->fd = open(name, open_flags, open_mode);
  122. if (ctx->fd < 0) {
  123. // Running as normal user, try to disable direct mode
  124. if ((errno == EINVAL)&&(ctx->sync_mode)) {
  125. ctx->sync_mode = 0;
  126. open_flags &= ~O_DIRECT;
  127. ctx->fd = open(name, open_flags, open_mode);
  128. }
  129. if (ctx->fd < 0) return errno;
  130. }
  131. if (((open_flags&FASTWRITER_FLAGS_OVERWRITE)==0)&&(strcmp(fs, "raw"))) {
  132. ctx->prior_size = lseek64(ctx->fd, 0, SEEK_END);
  133. if (ctx->prior_size%FASTWRITER_SYNCIO_ALIGN) {
  134. close(ctx->fd);
  135. ctx->fd = open(name, open_flags&~O_DIRECT, open_mode);
  136. if (ctx->fd < 0) return errno;
  137. ctx->prior_size = lseek64(ctx->fd, 0, SEEK_END);
  138. ctx->sync_mode = 0;
  139. ctx->aio_mode = 0;
  140. }
  141. }
  142. #ifndef DISABLE_XFS_REALTIME
  143. if (!strcmp(fs, "xfs")) {
  144. err = xfsctl (name, ctx->fd, XFS_IOC_FSGETXATTR, (void *) &attr);
  145. if (!err) {
  146. attr.fsx_xflags |= XFS_XFLAG_REALTIME;
  147. err = xfsctl (name, ctx->fd, XFS_IOC_FSSETXATTR, (void *) &attr);
  148. if (err) fprintf(stderr, "Error initializing XFS real-time mode (%i), disabling...\n", err);
  149. }
  150. }
  151. #endif /* !DISABLE_XFS_REALTIME */
  152. #ifndef DISABLE_AIO
  153. if (ctx->aio_mode) {
  154. int i;
  155. ctx->page_size = getpagesize();
  156. ctx->fd_offset = ctx->prior_size;
  157. ctx->ios_ready_n = AIO_QUEUE_LENGTH;
  158. for (i = 0; i < AIO_QUEUE_LENGTH; i++) {
  159. ctx->ios_ready[i] = i;
  160. }
  161. err = io_queue_init(AIO_QUEUE_LENGTH, &ctx->aio);
  162. if (err) {
  163. fprintf(stderr, "Error initializing AIO mode (%i), disabling...\n", -err);
  164. ctx->aio_mode = 0;
  165. }
  166. }
  167. #endif /* !DISABLE_AIO */
  168. ctx->preallocated = 0;
  169. return 0;
  170. }
  171. void fastwriter_default_close(fastwriter_t *fw) {
  172. if (fw->ctx) {
  173. fastwriter_default_t *ctx = (fastwriter_default_t*)fw->ctx;
  174. if (ctx->fd >= 0) {
  175. #ifndef DISABLE_AIO
  176. if ((ctx->aio_mode)&&(ctx->aio)) {
  177. int n_ev;
  178. struct io_event ev[AIO_QUEUE_LENGTH];
  179. while (ctx->ios_ready_n < AIO_QUEUE_LENGTH) {
  180. n_ev = io_getevents(ctx->aio, 1, AIO_QUEUE_LENGTH, &ev[0], NULL);
  181. if (n_ev <= 0) {
  182. fprintf(stderr, "AIO io_getevents have failed (%i)", -n_ev);
  183. break;
  184. }
  185. ctx->ios_ready_n += n_ev;
  186. }
  187. io_queue_release(ctx->aio);
  188. }
  189. #endif /* DISABLE_AIO */
  190. #ifdef HAVE_LINUX_FALLOC_H
  191. if (ctx->prior_size != (size_t)-1) {
  192. #else /* HAVE_LINUX_FALLOC_H */
  193. if ((ctx->prior_size != (size_t)-1)&&((ctx->sync_mode)||(ctx->aio_mode))) {
  194. #endif /* HAVE_LINUX_FALLOC_H */
  195. ftruncate(ctx->fd, ctx->prior_size + fw->written);
  196. }
  197. close(ctx->fd);
  198. }
  199. free(ctx);
  200. fw->ctx = NULL;
  201. }
  202. }
  203. int fastwriter_default_write(fastwriter_t *fw, fastwriter_write_flags_t flags, size_t size, void *data, size_t *written) {
  204. size_t sum = 0;
  205. size_t delta = 0;
  206. ssize_t res;
  207. fastwriter_default_t *ctx = (fastwriter_default_t*)fw->ctx;
  208. if ((flags&FASTWRITER_WRITE_FLAG_FORCE)==0) {
  209. if (size < ctx->wr_block) {
  210. *written = 0;
  211. return 0;
  212. }
  213. size -= size % ctx->wr_block;
  214. }
  215. if ((ctx->pa_block)&&((fw->written + size) > ctx->preallocated)) {
  216. #ifdef HAVE_LINUX_FALLOC_H
  217. if (fallocate(ctx->fd, FALLOC_FL_KEEP_SIZE, ctx->preallocated, ctx->pa_block)) {
  218. #else /* HAVE_LINUX_FALLOC_H */
  219. if (posix_fallocate(ctx->fd, ctx->preallocated, ctx->pa_block)) {
  220. #endif /* HAVE_LINUX_FALLOC_H */
  221. ctx->pa_block = 0;
  222. } else {
  223. ctx->preallocated += ctx->pa_block;
  224. }
  225. }
  226. // we expect this to happen only at last iteration (buffer is multiply of the required align)
  227. if (((ctx->aio_mode)||(ctx->sync_mode))&&(size%FASTWRITER_SYNCIO_ALIGN)) {
  228. delta = FASTWRITER_SYNCIO_ALIGN - size%FASTWRITER_SYNCIO_ALIGN;
  229. }
  230. #ifndef DISABLE_AIO
  231. if (ctx->aio_mode) {
  232. int err;
  233. size_t done = 0;
  234. size_t sched = 0;
  235. fastwriter_data_t *iodata;
  236. struct iocb *newio;
  237. size_t wr_block = ctx->wr_block;
  238. do {
  239. if (!ctx->ios_ready_n) {
  240. int i, n_ev;
  241. struct io_event ev[AIO_QUEUE_LENGTH];
  242. n_ev = io_getevents(ctx->aio, 1, AIO_QUEUE_LENGTH, &ev[0], NULL);
  243. if (n_ev <= 0) {
  244. fprintf(stderr, "AIO io_getevents have failed (%i)", -n_ev);
  245. return -n_ev;
  246. }
  247. for (i = 0; i < n_ev; i++) {
  248. fastwriter_data_t *ev_data = (fastwriter_data_t *)(ev[i].data);
  249. if ((ev[i].res2)||(ev[i].res < ev_data->size)) {
  250. fprintf(stderr, "AIO write failed (res: %li, res2: %li, expected: %zu), no handling data will be corrupted...\n", ev[i].res, ev[i].res2, ev_data->size);
  251. return -ev[i].res2;
  252. }
  253. ctx->ios_ready[ctx->ios_ready_n++] = ev_data->ios;
  254. // printf("Data: %i (ios %i)\n", ev_data->ready, ev_data->ios);
  255. ev_data->ready = 2;
  256. }
  257. while (ctx->data[ctx->data_tail].ready > 1) {
  258. // printf("Done: %i %zu\n", ctx->data_tail, ctx->data[ctx->data_tail].offset);
  259. ctx->data[ctx->data_tail].ready = 0;
  260. done += ctx->data[ctx->data_tail].size;
  261. if ((++ctx->data_tail) == AIO_BUFFERS) ctx->data_tail = 0;
  262. }
  263. }
  264. if ((ctx->sched + sched) < size) {
  265. if ((ctx->data_head == ctx->data_tail)&&(ctx->data[ctx->data_head].ready)) continue;
  266. newio = (struct iocb*)&ctx->ios[ctx->ios_ready[--ctx->ios_ready_n]];
  267. iodata = &ctx->data[ctx->data_head];
  268. if (wr_block > ((size + delta) - (ctx->sched + sched))) {
  269. wr_block = (size + delta) - (ctx->sched + sched);
  270. if (wr_block % ctx->page_size) {
  271. fprintf(stderr, "We need to write incomplete page (%zu bytes). This is no supported yet...\n", wr_block);
  272. return -1;
  273. }
  274. }
  275. // printf("Sched: %lu => %lu (%lu) [tail %lu, head %lu]\n", ctx->sched + sched, ctx->fd_offset, wr_block, ctx->data_tail, ctx->data_head);
  276. iodata->offset = ctx->fd_offset;
  277. iodata->size = wr_block;
  278. iodata->ios = ctx->ios_ready_n;
  279. io_prep_pwrite(newio, ctx->fd, data + ctx->sched + sched, wr_block, ctx->fd_offset);
  280. io_set_callback(newio, (void*)iodata);
  281. err = io_submit(ctx->aio, 1, &newio);
  282. if (err != 1) {
  283. fprintf(stderr, "Error submiting AIO job (%i)\n", -err);
  284. return -err;
  285. }
  286. iodata->ready = 1;
  287. sched += wr_block;
  288. ctx->fd_offset += wr_block;
  289. if ((++ctx->data_head) == AIO_BUFFERS) ctx->data_head = 0;
  290. }
  291. } while (!done);
  292. ctx->sched += sched - done;
  293. size = done;
  294. } else {
  295. #endif /* !DISABLE_AIO */
  296. do {
  297. res = write(ctx->fd, data + sum, size + delta - sum);
  298. if (res < 0) {
  299. *written = sum;
  300. return errno;
  301. }
  302. sum += res;
  303. } while (sum < size);
  304. #ifndef DISABLE_AIO
  305. }
  306. #endif /* !DISABLE_AIO */
  307. if ((ctx->sync_mode)||(ctx->aio_mode)) {
  308. posix_fadvise(ctx->fd, fw->written, size, POSIX_FADV_DONTNEED);
  309. }
  310. *written = size;
  311. return 0;
  312. }