1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2021, Intel Corporation.
*
* Sohil Mehta <[email protected]>
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <x86gprintrin.h>
#include <pthread.h>
#include <signal.h>
#include <sys/sysinfo.h>
#include <sys/wait.h>
#include "uintr_common.h"
unsigned int client_received;
unsigned int server_received;
unsigned int count = 100000;
unsigned int nprocs;
unsigned int start_cpu;
static void __attribute__((interrupt)) client_handler(struct __uintr_frame *ui_frame,
unsigned long long vector)
{
client_received = 1;
}
static void __attribute__((interrupt)) server_handler(struct __uintr_frame *ui_frame,
unsigned long long vector)
{
server_received = 1;
}
static void wait_for_interrupt(unsigned int *interrupt_received_flag)
{
/* TODO: Add alternate to test with usleep() */
while (!(*interrupt_received_flag))
cpu_relax();
}
static void client_process_bi(int server_fd, int sock)
{
int uipi_index;
int client_fd;
ssize_t size;
uipi_index = uintr_register_sender(server_fd, 0);
if (uipi_index < 0) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
exit(EXIT_FAILURE);
}
if (uintr_register_handler(client_handler, 0)) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
/* Create uintr_fd with vector 1 */
client_fd = uintr_create_fd(1, 0);
if (client_fd < 0) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
_stui();
/* Share client_fd */
if (sock_fd_write(sock, "1", 1, client_fd) != 1) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
do {
wait_for_interrupt(&client_received);
client_received = 0;
_senduipi(uipi_index);
} while (1);
}
static void test_process_ipi_bidirectional(int i)
{
int server_fd, client_fd, uipi_index, pid;
char buf[16];
ssize_t size;
int sv[2];
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET((start_cpu + (i % nprocs)) % get_nprocs(), &cpuset);
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) < 0) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
if (uintr_register_handler(server_handler, 0)) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
/* Create uintr_fd with vector 0 */
server_fd = uintr_create_fd(0, 0);
if (server_fd < 0) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
_stui();
printf("\tStarting Server-Client pair: %d\n", i);
pid = fork();
if (pid == 0) {
/* Child client process */
close(sv[0]);
CPU_ZERO(&cpuset);
CPU_SET((start_cpu + ((i + 1) % nprocs)) % get_nprocs(), &cpuset);
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
client_process_bi(server_fd, sv[1]);
exit(EXIT_SUCCESS);
}
close(sv[1]);
if (sock_fd_read(sv[0], buf, sizeof(buf), &client_fd) != 1) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
//Register server as sender
uipi_index = uintr_register_sender(client_fd, 0);
if (uipi_index < 0) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return;
}
do {
server_received = 0;
_senduipi(uipi_index);
wait_for_interrupt(&server_received);
} while (count--);
uintr_unregister_handler(0);
kill(pid, SIGKILL);
if (server_received)
printf("[OK]\tAll interrupts received: pair: %d\n", i);
else
printf("[FAIL]\tInterrupt not received: pair: %d\n", i);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
int num_forks, i, c, pairs;
int *pid = NULL;
if (!uintr_supported())
return EXIT_SUCCESS;
nprocs = get_nprocs();
pairs = nprocs * 2;
srand(time(NULL));
start_cpu = rand() % get_nprocs();
while ((c = getopt(argc, argv, "c:i:p:")) != EOF) {
switch (c) {
case 'c':
nprocs = atoi(optarg);
if (nprocs > get_nprocs())
nprocs = get_nprocs();
break;
case 'i':
count = atoi(optarg);
break;
case 'p':
pairs = atoi(optarg);
break;
default:
break;
}
}
printf("[RUN]\tContext switch IPC test\n");
pid = malloc(sizeof(int) * pairs);
if (!pid) {
printf("[SKIP]\t%s:%d\n", __func__, __LINE__);
return EXIT_SUCCESS;
}
printf("\tServer-Client pairs:%d Number of cpus:%d Iterations per pair:%d\n",
pairs, nprocs, count);
for (i = 0; i < pairs; i++) {
pid[i] = fork();
if (pid[i] == 0) {
test_process_ipi_bidirectional(i);
break;
}
}
for (i = 0; i < pairs; i++)
waitpid(pid[i], NULL, 0);
return EXIT_SUCCESS;
}
|