fastwriter.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 = 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. ctx->buffer = malloc(ctx->size);
  53. if (!ctx->buffer) {
  54. fastwriter_close(ctx);
  55. return ENOMEM;
  56. }
  57. ctx->err = 0;
  58. ctx->written = 0;
  59. ctx->commited = 0;
  60. ctx->chunked = 0;
  61. ctx->tail = 0;
  62. ctx->head = 0;
  63. ctx->pos = 0;
  64. err = ctx->api->open(ctx, name, ctx->flags);
  65. if (err) {
  66. fastwriter_close(ctx);
  67. return err;
  68. }
  69. e[0] = pthread_mutex_init(&ctx->data_cond_mutex, NULL);
  70. e[1] = pthread_mutex_init(&ctx->space_cond_mutex, NULL);
  71. e[2] = pthread_cond_init(&ctx->data_cond, NULL);
  72. e[3] = pthread_cond_init(&ctx->space_cond, NULL);
  73. if (e[0]|e[1]|e[2]|e[3]) {
  74. if (!e[3]) pthread_cond_destroy(&ctx->space_cond);
  75. if (!e[2]) pthread_cond_destroy(&ctx->data_cond);
  76. if (!e[1]) pthread_mutex_destroy(&ctx->space_cond_mutex);
  77. if (!e[0]) pthread_mutex_destroy(&ctx->data_cond_mutex);
  78. fastwriter_close(ctx);
  79. for (i = 0; i < 4; i++)
  80. if (e[i]) return e[i];
  81. }
  82. ctx->clean_locks = 1;
  83. ctx->run_flag = 1;
  84. err = pthread_create(&ctx->wthread, NULL, &fastwriter_writer_thread, ctx);
  85. if (err) {
  86. ctx->run_flag = 0;
  87. fastwriter_close(ctx);
  88. return err;
  89. }
  90. return 0;
  91. }
  92. int fastwriter_close(fastwriter_t *ctx) {
  93. if ((!ctx->err)&&(ctx->pos != ctx->head))
  94. return EBADFD;
  95. if (ctx->run_flag) {
  96. ctx->run_flag = 0;
  97. pthread_mutex_lock(&ctx->data_cond_mutex);
  98. pthread_cond_broadcast(&ctx->data_cond);
  99. pthread_mutex_unlock(&ctx->data_cond_mutex);
  100. pthread_join(ctx->wthread, NULL);
  101. }
  102. if (ctx->clean_locks) {
  103. pthread_cond_destroy(&ctx->space_cond);
  104. pthread_cond_destroy(&ctx->data_cond);
  105. pthread_mutex_destroy(&ctx->space_cond_mutex);
  106. pthread_mutex_destroy(&ctx->data_cond_mutex);
  107. ctx->clean_locks = 0;
  108. }
  109. ctx->api->close(ctx);
  110. if (ctx->buffer) {
  111. free(ctx->buffer);
  112. ctx->buffer = NULL;
  113. }
  114. return 0;
  115. }
  116. static inline size_t fastwriter_compute_free_space(fastwriter_t *ctx) {
  117. if (ctx->pos < ctx->tail) return ctx->tail - ctx->pos;
  118. return ctx->tail + ctx->size - ctx->pos - 1;
  119. }
  120. int fastwriter_get_stats(fastwriter_t *ctx, fastwriter_stats_t *stats) {
  121. stats->buffer_size = ctx->size;
  122. stats->buffer_used = ctx->size - fastwriter_compute_free_space(ctx);
  123. stats->buffer_max = ctx->max_usage;
  124. stats->commited = ctx->commited;
  125. stats->written = ctx->written;
  126. return 0;
  127. }
  128. static void *fastwriter_writer_thread(void *user) {
  129. int err = 0;
  130. fastwriter_write_flags_t flags;
  131. size_t size;
  132. size_t head;
  133. fastwriter_t *ctx = (fastwriter_t*)user;
  134. while ((ctx->run_flag)||(ctx->head != ctx->tail)) {
  135. if (ctx->head != ctx->tail) {
  136. head = ctx->head;
  137. if (head > ctx->tail) {
  138. size = head - ctx->tail;
  139. flags = FASTWRITER_WRITE_FLAGS_DEFAULT;
  140. } else {
  141. size = ctx->size - ctx->tail;
  142. flags = FASTWRITER_WRITE_FLAG_FORCE;
  143. }
  144. if (!ctx->run_flag)
  145. flags |= FASTWRITER_WRITE_FLAG_FORCE;
  146. err = ctx->api->write(ctx, flags, size, ctx->buffer + ctx->tail, &size);
  147. if (err) {
  148. ctx->err = err;
  149. ctx->run_flag = 0;
  150. pthread_mutex_lock(&ctx->space_cond_mutex);
  151. pthread_cond_broadcast(&ctx->space_cond);
  152. pthread_mutex_unlock(&ctx->space_cond_mutex);
  153. return NULL;
  154. }
  155. if (size > 0) {
  156. ctx->written += size;
  157. size += ctx->tail;
  158. if (size == ctx->size) ctx->tail = 0;
  159. else ctx->tail = size;
  160. pthread_mutex_lock(&ctx->space_cond_mutex);
  161. pthread_cond_broadcast(&ctx->space_cond);
  162. pthread_mutex_unlock(&ctx->space_cond_mutex);
  163. } else {
  164. pthread_mutex_lock(&ctx->data_cond_mutex);
  165. while ((ctx->run_flag)&&(ctx->head == head)) {
  166. pthread_cond_wait(&ctx->data_cond, &ctx->data_cond_mutex);
  167. }
  168. }
  169. } else {
  170. pthread_mutex_lock(&ctx->data_cond_mutex);
  171. while ((ctx->run_flag)&&(ctx->head == ctx->tail)) {
  172. pthread_cond_wait(&ctx->data_cond, &ctx->data_cond_mutex);
  173. }
  174. }
  175. }
  176. return NULL;
  177. }
  178. int fastwriter_push_chunk(fastwriter_t *ctx, size_t size, const void *data) {
  179. size_t part1, end;
  180. size_t free = fastwriter_compute_free_space(ctx);
  181. if (free < size) {
  182. ctx->max_usage = ctx->size;
  183. if ((ctx->flags&FASTWRITER_FLAGS_BLOCK)==0)
  184. return EWOULDBLOCK;
  185. pthread_mutex_lock(&ctx->space_cond_mutex);
  186. while ((ctx->run_flag)&&(fastwriter_compute_free_space(ctx) < size)) {
  187. pthread_cond_wait(&ctx->space_cond, &ctx->space_cond_mutex);
  188. }
  189. pthread_mutex_unlock(&ctx->space_cond_mutex);
  190. } else {
  191. end = ctx->size - (free - size);
  192. if (end > ctx->max_usage) ctx->max_usage = end;
  193. }
  194. if (!ctx->run_flag) {
  195. if (ctx->err) return ctx->err;
  196. return EBADFD;
  197. }
  198. if (ctx->pos < ctx->tail) end = ctx->tail;
  199. else end = ctx->size;
  200. part1 = end - ctx->pos;
  201. if (part1 > size) {
  202. // tail < pos (we have checked for free space)
  203. end = size - part1;
  204. memcpy(ctx->buffer + ctx->pos, data, part1);
  205. memcpy(ctx->buffer, data + part1, end);
  206. ctx->pos = end;
  207. } else {
  208. memcpy(ctx->buffer + ctx->pos, data, size);
  209. ctx->pos += size;
  210. if (ctx->pos == ctx->size) ctx->pos = 0;
  211. }
  212. ctx->chunked += size;
  213. return 0;
  214. }
  215. int fastwriter_commit(fastwriter_t *ctx) {
  216. ctx->head = ctx->pos;
  217. pthread_mutex_lock(&ctx->data_cond_mutex);
  218. pthread_cond_broadcast(&ctx->data_cond);
  219. pthread_mutex_unlock(&ctx->data_cond_mutex);
  220. ctx->commited += ctx->chunked;
  221. ctx->chunked = 0;
  222. return 0;
  223. }
  224. int fastwriter_cancel(fastwriter_t *ctx) {
  225. ctx->pos = ctx->head;
  226. ctx->chunked = 0;
  227. return 0;
  228. }
  229. int fastwriter_push_data(fastwriter_t *ctx, size_t size, const void *buf) {
  230. int err;
  231. err = fastwriter_push_chunk(ctx, size, buf);
  232. if (err) return err;
  233. err = fastwriter_commit(ctx);
  234. if (err) fastwriter_cancel(ctx);
  235. return err;
  236. }