MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mi_test3.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /* Test av locking */
17 
18 #include "myisam.h"
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
22 #endif
23 #ifndef WEXITSTATUS
24 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
25 #endif
26 #ifndef WIFEXITED
27 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
28 #endif
29 
30 
31 #if defined(HAVE_LRAND48)
32 #define rnd(X) (lrand48() % X)
33 #define rnd_init(X) srand48(X)
34 #else
35 #define rnd(X) (random() % X)
36 #define rnd_init(X) srandom(X)
37 #endif
38 
39 
40 const char *filename= "test3";
41 uint tests=10,forks=10,key_cacheing=0,use_log=0;
42 
43 static void get_options(int argc, char *argv[]);
44 void start_test(int id);
45 int test_read(MI_INFO *,int),test_write(MI_INFO *,int,int),
46  test_update(MI_INFO *,int,int),test_rrnd(MI_INFO *,int);
47 
48 struct record {
49  uchar id[8];
50  uchar nr[4];
51  uchar text[10];
52 } record;
53 
54 
55 int main(int argc,char **argv)
56 {
57  int status,wait_ret;
58  uint i=0;
59  MI_KEYDEF keyinfo[10];
60  MI_COLUMNDEF recinfo[10];
61  HA_KEYSEG keyseg[10][2];
62  MY_INIT(argv[0]);
63  get_options(argc,argv);
64 
65  memset(keyinfo, 0, sizeof(keyinfo));
66  memset(recinfo, 0, sizeof(recinfo));
67  memset(keyseg, 0, sizeof(keyseg));
68  keyinfo[0].seg= &keyseg[0][0];
69  keyinfo[0].seg[0].start=0;
70  keyinfo[0].seg[0].length=8;
71  keyinfo[0].seg[0].type=HA_KEYTYPE_TEXT;
72  keyinfo[0].seg[0].flag=HA_SPACE_PACK;
73  keyinfo[0].key_alg=HA_KEY_ALG_BTREE;
74  keyinfo[0].keysegs=1;
75  keyinfo[0].flag = (uint8) HA_PACK_KEY;
76  keyinfo[0].block_length= 0; /* Default block length */
77  keyinfo[1].seg= &keyseg[1][0];
78  keyinfo[1].seg[0].start=8;
79  keyinfo[1].seg[0].length=4; /* Long is always 4 in myisam */
80  keyinfo[1].seg[0].type=HA_KEYTYPE_LONG_INT;
81  keyinfo[1].seg[0].flag=0;
82  keyinfo[1].key_alg=HA_KEY_ALG_BTREE;
83  keyinfo[1].keysegs=1;
84  keyinfo[1].flag =HA_NOSAME;
85  keyinfo[1].block_length= 0; /* Default block length */
86 
87  recinfo[0].type=0;
88  recinfo[0].length=sizeof(record.id);
89  recinfo[1].type=0;
90  recinfo[1].length=sizeof(record.nr);
91  recinfo[2].type=0;
92  recinfo[2].length=sizeof(record.text);
93 
94  puts("- Creating myisam-file");
95  my_delete(filename,MYF(0)); /* Remove old locks under gdb */
96  if (mi_create(filename,2,&keyinfo[0],2,&recinfo[0],0,(MI_UNIQUEDEF*) 0,
97  (MI_CREATE_INFO*) 0,0))
98  exit(1);
99 
100  rnd_init(0);
101  printf("- Starting %d processes\n",forks); fflush(stdout);
102  for (i=0 ; i < forks; i++)
103  {
104  if (!fork())
105  {
106  start_test(i+1);
107  sleep(1);
108  return 0;
109  }
110  (void) rnd(1);
111  }
112 
113  for (i=0 ; i < forks ; i++)
114  while ((wait_ret=wait(&status)) && wait_ret == -1);
115  return 0;
116 }
117 
118 
119 static void get_options(int argc, char **argv)
120 {
121  char *pos,*progname;
122 
123  progname= argv[0];
124 
125  while (--argc >0 && *(pos = *(++argv)) == '-' ) {
126  switch(*++pos) {
127  case 'l':
128  use_log=1;
129  break;
130  case 'f':
131  forks=atoi(++pos);
132  break;
133  case 't':
134  tests=atoi(++pos);
135  break;
136  case 'K': /* Use key cacheing */
137  key_cacheing=1;
138  break;
139  case 'A': /* All flags */
140  use_log=key_cacheing=1;
141  break;
142  case '?':
143  case 'I':
144  case 'V':
145  printf("%s Ver 1.0 for %s at %s\n",progname,SYSTEM_TYPE,MACHINE_TYPE);
146  puts("By Monty, for your professional use\n");
147  puts("Test av locking with threads\n");
148  printf("Usage: %s [-?lKA] [-f#] [-t#]\n",progname);
149  exit(0);
150  case '#':
151  DBUG_PUSH (++pos);
152  break;
153  default:
154  printf("Illegal option: '%c'\n",*pos);
155  break;
156  }
157  }
158  return;
159 }
160 
161 
162 void start_test(int id)
163 {
164  uint i;
165  int error,lock_type;
166  MI_ISAMINFO isam_info;
167  MI_INFO *file,*file1,*file2=0,*lock;
168 
169  if (use_log)
170  mi_log(1);
171  if (!(file1=mi_open(filename,O_RDWR,HA_OPEN_WAIT_IF_LOCKED)) ||
172  !(file2=mi_open(filename,O_RDWR,HA_OPEN_WAIT_IF_LOCKED)))
173  {
174  fprintf(stderr,"Can't open isam-file: %s\n",filename);
175  exit(1);
176  }
177  if (key_cacheing && rnd(2) == 0)
178  init_key_cache(dflt_key_cache, KEY_CACHE_BLOCK_SIZE, 65536L, 0, 0);
179  printf("Process %d, pid: %d\n",id,getpid()); fflush(stdout);
180 
181  for (error=i=0 ; i < tests && !error; i++)
182  {
183  file= (rnd(2) == 1) ? file1 : file2;
184  lock=0 ; lock_type=0;
185  if (rnd(10) == 0)
186  {
187  if (mi_lock_database(lock=(rnd(2) ? file1 : file2),
188  lock_type=(rnd(2) == 0 ? F_RDLCK : F_WRLCK)))
189  {
190  fprintf(stderr,"%2d: start: Can't lock table %d\n",id,my_errno);
191  error=1;
192  break;
193  }
194  }
195  switch (rnd(4)) {
196  case 0: error=test_read(file,id); break;
197  case 1: error=test_rrnd(file,id); break;
198  case 2: error=test_write(file,id,lock_type); break;
199  case 3: error=test_update(file,id,lock_type); break;
200  }
201  if (lock)
202  mi_lock_database(lock,F_UNLCK);
203  }
204  if (!error)
205  {
206  mi_status(file1,&isam_info,HA_STATUS_VARIABLE);
207  printf("%2d: End of test. Records: %ld Deleted: %ld\n",
208  id,(long) isam_info.records, (long) isam_info.deleted);
209  fflush(stdout);
210  }
211 
212  mi_close(file1);
213  mi_close(file2);
214  if (use_log)
215  mi_log(0);
216  if (error)
217  {
218  printf("%2d: Aborted\n",id); fflush(stdout);
219  exit(1);
220  }
221 }
222 
223 
224 int test_read(MI_INFO *file,int id)
225 {
226  uint i,lock,found,next,prev;
227  ulong find;
228 
229  lock=0;
230  if (rnd(2) == 0)
231  {
232  lock=1;
233  if (mi_lock_database(file,F_RDLCK))
234  {
235  fprintf(stderr,"%2d: Can't lock table %d\n",id,my_errno);
236  return 1;
237  }
238  }
239 
240  found=next=prev=0;
241  for (i=0 ; i < 100 ; i++)
242  {
243  find=rnd(100000);
244  if (!mi_rkey(file,record.id,1,(uchar*) &find, HA_WHOLE_KEY,
245  HA_READ_KEY_EXACT))
246  found++;
247  else
248  {
249  if (my_errno != HA_ERR_KEY_NOT_FOUND)
250  {
251  fprintf(stderr,"%2d: Got error %d from read in read\n",id,my_errno);
252  return 1;
253  }
254  else if (!mi_rnext(file,record.id,1))
255  next++;
256  else
257  {
258  if (my_errno != HA_ERR_END_OF_FILE)
259  {
260  fprintf(stderr,"%2d: Got error %d from rnext in read\n",id,my_errno);
261  return 1;
262  }
263  else if (!mi_rprev(file,record.id,1))
264  prev++;
265  else
266  {
267  if (my_errno != HA_ERR_END_OF_FILE)
268  {
269  fprintf(stderr,"%2d: Got error %d from rnext in read\n",
270  id,my_errno);
271  return 1;
272  }
273  }
274  }
275  }
276  }
277  if (lock)
278  {
279  if (mi_lock_database(file,F_UNLCK))
280  {
281  fprintf(stderr,"%2d: Can't unlock table\n",id);
282  return 1;
283  }
284  }
285  printf("%2d: read: found: %5d next: %5d prev: %5d\n",
286  id,found,next,prev);
287  fflush(stdout);
288  return 0;
289 }
290 
291 
292 int test_rrnd(MI_INFO *file,int id)
293 {
294  uint count,lock;
295 
296  lock=0;
297  if (rnd(2) == 0)
298  {
299  lock=1;
300  if (mi_lock_database(file,F_RDLCK))
301  {
302  fprintf(stderr,"%2d: Can't lock table (%d)\n",id,my_errno);
303  mi_close(file);
304  return 1;
305  }
306  if (rnd(2) == 0)
307  mi_extra(file,HA_EXTRA_CACHE,0);
308  }
309 
310  count=0;
311  if (mi_rrnd(file,record.id,0L))
312  {
313  if (my_errno == HA_ERR_END_OF_FILE)
314  goto end;
315  fprintf(stderr,"%2d: Can't read first record (%d)\n",id,my_errno);
316  return 1;
317  }
318  for (count=1 ; !mi_rrnd(file,record.id,HA_OFFSET_ERROR) ;count++) ;
319  if (my_errno != HA_ERR_END_OF_FILE)
320  {
321  fprintf(stderr,"%2d: Got error %d from rrnd\n",id,my_errno);
322  return 1;
323  }
324 
325 end:
326  if (lock)
327  {
328  mi_extra(file,HA_EXTRA_NO_CACHE,0);
329  if (mi_lock_database(file,F_UNLCK))
330  {
331  fprintf(stderr,"%2d: Can't unlock table\n",id);
332  exit(0);
333  }
334  }
335  printf("%2d: rrnd: %5d\n",id,count); fflush(stdout);
336  return 0;
337 }
338 
339 
340 int test_write(MI_INFO *file,int id,int lock_type)
341 {
342  uint i,tries,count,lock;
343 
344  lock=0;
345  if (rnd(2) == 0 || lock_type == F_RDLCK)
346  {
347  lock=1;
348  if (mi_lock_database(file,F_WRLCK))
349  {
350  if (lock_type == F_RDLCK && my_errno == EDEADLK)
351  {
352  printf("%2d: write: deadlock\n",id); fflush(stdout);
353  return 0;
354  }
355  fprintf(stderr,"%2d: Can't lock table (%d)\n",id,my_errno);
356  mi_close(file);
357  return 1;
358  }
359  if (rnd(2) == 0)
360  mi_extra(file,HA_EXTRA_WRITE_CACHE,0);
361  }
362 
363  sprintf((char*) record.id,"%7d",getpid());
364  strnmov((char*) record.text,"Testing...", sizeof(record.text));
365 
366  tries=(uint) rnd(100)+10;
367  for (i=count=0 ; i < tries ; i++)
368  {
369  uint32 tmp=rnd(80000)+20000;
370  int4store(record.nr,tmp);
371  if (!mi_write(file,record.id))
372  count++;
373  else
374  {
375  if (my_errno != HA_ERR_FOUND_DUPP_KEY)
376  {
377  fprintf(stderr,"%2d: Got error %d (errno %d) from write\n",id,my_errno,
378  errno);
379  return 1;
380  }
381  }
382  }
383  if (lock)
384  {
385  mi_extra(file,HA_EXTRA_NO_CACHE,0);
386  if (mi_lock_database(file,F_UNLCK))
387  {
388  fprintf(stderr,"%2d: Can't unlock table\n",id);
389  exit(0);
390  }
391  }
392  printf("%2d: write: %5d\n",id,count); fflush(stdout);
393  return 0;
394 }
395 
396 
397 int test_update(MI_INFO *file,int id,int lock_type)
398 {
399  uint i,lock,found,next,prev,update;
400  uint32 tmp;
401  char find[4];
402  struct record new_record;
403 
404  lock=0;
405  if (rnd(2) == 0 || lock_type == F_RDLCK)
406  {
407  lock=1;
408  if (mi_lock_database(file,F_WRLCK))
409  {
410  if (lock_type == F_RDLCK && my_errno == EDEADLK)
411  {
412  printf("%2d: write: deadlock\n",id); fflush(stdout);
413  return 0;
414  }
415  fprintf(stderr,"%2d: Can't lock table (%d)\n",id,my_errno);
416  return 1;
417  }
418  }
419  memset(&new_record, 0, sizeof(new_record));
420  strmov((char*) new_record.text,"Updated");
421 
422  found=next=prev=update=0;
423  for (i=0 ; i < 100 ; i++)
424  {
425  tmp=rnd(100000);
426  int4store(find,tmp);
427  if (!mi_rkey(file,record.id,1,(uchar*) find, HA_WHOLE_KEY,
428  HA_READ_KEY_EXACT))
429  found++;
430  else
431  {
432  if (my_errno != HA_ERR_KEY_NOT_FOUND)
433  {
434  fprintf(stderr,"%2d: Got error %d from read in update\n",id,my_errno);
435  return 1;
436  }
437  else if (!mi_rnext(file,record.id,1))
438  next++;
439  else
440  {
441  if (my_errno != HA_ERR_END_OF_FILE)
442  {
443  fprintf(stderr,"%2d: Got error %d from rnext in update\n",
444  id,my_errno);
445  return 1;
446  }
447  else if (!mi_rprev(file,record.id,1))
448  prev++;
449  else
450  {
451  if (my_errno != HA_ERR_END_OF_FILE)
452  {
453  fprintf(stderr,"%2d: Got error %d from rnext in update\n",
454  id,my_errno);
455  return 1;
456  }
457  continue;
458  }
459  }
460  }
461  memcpy(new_record.id, record.id, sizeof(record.id));
462  tmp=rnd(20000)+40000;
463  int4store(new_record.nr,tmp);
464  if (!mi_update(file,record.id,new_record.id))
465  update++;
466  else
467  {
468  if (my_errno != HA_ERR_RECORD_CHANGED &&
469  my_errno != HA_ERR_RECORD_DELETED &&
470  my_errno != HA_ERR_FOUND_DUPP_KEY)
471  {
472  fprintf(stderr,"%2d: Got error %d from update\n",id,my_errno);
473  return 1;
474  }
475  }
476  }
477  if (lock)
478  {
479  if (mi_lock_database(file,F_UNLCK))
480  {
481  fprintf(stderr,"Can't unlock table,id, error%d\n",my_errno);
482  return 1;
483  }
484  }
485  printf("%2d: update: %5d\n",id,update); fflush(stdout);
486  return 0;
487 }
488 
489 #include "mi_extrafunc.h"