fwbench.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <glib.h>
  7. #include <math.h>
  8. #include <unistd.h>
  9. #include <sys/stat.h>
  10. #include <sys/time.h>
  11. #include <fastwriter.h>
  12. #include <ufo/ufo-graph.h>
  13. #include "config.h"
  14. #define FW_BUFFER 4096l
  15. #define WRITE_INTERVAL 1
  16. #define WRITE_SUMMARY 5
  17. #define NEWFILE 10
  18. #define run_time 3600
  19. const char *fifo_name = ".fifo";
  20. struct setup_s {
  21. size_t width;
  22. size_t height;
  23. size_t bpp;
  24. size_t fps;
  25. size_t iters;
  26. volatile int run_started;
  27. };
  28. typedef struct setup_s setup_t;
  29. void handle_error(GError *error) {
  30. if (error != NULL) {
  31. g_print("%s\n", error->message);
  32. g_error_free(error);
  33. exit(EXIT_FAILURE);
  34. }
  35. }
  36. void set_speed(setup_t *setup, size_t speed) {
  37. setup->bpp = sizeof(float);
  38. setup->width = 1024;
  39. setup->height = 768;
  40. setup->fps = 1 + speed / setup->width / setup->height / setup->bpp;
  41. }
  42. void set_size(setup_t *setup, size_t size) {
  43. setup->iters = size / setup->width / setup->height / setup->bpp;
  44. }
  45. void set_time(setup_t *setup, size_t time) {
  46. setup->iters = time * setup->fps;
  47. }
  48. static void *run(setup_t *setup) {
  49. GError *error = NULL;
  50. UfoGraph *graph = NULL;
  51. /* If you want to use system-wide installed filters:
  52. * graph = ufo_graph_new(); */
  53. graph = g_object_new(UFO_TYPE_GRAPH,
  54. #ifdef METABALLS_PATH
  55. "paths", METABALLS_PATH,
  56. #endif /* METABALLS_PATH */
  57. NULL);
  58. // printf("%lu %lu %lu %lu\n", setup->width, setup->height, setup->iters, setup->width * setup->height * setup->iters * sizeof(float));
  59. UfoFilter *metaballs = ufo_graph_get_filter(graph, "metaballs", &error);
  60. handle_error(error);
  61. g_object_set(G_OBJECT(metaballs),
  62. "width", setup->width,
  63. "height", setup->height,
  64. "num-balls", 1,
  65. "num-iterations", setup->iters,
  66. "frames-per-second", setup->fps,
  67. NULL);
  68. UfoFilter *writer = ufo_graph_get_filter(graph, "pipeoutput", &error);
  69. handle_error(error);
  70. g_object_set(G_OBJECT(writer),
  71. "pipe-name", fifo_name,
  72. NULL);
  73. ufo_filter_connect_to(metaballs, writer, &error);
  74. handle_error(error);
  75. setup->run_started = 1;
  76. ufo_graph_run(graph, &error);
  77. handle_error(error);
  78. // g_print("Wrote %lu bytes\n", width * height * num_iterations * sizeof(float));
  79. g_thread_exit(NULL);
  80. return NULL;
  81. }
  82. int main(int argc, char const* argv[])
  83. {
  84. int err;
  85. GError *gerr;
  86. GThread *thr;
  87. setup_t setup;
  88. fastwriter_t *fw;
  89. fastwriter_stats_t stats;
  90. const char *out = "/dev/null";
  91. size_t speed = 850;
  92. size_t num_read = 0;
  93. size_t buf_max = 0;
  94. int broken_frame = 0;
  95. size_t writeouts = 0;
  96. size_t duration, last_duration;
  97. struct timeval tv;
  98. struct timeval tv_started = {0};
  99. struct timeval tv_last_written = {0};
  100. unsigned long frames = 0, lost = 0;
  101. unsigned long delta, expected;
  102. unsigned long last_frames = 0, last_lost = 0;
  103. g_thread_init(NULL);
  104. g_type_init();
  105. if (argc > 1) {
  106. out = argv[1];
  107. }
  108. if (argc > 2) {
  109. speed = atoi(argv[2]);
  110. }
  111. unlink(fifo_name);
  112. g_assert(!mkfifo(fifo_name, S_IWUSR | S_IRUSR));
  113. fw = fastwriter_init(out, FASTWRITER_FLAGS_OVERWRITE);
  114. g_assert(fw);
  115. err = fastwriter_open(fw, out, FASTWRITER_FLAGS_OVERWRITE);
  116. if (err) printf("FastWriter returned error %i\n", err);
  117. g_assert(!err);
  118. fastwriter_set_buffer_size(fw, FW_BUFFER * 1024 * 1024);
  119. fastwriter_get_stats(fw, &stats);
  120. printf("*** Writing to %s, speed: %lu MB/s, buffer: %lu MB\n", out, speed, stats.buffer_size);
  121. memset(&setup, 0, sizeof(setup_t));
  122. set_speed(&setup, speed * 1024 * 1024);
  123. set_size(&setup, run_time * speed * 1024 * 1024);
  124. size_t frame_size = setup.width * setup.height * setup.bpp;
  125. void *buffer = malloc(frame_size);
  126. g_assert(buffer);
  127. thr = g_thread_create((GThreadFunc)run, &setup, 1, &gerr);
  128. g_assert(thr);
  129. while (!setup.run_started);
  130. int fd = open(fifo_name, O_RDONLY);
  131. g_assert(fd);
  132. ssize_t result = read(fd, buffer, frame_size);
  133. while (result > 0) {
  134. if (!broken_frame) {
  135. err = fastwriter_push(fw, result, buffer);
  136. if (err) {
  137. if (err == EWOULDBLOCK) {
  138. if (num_read) fastwriter_cancel(fw);
  139. broken_frame = 1;
  140. } else {
  141. if (err) printf("FastWriter returned error %i\n", err);
  142. g_assert(!err);
  143. }
  144. }
  145. }
  146. num_read += result;
  147. if (num_read < frame_size) {
  148. result = read(fd, buffer, frame_size - num_read);
  149. continue;
  150. }
  151. gettimeofday(&tv, NULL);
  152. if (!tv_started.tv_sec) {
  153. memcpy(&tv_started, &tv, sizeof(struct timeval));
  154. if (tv_started.tv_usec >= (1000000 / setup.fps))
  155. tv_started.tv_usec -= (1000000 / setup.fps);
  156. else {
  157. tv_started.tv_sec--;
  158. tv_started.tv_usec += 1000000 - (1000000 / setup.fps);
  159. }
  160. memcpy(&tv_last_written, &tv_started, sizeof(struct timeval));
  161. }
  162. num_read = 0;
  163. if (broken_frame) {
  164. lost++;
  165. } else {
  166. err = fastwriter_commit(fw);
  167. frames++;
  168. }
  169. fastwriter_get_stats(fw, &stats);
  170. if (stats.buffer_used > buf_max) buf_max = stats.buffer_used;
  171. if ((tv.tv_sec - tv_last_written.tv_sec) >= WRITE_INTERVAL) {
  172. last_duration = (tv.tv_sec - tv_last_written.tv_sec) * 1000000 + (tv.tv_usec - tv_last_written.tv_usec);
  173. /* expected = (tv.tv_sec - tv_last_summary.tv_sec) * setup.fps + (tv.tv_usec - tv_last_summary.tv_usec) * setup.fps / 1000000;
  174. delta = expected - lost - frames;
  175. if (delta > 0) delta--;*/
  176. printf("Lost %6.2lf%% (% 8lu of % 8lu), %9.3lf GB at %8.3lf MB/s, buf:%6.2lf%%\n", 100.*(lost - last_lost) / (lost + frames - last_lost - last_frames), lost - last_lost, lost + frames - (last_lost + last_frames), 1. * frame_size * (frames - last_frames) / 1024 / 1024 / 1024, 1000000. * frame_size * (frames - last_frames) / last_duration / 1024 / 1024, 100.*buf_max/stats.buffer_size);
  177. if (((++writeouts)%WRITE_SUMMARY)==0) {
  178. duration = (tv.tv_sec - tv_started.tv_sec) * 1000000 + (tv.tv_usec - tv_started.tv_usec);
  179. expected = (tv.tv_sec - tv_started.tv_sec) * setup.fps + round(1.*(tv.tv_usec - tv_started.tv_usec)*setup.fps/1000000);
  180. delta = expected - lost - frames;
  181. if ((delta > 1)||(delta < 1))
  182. printf(" *** Unexpected frame rate: %.2lf (%lu), delta: %li (%lu, %lu)\n", 1000000. * (frames + lost) / duration, setup.fps, delta, lost + frames, expected);
  183. printf("Total %6.2lf%% (% 8lu of % 8lu), %9.3lf GB at %8.3lf MB/s, buf:%6.2lf%%\n", 100.*lost / (lost + frames), lost, lost + frames, 1. * frames * frame_size / 1024 / 1024 / 1024, 1000000. * frames * frame_size / duration / 1024 / 1024, 100.*stats.buffer_max / stats.buffer_size);
  184. }
  185. buf_max = 0;
  186. memcpy(&tv_last_written, &tv, sizeof(struct timeval));
  187. last_frames = frames;
  188. last_lost = lost;
  189. }
  190. result = read(fd, buffer, frame_size);
  191. }
  192. printf("Wrote %lu GB\n", frame_size * frames / 1024 / 1024 / 1024);
  193. g_thread_join(thr);
  194. close(fd);
  195. free(buffer);
  196. fastwriter_close(fw);
  197. fastwriter_destroy(fw);
  198. unlink(fifo_name);
  199. return 0;
  200. }