2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2017 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_read( LDAP *ld, char *entry,
54 char **attrs, int noattrs, int nobind, int maxloop,
59 char *sbase, char *filter, char **attrs, int noattrs, int nobind,
64 char *sbase, char *filter, char **attrs, int noattrs, int nobind,
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)
85 struct tester_conn_args *config;
89 char *srchattrs[] = { "1.1", NULL };
90 char **attrs = srchattrs;
101 thread_error(int idx, char *string)
105 snprintf(thrstr, BUFSIZ, "error on tidx: %d: %s", idx, string);
106 tester_error( thrstr );
110 thread_output(int idx, char *string)
114 snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string);
115 tester_error( thrstr );
119 thread_verbose(int idx, char *string)
125 snprintf(thrstr, BUFSIZ, "tidx: %d says: %s", idx, string);
126 tester_error( thrstr );
130 usage( char *name, char opt )
133 fprintf( stderr, "%s: unable to handle option \'%c\'\n\n",
137 fprintf( stderr, "usage: %s " TESTER_COMMON_HELP
151 exit( EXIT_FAILURE );
155 main( int argc, char **argv )
159 char *host = "localhost";
161 char *manager = NULL;
162 struct berval passwd = { 0, NULL };
167 config = tester_init( "slapd-mtread", TESTER_READ );
169 /* by default, tolerate referrals and no such object */
170 tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
172 while ( (i = getopt( argc, argv, TESTER_COMMON_OPTS "Ac:e:Ff:M:m:NT:v" )) != EOF ) {
179 nobind = TESTER_INIT_ONLY;
186 case 'c': /* the number of connections */
187 if ( lutil_atoi( &noconns, optarg ) != 0 ) {
192 case 'e': /* DN to search for */
193 entry = strdup( optarg );
196 case 'f': /* the search request */
197 filter = strdup( optarg );
204 case 'M': /* the number of R/W threads */
205 if ( lutil_atoi( &rwthreads, optarg ) != 0 ) {
208 if (rwthreads > MAX_THREAD)
209 rwthreads = MAX_THREAD;
212 case 'm': /* the number of threads */
213 if ( lutil_atoi( &threads, optarg ) != 0 ) {
216 if (threads > MAX_THREAD)
217 threads = MAX_THREAD;
221 attrs = ldap_str2charray( optarg, "," );
222 if ( attrs == NULL ) {
228 if ( tester_config_opt( config, i, optarg ) == LDAP_SUCCESS ) {
239 if ( *entry == '\0' ) {
240 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
242 exit( EXIT_FAILURE );
245 if ( argv[optind] != NULL ) {
246 attrs = &argv[optind];
251 if (noconns > MAXCONN)
253 lds = (LDAP **) calloc( sizeof(LDAP *), noconns);
255 fprintf( stderr, "%s: Memory error: calloc noconns.\n",
257 exit( EXIT_FAILURE );
260 tester_config_finish( config );
261 ldap_pvt_thread_initialize();
263 for (i = 0; i < noconns; i++) {
264 tester_init_ld( &lds[i], config, nobind );
267 snprintf(outstr, BUFSIZ, "MT Test Start: conns: %d (%s)", noconns, uri);
268 tester_error(outstr);
269 snprintf(outstr, BUFSIZ, "Threads: RO: %d RW: %d", threads, rwthreads);
270 tester_error(outstr);
272 /* Set up read only threads */
273 for ( i = 0; i < threads; i++ ) {
274 ldap_pvt_thread_create( &rtid[i], 0, do_onethread, &rtid[i]);
275 snprintf(outstr, BUFSIZ, "Created RO thread %d", i);
276 thread_verbose(-1, outstr);
278 /* Set up read/write threads */
279 for ( i = 0; i < rwthreads; i++ ) {
280 ldap_pvt_thread_create( &rwtid[i], 0, do_onerwthread, &rwtid[i]);
281 snprintf(outstr, BUFSIZ, "Created RW thread %d", i + MAX_THREAD);
282 thread_verbose(-1, outstr);
285 ptpass = config->outerloops * config->loops;
287 /* wait for read only threads to complete */
288 for ( i = 0; i < threads; i++ )
289 ldap_pvt_thread_join(rtid[i], NULL);
290 /* wait for read/write threads to complete */
291 for ( i = 0; i < rwthreads; i++ )
292 ldap_pvt_thread_join(rwtid[i], NULL);
294 for(i = 0; i < noconns; i++) {
295 if ( lds[i] != NULL ) {
296 ldap_unbind_ext( lds[i], NULL, NULL );
301 for ( i = 0; i < threads; i++ ) {
302 snprintf(outstr, BUFSIZ, "RO thread %d pass=%d fail=%d", i,
303 rt_pass[i], rt_fail[i]);
304 tester_error(outstr);
305 if (rt_fail[i] != 0 || rt_pass[i] != ptpass) {
306 snprintf(outstr, BUFSIZ, "FAIL RO thread %d", i);
307 tester_error(outstr);
311 for ( i = 0; i < rwthreads; i++ ) {
312 snprintf(outstr, BUFSIZ, "RW thread %d pass=%d fail=%d", i + MAX_THREAD,
313 rwt_pass[i], rwt_fail[i]);
314 tester_error(outstr);
315 if (rwt_fail[i] != 0 || rwt_pass[i] != ptpass) {
316 snprintf(outstr, BUFSIZ, "FAIL RW thread %d", i);
317 tester_error(outstr);
321 snprintf(outstr, BUFSIZ, "MT Test complete" );
322 tester_error(outstr);
325 exit( EXIT_FAILURE );
326 exit( EXIT_SUCCESS );
330 do_onethread( void *arg )
336 int idx = (ldap_pvt_thread_t *)arg - rtid;
338 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
340 thread_error( idx, "Memory error: thread calloc for noconns" );
341 exit( EXIT_FAILURE );
344 for ( j = 0; j < config->outerloops; j++ ) {
345 for(i = 0; i < noconns; i++) {
346 mlds[i] = ldap_dup(lds[i]);
347 if (mlds[i] == NULL) {
348 thread_error( idx, "ldap_dup error" );
351 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
352 snprintf(thrstr, BUFSIZ,
353 "RO Thread conns: %d refcnt: %d (rc = %d)",
354 noconns, refcnt, rc);
355 thread_verbose(idx, thrstr);
357 thisconn = (idx + j) % noconns;
358 if (thisconn < 0 || thisconn >= noconns)
360 if (mlds[thisconn] == NULL) {
361 thread_error( idx, "(failed to dup)");
362 tester_perror( "ldap_dup", "(failed to dup)" );
363 exit( EXIT_FAILURE );
365 snprintf(thrstr, BUFSIZ, "Using conn %d", thisconn);
366 thread_verbose(idx, thrstr);
367 if ( filter != NULL ) {
368 if (strchr(filter, '['))
369 do_random2( mlds[thisconn], entry, filter, attrs,
370 noattrs, nobind, force, idx );
372 do_random( mlds[thisconn], entry, filter, attrs,
373 noattrs, nobind, force, idx );
376 do_read( mlds[thisconn], entry, attrs, noattrs,
377 nobind, config->loops, force, idx );
379 for(i = 0; i < noconns; i++) {
380 (void) ldap_destroy(mlds[i]);
389 do_onerwthread( void *arg )
394 char dn[256], uids[32], cns[32], *base;
395 LDAPMod *attrp[5], attrs[4];
396 char *oc_vals[] = { "top", "OpenLDAPperson", NULL };
397 char *cn_vals[] = { NULL, NULL };
398 char *sn_vals[] = { NULL, NULL };
399 char *uid_vals[] = { NULL, NULL };
404 int idx = (ldap_pvt_thread_t *)arg - rtid;
406 mlds = (LDAP **) calloc( sizeof(LDAP *), noconns);
408 thread_error( idx, "Memory error: thread calloc for noconns" );
409 exit( EXIT_FAILURE );
412 snprintf(uids, sizeof(uids), "rwtest%04d", idx);
413 snprintf(cns, sizeof(cns), "rwtest%04d", idx);
415 for (i = 0; i < 4; i++) {
416 attrp[i] = &attrs[i];
420 attrs[0].mod_type = "objectClass";
421 attrs[0].mod_values = oc_vals;
422 attrs[1].mod_type = "cn";
423 attrs[1].mod_values = cn_vals;
424 cn_vals[0] = &cns[0];
425 attrs[2].mod_type = "sn";
426 attrs[2].mod_values = sn_vals;
427 sn_vals[0] = &cns[0];
428 attrs[3].mod_type = "uid";
429 attrs[3].mod_values = uid_vals;
430 uid_vals[0] = &uids[0];
432 for ( j = 0; j < config->outerloops; j++ ) {
433 for(i = 0; i < noconns; i++) {
434 mlds[i] = ldap_dup(lds[i]);
435 if (mlds[i] == NULL) {
436 thread_error( idx, "ldap_dup error" );
439 rc = ldap_get_option(mlds[0], LDAP_OPT_SESSION_REFCNT, &refcnt);
440 snprintf(thrstr, BUFSIZ,
441 "RW Thread conns: %d refcnt: %d (rc = %d)",
442 noconns, refcnt, rc);
443 thread_verbose(idx, thrstr);
445 thisconn = (idx + j) % noconns;
446 if (thisconn < 0 || thisconn >= noconns)
448 if (mlds[thisconn] == NULL) {
449 thread_error( idx, "(failed to dup)");
450 tester_perror( "ldap_dup", "(failed to dup)" );
451 exit( EXIT_FAILURE );
453 snprintf(thrstr, BUFSIZ, "START RW Thread using conn %d", thisconn);
454 thread_verbose(idx, thrstr);
461 snprintf(dn, 256, "cn=%s,%s", cns, base);
465 for (i = 0; i < config->loops; i++) {
466 ret = ldap_add_ext_s(ld, dn, &attrp[0], NULL, NULL);
467 if (ret == LDAP_SUCCESS) {
469 ret = ldap_delete_ext_s(ld, dn, NULL, NULL);
470 if (ret == LDAP_SUCCESS) {
474 thread_output(idx, ldap_err2string(ret));
478 thread_output(idx, ldap_err2string(ret));
483 snprintf(thrstr, BUFSIZ,
484 "INNER STOP RW Thread using conn %d (%d/%d)",
485 thisconn, adds, dels);
486 thread_verbose(idx, thrstr);
488 for(i = 0; i < noconns; i++) {
489 (void) ldap_destroy(mlds[i]);
500 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
503 int i = 0, do_retry = config->retries;
505 int rc = LDAP_SUCCESS;
507 char **values = NULL;
508 LDAPMessage *res = NULL, *e = NULL;
511 attrs[ 0 ] = LDAP_NO_ATTRS;
514 snprintf( thrstr, BUFSIZ,
515 "Read(%d): base=\"%s\", filter=\"%s\".\n",
516 config->loops, sbase, filter );
517 thread_verbose( idx, thrstr );
519 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
520 filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
522 case LDAP_SIZELIMIT_EXCEEDED:
523 case LDAP_TIMELIMIT_EXCEEDED:
525 nvalues = ldap_count_entries( ld, res );
526 if ( nvalues == 0 ) {
528 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
533 values = malloc( ( nvalues + 1 ) * sizeof( char * ) );
534 for ( i = 0, e = ldap_first_entry( ld, res ); e != NULL; i++, e = ldap_next_entry( ld, e ) )
536 values[ i ] = ldap_get_dn( ld, e );
542 if ( do_retry == config->retries ) {
543 snprintf( thrstr, BUFSIZ,
544 "Read base=\"%s\" filter=\"%s\" got %d values.\n",
545 sbase, filter, nvalues );
546 thread_verbose( idx, thrstr );
549 for ( i = 0; i < config->loops; i++ ) {
550 int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
552 do_read( ld, values[ r ],
553 srchattrs, noattrs, nobind, 1, force, idx );
555 for( i = 0; i < nvalues; i++) {
556 if (values[i] != NULL)
557 ldap_memfree( values[i] );
563 tester_ldap_error( ld, "ldap_search_ext_s", NULL );
567 snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
568 thread_verbose( idx, thrstr );
571 /* substitute a generated int into the filter */
573 do_random2( LDAP *ld,
574 char *sbase, char *filter, char **srchattrs, int noattrs, int nobind,
577 int i = 0, do_retry = config->retries;
578 int rc = LDAP_SUCCESS;
581 LDAPMessage *res = NULL;
586 snprintf( thrstr, BUFSIZ,
587 "Read(%d): base=\"%s\", filter=\"%s\".\n",
588 config->loops, sbase, filter );
589 thread_verbose( idx, thrstr );
591 ptr = strchr(filter, '[');
594 ftail = strchr(filter, ']');
595 if (!ftail || ftail < ptr)
598 sscanf(ptr, "[%d-%d]", &lo, &hi);
604 for ( i = 0; i < config->loops; i++ ) {
605 int r = ((double)range)*rand()/(RAND_MAX + 1.0);
606 sprintf(fbuf, "%.*s%d%s", flen, filter, r, ftail);
608 rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
609 fbuf, srchattrs, noattrs, NULL, NULL, NULL,
610 LDAP_NO_LIMIT, &res );
617 int first = tester_ignore_err( rc );
621 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
625 /* only log if first occurrence */
626 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
627 tester_ldap_error( ld, buf, NULL );
632 /* busy needs special handling */
633 tester_ldap_error( ld, buf, NULL );
634 if ( rc == LDAP_BUSY && do_retry > 0 ) {
642 snprintf( thrstr, BUFSIZ, "Search done (%d).\n", rc );
643 thread_verbose( idx, thrstr );
647 do_read( LDAP *ld, char *entry,
648 char **attrs, int noattrs, int nobind, int maxloop,
651 int i = 0, do_retry = config->retries;
652 int rc = LDAP_SUCCESS;
656 if ( do_retry == config->retries ) {
657 snprintf( thrstr, BUFSIZ, "Read(%d): entry=\"%s\".\n",
659 thread_verbose( idx, thrstr );
662 snprintf(thrstr, BUFSIZ, "LD %p cnt: %d (retried %d) (%s)", \
663 (void *) ld, maxloop, (do_retry - config->retries), entry);
664 thread_verbose( idx, thrstr );
666 for ( ; i < maxloop; i++ ) {
667 LDAPMessage *res = NULL;
669 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
670 NULL, attrs, noattrs, NULL, NULL, NULL,
671 LDAP_NO_LIMIT, &res );
679 int first = tester_ignore_err( rc );
683 snprintf( buf, sizeof( buf ), "ldap_search_ext_s(%s)", entry );
687 /* only log if first occurrence */
688 if ( ( force < 2 && first > 0 ) || abs(first) == 1 ) {
689 tester_ldap_error( ld, buf, NULL );
694 /* busy needs special handling */
695 tester_ldap_error( ld, buf, NULL );
696 if ( rc == LDAP_BUSY && do_retry > 0 ) {