[1f8c11f] | 1 | /*
|
---|
[8848276] | 2 | * Copyright (c) 2012 Adam Hraska
|
---|
[324e471] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[1f8c11f] | 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <print.h>
|
---|
| 30 | #include <debug.h>
|
---|
| 31 |
|
---|
[63e27ef] | 32 | #include <assert.h>
|
---|
[1f8c11f] | 33 | #include <test.h>
|
---|
| 34 | #include <smp/smp_call.h>
|
---|
| 35 | #include <cpu.h>
|
---|
| 36 | #include <macros.h>
|
---|
| 37 | #include <config.h>
|
---|
| 38 | #include <arch.h>
|
---|
[1066041] | 39 | #include <proc/thread.h>
|
---|
[1f8c11f] | 40 |
|
---|
[1b20da0] | 41 | /*
|
---|
| 42 | * Maximum total number of smp_calls in the system is:
|
---|
| 43 | * 162000 == 9^2 * 1000 * 2
|
---|
[1f8c11f] | 44 | * == MAX_CPUS^2 * ITERATIONS * EACH_CPU_INC_PER_ITER
|
---|
| 45 | */
|
---|
[d70fc74] | 46 | #define MAX_CPUS 9
|
---|
[1f8c11f] | 47 | #define ITERATIONS 1000
|
---|
| 48 | #define EACH_CPU_INC_PER_ITER 2
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | static void inc(void *p)
|
---|
| 52 | {
|
---|
[63e27ef] | 53 | assert(interrupts_disabled());
|
---|
[1f8c11f] | 54 |
|
---|
[1433ecda] | 55 | size_t *pcall_cnt = (size_t *)p;
|
---|
[1b20da0] | 56 | /*
|
---|
| 57 | * No synchronization. Tests if smp_calls makes changes
|
---|
| 58 | * visible to the caller.
|
---|
[1f8c11f] | 59 | */
|
---|
| 60 | ++*pcall_cnt;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | static void test_thread(void *p)
|
---|
| 65 | {
|
---|
[1433ecda] | 66 | size_t *pcall_cnt = (size_t *)p;
|
---|
[1f8c11f] | 67 | smp_call_t call_info[MAX_CPUS];
|
---|
[a35b458] | 68 |
|
---|
[d70fc74] | 69 | unsigned int cpu_count = min(config.cpu_active, MAX_CPUS);
|
---|
[a35b458] | 70 |
|
---|
[1f8c11f] | 71 | for (int iter = 0; iter < ITERATIONS; ++iter) {
|
---|
| 72 | /* Synchronous version. */
|
---|
| 73 | for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
|
---|
[1b20da0] | 74 | /*
|
---|
| 75 | * smp_call should make changes by inc() visible on this cpu.
|
---|
| 76 | * As a result we can pass it our pcall_cnt and not worry
|
---|
[1f8c11f] | 77 | * about other synchronization.
|
---|
| 78 | */
|
---|
| 79 | smp_call(cpu_id, inc, pcall_cnt);
|
---|
| 80 | }
|
---|
[a35b458] | 81 |
|
---|
[1b20da0] | 82 | /*
|
---|
| 83 | * Async calls run in parallel on different cpus, so passing the
|
---|
[1f8c11f] | 84 | * same counter would clobber it without additional synchronization.
|
---|
| 85 | */
|
---|
[1433ecda] | 86 | size_t local_cnt[MAX_CPUS] = { 0 };
|
---|
[a35b458] | 87 |
|
---|
[1f8c11f] | 88 | /* Now start asynchronous calls. */
|
---|
| 89 | for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
|
---|
| 90 | smp_call_async(cpu_id, inc, &local_cnt[cpu_id], &call_info[cpu_id]);
|
---|
| 91 | }
|
---|
[a35b458] | 92 |
|
---|
[1f8c11f] | 93 | /* And wait for all async calls to complete. */
|
---|
| 94 | for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
|
---|
| 95 | smp_call_wait(&call_info[cpu_id]);
|
---|
| 96 | *pcall_cnt += local_cnt[cpu_id];
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /* Give other threads a chance to run. */
|
---|
| 100 | thread_usleep(10000);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[d70fc74] | 104 | static size_t calc_exp_calls(size_t thread_cnt)
|
---|
| 105 | {
|
---|
| 106 | return thread_cnt * ITERATIONS * EACH_CPU_INC_PER_ITER;
|
---|
| 107 | }
|
---|
[1f8c11f] | 108 |
|
---|
| 109 | const char *test_smpcall1(void)
|
---|
| 110 | {
|
---|
| 111 | /* Number of received calls that were sent by cpu[i]. */
|
---|
[1433ecda] | 112 | size_t call_cnt[MAX_CPUS] = { 0 };
|
---|
[205832b] | 113 | thread_t *thread[MAX_CPUS] = { NULL };
|
---|
[a35b458] | 114 |
|
---|
[d70fc74] | 115 | unsigned int cpu_count = min(config.cpu_active, MAX_CPUS);
|
---|
[1f8c11f] | 116 | size_t running_thread_cnt = 0;
|
---|
| 117 |
|
---|
| 118 | TPRINTF("Spawning threads on %u cpus.\n", cpu_count);
|
---|
[a35b458] | 119 |
|
---|
[1f8c11f] | 120 | /* Create a wired thread on each cpu. */
|
---|
| 121 | for (unsigned int id = 0; id < cpu_count; ++id) {
|
---|
[1b20da0] | 122 | thread[id] = thread_create(test_thread, &call_cnt[id], TASK,
|
---|
[1433ecda] | 123 | THREAD_FLAG_NONE, "smp-call-test");
|
---|
[a35b458] | 124 |
|
---|
[1f8c11f] | 125 | if (thread[id]) {
|
---|
| 126 | thread_wire(thread[id], &cpus[id]);
|
---|
| 127 | ++running_thread_cnt;
|
---|
| 128 | } else {
|
---|
| 129 | TPRINTF("Failed to create thread on cpu%u.\n", id);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[d70fc74] | 133 | size_t exp_calls = calc_exp_calls(running_thread_cnt);
|
---|
| 134 | size_t exp_calls_sum = exp_calls * cpu_count;
|
---|
[a35b458] | 135 |
|
---|
[1b20da0] | 136 | TPRINTF("Running %zu wired threads. Expecting %zu calls. Be patient.\n",
|
---|
[1433ecda] | 137 | running_thread_cnt, exp_calls_sum);
|
---|
[1f8c11f] | 138 |
|
---|
| 139 | for (unsigned int i = 0; i < cpu_count; ++i) {
|
---|
| 140 | if (thread[i] != NULL) {
|
---|
| 141 | thread_ready(thread[i]);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
[a35b458] | 144 |
|
---|
[1f8c11f] | 145 | /* Wait for threads to complete. */
|
---|
| 146 | for (unsigned int i = 0; i < cpu_count; ++i) {
|
---|
| 147 | if (thread[i] != NULL) {
|
---|
| 148 | thread_join(thread[i]);
|
---|
[d70fc74] | 149 | thread_detach(thread[i]);
|
---|
[1f8c11f] | 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | TPRINTF("Threads finished. Checking number of smp_call()s.\n");
|
---|
[a35b458] | 154 |
|
---|
[1f8c11f] | 155 | bool ok = true;
|
---|
| 156 | size_t calls_sum = 0;
|
---|
[a35b458] | 157 |
|
---|
[1f8c11f] | 158 | for (size_t i = 0; i < cpu_count; ++i) {
|
---|
| 159 | if (thread[i] != NULL) {
|
---|
| 160 | if (call_cnt[i] != exp_calls) {
|
---|
| 161 | ok = false;
|
---|
[e8471b9] | 162 | TPRINTF("Error: %zu instead of %zu cpu%zu's calls were"
|
---|
[1433ecda] | 163 | " acknowledged.\n", call_cnt[i], exp_calls, i);
|
---|
[1b20da0] | 164 | }
|
---|
[1f8c11f] | 165 | }
|
---|
[a35b458] | 166 |
|
---|
[1f8c11f] | 167 | calls_sum += call_cnt[i];
|
---|
| 168 | }
|
---|
[a35b458] | 169 |
|
---|
[1f8c11f] | 170 | if (calls_sum != exp_calls_sum) {
|
---|
[d70fc74] | 171 | TPRINTF("Error: total acknowledged sum: %zu instead of %zu.\n",
|
---|
[1433ecda] | 172 | calls_sum, exp_calls_sum);
|
---|
[a35b458] | 173 |
|
---|
[1f8c11f] | 174 | ok = false;
|
---|
| 175 | }
|
---|
[a35b458] | 176 |
|
---|
[1f8c11f] | 177 | if (ok) {
|
---|
[d70fc74] | 178 | TPRINTF("Success: number of received smp_calls is as expected (%zu).\n",
|
---|
[1433ecda] | 179 | exp_calls_sum);
|
---|
[1f8c11f] | 180 | return NULL;
|
---|
| 181 | } else
|
---|
| 182 | return "Failed: incorrect acknowledged smp_calls.\n";
|
---|
[a35b458] | 183 |
|
---|
[1f8c11f] | 184 | }
|
---|