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 #define DIRECT 0 // Direct IO not supported, fallback to normal mode
25 static timestamp_t timer;
27 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
28 #define P_UPDATE(cc) do { \
30 if (cnt >= cnt_rep) { cnt_ms += get_timer(&timer); \
31 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
32 fflush(stdout); cnt_rep += 1<<26; } } while(0)
33 #define P_FINAL do { \
34 cnt_ms += get_timer(&timer); \
35 msg(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
38 static struct asio_queue io_queue;
40 int main(int argc, char **argv)
45 cf_parse_int(argv[1], (int*) &files) ||
46 cf_parse_int(argv[2], (int*) &bufsize) ||
47 cf_parse_u64(argv[3], &total_size))
49 fprintf(stderr, "Usage: asio-test <nr-files> <bufsize> <totalsize>\n");
56 struct asio_request *req[files];
60 io_queue.buffer_size = bufsize;
61 io_queue.max_writebacks = 2;
62 asio_init_queue(&io_queue);
65 msg(L_INFO, "Creating input file");
66 int in_fd = ucw_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
68 ASSERT(!(total_size % bufsize));
70 for (uns i=0; i<total_size/bufsize; i++)
72 struct asio_request *r = asio_get(&io_queue);
73 r->op = ASIO_WRITE_BACK;
76 byte *xbuf = r->buffer;
77 for (uns j=0; j<bufsize; j++)
83 lseek(in_fd, 0, SEEK_SET);
88 msg(L_INFO, "Initializing output files");
89 for (uns i=0; i<files; i++)
91 sprintf(name[i], "tmp/ft-%d", i);
92 fd[i] = ucw_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
94 die("Cannot create %s: %m", name[i]);
99 msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
101 for (uns i=0; i<files; i++)
102 req[i] = asio_get(&io_queue);
103 for (uns round=0; round<total_size/bufsize/files; round++)
105 for (uns i=0; i<files; i++)
107 struct asio_request *r = req[i];
109 struct asio_request *rr, *rd = asio_get(&io_queue);
114 rr = asio_wait(&io_queue);
115 ASSERT(rr == rd && rd->status == (int)rd->len);
116 memcpy(r->buffer, rd->buffer, bufsize);
119 for (uns j=0; j<bufsize; j++)
120 r->buffer[j] = round+i+j;
122 r->op = ASIO_WRITE_BACK;
127 req[i] = asio_get(&io_queue);
130 for (uns i=0; i<files; i++)
132 asio_sync(&io_queue);
136 msg(L_INFO, "Syncing");
140 msg(L_INFO, "Reading the files sequentially");
142 for (uns i=0; i<files; i++)
144 lseek(fd[i], 0, SEEK_SET);
145 for (uns round=0; round<total_size/bufsize/files; round++)
147 struct asio_request *rr, *r = asio_get(&io_queue);
152 rr = asio_wait(&io_queue);
153 ASSERT(rr == r && r->status == (int)bufsize);
161 for (uns i=0; i<files; i++)
167 asio_cleanup_queue(&io_queue);