2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2015 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
21 * This tool is a MT reader. It behaves like slapd-read however
22 * with one or more threads simultaneously using the same connection.
23 * If -M is enabled, then M threads will also perform write operations.
29 #include "ldap_pvt_thread.h"
31 #include "ac/stdlib.h"
35 #include "ac/socket.h"
36 #include "ac/string.h"
37 #include "ac/unistd.h"
45 #include "slapd-common.h"
50 #define DEFAULT_BASE "ou=people,dc=example,dc=com"
53 do_conn( char *uri, char *manager, struct berval *passwd,
54 LDAP **ld, int nobind, int maxretries, int conn_num );
57 do_read( LDAP *ld, char *entry,
58 char **attrs, int noattrs, int nobind, int maxloop,
59 int maxretries, int delay, int force, int chaserefs, int idx );
63 char *sbase, char *filter, char **attrs, int noattrs, int nobind,
64 int innerloop, int maxretries, int delay, int force, int chaserefs,
69 char *sbase, char *filter, char **attrs, int noattrs, int nobind,
70 int innerloop, int maxretries, int delay, int force, int chaserefs,
74 do_onethread( void *arg );
77 do_onerwthread( void *arg );
79 #define MAX_THREAD 1024
80 /* Use same array for readers and writers, offset writers by MAX_THREAD */
81 int rt_pass[MAX_THREAD*2];
82 int rt_fail[MAX_THREAD*2];
83 int *rwt_pass = rt_pass + MAX_THREAD;
84 int *rwt_fail = rt_fail + MAX_THREAD;
85 ldap_pvt_thread_t rtid[MAX_THREAD*2], *rwtid = rtid + MAX_THREAD;
88 * Shared globals (command line args)
95 int retries = RETRIES;
99 char *srchattrs[] = { "1.1", NULL };
100 char **attrs = srchattrs;
111 thread_error(int idx, char *string)
115 snprintf(thrstr, BUFSIZ, "error on tidx: %d: %s", idx, string);
116 tester_error( thrstr );
120 thread_output(int idx, char *string)
124 snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string);
125 tester_error( thrstr );
129 thread_verbose(int idx, char *string)
135 snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string);
136 tester_error( thrstr );
144 "-H <uri> | ([-h <host>] -p <port>) "
166 exit( EXIT_FAILURE );
170 main( int argc, char **argv )
174 char *host = "localhost";
176 char *manager = NULL;
177 struct berval passwd = { 0, NULL };
183 tester_init( "slapd-mtread", TESTER_READ );
185 /* by default, tolerate referrals and no such object */
186 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
188 while ( (i = getopt( argc, argv, "ACc:D:e:Ff:H:h:i:L:l:M:m:p:r:t:T:w:v" )) != EOF ) {
198 case 'H': /* the server uri */
199 uri = strdup( optarg );
202 case 'h': /* the servers host */
203 host = strdup( optarg );
207 tester_ignore_str2errlist( optarg );
218 case 'p': /* the servers port */
219 if ( lutil_atoi( &port, optarg ) != 0 ) {
224 case 'D': /* the servers manager */
225 manager = strdup( optarg );
228 case 'w': /* the server managers password */
229 passwd.bv_val = strdup( optarg );
230 passwd.bv_len = strlen( optarg );
231 memset( optarg, '*', passwd.bv_len );
234 case 'c': /* the number of connections */
235 if ( lutil_atoi( &noconns, optarg ) != 0 ) {
240 case 'e': /* DN to search for */
241 entry = strdup( optarg );
244 case 'f': /* the search request */
245 filter = strdup( optarg );
252 case 'l': /* the number of loops */
253 if ( lutil_atoi( &loops, optarg ) != 0 ) {
258 case 'L': /* the number of outerloops */
259 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
264 case 'M': /* the number of R/W threads */
265 if ( lutil_atoi( &rwthreads, optarg ) != 0 ) {
268 if (rwthreads > MAX_THREAD)
269 rwthreads = MAX_THREAD;
272 case 'm': /* the number of threads */
273 if ( lutil_atoi( &threads, optarg ) != 0 ) {
276 if (threads > MAX_THREAD)
277 threads = MAX_THREAD;
280 case 'r': /* the number of retries */
281 if ( lutil_atoi( &retries, optarg ) != 0 ) {
286 case 't': /* delay in seconds */
287 if ( lutil_atoi( &delay, optarg ) != 0 ) {
293 attrs = ldap_str2charray( optarg, "," );
294 if ( attrs == NULL ) {
305 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
308 if ( *entry == '\0' ) {
309 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
311 exit( EXIT_FAILURE );
314 if ( argv[optind] != NULL ) {
315 attrs = &argv[optind];
320 if (noconns > MAXCONN)
322 lds = (LDAP **) calloc( sizeof(LDAP *), noconns);
324 fprintf( stderr, "%s: Memory error: calloc noconns.\n",
326 exit( EXIT_FAILURE );
329 uri = tester_uri( uri, host, port );
330 /* One connection and one connection only */
331 do_conn( uri, manager, &passwd, &ld, nobind, retries, 0 );
333 for(i = 1; i < noconns; i++) {
334 do_conn( uri, manager, &passwd, &lds[i], nobind, retries, i );
337 ldap_pvt_thread_initialize();
339 snprintf(outstr, BUFSIZ, "MT Test Start: conns: %d (%s)", noconns, uri);
340 tester_error(outstr);
341 snprintf(outstr, BUFSIZ, "Threads: RO: %d RW: %d", threads, rwthreads);
342 tester_error(outstr);
344 /* Set up read only threads */
345 for ( i = 0; i < threads; i++ ) {
346 ldap_pvt_thread_create( &rtid[i], 0, do_onethread, &rtid[i]);
347 snprintf(outstr, BUFSIZ, "Created RO thread %d", i);
348 thread_verbose(-1, outstr);
350 /* Set up read/write threads */
351 for ( i = 0; i < rwthreads; i++ ) {
352 ldap_pvt_thread_create( &rwtid[i], 0, do_onerwthread, &rwtid[i]);
353 snprintf(outstr, BUFSIZ, "Created RW thread %d", i + MAX_THREAD);
354 thread_verbose(-1, outstr);
357 ptpass = outerloops * loops;
359 /* wait for read only threads to complete */
360 for ( i = 0; i < threads; i++ )
361 ldap_pvt_thread_join(rtid[i], NULL);
362 /* wait for read/write threads to complete */
363 for ( i = 0; i < rwthreads; i++ )
364 ldap_pvt_thread_join(rwtid[i], NULL);
366 for(i = 0; i < noconns; i++) {
367 if ( lds[i] != NULL ) {
368 ldap_unbind_ext( lds[i], NULL, NULL );
373 for ( i = 0; i < threads; i++ ) {
374 snprintf(outstr, BUFSIZ, "RO thread %d pass=%d fail=%d", i,
375 rt_pass[i], rt_fail[i]);
376 tester_error(outstr);
377 if (rt_fail[i] != 0 || rt_pass[i] != ptpass) {
378 snprintf(outstr, BUFSIZ, "FAIL RO thread %d", i);
379 tester_error(outstr);
383 for ( i = 0; i < rwthreads; i++ ) {
384 snprintf(outstr, BUFSIZ, "RW thread %d pass=%d fail=%d", i + MAX_THREAD,
385 rwt_pass[i], rwt_fail[i]);
386 tester_error(outstr);
387 if (rwt_fail[i] != 0 || rwt_pass[i] != ptpass) {
388 snprintf(outstr, BUFSIZ, "FAIL RW thread %d", i);
389 tester_error(outstr);
393 snprintf(outstr, BUFSIZ, "MT Test complete" );
394 tester_error(outstr);
397 exit( EXIT_FAILURE );
398 exit( EXIT_SUCCESS );
402 do_onethread( void *arg )
408 int idx = (ldap_pvt_thread_t *)arg - rtid;
410 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
412 thread_error( idx, "Memory error: thread calloc for noconns" );
413 exit( EXIT_FAILURE );
416 for ( j = 0; j < outerloops; j++ ) {
417 for(i = 0; i < noconns; i++) {
418 mlds[i] = ldap_dup(lds[i]);
419 if (mlds[i] == NULL) {
420 thread_error( idx, "ldap_dup error" );
423 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
424 snprintf(thrstr, BUFSIZ,
425 "RO Thread conns: %d refcnt: %d (rc = %d)",
426 noconns, refcnt, rc);
427 thread_verbose(idx, thrstr);
429 thisconn = (idx + j) % noconns;
430 if (thisconn < 0 || thisconn >= noconns)
432 if (mlds[thisconn] == NULL) {
433 thread_error( idx, "(failed to dup)");
434 tester_perror( "ldap_dup", "(failed to dup)" );
435 exit( EXIT_FAILURE );
437 snprintf(thrstr, BUFSIZ, "Using conn %d", thisconn);
438 thread_verbose(idx, thrstr);
439 if ( filter != NULL ) {
440 if (strchr(filter, '['))
441 do_random2( mlds[thisconn], entry, filter, attrs,
442 noattrs, nobind, loops, retries, delay, force,
445 do_random( mlds[thisconn], entry, filter, attrs,
446 noattrs, nobind, loops, retries, delay, force,
450 do_read( mlds[thisconn], entry, attrs,
451 noattrs, nobind, loops, retries, delay, force,
454 for(i = 0; i < noconns; i++) {
455 (void) ldap_destroy(mlds[i]);
464 do_onerwthread( void *arg )
469 char dn[256], uids[32], cns[32], *base;
470 LDAPMod *attrp[5], attrs[4];
471 char *oc_vals[] = { "top", "OpenLDAPperson", NULL };
472 char *cn_vals[] = { NULL, NULL };
473 char *sn_vals[] = { NULL, NULL };
474 char *uid_vals[] = { NULL, NULL };
479 int idx = (ldap_pvt_thread_t *)arg - rtid;
481 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
483 thread_error( idx, "Memory error: thread calloc for noconns" );
484 exit( EXIT_FAILURE );
487 snprintf(uids, sizeof(uids), "rwtest%04d", idx);
488 snprintf(cns, sizeof(cns), "rwtest%04d", idx);
490 for (i = 0; i < 4; i++) {
491 attrp[i] = &attrs[i];
495 attrs[0].mod_type = "objectClass";
496 attrs[0].mod_values = oc_vals;
497 attrs[1].mod_type = "cn";
498 attrs[1].mod_values = cn_vals;
499 cn_vals[0] = &cns[0];
500 attrs[2].mod_type = "sn";
501 attrs[2].mod_values = sn_vals;
502 sn_vals[0] = &cns[0];
503 attrs[3].mod_type = "uid";
504 attrs[3].mod_values = uid_vals;
505 uid_vals[0] = &uids[0];
507 for ( j = 0; j < outerloops; j++ ) {
508 for(i = 0; i < noconns; i++) {
509 mlds[i] = ldap_dup(lds[i]);
510 if (mlds[i] == NULL) {
511 thread_error( idx, "ldap_dup error" );
514 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
515 snprintf(thrstr, BUFSIZ,
516 "RW Thread conns: %d refcnt: %d (rc = %d)",
517 noconns, refcnt, rc);
518 thread_verbose(idx, thrstr);
520 thisconn = (idx + j) % noconns;
521 if (thisconn < 0 || thisconn >= noconns)
523 if (mlds[thisconn] == NULL) {
524 thread_error( idx, "(failed to dup)");
525 tester_perror( "ldap_dup", "(failed to dup)" );
526 exit( EXIT_FAILURE );
528 snprintf(thrstr, BUFSIZ, "START RW Thread using conn %d", thisconn);
529 thread_verbose(idx, thrstr);
536 snprintf(dn, 256, "cn=%s,%s", cns, base);
540 for (i = 0; i < loops; i++) {
541 ret = ldap_add_ext_s(ld, dn, &attrp[0], NULL, NULL);
542 if (ret == LDAP_SUCCESS) {
544 ret = ldap_delete_ext_s(ld, dn, NULL, NULL);
545 if (ret == LDAP_SUCCESS) {
549 thread_output(idx, ldap_err2string(ret));
553 thread_output(idx, ldap_err2string(ret));
558 snprintf(thrstr, BUFSIZ,
559 "INNER STOP RW Thread using conn %d (%d/%d)",
560 thisconn, adds, dels);
561 thread_verbose(idx, thrstr);
563 for(i = 0; i < noconns; i++) {
564 (void) ldap_destroy(mlds[i]);
574 do_conn( char *uri, char *manager, struct berval *passwd,
575 LDAP **ldp, int nobind, int maxretries, int conn_num )
578 int version = LDAP_VERSION3;
579 int i = 0, do_retry = maxretries;
580 int rc = LDAP_SUCCESS;
584 ldap_initialize( &ld, uri );
586 snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
587 tester_error( thrstr );
588 tester_perror( "ldap_initialize", NULL );
589 exit( EXIT_FAILURE );
592 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
593 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
594 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
596 if ( do_retry == maxretries ) {
597 snprintf( thrstr, BUFSIZ, "do_conn #%d\n", conn_num );
598 thread_verbose( -1, thrstr );
602 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
603 if ( rc != LDAP_SUCCESS ) {
604 snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
605 tester_error( thrstr );
606 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
609 case LDAP_UNAVAILABLE:
610 if ( do_retry > 0 ) {
611 ldap_unbind_ext( ld, NULL, NULL );
623 exit( EXIT_FAILURE );
631 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
632 int innerloop, int maxretries, int delay, int force, int chaserefs,
635 int i = 0, do_retry = maxretries;
637 int rc = LDAP_SUCCESS;
639 char **values = NULL;
640 LDAPMessage *res = NULL, *e = NULL;
643 attrs[ 0 ] = LDAP_NO_ATTRS;
646 snprintf( thrstr, BUFSIZ,
647 "Read(%d): base=\"%s\", filter=\"%s\".\n",
648 innerloop, sbase, filter );
649 thread_verbose( idx, thrstr );
651 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
652 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
654 case LDAP_SIZELIMIT_EXCEEDED:
655 case LDAP_TIMELIMIT_EXCEEDED:
657 nvalues = ldap_count_entries( ld, res );
658 if ( nvalues == 0 ) {
660 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
665 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
666 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
668 values[ i ] = ldap_get_dn( ld, e );
674 if ( do_retry == maxretries ) {
675 snprintf( thrstr, BUFSIZ,
676 "Read base=\"%s\" filter=\"%s\" got %d values.\n",
677 sbase, filter, nvalues );
678 thread_verbose( idx, thrstr );
681 for ( i = 0; i < innerloop; i++ ) {
682 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
684 do_read( ld, values[ r ],
685 srchattrs, noattrs, nobind, 1, maxretries,
686 delay, force, chaserefs, idx );
688 for( i = 0; i < nvalues; i++) {
689 if (values[i] != NULL)
690 ldap_memfree( values[i] );
696 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
700 snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
701 thread_verbose( idx, thrstr );
704 /* substitute a generated int into the filter */
706 do_random2( LDAP *ld,
707 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
708 int innerloop, int maxretries, int delay, int force, int chaserefs,
711 int i = 0, do_retry = maxretries;
712 int rc = LDAP_SUCCESS;
715 LDAPMessage *res = NULL, *e = NULL;
720 snprintf( thrstr, BUFSIZ,
721 "Read(%d): base=\"%s\", filter=\"%s\".\n",
722 innerloop, sbase, filter );
723 thread_verbose( idx, thrstr );
725 ptr = strchr(filter, '[');
728 ftail = strchr(filter, ']');
729 if (!ftail || ftail < ptr)
732 sscanf(ptr, "[%d-%d]", &lo, &hi);
738 for ( i = 0; i < innerloop; i++ ) {
739 int r = ((double)range)*rand()/(RAND_MAX + 1.0);
740 sprintf(fbuf, "%.*s%d%s", flen, filter, r, ftail);
742 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
743 fbuf, srchattrs, noattrs, NULL, NULL, NULL,
744 LDAP_NO_LIMIT, &res );
751 int first = tester_ignore_err( rc );
755 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
759 /* only log if first occurrence */
760 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
761 tester_ldap_error( ld, buf, NULL );
766 /* busy needs special handling */
767 tester_ldap_error( ld, buf, NULL );
768 if ( rc == LDAP_BUSY && do_retry > 0 ) {
776 snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
777 thread_verbose( idx, thrstr );
781 do_read( LDAP *ld, char *entry,
782 char **attrs, int noattrs, int nobind, int maxloop,
783 int maxretries, int delay, int force, int chaserefs, int idx )
785 int i = 0, do_retry = maxretries;
786 int rc = LDAP_SUCCESS;
790 if ( do_retry == maxretries ) {
791 snprintf( thrstr, BUFSIZ, "Read(%d): entry=\"%s\".\n",
793 thread_verbose( idx, thrstr );
796 snprintf(thrstr, BUFSIZ, "LD %p cnt: %d (retried %d) (%s)", \
797 (void *) ld, maxloop, (do_retry - maxretries), entry);
798 thread_verbose( idx, thrstr );
800 for ( ; i < maxloop; i++ ) {
801 LDAPMessage *res = NULL;
803 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
804 NULL, attrs, noattrs, NULL, NULL, NULL,
805 LDAP_NO_LIMIT, &res );
813 int first = tester_ignore_err( rc );
817 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
821 /* only log if first occurrence */
822 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
823 tester_ldap_error( ld, buf, NULL );
828 /* busy needs special handling */
829 tester_ldap_error( ld, buf, NULL );
830 if ( rc == LDAP_BUSY && do_retry > 0 ) {