19 #include <sys/types.h> 
   20 #ifdef HAVE_SYS_WAIT_H 
   21 # include <sys/wait.h> 
   24 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) 
   27 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) 
   31 #if defined(HAVE_LRAND48) 
   32 #define rnd(X) (lrand48() % X) 
   33 #define rnd_init(X) srand48(X) 
   35 #define rnd(X) (random() % X) 
   36 #define rnd_init(X) srandom(X) 
   40 const char *filename= 
"test3";
 
   41 uint tests=10,forks=10,key_cacheing=0,use_log=0;
 
   43 static void get_options(
int argc, 
char *argv[]);
 
   44 void start_test(
int id);
 
   55 int main(
int argc,
char **argv)
 
   63   get_options(argc,argv);
 
   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;
 
   75   keyinfo[0].flag = (uint8) HA_PACK_KEY;
 
   76   keyinfo[0].block_length= 0;                   
 
   77   keyinfo[1].seg= &keyseg[1][0];
 
   78   keyinfo[1].seg[0].start=8;
 
   79   keyinfo[1].seg[0].length=4;           
 
   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;
 
   84   keyinfo[1].flag =HA_NOSAME;
 
   85   keyinfo[1].block_length= 0;                   
 
   88   recinfo[0].length=
sizeof(
record.id);
 
   90   recinfo[1].length=
sizeof(
record.nr);
 
   92   recinfo[2].length=
sizeof(
record.text);
 
   94   puts(
"- Creating myisam-file");
 
   95   my_delete(filename,MYF(0));           
 
   96   if (mi_create(filename,2,&keyinfo[0],2,&recinfo[0],0,(
MI_UNIQUEDEF*) 0,
 
  101   printf(
"- Starting %d processes\n",forks); fflush(stdout);
 
  102   for (i=0 ; i < forks; i++)
 
  113   for (i=0 ; i < forks ; i++)
 
  114     while ((wait_ret=wait(&status)) && wait_ret == -1);
 
  119 static void get_options(
int argc, 
char **argv)
 
  125   while (--argc >0 && *(pos = *(++argv)) == 
'-' ) {
 
  140       use_log=key_cacheing=1;
 
  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);
 
  154       printf(
"Illegal option: '%c'\n",*pos);
 
  162 void start_test(
int id)
 
  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)))
 
  174     fprintf(stderr,
"Can't open isam-file: %s\n",filename);
 
  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);
 
  181   for (error=i=0 ; i < tests && !error; i++)
 
  183     file= (rnd(2) == 1) ? file1 : file2;
 
  184     lock=0 ; lock_type=0;
 
  187       if (mi_lock_database(lock=(rnd(2) ? file1 : file2),
 
  188                            lock_type=(rnd(2) == 0 ? F_RDLCK : F_WRLCK)))
 
  190         fprintf(stderr,
"%2d: start: Can't lock table %d\n",
id,my_errno);
 
  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;
 
  202       mi_lock_database(lock,F_UNLCK);
 
  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);
 
  218     printf(
"%2d: Aborted\n",
id); fflush(stdout);
 
  224 int test_read(
MI_INFO *file,
int id)
 
  226   uint 
i,lock,found,next,prev;
 
  233     if (mi_lock_database(file,F_RDLCK))
 
  235       fprintf(stderr,
"%2d: Can't lock table %d\n",
id,my_errno);
 
  241   for (i=0 ; i < 100 ; i++)
 
  244     if (!mi_rkey(file,
record.id,1,(uchar*) &find, HA_WHOLE_KEY,
 
  249       if (my_errno != HA_ERR_KEY_NOT_FOUND)
 
  251         fprintf(stderr,
"%2d: Got error %d from read in read\n",
id,my_errno);
 
  254       else if (!mi_rnext(file,
record.id,1))
 
  258         if (my_errno != HA_ERR_END_OF_FILE)
 
  260           fprintf(stderr,
"%2d: Got error %d from rnext in read\n",
id,my_errno);
 
  263         else if (!mi_rprev(file,
record.id,1))
 
  267           if (my_errno != HA_ERR_END_OF_FILE)
 
  269             fprintf(stderr,
"%2d: Got error %d from rnext in read\n",
 
  279     if (mi_lock_database(file,F_UNLCK))
 
  281       fprintf(stderr,
"%2d: Can't unlock table\n",
id);
 
  285   printf(
"%2d: read:   found: %5d  next: %5d   prev: %5d\n",
 
  292 int test_rrnd(
MI_INFO *file,
int id)
 
  300     if (mi_lock_database(file,F_RDLCK))
 
  302       fprintf(stderr,
"%2d: Can't lock table (%d)\n",
id,my_errno);
 
  307       mi_extra(file,HA_EXTRA_CACHE,0);
 
  311   if (mi_rrnd(file,
record.id,0L))
 
  313     if (my_errno == HA_ERR_END_OF_FILE)
 
  315     fprintf(stderr,
"%2d: Can't read first record (%d)\n",
id,my_errno);
 
  318   for (count=1 ; !mi_rrnd(file,
record.id,HA_OFFSET_ERROR) ;count++) ;
 
  319   if (my_errno != HA_ERR_END_OF_FILE)
 
  321     fprintf(stderr,
"%2d: Got error %d from rrnd\n",
id,my_errno);
 
  328     mi_extra(file,HA_EXTRA_NO_CACHE,0);
 
  329     if (mi_lock_database(file,F_UNLCK))
 
  331       fprintf(stderr,
"%2d: Can't unlock table\n",
id);
 
  335   printf(
"%2d: rrnd:   %5d\n",
id,count); fflush(stdout);
 
  340 int test_write(
MI_INFO *file,
int id,
int lock_type)
 
  342   uint 
i,tries,count,lock;
 
  345   if (rnd(2) == 0 || lock_type == F_RDLCK)
 
  348     if (mi_lock_database(file,F_WRLCK))
 
  350       if (lock_type == F_RDLCK && my_errno == EDEADLK)
 
  352         printf(
"%2d: write:  deadlock\n",
id); fflush(stdout);
 
  355       fprintf(stderr,
"%2d: Can't lock table (%d)\n",
id,my_errno);
 
  360       mi_extra(file,HA_EXTRA_WRITE_CACHE,0);
 
  363   sprintf((
char*) 
record.id,
"%7d",getpid());
 
  364   strnmov((
char*) 
record.text,
"Testing...", 
sizeof(
record.text));
 
  366   tries=(uint) rnd(100)+10;
 
  367   for (i=count=0 ; i < tries ; i++)
 
  369     uint32 tmp=rnd(80000)+20000;
 
  371     if (!mi_write(file,
record.id))
 
  375       if (my_errno != HA_ERR_FOUND_DUPP_KEY)
 
  377         fprintf(stderr,
"%2d: Got error %d (errno %d) from write\n",
id,my_errno,
 
  385     mi_extra(file,HA_EXTRA_NO_CACHE,0);
 
  386     if (mi_lock_database(file,F_UNLCK))
 
  388       fprintf(stderr,
"%2d: Can't unlock table\n",
id);
 
  392   printf(
"%2d: write:  %5d\n",
id,count); fflush(stdout);
 
  397 int test_update(
MI_INFO *file,
int id,
int lock_type)
 
  399   uint 
i,lock,found,next,prev,update;
 
  405   if (rnd(2) == 0 || lock_type == F_RDLCK)
 
  408     if (mi_lock_database(file,F_WRLCK))
 
  410       if (lock_type == F_RDLCK && my_errno == EDEADLK)
 
  412         printf(
"%2d: write:  deadlock\n",
id); fflush(stdout);
 
  415       fprintf(stderr,
"%2d: Can't lock table (%d)\n",
id,my_errno);
 
  419   memset(&new_record, 0, 
sizeof(new_record));
 
  420   strmov((
char*) new_record.text,
"Updated");
 
  422   found=next=prev=update=0;
 
  423   for (i=0 ; i < 100 ; i++)
 
  427     if (!mi_rkey(file,
record.id,1,(uchar*) find, HA_WHOLE_KEY,
 
  432       if (my_errno != HA_ERR_KEY_NOT_FOUND)
 
  434         fprintf(stderr,
"%2d: Got error %d from read in update\n",
id,my_errno);
 
  437       else if (!mi_rnext(file,
record.id,1))
 
  441         if (my_errno != HA_ERR_END_OF_FILE)
 
  443           fprintf(stderr,
"%2d: Got error %d from rnext in update\n",
 
  447         else if (!mi_rprev(file,
record.id,1))
 
  451           if (my_errno != HA_ERR_END_OF_FILE)
 
  453             fprintf(stderr,
"%2d: Got error %d from rnext in update\n",
 
  462     tmp=rnd(20000)+40000;
 
  463     int4store(new_record.nr,tmp);
 
  464     if (!mi_update(file,
record.id,new_record.id))
 
  468       if (my_errno != HA_ERR_RECORD_CHANGED &&
 
  469           my_errno != HA_ERR_RECORD_DELETED &&
 
  470           my_errno != HA_ERR_FOUND_DUPP_KEY)
 
  472         fprintf(stderr,
"%2d: Got error %d from update\n",
id,my_errno);
 
  479     if (mi_lock_database(file,F_UNLCK))
 
  481       fprintf(stderr,
"Can't unlock table,id, error%d\n",my_errno);
 
  485   printf(
"%2d: update: %5d\n",
id,update); fflush(stdout);
 
  489 #include "mi_extrafunc.h"