1 /* Copyright (C) 2006-2008 MySQL AB
2 
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.
6 
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.
11 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
15 
16 /*
17   TODO: use pthread_join instead of wait_for_thread_count_to_be_zero, like in
18   my_atomic-t.c (see BUG#22320).
19 */
20 
21 #include <tap.h>
22 #include <my_sys.h>
23 #include <m_string.h>
24 #include "test_file.h"
25 #include <tap.h>
26 
27 #define PCACHE_SIZE (TEST_PAGE_SIZE*1024*8)
28 
29 #ifndef DBUG_OFF
30 static const char* default_dbug_option;
31 #endif
32 
33 static const char *base_file1_name= "page_cache_test_file_1";
34 static char file1_name[FN_REFLEN];
35 static PAGECACHE_FILE file1;
36 static pthread_cond_t COND_thread_count;
37 static pthread_mutex_t LOCK_thread_count;
38 static uint thread_count;
39 static PAGECACHE pagecache;
40 
41 #ifdef TEST_HIGH_CONCURENCY
42 static uint number_of_readers= 10;
43 static uint number_of_writers= 20;
44 static uint number_of_tests= 30000;
45 static uint record_length_limit= TEST_PAGE_SIZE/200;
46 static uint number_of_pages= 20;
47 static uint flush_divider= 1000;
48 #else /*TEST_HIGH_CONCURENCY*/
49 #ifdef TEST_READERS
50 static uint number_of_readers= 10;
51 static uint number_of_writers= 1;
52 static uint number_of_tests= 30000;
53 static uint record_length_limit= TEST_PAGE_SIZE/200;
54 static uint number_of_pages= 20;
55 static uint flush_divider= 1000;
56 #undef SKIP_BIG_TESTS
57 #define SKIP_BIG_TESTS(X) /* no-op */
58 #else /*TEST_READERS*/
59 #ifdef TEST_WRITERS
60 static uint number_of_readers= 0;
61 static uint number_of_writers= 10;
62 static uint number_of_tests= 30000;
63 static uint record_length_limit= TEST_PAGE_SIZE/200;
64 static uint number_of_pages= 20;
65 static uint flush_divider= 1000;
66 #undef SKIP_BIG_TESTS
67 #define SKIP_BIG_TESTS(X) /* no-op */
68 #else /*TEST_WRITERS*/
69 static uint number_of_readers= 10;
70 static uint number_of_writers= 10;
71 static uint number_of_tests= 50000;
72 static uint record_length_limit= TEST_PAGE_SIZE/200;
73 static uint number_of_pages= 20000;
74 static uint flush_divider= 1000;
75 #endif /*TEST_WRITERS*/
76 #endif /*TEST_READERS*/
77 #endif /*TEST_HIGH_CONCURENCY*/
78 
79 
80 /*
81   Get pseudo-random length of the field in (0;limit)
82 
83   SYNOPSYS
84     get_len()
85     limit                limit for generated value
86 
87   RETURN
88     length where length >= 0 & length < limit
89 */
90 
get_len(uint limit)91 static uint get_len(uint limit)
92 {
93   return (uint)((ulonglong)rand()*(limit-1)/RAND_MAX);
94 }
95 
96 
97 /*
98   Check page's consistency: layout is
99   4 bytes: number 'num' of records in this page, then num occurences of
100   { 4 bytes: record's length 'len'; then 4 bytes unchecked ('tag') then
101   'len' bytes each equal to the record's sequential number in this page,
102   modulo 256 }, then zeroes.
103  */
check_page(uchar * buff,ulong offset,int page_locked,int page_no,int tag)104 uint check_page(uchar *buff, ulong offset, int page_locked, int page_no,
105                 int tag)
106 {
107   uint end= sizeof(uint);
108   uint num= uint4korr(buff);
109   uint i;
110   DBUG_ENTER("check_page");
111 
112   for (i= 0; i < num; i++)
113   {
114     uint len= uint4korr(buff + end);
115     uint j;
116     end+= 4 + 4;
117     if (len + end > TEST_PAGE_SIZE)
118     {
119       diag("incorrect field header #%u by offset %lu\n", i, offset + end);
120       goto err;
121     }
122     for(j= 0; j < len; j++)
123     {
124       if (buff[end + j] != (uchar)((i+1) % 256))
125       {
126         diag("incorrect %lu byte\n", offset + end + j);
127         goto err;
128       }
129     }
130     end+= len;
131   }
132   for(i= end; i < TEST_PAGE_SIZE; i++)
133   {
134     if (buff[i] != 0)
135     {
136       int h;
137       DBUG_PRINT("err",
138                  ("byte %lu (%lu + %u), page %u (%s, end: %u, recs: %u, tag: %d) should be 0\n",
139                   offset + i, offset, i, page_no,
140                   (page_locked ? "locked" : "unlocked"),
141                   end, num, tag));
142       diag("byte %lu (%lu + %u), page %u (%s, end: %u, recs: %u, tag: %d) should be 0\n",
143            offset + i, offset, i, page_no,
144            (page_locked ? "locked" : "unlocked"),
145            end, num, tag);
146       h= my_open("wrong_page", O_CREAT | O_TRUNC | O_RDWR, MYF(0));
147       my_pwrite(h, (uchar*) buff, TEST_PAGE_SIZE, 0, MYF(0));
148       my_close(h, MYF(0));
149       goto err;
150     }
151   }
152   DBUG_RETURN(end);
153 err:
154   DBUG_PRINT("err", ("try to flush"));
155   if (page_locked)
156   {
157     pagecache_delete(&pagecache, &file1, page_no,
158                      PAGECACHE_LOCK_LEFT_WRITELOCKED, 1);
159   }
160   else
161   {
162     flush_pagecache_blocks(&pagecache, &file1, FLUSH_RELEASE);
163   }
164   exit(1);
165 }
166 
put_rec(uchar * buff,uint end,uint len,uint tag)167 void put_rec(uchar *buff, uint end, uint len, uint tag)
168 {
169   uint i;
170   uint num;
171   num= uint4korr(buff);
172   if (!len)
173     len= 1;
174   if (end + 4*2 + len > TEST_PAGE_SIZE)
175     return;
176   int4store(buff + end, len);
177   end+=  4;
178   int4store(buff + end, tag);
179   end+=  4;
180   num++;
181   int4store(buff, num);
182   for (i= end; i < (len + end); i++)
183   {
184     buff[i]= (uchar) num % 256;
185   }
186 }
187 
188 /*
189   Recreate and reopen a file for test
190 
191   SYNOPSIS
192     reset_file()
193     file                 File to reset
194     file_name            Path (and name) of file which should be reset
195 */
196 
reset_file(PAGECACHE_FILE file,char * file_name)197 void reset_file(PAGECACHE_FILE file, char *file_name)
198 {
199   flush_pagecache_blocks(&pagecache, &file1, FLUSH_RELEASE);
200   if (my_close(file1.file, MYF(0)) != 0)
201   {
202     diag("Got error during %s closing from close() (errno: %d)\n",
203          file_name, errno);
204     exit(1);
205   }
206   my_delete(file_name, MYF(0));
207   if ((file.file= my_open(file_name,
208                           O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
209   {
210     diag("Got error during %s creation from open() (errno: %d)\n",
211          file_name, errno);
212     exit(1);
213   }
214 }
215 
216 
reader(int num)217 void reader(int num)
218 {
219   unsigned char *buffr= malloc(TEST_PAGE_SIZE);
220   uint i;
221 
222   for (i= 0; i < number_of_tests; i++)
223   {
224     uint page= get_len(number_of_pages);
225     pagecache_read(&pagecache, &file1, page, 3, buffr,
226                    PAGECACHE_PLAIN_PAGE,
227                    PAGECACHE_LOCK_LEFT_UNLOCKED,
228                    0);
229     check_page(buffr, page * TEST_PAGE_SIZE, 0, page, -num);
230 
231   }
232   free(buffr);
233 }
234 
235 
writer(int num)236 void writer(int num)
237 {
238   unsigned char *buffr= malloc(TEST_PAGE_SIZE);
239   uint i;
240 
241   for (i= 0; i < number_of_tests; i++)
242   {
243     uint end;
244     uint page= get_len(number_of_pages);
245     pagecache_read(&pagecache, &file1, page, 3, buffr,
246                    PAGECACHE_PLAIN_PAGE,
247                    PAGECACHE_LOCK_WRITE,
248                    0);
249     end= check_page(buffr, page * TEST_PAGE_SIZE, 1, page, num);
250     put_rec(buffr, end, get_len(record_length_limit), num);
251     pagecache_write(&pagecache, &file1, page, 3, buffr,
252                     PAGECACHE_PLAIN_PAGE,
253                     PAGECACHE_LOCK_WRITE_UNLOCK,
254                     PAGECACHE_UNPIN,
255                     PAGECACHE_WRITE_DELAY,
256                     0, LSN_IMPOSSIBLE);
257 
258     if (i % flush_divider == 0)
259       flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
260   }
261   free(buffr);
262 }
263 
264 
test_thread_reader(void * arg)265 static void *test_thread_reader(void *arg)
266 {
267   int param=*((int*) arg);
268   my_thread_init();
269   {
270     DBUG_ENTER("test_reader");
271     DBUG_PRINT("enter", ("param: %d", param));
272 
273     reader(param);
274 
275     DBUG_PRINT("info", ("Thread %s ended", my_thread_name()));
276     pthread_mutex_lock(&LOCK_thread_count);
277     ok(1, "reader%d: done", param);
278     thread_count--;
279     pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
280     pthread_mutex_unlock(&LOCK_thread_count);
281     free((uchar*) arg);
282     my_thread_end();
283   }
284   return 0;
285 }
286 
287 
test_thread_writer(void * arg)288 static void *test_thread_writer(void *arg)
289 {
290   int param=*((int*) arg);
291   my_thread_init();
292   {
293     DBUG_ENTER("test_writer");
294     DBUG_PRINT("enter", ("param: %d", param));
295 
296     writer(param);
297 
298     DBUG_PRINT("info", ("Thread %s ended", my_thread_name()));
299     pthread_mutex_lock(&LOCK_thread_count);
300     ok(1, "writer%d: done", param);
301     thread_count--;
302     pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
303     pthread_mutex_unlock(&LOCK_thread_count);
304     free((uchar*) arg);
305     my_thread_end();
306   }
307   return 0;
308 }
309 
create_tmpdir(const char * progname)310 static char *create_tmpdir(const char *progname)
311 {
312   static char test_dirname[FN_REFLEN];
313   char tmp_name[FN_REFLEN];
314   size_t length;
315 
316   /* Create a temporary directory of name TMP-'executable', but without the -t extension */
317   fn_format(tmp_name, progname, "", "", MY_REPLACE_DIR | MY_REPLACE_EXT);
318   length= strlen(tmp_name);
319   if (length > 2 && tmp_name[length-2] == '-' && tmp_name[length-1] == 't')
320     tmp_name[length-2]= 0;
321   strxmov(test_dirname, "TMP-", tmp_name, NullS);
322 
323   /*
324     Don't give an error if we can't create dir, as it may already exist from a previously aborted
325     run
326   */
327   (void) my_mkdir(test_dirname, 0777, MYF(0));
328   return test_dirname;
329 }
330 
331 
main(int argc,char ** argv)332 int main(int argc __attribute__((unused)),
333          char **argv __attribute__((unused)))
334 {
335   pthread_t tid;
336   pthread_attr_t thr_attr;
337   int *param, error;
338   size_t pagen;
339   MY_INIT(argv[0]);
340 
341 #ifndef DBUG_OFF
342 #if defined(__WIN__)
343   default_dbug_option= "d:t:i:O,\\test_pagecache_consist.trace";
344 #else
345   default_dbug_option= "d:t:i:o,/tmp/test_pagecache_consist.trace";
346 #endif
347   if (argc > 1)
348   {
349     DBUG_SET(default_dbug_option);
350     DBUG_SET_INITIAL(default_dbug_option);
351   }
352 #endif
353 
354   {
355   DBUG_ENTER("main");
356   DBUG_PRINT("info", ("Main thread: %s\n", my_thread_name()));
357   plan(number_of_writers + number_of_readers);
358 
359   SKIP_BIG_TESTS(number_of_writers + number_of_readers)
360   {
361 
362   char *test_dirname= create_tmpdir(argv[0]);
363   fn_format(file1_name, base_file1_name, test_dirname, "", MYF(0));
364 
365   if ((file1.file= my_open(file1_name,
366                            O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
367   {
368     diag( "Got error during file1 creation from open() (errno: %d)\n",
369 	    errno);
370     exit(1);
371   }
372 
373   pagecache_file_set_null_hooks(&file1);
374   DBUG_PRINT("info", ("file1: %d", file1.file));
375   if (my_chmod(file1_name, 0777, MYF(MY_WME)))
376     exit(1);
377   my_pwrite(file1.file, (const uchar *)"test file", 9, 0, MYF(0));
378 
379   if ((error= pthread_cond_init(&COND_thread_count, NULL)))
380   {
381     diag( "COND_thread_count: %d from pthread_cond_init (errno: %d)\n",
382 	    error, errno);
383     exit(1);
384   }
385   if ((error= pthread_mutex_init(&LOCK_thread_count, MY_MUTEX_INIT_FAST)))
386   {
387     diag( "LOCK_thread_count: %d from pthread_cond_init (errno: %d)\n",
388 	    error, errno);
389     exit(1);
390   }
391 
392   if ((error= pthread_attr_init(&thr_attr)))
393   {
394     diag("Got error: %d from pthread_attr_init (errno: %d)\n",
395 	    error,errno);
396     exit(1);
397   }
398   if ((error= pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED)))
399   {
400     diag(
401 	    "Got error: %d from pthread_attr_setdetachstate (errno: %d)\n",
402 	    error,errno);
403     exit(1);
404   }
405 
406 #ifdef HAVE_THR_SETCONCURRENCY
407   thr_setconcurrency(2);
408 #endif
409 
410   if ((pagen= init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
411                              TEST_PAGE_SIZE, 0, 0)) == 0)
412   {
413     diag("Got error: init_pagecache() (errno: %d)\n",
414             errno);
415     exit(1);
416   }
417   DBUG_PRINT("info", ("Page cache %zd pages", pagen));
418   {
419     unsigned char *buffr= malloc(TEST_PAGE_SIZE);
420     uint i;
421     memset(buffr, '\0', TEST_PAGE_SIZE);
422     for (i= 0; i < number_of_pages; i++)
423     {
424       pagecache_write(&pagecache, &file1, i, 3, buffr,
425                       PAGECACHE_PLAIN_PAGE,
426                       PAGECACHE_LOCK_LEFT_UNLOCKED,
427                       PAGECACHE_PIN_LEFT_UNPINNED,
428                       PAGECACHE_WRITE_DELAY,
429                       0, LSN_IMPOSSIBLE);
430     }
431     flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
432     free(buffr);
433   }
434   pthread_mutex_lock(&LOCK_thread_count);
435   while (number_of_readers != 0 || number_of_writers != 0)
436   {
437     if (number_of_readers != 0)
438     {
439       param=(int*) malloc(sizeof(int));
440       *param= number_of_readers;
441       if ((error= pthread_create(&tid, &thr_attr, test_thread_reader,
442                                  (void*) param)))
443       {
444         diag("Got error: %d from pthread_create (errno: %d)\n",
445                 error,errno);
446         exit(1);
447       }
448       thread_count++;
449       number_of_readers--;
450     }
451     if (number_of_writers != 0)
452     {
453       param=(int*) malloc(sizeof(int));
454       *param= number_of_writers;
455       if ((error= pthread_create(&tid, &thr_attr, test_thread_writer,
456                                  (void*) param)))
457       {
458         diag("Got error: %d from pthread_create (errno: %d)\n",
459                 error,errno);
460         exit(1);
461       }
462       thread_count++;
463       number_of_writers--;
464     }
465   }
466   DBUG_PRINT("info", ("Thread started"));
467   pthread_mutex_unlock(&LOCK_thread_count);
468 
469   pthread_attr_destroy(&thr_attr);
470 
471   /* wait finishing */
472   pthread_mutex_lock(&LOCK_thread_count);
473   while (thread_count)
474   {
475     if ((error= pthread_cond_wait(&COND_thread_count,&LOCK_thread_count)))
476       diag("COND_thread_count: %d from pthread_cond_wait\n",error);
477   }
478   pthread_mutex_unlock(&LOCK_thread_count);
479   DBUG_PRINT("info", ("thread ended"));
480 
481   flush_pagecache_blocks(&pagecache, &file1, FLUSH_IGNORE_CHANGED);
482   end_pagecache(&pagecache, 1);
483   DBUG_PRINT("info", ("Page cache ended"));
484 
485   if (my_close(file1.file, MYF(0)) != 0)
486   {
487     diag( "Got error during file1 closing from close() (errno: %d)\n",
488 	    errno);
489     exit(1);
490   }
491   my_delete(file1_name, MYF(0));
492 
493   DBUG_PRINT("info", ("file1 (%d) closed", file1.file));
494   DBUG_PRINT("info", ("Program end"));
495 
496   rmdir(test_dirname);
497   } /* SKIP_BIG_TESTS */
498   my_end(0);
499 
500   return exit_status();
501   }
502 }
503 
504 #include "../ma_check_standalone.h"
505