2 * An experiment with parallel reading and writing of files using ASIO.
17 #define DIRECT O_DIRECT
19 static timestamp_t timer;
21 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
22 #define P_UPDATE(cc) do { \
24 if (cnt >= cnt_rep) { cnt_ms += get_timer(&timer); \
25 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
26 fflush(stdout); cnt_rep += 1<<26; } } while(0)
27 #define P_FINAL do { \
28 cnt_ms += get_timer(&timer); \
29 msg(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
32 static struct asio_queue io_queue;
34 int main(int argc, char **argv)
39 cf_parse_int(argv[1], (int*) &files) ||
40 cf_parse_int(argv[2], (int*) &bufsize) ||
41 cf_parse_u64(argv[3], &total_size))
43 fprintf(stderr, "Usage: asio-test <nr-files> <bufsize> <totalsize>\n");
50 struct asio_request *req[files];
54 io_queue.buffer_size = bufsize;
55 io_queue.max_writebacks = 2;
56 asio_init_queue(&io_queue);
59 msg(L_INFO, "Creating input file");
60 int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
62 ASSERT(!(total_size % bufsize));
64 for (uns i=0; i<total_size/bufsize; i++)
66 struct asio_request *r = asio_get(&io_queue);
67 r->op = ASIO_WRITE_BACK;
70 byte *xbuf = r->buffer;
71 for (uns j=0; j<bufsize; j++)
77 lseek(in_fd, 0, SEEK_SET);
82 msg(L_INFO, "Initializing output files");
83 for (uns i=0; i<files; i++)
85 sprintf(name[i], "tmp/ft-%d", i);
86 fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
88 die("Cannot create %s: %m", name[i]);
93 msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
95 for (uns i=0; i<files; i++)
96 req[i] = asio_get(&io_queue);
97 for (uns round=0; round<total_size/bufsize/files; round++)
99 for (uns i=0; i<files; i++)
101 struct asio_request *r = req[i];
103 struct asio_request *rr, *rd = asio_get(&io_queue);
108 rr = asio_wait(&io_queue);
109 ASSERT(rr == rd && rd->status == (int)rd->len);
110 memcpy(r->buffer, rd->buffer, bufsize);
113 for (uns j=0; j<bufsize; j++)
114 r->buffer[j] = round+i+j;
116 r->op = ASIO_WRITE_BACK;
121 req[i] = asio_get(&io_queue);
124 for (uns i=0; i<files; i++)
126 asio_sync(&io_queue);
130 msg(L_INFO, "Syncing");
134 msg(L_INFO, "Reading the files sequentially");
136 for (uns i=0; i<files; i++)
138 lseek(fd[i], 0, SEEK_SET);
139 for (uns round=0; round<total_size/bufsize/files; round++)
141 struct asio_request *rr, *r = asio_get(&io_queue);
146 rr = asio_wait(&io_queue);
147 ASSERT(rr == r && r->status == (int)bufsize);
155 for (uns i=0; i<files; i++)
161 asio_cleanup_queue(&io_queue);