seqreader.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/time.h>
  8. #include <unistd.h>
  9. #include <dirent.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <libaio.h>
  14. #define FASTWRITER_SYNCIO_ALIGN 512
  15. #define SYNC_MODE
  16. #define AIO_MODE 2
  17. //#define FS_SYNC_MODE
  18. #define EXTRA_BUFFERS 2
  19. #define WRITE_INTERVAL 1
  20. #define RAID_STRIP_SIZE 256
  21. #define RAID_DISKS 8
  22. #define STRIPS_AT_ONCE 2
  23. #define FS_MIN_BLOCK_SIZE (1024 * RAID_STRIP_SIZE)
  24. #define FS_BLOCK_SIZE (1024 * RAID_STRIP_SIZE * RAID_DISKS)
  25. #ifdef AIO_MODE
  26. # define SYNC_MODE
  27. #endif /* AIO_MODE */
  28. #ifdef SYNC_MODE
  29. # define BLOCK_SIZE (1024 * RAID_STRIP_SIZE * RAID_DISKS * STRIPS_AT_ONCE)
  30. #else /* SYNC_MODE */
  31. # define BLOCK_SIZE 16384
  32. #endif /* SYNC_MODE */
  33. #ifdef AIO_MODE
  34. # define BUFSIZE (BLOCK_SIZE * (AIO_MODE + EXTRA_BUFFERS))
  35. #else /* AIO_MODE */
  36. # define BUFSIZE (1024 * RAID_STRIP_SIZE * RAID_DISKS * STRIPS_AT_ONCE)
  37. #endif /* AIO_MODE */
  38. int main(int argc, char *argv[]) {
  39. int err;
  40. size_t SKIP = 1;
  41. DIR *dir;
  42. struct dirent *ent;
  43. struct timeval start, fstart, tv;
  44. size_t us;
  45. size_t files = 0;
  46. size_t total_size = 0;
  47. size_t last_write = 0;
  48. size_t last_size = 0;
  49. size_t skip;
  50. size_t run;
  51. size_t ready;
  52. ssize_t res;
  53. size_t max_size = (size_t)-1;
  54. char *buffer;
  55. long double mcoef = 1000000. / (1024 * 1024);
  56. int flags = O_RDONLY|O_NOATIME|O_LARGEFILE;
  57. struct stat st;
  58. #ifdef AIO_MODE
  59. int i;
  60. size_t curio, schedio;
  61. int done[AIO_MODE + EXTRA_BUFFERS];
  62. io_context_t aio;
  63. struct iocb io[AIO_MODE], *ioptr[AIO_MODE];
  64. int events;
  65. struct io_event ev[AIO_MODE];
  66. #endif /* AIO_MODE */
  67. posix_memalign((void**)&buffer, FASTWRITER_SYNCIO_ALIGN, BUFSIZE);
  68. if (argc < 2) {
  69. printf("Usage: %s <directory|file|device> [skip|size]\n", argv[0]);
  70. exit(0);
  71. }
  72. if (stat(argv[1], &st)) {
  73. printf("stat on (%s) have failed", argv[1]);
  74. exit(-1);
  75. }
  76. if (strstr(argv[1], "/dev/")||(S_ISREG(st.st_mode))) {
  77. if (argc > 2) {
  78. max_size = atol(argv[2]);
  79. max_size *= 1024 * 1024 * 1024;
  80. }
  81. #ifdef SYNC_MODE
  82. flags |= O_DIRECT;
  83. #endif
  84. printf("Used buffer: %i MB, Block: %i KB\n", BUFSIZE / 1024 / 1024, BLOCK_SIZE/1024);
  85. int fd = open(argv[1], flags, 0);
  86. if (fd < 0) {
  87. printf("Unable to open device %s\n", argv[1]);
  88. exit(1);
  89. }
  90. size_t size = BLOCK_SIZE;
  91. #ifdef AIO_MODE
  92. memset(done, 0, sizeof(done));
  93. memset(&aio, 0, sizeof(aio));
  94. io_queue_init(AIO_MODE, &aio);
  95. for (i = 0; i < AIO_MODE; i++) {
  96. ioptr[i] = &io[i];
  97. memset(ioptr[i], 0, sizeof(struct iocb));
  98. io_prep_pread(ioptr[i], fd, buffer + i * BLOCK_SIZE, BLOCK_SIZE, i * BLOCK_SIZE);
  99. io_set_callback(ioptr[i], (void*)(uintptr_t)i);
  100. }
  101. curio = 0;
  102. schedio = AIO_MODE;
  103. events = 0;
  104. #endif /* AIO_MODE */
  105. gettimeofday(&start, NULL);
  106. #ifdef AIO_MODE
  107. err = io_submit(aio, AIO_MODE, ioptr);
  108. if (err != AIO_MODE) {
  109. printf("Failed to submit initial AIO job, io_submit returned %i\n", err);
  110. exit(-1);
  111. }
  112. #endif /* AIO_MODE */
  113. #ifdef AIO_MODE
  114. ready = 0;
  115. while (1) {
  116. if (!done[curio%(AIO_MODE + EXTRA_BUFFERS)]) {
  117. // printf("%i,%i - %i [%i %i %i %i]\n", curio, schedio, events, done[0], done[1], done[2], done[3]);
  118. if (curio < schedio) {
  119. err = io_getevents(aio, 1, AIO_MODE + EXTRA_BUFFERS - events, &ev[events], NULL);
  120. if (err < 0) {
  121. printf("Error waiting for AIO (%i)\n", -err);
  122. exit(-1);
  123. }
  124. } else {
  125. err = 0;
  126. }
  127. if ((!ready)&&(err > 1)) {
  128. printf("*** Multiple read requests (%i of %i) are finished simultaneously. It is either:\n", err, AIO_MODE);
  129. printf(" Small buffer size (%i KB)\n", BLOCK_SIZE/1024);
  130. printf(" More parallel AIOs (%i) than supported by kernel, try %i\n", AIO_MODE, AIO_MODE - err);
  131. }
  132. for (i = 0; i < err; i++) {
  133. struct io_event *ep = &ev[events + i];
  134. int doneio = (uintptr_t)ep->data;
  135. if (ep->res2 || (ep->res != BLOCK_SIZE)) {
  136. printf("Error in async IO\n");
  137. exit(-1);
  138. }
  139. done[doneio%(AIO_MODE + EXTRA_BUFFERS)] = 1;
  140. // printf("done (%i): %i\n", i, doneio);
  141. }
  142. events += err;
  143. for (i = events - 1; (i >= 0)&&((schedio - curio) < (AIO_MODE + EXTRA_BUFFERS)); i--) {
  144. // printf("sched (%i): %i\n", i, schedio);
  145. struct iocb *newio = (struct iocb *)ev[i].obj;
  146. memset(newio, 0, sizeof(struct iocb));
  147. io_prep_pread(newio, fd, buffer + (schedio % (AIO_MODE + EXTRA_BUFFERS)) * BLOCK_SIZE, BLOCK_SIZE, schedio * BLOCK_SIZE);
  148. io_set_callback(newio, (void*)(uintptr_t)schedio);
  149. err = io_submit(aio, 1, &newio);
  150. if (err != 1) {
  151. printf("Failed to submit AIO jobs %i", err);
  152. exit(-1);
  153. }
  154. schedio++;
  155. }
  156. events = i + 1;
  157. if (events) {
  158. printf("*** Unprocessed events (%i), probably not enough buffer space...\n", events);
  159. // printf(" curio (%zu), schedio (%zu)\n", curio, schedio);
  160. }
  161. ready = 1;
  162. continue;
  163. }
  164. done[curio%(AIO_MODE + EXTRA_BUFFERS)] = 0;
  165. curio++;
  166. res = BLOCK_SIZE;
  167. #else /* AIO_MODE */
  168. res = read(fd, buffer, size);
  169. while (res > 0) {
  170. #endif /* AIO_MODE */
  171. if (res != size) {
  172. printf("Incomplete read: %zu bytes read instead of %zu\n", res, size);
  173. exit(-1);
  174. }
  175. total_size += res;
  176. gettimeofday(&tv, NULL);
  177. us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
  178. if ((us - last_write) > WRITE_INTERVAL * 1000000) {
  179. printf("Reading: %s (%lu GB), Measured speed: %zu MB/s, Current speed: %zu MB/s\n", argv[0], total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us), (size_t)(mcoef * (total_size - last_size) / (us - last_write)));
  180. last_write = us;
  181. last_size = total_size;
  182. }
  183. if (total_size > max_size) {
  184. printf("Reading: %s (%lu GB), Measured speed: %zu MB/s\n", argv[0], total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
  185. break;
  186. }
  187. #ifndef AIO_MODE
  188. res = read(fd, buffer, size);
  189. #endif /* AIO_MODE */
  190. }
  191. #ifdef AIO_MODE
  192. io_queue_release(aio);
  193. #endif /* AIO_MODE */
  194. close(fd);
  195. if (res < 0) {
  196. printf("Read failed with errno %i\n", errno);
  197. exit(-1);
  198. }
  199. free(buffer);
  200. return 0;
  201. }
  202. #ifdef FS_SYNC_MODE
  203. flags |= O_DIRECT;
  204. #endif /* FS_SYNC_MODE */
  205. chdir(argv[1]);
  206. if (argc > 2) {
  207. SKIP = atoi(argv[2]);
  208. printf("Skip %zu\n", SKIP);
  209. }
  210. gettimeofday(&start, NULL);
  211. for (run = 0; run < SKIP; run++) {
  212. skip = 0;
  213. dir = opendir(".");
  214. while ((ent = readdir(dir))) {
  215. if (((skip++)%SKIP) != run) continue;
  216. if (stat(ent->d_name, &st)) continue;
  217. if (!S_ISREG(st.st_mode)) continue;
  218. int size = st.st_blksize;
  219. #ifdef F_MODE
  220. FILE *f = fopen(ent->d_name, "r");
  221. if (!f) continue;
  222. #else
  223. int fd = open(ent->d_name, flags, 0);
  224. if (fd < 0) continue;
  225. # ifdef FS_SYNC_MODE
  226. if (size < BLOCK_SIZE) size = BLOCK_SIZE;
  227. # endif /* FS_SYNC_MODE */
  228. if (size < FS_MIN_BLOCK_SIZE) size = FS_BLOCK_SIZE;
  229. #endif
  230. if (!files)
  231. printf("Reading %s, Block: %i KB\n", ent->d_name, size / 1024);
  232. if (size > BUFSIZE) {
  233. printf("Buffer too small\n");
  234. exit(1);
  235. }
  236. size_t last_file_write = 0;
  237. size_t last_file_size = 0;
  238. size_t file_size = 0;
  239. gettimeofday(&fstart, NULL);
  240. #ifdef F_MODE
  241. while (!feof(f)) {
  242. ssize_t ret = fread(buffer, 1, size, f);
  243. #else
  244. while (1) {
  245. ssize_t ret = read(fd, buffer, size);
  246. #endif
  247. if (ret <= 0) break;
  248. file_size += ret;
  249. gettimeofday(&tv, NULL);
  250. us = (tv.tv_sec - fstart.tv_sec) * 1000000 + (tv.tv_usec - fstart.tv_usec);
  251. if ((us - last_file_write) > WRITE_INTERVAL * 1000000) {
  252. printf("Reading: %s (%lu GB), Measured speed: %zu MB/s, Current speed: %zu MB/s\n", ent->d_name, file_size / 1024 / 1024 / 1024, (size_t)(mcoef * file_size / us), (size_t)(mcoef * (file_size - last_file_size) / (us - last_file_write)));
  253. last_file_write = us;
  254. last_file_size = file_size;
  255. }
  256. }
  257. if (!file_size) {
  258. printf("Read failed\n");
  259. exit(1);
  260. }
  261. #ifdef F_MODE
  262. fclose(f);
  263. #else
  264. close(fd);
  265. #endif
  266. total_size += st.st_size;
  267. files++;
  268. gettimeofday(&tv, NULL);
  269. us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
  270. if ((us - last_write) > WRITE_INTERVAL * 1000000) {
  271. last_write = us;
  272. printf("Read: %lu files (%lu GB) at %zu MB/s", files, total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
  273. us = (tv.tv_sec - fstart.tv_sec) * 1000000 + (tv.tv_usec - fstart.tv_usec);
  274. printf(", Last: %s (%lu MB) at %zu MB/s\n", ent->d_name, st.st_size/1024/1024, (size_t)(mcoef * file_size / us));
  275. }
  276. }
  277. closedir(dir);
  278. us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
  279. printf("Total: %lu files (%lu GB) at %zu MB/s\n", files, total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
  280. }
  281. free(buffer);
  282. }