source: mainline/kernel/test/smpcall/smpcall1.c@ 4ec9ea41

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4ec9ea41 was e8471b9, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

smpcall: Fixed to compile for amd64.

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