1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/*
Copyright 2011 Kristian Nielsen and Monty Program Ab.
This file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with this. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Run a set of queries in parallel against a server using the non-blocking
API, and compare to running same queries with the normal blocking API.
*/
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <my_getopt.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <event.h>
#define SL(s) (s), sizeof(s)
static const char *my_groups[]= { "client", NULL };
/* Maintaining a list of queries to run. */
struct query_entry {
struct query_entry *next;
char *query;
int index;
};
static struct query_entry *query_list;
static struct query_entry **tail_ptr= &query_list;
static int query_counter= 0;
/* State kept for each connection. */
struct state_data {
int ST; /* State machine current state */
struct event ev_mysql;
MYSQL mysql;
MYSQL_RES *result;
MYSQL *ret;
int err;
MYSQL_ROW row;
struct query_entry *query_element;
int index;
};
static const char *opt_db= NULL;
static const char *opt_user= NULL;
static const char *opt_password= NULL;
static int tty_password= 0;
static const char *opt_host= NULL;
static const char *opt_socket= NULL;
static unsigned int opt_port= 0;
static unsigned int opt_connections= 5;
static const char *opt_query_file= NULL;
static struct my_option options[] =
{
{"database", 'D', "Database to use", &opt_db, &opt_db,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"help", '?', "Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
0, 0, 0, 0, 0},
{"host", 'h', "Connect to host", &opt_host, &opt_host,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"password", 'p',
"Password to use when connecting to server. If password is not given it's asked from the tty.",
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
{"port", 'P', "Port number to use for connection.",
&opt_port, &opt_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"socket", 'S', "Socket file to use for connection",
&opt_socket, &opt_socket, 0, GET_STR,
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"user", 'u', "User for login if not current user", &opt_user,
&opt_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"connections", 'n', "Number of simultaneous connections/queries.",
&opt_connections, &opt_connections, 0, GET_UINT, REQUIRED_ARG,
5, 0, 0, 0, 0, 0},
{"queryfile", 'q', "Name of file containing extra queries to run",
&opt_query_file, &opt_query_file, 0, GET_STR, REQUIRED_ARG,
0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
static void
fatal(struct state_data *sd, const char *msg)
{
fprintf(stderr, "%s: %s\n", msg, (sd ? mysql_error(&sd->mysql) : ""));
exit(1);
}
static void state_machine_handler(int fd, short event, void *arg);
static void
next_event(int new_st, int status, struct state_data *sd)
{
short wait_event= 0;
struct timeval tv, *ptv;
int fd;
if (status & MYSQL_WAIT_READ)
wait_event|= EV_READ;
if (status & MYSQL_WAIT_WRITE)
wait_event|= EV_WRITE;
if (wait_event)
fd= mysql_get_socket(&sd->mysql);
else
fd= -1;
if (status & MYSQL_WAIT_TIMEOUT)
{
tv.tv_sec= mysql_get_timeout_value(&sd->mysql);
tv.tv_usec= 0;
ptv= &tv;
}
else
ptv= NULL;
event_set(&sd->ev_mysql, fd, wait_event, state_machine_handler, sd);
event_add(&sd->ev_mysql, ptv);
sd->ST= new_st;
}
static int
mysql_status(short event)
{
int status= 0;
if (event & EV_READ)
status|= MYSQL_WAIT_READ;
if (event & EV_WRITE)
status|= MYSQL_WAIT_WRITE;
if (event & EV_TIMEOUT)
status|= MYSQL_WAIT_TIMEOUT;
return status;
}
static int num_active_connections;
/* Shortcut for going to new state immediately without waiting. */
#define NEXT_IMMEDIATE(sd_, new_st) do { sd_->ST= new_st; goto again; } while (0)
static void
state_machine_handler(int fd __attribute__((unused)), short event, void *arg)
{
struct state_data *sd= arg;
int status;
again:
switch(sd->ST)
{
case 0:
/* Initial state, start making the connection. */
status= mysql_real_connect_start(&sd->ret, &sd->mysql, opt_host, opt_user, opt_password, opt_db, opt_port, opt_socket, 0);
if (status)
/* Wait for connect to complete. */
next_event(1, status, sd);
else
NEXT_IMMEDIATE(sd, 9);
break;
case 1:
status= mysql_real_connect_cont(&sd->ret, &sd->mysql, mysql_status(event));
if (status)
next_event(1, status, sd);
else
NEXT_IMMEDIATE(sd, 9);
break;
case 9:
if (!sd->ret)
fatal(sd, "Failed to mysql_real_connect()");
NEXT_IMMEDIATE(sd, 10);
break;
case 10:
/* Now run the next query. */
sd->query_element= query_list;
if (!sd->query_element)
{
/* No more queries, end the connection. */
NEXT_IMMEDIATE(sd, 40);
}
query_list= query_list->next;
sd->index= sd->query_element->index;
printf("%d ! %s\n", sd->index, sd->query_element->query);
status= mysql_real_query_start(&sd->err, &sd->mysql, sd->query_element->query,
strlen(sd->query_element->query));
if (status)
next_event(11, status, sd);
else
NEXT_IMMEDIATE(sd, 20);
break;
case 11:
status= mysql_real_query_cont(&sd->err, &sd->mysql, mysql_status(event));
if (status)
next_event(11, status, sd);
else
NEXT_IMMEDIATE(sd, 20);
break;
case 20:
my_free(sd->query_element->query);
my_free(sd->query_element);
if (sd->err)
{
printf("%d | Error: %s\n", sd->index, mysql_error(&sd->mysql));
NEXT_IMMEDIATE(sd, 10);
}
else
{
sd->result= mysql_use_result(&sd->mysql);
if (!sd->result)
fatal(sd, "mysql_use_result() returns error");
NEXT_IMMEDIATE(sd, 30);
}
break;
case 30:
status= mysql_fetch_row_start(&sd->row, sd->result);
if (status)
next_event(31, status, sd);
else
NEXT_IMMEDIATE(sd, 39);
break;
case 31:
status= mysql_fetch_row_cont(&sd->row, sd->result, mysql_status(event));
if (status)
next_event(31, status, sd);
else
NEXT_IMMEDIATE(sd, 39);
break;
case 39:
if (sd->row)
{
/* Got a row. */
unsigned int i;
printf("%d - ", sd->index);
for (i= 0; i < mysql_num_fields(sd->result); i++)
printf("%s%s", (i ? "\t" : ""), (sd->row[i] ? sd->row[i] : "(null)"));
printf ("\n");
NEXT_IMMEDIATE(sd, 30);
}
else
{
if (mysql_errno(&sd->mysql))
{
/* An error occured. */
printf("%d | Error: %s\n", sd->index, mysql_error(&sd->mysql));
}
else
{
/* EOF. */
printf("%d | EOF\n", sd->index);
}
mysql_free_result(sd->result);
NEXT_IMMEDIATE(sd, 10);
}
break;
case 40:
status= mysql_close_start(&sd->mysql);
if (status)
next_event(41, status, sd);
else
NEXT_IMMEDIATE(sd, 50);
break;
case 41:
status= mysql_close_cont(&sd->mysql, mysql_status(event));
if (status)
next_event(41, status, sd);
else
NEXT_IMMEDIATE(sd, 50);
break;
case 50:
/* We are done! */
num_active_connections--;
if (num_active_connections == 0)
event_loopbreak();
break;
default:
abort();
}
}
void
add_query(const char *q)
{
struct query_entry *e;
char *q2;
size_t len;
e= my_malloc(sizeof(*e), MYF(0));
q2= my_strdup(q, MYF(0));
if (!e || !q2)
fatal(NULL, "Out of memory");
/* Remove any trailing newline. */
len= strlen(q2);
if (q2[len] == '\n')
q2[len--]= '\0';
if (q2[len] == '\r')
q2[len--]= '\0';
e->next= NULL;
e->query= q2;
e->index= query_counter++;
*tail_ptr= e;
tail_ptr= &e->next;
}
static my_bool
handle_option(int optid, const struct my_option *opt __attribute__((unused)),
char *arg)
{
switch (optid)
{
case '?':
printf("Usage: async_queries [OPTIONS] query ...\n");
my_print_help(options);
my_print_variables(options);
exit(0);
break;
case 'p':
if (arg)
opt_password= arg;
else
tty_password= 1;
break;
}
return 0;
}
int
main(int argc, char *argv[])
{
struct state_data *sds;
unsigned int i;
int err;
struct event_base *libevent_base;
err= handle_options(&argc, &argv, options, handle_option);
if (err)
exit(err);
if (tty_password)
opt_password= get_tty_password(NullS);
if (opt_query_file)
{
FILE *f= fopen(opt_query_file, "r");
char buf[65536];
if (!f)
fatal(NULL, "Cannot open query file");
while (!feof(f))
{
if (!fgets(buf, sizeof(buf), f))
break;
add_query(buf);
}
fclose(f);
}
/* Add extra queries directly on command line. */
while (argc > 0)
{
--argc;
add_query(*argv++);
}
sds= my_malloc(opt_connections * sizeof(*sds), MYF(0));
if (!sds)
fatal(NULL, "Out of memory");
libevent_base= event_init();
err= mysql_library_init(argc, argv, (char **)my_groups);
if (err)
{
fprintf(stderr, "Fatal: mysql_library_init() returns error: %d\n", err);
exit(1);
}
num_active_connections= 0;
for (i= 0; i < opt_connections; i++)
{
mysql_init(&sds[i].mysql);
mysql_options(&sds[i].mysql, MYSQL_OPT_NONBLOCK, 0);
mysql_options(&sds[i].mysql, MYSQL_READ_DEFAULT_GROUP, "async_queries");
/*
We put the initial connect call in the first state 0 of the state machine
and run that manually, just to have everything in one place.
*/
sds[i].ST= 0;
num_active_connections++;
state_machine_handler(-1, -1, &sds[i]);
}
event_dispatch();
free(sds);
mysql_library_end();
event_base_free(libevent_base);
return 0;
}
|