2 * An experiment with parallel reading and writing of files using ASIO.
4 * (c) 2007 Martin Mares <mj@ucw.cz>
19 #define DIRECT O_DIRECT
21 static timestamp_t timer;
23 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
24 #define P_UPDATE(cc) do { \
26 if (cnt >= cnt_rep) { cnt_ms += get_timer(&timer); \
27 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
28 fflush(stdout); cnt_rep += 1<<26; } } while(0)
29 #define P_FINAL do { \
30 cnt_ms += get_timer(&timer); \
31 msg(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
34 static struct asio_queue io_queue;
36 int main(int argc, char **argv)
41 cf_parse_int(argv[1], (int*) &files) ||
42 cf_parse_int(argv[2], (int*) &bufsize) ||
43 cf_parse_u64(argv[3], &total_size))
45 fprintf(stderr, "Usage: asio-test <nr-files> <bufsize> <totalsize>\n");
52 struct asio_request *req[files];
56 io_queue.buffer_size = bufsize;
57 io_queue.max_writebacks = 2;
58 asio_init_queue(&io_queue);
61 msg(L_INFO, "Creating input file");
62 int in_fd = ucw_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
64 ASSERT(!(total_size % bufsize));
66 for (uns i=0; i<total_size/bufsize; i++)
68 struct asio_request *r = asio_get(&io_queue);
69 r->op = ASIO_WRITE_BACK;
72 byte *xbuf = r->buffer;
73 for (uns j=0; j<bufsize; j++)
79 lseek(in_fd, 0, SEEK_SET);
84 msg(L_INFO, "Initializing output files");
85 for (uns i=0; i<files; i++)
87 sprintf(name[i], "tmp/ft-%d", i);
88 fd[i] = ucw_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
90 die("Cannot create %s: %m", name[i]);
95 msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
97 for (uns i=0; i<files; i++)
98 req[i] = asio_get(&io_queue);
99 for (uns round=0; round<total_size/bufsize/files; round++)
101 for (uns i=0; i<files; i++)
103 struct asio_request *r = req[i];
105 struct asio_request *rr, *rd = asio_get(&io_queue);
110 rr = asio_wait(&io_queue);
111 ASSERT(rr == rd && rd->status == (int)rd->len);
112 memcpy(r->buffer, rd->buffer, bufsize);
115 for (uns j=0; j<bufsize; j++)
116 r->buffer[j] = round+i+j;
118 r->op = ASIO_WRITE_BACK;
123 req[i] = asio_get(&io_queue);
126 for (uns i=0; i<files; i++)
128 asio_sync(&io_queue);
132 msg(L_INFO, "Syncing");
136 msg(L_INFO, "Reading the files sequentially");
138 for (uns i=0; i<files; i++)
140 lseek(fd[i], 0, SEEK_SET);
141 for (uns round=0; round<total_size/bufsize/files; round++)
143 struct asio_request *rr, *r = asio_get(&io_queue);
148 rr = asio_wait(&io_queue);
149 ASSERT(rr == r && r->status == (int)bufsize);
157 for (uns i=0; i<files; i++)
163 asio_cleanup_queue(&io_queue);