2 * An experiment with parallel reading and writing of files using ASIO.
4 * (c) 2007 Martin Mares <mj@ucw.cz>
20 #define DIRECT O_DIRECT
22 static timestamp_t timer;
24 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
25 #define P_UPDATE(cc) do { \
27 if (cnt >= cnt_rep) { cnt_ms += get_timer(&timer); \
28 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
29 fflush(stdout); cnt_rep += 1<<26; } } while(0)
30 #define P_FINAL do { \
31 cnt_ms += get_timer(&timer); \
32 msg(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
35 static struct asio_queue io_queue;
37 int main(int argc, char **argv)
42 cf_parse_int(argv[1], (int*) &files) ||
43 cf_parse_int(argv[2], (int*) &bufsize) ||
44 cf_parse_u64(argv[3], &total_size))
46 fprintf(stderr, "Usage: asio-test <nr-files> <bufsize> <totalsize>\n");
53 struct asio_request *req[files];
57 io_queue.buffer_size = bufsize;
58 io_queue.max_writebacks = 2;
59 asio_init_queue(&io_queue);
62 msg(L_INFO, "Creating input file");
63 int in_fd = ucw_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
65 ASSERT(!(total_size % bufsize));
67 for (uint i=0; i<total_size/bufsize; i++)
69 struct asio_request *r = asio_get(&io_queue);
70 r->op = ASIO_WRITE_BACK;
73 byte *xbuf = r->buffer;
74 for (uint j=0; j<bufsize; j++)
80 lseek(in_fd, 0, SEEK_SET);
85 msg(L_INFO, "Initializing output files");
86 for (uint i=0; i<files; i++)
88 sprintf(name[i], "tmp/ft-%d", i);
89 fd[i] = ucw_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
91 die("Cannot create %s: %m", name[i]);
96 msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
98 for (uint i=0; i<files; i++)
99 req[i] = asio_get(&io_queue);
100 for (uint round=0; round<total_size/bufsize/files; round++)
102 for (uint i=0; i<files; i++)
104 struct asio_request *r = req[i];
106 struct asio_request *rr, *rd = asio_get(&io_queue);
111 rr = asio_wait(&io_queue);
112 ASSERT(rr == rd && rd->status == (int)rd->len);
113 memcpy(r->buffer, rd->buffer, bufsize);
116 for (uint j=0; j<bufsize; j++)
117 r->buffer[j] = round+i+j;
119 r->op = ASIO_WRITE_BACK;
124 req[i] = asio_get(&io_queue);
127 for (uint i=0; i<files; i++)
129 asio_sync(&io_queue);
133 msg(L_INFO, "Syncing");
137 msg(L_INFO, "Reading the files sequentially");
139 for (uint i=0; i<files; i++)
141 lseek(fd[i], 0, SEEK_SET);
142 for (uint round=0; round<total_size/bufsize/files; round++)
144 struct asio_request *rr, *r = asio_get(&io_queue);
149 rr = asio_wait(&io_queue);
150 ASSERT(rr == r && r->status == (int)bufsize);
158 for (uint i=0; i<files; i++)
164 asio_cleanup_queue(&io_queue);