fastwriter.c 7.6 KB

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