//gcc bug29948.c -g -o bug29948 -L/home/sbester/server/5.0/mysql-5.0.48-linux-i686/lib -I/home/sbester/server/5.0/mysql-5.0.48-linux-i686/include -lmysqlclient_r -lz -lpthread /*This is generated code!*/ /*section: includes*/ #include #include #include #include #include #include "errmsg.h" #define TESTTIME (3600) #define NUMTHREADS (10) #define DATASIZE (16*1024*1024) /*section: globals*/ char host[]="127.0.0.1"; char username[]="root"; char password[]=""; char database[]="test"; int port=3306; pthread_t pthreads[NUMTHREADS]; int threaddone=0; char *databucket=NULL; /*section: function declarations*/ void *worker_thread(void *arg); MYSQL *db_connect(MYSQL *dbc); int write_string(char *buf,const int minlen,const int maxlen,const int flag); void *killer_thread(void *arg) { MYSQL *dbc=NULL; int res=0; MYSQL_RES *r=NULL; MYSQL_ROW w; MYSQL_FIELD *field=NULL; int i=0,moreresult=0; my_bool auto_reconnect=0; char shortquery[1024]; if (!(dbc = mysql_init(NULL))) { printf("mysql_init\n"); dbc=NULL; goto threadexit; } else { mysql_options(dbc,MYSQL_OPT_RECONNECT,(char*)&auto_reconnect); if (!mysql_real_connect(dbc,host,username,password,database,port, NULL, CLIENT_FOUND_ROWS|CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS)) { printf("mysql_real_connect failed: %s (%d)", mysql_error(dbc),mysql_errno(dbc)); dbc=NULL; } } while(!threaddone) { res = mysql_query(dbc,"show processlist"); if(res != 0) { printf("can't get processlist. server died. %d (%s)\n",mysql_errno(dbc),mysql_error(dbc)); goto threadexit; } do { r = mysql_store_result(dbc); if(r) { unsigned int numrows=mysql_num_rows(r); if(numrows == 1) { printf("killer thread is waiting...\n"); sleep(1); } while ((w = mysql_fetch_row(r))) { if(dbc->thread_id == atoi(w[0])) continue; //we don't kill ourselves if(lrand48()%3 == 0) { sprintf(shortquery,"kill %s",w[0]); mysql_query(dbc,shortquery); } } } else { printf("error processing processlist results...\n"); sleep(1); goto threadexit; } moreresult=mysql_next_result(dbc); if(moreresult > 0) { printf("mysql_next_result returned %d, mysql error %s, (%d)\n",moreresult,mysql_error(dbc),mysql_errno(dbc)); goto threadexit; } } while (0==moreresult); mysql_free_result(r); } threadexit: mysql_close(dbc); mysql_thread_end(); pthread_exit(0); } void *worker_thread(void *arg) { MYSQL *dbc=NULL; MYSQL_STMT *stmt=NULL; MYSQL_TIME ts; MYSQL_BIND bind; int val=0; int cancelstate=0; int i=0; int j=0; int res=0; my_bool auto_reconnect=1; unsigned int counter=0; unsigned long length=0; my_bool error=0; my_bool is_null=0; char *longquery=NULL; char *c=NULL; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&cancelstate); //printf("mysql_init\n"); if (!(dbc = mysql_init(NULL))) { printf("mysql_init failed\n"); dbc=NULL; goto threadexit; } else { //printf("mysql_options\n"); mysql_options(dbc,MYSQL_OPT_RECONNECT,(char*)&auto_reconnect); //printf("mysql_real_connect\n"); if (!mysql_real_connect(dbc,host,username,password,database,port, NULL, CLIENT_FOUND_ROWS|CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS)) { printf("mysql_real_connect failed: %s (%d)", mysql_error(dbc),mysql_errno(dbc)); dbc=NULL; } } while(!threaddone && dbc!=NULL) { for(i=0;i<3000 && !threaddone;i++) { if(i % 100 == 0) { printf("done %08d loops\n",i); } //printf("mysql_stmt_init\n"); stmt= mysql_stmt_init(dbc); if(!stmt) { printf("mysql_stmt_init failed: %s (%d)\n",mysql_error(dbc),mysql_errno(dbc)); continue; } //val=CURSOR_TYPE_READ_ONLY; val=CURSOR_TYPE_NO_CURSOR; if(0!=mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void *)&val)) { printf("mysql_stmt_attr_set2 failed: ",mysql_stmt_error(stmt),mysql_stmt_errno(stmt)); } char *query="SELECT now()"; //printf("mysql_stmt_prepare\n"); res= mysql_stmt_prepare(stmt,query,strlen(query)); if(res) { printf("mysql_stmt_prepare failed: %s (%d)\n",mysql_stmt_error(stmt),mysql_stmt_errno(stmt)); // printf("mysql_stmt_close\n"); res= mysql_stmt_close(stmt); if(res) { printf("mysql_stmt_close failed: %s (%d)\n",mysql_error(dbc),mysql_errno(dbc)); } continue; } int errfetch=0; for(j=0;j<10;j++) { //printf("mysql_stmt_execute\n"); res= mysql_stmt_execute(stmt); if(res) { printf("mysql_stmt_execute failed: %s (%d)\n",mysql_stmt_error(stmt),mysql_stmt_errno(stmt)); break; } memset(&bind,0,sizeof(bind)); bind.buffer_type= MYSQL_TYPE_TIMESTAMP; bind.buffer= (char *)&ts; bind.is_null= &is_null; bind.length= &length; bind.error= &error; // printf("mysql_stmt_bind_result\n"); res= mysql_stmt_bind_result(stmt,&bind); if(res) { printf("mysql_stmt_bind_result failed: %s (%d)\n", mysql_stmt_error(stmt),mysql_stmt_errno(stmt)); break; } // printf("mysql_stmt_store_result\n"); res= mysql_stmt_store_result(stmt); if(res) { printf("mysql_stmt_store_result failed: %s (%d)\n",mysql_stmt_error(stmt),mysql_stmt_errno(stmt)); break; } do { // printf("mysql_stmt_fetch\n"); errfetch = mysql_stmt_fetch(stmt); //printf("%04d-%02d-%02d %02d:%02d:%02d (%ld)\n",ts.year, ts.month, ts.day,ts.hour, ts.minute, ts.second,length); }while(errfetch!=MYSQL_NO_DATA && errfetch!=1); //printf("mysql_stmt_free_result\n"); res= mysql_stmt_free_result(stmt); if(res) { printf("mysql_stmt_free_result failed: %s (%d)\n",mysql_stmt_error(stmt),mysql_stmt_errno(stmt)); break; } } // printf("mysql_stmt_close\n"); res= mysql_stmt_close(stmt); if(res) { printf("mysql_stmt_close failed: %s (%d)\n",mysql_error(dbc),mysql_errno(dbc)); continue; } } } threadexit: printf("mysql_close\n"); mysql_close(dbc); mysql_thread_end(); pthread_exit(0); } int main(int argc, const char *argv[]) { MYSQL *dbc=NULL; int i=0,err=0; srand48((unsigned long)time(NULL)); time_t timestart=0,timenow=0; unsigned int counter=0; char shortquery[1024]={0}; char *longquery=NULL; char *c=NULL; my_init(); if (!(dbc = mysql_init(NULL))) { //printf("mysql_init\n"); dbc=NULL; goto threadexit; } else { if (!mysql_real_connect(dbc,host,username,password,database,port, NULL, CLIENT_FOUND_ROWS|CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS)) { printf("mysql_real_connect failed: %s (%d)", mysql_error(dbc),mysql_errno(dbc)); dbc=NULL; goto threadexit; } } printf("running initializations..\n"); mysql_close(dbc); printf("about to spawn %d threads\n",NUMTHREADS); for (i=0;i