示例#1
0
int
main(int argc, char **argv) {
	sg_log_init("libstatgrab-test", "SGTEST_LOG_PROPERTIES", argc ? argv[0] : NULL);
	sg_init(1);

	if( 0 != get_params( opt_def, argc, argv ) ) {
		help(argv[0]);
		return 1;
	}

	if( opt_def[OPT_HLP].optarg.b ) {
		help(argv[0]);
		return 0;
	}
	else if( opt_def[OPT_LIST].optarg.b ) {
		print_testable_functions(0);
		return 0;
	}
	else if( opt_def[OPT_RUN].optarg.str ) {
		size_t *test_routines = NULL;
		size_t entries = funcnames_to_indices(opt_def[OPT_RUN].optarg.str, &test_routines, 0);
		int errors = 0;

		if( 0 == entries ) {
			die( ESRCH, "no functions to test" );
			return 255;
		}

		while( opt_def[OPT_NLOOPS].optarg.u-- > 0 ) {
			size_t func_rel_idx;

			for( func_rel_idx = 0; func_rel_idx < entries; ++func_rel_idx ) {
				mark_func(test_routines[func_rel_idx]);
				if( !run_func( test_routines[func_rel_idx], 0 ) ) {
					done_func(test_routines[func_rel_idx], 0);
					++errors;
				}
				else {
					done_func(test_routines[func_rel_idx], 1);
				}
			}
		}

		(void)report_testable_functions(0);
		free(test_routines);

		return errors;
	}

	help(argv[0]);
	return 1;
}
int
main(int argc, char **argv) {
	sg_log_init("libstatgrab-test", "SGTEST_LOG_PROPERTIES", argc ? argv[0] : NULL);
	sg_init(1);

	if( 0 != get_params( opt_def, argc, argv ) ) {
		help(argv[0]);
		return 1;
	}

	if( opt_def[OPT_HLP].optarg.b ) {
		help(argv[0]);
		return 0;
	}
	else if( opt_def[OPT_LIST].optarg.b ) {
		print_testable_functions(0);
		return 0;
	}
	else if( opt_def[OPT_RUN].optarg.str ) {
		unsigned long numthreads, i;
		size_t *test_routines = NULL, nfuncs, ok;
		struct statgrab_testfuncs *sg_testfuncs = get_testable_functions(&nfuncs);
		size_t entries = funcnames_to_indices(opt_def[OPT_RUN].optarg.str, &test_routines, 0);
		pthread_t *threadid = NULL;
		int rc, errors = 0;

		if( 0 == entries ) {
			die( ESRCH, "no functions to test" );
			return 255;
		}
		if( -1 != opt_def[OPT_NTHREADS].optarg.s ) {
			numthreads = opt_def[OPT_NTHREADS].optarg.u;
			if( numthreads < entries ) {
				die( ERANGE, "%s %s - to small number for thread count", argv[0], argv[2] );
			}
		}
		else if( opt_def[OPT_MTHREADS].optarg.u > 1 ) {
			numthreads = entries * opt_def[OPT_MTHREADS].optarg.u;
		}
		else {
			numthreads = entries;
		}

		if( NULL == ( threadid = calloc( sizeof(threadid[0]), numthreads ) ) )
			die( ENOMEM, "%s", argv[0] );

		rc = pthread_mutex_lock(&mutex);
		prove_libcall("pthread_mutex_lock", rc);

		TRACE_LOG_FMT( "multi_threaded", "create %lu threads", numthreads );
		for( i = 0; i < numthreads; ++i ) {
			mark_func(test_routines[i % entries]);
			rc = pthread_create( &threadid[i], NULL, threadfunc, &test_routines[i % entries] );
			prove_libcall("pthread_create", rc);
		}

		rc = pthread_mutex_unlock(&mutex);
		prove_libcall("pthread_mutex_unlock", rc);

		while( test_counter < numthreads )
			sched_yield();

		rc = pthread_mutex_lock(&mutex);
		prove_libcall("pthread_mutex_lock", rc);

		/* The condition has occured. Set the flag and wake up any waiting threads */
		conditionMet = 1;
		TRACE_LOG( "multi_threaded", "Wake up all waiting threads..." );
		rc = pthread_cond_broadcast(&cond);
		prove_libcall("pthread_cond_broadcast", rc);

		rc = pthread_mutex_unlock(&mutex);
		prove_libcall("pthread_mutex_unlock", rc);

		TRACE_LOG( "multi_threaded", "Wait for threads and cleanup" );
		do {
			struct timespec ts = { 1, 0 };
			struct timeval tv;

			gettimeofday(&tv, NULL);
			ts.tv_sec += tv.tv_sec;

			rc = pthread_mutex_lock(&mutex);
			prove_libcall("pthread_mutex_lock", rc);

			pthread_cond_timedwait(&cond, &mutex, &ts);
			prove_libcall("pthread_cond_timedwait", rc);

			ok = report_testable_functions(0);

			rc = pthread_mutex_unlock(&mutex);
			prove_libcall("pthread_mutex_unlock", rc);

			if( ok != nfuncs )
				printf( "---------------\n" );
			fflush(stdout);
		} while( ok != nfuncs );

		for (i=0; i<numthreads; ++i) {
			rc = pthread_join(threadid[i], NULL);
			prove_libcall("pthread_join", rc);
		}
		pthread_cond_destroy(&cond);
		pthread_mutex_destroy(&mutex);

		for( i = 0; i < nfuncs; ++i )
			errors += sg_testfuncs[i].needed - sg_testfuncs[i].succeeded;

		TRACE_LOG_FMT( "multi_threaded", "Main completed with test_counter = %lu", test_counter );

		return errors;
	}

	help(argv[0]);
	return 1;
}