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,
68 do_onethread( void *arg );
71 do_onerwthread( void *arg );
73 #define MAX_THREAD 1024
74 /* Use same array for readers and writers, offset writers by MAX_THREAD */
75 int rt_pass[MAX_THREAD*2];
76 int rt_fail[MAX_THREAD*2];
77 int *rwt_pass = rt_pass + MAX_THREAD;
78 int *rwt_fail = rt_fail + MAX_THREAD;
79 ldap_pvt_thread_t rtid[MAX_THREAD*2], *rwtid = rtid + MAX_THREAD;
82 * Shared globals (command line args)
89 int retries = RETRIES;
93 char *srchattrs[] = { "1.1", NULL };
94 char **attrs = srchattrs;
105 thread_error(int idx, char *string)
109 snprintf(thrstr, BUFSIZ, "error on tidx: %d: %s", idx, string);
110 tester_error( thrstr );
114 thread_output(int idx, char *string)
118 snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string);
119 tester_error( thrstr );
123 thread_verbose(int idx, char *string)
129 snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string);
130 tester_error( thrstr );
138 "-H <uri> | ([-h <host>] -p <port>) "
160 exit( EXIT_FAILURE );
164 main( int argc, char **argv )
168 char *host = "localhost";
170 char *manager = NULL;
171 struct berval passwd = { 0, NULL };
177 tester_init( "slapd-mtread", TESTER_READ );
179 /* by default, tolerate referrals and no such object */
180 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
182 while ( (i = getopt( argc, argv, "ACc:D:e:Ff:H:h:i:L:l:M:m:p:r:t:T:w:v" )) != EOF ) {
192 case 'H': /* the server uri */
193 uri = strdup( optarg );
196 case 'h': /* the servers host */
197 host = strdup( optarg );
200 tester_ignore_str2errlist( optarg );
211 case 'p': /* the servers port */
212 if ( lutil_atoi( &port, optarg ) != 0 ) {
217 case 'D': /* the servers manager */
218 manager = strdup( optarg );
221 case 'w': /* the server managers password */
222 passwd.bv_val = strdup( optarg );
223 passwd.bv_len = strlen( optarg );
224 memset( optarg, '*', passwd.bv_len );
227 case 'c': /* the number of connections */
228 if ( lutil_atoi( &noconns, optarg ) != 0 ) {
233 case 'e': /* DN to search for */
234 entry = strdup( optarg );
237 case 'f': /* the search request */
238 filter = strdup( optarg );
245 case 'l': /* the number of loops */
246 if ( lutil_atoi( &loops, optarg ) != 0 ) {
251 case 'L': /* the number of outerloops */
252 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
257 case 'M': /* the number of R/W threads */
258 if ( lutil_atoi( &rwthreads, optarg ) != 0 ) {
261 if (rwthreads > MAX_THREAD)
262 rwthreads = MAX_THREAD;
265 case 'm': /* the number of threads */
266 if ( lutil_atoi( &threads, optarg ) != 0 ) {
269 if (threads > MAX_THREAD)
270 threads = MAX_THREAD;
273 case 'r': /* the number of retries */
274 if ( lutil_atoi( &retries, optarg ) != 0 ) {
279 case 't': /* delay in seconds */
280 if ( lutil_atoi( &delay, optarg ) != 0 ) {
286 attrs = ldap_str2charray( optarg, "," );
287 if ( attrs == NULL ) {
298 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
301 if ( *entry == '\0' ) {
302 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
304 exit( EXIT_FAILURE );
307 if ( argv[optind] != NULL ) {
308 attrs = &argv[optind];
313 if (noconns > MAXCONN)
315 lds = (LDAP **) calloc( sizeof(LDAP *), noconns);
317 fprintf( stderr, "%s: Memory error: calloc noconns.\n",
319 exit( EXIT_FAILURE );
322 uri = tester_uri( uri, host, port );
323 /* One connection and one connection only */
324 do_conn( uri, manager, &passwd, &ld, nobind, retries, 0 );
326 for(i = 1; i < noconns; i++) {
327 do_conn( uri, manager, &passwd, &lds[i], nobind, retries, i );
330 ldap_pvt_thread_initialize();
332 snprintf(outstr, BUFSIZ, "MT Test Start: conns: %d (%s)", noconns, uri);
333 tester_error(outstr);
334 snprintf(outstr, BUFSIZ, "Threads: RO: %d RW: %d", threads, rwthreads);
335 tester_error(outstr);
337 /* Set up read only threads */
338 for ( i = 0; i < threads; i++ ) {
339 ldap_pvt_thread_create( &rtid[i], 0, do_onethread, &rtid[i]);
340 snprintf(outstr, BUFSIZ, "Created RO thread %d", i);
341 thread_verbose(-1, outstr);
343 /* Set up read/write threads */
344 for ( i = 0; i < rwthreads; i++ ) {
345 ldap_pvt_thread_create( &rwtid[i], 0, do_onerwthread, &rwtid[i]);
346 snprintf(outstr, BUFSIZ, "Created RW thread %d", i + MAX_THREAD);
347 thread_verbose(-1, outstr);
350 ptpass = outerloops * loops;
352 /* wait for read only threads to complete */
353 for ( i = 0; i < threads; i++ )
354 ldap_pvt_thread_join(rtid[i], NULL);
355 /* wait for read/write threads to complete */
356 for ( i = 0; i < rwthreads; i++ )
357 ldap_pvt_thread_join(rwtid[i], NULL);
359 for(i = 0; i < noconns; i++) {
360 if ( lds[i] != NULL ) {
361 ldap_unbind_ext( lds[i], NULL, NULL );
366 for ( i = 0; i < threads; i++ ) {
367 snprintf(outstr, BUFSIZ, "RO thread %d pass=%d fail=%d", i,
368 rt_pass[i], rt_fail[i]);
369 tester_error(outstr);
370 if (rt_fail[i] != 0 || rt_pass[i] != ptpass) {
371 snprintf(outstr, BUFSIZ, "FAIL RO thread %d", i);
372 tester_error(outstr);
376 for ( i = 0; i < rwthreads; i++ ) {
377 snprintf(outstr, BUFSIZ, "RW thread %d pass=%d fail=%d", i + MAX_THREAD,
378 rwt_pass[i], rwt_fail[i]);
379 tester_error(outstr);
380 if (rwt_fail[i] != 0 || rwt_pass[i] != ptpass) {
381 snprintf(outstr, BUFSIZ, "FAIL RW thread %d", i);
382 tester_error(outstr);
386 snprintf(outstr, BUFSIZ, "MT Test complete" );
387 tester_error(outstr);
390 exit( EXIT_FAILURE );
391 exit( EXIT_SUCCESS );
395 do_onethread( void *arg )
401 int idx = (ldap_pvt_thread_t *)arg - rtid;
403 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
405 thread_error( idx, "Memory error: thread calloc for noconns" );
406 exit( EXIT_FAILURE );
409 for ( j = 0; j < outerloops; j++ ) {
410 for(i = 0; i < noconns; i++) {
411 mlds[i] = ldap_dup(lds[i]);
412 if (mlds[i] == NULL) {
413 thread_error( idx, "ldap_dup error" );
416 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
417 snprintf(thrstr, BUFSIZ,
418 "RO Thread conns: %d refcnt: %d (rc = %d)",
419 noconns, refcnt, rc);
420 thread_verbose(idx, thrstr);
422 thisconn = (idx + j) % noconns;
423 if (thisconn < 0 || thisconn >= noconns)
425 if (mlds[thisconn] == NULL) {
426 thread_error( idx, "(failed to dup)");
427 tester_perror( "ldap_dup", "(failed to dup)" );
428 exit( EXIT_FAILURE );
430 snprintf(thrstr, BUFSIZ, "Using conn %d", thisconn);
431 thread_verbose(idx, thrstr);
432 if ( filter != NULL ) {
433 do_random( mlds[thisconn], entry, filter, attrs,
434 noattrs, nobind, loops, retries, delay, force,
438 do_read( mlds[thisconn], entry, attrs,
439 noattrs, nobind, loops, retries, delay, force,
442 for(i = 0; i < noconns; i++) {
443 (void) ldap_destroy(mlds[i]);
452 do_onerwthread( void *arg )
457 char dn[256], uids[32], cns[32], *base;
458 LDAPMod *attrp[5], attrs[4];
459 char *oc_vals[] = { "top", "OpenLDAPperson", NULL };
460 char *cn_vals[] = { NULL, NULL };
461 char *sn_vals[] = { NULL, NULL };
462 char *uid_vals[] = { NULL, NULL };
467 int idx = (ldap_pvt_thread_t *)arg - rtid;
469 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
471 thread_error( idx, "Memory error: thread calloc for noconns" );
472 exit( EXIT_FAILURE );
475 snprintf(uids, sizeof(uids), "rwtest%04d", idx);
476 snprintf(cns, sizeof(cns), "rwtest%04d", idx);
478 for (i = 0; i < 4; i++) {
479 attrp[i] = &attrs[i];
483 attrs[0].mod_type = "objectClass";
484 attrs[0].mod_values = oc_vals;
485 attrs[1].mod_type = "cn";
486 attrs[1].mod_values = cn_vals;
487 cn_vals[0] = &cns[0];
488 attrs[2].mod_type = "sn";
489 attrs[2].mod_values = sn_vals;
490 sn_vals[0] = &cns[0];
491 attrs[3].mod_type = "uid";
492 attrs[3].mod_values = uid_vals;
493 uid_vals[0] = &uids[0];
495 for ( j = 0; j < outerloops; j++ ) {
496 for(i = 0; i < noconns; i++) {
497 mlds[i] = ldap_dup(lds[i]);
498 if (mlds[i] == NULL) {
499 thread_error( idx, "ldap_dup error" );
502 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
503 snprintf(thrstr, BUFSIZ,
504 "RW Thread conns: %d refcnt: %d (rc = %d)",
505 noconns, refcnt, rc);
506 thread_verbose(idx, thrstr);
508 thisconn = (idx + j) % noconns;
509 if (thisconn < 0 || thisconn >= noconns)
511 if (mlds[thisconn] == NULL) {
512 thread_error( idx, "(failed to dup)");
513 tester_perror( "ldap_dup", "(failed to dup)" );
514 exit( EXIT_FAILURE );
516 snprintf(thrstr, BUFSIZ, "START RW Thread using conn %d", thisconn);
517 thread_verbose(idx, thrstr);
524 snprintf(dn, 256, "cn=%s,%s", cns, base);
528 for (i = 0; i < loops; i++) {
529 ret = ldap_add_ext_s(ld, dn, &attrp[0], NULL, NULL);
530 if (ret == LDAP_SUCCESS) {
532 ret = ldap_delete_ext_s(ld, dn, NULL, NULL);
533 if (ret == LDAP_SUCCESS) {
537 thread_output(idx, ldap_err2string(ret));
541 thread_output(idx, ldap_err2string(ret));
546 snprintf(thrstr, BUFSIZ,
547 "INNER STOP RW Thread using conn %d (%d/%d)",
548 thisconn, adds, dels);
549 thread_verbose(idx, thrstr);
551 for(i = 0; i < noconns; i++) {
552 (void) ldap_destroy(mlds[i]);
562 do_conn( char *uri, char *manager, struct berval *passwd,
563 LDAP **ldp, int nobind, int maxretries, int conn_num )
566 int version = LDAP_VERSION3;
567 int i = 0, do_retry = maxretries;
568 int rc = LDAP_SUCCESS;
572 ldap_initialize( &ld, uri );
574 snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
575 tester_error( thrstr );
576 tester_perror( "ldap_initialize", NULL );
577 exit( EXIT_FAILURE );
580 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
581 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
582 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
584 if ( do_retry == maxretries ) {
585 snprintf( thrstr, BUFSIZ, "do_conn #%d\n", conn_num );
586 thread_verbose( -1, thrstr );
590 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
591 if ( rc != LDAP_SUCCESS ) {
592 snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
593 tester_error( thrstr );
594 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
597 case LDAP_UNAVAILABLE:
598 if ( do_retry > 0 ) {
599 ldap_unbind_ext( ld, NULL, NULL );
611 exit( EXIT_FAILURE );
619 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
620 int innerloop, int maxretries, int delay, int force, int chaserefs,
623 int i = 0, do_retry = maxretries;
625 int rc = LDAP_SUCCESS;
627 char **values = NULL;
628 LDAPMessage *res = NULL, *e = NULL;
631 attrs[ 0 ] = LDAP_NO_ATTRS;
634 snprintf( thrstr, BUFSIZ,
635 "Read(%d): base=\"%s\", filter=\"%s\".\n",
636 innerloop, sbase, filter );
637 thread_verbose( idx, thrstr );
639 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
640 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
642 case LDAP_SIZELIMIT_EXCEEDED:
643 case LDAP_TIMELIMIT_EXCEEDED:
645 nvalues = ldap_count_entries( ld, res );
646 if ( nvalues == 0 ) {
648 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
653 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
654 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
656 values[ i ] = ldap_get_dn( ld, e );
662 if ( do_retry == maxretries ) {
663 snprintf( thrstr, BUFSIZ,
664 "Read base=\"%s\" filter=\"%s\" got %d values.\n",
665 sbase, filter, nvalues );
666 thread_verbose( idx, thrstr );
669 for ( i = 0; i < innerloop; i++ ) {
670 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
672 do_read( ld, values[ r ],
673 srchattrs, noattrs, nobind, 1, maxretries,
674 delay, force, chaserefs, idx );
676 for( i = 0; i < nvalues; i++) {
677 if (values[i] != NULL)
678 ldap_memfree( values[i] );
684 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
688 snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
689 thread_verbose( idx, thrstr );
693 do_read( LDAP *ld, char *entry,
694 char **attrs, int noattrs, int nobind, int maxloop,
695 int maxretries, int delay, int force, int chaserefs, int idx )
697 int i = 0, do_retry = maxretries;
698 int rc = LDAP_SUCCESS;
702 if ( do_retry == maxretries ) {
703 snprintf( thrstr, BUFSIZ, "Read(%d): entry=\"%s\".\n",
705 thread_verbose( idx, thrstr );
708 snprintf(thrstr, BUFSIZ, "LD %p cnt: %d (retried %d) (%s)", \
709 (void *) ld, maxloop, (do_retry - maxretries), entry);
710 thread_verbose( idx, thrstr );
712 for ( ; i < maxloop; i++ ) {
713 LDAPMessage *res = NULL;
715 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
716 NULL, attrs, noattrs, NULL, NULL, NULL,
717 LDAP_NO_LIMIT, &res );
725 int first = tester_ignore_err( rc );
729 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
733 /* only log if first occurrence */
734 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
735 tester_ldap_error( ld, buf, NULL );
740 /* busy needs special handling */
741 tester_ldap_error( ld, buf, NULL );
742 if ( rc == LDAP_BUSY && do_retry > 0 ) {