2 * An experiment with parallel reading and writing of files.
4 * (c) 2007 Martin Mares <mj@ucw.cz>
17 #define DIRECT 0 // or O_DIRECT
19 static timestamp_t timer;
21 #define P_INIT do { cnt = 0; cnt_rep = 0; cnt_ms = 1; } while(0)
22 #define P_UPDATE(cc) do { \
24 if (cnt >= cnt_rep) { cnt_ms += get_timer(&timer); \
25 printf("%d of %d MB (%.2f MB/sec)\r", (int)(cnt >> 20), (int)(total_size >> 20), (double)cnt / 1048576 * 1000 / cnt_ms); \
26 fflush(stdout); cnt_rep += 1<<26; } } while(0)
27 #define P_FINAL do { \
28 cnt_ms += get_timer(&timer); \
29 msg(L_INFO, "Spent %.3f sec (%.2f MB/sec)", (double)cnt_ms/1000, (double)cnt / 1048576 * 1000 / cnt_ms); \
32 int main(int argc, char **argv)
37 cf_parse_int(argv[1], (int*) &files) ||
38 cf_parse_int(argv[2], (int*) &bufsize) ||
39 cf_parse_u64(argv[3], &total_size))
41 fprintf(stderr, "Usage: file-test <nr-files> <bufsize> <totalsize>\n");
47 byte *buf[files], name[files][16];
48 uns xbufsize = bufsize; // Used for single-file I/O
49 byte *xbuf = big_alloc(xbufsize);
54 msg(L_INFO, "Creating input file");
55 int in_fd = sh_open("tmp/ft-in", O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
57 ASSERT(!(total_size % xbufsize));
59 for (uns i=0; i<total_size/xbufsize; i++)
61 for (uns j=0; j<xbufsize; j++)
63 uns c = write(in_fd, xbuf, xbufsize);
64 ASSERT(c == xbufsize);
67 lseek(in_fd, 0, SEEK_SET);
72 msg(L_INFO, "Initializing output files");
73 for (uns i=0; i<files; i++)
75 sprintf(name[i], "tmp/ft-%d", i);
76 fd[i] = sh_open(name[i], O_RDWR | O_CREAT | O_TRUNC | DIRECT, 0666);
78 die("Cannot create %s: %m", name[i]);
79 buf[i] = big_alloc(bufsize);
84 msg(L_INFO, "Writing %d MB to %d files in parallel with %d byte buffers", (int)(total_size >> 20), files, bufsize);
86 for (uns r=0; r<total_size/bufsize/files; r++)
88 for (uns i=0; i<files; i++)
91 uns ci = read(in_fd, buf[i], bufsize);
92 ASSERT(ci == bufsize);
94 for (uns j=0; j<bufsize; j++)
97 uns c = write(fd[i], buf[i], bufsize);
105 msg(L_INFO, "Syncing");
109 msg(L_INFO, "Reading the files sequentially");
111 for (uns i=0; i<files; i++)
113 lseek(fd[i], 0, SEEK_SET);
114 for (uns r=0; r<total_size/xbufsize/files; r++)
116 uns c = read(fd[i], xbuf, xbufsize);
117 ASSERT(c == xbufsize);
124 for (uns i=0; i<files; i++)