2 * An experiment with parallel reading and writing of files.
15 #define DIRECT 0 // or O_DIRECT
17 static timestamp_t timer;
19 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
20 #define P_UPDATE(cc) do { \
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); \
30 int main(int argc, char **argv)
35 cf_parse_int(argv[1], (int*) &files) ||
36 cf_parse_int(argv[2], (int*) &bufsize) ||
37 cf_parse_u64(argv[3], &total_size))
39 fprintf(stderr, "Usage: file-test <nr-files> <bufsize> <totalsize>\n");
45 byte *buf[files], name[files][16];
46 uns xbufsize = bufsize; // Used for single-file I/O
47 byte *xbuf = big_alloc(xbufsize);
52 msg(L_INFO, "Creating input file");
53 int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
55 ASSERT(!(total_size % xbufsize));
57 for (uns i=0; i<total_size/xbufsize; i++)
59 for (uns j=0; j<xbufsize; j++)
61 uns c = write(in_fd, xbuf, xbufsize);
62 ASSERT(c == xbufsize);
65 lseek(in_fd, 0, SEEK_SET);
70 msg(L_INFO, "Initializing output files");
71 for (uns i=0; i<files; i++)
73 sprintf(name[i], "tmp/ft-%d", i);
74 fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
76 die("Cannot create %s: %m", name[i]);
77 buf[i] = big_alloc(bufsize);
82 msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
84 for (uns r=0; r<total_size/bufsize/files; r++)
86 for (uns i=0; i<files; i++)
89 uns ci = read(in_fd, buf[i], bufsize);
90 ASSERT(ci == bufsize);
92 for (uns j=0; j<bufsize; j++)
95 uns c = write(fd[i], buf[i], bufsize);
103 msg(L_INFO, "Syncing");
107 msg(L_INFO, "Reading the files sequentially");
109 for (uns i=0; i<files; i++)
111 lseek(fd[i], 0, SEEK_SET);
112 for (uns r=0; r<total_size/xbufsize/files; r++)
114 uns c = read(fd[i], xbuf, xbufsize);
115 ASSERT(c == xbufsize);
122 for (uns i=0; i<files; i++)