📄 tm_basic.cxx
字号:
con_min = (con_min * 100) / total_samples; show_ticks_in_us(ave); show_ticks_in_us(min); show_ticks_in_us(max); show_ticks_in_us(ave_dev); disable_clock_latency_measurement(); diag_printf(" %3d%% %3d%%", con_ave, con_min); diag_printf(" %s\n", title); enable_clock_latency_measurement();}voidshow_times(fun_times ft[], int nsamples, char *title){ show_times_detail(ft, nsamples, title, false);#ifdef STATS_WITHOUT_FIRST_SAMPLE show_times_detail(ft, nsamples, "", true);#endif}voidshow_test_parameters(void){ disable_clock_latency_measurement(); diag_printf("\nTesting parameters:\n"); diag_printf(" Clock samples: %5d\n", nsamples); diag_printf(" Threads: %5d\n", ntest_threads); diag_printf(" Thread switches: %5d\n", nthread_switches); diag_printf(" Mutexes: %5d\n", nmutexes); diag_printf(" Mailboxes: %5d\n", nmboxes); diag_printf(" Semaphores: %5d\n", nsemaphores); diag_printf(" Scheduler operations: %5d\n", nscheds); diag_printf(" Counters: %5d\n", ncounters); diag_printf(" Alarms: %5d\n", nalarms); diag_printf("\n"); enable_clock_latency_measurement();}voidend_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, never executedvoidtest0(cyg_uint32 indx){ diag_printf("test0.%d executed?\n", indx); cyg_thread_exit();}// test1 - empty test, simply exit. Last thread signals parent.voidtest1(cyg_uint32 indx){ if (indx == (cyg_uint32)(ntest_threads-1)) { cyg_semaphore_post(&synchro); // Signal that last thread is dying } cyg_thread_exit();}// test2 - measure thread switch timesvoidtest2(cyg_uint32 indx){ int i; for (i = 0; i < nthread_switches; i++) { if (indx == 0) { HAL_CLOCK_READ(&test2_ft[i].start); } else { HAL_CLOCK_READ(&test2_ft[i].end); } cyg_thread_yield(); } if (indx == 1) { cyg_semaphore_post(&synchro); } cyg_thread_exit();}// Full-circuit mutex unlock/lock testvoidmutex_test(cyg_uint32 indx){ int i; cyg_mutex_lock(&test_mutexes[0]); for (i = 0; i < nmutexes; i++) { cyg_semaphore_wait(&synchro); wait_for_tick(); // Wait until the next clock tick to minimize aberations HAL_CLOCK_READ(&mutex_ft[i].start); cyg_mutex_unlock(&test_mutexes[0]); cyg_mutex_lock(&test_mutexes[0]); cyg_semaphore_post(&synchro); } cyg_thread_exit();}// Full-circuit mbox put/get testvoidmbox_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();}// Full-circuit semaphore post/wait testvoidsemaphore_test(cyg_uint32 indx){ int i; for (i = 0; i < nsemaphores; i++) { cyg_semaphore_wait(&test_semaphores[0]); HAL_CLOCK_READ(&semaphore_ft[i].end); cyg_semaphore_post(&synchro); } cyg_thread_exit();}//// This set of tests is used to measure kernel primitives that deal with threads//voidrun_thread_tests(void){ int i; cyg_priority_t prio; // Set my priority higher than any I plan to create cyg_thread_set_priority(cyg_thread_self(), 2); 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); cyg_thread_create(10, // Priority - just a number test0, // entry i, // index thread_name("thread", i), // Name &stacks[i][0], // Stack STACK_SIZE, // Size &threads[i], // Handle &test_threads[i] // Thread data structure ); 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); cyg_thread_yield(); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Yield thread [all suspended]"); 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); cyg_thread_suspend(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Suspend [suspended] 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); cyg_thread_resume(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Resume 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); cyg_thread_set_priority(threads[i], 11); 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); prio = cyg_thread_get_priority(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Get 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); cyg_thread_kill(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Kill [suspended] 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); cyg_thread_yield(); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Yield [no other] thread"); // Set my priority higher than any I plan to create cyg_thread_set_priority(cyg_thread_self(), 2); // Recreate the test set for (i = 0; i < ntest_threads; i++) { cyg_thread_create(10, // Priority - just a number test0, // entry i, // index thread_name("thread", i), // Name &stacks[i][0], // Stack STACK_SIZE, // Size &threads[i], // Handle &test_threads[i] // Thread data structure ); } 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); cyg_thread_resume(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Resume [suspended low prio] 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); cyg_thread_resume(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Resume [runnable low prio] 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); cyg_thread_suspend(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Suspend [runnable] 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); cyg_thread_yield(); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Yield [only low prio] 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); cyg_thread_suspend(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Suspend [runnable->not runnable]"); for (i = 0; i < ntest_threads; i++) { cyg_thread_resume(threads[i]); } 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); cyg_thread_kill(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Kill [runnable] 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); cyg_thread_delete(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Destroy [dead] thread"); // Recreate the test set for (i = 0; i < ntest_threads; i++) { cyg_thread_create(10, // Priority - just a number test0, // entry i, // index thread_name("thread", i), // Name &stacks[i][0], // Stack STACK_SIZE, // Size &threads[i], // Handle &test_threads[i] // Thread data structure ); cyg_thread_resume(threads[i]); } 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); cyg_thread_delete(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Destroy [runnable] thread"); // Set my priority lower than any I plan to create cyg_thread_set_priority(cyg_thread_self(), 3); // Set up the end-of-threads synchronizer cyg_semaphore_init(&synchro, 0); // Recreate the test set for (i = 0; i < ntest_threads; i++) { cyg_thread_create(2, // Priority - just a number test1, // entry i, // index thread_name("thread", i), // Name &stacks[i][0], // Stack STACK_SIZE, // Size &threads[i], // Handle &test_threads[i] // Thread data structure ); } 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); cyg_thread_resume(threads[i]); HAL_CLOCK_READ(&thread_ft[i].end); } show_times(thread_ft, ntest_threads, "Resume [high priority] thread"); cyg_semaphore_wait(&synchro); // Wait for all threads to finish // Make sure they are all dead for (i = 0; i < ntest_threads; i++) { cyg_thread_delete(threads[i]); } run_thread_switch_test();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -