2 * An experiment with parallel reading and writing of files using ASIO.
16 #define DIRECT O_DIRECT
18 static timestamp_t timer;
20 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
21 #define P_UPDATE(cc) do { \
23 if (cnt >= cnt_rep) { cnt_ms += get_timer(&timer); \
24 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
25 fflush(stdout); cnt_rep += 1<<26; } } while(0)
26 #define P_FINAL do { \
27 cnt_ms += get_timer(&timer); \
28 msg(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
31 static struct asio_queue io_queue;
33 int main(int argc, char **argv)
36 uns files = atol(argv[1]);
37 uns bufsize = atol(argv[2]) * 1024; // Kbytes
38 u64 total_size = (u64)atol(argv[3]) * 1024*1024*1024; // Gbytes
43 struct asio_request *req[files];
47 io_queue.buffer_size = bufsize;
48 io_queue.max_writebacks = 2;
49 asio_init_queue(&io_queue);
52 msg(L_INFO, "Creating input file");
53 int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
55 ASSERT(!(total_size % bufsize));
57 for (uns i=0; i<total_size/bufsize; i++)
59 struct asio_request *r = asio_get(&io_queue);
60 r->op = ASIO_WRITE_BACK;
63 byte *xbuf = r->buffer;
64 for (uns j=0; j<bufsize; j++)
70 lseek(in_fd, 0, SEEK_SET);
75 msg(L_INFO, "Initializing output files");
76 for (uns i=0; i<files; i++)
78 sprintf(name[i], "tmp/ft-%d", i);
79 fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
81 die("Cannot create %s: %m", name[i]);
86 msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
88 for (uns i=0; i<files; i++)
89 req[i] = asio_get(&io_queue);
90 for (uns round=0; round<total_size/bufsize/files; round++)
92 for (uns i=0; i<files; i++)
94 struct asio_request *r = req[i];
96 struct asio_request *rr, *rd = asio_get(&io_queue);
101 rr = asio_wait(&io_queue);
102 ASSERT(rr == rd && rd->status == (int)rd->len);
103 memcpy(r->buffer, rd->buffer, bufsize);
106 for (uns j=0; j<bufsize; j++)
107 r->buffer[j] = round+i+j;
109 r->op = ASIO_WRITE_BACK;
114 req[i] = asio_get(&io_queue);
117 for (uns i=0; i<files; i++)
119 asio_sync(&io_queue);
123 msg(L_INFO, "Syncing");
127 msg(L_INFO, "Reading the files sequentially");
129 for (uns i=0; i<files; i++)
131 lseek(fd[i], 0, SEEK_SET);
132 for (uns round=0; round<total_size/bufsize/files; round++)
134 struct asio_request *rr, *r = asio_get(&io_queue);
139 rr = asio_wait(&io_queue);
140 ASSERT(rr == r && r->status == (int)bufsize);
148 for (uns i=0; i<files; i++)
154 asio_cleanup_queue(&io_queue);