2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2009 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"
55 int me = ldap_pvt_thread_self();
60 do_conn( char *uri, char *manager, struct berval *passwd,
61 LDAP **ld, int nobind, int maxretries, int conn_num );
64 do_read( LDAP *ld, char *entry,
65 char **attrs, int noattrs, int nobind, int maxloop,
66 int maxretries, int delay, int force, int chaserefs, int idx );
70 char *sbase, char *filter, char **attrs, int noattrs, int nobind,
71 int innerloop, int maxretries, int delay, int force, int chaserefs,
75 do_onethread( void *arg );
78 do_onerwthread( void *arg );
80 #define MAX_THREAD 1024
81 int rt_pass[MAX_THREAD];
82 int rt_fail[MAX_THREAD];
83 int rwt_pass[MAX_THREAD];
84 int rwt_fail[MAX_THREAD];
87 * Shared globals (command line args)
94 int retries = RETRIES;
98 char *srchattrs[] = { "1.1", NULL };
99 char **attrs = srchattrs;
110 thread_error(char *string)
114 snprintf(thrstr, BUFSIZ, "error on tid: %d: %s", whoami(), string);
115 tester_error( thrstr );
119 thread_output(char *string)
123 snprintf(thrstr, BUFSIZ, "tid: %d says: %s", whoami(), string);
124 tester_error( thrstr );
128 thread_verbose(char *string)
134 snprintf(thrstr, BUFSIZ, "tid: %d says: %s", whoami(), string);
135 tester_error( thrstr );
143 "-H <uri> | ([-h <host>] -p <port>) "
165 exit( EXIT_FAILURE );
169 main( int argc, char **argv )
173 char *host = "localhost";
175 char *manager = NULL;
176 struct berval passwd = { 0, NULL };
177 ldap_pvt_thread_t rtid[MAX_THREAD], rwtid[MAX_THREAD];
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, (void*)i);
346 snprintf(outstr, BUFSIZ, "Created RO thread %d [%d]", i, rtid[i]);
347 thread_verbose(outstr);
349 /* Set up read/write threads */
350 for ( i = 0; i < rwthreads; i++ ) {
351 ldap_pvt_thread_create( &rwtid[i], 0, do_onerwthread, (void*)i);
352 snprintf(outstr, BUFSIZ, "Created RW thread %d [%d]", i, rwtid[i]);
353 thread_verbose(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,
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 )
408 int myidx = (int)arg;
410 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
412 thread_error( "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( "ldap_dup error" );
423 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
424 snprintf(thrstr, BUFSIZ,
425 "RO Thread: %d conns: %d refcnt: %d (rc = %d)",
426 me, noconns, refcnt, rc);
427 thread_verbose(thrstr);
429 thisconn = (me + j) % noconns;
430 if (thisconn < 0 || thisconn >= noconns)
432 if (mlds[thisconn] == NULL) {
433 thread_error("(failed to dup)");
434 tester_perror( "ldap_dup", "(failed to dup)" );
435 exit( EXIT_FAILURE );
437 snprintf(thrstr, BUFSIZ, "Thread %d using conn %d", me, thisconn);
438 thread_verbose(thrstr);
439 if ( filter != NULL ) {
440 do_random( mlds[thisconn], entry, filter, attrs,
441 noattrs, nobind, loops, retries, delay, force,
445 do_read( mlds[thisconn], entry, attrs,
446 noattrs, nobind, loops, retries, delay, force,
449 for(i = 0; i < noconns; i++) {
450 (void) ldap_destroy(mlds[i]);
459 do_onerwthread( void *arg )
465 char dn[256], uids[32], cns[32], *base;
466 LDAPMod *attrp[5], attrs[4];
467 char *oc_vals[] = { "top", "OpenLDAPperson", NULL };
468 char *cn_vals[] = { NULL, NULL };
469 char *sn_vals[] = { NULL, NULL };
470 char *uid_vals[] = { NULL, NULL };
475 int myidx = (int)arg;
477 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
479 thread_error( "Memory error: thread calloc for noconns" );
480 exit( EXIT_FAILURE );
483 snprintf(uids, 32, "rwtest%04.4d", me);
484 snprintf(cns, 32, "rwtest%04.4d", me);
486 for (i = 0; i < 4; i++) {
487 attrp[i] = &attrs[i];
491 attrs[0].mod_type = "objectClass";
492 attrs[0].mod_values = oc_vals;
493 attrs[1].mod_type = "cn";
494 attrs[1].mod_values = cn_vals;
495 cn_vals[0] = &cns[0];
496 attrs[2].mod_type = "sn";
497 attrs[2].mod_values = sn_vals;
498 sn_vals[0] = &cns[0];
499 attrs[3].mod_type = "uid";
500 attrs[3].mod_values = uid_vals;
501 uid_vals[0] = &uids[0];
503 for ( j = 0; j < outerloops; j++ ) {
504 for(i = 0; i < noconns; i++) {
505 mlds[i] = ldap_dup(lds[i]);
506 if (mlds[i] == NULL) {
507 thread_error( "ldap_dup error" );
510 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
511 snprintf(thrstr, BUFSIZ,
512 "RW Thread: %d conns: %d refcnt: %d (rc = %d)",
513 me, noconns, refcnt, rc);
514 thread_verbose(thrstr);
516 thisconn = (me + j) % noconns;
517 if (thisconn < 0 || thisconn >= noconns)
519 if (mlds[thisconn] == NULL) {
520 thread_error("(failed to dup)");
521 tester_perror( "ldap_dup", "(failed to dup)" );
522 exit( EXIT_FAILURE );
524 snprintf(thrstr, BUFSIZ, "START RW Thread %d using conn %d",
526 thread_verbose(thrstr);
533 snprintf(dn, 256, "cn=%s,%s", cns, base);
537 for (i = 0; i < loops; i++) {
538 ret = ldap_add_ext_s(ld, dn, &attrp[0], NULL, NULL);
539 if (ret == LDAP_SUCCESS) {
541 ret = ldap_delete_ext_s(ld, dn, NULL, NULL);
542 if (ret == LDAP_SUCCESS) {
546 thread_output(ldap_err2string(ret));
550 thread_output(ldap_err2string(ret));
555 snprintf(thrstr, BUFSIZ,
556 "INNER STOP RW Thread %d using conn %d (%d/%d)",
557 me, thisconn, adds, dels);
558 thread_verbose(thrstr);
560 for(i = 0; i < noconns; i++) {
561 (void) ldap_destroy(mlds[i]);
571 do_conn( char *uri, char *manager, struct berval *passwd,
572 LDAP **ldp, int nobind, int maxretries, int conn_num )
575 int version = LDAP_VERSION3;
576 int i = 0, do_retry = maxretries;
577 int rc = LDAP_SUCCESS;
581 ldap_initialize( &ld, uri );
583 snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
584 tester_error( thrstr );
585 tester_perror( "ldap_initialize", NULL );
586 exit( EXIT_FAILURE );
589 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
590 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
591 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
593 if ( do_retry == maxretries ) {
594 snprintf( thrstr, BUFSIZ, "do_conn #%d\n", conn_num );
595 thread_verbose( thrstr );
599 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
600 if ( rc != LDAP_SUCCESS ) {
601 snprintf( thrstr, BUFSIZ, "connection: %d", conn_num );
602 tester_error( thrstr );
603 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
606 case LDAP_UNAVAILABLE:
607 if ( do_retry > 0 ) {
608 ldap_unbind_ext( ld, NULL, NULL );
620 exit( EXIT_FAILURE );
628 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
629 int innerloop, int maxretries, int delay, int force, int chaserefs,
632 int i = 0, do_retry = maxretries;
634 int rc = LDAP_SUCCESS;
636 char **values = NULL;
637 LDAPMessage *res = NULL, *e = NULL;
640 attrs[ 0 ] = LDAP_NO_ATTRS;
643 snprintf( thrstr, BUFSIZ,
644 "Read(%d): base=\"%s\", filter=\"%s\".\n",
645 innerloop, sbase, filter );
646 thread_verbose( thrstr );
648 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
649 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
651 case LDAP_SIZELIMIT_EXCEEDED:
652 case LDAP_TIMELIMIT_EXCEEDED:
654 nvalues = ldap_count_entries( ld, res );
655 if ( nvalues == 0 ) {
657 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
662 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
663 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
665 values[ i ] = ldap_get_dn( ld, e );
671 if ( do_retry == maxretries ) {
672 snprintf( thrstr, BUFSIZ,
673 "Read base=\"%s\" filter=\"%s\" got %d values.\n",
674 sbase, filter, nvalues );
675 thread_verbose( thrstr );
678 for ( i = 0; i < innerloop; i++ ) {
679 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
681 do_read( ld, values[ r ],
682 srchattrs, noattrs, nobind, 1, maxretries,
683 delay, force, chaserefs, idx );
685 for( i = 0; i < nvalues; i++) {
686 if (values[i] != NULL)
693 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
697 snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
698 thread_verbose( thrstr );
702 do_read( LDAP *ld, char *entry,
703 char **attrs, int noattrs, int nobind, int maxloop,
704 int maxretries, int delay, int force, int chaserefs, int idx )
706 int i = 0, do_retry = maxretries;
707 int rc = LDAP_SUCCESS;
711 if ( do_retry == maxretries ) {
712 snprintf( thrstr, BUFSIZ, "Read(%d): entry=\"%s\".\n",
714 thread_verbose( thrstr );
717 snprintf(thrstr, BUFSIZ, "tid: %d LD %x cnt: %d (retried %d) (%s)", \
718 whoami(), ld, maxloop, (do_retry - maxretries), entry);
719 thread_verbose( thrstr );
721 for ( ; i < maxloop; i++ ) {
722 LDAPMessage *res = NULL;
724 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
725 NULL, attrs, noattrs, NULL, NULL, NULL,
726 LDAP_NO_LIMIT, &res );
734 int first = tester_ignore_err( rc );
738 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
742 /* only log if first occurrence */
743 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
744 tester_ldap_error( ld, buf, NULL );
749 /* busy needs special handling */
750 tester_ldap_error( ld, buf, NULL );
751 if ( rc == LDAP_BUSY && do_retry > 0 ) {