source: mainline/kernel/test/smpcall/smpcall1.c@ 903b5455

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 903b5455 was 8848276, checked in by Martin Decky <martin@…>, 11 years ago

avoid potentially unsafe use of a string literal

  • Property mode set to 100644
File size: 5.1 KB
Line 
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 <test.h>
33#include <smp/smp_call.h>
34#include <cpu.h>
35#include <macros.h>
36#include <config.h>
37#include <arch.h>
38#include <proc/thread.h>
39
40/*
41 * Maximum total number of smp_calls in the system is:
42 * 162000 == 9^2 * 1000 * 2
43 * == MAX_CPUS^2 * ITERATIONS * EACH_CPU_INC_PER_ITER
44 */
45#define MAX_CPUS 9
46#define ITERATIONS 1000
47#define EACH_CPU_INC_PER_ITER 2
48
49
50static void inc(void *p)
51{
52 ASSERT(interrupts_disabled());
53
54 size_t *pcall_cnt = (size_t*)p;
55 /*
56 * No synchronization. Tests if smp_calls makes changes
57 * visible to the caller.
58 */
59 ++*pcall_cnt;
60}
61
62
63static void test_thread(void *p)
64{
65 size_t *pcall_cnt = (size_t*)p;
66 smp_call_t call_info[MAX_CPUS];
67
68 unsigned int cpu_count = min(config.cpu_active, MAX_CPUS);
69
70 for (int iter = 0; iter < ITERATIONS; ++iter) {
71 /* Synchronous version. */
72 for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
73 /*
74 * smp_call should make changes by inc() visible on this cpu.
75 * As a result we can pass it our pcall_cnt and not worry
76 * about other synchronization.
77 */
78 smp_call(cpu_id, inc, pcall_cnt);
79 }
80
81 /*
82 * Async calls run in parallel on different cpus, so passing the
83 * same counter would clobber it without additional synchronization.
84 */
85 size_t local_cnt[MAX_CPUS] = {0};
86
87 /* Now start asynchronous calls. */
88 for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
89 smp_call_async(cpu_id, inc, &local_cnt[cpu_id], &call_info[cpu_id]);
90 }
91
92 /* And wait for all async calls to complete. */
93 for (unsigned cpu_id = 0; cpu_id < cpu_count; ++cpu_id) {
94 smp_call_wait(&call_info[cpu_id]);
95 *pcall_cnt += local_cnt[cpu_id];
96 }
97
98 /* Give other threads a chance to run. */
99 thread_usleep(10000);
100 }
101}
102
103static size_t calc_exp_calls(size_t thread_cnt)
104{
105 return thread_cnt * ITERATIONS * EACH_CPU_INC_PER_ITER;
106}
107
108const char *test_smpcall1(void)
109{
110 /* Number of received calls that were sent by cpu[i]. */
111 size_t call_cnt[MAX_CPUS] = {0};
112 thread_t *thread[MAX_CPUS] = { NULL };
113
114 unsigned int cpu_count = min(config.cpu_active, MAX_CPUS);
115 size_t running_thread_cnt = 0;
116
117 TPRINTF("Spawning threads on %u cpus.\n", cpu_count);
118
119 /* Create a wired thread on each cpu. */
120 for (unsigned int id = 0; id < cpu_count; ++id) {
121 thread[id] = thread_create(test_thread, &call_cnt[id], TASK,
122 THREAD_FLAG_NONE, "smp-call-test");
123
124 if (thread[id]) {
125 thread_wire(thread[id], &cpus[id]);
126 ++running_thread_cnt;
127 } else {
128 TPRINTF("Failed to create thread on cpu%u.\n", id);
129 }
130 }
131
132 size_t exp_calls = calc_exp_calls(running_thread_cnt);
133 size_t exp_calls_sum = exp_calls * cpu_count;
134
135 TPRINTF("Running %zu wired threads. Expecting %zu calls. Be patient.\n",
136 running_thread_cnt, exp_calls_sum);
137
138 for (unsigned int i = 0; i < cpu_count; ++i) {
139 if (thread[i] != NULL) {
140 thread_ready(thread[i]);
141 }
142 }
143
144 /* Wait for threads to complete. */
145 for (unsigned int i = 0; i < cpu_count; ++i) {
146 if (thread[i] != NULL) {
147 thread_join(thread[i]);
148 thread_detach(thread[i]);
149 }
150 }
151
152 TPRINTF("Threads finished. Checking number of smp_call()s.\n");
153
154 bool ok = true;
155 size_t calls_sum = 0;
156
157 for (size_t i = 0; i < cpu_count; ++i) {
158 if (thread[i] != NULL) {
159 if (call_cnt[i] != exp_calls) {
160 ok = false;
161 TPRINTF("Error: %zu instead of %zu cpu%zu's calls were"
162 " acknowledged.\n", call_cnt[i], exp_calls, i);
163 }
164 }
165
166 calls_sum += call_cnt[i];
167 }
168
169 if (calls_sum != exp_calls_sum) {
170 TPRINTF("Error: total acknowledged sum: %zu instead of %zu.\n",
171 calls_sum, exp_calls_sum);
172
173 ok = false;
174 }
175
176 if (ok) {
177 TPRINTF("Success: number of received smp_calls is as expected (%zu).\n",
178 exp_calls_sum);
179 return NULL;
180 } else
181 return "Failed: incorrect acknowledged smp_calls.\n";
182
183}
Note: See TracBrowser for help on using the repository browser.