bcache_test.cc (2889254e) bcache_test.cc (2534ac38)
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <pthread.h>
4
5#include "test.h"
6#include "blockcache.h"
7#include "filemgr.h"
8#include "filemgr_ops_linux.h"
5
6#include "test.h"
7#include "blockcache.h"
8#include "filemgr.h"
9#include "filemgr_ops_linux.h"
10#include "crc32.h"
9
11
12#include "memleak.h"
13
10void basic_test()
11{
12 TEST_INIT();
13
14 struct filemgr *file;
15 struct filemgr_config config;
16 bid_t bid;
17 int i, j;
18 uint8_t buf[4096];
19
14void basic_test()
15{
16 TEST_INIT();
17
18 struct filemgr *file;
19 struct filemgr_config config;
20 bid_t bid;
21 int i, j;
22 uint8_t buf[4096];
23
24 memset(&config, 0, sizeof(config));
20 config.blocksize = 4096;
21 config.ncacheblock = 5;
22 file = filemgr_open("./dummy", get_linux_filemgr_ops(), config);
23
24 for (i=0;i<5;++i) {
25 filemgr_alloc(file);
26 filemgr_write(file, i, buf);
27 }

--- 25 unchanged lines hidden (view full) ---

53{
54 TEST_INIT();
55
56 struct filemgr *file;
57 struct filemgr_config config;
58 bid_t bid;
59 int i, j;
60 uint8_t buf[4096];
25 config.blocksize = 4096;
26 config.ncacheblock = 5;
27 file = filemgr_open("./dummy", get_linux_filemgr_ops(), config);
28
29 for (i=0;i<5;++i) {
30 filemgr_alloc(file);
31 filemgr_write(file, i, buf);
32 }

--- 25 unchanged lines hidden (view full) ---

58{
59 TEST_INIT();
60
61 struct filemgr *file;
62 struct filemgr_config config;
63 bid_t bid;
64 int i, j;
65 uint8_t buf[4096];
61
66 int r;
67 r = system("rm -rf ./dummy");
68
69 memset(&config, 0, sizeof(config));
62 config.blocksize = 4096;
63 config.ncacheblock = 5;
70 config.blocksize = 4096;
71 config.ncacheblock = 5;
72 config.flag = 0x0;
64 file = filemgr_open("./dummy", get_linux_filemgr_ops(), config);
65
66 for (i=0;i<5;++i) {
67 filemgr_alloc(file);
68 filemgr_write(file, i, buf);
69 }
70 for (i=5;i<10;++i) {
71 filemgr_alloc(file);
72 filemgr_write(file, i, buf);
73 }
74 filemgr_commit(file);
73 file = filemgr_open("./dummy", get_linux_filemgr_ops(), config);
74
75 for (i=0;i<5;++i) {
76 filemgr_alloc(file);
77 filemgr_write(file, i, buf);
78 }
79 for (i=5;i<10;++i) {
80 filemgr_alloc(file);
81 filemgr_write(file, i, buf);
82 }
83 filemgr_commit(file);
84 filemgr_close(file);
85 filemgr_shutdown();
75
76 TEST_RESULT("basic test");
77
78}
79
86
87 TEST_RESULT("basic test");
88
89}
90
80int main()
91struct timespec _time_gap(struct timespec a, struct timespec b)
81{
92{
93 struct timespec ret;
94 if (b.tv_nsec >= a.tv_nsec) {
95 ret.tv_nsec = b.tv_nsec - a.tv_nsec;
96 ret.tv_sec = b.tv_sec - a.tv_sec;
97 }else{
98 ret.tv_nsec = 1000000000 + b.tv_nsec - a.tv_nsec;
99 ret.tv_sec = b.tv_sec - a.tv_sec - 1;
100 }
101 return ret;
102}
103
104struct worker_args{
105 struct filemgr *file;
106 size_t writer;
107 size_t nblocks;
108 size_t time_sec;
109};
110
111void * worker(void *voidargs)
112{
113 void *buf = (void *)malloc(4096);
114 struct worker_args *args = (struct worker_args*)voidargs;
115 struct timespec ts_begin, ts_cur, ts_gap;
116 int ret;
117 bid_t bid;
118 uint32_t crc, crc_file;
119 uint64_t i, c, run_count=0;
120
121 memset(buf, 0, 4096);
122 clock_gettime(CLOCK_REALTIME, &ts_begin);
123
124 while(1) {
125 bid = rand() % args->nblocks;
126 ret = bcache_read(args->file, bid, buf);
127 if (ret <= 0) {
128 ret = args->file->ops->pread(args->file->fd, buf, args->file->blocksize, bid * args->file->blocksize);
129 assert(ret == args->file->blocksize);
130 ret = bcache_write(args->file, bid, buf, BCACHE_CLEAN);
131 assert(ret == args->file->blocksize);
132 }
133 crc_file = crc32_8(buf, sizeof(uint64_t)*2, 0);
134 memcpy(&i, buf, sizeof(i));
135 memcpy(&crc, buf + sizeof(uint64_t)*2, sizeof(crc));
136 assert(crc == crc_file && i==bid);
137 //DBG("%d %d %x %x\n", (int)i, (int)bid, (int)crc, (int)crc_file);
138
139 if (args->writer) {
140 memcpy(&c, buf+sizeof(i), sizeof(c));
141 c++;
142 memcpy(buf+sizeof(i), &c, sizeof(c));
143 crc = crc32_8(buf, sizeof(uint64_t)*2, 0);
144 memcpy(buf + sizeof(uint64_t)*2, &crc, sizeof(crc));
145
146 ret = bcache_write(args->file, bid, buf, BCACHE_DIRTY);
147 assert(ret == args->file->blocksize);
148 }
149
150 clock_gettime(CLOCK_REALTIME, &ts_cur);
151 ts_gap = _time_gap(ts_begin, ts_cur);
152 if (ts_gap.tv_sec >= args->time_sec) break;
153
154 run_count++;
155 }
156
157 free(buf);
158}
159
160void multi_thread_test(
161 int nblocks, int cachesize, int blocksize, int time_sec, int nwriters, int nreaders)
162{
163 TEST_INIT();
164
165 struct filemgr *file;
166 struct filemgr_config config;
167 bid_t bid;
168 int n = nwriters + nreaders;
169 uint64_t i, j;
170 uint32_t crc;
171 void *buf;
82 int r;
172 int r;
173 pthread_t tid[n];
174 struct worker_args args[n];
175 void *ret[n];
176
83 r = system("rm -rf ./dummy");
177 r = system("rm -rf ./dummy");
178
179 memleak_start();
180
181 buf = (void *)malloc(4096);
182 memset(buf, 0, 4096);
183
184 memset(&config, 0, sizeof(config));
185 config.blocksize = blocksize;
186 config.ncacheblock = cachesize;
187 config.flag = 0x0;
188 file = filemgr_open("./dummy", get_linux_filemgr_ops(), config);
189
190 for (i=0;i<nblocks;++i) {
191 memcpy(buf, &i, sizeof(i));
192 j = 0;
193 memcpy(buf + sizeof(i), &j, sizeof(j));
194 crc = crc32_8(buf, sizeof(i) + sizeof(j), 0);
195 memcpy(buf + sizeof(i) + sizeof(j), &crc, sizeof(crc));
196 bcache_write(file, (bid_t)i, buf, BCACHE_DIRTY);
197 }
198
199 for (i=0;i<n;++i){
200 args[i].file = file;
201 args[i].writer = ((i<nwriters)?(1):(0));
202 args[i].nblocks = nblocks;
203 args[i].time_sec = time_sec;
204 pthread_create(&tid[i], NULL, worker, &args[i]);
205 }
206
207 DBG("wait for %d seconds..\n", time_sec);
208 for (i=0;i<n;++i){
209 pthread_join(tid[i], &ret[i]);
210 }
211
212 filemgr_commit(file);
213 filemgr_close(file);
214 filemgr_shutdown();
215 free(buf);
216
217 memleak_end();
218 TEST_RESULT("multi thread test");
219}
220
221int main()
222{
84 basic_test2();
223 basic_test2();
224 multi_thread_test(4, 2, 32, 3, 1, 1);
85
86 return 0;
87}
225
226 return 0;
227}