2 * An experiment with parallel reading and writing of files.
14 #define DIRECT O_DIRECT
16 static timestamp_t timer;
18 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
19 #define P_UPDATE(cc) do { \
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); \
29 int main(int argc, char **argv)
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
38 byte *buf[files], name[files][16];
39 uns xbufsize = bufsize; // Used for single-file I/O
40 byte *xbuf = big_alloc(xbufsize);
45 log(L_INFO, "Creating input file");
46 int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
48 ASSERT(!(total_size % xbufsize));
50 for (uns i=0; i<total_size/xbufsize; i++)
52 for (uns j=0; j<xbufsize; j++)
54 uns c = write(in_fd, xbuf, xbufsize);
55 ASSERT(c == xbufsize);
58 lseek(in_fd, 0, SEEK_SET);
63 log(L_INFO, "Initializing output files");
64 for (uns i=0; i<files; i++)
66 sprintf(name[i], "tmp/ft-%d", i);
67 fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
69 die("Cannot create %s: %m", name[i]);
70 buf[i] = big_alloc(bufsize);
75 log(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
77 for (uns r=0; r<total_size/bufsize/files; r++)
79 for (uns i=0; i<files; i++)
82 uns ci = read(in_fd, buf[i], bufsize);
83 ASSERT(ci == bufsize);
85 for (uns j=0; j<bufsize; j++)
88 uns c = write(fd[i], buf[i], bufsize);
96 log(L_INFO, "Syncing");
100 log(L_INFO, "Reading the files sequentially");
102 for (uns i=0; i<files; i++)
104 lseek(fd[i], 0, SEEK_SET);
105 for (uns r=0; r<total_size/xbufsize/files; r++)
107 uns c = read(fd[i], xbuf, xbufsize);
108 ASSERT(c == xbufsize);
115 for (uns i=0; i<files; i++)