2 * An experiment with parallel reading and writing of files using ASIO.
16 #define DIRECT O_DIRECT
18 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
19 #define P_UPDATE(cc) do { \
21 if (cnt >= cnt_rep) { cnt_ms += get_timer(); \
22 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
23 fflush(stdout); cnt_rep += 1<<26; } } while(0)
24 #define P_FINAL do { \
25 cnt_ms += get_timer(); \
26 log(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
29 static struct asio_queue io_queue;
31 int main(int argc, char **argv)
34 uns files = atol(argv[1]);
35 uns bufsize = atol(argv[2]) * 1024; // Kbytes
36 u64 total_size = (u64)atol(argv[3]) * 1024*1024*1024; // Gbytes
41 struct asio_request *req[files];
45 io_queue.buffer_size = bufsize;
46 io_queue.max_writebacks = 2;
47 asio_init_queue(&io_queue);
50 log(L_INFO, "Creating input file");
51 int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
53 ASSERT(!(total_size % bufsize));
55 for (uns i=0; i<total_size/bufsize; i++)
57 struct asio_request *r = asio_get(&io_queue);
58 r->op = ASIO_WRITE_BACK;
61 byte *xbuf = r->buffer;
62 for (uns j=0; j<bufsize; j++)
68 lseek(in_fd, 0, SEEK_SET);
73 log(L_INFO, "Initializing output files");
74 for (uns i=0; i<files; i++)
76 sprintf(name[i], "tmp/ft-%d", i);
77 fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
79 die("Cannot create %s: %m", name[i]);
84 log(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
86 for (uns i=0; i<files; i++)
87 req[i] = asio_get(&io_queue);
88 for (uns round=0; round<total_size/bufsize/files; round++)
90 for (uns i=0; i<files; i++)
92 struct asio_request *r = req[i];
94 struct asio_request *rr, *rd = asio_get(&io_queue);
99 rr = asio_wait(&io_queue);
100 ASSERT(rr == rd && rd->status == (int)rd->len);
101 memcpy(r->buffer, rd->buffer, bufsize);
104 for (uns j=0; j<bufsize; j++)
105 r->buffer[j] = round+i+j;
107 r->op = ASIO_WRITE_BACK;
112 req[i] = asio_get(&io_queue);
115 for (uns i=0; i<files; i++)
117 asio_sync(&io_queue);
121 log(L_INFO, "Syncing");
125 log(L_INFO, "Reading the files sequentially");
127 for (uns i=0; i<files; i++)
129 lseek(fd[i], 0, SEEK_SET);
130 for (uns round=0; round<total_size/bufsize/files; round++)
132 struct asio_request *rr, *r = asio_get(&io_queue);
137 rr = asio_wait(&io_queue);
138 ASSERT(rr == r && r->status == (int)bufsize);
146 for (uns i=0; i<files; i++)
152 asio_cleanup_queue(&io_queue);