1 | /*
|
---|
2 | * Copyright (c) 2012 Adam Hraska
|
---|
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.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <print.h>
|
---|
30 | #include <debug.h>
|
---|
31 |
|
---|
32 | #include <assert.h>
|
---|
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>
|
---|
39 | #include <proc/thread.h>
|
---|
40 |
|
---|
41 | /*
|
---|
42 | * Maximum total number of smp_calls in the system is:
|
---|
43 | * 162000 == 9^2 * 1000 * 2
|
---|
44 | * == MAX_CPUS^2 * ITERATIONS * EACH_CPU_INC_PER_ITER
|
---|
45 | */
|
---|
46 | #define MAX_CPUS 9
|
---|
47 | #define ITERATIONS 1000
|
---|
48 | #define EACH_CPU_INC_PER_ITER 2
|
---|
49 |
|
---|
50 |
|
---|
51 | static void inc(void *p)
|
---|
52 | {
|
---|
53 | assert(interrupts_disabled());
|
---|
54 |
|
---|
55 | size_t *pcall_cnt = (size_t*)p;
|
---|
56 | /*
|
---|
57 | * No synchronization. Tests if smp_calls makes changes
|
---|
58 | * visible to the caller.
|
---|
59 | */
|
---|
60 | ++*pcall_cnt;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | static void test_thread(void *p)
|
---|
65 | {
|
---|
66 | size_t *pcall_cnt = (size_t*)p;
|
---|
67 | smp_call_t call_info[MAX_CPUS];
|
---|
68 |
|
---|
69 | unsigned int cpu_count = min(config.cpu_active, MAX_CPUS);
|
---|
70 |
|
---|
71 | for (int iter = 0; iter < ITERATIONS; ++iter) {
|
---|
72 | /* Synchronous version. */
|
---|
73 | for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
|
---|
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
|
---|
77 | * about other synchronization.
|
---|
78 | */
|
---|
79 | smp_call(cpu_id, inc, pcall_cnt);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Async calls run in parallel on different cpus, so passing the
|
---|
84 | * same counter would clobber it without additional synchronization.
|
---|
85 | */
|
---|
86 | size_t local_cnt[MAX_CPUS] = {0};
|
---|
87 |
|
---|
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 | }
|
---|
92 |
|
---|
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 |
|
---|
104 | static size_t calc_exp_calls(size_t thread_cnt)
|
---|
105 | {
|
---|
106 | return thread_cnt * ITERATIONS * EACH_CPU_INC_PER_ITER;
|
---|
107 | }
|
---|
108 |
|
---|
109 | const char *test_smpcall1(void)
|
---|
110 | {
|
---|
111 | /* Number of received calls that were sent by cpu[i]. */
|
---|
112 | size_t call_cnt[MAX_CPUS] = {0};
|
---|
113 | thread_t *thread[MAX_CPUS] = { NULL };
|
---|
114 |
|
---|
115 | unsigned int cpu_count = min(config.cpu_active, MAX_CPUS);
|
---|
116 | size_t running_thread_cnt = 0;
|
---|
117 |
|
---|
118 | TPRINTF("Spawning threads on %u cpus.\n", cpu_count);
|
---|
119 |
|
---|
120 | /* Create a wired thread on each cpu. */
|
---|
121 | for (unsigned int id = 0; id < cpu_count; ++id) {
|
---|
122 | thread[id] = thread_create(test_thread, &call_cnt[id], TASK,
|
---|
123 | THREAD_FLAG_NONE, "smp-call-test");
|
---|
124 |
|
---|
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 |
|
---|
133 | size_t exp_calls = calc_exp_calls(running_thread_cnt);
|
---|
134 | size_t exp_calls_sum = exp_calls * cpu_count;
|
---|
135 |
|
---|
136 | TPRINTF("Running %zu wired threads. Expecting %zu calls. Be patient.\n",
|
---|
137 | running_thread_cnt, exp_calls_sum);
|
---|
138 |
|
---|
139 | for (unsigned int i = 0; i < cpu_count; ++i) {
|
---|
140 | if (thread[i] != NULL) {
|
---|
141 | thread_ready(thread[i]);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
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]);
|
---|
149 | thread_detach(thread[i]);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | TPRINTF("Threads finished. Checking number of smp_call()s.\n");
|
---|
154 |
|
---|
155 | bool ok = true;
|
---|
156 | size_t calls_sum = 0;
|
---|
157 |
|
---|
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;
|
---|
162 | TPRINTF("Error: %zu instead of %zu cpu%zu's calls were"
|
---|
163 | " acknowledged.\n", call_cnt[i], exp_calls, i);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | calls_sum += call_cnt[i];
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (calls_sum != exp_calls_sum) {
|
---|
171 | TPRINTF("Error: total acknowledged sum: %zu instead of %zu.\n",
|
---|
172 | calls_sum, exp_calls_sum);
|
---|
173 |
|
---|
174 | ok = false;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (ok) {
|
---|
178 | TPRINTF("Success: number of received smp_calls is as expected (%zu).\n",
|
---|
179 | exp_calls_sum);
|
---|
180 | return NULL;
|
---|
181 | } else
|
---|
182 | return "Failed: incorrect acknowledged smp_calls.\n";
|
---|
183 |
|
---|
184 | }
|
---|