📄 tm_basic.cxx
字号:
end_of_test_group(void){ disable_clock_latency_measurement(); diag_printf("\n"); enable_clock_latency_measurement();}//--------------------------------------------------------------------------// Compute a name for a threadchar *thread_name(char *basename, int indx) { return "<<NULL>>"; // Not currently used}//--------------------------------------------------------------------------// test0 - null test, just returnvoid *test0(void *indx){ return indx;}//--------------------------------------------------------------------------// test3 - loop, yeilding repeatedly and checking for cancellationvoid *test3(void *indx){ for(;;) { sched_yield(); pthread_testcancel(); } return indx;}//--------------------------------------------------------------------------// test1 - empty test, simply exit. Last thread signals parent.void *test1( void *indx){ if ((cyg_uint32)indx == (cyg_uint32)(ntest_threads-1)) { sem_post(&synchro); // Signal that last thread is dying } return indx;}//--------------------------------------------------------------------------// test2 - measure thread switch timesvoid *test2(void *indx){ int i; for (i = 0; i < nthread_switches; i++) { if ((int)indx == 0) { HAL_CLOCK_READ(&test2_ft[i].start); } else { HAL_CLOCK_READ(&test2_ft[i].end); } sched_yield(); } if ((int)indx == 1) { sem_post(&synchro); } return indx;}//--------------------------------------------------------------------------// Full-circuit mutex unlock/lock testvoid *mutex_test(void * indx){ int i; pthread_mutex_lock(&test_mutexes[0]); for (i = 0; i < nmutexes; i++) { sem_wait(&synchro); wait_for_tick(); // Wait until the next clock tick to minimize aberations HAL_CLOCK_READ(&mutex_ft[i].start); pthread_mutex_unlock(&test_mutexes[0]); pthread_mutex_lock(&test_mutexes[0]); sem_post(&synchro); } return indx;}//--------------------------------------------------------------------------// Full-circuit mbox put/get test#if 0voidmbox_test(cyg_uint32 indx){ void *item; do { item = cyg_mbox_get(test_mbox_handles[0]); HAL_CLOCK_READ(&mbox_ft[(int)item].end); cyg_semaphore_post(&synchro); } while ((int)item != (nmboxes-1)); cyg_thread_exit(0);}#endif//--------------------------------------------------------------------------// Full-circuit semaphore post/wait testvoid *semaphore_test(void * indx){ int i; for (i = 0; i < nsemaphores; i++) { sem_wait(&test_semaphores[0]); HAL_CLOCK_READ(&semaphore_ft[i].end); sem_post(&synchro); } return indx;}//--------------------------------------------------------------------------//// This set of tests is used to measure kernel primitives that deal with threads//voidrun_thread_tests(void){ int i; struct sched_param schedparam; pthread_attr_t attr; int policy; void *retval; // Set my priority higher than any I plan to create schedparam.sched_priority = 30; pthread_setschedparam( pthread_self(), SCHED_RR, &schedparam ); // Initiaize thread creation attributes pthread_attr_init( &attr ); pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ); pthread_attr_setschedpolicy( &attr, SCHED_RR ); schedparam.sched_priority = 10; pthread_attr_setschedparam( &attr, &schedparam ); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); pthread_attr_setstackaddr( &attr, &stacks[i][STACK_SIZE] ); pthread_attr_setstacksize( &attr, STACK_SIZE ); pthread_create( &threads[i], &attr, test0, (void *)i ); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Create thread"); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); sched_yield(); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Yield thread [all lower priority]"); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); schedparam.sched_priority = 11; pthread_attr_setschedparam( &attr, &schedparam ); pthread_setschedparam(threads[i], SCHED_RR, &schedparam); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Set priority"); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); pthread_getschedparam( threads[i], &policy, &schedparam ); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Get priority"); cyg_thread_delay(1); // Let the test threads run wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); pthread_join(threads[i], &retval); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Join exited thread"); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); sched_yield(); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Yield [no other] thread"); // Recreate the test set schedparam.sched_priority = 10; pthread_attr_setschedparam( &attr, &schedparam ); for (i = 0; i < ntest_threads; i++) { pthread_attr_setstackaddr( &attr, &stacks[i][STACK_SIZE] ); pthread_attr_setstacksize( &attr, STACK_SIZE ); pthread_create( &threads[i], &attr, test3, (void *)i ); } cyg_thread_delay(1); // Let the test threads run wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); pthread_cancel(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Cancel [running] thread"); cyg_thread_delay(1); // Let the test threads do their cancellations wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); pthread_join(threads[i], &retval); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Join [cancelled] thread"); // Set my priority lower than any I plan to create schedparam.sched_priority = 5; pthread_setschedparam( pthread_self(), SCHED_RR, &schedparam ); // Set up the end-of-threads synchronizer sem_init(&synchro, 0, 0); schedparam.sched_priority = 10; pthread_attr_setschedparam( &attr, &schedparam ); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < ntest_threads; i++) { HAL_CLOCK_READ(&thread_ft[i].start); pthread_attr_setstackaddr( &attr, &stacks[i][STACK_SIZE] ); pthread_attr_setstacksize( &attr, STACK_SIZE ); pthread_create( &threads[i], &attr, test2, (void *)i ); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Create [high priority] thread"); sem_wait(&synchro); // Wait for all threads to finish // Make sure they are all dead for (i = 0; i < ntest_threads; i++) { pthread_join(threads[i], &retval); } run_thread_switch_test(); end_of_test_group();}//--------------------------------------------------------------------------voidrun_thread_switch_test(void){ int i; struct sched_param schedparam; pthread_attr_t attr; void *retval; // Set my priority higher than any I plan to create schedparam.sched_priority = 30; pthread_setschedparam( pthread_self(), SCHED_RR, &schedparam ); // Initiaize thread creation attributes pthread_attr_init( &attr ); pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ); pthread_attr_setschedpolicy( &attr, SCHED_RR ); schedparam.sched_priority = 10; pthread_attr_setschedparam( &attr, &schedparam ); // Set up the end-of-threads synchronizer sem_init(&synchro, 0, 0); // Set up for thread context switch for (i = 0; i < 2; i++) { pthread_attr_setstackaddr( &attr, &stacks[i][STACK_SIZE] ); pthread_attr_setstacksize( &attr, STACK_SIZE ); pthread_create( &threads[i], &attr, test2, (void *)i ); } wait_for_tick(); // Wait until the next clock tick to minimize aberations sem_wait(&synchro); show_times(test2_ft, nthread_switches, "Thread switch"); // Clean up for (i = 0; i < 2; i++) { pthread_join(threads[i], &retval); }}//--------------------------------------------------------------------------voidrun_mutex_tests(void){ int i; pthread_mutexattr_t attr; pthread_mutexattr_init( &attr ); // Mutex primitives wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < nmutexes; i++) { HAL_CLOCK_READ(&mutex_ft[i].start); pthread_mutex_init(&test_mutexes[i], &attr); HAL_CLOCK_READ(&mutex_ft[i].end); } show_times(mutex_ft, nmutexes, "Init mutex"); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < nmutexes; i++) { HAL_CLOCK_READ(&mutex_ft[i].start); pthread_mutex_lock(&test_mutexes[i]); HAL_CLOCK_READ(&mutex_ft[i].end); } show_times(mutex_ft, nmutexes, "Lock [unlocked] mutex"); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < nmutexes; i++) { HAL_CLOCK_READ(&mutex_ft[i].start); pthread_mutex_unlock(&test_mutexes[i]); HAL_CLOCK_READ(&mutex_ft[i].end); } show_times(mutex_ft, nmutexes, "Unlock [locked] mutex"); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < nmutexes; i++) { HAL_CLOCK_READ(&mutex_ft[i].start); pthread_mutex_trylock(&test_mutexes[i]); HAL_CLOCK_READ(&mutex_ft[i].end); } show_times(mutex_ft, nmutexes, "Trylock [unlocked] mutex"); wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < nmutexes; i++) { HAL_CLOCK_READ(&mutex_ft[i].start); pthread_mutex_trylock(&test_mutexes[i]); HAL_CLOCK_READ(&mutex_ft[i].end); } show_times(mutex_ft, nmutexes, "Trylock [locked] mutex"); // Must unlock mutices before destroying them. for (i = 0; i < nmutexes; i++) { pthread_mutex_unlock(&test_mutexes[i]); } wait_for_tick(); // Wait until the next clock tick to minimize aberations for (i = 0; i < nmutexes; i++) { HAL_CLOCK_READ(&mutex_ft[i].start); pthread_mutex_destroy(&test_mutexes[i]); HAL_CLOCK_READ(&mutex_ft[i].end); } show_times(mutex_ft, nmutexes, "Destroy mutex"); run_mutex_circuit_test(); end_of_test_group();}//--------------------------------------------------------------------------voidrun_mutex_circuit_test(void){ int i; pthread_mutexattr_t mattr; struct sched_param schedparam; pthread_attr_t attr; void *retval;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -