1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "loader/dbufio.h"
40 #include <stdio.h>
41 #include <fcntl.h>
42 #include <toku_assert.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <string.h>
46 
47 enum { N  = 5 };
48 enum { M = 10 };
test1(size_t chars_per_file,size_t bytes_per_read)49 static void test1 (size_t chars_per_file, size_t bytes_per_read) {
50     int fds[N];
51     char fnames[N][100];
52     size_t n_read[N];
53     int still_live[N];
54     int n_live=N;
55     for (int i=0; i<N; i++) {
56 	snprintf(fnames[i], 100, "dbufio-test-file%d.data", i);
57 	unlink(fnames[i]);
58 	fds[i] = open(fnames[i], O_CREAT|O_RDWR, S_IRWXU);
59 	//printf("fds[%d]=%d is %s\n", i, fds[i], fnames[i]);
60 	assert(fds[i]>=0);
61 	n_read[i]=0;
62 	still_live[i]=i;
63 	for (size_t j=0; j<chars_per_file; j++) {
64 	    unsigned char c = (i+j)%256;
65 	    int r = toku_os_write(fds[i], &c, 1);
66 	    if (r!=0) printf("fds[%d]=%d r=%d errno=%d (%s)\n", i, fds[i], r, errno, strerror(errno));
67 	    assert(r==0);
68 	}
69 	{
70 	    int r = lseek(fds[i], 0, SEEK_SET);
71 	    assert(r==0);
72 	}
73 
74     }
75     DBUFIO_FILESET bfs;
76     {
77 	int r = create_dbufio_fileset(&bfs, N, fds, M, false);
78 	assert(r==0);
79     }
80     while (n_live>0) {
81 	int indirectnum = random()%n_live;
82 	int filenum = still_live[indirectnum];
83 	char buf[bytes_per_read];
84 	size_t n_read_here=0;
85 	int r = dbufio_fileset_read(bfs, filenum, buf, bytes_per_read, &n_read_here);
86 	//printf("read(%d) -> %d (%ld) (old n_read=%ld)\n", filenum, r, n_read_here, n_read[filenum]);
87 	if (r==0) {
88 	    // did read something
89 	    assert(n_read_here==bytes_per_read);
90 	    n_read[filenum]+=n_read_here;
91 	    //printf(" new n_read=%ld\n", n_read[filenum]);
92 	    assert(n_read[filenum]<=chars_per_file);
93 	} else {
94 	    assert(r==EOF);
95 	    assert(n_read[filenum]==chars_per_file);
96 	    still_live[indirectnum] = still_live[n_live-1];
97 	    n_live--;
98 	}
99     }
100     {
101 	int r = destroy_dbufio_fileset(bfs);
102 	assert(r==0);
103     }
104     for (int i=0; i<N; i++) {
105 	{
106 	    int r = unlink(fnames[i]);
107 	    assert(r==0);
108 	}
109 	{
110 	    int r = close(fds[i]);
111 	    assert(r==0);
112 	}
113 	assert(n_read[i]==chars_per_file);
114     }
115 }
116 
117 
118 
main(int argc,char * argv[])119 int main (int argc __attribute__((__unused__)), char *argv[]__attribute__((__unused__))) {
120 //    test1(0, 1);
121 //    test1(1, 1);
122 //    test1(15, 1);
123 //    test1(100, 1);
124     test1(30, 3); // 3 and M are relatively prime.  But 3 divides the file size.
125     return 0;
126 }
127