| 1 | /*
|
|---|
| 2 | */
|
|---|
| 3 |
|
|---|
| 4 | #include <print.h>
|
|---|
| 5 | #include <debug.h>
|
|---|
| 6 |
|
|---|
| 7 | #include <test.h>
|
|---|
| 8 | #include <smp/smp_call.h>
|
|---|
| 9 | #include <cpu.h>
|
|---|
| 10 | #include <macros.h>
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 | #include <arch.h>
|
|---|
| 13 |
|
|---|
| 14 | /*
|
|---|
| 15 | * Maximum total number of smp_calls in the system is:
|
|---|
| 16 | * 128000 == 8^2 * 1000 * 2
|
|---|
| 17 | * == MAX_CPUS^2 * ITERATIONS * EACH_CPU_INC_PER_ITER
|
|---|
| 18 | */
|
|---|
| 19 | #define MAX_CPUS 8
|
|---|
| 20 | #define ITERATIONS 1000
|
|---|
| 21 | #define EACH_CPU_INC_PER_ITER 2
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | static void inc(void *p)
|
|---|
| 25 | {
|
|---|
| 26 | ASSERT(interrupts_disabled());
|
|---|
| 27 |
|
|---|
| 28 | size_t *pcall_cnt = (size_t*)p;
|
|---|
| 29 | /*
|
|---|
| 30 | * No synchronization. Tests if smp_calls makes changes
|
|---|
| 31 | * visible to the caller.
|
|---|
| 32 | */
|
|---|
| 33 | ++*pcall_cnt;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | static void test_thread(void *p)
|
|---|
| 38 | {
|
|---|
| 39 | size_t *pcall_cnt = (size_t*)p;
|
|---|
| 40 | smp_call_t call_info[MAX_CPUS];
|
|---|
| 41 |
|
|---|
| 42 | size_t cpu_count = min(config.cpu_count, MAX_CPUS);
|
|---|
| 43 |
|
|---|
| 44 | for (int iter = 0; iter < ITERATIONS; ++iter) {
|
|---|
| 45 | /* Synchronous version. */
|
|---|
| 46 | for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
|
|---|
| 47 | /*
|
|---|
| 48 | * smp_call should make changes by inc() visible on this cpu.
|
|---|
| 49 | * As a result we can pass it our pcall_cnt and not worry
|
|---|
| 50 | * about other synchronization.
|
|---|
| 51 | */
|
|---|
| 52 | smp_call(cpu_id, inc, pcall_cnt);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /*
|
|---|
| 56 | * Async calls run in parallel on different cpus, so passing the
|
|---|
| 57 | * same counter would clobber it without additional synchronization.
|
|---|
| 58 | */
|
|---|
| 59 | size_t local_cnt[MAX_CPUS] = {0};
|
|---|
| 60 |
|
|---|
| 61 | /* Now start asynchronous calls. */
|
|---|
| 62 | for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
|
|---|
| 63 | smp_call_async(cpu_id, inc, &local_cnt[cpu_id], &call_info[cpu_id]);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /* And wait for all async calls to complete. */
|
|---|
| 67 | for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
|
|---|
| 68 | smp_call_wait(&call_info[cpu_id]);
|
|---|
| 69 | *pcall_cnt += local_cnt[cpu_id];
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /* Give other threads a chance to run. */
|
|---|
| 73 | thread_usleep(10000);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | const char *test_smpcall1(void)
|
|---|
| 79 | {
|
|---|
| 80 | /* Number of received calls that were sent by cpu[i]. */
|
|---|
| 81 | size_t call_cnt[MAX_CPUS] = {0};
|
|---|
| 82 | thread_t *thread[MAX_CPUS] = {0};
|
|---|
| 83 |
|
|---|
| 84 | unsigned int cpu_count = min(config.cpu_count, MAX_CPUS);
|
|---|
| 85 | size_t running_thread_cnt = 0;
|
|---|
| 86 |
|
|---|
| 87 | TPRINTF("Spawning threads on %u cpus.\n", cpu_count);
|
|---|
| 88 |
|
|---|
| 89 | /* Create a wired thread on each cpu. */
|
|---|
| 90 | for (unsigned int id = 0; id < cpu_count; ++id) {
|
|---|
| 91 | thread[id] = thread_create(test_thread, &call_cnt[id], TASK,
|
|---|
| 92 | THREAD_FLAG_NONE, "smp-call-test");
|
|---|
| 93 |
|
|---|
| 94 | if (thread[id]) {
|
|---|
| 95 | thread_wire(thread[id], &cpus[id]);
|
|---|
| 96 | ++running_thread_cnt;
|
|---|
| 97 | } else {
|
|---|
| 98 | TPRINTF("Failed to create thread on cpu%u.\n", id);
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | TPRINTF("Running %u wired threads.\n", running_thread_cnt);
|
|---|
| 103 |
|
|---|
| 104 | for (unsigned int i = 0; i < cpu_count; ++i) {
|
|---|
| 105 | if (thread[i] != NULL) {
|
|---|
| 106 | thread_ready(thread[i]);
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /* Wait for threads to complete. */
|
|---|
| 111 | for (unsigned int i = 0; i < cpu_count; ++i) {
|
|---|
| 112 | if (thread[i] != NULL) {
|
|---|
| 113 | thread_join(thread[i]);
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | TPRINTF("Threads finished. Checking number of smp_call()s.\n");
|
|---|
| 118 |
|
|---|
| 119 | size_t exp_calls = running_thread_cnt * ITERATIONS * EACH_CPU_INC_PER_ITER;
|
|---|
| 120 | size_t exp_calls_sum = exp_calls * cpu_count;
|
|---|
| 121 |
|
|---|
| 122 | bool ok = true;
|
|---|
| 123 | size_t calls_sum = 0;
|
|---|
| 124 |
|
|---|
| 125 | for (size_t i = 0; i < cpu_count; ++i) {
|
|---|
| 126 | if (thread[i] != NULL) {
|
|---|
| 127 | if (call_cnt[i] != exp_calls) {
|
|---|
| 128 | ok = false;
|
|---|
| 129 | TPRINTF("Error: %u instead of %u cpu%u's calls were"
|
|---|
| 130 | " acknowledged.\n", call_cnt[i], exp_calls, i);
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | calls_sum += call_cnt[i];
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | if (calls_sum != exp_calls_sum) {
|
|---|
| 138 | TPRINTF("Error: total acknowledged sum: %u instead of %u.\n",
|
|---|
| 139 | calls_sum, exp_calls_sum);
|
|---|
| 140 |
|
|---|
| 141 | ok = false;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | if (ok) {
|
|---|
| 145 | TPRINTF("Success: number of received smp_calls is as expected (%u).\n",
|
|---|
| 146 | exp_calls_sum);
|
|---|
| 147 | return NULL;
|
|---|
| 148 | } else
|
|---|
| 149 | return "Failed: incorrect acknowledged smp_calls.\n";
|
|---|
| 150 |
|
|---|
| 151 | }
|
|---|