| 1 |
/* |
| 2 |
* xs-test.c |
| 3 |
* |
| 4 |
* Do Xenstore tests. |
| 5 |
* |
| 6 |
* Copyright (C) 2016 Juergen Gross <[email protected]>, |
| 7 |
* SUSE Linux GmbH |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms and conditions of the GNU General Public |
| 11 |
* License, version 2, as published by the Free Software Foundation. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public |
| 19 |
* License along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#define _GNU_SOURCE |
| 23 |
#include <err.h> |
| 24 |
#include <getopt.h> |
| 25 |
#include <inttypes.h> |
| 26 |
#include <stdbool.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <time.h> |
| 32 |
#include <xenstore.h> |
| 33 |
|
| 34 |
#include <xen-tools/common-macros.h> |
| 35 |
|
| 36 |
#define TEST_PATH "xenstore-test" |
| 37 |
#define WRITE_BUFFERS_N 10 |
| 38 |
#define WRITE_BUFFERS_SIZE 4000 |
| 39 |
#define MAX_TA_LOOPS 100 |
| 40 |
|
| 41 |
struct test { |
| 42 |
char *name; |
| 43 |
int (*func_init)(uintptr_t par); |
| 44 |
int (*func)(uintptr_t par); |
| 45 |
int (*func_deinit)(uintptr_t par); |
| 46 |
uintptr_t par; |
| 47 |
char *descr; |
| 48 |
}; |
| 49 |
|
| 50 |
static struct xs_handle *xsh; |
| 51 |
static char *path; |
| 52 |
static char *paths[WRITE_BUFFERS_N]; |
| 53 |
static char write_buffers[WRITE_BUFFERS_N][WRITE_BUFFERS_SIZE]; |
| 54 |
static int ta_loops; |
| 55 |
|
| 56 |
static struct option options[] = { |
| 57 |
{ "list-tests", 0, NULL, 'l' }, |
| 58 |
{ "test", 1, NULL, 't' }, |
| 59 |
{ "random", 1, NULL, 'r' }, |
| 60 |
{ "help", 0, NULL, 'h' }, |
| 61 |
{ "iterations", 1, NULL, 'i' }, |
| 62 |
{ NULL, 0, NULL, 0 } |
| 63 |
}; |
| 64 |
|
| 65 |
static int call_test(struct test *tst, int iters, bool no_clock) |
| 66 |
{ |
| 67 |
char *stage = "?"; |
| 68 |
struct timespec tp1, tp2; |
| 69 |
uint64_t nsec, nsec_min, nsec_max, nsec_sum; |
| 70 |
int i, ret = 0; |
| 71 |
|
| 72 |
nsec_min = -1; |
| 73 |
nsec_max = 0; |
| 74 |
nsec_sum = 0; |
| 75 |
|
| 76 |
for ( i = 0; i < iters; i++ ) |
| 77 |
{ |
| 78 |
stage = "pre-init"; |
| 79 |
xs_rm(xsh, XBT_NULL, path); |
| 80 |
if ( !xs_write(xsh, XBT_NULL, path, "", 0) ) |
| 81 |
{ |
| 82 |
ret = errno; |
| 83 |
break; |
| 84 |
} |
| 85 |
stage = "init"; |
| 86 |
ret = tst->func_init(tst->par); |
| 87 |
if ( ret ) |
| 88 |
break; |
| 89 |
if ( clock_gettime(CLOCK_REALTIME, &tp1) ) |
| 90 |
no_clock = true; |
| 91 |
stage = "run"; |
| 92 |
ret = tst->func(tst->par); |
| 93 |
if ( ret ) |
| 94 |
break; |
| 95 |
if ( clock_gettime(CLOCK_REALTIME, &tp2) ) |
| 96 |
no_clock = true; |
| 97 |
if ( !no_clock ) |
| 98 |
{ |
| 99 |
nsec = tp2.tv_sec * 1000000000 + tp2.tv_nsec - |
| 100 |
tp1.tv_sec * 1000000000 - tp1.tv_nsec; |
| 101 |
if ( nsec < nsec_min ) |
| 102 |
nsec_min = nsec; |
| 103 |
if ( nsec > nsec_max ) |
| 104 |
nsec_max = nsec; |
| 105 |
nsec_sum += nsec; |
| 106 |
} |
| 107 |
stage = "deinit"; |
| 108 |
ret = tst->func_deinit(tst->par); |
| 109 |
if ( ret ) |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
if ( ret ) |
| 114 |
printf("%-10s: failed (ret = %d, stage %s)\n", tst->name, ret, stage); |
| 115 |
else if ( !no_clock ) |
| 116 |
{ |
| 117 |
printf("%-10s:", tst->name); |
| 118 |
if ( iters > 1 ) |
| 119 |
printf(" avg: %"PRIu64" ns (%"PRIu64" ns .. %"PRIu64" ns)", |
| 120 |
nsec_sum / iters, nsec_min, nsec_max); |
| 121 |
else |
| 122 |
printf(" %"PRIu64" ns", nsec_sum); |
| 123 |
printf("\n"); |
| 124 |
} |
| 125 |
|
| 126 |
return ret; |
| 127 |
} |
| 128 |
|
| 129 |
static void usage(int ret) |
| 130 |
{ |
| 131 |
FILE *out; |
| 132 |
|
| 133 |
out = ret ? stderr : stdout; |
| 134 |
|
| 135 |
fprintf(out, "usage: xs-test [<options>]\n"); |
| 136 |
fprintf(out, " <options> are:\n"); |
| 137 |
fprintf(out, " -i|--iterations <i> perform each test <i> times (default 1)\n"); |
| 138 |
fprintf(out, " -l|--list-tests list available tests\n"); |
| 139 |
fprintf(out, " -r|--random <time> perform random tests for <time> seconds\n"); |
| 140 |
fprintf(out, " -t|--test <test> run <test> (default is all tests)\n"); |
| 141 |
fprintf(out, " -h|--help print this usage information\n"); |
| 142 |
exit(ret); |
| 143 |
} |
| 144 |
|
| 145 |
static int ret0(uintptr_t par) |
| 146 |
{ |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
static int verify_node(char *node, char *data, unsigned int size) |
| 151 |
{ |
| 152 |
char *buf; |
| 153 |
unsigned int len; |
| 154 |
int ret; |
| 155 |
|
| 156 |
buf = xs_read(xsh, XBT_NULL, node, &len); |
| 157 |
if ( !buf ) |
| 158 |
return errno; |
| 159 |
|
| 160 |
ret = (len == size && !memcmp(buf, data, len)) ? 0 : ENOENT; |
| 161 |
free(buf); |
| 162 |
|
| 163 |
return ret; |
| 164 |
} |
| 165 |
|
| 166 |
static int test_read_init(uintptr_t par) |
| 167 |
{ |
| 168 |
if ( par > WRITE_BUFFERS_SIZE ) |
| 169 |
return EFBIG; |
| 170 |
return xs_write(xsh, XBT_NULL, paths[0], write_buffers[0], par) ? 0 : errno; |
| 171 |
} |
| 172 |
|
| 173 |
static int test_read(uintptr_t par) |
| 174 |
{ |
| 175 |
char *buf; |
| 176 |
unsigned int len; |
| 177 |
|
| 178 |
buf = xs_read(xsh, XBT_NULL, paths[0], &len); |
| 179 |
if ( !buf ) |
| 180 |
return errno; |
| 181 |
free(buf); |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
#define test_read_deinit ret0 |
| 186 |
|
| 187 |
static int test_write_init(uintptr_t par) |
| 188 |
{ |
| 189 |
return (par > WRITE_BUFFERS_SIZE) ? EFBIG : 0; |
| 190 |
} |
| 191 |
|
| 192 |
static int test_write(uintptr_t par) |
| 193 |
{ |
| 194 |
return xs_write(xsh, XBT_NULL, paths[0], write_buffers[0], par) ? 0 : errno; |
| 195 |
} |
| 196 |
|
| 197 |
static int test_write_deinit(uintptr_t par) |
| 198 |
{ |
| 199 |
return verify_node(paths[0], write_buffers[0], par); |
| 200 |
} |
| 201 |
|
| 202 |
static int test_dir_init(uintptr_t par) |
| 203 |
{ |
| 204 |
unsigned int i; |
| 205 |
|
| 206 |
for ( i = 0; i < WRITE_BUFFERS_N; i++ ) |
| 207 |
if ( !xs_write(xsh, XBT_NULL, paths[i], write_buffers[i], 1) ) |
| 208 |
return errno; |
| 209 |
|
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
static int test_dir(uintptr_t par) |
| 214 |
{ |
| 215 |
char **dir; |
| 216 |
unsigned int num; |
| 217 |
|
| 218 |
dir = xs_directory(xsh, XBT_NULL, path, &num); |
| 219 |
if ( !dir ) |
| 220 |
return errno; |
| 221 |
|
| 222 |
free(dir); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
static int test_dir_deinit(uintptr_t par) |
| 227 |
{ |
| 228 |
char **dir; |
| 229 |
unsigned int i, j, num; |
| 230 |
int rc = 0; |
| 231 |
|
| 232 |
dir = xs_directory(xsh, XBT_NULL, path, &num); |
| 233 |
if ( !dir ) |
| 234 |
return errno; |
| 235 |
|
| 236 |
for ( j = 0; j < WRITE_BUFFERS_N; j++ ) |
| 237 |
{ |
| 238 |
for ( i = 0; i < num; i++ ) |
| 239 |
if ( dir[i][0] == 'a' + j && dir[i][1] == 0 ) |
| 240 |
break; |
| 241 |
if ( i == num ) |
| 242 |
rc = ENOENT; |
| 243 |
} |
| 244 |
if ( num != WRITE_BUFFERS_N ) |
| 245 |
rc = ENOENT; |
| 246 |
free(dir); |
| 247 |
return rc; |
| 248 |
} |
| 249 |
|
| 250 |
static int test_rm_init(uintptr_t par) |
| 251 |
{ |
| 252 |
unsigned int i; |
| 253 |
|
| 254 |
if ( par > WRITE_BUFFERS_N ) |
| 255 |
return EFBIG; |
| 256 |
|
| 257 |
for ( i = 0; i < par; i++ ) |
| 258 |
if ( xs_write(xsh, XBT_NULL, paths[i], write_buffers[i], 1) ) |
| 259 |
return errno; |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
static int test_rm(uintptr_t par) |
| 265 |
{ |
| 266 |
if ( !xs_rm(xsh, XBT_NULL, path) ) |
| 267 |
return errno; |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
#define test_rm_deinit ret0 |
| 273 |
|
| 274 |
#define test_ta1_init ret0 |
| 275 |
|
| 276 |
static int test_ta1(uintptr_t par) |
| 277 |
{ |
| 278 |
xs_transaction_t t; |
| 279 |
int l; |
| 280 |
|
| 281 |
for ( l = 0; l < MAX_TA_LOOPS; l++ ) |
| 282 |
{ |
| 283 |
t = xs_transaction_start(xsh); |
| 284 |
if ( t == XBT_NULL ) |
| 285 |
return errno; |
| 286 |
if ( xs_transaction_end(xsh, t, par ? true : false) ) |
| 287 |
return 0; |
| 288 |
if ( errno != EAGAIN ) |
| 289 |
return errno; |
| 290 |
} |
| 291 |
|
| 292 |
ta_loops++; |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
#define test_ta1_deinit ret0 |
| 297 |
|
| 298 |
static int test_ta2_init(uintptr_t par) |
| 299 |
{ |
| 300 |
return xs_write(xsh, XBT_NULL, paths[0], write_buffers[0], 1) ? 0 : errno; |
| 301 |
} |
| 302 |
|
| 303 |
static int test_ta2(uintptr_t par) |
| 304 |
{ |
| 305 |
xs_transaction_t t; |
| 306 |
char *buf; |
| 307 |
unsigned int len; |
| 308 |
int ret; |
| 309 |
int l; |
| 310 |
|
| 311 |
for ( l = 0; l < MAX_TA_LOOPS; l++ ) |
| 312 |
{ |
| 313 |
t = xs_transaction_start(xsh); |
| 314 |
if ( t == XBT_NULL ) |
| 315 |
return errno; |
| 316 |
buf = xs_read(xsh, t, paths[0], &len); |
| 317 |
if ( !buf ) |
| 318 |
goto out; |
| 319 |
free(buf); |
| 320 |
if ( !xs_write(xsh, t, paths[0], "b", 1) ) |
| 321 |
goto out; |
| 322 |
buf = xs_read(xsh, t, paths[0], &len); |
| 323 |
if ( !buf ) |
| 324 |
goto out; |
| 325 |
errno = (len == 1 && buf[0] == 'b') ? 0 : ENOENT; |
| 326 |
free(buf); |
| 327 |
if ( errno ) |
| 328 |
goto out; |
| 329 |
buf = xs_read(xsh, XBT_NULL, paths[0], &len); |
| 330 |
if ( !buf ) |
| 331 |
goto out; |
| 332 |
errno = (len == 1 && buf[0] == 'a') ? 0 : ENOENT; |
| 333 |
free(buf); |
| 334 |
if ( errno ) |
| 335 |
goto out; |
| 336 |
if ( xs_transaction_end(xsh, t, par ? true : false) ) |
| 337 |
return 0; |
| 338 |
if ( errno != EAGAIN ) |
| 339 |
return errno; |
| 340 |
} |
| 341 |
|
| 342 |
ta_loops++; |
| 343 |
return 0; |
| 344 |
|
| 345 |
out: |
| 346 |
ret = errno; |
| 347 |
xs_transaction_end(xsh, t, true); |
| 348 |
return ret; |
| 349 |
} |
| 350 |
|
| 351 |
static int test_ta2_deinit(uintptr_t par) |
| 352 |
{ |
| 353 |
return verify_node(paths[0], par ? "a" : "b", 1); |
| 354 |
} |
| 355 |
|
| 356 |
static int test_ta3_init(uintptr_t par) |
| 357 |
{ |
| 358 |
return xs_write(xsh, XBT_NULL, paths[0], write_buffers[0], 1) ? 0 : errno; |
| 359 |
} |
| 360 |
|
| 361 |
static int test_ta3(uintptr_t par) |
| 362 |
{ |
| 363 |
xs_transaction_t t; |
| 364 |
char *buf; |
| 365 |
unsigned int len; |
| 366 |
int ret; |
| 367 |
|
| 368 |
t = xs_transaction_start(xsh); |
| 369 |
if ( t == XBT_NULL ) |
| 370 |
return errno; |
| 371 |
buf = xs_read(xsh, t, paths[0], &len); |
| 372 |
if ( !buf ) |
| 373 |
goto out; |
| 374 |
free(buf); |
| 375 |
if ( !xs_write(xsh, XBT_NULL, paths[0], "b", 1) ) |
| 376 |
goto out; |
| 377 |
buf = xs_read(xsh, t, paths[0], &len); |
| 378 |
if ( !buf ) |
| 379 |
goto out; |
| 380 |
errno = (len == 1 && buf[0] == 'a') ? 0 : ENOENT; |
| 381 |
free(buf); |
| 382 |
if ( errno ) |
| 383 |
goto out; |
| 384 |
if ( !xs_write(xsh, t, paths[0], "c", 1) ) |
| 385 |
goto out; |
| 386 |
buf = xs_read(xsh, t, paths[0], &len); |
| 387 |
if ( !buf ) |
| 388 |
goto out; |
| 389 |
errno = (len == 1 && buf[0] == 'c') ? 0 : ENOENT; |
| 390 |
free(buf); |
| 391 |
if ( errno ) |
| 392 |
goto out; |
| 393 |
if ( xs_transaction_end(xsh, t, false) || errno != EAGAIN ) |
| 394 |
return ENOENT; |
| 395 |
return 0; |
| 396 |
|
| 397 |
out: |
| 398 |
ret = errno; |
| 399 |
xs_transaction_end(xsh, t, true); |
| 400 |
return ret; |
| 401 |
} |
| 402 |
|
| 403 |
static int test_ta3_deinit(uintptr_t par) |
| 404 |
{ |
| 405 |
return verify_node(paths[0], "b", 1); |
| 406 |
} |
| 407 |
|
| 408 |
#define TEST(s, f, p, l) { s, f ## _init, f, f ## _deinit, (uintptr_t)(p), l } |
| 409 |
struct test tests[] = { |
| 410 |
TEST("read 1", test_read, 1, "Read node with 1 byte data"), |
| 411 |
TEST("read 2000", test_read, 2000, "Read node with 2000 bytes data"), |
| 412 |
TEST("write 1", test_write, 1, "Write node with 1 byte data"), |
| 413 |
TEST("write 2000", test_write, 2000, "Write node with 2000 bytes data"), |
| 414 |
TEST("dir", test_dir, 0, "List directory"), |
| 415 |
TEST("rm node", test_rm, 0, "Remove single node"), |
| 416 |
TEST("rm dir", test_rm, WRITE_BUFFERS_N, "Remove node with sub-nodes"), |
| 417 |
TEST("ta empty", test_ta1, 0, "Empty transaction"), |
| 418 |
TEST("ta empty x", test_ta1, 1, "Empty transaction abort"), |
| 419 |
TEST("ta rmw", test_ta2, 0, "Read-modify-write transaction"), |
| 420 |
TEST("ta rmw x", test_ta2, 1, "Read-modify-write transaction abort"), |
| 421 |
TEST("ta err", test_ta3, 0, "Transaction with conflict"), |
| 422 |
}; |
| 423 |
|
| 424 |
static void cleanup(void) |
| 425 |
{ |
| 426 |
xs_transaction_t t; |
| 427 |
char **dir; |
| 428 |
unsigned int num; |
| 429 |
|
| 430 |
xs_rm(xsh, XBT_NULL, path); |
| 431 |
|
| 432 |
while ( true ) |
| 433 |
{ |
| 434 |
t = xs_transaction_start(xsh); |
| 435 |
if ( t == XBT_NULL ) |
| 436 |
return; |
| 437 |
|
| 438 |
dir = xs_directory(xsh, t, TEST_PATH, &num); |
| 439 |
if ( dir && !num ) |
| 440 |
xs_rm(xsh, t, TEST_PATH); |
| 441 |
free(dir); |
| 442 |
|
| 443 |
if ( xs_transaction_end(xsh, t, false) || errno != EAGAIN ) |
| 444 |
return; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
int main(int argc, char *argv[]) |
| 449 |
{ |
| 450 |
int opt, t, iters = 1, ret = 0, randtime = 0; |
| 451 |
char *test = NULL; |
| 452 |
bool list = false; |
| 453 |
time_t stop; |
| 454 |
|
| 455 |
while ( (opt = getopt_long(argc, argv, "lr:t:hi:", options, |
| 456 |
NULL)) != -1 ) |
| 457 |
{ |
| 458 |
switch ( opt ) |
| 459 |
{ |
| 460 |
case 'i': |
| 461 |
iters = atoi(optarg); |
| 462 |
break; |
| 463 |
case 'l': |
| 464 |
list = true; |
| 465 |
break; |
| 466 |
case 'r': |
| 467 |
randtime = atoi(optarg); |
| 468 |
break; |
| 469 |
case 't': |
| 470 |
test = optarg; |
| 471 |
break; |
| 472 |
case 'h': |
| 473 |
usage(0); |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
if ( optind != argc ) |
| 478 |
usage(1); |
| 479 |
|
| 480 |
if ( list ) |
| 481 |
{ |
| 482 |
for ( t = 0; t < ARRAY_SIZE(tests); t++ ) |
| 483 |
printf("%-10s: %s\n", tests[t].name, tests[t].descr); |
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
if ( asprintf(&path, "%s/%u", TEST_PATH, getpid()) < 0 ) |
| 488 |
err(2, "asprintf() malloc failure\n"); |
| 489 |
|
| 490 |
for ( t = 0; t < WRITE_BUFFERS_N; t++ ) |
| 491 |
{ |
| 492 |
memset(write_buffers[t], 'a' + t, WRITE_BUFFERS_SIZE); |
| 493 |
if ( asprintf(&paths[t], "%s/%c", path, 'a' + t) < 0 ) |
| 494 |
err(2, "asprintf() malloc failure\n"); |
| 495 |
} |
| 496 |
|
| 497 |
xsh = xs_open(0); |
| 498 |
if ( !xsh ) |
| 499 |
{ |
| 500 |
fprintf(stderr, "could not connect to xenstore\n"); |
| 501 |
exit(2); |
| 502 |
} |
| 503 |
|
| 504 |
if ( randtime ) |
| 505 |
{ |
| 506 |
stop = time(NULL) + randtime; |
| 507 |
srandom((unsigned int)stop); |
| 508 |
|
| 509 |
while ( time(NULL) < stop && !ret ) |
| 510 |
{ |
| 511 |
t = random() % ARRAY_SIZE(tests); |
| 512 |
ret = call_test(tests + t, iters, true); |
| 513 |
} |
| 514 |
} |
| 515 |
else |
| 516 |
for ( t = 0; t < ARRAY_SIZE(tests) && !ret; t++ ) |
| 517 |
{ |
| 518 |
if ( !test || !strcmp(test, tests[t].name) ) |
| 519 |
ret = call_test(tests + t, iters, false); |
| 520 |
} |
| 521 |
|
| 522 |
if ( !ret ) |
| 523 |
cleanup(); |
| 524 |
|
| 525 |
xs_close(xsh); |
| 526 |
|
| 527 |
if ( ta_loops ) |
| 528 |
printf("Exhaustive transaction retries (%d) occurred %d times.\n", |
| 529 |
MAX_TA_LOOPS, ta_loops); |
| 530 |
|
| 531 |
return ret ? 3 : 0; |
| 532 |
} |
| 533 |
|
| 534 |
/* |
| 535 |
* Local variables: |
| 536 |
* mode: C |
| 537 |
* c-file-style: "BSD" |
| 538 |
* c-basic-offset: 4 |
| 539 |
* indent-tabs-mode: nil |
| 540 |
* End: |
| 541 |
*/ |