fastwriter.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <limits.h>
  7. #include <errno.h>
  8. #include <pthread.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/time.h>
  12. #include <fcntl.h>
  13. #include "private.h"
  14. #include "default.h"
  15. #include "sysinfo.h"
  16. fastwriter_t *fastwriter_init(const char *fs, fastwriter_flags_t flags) {
  17. fastwriter_t *ctx;
  18. ctx = (fastwriter_t*)malloc(sizeof(fastwriter_t));
  19. if (!ctx) return ctx;
  20. memset(ctx, 0, sizeof(fastwriter_t));
  21. ctx->params.flags = flags;
  22. ctx->api = &fastwriter_default_api;
  23. return ctx;
  24. }
  25. void fastwriter_destroy(fastwriter_t *ctx) {
  26. free(ctx);
  27. }
  28. int fastwriter_set_buffer_size(fastwriter_t *ctx, size_t buffer_size) {
  29. ctx->params.buffer_size = buffer_size;
  30. return 0;
  31. }
  32. static void *fastwriter_writer_thread(void *user);
  33. int fastwriter_open(fastwriter_t *ctx, const char *name, fastwriter_flags_t flags) {
  34. int i;
  35. int err;
  36. int e[4];
  37. ctx->flags = flags | ctx->params.flags;
  38. switch (ctx->params.buffer_size) {
  39. case FASTWRITER_BUFFER_DEFAULT:
  40. ctx->size = FASTWRITER_DEFAULT_BUFFER_SIZE;
  41. break;
  42. case FASTWRITER_BUFFER_MAX:
  43. ctx->size = fastwriter_get_free_memory();
  44. if ((ctx->size - FASTWRITER_RESERVE_MEMORY) < FASTWRITER_DEFAULT_BUFFER_SIZE)
  45. ctx->size = FASTWRITER_DEFAULT_BUFFER_SIZE;
  46. else
  47. ctx->size -= FASTWRITER_RESERVE_MEMORY;
  48. break;
  49. default:
  50. ctx->size = ctx->params.buffer_size;
  51. }
  52. if (ctx->size%FASTWRITER_SYNCIO_ALIGN)
  53. ctx->size += FASTWRITER_SYNCIO_ALIGN - (ctx->size%FASTWRITER_SYNCIO_ALIGN);
  54. err = posix_memalign(&ctx->buffer, FASTWRITER_SYNCIO_ALIGN, ctx->size);
  55. if ((err)||(!ctx->buffer)) {
  56. fastwriter_close(ctx);
  57. return ENOMEM;
  58. }
  59. ctx->err = 0;
  60. ctx->written = 0;
  61. ctx->commited = 0;
  62. ctx->chunked = 0;
  63. ctx->tail = 0;
  64. ctx->head = 0;
  65. ctx->pos = 0;
  66. err = ctx->api->open(ctx, name, ctx->flags);
  67. if (err) {
  68. fastwriter_close(ctx);
  69. return err;
  70. }
  71. e[0] = pthread_mutex_init(&ctx->data_cond_mutex, NULL);
  72. e[1] = pthread_mutex_init(&ctx->space_cond_mutex, NULL);
  73. e[2] = pthread_cond_init(&ctx->data_cond, NULL);
  74. e[3] = pthread_cond_init(&ctx->space_cond, NULL);
  75. if (e[0]|e[1]|e[2]|e[3]) {
  76. if (!e[3]) pthread_cond_destroy(&ctx->space_cond);
  77. if (!e[2]) pthread_cond_destroy(&ctx->data_cond);
  78. if (!e[1]) pthread_mutex_destroy(&ctx->space_cond_mutex);
  79. if (!e[0]) pthread_mutex_destroy(&ctx->data_cond_mutex);
  80. fastwriter_close(ctx);
  81. for (i = 0; i < 4; i++)
  82. if (e[i]) return e[i];
  83. }
  84. ctx->clean_locks = 1;
  85. ctx->run_flag = 1;
  86. err = pthread_create(&ctx->wthread, NULL, &fastwriter_writer_thread, ctx);
  87. if (err) {
  88. ctx->run_flag = 0;
  89. fastwriter_close(ctx);
  90. return err;
  91. }
  92. return 0;
  93. }
  94. int fastwriter_close(fastwriter_t *ctx) {
  95. if ((!ctx->err)&&(ctx->pos != ctx->head))
  96. return EBADFD;
  97. if (ctx->run_flag) {
  98. ctx->run_flag = 0;
  99. pthread_mutex_lock(&ctx->data_cond_mutex);
  100. pthread_cond_broadcast(&ctx->data_cond);
  101. pthread_mutex_unlock(&ctx->data_cond_mutex);
  102. pthread_join(ctx->wthread, NULL);
  103. }
  104. if (ctx->clean_locks) {
  105. pthread_cond_destroy(&ctx->space_cond);
  106. pthread_cond_destroy(&ctx->data_cond);
  107. pthread_mutex_destroy(&ctx->space_cond_mutex);
  108. pthread_mutex_destroy(&ctx->data_cond_mutex);
  109. ctx->clean_locks = 0;
  110. }
  111. ctx->api->close(ctx);
  112. if (ctx->buffer) {
  113. free(ctx->buffer);
  114. ctx->buffer = NULL;
  115. }
  116. return ctx->err;
  117. }
  118. static inline size_t fastwriter_compute_free_space(fastwriter_t *ctx) {
  119. if (ctx->pos < ctx->tail) return ctx->tail - ctx->pos;
  120. return ctx->tail + ctx->size - ctx->pos - 1;
  121. }
  122. int fastwriter_get_stats(fastwriter_t *ctx, fastwriter_stats_t *stats) {
  123. stats->buffer_size = ctx->size;
  124. stats->buffer_used = ctx->size - fastwriter_compute_free_space(ctx);
  125. stats->buffer_max = ctx->max_usage;
  126. stats->commited = ctx->commited;
  127. stats->written = ctx->written;
  128. return 0;
  129. }
  130. static void *fastwriter_writer_thread(void *user) {
  131. int err = 0;
  132. fastwriter_write_flags_t flags;
  133. size_t size;
  134. size_t head;
  135. fastwriter_t *ctx = (fastwriter_t*)user;
  136. while ((ctx->run_flag)||(ctx->head != ctx->tail)) {
  137. if (ctx->head != ctx->tail) {
  138. head = ctx->head;
  139. if (head > ctx->tail) {
  140. size = head - ctx->tail;
  141. flags = FASTWRITER_WRITE_FLAGS_DEFAULT;
  142. } else {
  143. size = ctx->size - ctx->tail;
  144. flags = FASTWRITER_WRITE_FLAG_FORCE;
  145. }
  146. if (!ctx->run_flag)
  147. flags |= FASTWRITER_WRITE_FLAG_FORCE;
  148. err = ctx->api->write(ctx, flags, size, ctx->buffer + ctx->tail, &size);
  149. if (err) {
  150. ctx->err = err;
  151. ctx->run_flag = 0;
  152. pthread_mutex_lock(&ctx->space_cond_mutex);
  153. pthread_cond_broadcast(&ctx->space_cond);
  154. pthread_mutex_unlock(&ctx->space_cond_mutex);
  155. return NULL;
  156. }
  157. if (size > 0) {
  158. ctx->written += size;
  159. size += ctx->tail;
  160. if (size == ctx->size) ctx->tail = 0;
  161. else ctx->tail = size;
  162. pthread_mutex_lock(&ctx->space_cond_mutex);
  163. pthread_cond_broadcast(&ctx->space_cond);
  164. pthread_mutex_unlock(&ctx->space_cond_mutex);
  165. } else {
  166. pthread_mutex_lock(&ctx->data_cond_mutex);
  167. while ((ctx->run_flag)&&(ctx->head == head)) {
  168. pthread_cond_wait(&ctx->data_cond, &ctx->data_cond_mutex);
  169. }
  170. pthread_mutex_unlock(&ctx->data_cond_mutex);
  171. }
  172. } else {
  173. pthread_mutex_lock(&ctx->data_cond_mutex);
  174. while ((ctx->run_flag)&&(ctx->head == ctx->tail)) {
  175. pthread_cond_wait(&ctx->data_cond, &ctx->data_cond_mutex);
  176. }
  177. pthread_mutex_unlock(&ctx->data_cond_mutex);
  178. }
  179. }
  180. return NULL;
  181. }
  182. int fastwriter_push(fastwriter_t *ctx, size_t size, const void *data) {
  183. size_t part1, end;
  184. size_t free = fastwriter_compute_free_space(ctx);
  185. if (free < size) {
  186. ctx->max_usage = ctx->size;
  187. if ((ctx->flags&FASTWRITER_FLAGS_BLOCK)==0)
  188. return EWOULDBLOCK;
  189. pthread_mutex_lock(&ctx->space_cond_mutex);
  190. while ((ctx->run_flag)&&(fastwriter_compute_free_space(ctx) < size)) {
  191. pthread_cond_wait(&ctx->space_cond, &ctx->space_cond_mutex);
  192. }
  193. pthread_mutex_unlock(&ctx->space_cond_mutex);
  194. } else {
  195. end = ctx->size - (free - size);
  196. if (end > ctx->max_usage) ctx->max_usage = end;
  197. }
  198. if (!ctx->run_flag) {
  199. if (ctx->err) return ctx->err;
  200. return EBADFD;
  201. }
  202. if (ctx->pos < ctx->tail) end = ctx->tail;
  203. else end = ctx->size;
  204. part1 = end - ctx->pos;
  205. if (part1 < size) {
  206. // tail < pos (we have checked for free space)
  207. end = size - part1;
  208. memcpy(ctx->buffer + ctx->pos, data, part1);
  209. memcpy(ctx->buffer, data + part1, end);
  210. ctx->pos = end;
  211. } else {
  212. memcpy(ctx->buffer + ctx->pos, data, size);
  213. ctx->pos += size;
  214. if (ctx->pos == ctx->size) ctx->pos = 0;
  215. }
  216. ctx->chunked += size;
  217. return 0;
  218. }
  219. int fastwriter_commit(fastwriter_t *ctx) {
  220. ctx->head = ctx->pos;
  221. pthread_mutex_lock(&ctx->data_cond_mutex);
  222. pthread_cond_broadcast(&ctx->data_cond);
  223. pthread_mutex_unlock(&ctx->data_cond_mutex);
  224. ctx->commited += ctx->chunked;
  225. ctx->chunked = 0;
  226. return 0;
  227. }
  228. int fastwriter_cancel(fastwriter_t *ctx) {
  229. ctx->pos = ctx->head;
  230. ctx->chunked = 0;
  231. return 0;
  232. }
  233. int fastwriter_push_data(fastwriter_t *ctx, size_t size, const void *buf) {
  234. int err;
  235. err = fastwriter_push(ctx, size, buf);
  236. if (err) return err;
  237. err = fastwriter_commit(ctx);
  238. if (err) fastwriter_cancel(ctx);
  239. return err;
  240. }