]> mj.ucw.cz Git - libucw.git/blob - debug/sorter/asio-test.c
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / debug / sorter / asio-test.c
1 /*
2  *  An experiment with parallel reading and writing of files using ASIO.
3  */
4
5 #include "lib/lib.h"
6 #include "lib/lfs.h"
7 #include "lib/asio.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14
15 #define COPY
16 #define DIRECT O_DIRECT
17
18 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
19 #define P_UPDATE(cc) do { \
20   cnt += cc; \
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); \
27 } while(0)
28
29 static struct asio_queue io_queue;
30
31 int main(int argc, char **argv)
32 {
33   ASSERT(argc == 4);
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
37   u64 cnt, cnt_rep;
38   uns cnt_ms;
39   int fd[files];
40   byte name[files][16];
41   struct asio_request *req[files];
42
43   init_timer();
44
45   io_queue.buffer_size = bufsize;
46   io_queue.max_writebacks = 2;
47   asio_init_queue(&io_queue);
48
49 #ifdef COPY
50   log(L_INFO, "Creating input file");
51   int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
52   ASSERT(in_fd >= 0);
53   ASSERT(!(total_size % bufsize));
54   P_INIT;
55   for (uns i=0; i<total_size/bufsize; i++)
56     {
57       struct asio_request *r = asio_get(&io_queue);
58       r->op = ASIO_WRITE_BACK;
59       r->fd = in_fd;
60       r->len = bufsize;
61       byte *xbuf = r->buffer;
62       for (uns j=0; j<bufsize; j++)
63         xbuf[j] = i+j;
64       asio_submit(r);
65       P_UPDATE(bufsize);
66     }
67   asio_sync(&io_queue);
68   lseek(in_fd, 0, SEEK_SET);
69   sync();
70   P_FINAL;
71 #endif
72
73   log(L_INFO, "Initializing output files");
74   for (uns i=0; i<files; i++)
75     {
76       sprintf(name[i], "tmp/ft-%d", i);
77       fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
78       if (fd[i] < 0)
79         die("Cannot create %s: %m", name[i]);
80     }
81   sync();
82   get_timer();
83
84   log(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
85   P_INIT;
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++)
89     {
90       for (uns i=0; i<files; i++)
91         {
92           struct asio_request *r = req[i];
93 #ifdef COPY
94           struct asio_request *rr, *rd = asio_get(&io_queue);
95           rd->op = ASIO_READ;
96           rd->fd = in_fd;
97           rd->len = bufsize;
98           asio_submit(rd);
99           rr = asio_wait(&io_queue);
100           ASSERT(rr == rd && rd->status == (int)rd->len);
101           memcpy(r->buffer, rd->buffer, bufsize);
102           asio_put(rr);
103 #else
104           for (uns j=0; j<bufsize; j++)
105             r->buffer[j] = round+i+j;
106 #endif
107           r->op = ASIO_WRITE_BACK;
108           r->fd = fd[i];
109           r->len = bufsize;
110           asio_submit(r);
111           P_UPDATE(bufsize);
112           req[i] = asio_get(&io_queue);
113         }
114     }
115   for (uns i=0; i<files; i++)
116     asio_put(req[i]);
117   asio_sync(&io_queue);
118 #ifdef COPY
119   close(in_fd);
120 #endif
121   log(L_INFO, "Syncing");
122   sync();
123   P_FINAL;
124
125   log(L_INFO, "Reading the files sequentially");
126   P_INIT;
127   for (uns i=0; i<files; i++)
128     {
129       lseek(fd[i], 0, SEEK_SET);
130       for (uns round=0; round<total_size/bufsize/files; round++)
131         {
132           struct asio_request *rr, *r = asio_get(&io_queue);
133           r->op = ASIO_READ;
134           r->fd = fd[i];
135           r->len = bufsize;
136           asio_submit(r);
137           rr = asio_wait(&io_queue);
138           ASSERT(rr == r && r->status == (int)bufsize);
139           asio_put(r);
140           P_UPDATE(bufsize);
141         }
142       close(fd[i]);
143     }
144   P_FINAL;
145
146   for (uns i=0; i<files; i++)
147     unlink(name[i]);
148 #ifdef COPY
149   unlink("tmp/ft-in");
150 #endif
151
152   asio_cleanup_queue(&io_queue);
153   log(L_INFO, "Done");
154   return 0;
155 }