fastwriter.c 7.5 KB

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