1
/* Copyright (C) 2006-2008 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
TODO: use pthread_join instead of wait_for_thread_count_to_be_zero, like in
18
my_atomic-t.c (see BUG#22320).
24
#include "test_file.h"
27
#define PCACHE_SIZE (TEST_PAGE_SIZE*1024*8)
30
static const char* default_dbug_option;
33
static char *file1_name= (char*)"page_cache_test_file_1";
34
static PAGECACHE_FILE file1;
35
static pthread_cond_t COND_thread_count;
36
static pthread_mutex_t LOCK_thread_count;
37
static uint thread_count;
38
static PAGECACHE pagecache;
40
#ifdef TEST_HIGH_CONCURENCY
41
static uint number_of_readers= 10;
42
static uint number_of_writers= 20;
43
static uint number_of_tests= 30000;
44
static uint record_length_limit= TEST_PAGE_SIZE/200;
45
static uint number_of_pages= 20;
46
static uint flush_divider= 1000;
47
#else /*TEST_HIGH_CONCURENCY*/
49
static uint number_of_readers= 10;
50
static uint number_of_writers= 1;
51
static uint number_of_tests= 30000;
52
static uint record_length_limit= TEST_PAGE_SIZE/200;
53
static uint number_of_pages= 20;
54
static uint flush_divider= 1000;
56
#define SKIP_BIG_TESTS(X) /* no-op */
57
#else /*TEST_READERS*/
59
static uint number_of_readers= 0;
60
static uint number_of_writers= 10;
61
static uint number_of_tests= 30000;
62
static uint record_length_limit= TEST_PAGE_SIZE/200;
63
static uint number_of_pages= 20;
64
static uint flush_divider= 1000;
66
#define SKIP_BIG_TESTS(X) /* no-op */
67
#else /*TEST_WRITERS*/
68
static uint number_of_readers= 10;
69
static uint number_of_writers= 10;
70
static uint number_of_tests= 50000;
71
static uint record_length_limit= TEST_PAGE_SIZE/200;
72
static uint number_of_pages= 20000;
73
static uint flush_divider= 1000;
74
#endif /*TEST_WRITERS*/
75
#endif /*TEST_READERS*/
76
#endif /*TEST_HIGH_CONCURENCY*/
80
@brief Dummy pagecache callback.
84
dummy_callback(uchar *page __attribute__((unused)),
85
pgcache_page_no_t page_no __attribute__((unused)),
86
uchar* data_ptr __attribute__((unused)))
93
@brief Dummy pagecache callback.
97
dummy_fail_callback(uchar* data_ptr __attribute__((unused)))
104
Get pseudo-random length of the field in (0;limit)
108
limit limit for generated value
111
length where length >= 0 & length < limit
114
static uint get_len(uint limit)
116
return (uint)((ulonglong)rand()*(limit-1)/RAND_MAX);
121
Check page's consistency: layout is
122
4 bytes: number 'num' of records in this page, then num occurences of
123
{ 4 bytes: record's length 'len'; then 4 bytes unchecked ('tag') then
124
'len' bytes each equal to the record's sequential number in this page,
125
modulo 256 }, then zeroes.
127
uint check_page(uchar *buff, ulong offset, int page_locked, int page_no,
130
uint end= sizeof(uint);
131
uint num= uint4korr(buff);
133
DBUG_ENTER("check_page");
135
for (i= 0; i < num; i++)
137
uint len= uint4korr(buff + end);
140
if (len + end > TEST_PAGE_SIZE)
142
diag("incorrect field header #%u by offset %lu\n", i, offset + end);
145
for(j= 0; j < len; j++)
147
if (buff[end + j] != (uchar)((i+1) % 256))
149
diag("incorrect %lu byte\n", offset + end + j);
155
for(i= end; i < TEST_PAGE_SIZE; i++)
161
("byte %lu (%lu + %u), page %u (%s, end: %u, recs: %u, tag: %d) should be 0\n",
162
offset + i, offset, i, page_no,
163
(page_locked ? "locked" : "unlocked"),
165
diag("byte %lu (%lu + %u), page %u (%s, end: %u, recs: %u, tag: %d) should be 0\n",
166
offset + i, offset, i, page_no,
167
(page_locked ? "locked" : "unlocked"),
169
h= my_open("wrong_page", O_CREAT | O_TRUNC | O_RDWR, MYF(0));
170
my_pwrite(h, (uchar*) buff, TEST_PAGE_SIZE, 0, MYF(0));
177
DBUG_PRINT("err", ("try to flush"));
180
pagecache_delete(&pagecache, &file1, page_no,
181
PAGECACHE_LOCK_LEFT_WRITELOCKED, 1);
185
flush_pagecache_blocks(&pagecache, &file1, FLUSH_RELEASE);
190
void put_rec(uchar *buff, uint end, uint len, uint tag)
194
num= uint4korr(buff);
197
if (end + 4*2 + len > TEST_PAGE_SIZE)
199
int4store(buff + end, len);
201
int4store(buff + end, tag);
204
int4store(buff, num);
205
for (i= end; i < (len + end); i++)
207
buff[i]= (uchar) num % 256;
212
Recreate and reopen a file for test
217
file_name Path (and name) of file which should be reset
220
void reset_file(PAGECACHE_FILE file, char *file_name)
222
flush_pagecache_blocks(&pagecache, &file1, FLUSH_RELEASE);
223
if (my_close(file1.file, MYF(0)) != 0)
225
diag("Got error during %s closing from close() (errno: %d)\n",
229
my_delete(file_name, MYF(0));
230
if ((file.file= my_open(file_name,
231
O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
233
diag("Got error during %s creation from open() (errno: %d)\n",
242
unsigned char *buffr= malloc(TEST_PAGE_SIZE);
245
for (i= 0; i < number_of_tests; i++)
247
uint page= get_len(number_of_pages);
248
pagecache_read(&pagecache, &file1, page, 3, buffr,
249
PAGECACHE_PLAIN_PAGE,
250
PAGECACHE_LOCK_LEFT_UNLOCKED,
252
check_page(buffr, page * TEST_PAGE_SIZE, 0, page, -num);
261
unsigned char *buffr= malloc(TEST_PAGE_SIZE);
264
for (i= 0; i < number_of_tests; i++)
267
uint page= get_len(number_of_pages);
268
pagecache_read(&pagecache, &file1, page, 3, buffr,
269
PAGECACHE_PLAIN_PAGE,
270
PAGECACHE_LOCK_WRITE,
272
end= check_page(buffr, page * TEST_PAGE_SIZE, 1, page, num);
273
put_rec(buffr, end, get_len(record_length_limit), num);
274
pagecache_write(&pagecache, &file1, page, 3, buffr,
275
PAGECACHE_PLAIN_PAGE,
276
PAGECACHE_LOCK_WRITE_UNLOCK,
278
PAGECACHE_WRITE_DELAY,
281
if (i % flush_divider == 0)
282
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
288
static void *test_thread_reader(void *arg)
290
int param=*((int*) arg);
293
DBUG_ENTER("test_reader");
294
DBUG_PRINT("enter", ("param: %d", param));
298
DBUG_PRINT("info", ("Thread %s ended", my_thread_name()));
299
pthread_mutex_lock(&LOCK_thread_count);
300
ok(1, "reader%d: done", param);
302
(void)(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
303
pthread_mutex_unlock(&LOCK_thread_count);
311
static void *test_thread_writer(void *arg)
313
int param=*((int*) arg);
316
DBUG_ENTER("test_writer");
317
DBUG_PRINT("enter", ("param: %d", param));
321
DBUG_PRINT("info", ("Thread %s ended", my_thread_name()));
322
pthread_mutex_lock(&LOCK_thread_count);
323
ok(1, "writer%d: done", param);
325
(void)(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
326
pthread_mutex_unlock(&LOCK_thread_count);
334
int main(int argc __attribute__((unused)),
335
char **argv __attribute__((unused)))
338
pthread_attr_t thr_attr;
339
int *param, error, pagen;
345
default_dbug_option= "d:t:i:O,\\test_pagecache_consist.trace";
347
default_dbug_option= "d:t:i:o,/tmp/test_pagecache_consist.trace";
351
DBUG_SET(default_dbug_option);
352
DBUG_SET_INITIAL(default_dbug_option);
358
DBUG_PRINT("info", ("Main thread: %s\n", my_thread_name()));
359
plan(number_of_writers + number_of_readers);
360
SKIP_BIG_TESTS(number_of_writers + number_of_readers)
363
if ((file1.file= my_open(file1_name,
364
O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
366
diag( "Got error during file1 creation from open() (errno: %d)\n",
370
pagecache_file_init(file1, &dummy_callback, &dummy_callback,
371
&dummy_fail_callback, &dummy_callback, NULL);
372
DBUG_PRINT("info", ("file1: %d", file1.file));
373
if (my_chmod(file1_name, S_IRWXU | S_IRWXG | S_IRWXO, MYF(MY_WME)))
375
my_pwrite(file1.file, (const uchar *)"test file", 9, 0, MYF(0));
377
if ((error= pthread_cond_init(&COND_thread_count, NULL)))
379
diag( "COND_thread_count: %d from pthread_cond_init (errno: %d)\n",
383
if ((error= pthread_mutex_init(&LOCK_thread_count, MY_MUTEX_INIT_FAST)))
385
diag( "LOCK_thread_count: %d from pthread_cond_init (errno: %d)\n",
390
if ((error= pthread_attr_init(&thr_attr)))
392
diag("Got error: %d from pthread_attr_init (errno: %d)\n",
396
if ((error= pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED)))
399
"Got error: %d from pthread_attr_setdetachstate (errno: %d)\n",
404
#ifdef HAVE_THR_SETCONCURRENCY
405
(void)(thr_setconcurrency(2));
408
if ((pagen= init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
409
TEST_PAGE_SIZE, 0)) == 0)
411
diag("Got error: init_pagecache() (errno: %d)\n",
415
DBUG_PRINT("info", ("Page cache %d pages", pagen));
417
unsigned char *buffr= malloc(TEST_PAGE_SIZE);
419
memset(buffr, '\0', TEST_PAGE_SIZE);
420
for (i= 0; i < number_of_pages; i++)
422
pagecache_write(&pagecache, &file1, i, 3, buffr,
423
PAGECACHE_PLAIN_PAGE,
424
PAGECACHE_LOCK_LEFT_UNLOCKED,
425
PAGECACHE_PIN_LEFT_UNPINNED,
426
PAGECACHE_WRITE_DELAY,
429
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
432
pthread_mutex_lock(&LOCK_thread_count);
433
while (number_of_readers != 0 || number_of_writers != 0)
435
if (number_of_readers != 0)
437
param=(int*) malloc(sizeof(int));
438
*param= number_of_readers;
439
if ((error= pthread_create(&tid, &thr_attr, test_thread_reader,
442
diag("Got error: %d from pthread_create (errno: %d)\n",
449
if (number_of_writers != 0)
451
param=(int*) malloc(sizeof(int));
452
*param= number_of_writers;
453
if ((error= pthread_create(&tid, &thr_attr, test_thread_writer,
456
diag("Got error: %d from pthread_create (errno: %d)\n",
464
DBUG_PRINT("info", ("Thread started"));
465
pthread_mutex_unlock(&LOCK_thread_count);
467
pthread_attr_destroy(&thr_attr);
470
pthread_mutex_lock(&LOCK_thread_count);
473
if ((error= pthread_cond_wait(&COND_thread_count,&LOCK_thread_count)))
474
diag("COND_thread_count: %d from pthread_cond_wait\n",error);
476
pthread_mutex_unlock(&LOCK_thread_count);
477
DBUG_PRINT("info", ("thread ended"));
479
end_pagecache(&pagecache, 1);
480
DBUG_PRINT("info", ("Page cache ended"));
482
if (my_close(file1.file, MYF(0)) != 0)
484
diag( "Got error during file1 closing from close() (errno: %d)\n",
488
my_delete(file1_name, MYF(0));
490
DBUG_PRINT("info", ("file1 (%d) closed", file1.file));
491
DBUG_PRINT("info", ("Program end"));
493
} /* SKIP_BIG_TESTS */
496
return exit_status();
500
#include "../ma_check_standalone.h"