2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2011 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 );
206 tester_ignore_str2errlist( optarg );
217 case 'p': /* the servers port */
218 if ( lutil_atoi( &port, optarg ) != 0 ) {
223 case 'D': /* the servers manager */
224 manager = strdup( optarg );
227 case 'w': /* the server managers password */
228 passwd.bv_val = strdup( optarg );
229 passwd.bv_len = strlen( optarg );
230 memset( optarg, '*', passwd.bv_len );
233 case 'c': /* the number of connections */
234 if ( lutil_atoi( &noconns, optarg ) != 0 ) {
239 case 'e': /* DN to search for */
240 entry = strdup( optarg );
243 case 'f': /* the search request */
244 filter = strdup( optarg );
251 case 'l': /* the number of loops */
252 if ( lutil_atoi( &loops, optarg ) != 0 ) {
257 case 'L': /* the number of outerloops */
258 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
263 case 'M': /* the number of R/W threads */
264 if ( lutil_atoi( &rwthreads, optarg ) != 0 ) {
267 if (rwthreads > MAX_THREAD)
268 rwthreads = MAX_THREAD;
271 case 'm': /* the number of threads */
272 if ( lutil_atoi( &threads, optarg ) != 0 ) {
275 if (threads > MAX_THREAD)
276 threads = MAX_THREAD;
279 case 'r': /* the number of retries */
280 if ( lutil_atoi( &retries, optarg ) != 0 ) {
285 case 't': /* delay in seconds */
286 if ( lutil_atoi( &delay, optarg ) != 0 ) {
292 attrs = ldap_str2charray( optarg, "," );
293 if ( attrs == NULL ) {
304 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
307 if ( *entry == '\0' ) {
308 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
310 exit( EXIT_FAILURE );
313 if ( argv[optind] != NULL ) {
314 attrs = &argv[optind];
319 if (noconns > MAXCONN)
321 lds = (LDAP **) calloc( sizeof(LDAP *), noconns);
323 fprintf( stderr, "%s: Memory error: calloc noconns.\n",
325 exit( EXIT_FAILURE );
328 uri = tester_uri( uri, host, port );
329 /* One connection and one connection only */
330 do_conn( uri, manager, &passwd, &ld, nobind, retries, 0 );
332 for(i = 1; i < noconns; i++) {
333 do_conn( uri, manager, &passwd, &lds[i], nobind, retries, i );
336 ldap_pvt_thread_initialize();
338 snprintf(outstr, BUFSIZ, "MT Test Start: conns: %d (%s)", noconns, uri);
339 tester_error(outstr);
340 snprintf(outstr, BUFSIZ, "Threads: RO: %d RW: %d", threads, rwthreads);
341 tester_error(outstr);
343 /* Set up read only threads */
344 for ( i = 0; i < threads; i++ ) {
345 ldap_pvt_thread_create( &rtid[i], 0, do_onethread, &rtid[i]);
346 snprintf(outstr, BUFSIZ, "Created RO thread %d", i);
347 thread_verbose(-1, outstr);
349 /* Set up read/write threads */
350 for ( i = 0; i < rwthreads; i++ ) {
351 ldap_pvt_thread_create( &rwtid[i], 0, do_onerwthread, &rwtid[i]);
352 snprintf(outstr, BUFSIZ, "Created RW thread %d", i + MAX_THREAD);
353 thread_verbose(-1, outstr);
356 ptpass = outerloops * loops;
358 /* wait for read only threads to complete */
359 for ( i = 0; i < threads; i++ )
360 ldap_pvt_thread_join(rtid[i], NULL);
361 /* wait for read/write threads to complete */
362 for ( i = 0; i < rwthreads; i++ )
363 ldap_pvt_thread_join(rwtid[i], NULL);
365 for(i = 0; i < noconns; i++) {
366 if ( lds[i] != NULL ) {
367 ldap_unbind_ext( lds[i], NULL, NULL );
372 for ( i = 0; i < threads; i++ ) {
373 snprintf(outstr, BUFSIZ, "RO thread %d pass=%d fail=%d", i,
374 rt_pass[i], rt_fail[i]);
375 tester_error(outstr);
376 if (rt_fail[i] != 0 || rt_pass[i] != ptpass) {
377 snprintf(outstr, BUFSIZ, "FAIL RO thread %d", i);
378 tester_error(outstr);
382 for ( i = 0; i < rwthreads; i++ ) {
383 snprintf(outstr, BUFSIZ, "RW thread %d pass=%d fail=%d", i + MAX_THREAD,
384 rwt_pass[i], rwt_fail[i]);
385 tester_error(outstr);
386 if (rwt_fail[i] != 0 || rwt_pass[i] != ptpass) {
387 snprintf(outstr, BUFSIZ, "FAIL RW thread %d", i);
388 tester_error(outstr);
392 snprintf(outstr, BUFSIZ, "MT Test complete" );
393 tester_error(outstr);
396 exit( EXIT_FAILURE );
397 exit( EXIT_SUCCESS );
401 do_onethread( void *arg )
407 int idx = (ldap_pvt_thread_t *)arg - rtid;
409 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
411 thread_error( idx, "Memory error: thread calloc for noconns" );
412 exit( EXIT_FAILURE );
415 for ( j = 0; j < outerloops; j++ ) {
416 for(i = 0; i < noconns; i++) {
417 mlds[i] = ldap_dup(lds[i]);
418 if (mlds[i] == NULL) {
419 thread_error( idx, "ldap_dup error" );
422 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
423 snprintf(thrstr, BUFSIZ,
424 "RO Thread conns: %d refcnt: %d (rc = %d)",
425 noconns, refcnt, rc);
426 thread_verbose(idx, thrstr);
428 thisconn = (idx + j) % noconns;
429 if (thisconn < 0 || thisconn >= noconns)
431 if (mlds[thisconn] == NULL) {
432 thread_error( idx, "(failed to dup)");
433 tester_perror( "ldap_dup", "(failed to dup)" );
434 exit( EXIT_FAILURE );
436 snprintf(thrstr, BUFSIZ, "Using conn %d", thisconn);
437 thread_verbose(idx, thrstr);
438 if ( filter != NULL ) {
439 if (strchr(filter, '['))
440 do_random2( mlds[thisconn], entry, filter, attrs,
441 noattrs, nobind, loops, retries, delay, force,
444 do_random( mlds[thisconn], entry, filter, attrs,
445 noattrs, nobind, loops, retries, delay, force,
449 do_read( mlds[thisconn], entry, attrs,
450 noattrs, nobind, loops, retries, delay, force,
453 for(i = 0; i < noconns; i++) {
454 (void) ldap_destroy(mlds[i]);
463 do_onerwthread( void *arg )
468 char dn[256], uids[32], cns[32], *base;
469 LDAPMod *attrp[5], attrs[4];
470 char *oc_vals[] = { "top", "OpenLDAPperson", NULL };
471 char *cn_vals[] = { NULL, NULL };
472 char *sn_vals[] = { NULL, NULL };
473 char *uid_vals[] = { NULL, NULL };
478 int idx = (ldap_pvt_thread_t *)arg - rtid;
480 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
482 thread_error( idx, "Memory error: thread calloc for noconns" );
483 exit( EXIT_FAILURE );
486 snprintf(uids, sizeof(uids), "rwtest%04d", idx);
487 snprintf(cns, sizeof(cns), "rwtest%04d", idx);
489 for (i = 0; i < 4; i++) {
490 attrp[i] = &attrs[i];
494 attrs[0].mod_type = "objectClass";
495 attrs[0].mod_values = oc_vals;
496 attrs[1].mod_type = "cn";
497 attrs[1].mod_values = cn_vals;
498 cn_vals[0] = &cns[0];
499 attrs[2].mod_type = "sn";
500 attrs[2].mod_values = sn_vals;
501 sn_vals[0] = &cns[0];
502 attrs[3].mod_type = "uid";
503 attrs[3].mod_values = uid_vals;
504 uid_vals[0] = &uids[0];
506 for ( j = 0; j < outerloops; j++ ) {
507 for(i = 0; i < noconns; i++) {
508 mlds[i] = ldap_dup(lds[i]);
509 if (mlds[i] == NULL) {
510 thread_error( idx, "ldap_dup error" );
513 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
514 snprintf(thrstr, BUFSIZ,
515 "RW Thread conns: %d refcnt: %d (rc = %d)",
516 noconns, refcnt, rc);
517 thread_verbose(idx, thrstr);
519 thisconn = (idx + j) % noconns;
520 if (thisconn < 0 || thisconn >= noconns)
522 if (mlds[thisconn] == NULL) {
523 thread_error( idx, "(failed to dup)");
524 tester_perror( "ldap_dup", "(failed to dup)" );
525 exit( EXIT_FAILURE );
527 snprintf(thrstr, BUFSIZ, "START RW Thread using conn %d", thisconn);
528 thread_verbose(idx, thrstr);
535 snprintf(dn, 256, "cn=%s,%s", cns, base);
539 for (i = 0; i < loops; i++) {
540 ret = ldap_add_ext_s(ld, dn, &attrp[0], NULL, NULL);
541 if (ret == LDAP_SUCCESS) {
543 ret = ldap_delete_ext_s(ld, dn, NULL, NULL);
544 if (ret == LDAP_SUCCESS) {
548 thread_output(idx, ldap_err2string(ret));
552 thread_output(idx, ldap_err2string(ret));
557 snprintf(thrstr, BUFSIZ,
558 "INNER STOP RW Thread using conn %d (%d/%d)",
559 thisconn, adds, dels);
560 thread_verbose(idx, thrstr);
562 for(i = 0; i < noconns; i++) {
563 (void) ldap_destroy(mlds[i]);
573 do_conn( char *uri, char *manager, struct berval *passwd,
574 LDAP **ldp, int nobind, int maxretries, int conn_num )
577 int version = LDAP_VERSION3;
578 int i = 0, do_retry = maxretries;
579 int rc = LDAP_SUCCESS;
583 ldap_initialize( &ld, uri );
585 snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
586 tester_error( thrstr );
587 tester_perror( "ldap_initialize", NULL );
588 exit( EXIT_FAILURE );
591 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
592 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
593 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
595 if ( do_retry == maxretries ) {
596 snprintf( thrstr, BUFSIZ, "do_conn #%d\n", conn_num );
597 thread_verbose( -1, thrstr );
601 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
602 if ( rc != LDAP_SUCCESS ) {
603 snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
604 tester_error( thrstr );
605 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
608 case LDAP_UNAVAILABLE:
609 if ( do_retry > 0 ) {
610 ldap_unbind_ext( ld, NULL, NULL );
622 exit( EXIT_FAILURE );
630 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
631 int innerloop, int maxretries, int delay, int force, int chaserefs,
634 int i = 0, do_retry = maxretries;
636 int rc = LDAP_SUCCESS;
638 char **values = NULL;
639 LDAPMessage *res = NULL, *e = NULL;
642 attrs[ 0 ] = LDAP_NO_ATTRS;
645 snprintf( thrstr, BUFSIZ,
646 "Read(%d): base=\"%s\", filter=\"%s\".\n",
647 innerloop, sbase, filter );
648 thread_verbose( idx, thrstr );
650 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
651 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
653 case LDAP_SIZELIMIT_EXCEEDED:
654 case LDAP_TIMELIMIT_EXCEEDED:
656 nvalues = ldap_count_entries( ld, res );
657 if ( nvalues == 0 ) {
659 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
664 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
665 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
667 values[ i ] = ldap_get_dn( ld, e );
673 if ( do_retry == maxretries ) {
674 snprintf( thrstr, BUFSIZ,
675 "Read base=\"%s\" filter=\"%s\" got %d values.\n",
676 sbase, filter, nvalues );
677 thread_verbose( idx, thrstr );
680 for ( i = 0; i < innerloop; i++ ) {
681 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
683 do_read( ld, values[ r ],
684 srchattrs, noattrs, nobind, 1, maxretries,
685 delay, force, chaserefs, idx );
687 for( i = 0; i < nvalues; i++) {
688 if (values[i] != NULL)
689 ldap_memfree( values[i] );
695 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
699 snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
700 thread_verbose( idx, thrstr );
703 /* substitute a generated int into the filter */
705 do_random2( LDAP *ld,
706 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
707 int innerloop, int maxretries, int delay, int force, int chaserefs,
710 int i = 0, do_retry = maxretries;
711 int rc = LDAP_SUCCESS;
714 LDAPMessage *res = NULL, *e = NULL;
719 snprintf( thrstr, BUFSIZ,
720 "Read(%d): base=\"%s\", filter=\"%s\".\n",
721 innerloop, sbase, filter );
722 thread_verbose( idx, thrstr );
724 ptr = strchr(filter, '[');
727 ftail = strchr(filter, ']');
728 if (!ftail || ftail < ptr)
731 sscanf(ptr, "[%d-%d]", &lo, &hi);
737 for ( i = 0; i < innerloop; i++ ) {
738 int r = ((double)range)*rand()/(RAND_MAX + 1.0);
739 sprintf(fbuf, "%.*s%d%s", flen, filter, r, ftail);
741 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
742 fbuf, srchattrs, noattrs, NULL, NULL, NULL,
743 LDAP_NO_LIMIT, &res );
750 int first = tester_ignore_err( rc );
754 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
758 /* only log if first occurrence */
759 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
760 tester_ldap_error( ld, buf, NULL );
765 /* busy needs special handling */
766 tester_ldap_error( ld, buf, NULL );
767 if ( rc == LDAP_BUSY && do_retry > 0 ) {
775 snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
776 thread_verbose( idx, thrstr );
780 do_read( LDAP *ld, char *entry,
781 char **attrs, int noattrs, int nobind, int maxloop,
782 int maxretries, int delay, int force, int chaserefs, int idx )
784 int i = 0, do_retry = maxretries;
785 int rc = LDAP_SUCCESS;
789 if ( do_retry == maxretries ) {
790 snprintf( thrstr, BUFSIZ, "Read(%d): entry=\"%s\".\n",
792 thread_verbose( idx, thrstr );
795 snprintf(thrstr, BUFSIZ, "LD %p cnt: %d (retried %d) (%s)", \
796 (void *) ld, maxloop, (do_retry - maxretries), entry);
797 thread_verbose( idx, thrstr );
799 for ( ; i < maxloop; i++ ) {
800 LDAPMessage *res = NULL;
802 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
803 NULL, attrs, noattrs, NULL, NULL, NULL,
804 LDAP_NO_LIMIT, &res );
812 int first = tester_ignore_err( rc );
816 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
820 /* only log if first occurrence */
821 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
822 tester_ldap_error( ld, buf, NULL );
827 /* busy needs special handling */
828 tester_ldap_error( ld, buf, NULL );
829 if ( rc == LDAP_BUSY && do_retry > 0 ) {