2 * An experiment with parallel reading and writing of files using ASIO.
15 #define DIRECT O_DIRECT
17 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
18 #define P_UPDATE(cc) do { \
20 if (cnt >= cnt_rep) { cnt_ms += get_timer(); \
21 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
22 fflush(stdout); cnt_rep += 1<<26; } } while(0)
23 #define P_FINAL do { \
24 cnt_ms += get_timer(); \
25 log(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
28 static struct asio_queue io_queue;
30 int main(int argc, char **argv)
33 uns files = atol(argv[1]);
34 uns bufsize = atol(argv[2]) * 1024; // Kbytes
35 u64 total_size = (u64)atol(argv[3]) * 1024*1024*1024; // Gbytes
43 io_queue.buffer_size = bufsize;
44 io_queue.max_writebacks = 2;
45 asio_init_queue(&io_queue);
48 log(L_INFO, "Creating input file");
49 int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
51 ASSERT(!(total_size % bufsize));
53 for (uns i=0; i<total_size/bufsize; i++)
55 struct asio_request *r = asio_get(&io_queue);
56 r->op = ASIO_WRITE_BACK;
59 byte *xbuf = r->buffer;
60 for (uns j=0; j<bufsize; j++)
66 lseek(in_fd, 0, SEEK_SET);
71 log(L_INFO, "Initializing output files");
72 for (uns i=0; i<files; i++)
74 sprintf(name[i], "tmp/ft-%d", i);
75 fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
77 die("Cannot create %s: %m", name[i]);
82 log(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
84 for (uns round=0; round<total_size/bufsize/files; round++)
86 for (uns i=0; i<files; i++)
88 struct asio_request *r = asio_get(&io_queue);
94 struct asio_request *rr = asio_wait(&io_queue);
95 ASSERT(rr == r && r->status == (int)r->len);
97 for (uns j=0; j<bufsize; j++)
98 r->buffer[j] = round+i+j;
100 r->op = ASIO_WRITE_BACK;
107 asio_sync(&io_queue);
111 log(L_INFO, "Syncing");
115 log(L_INFO, "Reading the files sequentially");
117 for (uns i=0; i<files; i++)
119 lseek(fd[i], 0, SEEK_SET);
120 for (uns round=0; round<total_size/bufsize/files; round++)
122 struct asio_request *rr, *r = asio_get(&io_queue);
127 rr = asio_wait(&io_queue);
128 ASSERT(rr == r && r->status == (int)bufsize);
136 for (uns i=0; i<files; i++)
142 asio_cleanup_queue(&io_queue);