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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#ifdef IDIOT_DECL_SECTION
#include <linux/kthread.h>
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/list.h>
#include <linux/kfifo.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include "npu-util-llq.h"
#define DEFAULT_LEN (2<<8)
/* basic */
struct ast_basic_data {
int seq;
void* data;
};
static void set_seq(int seq, struct ast_basic_data* pElem) {
// Atomic serial counter for auto-increasing sequence
static atomic_t incr_seq = ATOMIC_INIT(0);
if(pElem == NULL) {
/* Reset counter */
atomic_set(&incr_seq, seq);
return;
}
pElem->seq = 0;
if(seq == 0) {
pElem->seq = atomic_inc_return(&incr_seq);
} else {
pElem->seq = seq;
}
}
LLQ_DECLARE(llq_ast_test, struct ast_basic_data, DEFAULT_LEN);
static struct auto_sleep_thread* ast_thr;
struct ast_test_result_t {
int code;
int loc;
int serial;
};
static int pop_llq_do_task(struct auto_sleep_thread_param* data) {
struct ast_test_result_t* param = (struct ast_test_result_t*)(data->data);
struct ast_basic_data elem;
if(LLQ_GET(llq_ast_test, &elem) == 0) {
return 0;
}
if(elem.seq != param->serial) {
param->code = -2;
param->loc = param->serial;
}
(param->serial)++;
return 1;
}
static int pop_llq_check_work(struct auto_sleep_thread_param* data) {
return !(LLQ_IS_EMPTY(llq_ast_test));
}
static void AST_basic_signal(void) {
printk(KERN_INFO "NPU: %s : %s() invoked.\n", __FILE__, __func__);
if(ast_thr) {
auto_sleep_thread_signal(ast_thr);
} else {
printk(KERN_ERR "NPU: %s : %s() ast_thr is null.\n", __FILE__, __func__);
}
}
static struct auto_sleep_thread* AST_basic_setup(void) {
int ret;
struct auto_sleep_thread* ast_thr = NULL;
set_seq(0, NULL);
ast_thr = kmalloc(sizeof(*ast_thr), GFP_ATOMIC);
LLQ_REGISTER_TASK(llq_ast_test, AST_basic_signal);
if(ast_thr) {
ret = auto_sleep_thread_create(ast_thr, "AST_test_pop_llq", pop_llq_do_task, pop_llq_check_work, NULL);
if (ret) return NULL;
else return ast_thr;
} else {
return NULL;
}
}
static void AST_basic_teardown(struct auto_sleep_thread* ast_thr) {
auto_sleep_thread_terminate(ast_thr);
LLQ_PURGE(llq_ast_test);
kfree(ast_thr);
ast_thr = NULL;
}
static int AST_llq_enqueue_worker(void* data) {
struct ast_basic_data entry = {};
int delay = 0;
if(data != NULL) {
delay = *((int*)data);
}
/* Reset sequence counter */
set_seq(0, NULL);
while(!kthread_should_stop()) {
set_seq(0, &entry);
LLQ_PUT(llq_ast_test, entry);
if(delay) udelay(delay);
}
return 0;
}
#endif
TESTDEF(AST_02_simple_wakeup_01,
struct ast_basic_data entry = {};
struct ast_test_result_t result = {.code = 0, .loc = 0, .serial = 1};
struct auto_sleep_thread_param ast_param;
ast_thr = AST_basic_setup();
IDIOT_ASSERT_NOT(struct auto_sleep_thread*, %p, ast_thr, NULL);
ast_param.data = &result;
IDIOT_ASSERT(int, %d, auto_sleep_thread_start(ast_thr, ast_param), 0);
/* Wait until the thread sleep */
msleep(100);
/* Push entry */
set_seq(0, &entry);
IDIOT_MSG("test-put element\n");
LLQ_PUT(llq_ast_test, entry);
/* Wait until the thread sleep */
msleep(100);
printk(KERN_ERR "NPU: test-check condition\n");
IDIOT_ASSERT(int, %d, atomic_read(&(ast_thr->thr_state)), THREAD_STATE_SLEEPING);
IDIOT_ASSERT_NOT(int, %d, LLQ_IS_EMPTY(llq_ast_test), 0); /* Shall be empty */
/* check result*/
IDIOT_ASSERT(int, %d, result.loc, 0);
AST_basic_teardown(ast_thr);
)
TESTDEF(AST_03_termination_01,
struct ast_test_result_t result = {.code = 0, .loc = 0, .serial = 1};
struct auto_sleep_thread_param ast_param;
ast_thr = AST_basic_setup();
IDIOT_ASSERT_NOT(struct auto_sleep_thread*, %p, ast_thr, NULL);
ast_param.data = &result;
IDIOT_ASSERT(int, %d, auto_sleep_thread_start(ast_thr, ast_param), 0);
/* Wait until the thread sleep */
msleep(100);
IDIOT_ASSERT(int, %d, atomic_read(&(ast_thr->thr_state)), THREAD_STATE_SLEEPING);
/* Shutdown */
AST_basic_teardown(ast_thr);
)
TESTDEF(AST_4_termination_03,
struct ast_test_result_t result = {.code = 0, .loc = 0, .serial = 1};
struct task_struct* worker;
int check;
struct auto_sleep_thread_param ast_param;
ast_thr = AST_basic_setup();
IDIOT_ASSERT_NOT(struct auto_sleep_thread*, %p, ast_thr, NULL);
ast_param.data = &result;
IDIOT_ASSERT(int, %d, auto_sleep_thread_start(ast_thr, ast_param), 0);
/* Execute worker enqueue data */
worker = kthread_run(AST_llq_enqueue_worker, NULL, "LLQ_TEST_02_WORKER");
/* Wait while the counter increasing */
msleep(1000);
/* Check current counter */
check = result.serial;
IDIOT_ASSERT_NOT(int, %d, result.serial, 1);
/* Wait a little and terminate the thread */
msleep(10);
auto_sleep_thread_terminate(ast_thr);
IDIOT_ASSERT(int, %d, atomic_read(&(ast_thr->thr_state)), THREAD_STATE_TERMINATED);
IDIOT_ASSERT_NOT(int, %d, result.serial, check); /* Need to be increased */
check = result.serial;
/* Wait a little more */
msleep(100);
IDIOT_ASSERT(int, %d, result.serial, check); /* Should not be changed */
/* Tear down */
kthread_stop(worker);
/* AST is already terminated. Just empty the LLQ */
LLQ_PURGE(llq_ast_test);
kfree(ast_thr);
ast_thr = NULL;
)
TESTDEF(AST_05_check_race,
struct ast_test_result_t result = {.code = 0, .loc = 0, .serial = 1};
struct auto_sleep_thread_param ast_param = {};
struct ast_basic_data entry = {};
const int TEST_COUNT = 10000;
int i;
ast_thr = AST_basic_setup();
IDIOT_ASSERT_NEQ(ast_thr, NULL, %p);
ast_param.data = &result;
IDIOT_ASSERT_EQ(auto_sleep_thread_start(ast_thr, ast_param), 0, %d);
/* Enqueue data */
for(i = 0; i < TEST_COUNT; i++) {
entry.seq = i + 1;
while(LLQ_PUT(llq_ast_test, entry) == 0) {
udelay(1); // Flow control
}
}
/* Double check last entry's serial number */
IDIOT_ASSERT_EQ(entry.seq, TEST_COUNT, %d);
/* Wait while the task is done */
msleep(1000);
/* Make sure all the entries are processed */
IDIOT_ASSERT_EQ(result.serial, TEST_COUNT + 1, %d);
/* Shutdown */
AST_basic_teardown(ast_thr);
)
TESTDEF(AST_06_check_race_longtime_func,
struct ast_test_result_t result = {.code = 0, .loc = 0, .serial = 1};
struct auto_sleep_thread_param ast_param;
struct ast_basic_data entry = {};
const int TEST_COUNT = 10000;
int i;
ast_thr = AST_basic_setup();
IDIOT_ASSERT_NEQ(ast_thr, NULL, %p);
ast_param.data = &result;
IDIOT_ASSERT_EQ(auto_sleep_thread_start(ast_thr, ast_param), 0, %d);
/* Enqueue data */
for(i = 0; i < TEST_COUNT; i++) {
entry.seq = i + 1;
while(LLQ_PUT(llq_ast_test, entry) == 0);
msleep(10);
}
/* Double check last entry's serial number */
IDIOT_ASSERT_EQ(entry.seq, TEST_COUNT, %d);
/* Wait while the task is done */
msleep(1000);
/* Make sure all the entries are processed */
IDIOT_ASSERT_EQ(result.serial, TEST_COUNT + 1, %d);
/* Shutdown */
AST_basic_teardown(ast_thr);
)
|