2 * An experiment with parallel reading and writing of files.
14 #define DIRECT O_DIRECT
16 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
17 #define P_UPDATE(cc) do { \
19 if (cnt >= cnt_rep) { cnt_ms += get_timer(); \
20 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
21 fflush(stdout); cnt_rep += 1<<26; } } while(0)
22 #define P_FINAL do { \
23 cnt_ms += get_timer(); \
24 log(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
27 int main(int argc, char **argv)
30 uns files = atol(argv[1]);
31 uns bufsize = atol(argv[2]) * 1024; // Kbytes
32 u64 total_size = (u64)atol(argv[3]) * 1024*1024*1024; // Gbytes
36 byte *buf[files], name[files][16];
37 uns xbufsize = bufsize; // Used for single-file I/O
38 byte *xbuf = big_alloc(xbufsize);
43 log(L_INFO, "Creating input file");
44 int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
46 ASSERT(!(total_size % xbufsize));
48 for (uns i=0; i<total_size/xbufsize; i++)
50 for (uns j=0; j<xbufsize; j++)
52 uns c = write(in_fd, xbuf, xbufsize);
53 ASSERT(c == xbufsize);
56 lseek(in_fd, 0, SEEK_SET);
61 log(L_INFO, "Initializing output files");
62 for (uns i=0; i<files; i++)
64 sprintf(name[i], "tmp/ft-%d", i);
65 fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
67 die("Cannot create %s: %m", name[i]);
68 buf[i] = big_alloc(bufsize);
73 log(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
75 for (uns r=0; r<total_size/bufsize/files; r++)
77 for (uns i=0; i<files; i++)
80 uns ci = read(in_fd, buf[i], bufsize);
81 ASSERT(ci == bufsize);
83 for (uns j=0; j<bufsize; j++)
86 uns c = write(fd[i], buf[i], bufsize);
94 log(L_INFO, "Syncing");
98 log(L_INFO, "Reading the files sequentially");
100 for (uns i=0; i<files; i++)
102 lseek(fd[i], 0, SEEK_SET);
103 for (uns r=0; r<total_size/xbufsize/files; r++)
105 uns c = read(fd[i], xbuf, xbufsize);
106 ASSERT(c == xbufsize);
113 for (uns i=0; i<files; i++)