]> mj.ucw.cz Git - libucw.git/blob - debug/sorter/radix-file-test.c
b776d1ea9fd01a7e4332060c22bb06bbb541ec3f
[libucw.git] / debug / sorter / radix-file-test.c
1 /*
2  *  An experiment with parallel reading and writing of files.
3  */
4
5 #include "lib/lib.h"
6 #include "lib/conf.h"
7 #include "lib/lfs.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13
14 #define COPY
15 #define DIRECT 0                // or O_DIRECT
16
17 static timestamp_t timer;
18
19 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
20 #define P_UPDATE(cc) do { \
21   cnt += cc; \
22   if (cnt >= cnt_rep) { cnt_ms += get_timer(&timer); \
23     printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
24     fflush(stdout); cnt_rep += 1<<26; } } while(0)
25 #define P_FINAL do { \
26   cnt_ms += get_timer(&timer); \
27   msg(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
28 } while(0)
29
30 int main(int argc, char **argv)
31 {
32   uns files, bufsize;
33   u64 total_size;
34   if (argc != 4 ||
35       cf_parse_int(argv[1], (int*) &files) ||
36       cf_parse_int(argv[2], (int*) &bufsize) ||
37       cf_parse_u64(argv[3], &total_size))
38     {
39       fprintf(stderr, "Usage: file-test <nr-files> <bufsize> <totalsize>\n");
40       return 1;
41     }
42   u64 cnt, cnt_rep;
43   uns cnt_ms;
44   int fd[files];
45   byte *buf[files], name[files][16];
46   uns xbufsize = bufsize;                                       // Used for single-file I/O
47   byte *xbuf = big_alloc(xbufsize);
48
49   init_timer(&timer);
50
51 #ifdef COPY
52   msg(L_INFO, "Creating input file");
53   int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
54   ASSERT(in_fd >= 0);
55   ASSERT(!(total_size % xbufsize));
56   P_INIT;
57   for (uns i=0; i<total_size/xbufsize; i++)
58     {
59       for (uns j=0; j<xbufsize; j++)
60         xbuf[j] = i+j;
61       uns c = write(in_fd, xbuf, xbufsize);
62       ASSERT(c == xbufsize);
63       P_UPDATE(c);
64     }
65   lseek(in_fd, 0, SEEK_SET);
66   sync();
67   P_FINAL;
68 #endif
69
70   msg(L_INFO, "Initializing output files");
71   for (uns i=0; i<files; i++)
72     {
73       sprintf(name[i], "tmp/ft-%d", i);
74       fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
75       if (fd[i] < 0)
76         die("Cannot create %s: %m", name[i]);
77       buf[i] = big_alloc(bufsize);
78     }
79   sync();
80   get_timer(&timer);
81
82   msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
83   P_INIT;
84   for (uns r=0; r<total_size/bufsize/files; r++)
85     {
86       for (uns i=0; i<files; i++)
87         {
88 #ifdef COPY
89           uns ci = read(in_fd, buf[i], bufsize);
90           ASSERT(ci == bufsize);
91 #else
92           for (uns j=0; j<bufsize; j++)
93             buf[i][j] = r+i+j;
94 #endif
95           uns c = write(fd[i], buf[i], bufsize);
96           ASSERT(c == bufsize);
97           P_UPDATE(c);
98         }
99     }
100 #ifdef COPY
101   close(in_fd);
102 #endif
103   msg(L_INFO, "Syncing");
104   sync();
105   P_FINAL;
106
107   msg(L_INFO, "Reading the files sequentially");
108   P_INIT;
109   for (uns i=0; i<files; i++)
110     {
111       lseek(fd[i], 0, SEEK_SET);
112       for (uns r=0; r<total_size/xbufsize/files; r++)
113         {
114           uns c = read(fd[i], xbuf, xbufsize);
115           ASSERT(c == xbufsize);
116           P_UPDATE(c);
117         }
118       close(fd[i]);
119     }
120   P_FINAL;
121
122   for (uns i=0; i<files; i++)
123     unlink(name[i]);
124 #ifdef COPY
125   unlink("tmp/ft-in");
126 #endif
127   msg(L_INFO, "Done");
128   return 0;
129 }