test3.c
Upload User: tsgydb
Upload Date: 2007-04-14
Package Size: 10674k
Code Size: 11k
Category:

MySQL

Development Platform:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16. /* Test av locking */
  17. #include "nisam.h"
  18. #include <sys/types.h>
  19. #ifdef HAVE_SYS_WAIT_H
  20. # include <sys/wait.h>
  21. #endif
  22. #ifndef WEXITSTATUS
  23. # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  24. #endif
  25. #ifndef WIFEXITED
  26. # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  27. #endif
  28. #if defined(HAVE_LRAND48)
  29. #define rnd(X) (lrand48() % X)
  30. #define rnd_init(X) srand48(X)
  31. #else
  32. #define rnd(X) (random() % X)
  33. #define rnd_init(X) srandom(X)
  34. #endif
  35. const char *filename= "test3.ISM";
  36. uint tests=10,forks=10,key_cacheing=0,use_log=0;
  37. static void get_options(int argc, char *argv[]);
  38. void start_test(int id);
  39. int test_read(N_INFO *,int),test_write(N_INFO *,int,int),
  40.     test_update(N_INFO *,int,int),test_rrnd(N_INFO *,int);
  41. struct record {
  42.   char id[8];
  43.   uint32 nr;
  44.   char text[10];
  45. } record;
  46. int main(int argc,char **argv)
  47. {
  48.   int status,wait_ret;
  49.   uint i;
  50.   N_KEYDEF keyinfo[10];
  51.   N_RECINFO recinfo[10];
  52.   MY_INIT(argv[0]);
  53.   get_options(argc,argv);
  54.   keyinfo[0].seg[0].base.start=0;
  55.   keyinfo[0].seg[0].base.length=8;
  56.   keyinfo[0].seg[0].base.type=HA_KEYTYPE_TEXT;
  57.   keyinfo[0].seg[0].base.flag=HA_SPACE_PACK;
  58.   keyinfo[0].seg[1].base.type=0;
  59.   keyinfo[0].base.flag = (uint8) HA_PACK_KEY;
  60.   keyinfo[1].seg[0].base.start=8;
  61.   keyinfo[1].seg[0].base.length=sizeof(uint32);
  62.   keyinfo[1].seg[0].base.type=HA_KEYTYPE_LONG_INT;
  63.   keyinfo[1].seg[0].base.flag=0;
  64.   keyinfo[1].seg[1].base.type=0;
  65.   keyinfo[1].base.flag =HA_NOSAME;
  66.   recinfo[0].base.type=0;
  67.   recinfo[0].base.length=sizeof(record.id);
  68.   recinfo[1].base.type=0;
  69.   recinfo[1].base.length=sizeof(record.nr);
  70.   recinfo[2].base.type=0;
  71.   recinfo[2].base.length=sizeof(record.text);
  72.   recinfo[3].base.type=FIELD_LAST;
  73.   puts("- Creating isam-file");
  74.   my_delete(filename,MYF(0)); /* Remove old locks under gdb */
  75.   if (nisam_create(filename,2,&keyinfo[0],&recinfo[0],10000,0,0,0,0L))
  76.     exit(1);
  77.   rnd_init(0);
  78.   printf("- Starting %d processesn",forks); fflush(stdout);
  79.   for (i=0 ; i < forks; i++)
  80.   {
  81.     if (!fork())
  82.     {
  83.       start_test(i+1);
  84.       sleep(1);
  85.       return 0;
  86.     }
  87.     VOID(rnd(1));
  88.   }
  89.   for (i=0 ; i < forks ; i++)
  90.     while ((wait_ret=wait(&status)) && wait_ret == -1);
  91.   return 0;
  92. }
  93. static void get_options(argc,argv)
  94. int argc;
  95. char *argv[];
  96. {
  97.   char *pos,*progname;
  98.   DEBUGGER_OFF;
  99.   progname= argv[0];
  100.   while (--argc >0 && *(pos = *(++argv)) == '-' ) {
  101.     switch(*++pos) {
  102.     case 'l':
  103.       use_log=1;
  104.       break;
  105.     case 'f':
  106.       forks=atoi(++pos);
  107.       break;
  108.     case 't':
  109.       tests=atoi(++pos);
  110.       break;
  111.     case 'K': /* Use key cacheing */
  112.       key_cacheing=1;
  113.       break;
  114.     case 'A': /* All flags */
  115.       use_log=key_cacheing=1;
  116.       break;
  117.    case '?':
  118.     case 'I':
  119.     case 'V':
  120.       printf("%s  Ver 1.0 for %s at %sn",progname,SYSTEM_TYPE,MACHINE_TYPE);
  121.       puts("TCX Datakonsult AB, by Monty, for your professional usen");
  122.       puts("Test av locking with threadsn");
  123.       printf("Usage: %s [-?lKA] [-f#] [-t#]n",progname);
  124.       exit(0);
  125.     case '#':
  126.       DEBUGGER_ON;
  127.       DBUG_PUSH (++pos);
  128.       break;
  129.     default:
  130.       printf("Illegal option: '%c'n",*pos);
  131.       break;
  132.     }
  133.   }
  134.   return;
  135. }
  136. void start_test(int id)
  137. {
  138.   uint i;
  139.   int error,lock_type;
  140.   N_ISAMINFO isam_info;
  141.   N_INFO *file,*file1,*file2,*lock;
  142.   if (use_log)
  143.     nisam_log(1);
  144.   if (!(file1=nisam_open(filename,O_RDWR,HA_OPEN_WAIT_IF_LOCKED)) ||
  145.       !(file2=nisam_open(filename,O_RDWR,HA_OPEN_WAIT_IF_LOCKED)))
  146.   {
  147.     fprintf(stderr,"Can't open isam-file: %sn",filename);
  148.     exit(1);
  149.   }
  150.   if (key_cacheing && rnd(2) == 0)
  151.     init_key_cache(65536L,(uint) IO_SIZE*4*10);
  152.   printf("Process %d, pid: %dn",id,getpid()); fflush(stdout);
  153.   for (error=i=0 ; i < tests && !error; i++)
  154.   {
  155.     file= (rnd(2) == 1) ? file1 : file2;
  156.     lock=0 ; lock_type=0;
  157.     if (rnd(10) == 0)
  158.     {
  159.       if (nisam_lock_database(lock=(rnd(2) ? file1 : file2),
  160.    lock_type=(rnd(2) == 0 ? F_RDLCK : F_WRLCK)))
  161.       {
  162. fprintf(stderr,"%2d: start: Can't lock table %dn",id,my_errno);
  163. error=1;
  164. break;
  165.       }
  166.     }
  167.     switch (rnd(4)) {
  168.     case 0: error=test_read(file,id); break;
  169.     case 1: error=test_rrnd(file,id); break;
  170.     case 2: error=test_write(file,id,lock_type); break;
  171.     case 3: error=test_update(file,id,lock_type); break;
  172.     }
  173.     if (lock)
  174.       nisam_lock_database(lock,F_UNLCK);
  175.   }
  176.   if (!error)
  177.   {
  178.     nisam_info(file1,&isam_info,0);
  179.     printf("%2d: End of test.  Records:  %ld  Deleted:  %ldn",
  180.    id,isam_info.records,isam_info.deleted);
  181.     fflush(stdout);
  182.   }
  183.   nisam_close(file1);
  184.   nisam_close(file2);
  185.   if (use_log)
  186.     nisam_log(0);
  187.   if (error)
  188.   {
  189.     printf("%2d: Abortedn",id); fflush(stdout);
  190.     exit(1);
  191.   }
  192. }
  193. int test_read(N_INFO *file,int id)
  194. {
  195.   uint i,lock,found,next,prev;
  196.   ulong find;
  197.   lock=0;
  198.   if (rnd(2) == 0)
  199.   {
  200.     lock=1;
  201.     if (nisam_lock_database(file,F_RDLCK))
  202.     {
  203.       fprintf(stderr,"%2d: Can't lock table %dn",id,my_errno);
  204.       return 1;
  205.     }
  206.   }
  207.   found=next=prev=0;
  208.   for (i=0 ; i < 100 ; i++)
  209.   {
  210.     find=rnd(100000);
  211.     if (!nisam_rkey(file,record.id,1,(byte*) &find,
  212.  sizeof(find),HA_READ_KEY_EXACT))
  213.       found++;
  214.     else
  215.     {
  216.       if (my_errno != HA_ERR_KEY_NOT_FOUND)
  217.       {
  218. fprintf(stderr,"%2d: Got error %d from read in readn",id,my_errno);
  219. return 1;
  220.       }
  221.       else if (!nisam_rnext(file,record.id,1))
  222. next++;
  223.       else
  224.       {
  225. if (my_errno != HA_ERR_END_OF_FILE)
  226. {
  227.   fprintf(stderr,"%2d: Got error %d from rnext in readn",id,my_errno);
  228.   return 1;
  229. }
  230. else if (!nisam_rprev(file,record.id,1))
  231.   prev++;
  232. else
  233. {
  234.   if (my_errno != HA_ERR_END_OF_FILE)
  235.   {
  236.     fprintf(stderr,"%2d: Got error %d from rnext in readn",
  237.     id,my_errno);
  238.     return 1;
  239.   }
  240. }
  241.       }
  242.     }
  243.   }
  244.   if (lock)
  245.   {
  246.     if (nisam_lock_database(file,F_UNLCK))
  247.     {
  248.       fprintf(stderr,"%2d: Can't unlock tablen",id);
  249.       return 1;
  250.     }
  251.   }
  252.   printf("%2d: read:   found: %5d  next: %5d   prev: %5dn",
  253.  id,found,next,prev);
  254.   fflush(stdout);
  255.   return 0;
  256. }
  257. int test_rrnd(N_INFO *file,int id)
  258. {
  259.   uint count,lock;
  260.   lock=0;
  261.   if (rnd(2) == 0)
  262.   {
  263.     lock=1;
  264.     if (nisam_lock_database(file,F_RDLCK))
  265.     {
  266.       fprintf(stderr,"%2d: Can't lock table (%d)n",id,my_errno);
  267.       nisam_close(file);
  268.       return 1;
  269.     }
  270.     if (rnd(2) == 0)
  271.       nisam_extra(file,HA_EXTRA_CACHE);
  272.   }
  273.   count=0;
  274.   if (nisam_rrnd(file,record.id,0L))
  275.   {
  276.     if (my_errno == HA_ERR_END_OF_FILE)
  277.       goto end;
  278.     fprintf(stderr,"%2d: Can't read first record (%d)n",id,my_errno);
  279.     return 1;
  280.   }
  281.   for (count=1 ; !nisam_rrnd(file,record.id,NI_POS_ERROR) ;count++) ;
  282.   if (my_errno != HA_ERR_END_OF_FILE)
  283.   {
  284.     fprintf(stderr,"%2d: Got error %d from rrndn",id,my_errno);
  285.     return 1;
  286.   }
  287. end:
  288.   if (lock)
  289.   {
  290.     nisam_extra(file,HA_EXTRA_NO_CACHE);
  291.     if (nisam_lock_database(file,F_UNLCK))
  292.     {
  293.       fprintf(stderr,"%2d: Can't unlock tablen",id);
  294.       exit(0);
  295.     }
  296.   }
  297.   printf("%2d: rrnd:   %5dn",id,count); fflush(stdout);
  298.   return 0;
  299. }
  300. int test_write(N_INFO *file,int id,int lock_type)
  301. {
  302.   uint i,tries,count,lock;
  303.   lock=0;
  304.   if (rnd(2) == 0 || lock_type == F_RDLCK)
  305.   {
  306.     lock=1;
  307.     if (nisam_lock_database(file,F_WRLCK))
  308.     {
  309.       if (lock_type == F_RDLCK && my_errno == EDEADLK)
  310.       {
  311. printf("%2d: write:  deadlockn",id); fflush(stdout);
  312. return 0;
  313.       }
  314.       fprintf(stderr,"%2d: Can't lock table (%d)n",id,my_errno);
  315.       nisam_close(file);
  316.       return 1;
  317.     }
  318.     if (rnd(2) == 0)
  319.       nisam_extra(file,HA_EXTRA_WRITE_CACHE);
  320.   }
  321.   sprintf(record.id,"%7d",getpid());
  322.   strmov(record.text,"Testing...");
  323.   tries=(uint) rnd(100)+10;
  324.   for (i=count=0 ; i < tries ; i++)
  325.   {
  326.     record.nr=rnd(80000)+20000;
  327.     if (!nisam_write(file,record.id))
  328.       count++;
  329.     else
  330.     {
  331.       if (my_errno != HA_ERR_FOUND_DUPP_KEY)
  332.       {
  333. fprintf(stderr,"%2d: Got error %d (errno %d) from writen",id,my_errno,
  334. errno);
  335. return 1;
  336.       }
  337.     }
  338.   }
  339.   if (lock)
  340.   {
  341.     nisam_extra(file,HA_EXTRA_NO_CACHE);
  342.     if (nisam_lock_database(file,F_UNLCK))
  343.     {
  344.       fprintf(stderr,"%2d: Can't unlock tablen",id);
  345.       exit(0);
  346.     }
  347.   }
  348.   printf("%2d: write:  %5dn",id,count); fflush(stdout);
  349.   return 0;
  350. }
  351. int test_update(N_INFO *file,int id,int lock_type)
  352. {
  353.   uint i,lock,found,next,prev,update;
  354.   ulong find;
  355.   struct record new_record;
  356.   lock=0;
  357.   if (rnd(2) == 0 || lock_type == F_RDLCK)
  358.   {
  359.     lock=1;
  360.     if (nisam_lock_database(file,F_WRLCK))
  361.     {
  362.       if (lock_type == F_RDLCK && my_errno == EDEADLK)
  363.       {
  364. printf("%2d: write:  deadlockn",id); fflush(stdout);
  365. return 0;
  366.       }
  367.       fprintf(stderr,"%2d: Can't lock table (%d)n",id,my_errno);
  368.       return 1;
  369.     }
  370.   }
  371.   bzero((char*) &new_record,sizeof(new_record));
  372.   strmov(new_record.text,"Updated");
  373.   found=next=prev=update=0;
  374.   for (i=0 ; i < 100 ; i++)
  375.   {
  376.     find=rnd(100000);
  377.     if (!nisam_rkey(file,record.id,1,(byte*) &find,
  378.  sizeof(find),HA_READ_KEY_EXACT))
  379.       found++;
  380.     else
  381.     {
  382.       if (my_errno != HA_ERR_KEY_NOT_FOUND)
  383.       {
  384. fprintf(stderr,"%2d: Got error %d from read in updaten",id,my_errno);
  385. return 1;
  386.       }
  387.       else if (!nisam_rnext(file,record.id,1))
  388. next++;
  389.       else
  390.       {
  391. if (my_errno != HA_ERR_END_OF_FILE)
  392. {
  393.   fprintf(stderr,"%2d: Got error %d from rnext in updaten",
  394.   id,my_errno);
  395.   return 1;
  396. }
  397. else if (!nisam_rprev(file,record.id,1))
  398.   prev++;
  399. else
  400. {
  401.   if (my_errno != HA_ERR_END_OF_FILE)
  402.   {
  403.     fprintf(stderr,"%2d: Got error %d from rnext in updaten",
  404.     id,my_errno);
  405.     return 1;
  406.   }
  407.   continue;
  408. }
  409.       }
  410.     }
  411.     memcpy_fixed(new_record.id,record.id,sizeof(record.id));
  412.     new_record.nr=rnd(20000)+40000;
  413.     if (!nisam_update(file,record.id,new_record.id))
  414.       update++;
  415.     else
  416.     {
  417.       if (my_errno != HA_ERR_RECORD_CHANGED &&
  418.   my_errno != HA_ERR_RECORD_DELETED &&
  419.   my_errno != HA_ERR_FOUND_DUPP_KEY)
  420.       {
  421. fprintf(stderr,"%2d: Got error %d from updaten",id,my_errno);
  422. return 1;
  423.       }
  424.     }
  425.   }
  426.   if (lock)
  427.   {
  428.     if (nisam_lock_database(file,F_UNLCK))
  429.     {
  430.       fprintf(stderr,"Can't unlock table,id, error%dn",my_errno);
  431.       return 1;
  432.     }
  433.   }
  434.   printf("%2d: update: %5dn",id,update); fflush(stdout);
  435.   return 0;
  436. }