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